0ورڈپریس مقامی Gravatar کے کیشے

میں نے لوگوں کے لئے دکھایا اوتار میرے بلاگ پر تبصرہ کرنے والے رکھنا پسند. ورڈپریس gravatar کی سروس natively کی تو یہ بہت آسان ہے کو چالو کرنے کے کی حمایت. تاہم یہ ایک سرمایہ کاری کے ساتھ آتا ہے - ایک اور ڈومین سے بیرونی فائلوں تک رسائی حاصل کرنے کے نئے کی شکل میں اضافی بوجھ وقت کے بہت اضافہ کر دیتی ہے DNS لک اپ, نئی SSL بنانا کنکشنز, وغیرہ. Would­n’t it be nice to have gravatars stored loc­ally and served from your own serv­er. ٹھیک ہے کہ کیا میں اب کچھ وقت کے لئے کر رہا ہوں ہے, آپ کو کیسے معلوم کرنا چاہتے ہیں تو, پر پڑھیں ...

In case you’re still won­der­ing just why you’d want to do this, let me offer a few more bene­fits — images served loc­ally can be com­pressed before serving them, for example ‑all my images are con­ver­ted into webp ver­sions, and any browser which sup­ports webp gets the webp ver­sion. This is in addi­tion to the reduc­tion from not hav­ing to con­nect to an extern­al serv­er. The loc­al resources can also have a cach­ing head­er set on them so that browsers will cache them. Gravatar does­n’t provide for a very long cache time.

I have cre­ated 3 cus­tom func­tions, جس میں صرف اپنے تھیم کی functions.php میں جگہ. The first is a cus­tom fil­ter for the nat­ive word­press get_avatar(). جب زور دیا کہ gravatar اور گوگل سے دوسرے گرپس اوتار, and the third cre­ates a daily cronjob which refreshes the gravatars — in case they’ve been changed, or a pre­vi­ously unavail­able one is now available.

You will need to do a little bit of tinker­ing to make it com­pat­ible with your theme — I use the “bones” frame­work as you’ll notice in the 3RD func­tion I’ve reused some code that it provided. You will also need to cre­ate, or cus­tom­ise the path to store the gravatars, which in my case are stored in the theme folder in the sub­path “/library/images/gravatars/”

/*********************************************\
* صرف مقامی اوتار استعمال get_avatar فلٹر *
\*********************************************/
تقریب bones_gravatar($اوتار, $id_or_email, $سائز, $پہلے سے طے شدہ, $ALT)
{
$root_path = get_template_directory_uri() . '/library/images/gravatars/';
$root_path_local = get_template_directory() . '/library/images/gravatars/';
$gravatar_path = $ root_path . 'default_avatar';
$gravatar_path_hidpi= 'data-gravatar-hidpi="'.$root_path . 'default_avatar-hidpi.png"';

//shamelessly reuse original code to get the e-mail address

$email = '';
اگر ( is_numeric($id_or_email) )
{
$ID = (INT) $id_or_email;
$صارف = get_userdata($ID);
اگر ( $صارف ) $ای میل = $ صارف>USER_EMAIL;
}
elseif ( is_object($id_or_email) )
{
// No avatar for pingbacks or trackbacks
$allowed_comment_types = apply_filters( 'get_avatar_comment_types', سرنی( 'comment' ) );
اگر ( ! خالی( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (سرنی) $allowed_comment_types ) ) جھوٹے واپس;
اگر ( !خالی($id_or_email->صارف کی شناخت) )
{
$ID = (INT) $id_or_email->صارف کی شناخت;
$صارف = get_userdata($ID);
اگر ( $صارف) $ای میل = $ صارف>USER_EMAIL;
}
elseif ( !خالی($id_or_email->comment_author_email) )
{
$ای میل = $ id_or_email->comment_author_email;
}
}
اور
{
$ای میل = $ id_or_email;
}

اگر ( !خالی($ای میل) )
{
$email_hash = MD5( strtolower( ٹرم( $ای میل ) ) );
اگر(file_exists($root_path_local . $email_hash . '.png'))
{
//if we have a local cache then use it
$gravatar_path= $root_path . $email_hash ;
$gravatar_path_hidpi= 'data-gravatar-hidpi="'.$root_path . $email_hash . '-hidpi.png"';
}
}
اگر($سائز >= 47) $avatar= '<IMG کلاس ="load-gravatar avatar avatar-'.$size.' photo" چوڑائی ="'.$size.'" اونچائی ="'.$size.'" ایسآرسی ="' . $gravatar_path . '-hidpi.png" ALT ="Gravatar کے" />';
else $avatar= '<IMG کلاس ="load-gravatar avatar avatar-'.$size.' photo" چوڑائی ="'.$size.'" اونچائی ="'.$size.'" ایسآرسی ="' . $gravatar_path . '.png" ALT ="Gravatar کے" ' . $gravatar_path_hidpi . ' />';

واپسی $ اوتار;
}

