PDA

View Full Version : How to define custom value in existing script



Libero
24 Feb 2011, 01:41 AM
Hi, all,
I have this script: (I`m sorry I`m putting it all here, but dont`t sure what`s important)

<?php
add_action('the_content','rpf_content_filter');
add_filter('wp_head', 'rpf_init');
$rpf_options = get_option('rpf_options');

function rpf_content_filter($content){
if(is_admin())
return $content;

$id = get_post_meta(get_the_ID(), 'rpf_feed_id', true);

if(!$id)
return $content;//might be normal content

global $rpf_options;
if($rpf_options['word_limit'] >0 )
$content = rpf_truncate($content, $rpf_options['word_limit']);


$sourcelink=get_post_meta(get_the_ID(), 'rpf_sourcepermalink', true);

if($sourcelink)
$link='<p>'.rpf_custom_template($sourcelink,$id).'</p>';

$content=$content.$link;

return $content;


}

function rpf_custom_template($sourcelink,$feed_id){



global $rpf_options;
$custom_template = $rpf_options['custom_template'];

if( false === strpos($custom_template, '%SOURCE_URL%' ) )
$custom_template .= ' <a href="%SOURCE_URL%">%SOURCE_URL%</a> ';



$feed = rpf_get_feed($feed_id);

$user = get_userdata($feed['author']);
$author = $user->nickname;

$category = get_cat_name($feed['category']);

$feed_url = $feed['url'];

$feed_name = $feed['name'];

$custom_template = str_replace(
array('%SOURCE_URL%','%AUTHOR%','%CATEGORY%','%FEED_URL%','%FEED_NAME%'),
array($sourcelink,$author,$category,$feed_url,$feed_name),
$custom_template

);

return $custom_template;


}
function rpf_truncate($text,$count) {
$count=intval($count);
if( $count<1 || substr_count($text,' ') < $count )
return $text;

$temp = explode(' ', $text);

$text = implode(' ', array_slice($temp, 0, $count));

return $text;

}
function rpf_init(){

global $rpf_options;

$now_time=time();

$interval=60*60*3;
$lastactive=$rpf_options['lastactive'];
if(!$lastactive){
$lastactive=$now_time;
$rpf_options['lastactive']=$lastactive;
update_option('rpf_options', $rpf_options);

}

if ( ($now_time - $lastactive ) >= $interval ) {

$rpf_options['lastactive'] = $now_time+$interval;
update_option('rpf_options', $rpf_options);
rpf_process_feeds();
}

}



function rpf_process_feeds(){

@set_time_limit(0);


$feeds=rpf_get_feeds();
if($feeds){

rpf_log("<b>----------Processing all feeds-------------</b>");
foreach($feeds as $feed){

rpf_process_feed($feed);

}
}else{

rpf_log("No feeds data found!");
}


}


function rpf_process_feed($feed){
@set_time_limit(0);
if(empty($feed['url'])){

return false;
}


rpf_log("Processing feed <b>{$feed['url']}</b>");

$lastactive = $feed['lastactive'];

$now=time();

$frequency = $feed['frequency']*60*60;

if( ($now - $lastactive) >= $frequency ){

rpf_update_feed_lastactive($feed['id'],$now);

}else{

rpf_log("It's not the time to update <b>{$feed['url']}</b>. <a href='http://www.wprssposter.com/faq.html#cronjob'>Explanation</a>");
return false;
}

global $rpf_options;

$max_items=$feed['max_items'];

if(empty($max_items)||!is_numeric($max_items))

$max_items=3;

$simplepie = rpf_fetch_feed($feed['url'],$max_items);
$error=$simplepie->error();

if($error){

rpf_log("Feed Error: <b>$error</b>");
return false;
}

$count = 0;

foreach($simplepie->get_items() as $item){
if( rpf_is_duplicate( $item->get_title() ) ){
rpf_log('Filtering duplicate post');
continue;
}
if(false == rpf_process_item($item,$feed))
continue;
$count++;
if($count == $max_items)
break;
}

if($count==0)
rpf_log("No new or qualified post for <b>{$feed['url']}</b> <a href='http://www.wprssposter.com/faq.html#newandqualified'>Explanation</a>");
else
rpf_log( "Fetch $count posts from <b>{$feed['url']}</b>" );


return true;


}
function rpf_process_item($item,$feed){


global $wpdb;

$title = $item->get_title();

$link = $item->get_permalink();

if(false !== strpos($link,'news.google.com')){
$link=urldecode(substr($link,strpos($link,'url=')+4));
}elseif(false !== strpos($link,'/**')){
$link=urldecode(substr($link,strpos($link,'/**')+3));

}

$content = rpf_full_feed($link);

if(!$content ){
//rpf_log("Cannot grab full content from <b>$link</b>");
return false;
}
$title=rpf_title_fix($title);

$content=rpf_content_fix($content);

if(empty($title)||empty($content)){

return false;
}

$content=rpf_parse_images($content,$item->get_base());
$meta = array(
'rpf_feed_id' => $feed['id'],
'rpf_sourcepermalink' => $link,

);

$rpf_date = date("Y-m-d H:i:s", time());
if($feed['publish_date'] === 'Publish Immediately'){

}elseif($feed['publish_date'] === 'RSS Publication Date'){

$rpf_date = $item->get_date("Y-m-d H:i:s");
}



rpf_insert_post( $title,$content,$feed['category'],$feed['tags'],$meta,$feed['post_status'],$rpf_date,$feed['author']);
rpf_log("<b>{$title}</b> added");
global $wpdb;
global $rpf_options;
$hash=rpf_item_hash($item->get_title());
$sql="insert into {$rpf_options['db']['post']} (hash) values ('{$hash}')";
$wpdb->query($sql);

return true;


}
function rpf_title_fix($title){
if($title && strpos($title,' - ')){

$backup=$title;
$backup=preg_replace('/([-])/','$1[D]',$backup);

$backup=explode('[D]',$backup);

if( strlen($backup[0])>10 || count($backup)>=2 )
unset($backup[count($backup)-1]);
else
return $title;

$title=trim(implode('',$backup),' - ');
}
return $title;

}

