<?php
add_action('publish_post', 'fbshare_post_facebook', 10, 2);
add_action( 'admin_menu', 'register_my_custom_menu_page' );
function register_my_custom_menu_page() {
add_menu_page( 'Facebookshare', 'FacebookShare', 'manage_options', 'facebookshare/fb-admin.php', '', plugins_url( 'facebookshare/images/facebookshare.png' ), 75 );
}
add_action( 'publish_post', 'fbshare_post_facebook', 10, 2);
function fbshare_post_facebook($ID, $post) {
require_once ('facebook-php-sdk/src/facebook.php');
require_once ('facebook-php-sdk/autoload.php');
require_once( 'Facebook/FacebookSession.php' );
date_default_timezone_set('UTC');
$api = get_option('fbshare_appID');
$secret = get_option('fbshare_appSecret');
$pageId = get_option('fbshare_pageID');
$token = get_option('fbshare_token');
$title = get_the_title($post);
$link = get_permalink($ID);
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $ID ), 'large' );
if($thumbnail==false) {
$image = false;
}else{
$image = $thumbnail[0];
}
$api_url= plugins_url().'/facebookshare/fb-cron.php';
if(get_option("fb_show_type")==0|| get_option("fb_show_type")==2) {
$post_with_link = get_option("fb_show_type");
if(get_option("fb_show_type")==0) {
$title.="
";
$title .= strip_tags(gw_filter_content(get_post_field('post_content', $ID)));
$title = preg_replace("/?[a-z0-9]+;/i", "", $title);
}
$post_to_facebook = share_to_facebook($title, $link, $api, $secret, $token, $pageId, $api_url, $image, $post_with_link);
}elseif (get_option("fb_show_type")==1 || get_option("fb_show_type")==3) {
$post_with_link = get_option("fb_show_type");
if(get_option("fb_show_type")==1) {
$title.="
";
$title .= strip_tags(gw_filter_content(get_post_field('post_content', $ID)));
$title = preg_replace("/?[a-z0-9]+;/i", "", $title);
}
if($post->post_modified_gmt ==$post->post_date_gmt ) {
$post_to_facebook = share_to_facebook($title, $link, $api, $secret, $token, $pageId, $api_url, $image, $post_with_link);
}
}
}
function gw_filter_content($content) {
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
function share_to_facebook($title, $link, $api, $secret, $token, $pageId, $api_url, $image, $post_with_link) {
$url = $api_url;
$params = array(
"message" => $title,
"link" => $link,
"api" => $api,
"token" => $token,
"pageId" => $pageId,
"secret" => $secret,
"redirect" =>$api_url,
"image" => $image,
"post_with_link"=>$post_with_link
);
$url .= '?' . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$data = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status == 200) {
$data =["status"=>"success"];
} else {
$data =["status"=>"failed"];
}
return json_encode($data);
}
function get_facebook_token($client_id, $client_secret, $code, $redirect_uri) {
$url = 'https:
$params = array(
"client_id" => $client_id,
"client_secret" => $client_secret,
"code" => $code,
"redirect_uri" => $redirect_uri
);
$url .= '?' . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$data = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ($data);
}