add_filter('get_avatar', 'bones_gravatar', 10, 5);






/***********************\
* Gravatar کے کیشے بنائیں *
\***********************/
تقریب grab_avatar($comment_id,$تبصرہ,$جھوٹے = ریفریش,$email='')
{
usleep(20);
$root_path = get_template_directory_uri() . '/library/images/gravatars/';
$root_path_local = get_template_directory() . '/library/images/gravatars/';
اگر($== جھوٹے ریفریش) $img_name = MD5( get_comment_author_email($comment_id) );
ورنہ $ img_name MD5 =( $ای میل );
اگر(!file_exists($root_path_local . $img_name . '.png') || $== سچ ریفریش)
{
اگر($== جھوٹے ریفریش) $bgauthemail = get_comment_author_email();
ورنہ $ bgauthemail = $ ای میل;
//try google first
$domain= explode("@",$bgauthemail);
اگر($ڈومین[1]=="gmail.com")
{
$چوہدری = curl_init();
curl_setopt($چودھری, CURLOPT_SSL_VERIFYPEER, جھوٹے);
curl_setopt($چودھری, CURLOPT_RETURNTRANSFER, سچ ہے);
curl_setopt($چودھری, CURLOPT_URL, "HTTPS://picasaweb.google.com/data/entry/api/user/" . $bgauthemail . "?ALT = JSON");
$نتیجہ = curl_exec($چودھری);
curl_close($چودھری);

$پر obj = json_decode($نتیجہ,سچ ہے);
$avatar_from_gmail = $ پر obj['entry']['gphoto$thumbnail']['$t'];

$توسیع = strrchr($avatar_from_gmail, '.');

$چوہدری = curl_init($avatar_from_gmail);
curl_setopt( $چودھری, CURLOPT_NOBODY, سچ ہے );
curl_setopt( $چودھری, CURLOPT_RETURNTRANSFER, جھوٹے );
curl_setopt( $چودھری, CURLOPT_HEADER, جھوٹے );
curl_setopt( $چودھری, CURLOPT_FOLLOWLOCATION, سچ ہے );
curl_setopt( $چودھری, CURLOPT_MAXREDIRS, 3 );
curl_exec( $چودھری );
$ہیڈرز = curl_getinfo( $چودھری );
curl_close( $چودھری );
اگر($سرنامے['http_code'] === 200)
{
$چوہدری = curl_init($avatar_from_gmail);
$FP = fopen کی($root_path_local . $img_name . '-hidpi' . $توسیع, 'wb');
curl_setopt($چودھری,CURLOPT_USERAGENT,'Mozilla/5.0 (ونڈوز NT 6.1; Win64; x64 کے) AppleWebKit / 537،36 (KHTML, چھپکلی کی طرح) Chrome/39.0.2171.62 Safari/537.36');
curl_setopt($چودھری, CURLOPT_FILE, $FP);
curl_setopt($چودھری, CURLOPT_HEADER, 0);

curl_exec($چودھری);
curl_close($چودھری);

fclose کے($FP);

$usegravatar = جھوٹے;

$small_image = wp_get_image_editor($root_path_local . $img_name . '-hidpi' . $توسیع);
اگر ( ! is_wp_error( $small_image ) )
{
//check it isn't a blank man image, اگر ایسا ہے, delete it
if (md5_file($root_path_local . $img_name . '-hidpi.jpg') == strtolower("686E5C46776BA0E5C488853C1C0B492C"))
{
//delete
unlink($root_path_local . $img_name . '-hidpi.jpg');
//try gravatar
$usegravatar=true;
}
ورنہ تو (md5_file($root_path_local . $img_name . '-hidpi.jpg') == strtolower("6D4083BE95FB32358A5110F5A83B9979"))
{
//delete
unlink($root_path_local . $img_name . '-hidpi.jpg');
//try gravatar
$usegravatar=true;
}
اور
{
//always convert to png because some plugins expect all gravatars to be pngs
$small_image->کو بچانے کے($root_path_local . $img_name . '-hidpi.png','image/png');
$small_image->سائز تبدیل کریں(40, 40, سچ ہے);
$small_image->set_quality( 10 );
$small_image->کو بچانے کے($root_path_local . $img_name . '.png','image/png');
اگر($توسیع ==".JPG") لنک ختم($root_path_local . $img_name . '-hidpi.jpg');
}
}
}

}
//check e-mail isnt a blank generic one
else if($bgauthemail!=="noemail@intensedebate.com" || $usegravatar == سچ)
{
$avatar_from_gravatar = "HTTPS://www.gravatar.com/avatar/" . $img_name . "?ے = 80&D = 404";
$چوہدری = curl_init($avatar_from_gravatar);
curl_setopt( $چودھری, CURLOPT_NOBODY, سچ ہے );
curl_setopt( $چودھری, CURLOPT_RETURNTRANSFER, جھوٹے );
curl_setopt( $چودھری, CURLOPT_HEADER, جھوٹے );
curl_setopt( $چودھری, CURLOPT_FOLLOWLOCATION, سچ ہے );
curl_setopt( $چودھری, CURLOPT_MAXREDIRS, 3 );
curl_exec( $چودھری );
$ہیڈرز = curl_getinfo( $چودھری );
curl_close( $چودھری );
اگر($سرنامے['http_code'] === 200)
{
$چوہدری = curl_init($avatar_from_gravatar);
$FP = fopen کی($root_path_local . $img_name . '-hidpi.png', 'wb');
curl_setopt($چودھری,CURLOPT_USERAGENT,'Mozilla/5.0 (ونڈوز NT 6.1; Win64; x64 کے) AppleWebKit / 537،36 (KHTML, چھپکلی کی طرح) Chrome/39.0.2171.62 Safari/537.36');
curl_setopt($چودھری, CURLOPT_FILE, $FP);
curl_setopt($چودھری, CURLOPT_HEADER, 0);

curl_exec($چودھری);
curl_close($چودھری);

fclose کے($FP);

$small_image = wp_get_image_editor($root_path_local . $img_name . '-hidpi.png');
اگر ( ! is_wp_error( $small_image ) )
{
$small_image->سائز تبدیل کریں(40, 40, سچ ہے);
$small_image->کو بچانے کے($root_path_local . $img_name . '.png');
}
}
}
}
}
ADD_ACTION('wp_insert_comment', 'grab_avatar');






/********************************************\
* روزانہ پس منظر میں Gravatar کے کیشے تازہ کریں *
\********************************************/
ADD_ACTION( 'wp', 'bones_setup_schedule' );
تقریب bones_setup_schedule() {
اگر ( ! wp_next_scheduled( 'bones_daily_event' ) ) {
wp_schedule_event( وقت(), 'daily', 'bones_daily_event');
}
}

ADD_ACTION( 'bones_daily_event', 'bones_refresh_gravatars' );
تقریب bones_refresh_gravatars()
{
//get list of gravatars somehow
global $wpdb;
$comment_author_emails = $ wpdb->get_results( "واضح منتخب(comment_author_email) FROM " . $wpdb->سابقہ . "تبصروں" );
ہر ایک کے لئے($$ کلید کے طور پر comment_author_emails => $ویل)
{
ہر ایک کے لئے($ویل $ ایک کے طور => $B)
{
grab_avatar('','',سچ ہے,$B);
}
}
}

جواب چھوڑیں