function rpf_content_fix($text){
preg_match_all('@(<a.+?href=\".+?\">)(.*?</a>)@',$text,$m);
$urls = $m[1];
if(count($urls)){
foreach($urls as $pos => $link){
if(false === stripos($link,'http://') && false === stripos($link,'https://')){

$text=str_replace($link,'',$text);

$text=str_replace($m[2][$pos],str_replace('</a>','',$m[2][$pos]),$text);
}
}
}
$text=preg_replace("/[&|#|&#]+[a-z0-9]+;/i","",$text);
$text=preg_replace('@<[dtrx][^>]*>@','',$text);
$text=preg_replace('@</[dtrx][^>]*>@','',$text);
return $text;

}
function rpf_parse_images($content,$link){

preg_match_all('/<img(.+?)src=\"(.+?)\"(.*?)>/', $content, $images);
$urls = $images[2];

if(count($urls)){

foreach($urls as $pos => $url){
$oldurl=$url;
$meta=parse_url($url);

if(!isset($meta['host'])){

$meta=parse_url($link);
$url=$meta['scheme'].'://'.$meta['host'].'/'.$url;

}

$newurl = rpf_cache_image($url);
if($newurl)
$content = str_replace($oldurl, $newurl, $content);
else
$content = str_replace($images[0][$pos],'',$content);
}
}
return $content;



}
function rpf_cache_image($url){
if( strpos($url, "icon_") !== FALSE)
return false;
global $rpf_options;
$contents = rpf_get_file($url);

if( !$contents )
return false;
$basename = basename($url);
$paresed_url = parse_url($basename);

$filename = substr(md5(time()), 0, 5) . '_' . $paresed_url['path'];

$cachepath = RPF_CACHE;
$pluginpath = RPF_URL_ROOT;
$real_cachepath=dirname(__FILE__).'/'.$cachepath;
if(is_writable( $real_cachepath ) ){

if($contents){

file_put_contents($real_cachepath . $filename, $contents);
$i=@exif_imagetype($real_cachepath . $filename);
if($i)
return $pluginpath . $cachepath . rawurlencode($filename);
}
}else{

rpf_log($real_cachepath . " directory is not writable" );

}

return false;

}
function rpf_insert_post($title,$content,$category=array(1),$tags_input='',$meta='',$post_status='publish',$r pf_date,$post_author=1){


$category=(array)$category;

if(!$rpf_date)
$rpf_date = time();


$postid = wp_insert_post(array(
'post_title' => $title,
'post_content' => $content,
'post_category' => $category,
'tags_input' => $tags_input,
'post_status' => $post_status,
'post_author' => $post_author,
"post_date" => get_date_from_gmt($rpf_date),
));
if($meta)
foreach($meta as $key => $value)
rpf_insert_post_meta($postid, $key, $value);


return $postid;

}


?>

So in line 229 I want to define one more value like this:

$content=rpf_parse_images($content,$item->get_base());
$meta = array(
'rpf_feed_id' => $feed['id'],
'rpf_sourcepermalink' => $link,
'preview' =>
I did it and it`s work, but the problem is that I want to insert here already existing value - from line 337 - variable $real_cachepath from functio rpf_cache_image. I have try to define it directly, but it don`t work.
Can you help me, please

resdog
24 Feb 2011, 07:42 AM
Try making the $real_cachpath variable a global variable:



global $real_cachepath = dirname(__FILE__).'/'.$cachepath;


I think the reason you can't call it now is because the scope is tied to the function only, so once the function is run, the variable disappears too. Making it global makes it available OUTSIDE the scope of the function.