/facebookshare.php

  1. <?php 
  2. /** 
  3. Plugin Name: Facebookshare - Share posts to Facebook page automatically 
  4. Version: 1.0.1 
  5. Plugin URI: http://www.lazystudentblog.com 
  6. Description: Auto post to facebook any new posts - By Truc Truong  
  7. Author: Truc Truong  
  8. Author URI: http://www.lazystudentblog.com 
  9. */ 
  10.  
  11.  
  12. add_action('publish_post', 'fbshare_post_facebook', 10, 2); 
  13. add_action( 'admin_menu', 'register_my_custom_menu_page' ); 
  14.  
  15. function register_my_custom_menu_page() { 
  16. add_menu_page( 'Facebookshare', 'FacebookShare', 'manage_options', 'facebookshare/fb-admin.php', '', plugins_url( 'facebookshare/images/facebookshare.png' ), 75 );  
  17.  
  18. add_action( 'publish_post', 'fbshare_post_facebook', 10, 2); 
  19.  
  20. function fbshare_post_facebook($ID, $post) { 
  21.  
  22.  
  23. // Load FB API 
  24. require_once ('facebook-php-sdk/src/facebook.php'); 
  25. require_once ('facebook-php-sdk/autoload.php'); 
  26. require_once( 'Facebook/FacebookSession.php' ); 
  27.  
  28. // Set UTC time for Feed 
  29. date_default_timezone_set('UTC'); 
  30.  
  31.  
  32. // My facebook api 
  33. $api = get_option('fbshare_appID'); 
  34. $secret = get_option('fbshare_appSecret'); 
  35. $pageId = get_option('fbshare_pageID'); 
  36. $token = get_option('fbshare_token'); 
  37.  
  38.  
  39. $title = get_the_title($post); 
  40.  
  41. $link = get_permalink($ID); 
  42.  
  43. $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $ID ), 'large' ); 
  44.  
  45. if($thumbnail==false) { 
  46.  
  47. $image = false; 
  48. }else{ 
  49. $image = $thumbnail[0]; 
  50.  
  51. // $meta_values = get_post_meta($ID); 
  52. // if($meta_values['post_to_facebook'][0]==1) { 
  53.  
  54. $api_url= plugins_url().'/facebookshare/fb-cron.php'; 
  55. //Check if posts are newly created or not 
  56. //Share all posts ==0 
  57. if(get_option("fb_show_type")==0|| get_option("fb_show_type")==2) { 
  58. $post_with_link = get_option("fb_show_type"); 
  59. //Get the content 
  60. if(get_option("fb_show_type")==0) { 
  61. $title.=" 
  62. "; 
  63. $title .= strip_tags(gw_filter_content(get_post_field('post_content', $ID))); 
  64. $title = preg_replace("/&#?[a-z0-9]+;/i", "", $title); 
  65. // Call the fb-cron.php with POST method including some parameters 
  66. $post_to_facebook = share_to_facebook($title, $link, $api, $secret, $token, $pageId, $api_url, $image, $post_with_link); 
  67.  
  68. }elseif (get_option("fb_show_type")==1 || get_option("fb_show_type")==3) { 
  69. $post_with_link = get_option("fb_show_type"); 
  70. //Get the content 
  71. if(get_option("fb_show_type")==1) { 
  72. $title.=" 
  73. "; 
  74. $title .= strip_tags(gw_filter_content(get_post_field('post_content', $ID))); 
  75. $title = preg_replace("/&#?[a-z0-9]+;/i", "", $title); 
  76. if($post->post_modified_gmt ==$post->post_date_gmt ) { 
  77. $post_to_facebook = share_to_facebook($title, $link, $api, $secret, $token, $pageId, $api_url, $image, $post_with_link); 
  78. }  
  79. //dd($post_to_facebook); 
  80. function gw_filter_content($content) { 
  81. $content = apply_filters('the_content', $content); 
  82. $content = str_replace(']]>', ']]>', $content); 
  83. return $content; 
  84.  
  85.  
  86.  
  87.  
  88. /******** 12-12-2014 ********* 
  89. * Post to facebook function 
  90. * @param some nesscessary infomation to post to fb  
  91. * @return JSON failed or success 
  92. * Ask TRUC if you need help with this code 
  93. *******************************/ 
  94. function share_to_facebook($title, $link, $api, $secret, $token, $pageId, $api_url, $image, $post_with_link) { 
  95.  
  96. /** Script URL */ 
  97. $url = $api_url; 
  98.  
  99. /** $_GET Parameters to Send */ 
  100. $params = array( 
  101. "message" => $title,  
  102. "link" => $link,  
  103. "api" => $api,  
  104. "token" => $token,  
  105. "pageId" => $pageId,  
  106. "secret" => $secret,  
  107. "redirect" =>$api_url,  
  108. "image" => $image,  
  109. "post_with_link"=>$post_with_link 
  110. ); 
  111.  
  112. /** Update URL to container Query String of Paramaters */ 
  113. $url .= '?' . http_build_query($params); 
  114.  
  115. /** cURL Resource */ 
  116. $ch = curl_init(); 
  117.  
  118. /** Set URL */ 
  119. curl_setopt($ch, CURLOPT_URL, $url); 
  120.  
  121. /** Tell cURL to return the output */ 
  122. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  123.  
  124. /** Tell cURL NOT to return the headers */ 
  125. curl_setopt($ch, CURLOPT_HEADER, false); 
  126.  
  127. /** Execute cURL, Return Data */ 
  128. $data = curl_exec($ch); 
  129.  
  130. /** Check HTTP Code */ 
  131. $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
  132.  
  133. /** Close cURL Resource */ 
  134. curl_close($ch); 
  135.  
  136. /** 200 Response! */ 
  137. if ($status == 200) { 
  138.  
  139. /** Debug */ 
  140. $data =["status"=>"success"]; 
  141.  
  142. } else { 
  143.  
  144. /** Debug */ 
  145. $data =["status"=>"failed"]; 
  146.  
  147. return json_encode($data); 
  148.  
  149.  
  150. function get_facebook_token($client_id, $client_secret, $code, $redirect_uri) { 
  151.  
  152. /** Script URL */ 
  153. $url = 'https://graph.facebook.com/oauth/access_token'; 
  154.  
  155. /** $_GET Parameters to Send */ 
  156. $params = array( 
  157. "client_id" => $client_id,  
  158. "client_secret" => $client_secret,  
  159. "code" => $code,  
  160. "redirect_uri" => $redirect_uri 
  161.  
  162. ); 
  163.  
  164. /** Update URL to container Query String of Paramaters */ 
  165. $url .= '?' . http_build_query($params); 
  166.  
  167. /** cURL Resource */ 
  168. $ch = curl_init(); 
  169.  
  170. /** Set URL */ 
  171. curl_setopt($ch, CURLOPT_URL, $url); 
  172.  
  173. /** Tell cURL to return the output */ 
  174. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  175.  
  176. /** Tell cURL NOT to return the headers */ 
  177. curl_setopt($ch, CURLOPT_HEADER, false); 
  178.  
  179. /** Execute cURL, Return Data */ 
  180. $data = curl_exec($ch); 
  181.  
  182. /** Check HTTP Code */ 
  183. $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
  184.  
  185. /** Close cURL Resource */ 
  186. curl_close($ch); 
  187.  
  188. // var_dump($params); 
  189. return ($data); 
  190.  
.