ptObject = $ptObject;
unset($ptObject);
$this->settings = $this->ptObject->settings;
// 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('delete_post', array(&$this, 'deletePostImage'));
// add option screen menu
if (is_admin()) {
add_action('admin_menu', array(&$this, 'pt_options'));
// check if we need to upgrade
if ( $this->settings['version'] < $this->PTRVERSION ) {
// Execute installation
$this->pt_install();
// Update version number in the options
$this->settings['version'] = $this->PTRVERSION;
// $this->UpdateOptions();
}
}
}
/****************************************************************/
/* Add post-thumb option
/****************************************************************/
function pt_options() {
if (function_exists('add_options_page'))
add_options_page('Post Thumb Revisited', 'Post Thumb', 8, basename(__FILE__), array(&$this, 'pt_MenuOptions'));
}
/****************************************************************/
/****************************************************************/
function pt_install() {
global $wpdb;
/* create tags table if it doesn't exist */
$tablename = $this->ptObject->table_pt_post;
$found = false;
foreach ($wpdb->get_results("SHOW TABLES;", ARRAY_N) as $row) {
if ($row[0] == $tablename) {
$found = true;
break;
}
}
if ($found && $this->settings['version'] < '2.1.4') {
$res = $wpdb->query(" DROP TABLE ".$tablename);
$found = false;
}
if (!$found) {
$res = $wpdb->get_results("CREATE TABLE $tablename " . $this->tablestruct);
$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);
$this->UpdatePostDB(0, $count_posts);
}
}
/****************************************************************/
/* Creates all record in DB
/****************************************************************/
function UpdatePostDB($llimit, $hlimit) {
global $wpdb;
echo '
'.__('Posts: ', 'post-thumb').$llimit.' - '.$hlimit.' '.__('updated', 'post-thumb').'
';
$lines = $hlimit-$llimit;
$dbresults = $wpdb->get_results(" SELECT * FROM ".$wpdb->posts." 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'))
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);
if ($meta_content != '')
$p->image = tb_thumb_absolute($meta_content);
// finds an image from the post content
elseif (preg_match('/
/i', $post->post_content, $matches)) {
// put matches into recognizable vars
$p->image = tb_thumb_absolute($matches[2]);
// detects if the image is already linked to a thumbnail
$img_src = str_replace('/', '\/', $matches[2]);
$pattern = '/(.*?)
/i';
if (preg_match($pattern,$post->post_content,$matches))
$p->image = tb_thumb_absolute($matches[2].'.'.$matches[3]);
}
// 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] ");
$p->image = $media->image;
$p->media = $media->file;
}
}
// Search for Youtube video.
if ($this->settings['hs_youtube'] == 'true') {
$pattern1 = '/\[youtube=\((.*?)\)(.*?)\]/i';
$pattern2 = '/\/i';
$pattern3 = '/\