now = $this->date_expl(gmdate('Y-m-d H:i:59',time())); // Insert the menu and options add_option('post_thumbnail_settings','','Post Thumb Revisited Options'); $this->table_pt_post = $wpdb->prefix . "pt_post"; $this->settings = pt_GetStarterOptions(); $this->wt_path = $this->settings['wt_path']; // defines constants define('POSTTHUMB_URLPATH', str_replace($this->settings['full_domain_name'], '/',SITEURL).'/wp-content/plugins/' . PT_PLUGIN_BASENAME.'/'); define('POSTTHUMB_USE_HS', $this->settings['hs_use']=='true'); define('POSTTHUMB_USE_TB', $this->settings['tb_use']=='true'); define('POSTTHUMB_USE_SB', $this->settings['sb_use']=='true'); // call classes if (POSTTHUMB_USE_HS) require(PT_ABSPATH . 'lib/post-thumb-highslide.php'); if (POSTTHUMB_USE_TB) require(PT_ABSPATH . 'lib/post-thumb-thickbox.php'); if (POSTTHUMB_USE_SB) require(PT_ABSPATH . 'lib/post-thumb-smoothbox.php'); // Wordtube initialization if ($this->settings['hs_wordtube'] == 'true' || $this->settings['wt_media'] == 'true') { $wpdb->wordtube = $wpdb->prefix . 'wordtube'; $wpdb->wordtube_med2play = $wpdb->prefix . 'wordtube_med2play'; $this->InitWordTube(); } if (stripos($_SERVER['REQUEST_URI'], 'wp-admin') !== false) { if (has_to_be_loaded()) { include ( PT_ABSPATH . '/post-thumb-options.php' ); $PTAdmin = new PostThumbAdmin(); } } else { add_action('wp_head', array(&$this, 'include_header')); if (is_feed()) { $this->settings['hs_use']='false'; $this->settings['tb_use']='false'; $this->settings['hs_post']='true'; } $this->GetPostList(); } } /***********************************************************************************/ /* Retrieve posts informations /***********************************************************************************/ function GetPostList () { global $wpdb; if($wpdb->get_var("show tables like '$this->table_pt_post'") == $this->table_pt_post) { $select = " SELECT * FROM ".$this->table_pt_post." ORDER BY post_id DESC"; // To limit memory usage, uncomment the following line. // $select .= " LIMIT ".$this->LoadPostLimit; $dbresults = $wpdb->get_results($select); if ($dbresults) { foreach ($dbresults as $dbresult) : $temp = stripslashes($dbresult->body); $unser_temp=unserialize($temp); if ($this->is_published($unser_temp['date'])) $this->post_array[$dbresult->post_id] = $temp; endforeach; } $this->post_nb = count($this->post_array); unset($dbresults); unset($unser_temp); unset($temp); } } /***********************************************************************************/ /* format date /***********************************************************************************/ function date_expl($date) { $date = str_replace('-', '', $date); $date = str_replace(':', '', $date); return explode(' ', $date); } /***********************************************************************************/ /* Returns true if post date before now /***********************************************************************************/ function is_published ($date) { $date = $this->date_expl(mysql2date('Y-m-d H:i:59', $date)); if ($date[0] > $this->now[0]) return false; if (($date[0] == $this->now[0]) && ($date[1] > $this->now[1])) return false; return true; } /***********************************************************************************/ /* Get thumbnail. Loop function. /***********************************************************************************/ function GetPostData ($key) { if (array_key_exists($key, $this->post_array)) return unserialize($this->post_array[$key]); else return ''; } /***********************************************************************************/ /* Get thumbnail. Loop function. /***********************************************************************************/ function GetThumb ($arg='') { global $post; $new_args = pt_parse_arg($arg); if (isset($new_args['OBJECT'])) $object = ($new_args['OBJECT']==1); else $object = false; setup_postdata($post); return $this->GetSingleThumb ($post, $arg, '', '', $this->GetPostData($post->ID), $object); } /***********************************************************************************/ /* Prepares thumbnail for display. /***********************************************************************************/ function GetSingleThumb ($post, $arg='', $before='', $after='', $post_array='', $object=false) { $path = pathinfo($post_array['media_url']); if ($post_array['media_url'] != '' || $post_array['image_url'] != '') { $p = new pt_post_thumbnail( $this->settings, $post, $arg, $post_array['image_url'], $post_array['media_url'], $post_array['permalink'], $post_array['title'], $post_array['date'], $post_array['author'], $post_array['link'], true, $post_array['vwidth'], $post_array['vheight'] ); $post_link = $p->GetImgHTML(); if ($object) { $post_obj = new PTObject($post_array); $post_obj->thumb_url = $p->thumb_url; $post_obj->thumb_html = $post_link; } unset ($p); } else { $p = new pt_post_thumbnail($this->settings, $post, $arg); $post_link = $p->GetImgHTML(); if ($object) { $post_obj = new PTObject($this->GetPostData($post->ID)); $post_obj->thumb_url = $p->thumb_url; $post_obj->thumb_html = $post_link; } unset ($p); } if ($object) return $post_obj; if (!empty($post_link)) return $before.$post_link.$after; return ''; } /***********************************************************************************/ /* Returns recent posts /***********************************************************************************/ function GetTheRecentThumbs ($arg='', $beforeli='', $afterli='', $before='', $after='') { // Create a query object to retrieve posts $my_query = new WP_Query(); $new_args = pt_parse_arg($arg); // Retrieves specific parameters if (isset($new_args['OBJECT'])) $object = ($new_args['OBJECT']==1); else $object = false; if (isset($new_args['MEDIA'])) $media = $new_args['MEDIA']; else $media = 'all'; if (isset($new_args['LIMIT'])) { $limit = $new_args['LIMIT']; settype($limit,"integer"); } else $limit = 8; if (isset($new_args['OFFSET'])) { $offset = $new_args['OFFSET']; settype($offset,"integer"); } else $offset = 0; if (isset($new_args['CATEGORY'])) { $cat_ID = $new_args['CATEGORY'];$catid = explode(',', $cat_ID);$cat_arg = '&cat='.$cat_ID; } else {$cat_arg = '';$cat_ID='';$catid=array();} $content = $before; $l = $limit+$offset; $post_obj = array(); // Retrieve only images if ($media == '0') { $i = 1; foreach ($this->post_array as $key => $post_arrayS) : $post_array = $this->GetPostData($key); if ($i > $l) break; if ($i > $offset) { if ($post_array['media_url'] == '' && $this->in_category($post_array, $catid)) { if ($object) $post_obj[]= $this->GetSingleThumb ('', $arg, $beforeli, $afterli, $post_array, true); else $content .= $this->GetSingleThumb ('', $arg, $beforeli, $afterli, $post_array); $i++; } } else { if ($post_array['media_url'] == '' && $this->in_category($post_array, $cat_ID)) $i++; } endforeach; } // Retrieve non-youtube media elseif ($media == '1') { $i = 1; foreach ($this->post_array as $key => $post_arrayS) : $post_array = $this->GetPostData($key); if ($i > $l) break; if ($i > $offset) { if ($post_array['media_url'] != '' && $this->in_category($post_array, $catid)) { $path = pathinfo($post_array['media_url']); if ($path['extension'] != '') { if ($object) $post_obj[]= $this->GetSingleThumb ('', $arg, $beforeli, $afterli, $post_array, true); else $content .= $this->GetSingleThumb ('', $arg, $beforeli, $afterli, $post_array); $i++; } } } else { if ($post_array['media_url'] != '' && $this->in_category($post_array, $catid)) { $path = pathinfo($post_array['media_url']); if ($path['extension'] != '') { $i++; } } } endforeach; } // Retrieve youtube media elseif ($media == '2') { $i = 1; foreach ($this->post_array as $key => $post_arrayS) : $post_array = $this->GetPostData($key); if ($i > $l) break; if ($i > $offset) { if ($post_array['media_url'] != '' && $this->in_category($post_array, $catid)) { $path = pathinfo($post_array['media_url']); if ($path['extension'] == '') { if ($object) $post_obj[]= $this->GetSingleThumb ('', $arg, $beforeli, $afterli, $post_array, true); else $content .= $this->GetSingleThumb ('', $arg, $beforeli, $afterli, $post_array); $i++; } } } else { if ($post_array['media_url'] != '' && $this->in_category($post_array, $catid)) { $path = pathinfo($post_array['media_url']); if ($path['extension'] == '') $i++; } } endforeach; } // Retrieve all-media elseif ($media == '3') { $i = 1; foreach ($this->post_array as $key => $post_arrayS) : $post_array = $this->GetPostData($key); if ($i > $l) break; if ($i > $offset) { if ($post_array['media_url'] != '' && $this->in_category($post_array, $catid)) { if ($object) $post_obj[]= $this->GetSingleThumb ('', $arg, $beforeli, $afterli, $post_array, true); else $content .= $this->GetSingleThumb ('', $arg, $beforeli, $afterli, $post_array); $i++; } } else { if ($post_array['media_url'] != '' && $this->in_category($post_array, $catid)) $i++; } endforeach; } else { $posts = $my_query->query('showposts='.$limit.'&offset='.$offset.$cat_arg); foreach ($posts as $post) : $post_array = $this->GetPostData($post->ID); if ($object) $post_obj[]= $this->GetSingleThumb ('', $arg, $beforeli, $afterli, $post_array, true); else $content .= $this->GetSingleThumb ($post, $arg, $beforeli, $afterli, $post_array); endforeach; unset ($my_query); unset ($posts); } if ($object) return $post_obj; $content .= $after; return $content; } /****************************************************************/ /* Returns true if post is in category id /****************************************************************/ function in_category($post_array, $catid='') { if ($catid[0] == '') return true; if ($catid[0] < 0) { $catid= explode('-', implode('', $catid)); return (count(array_intersect($catid, unserialize($post_array['categories']))) == 0); } else return (count(array_intersect($catid, unserialize($post_array['categories']))) != 0); } /***********************************************************************************/ /* Return random thumbnails. /* /* LIMIT: number of thumbnail to display. Default is 1. /***********************************************************************************/ function GetRandomThumb ($arg='', $beforeli='', $afterli='', $before='', $after='') { $new_args = pt_parse_arg($arg); // Retrieves specific parameters if (isset($new_args['OBJECT'])) $object = ($new_args['OBJECT']==1); else $object = false; if (isset($new_args['LIMIT'])) { $limit = $new_args['LIMIT']; settype($limit,"integer"); } else $limit = 1; if (isset($new_args['CATEGORY'])) $catid = explode(',', $new_args['CATEGORY']); else $catid =array(); if (isset($new_args['ALTAPPEND'])) $altappend = $new_args['ALTAPPEND']; else $altappend = 'azerty123456789'; if (isset($new_args['BASENAME'])) $basename = ($new_args['BASENAME'] == 1); else $basename = false; $posts = $this->RandomId($limit, $catid); $random_str = $before; $random_obj = array(); if (count($posts) == 1) { if ($object) $random_obj[] = $this->GetSingleThumb ('', $arg.'&ALTAPPEND='.$altappend, $beforeli, $afterli, $this->GetPostData($posts), true); else $random_str = $this->GetSingleThumb ('', $arg.'&ALTAPPEND='.$altappend, $beforeli, $afterli, $this->GetPostData($posts)); } else { $i = 1; foreach ($posts as $post) : if ($basename) $add = $i; else $add = ''; if ($object) $random_obj[] = $this->GetSingleThumb ('', $arg.'&ALTAPPEND='.$altappend.$add, $beforeli, $afterli, $this->GetPostData($post), true); else $random_str .= $this->GetSingleThumb ('', $arg.'&ALTAPPEND='.$altappend.$add, $beforeli, $afterli, $this->GetPostData($post)); $i++; endforeach; } $random_str .= $after; if ($object) return $random_obj; return $random_str; } /****************************************************************/ /* Returns displayable post content /****************************************************************/ function RandomId($limit=1, $catid='') { if ($catid[0] == '') { if ($this->post_nb < $limit) $limit = $this->post_nb; $ret = array_rand ($this->post_array, $limit); } else { $rand = array(); $i = 0; foreach ($this->post_array as $key => $post_arrayS) : $post_array = $this->GetPostData($key); if ($this->in_category($post_array, $catid)) { $rand[$key]=$post_array; $i++; } endforeach; if ($i < $limit) $limit = $i; $ret = array_rand ($rand, $limit); unset($rand); } return $ret; } /****************************************************************/ /* Returns displayable post content /****************************************************************/ function TheExcerpt($earg='', $arg='', $addstr='') { global $post; setup_postdata($post); $new_args = pt_parse_arg($earg); // Retrieves specific parameters if (isset($new_args['SHOW'])) $show = strtoupper($new_args['SHOW']); else $show = 'ITAE'; if (isset($new_args['ITAG'])) $itag = $new_args['ITAG']; else $itag = ''; if (isset($new_args['TTAG'])) $ttag = $new_args['TTAG']; else $ttag = 'h2'; if (isset($new_args['ETAG'])) $etag = $new_args['ETAG']; else $etag = 'p'; $ret_str = ''; while ($show <>'') : $option = substr($show, 0, 1); switch ($option) : case 'I': $before = '<'.$itag.'>'; $after = ''.$itag.'>'; $p = new pt_post_thumbnail($this->settings, $post, $arg); $ret = $p->GetImgHTML(); unset ($p); break; case 'T': $before = '<'.$ttag.'>'; $after = ''.$ttag.'>'; $ret = ''.get_the_title().''; break; case 'A': $before = ''; $after = ''; $ret = $addstr; break; case 'E': $before = '<'.$etag.'>'; $after = ''.$etag.'>'; $post->post_content = exclude_regex($post->post_content); $ret = get_pt_excerpt($earg); break; default : $before = ''; $after = ''; $ret = ''; endswitch; $ret_str .= $before.$ret.$after; $show = substr($show, 1); endwhile; // Return everything return $ret_str; } /****************************************************************/ /* Add post-thumb option TITLE /****************************************************************/ function ReturnOption ($option, $sep) { switch ($option) : case 'A': if ($this->default_author == '') $ret = "\n\t".'