settings = pt_GetStarterOptions();
$this->table_pt_post = $wpdb->prefix . "pt_post";
$this->PTRVERSION = PTRVERSION;
// add WP actions - not limited to is_admin() to be applied also in case of xmlrpc posts by external blogging application
add_action('publish_post', array(&$this, 'savePostImage'));
add_action('edit_post', array(&$this, 'savePostImage'));
add_action('save_post', array(&$this, 'savePostImage'));
add_action('wp_insert_post', array(&$this, 'savePostImage'));
add_action('wp_insert_post', array(&$this, 'savePostImage'));
add_action('private_to_published', array(&$this, 'savePostImage'));
add_action('delete_post', array(&$this, 'deletePostImage'));
// Header
if ('deactivate' != $_GET['action'] && stripos($_SERVER['REQUEST_URI'], 'post-thumb-options.php') !== false) {
add_action('admin_head', 'pt_admin_include_header');
}
// Plugin activation
add_action('activate_post-thumb/post-thumb.php', array(&$this, 'activate'));
// add option screen menu
add_action('admin_menu', array(&$this, 'options'));
// check if we need to upgrade
if ( $this->settings['version'] < $this->PTRVERSION ) {
// Execute installation
$this->install();
// Update version number in the options
$this->settings['version'] = $this->PTRVERSION;
}
}
/****************************************************************/
/* Plugin activation
/****************************************************************/
function activate() {
$this->install(false);
}
/****************************************************************/
/* Add post-thumb option
/****************************************************************/
function options() {
if (function_exists('add_options_page'))
add_options_page('Post Thumb Revisited', 'Post Thumb', 8, basename(__FILE__), array(&$this, 'MenuOptions'));
}
/****************************************************************/
/* Installation routine
/****************************************************************/
function install($show_results=true) {
global $wpdb;
// add charset & collate like wp core
$charset_collate = '';
if ( version_compare(mysql_get_server_info(), '4.1.0', '>=') ) {
if ( ! empty($wpdb->charset) )
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
if ( ! empty($wpdb->collate) )
$charset_collate .= " COLLATE $wpdb->collate";
}
// create post table if it doesn't exist
$tablename = $this->table_pt_post;
$found = false;
foreach ($wpdb->get_results("SHOW TABLES;", ARRAY_N) as $row) {
if ($row[0] == $tablename) {
$found = true;
break;
}
}
// Drop table if previous version is older than 2.1.4 (format has changed)
if ($found && $this->settings['version'] < '2.2') {
$res = $wpdb->query(" DROP TABLE ".$tablename);
$found = false;
}
if (!$found) {
$res = $wpdb->get_results("CREATE TABLE $tablename " . $this->tablestruct . $charset_collate);
$ptr2_options = pt_get_default_options();
$ptr2_options['version'] = $this->PTRVERSION;
update_option('post_thumbnail_settings', $ptr2_options);
$count_posts = $wpdb->get_var("SELECT COUNT(*) FROM ".$wpdb->posts." WHERE post_type <> 'attachment'");
$this->UpdatePostDB(0, $count_posts, $show_results);
}
}
/****************************************************************/
/* Creates all record in DB
/****************************************************************/
function UpdatePostDB($llimit, $hlimit, $show_results=true) {
global $wpdb;
if ($show_results) echo '
'.__('Posts: ', 'post-thumb-admin').$llimit.' - '.$hlimit.' '.__('updated', 'post-thumb-admin').'
';
$lines = $hlimit-$llimit;
$dbresults = $wpdb->get_results(" SELECT * FROM ".$wpdb->posts." WHERE post_type <> 'attachment' LIMIT ".$llimit.",".$lines);
foreach ($dbresults as $dbresult) :
// Saves post data
$this->StorePostData($dbresult);
endforeach;
unset($dbresults);
}
/****************************************************************/
/* save new list of post tags to database
/****************************************************************/
function savePostImage($id) {
// authorization
if ( !current_user_can('edit_post', $id) )
return $id;
// clear old values first
$this->deletePostImage($id);
// Retrieve post content as list
$post = &get_post($id);
// skip drafts
if ( !($post->post_status == 'publish' OR $post->post_status == 'static' OR $post->post_status == 'future'))
return $id;
// Saves post data
$this->StorePostData($post);
}
/*******************************************************************************/
/* Deletes a post record
/*******************************************************************************/
function GetMetacontent($id) {
// finds an attachement to the post
if ($this->settings['use_meta'] == 'true')
$metaContent = get_post_meta($id, 'pt_meta_thumb', true);
else
$metaContent = '';
return $metaContent;
}
/*******************************************************************************/
/* Deletes a post record
/*******************************************************************************/
function StorePostData($post) {
global $wpdb;
$p = new postThumb($post);
$metaContent = $this->GetMetacontent($p->post_id);
$post->post_content = $this->apply_filters($post->post_content);
// Check metacontent
if ($metaContent != '') {
$pattern1 = '/youtube=\((.*?)\)(.*?)/i';
$pattern2 = '/MEDIA=([0-9]+%?)/i';
if (preg_match($pattern1, $metaContent, $match)) {
$p->image = 'http://img.youtube.com/vi/'.$match[1].'/0.jpg';
$p->media = $match[1];
} elseif (preg_match($pattern2, $metaContent, $match)) {
$media = $wpdb->get_row("SELECT * FROM $wpdb->wordtube WHERE vid = $match[1] ");
if ($media) {
$p->image = htmlspecialchars(addslashes($media->image));
$p->media = htmlspecialchars(addslashes($media->file));
if (pt_is_flv($media->file)) {
$p->vwidth = $media->width;
$p->vheight = $media->height;
}
}
} else
$p->image = $metaContent;
}
// If no meta content detected
if ($p->image == '') {
// finds an image from the post content
if (preg_match('@
]*)\/>@si', $post->post_content, $matches)) {
// put matches into recognizable vars
$la = pt_parseAtributes($matches[0], array('src'));
$p->image = $la['src'];
// Prepare for RegEx
$img_src = str_replace(array("%", "|", "@", ")", "("), array("\%", "\|", "\@", "\)", "\("), $p->image);
// detects if the image is already linked to a thumbnail
$pattern = '%]*).(jpg|jpeg|png|gif)([^>]*)\>([^>]*)'.$img_src.'([^\<]*)\<\/a>%si';
if (preg_match($pattern,$post->post_content,$matches)) {
$la = pt_parseAtributes($matches[0], array('href'));
$p->image = $la['href'];
}
// detects if the image is linked to an url
$pattern = '%]*)>([^>]*)'.$img_src.'([^<]*)\<\/a>%i';
if (preg_match($pattern,$post->post_content,$matches)) {
$la = pt_parseAtributes($matches[0], array('href'));
$p->link = $la['href'];
}
}
// Search for wordTube MEDIA. Hope it won't change after that. Needs to be refreshed if it does.
if ($this->settings['hs_wordtube'] == 'true') {
$pattern = '/\[MEDIA=([0-9]+%?)\]/i';
if (preg_match($pattern, $post->post_content, $match)) {
$media = $wpdb->get_row("SELECT * FROM $wpdb->wordtube WHERE vid = $match[1] ");
if ($media) {
$p->image = htmlspecialchars(addslashes($media->image));
$p->media = htmlspecialchars(addslashes($media->file));
if (pt_is_flv($media->file)) {
$p->vwidth = $media->width;
$p->vheight = $media->height;
}
}
}
}
// Search for Youtube video.
if ($this->settings['hs_youtube'] == 'true') {
$pattern1 = '/\[youtube=\((.*?)\)(.*?)\]/i';
$pattern2 = '/\/i';
$pattern3 = '/\