nxs_doPublishToFB

The NextScripts: Social Networks Auto-Poster nxs doPublishToFB function.

Description

nxs_doPublishToFB( $postID, $options ); 

Parameters (2)

0. $postid
The postid.
1. $options
The options.

Usage

  1. if ( !function_exists( 'nxs_doPublishToFB' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'nextscripts-social-networks-auto-poster/inc-cl/fb.php'; 
  3.  
  4. // The postid. 
  5. $postid = null; 
  6.  
  7. // The options. 
  8. $options = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = nxs_doPublishToFB($postid, $options); 
  12.  

Defined (1)

The function is defined in the following location(s).

/inc-cl/fb.php  
  1. function nxs_doPublishToFB($postID, $options) { global $ShownAds; $ntCd = 'FB'; $ntCdL = 'fb'; $ntNm = 'Facebook'; $dsc = ''; $vidURL = '';  
  2. if (!is_array($options)) $options = maybe_unserialize(get_post_meta($postID, $options, true)); $wprg = array('sslverify'=>false, 'timeout' => 30);  
  3. if (!class_exists('nxs_class_SNAP_FB')) { nxs_addToLogN('E', 'Error', $ntCd, '-=ERROR=- No Facebook API Lib Detected', ''); return "No Facebook API Lib Detected";} 
  4.  
  5. $fbWhere = 'feed'; $page_id = $options['fbPgID']; if (isset($ShownAds)) $ShownAdsL = $ShownAds;  
  6. $addParams = nxs_makeURLParams(array('NTNAME'=>$ntNm, 'NTCODE'=>$ntCd, 'POSTID'=>$postID, 'ACCNAME'=>$options['nName'])); 
  7. //## Some Common stuff  
  8. if (empty($options['postType']) && !empty($options['fbPostType'])) { $options['postType'] = $options['fbPostType']; unset($options['fbPostType']); } //## Compatibility with v <3.2 
  9. if (empty($options['postType']) && !empty($options['PostType'])) { $pt = $options['PostType']; unset($options['PostType']); $options['postType'] = $pt; } //## Compatibility with v <3.2 
  10. //## Compatibility with v <3.4.15 
  11. if (empty($options['atchUse'])) $options['atchUse'] = 'F';  
  12. if (!empty($options['useFBGURLInfo']) && $options['useFBGURLInfo']=='1') $options['atchUse'] = 'F';  
  13. if (isset($options['useFBGURLInfo']) && $options['useFBGURLInfo']=='0') $options['atchUse'] = 'A';  
  14. if (!empty($options['fbMsgAFrmt'])) $options['atchUse'] = 'M';  
  15. if (!empty($options['fbMsgAFrmt']) && empty($options['msgDAFrmt'])) { $options['msgDAFrmt'] = $options['fbMsgAFrmt']; $options['msgTAFrmt'] = '%TITLE%'; } 
  16. if (isset($options['fbAttchAsVid']) && $options['fbAttchAsVid'] == '1') $options['fbAttchAsVid'] = 'A';  
  17. if (empty($options['fbAttchAsVid'])) $options['fbAttchAsVid'] = 'N';  
  18.  
  19. $ii = $options['ii']; if (!isset($options['pType'])) $options['pType'] = 'im'; if ($options['pType']=='sh') sleep(rand(1, 10));  
  20. $logNT = '<span style="color:#0000FF">Facebook</span> - '.$options['nName']; 
  21. $snap_ap = get_post_meta($postID, 'snap'.$ntCd, true); $snap_ap = maybe_unserialize($snap_ap);  
  22. if ($options['pType']!='aj' && is_array($snap_ap) && (nxs_chArrVar($snap_ap[$ii], 'isPosted', '1') || nxs_chArrVar($snap_ap[$ii], 'isPrePosted', '1'))) { 
  23. $snap_isAutoPosted = get_post_meta($postID, 'snap_isAutoPosted', true); if ($snap_isAutoPosted!='2') {  
  24. nxs_addToLogN('W', 'Notice', $logNT, '-=Duplicate=- Post ID:'.$postID, 'Already posted. No reason for posting duplicate'.' |'.$options['pType']); return; 
  25. }  
  26. //## Make the post 
  27. if (isset($options['qTLng'])) $lng = $options['qTLng']; else $lng = ''; if (!isset($options['fbAppPageAuthToken'])) $options['fbAppPageAuthToken'] = ''; 
  28. $blogTitle = htmlspecialchars_decode(get_bloginfo('name'), ENT_QUOTES); if ($blogTitle=='') $blogTitle = home_url();  
  29. //## Initiate Posting Array 
  30. $message = array('message'=>'', 'link'=>'', 'title'=>'', 'description'=>'', 'imageURL'=>'', 'videoURL'=>'', 'siteName'=>$blogTitle); $imgURL = ''; 
  31.  
  32. if ($postID=='0') { $options['fbMsgFormat'] = 'Test Post, Please Ignore'; 
  33. $dsc = 'Test Post, Description'; $urlTitle = 'Test Post - Title'; $urlToGo = home_url();  
  34. } else { $post = get_post($postID); if(!$post) return; $msg = nsFormatMessage($options['fbMsgFormat'], $postID, $addParams);  
  35. $fbPostType = $options['postType']; if ($fbPostType=='A') $fbPostType = (int)$options['fbAttch']==2?'S':'A'; $isAttachVidFB = $options['fbAttchAsVid']; 
  36. nxs_metaMarkAsPosted($postID, $ntCd, $options['ii'], array('isPrePosted'=>'1'));  
  37. $extInfo = ' | PostID: '.$postID." - ".(isset($post) && is_object($post)?nxs_doQTrans($post->post_title, $lng):'').' |'.$options['pType']; 
  38. if ($fbPostType=='A') $imgURL = nxs_getPostImage($postID, 'medium'); // prr($options); echo "PP - ".$postID; prr($src);  
  39. if ($fbPostType=='I' || $fbPostType=='S') $imgURL = nxs_getPostImage($postID, 'full'); // prr($options); echo "PP - ".$postID; prr($src);  
  40. if ( ($fbPostType=='A' || $fbPostType=='S') && $options['atchUse']!='F' ) { 
  41. //## AUTO - Get Post Descr from SEO Plugins or make it.  
  42. if ($options['atchUse']=='M') {$dsc = nsFormatMessage($options['msgDAFrmt'], $postID, $addParams); $urlTitle = nsFormatMessage($options['msgTAFrmt'], $postID, $addParams); }  
  43. else { if (function_exists('aioseop_mrt_fix_meta') && $dsc=='') $dsc = trim(get_post_meta($postID, '_aioseop_description', true));  
  44. if (function_exists('wpseo_admin_init') && $dsc=='') $dsc = trim(get_post_meta($postID, '_yoast_wpseo_opengraph-description', true));  
  45. if (function_exists('wpseo_admin_init') && $dsc=='') $dsc = trim(get_post_meta($postID, '_yoast_wpseo_metadesc', true));  
  46. if ($dsc=='') $dsc = trim(nxs_doQTrans($post->post_excerpt, $lng));  
  47. if ($dsc=='') $dsc = trim(nxs_doQTrans($post->post_content, $lng));  
  48. global $plgn_NS_SNAutoPoster; $gOptions = $plgn_NS_SNAutoPoster->nxs_options;if (empty($gOptions['brokenCntFilters'])) $dsc = apply_filters('the_content', $dsc); 
  49. if ($dsc=='') $dsc = get_bloginfo('description'); $urlTitle = nxs_doQTrans($post->post_title, $lng); 
  50. }  
  51. $dsc = strip_tags(strip_shortcodes($dsc));// $dsc = nxs_decodeEntitiesFull($dsc); /## This is commented out to support Emoji in Link Description 
  52. $dsc = nsTrnc($dsc, 900, ' '); 
  53.  
  54. $msg = str_replace('<br>', "\n", $msg); $msg = str_replace('<br/>', "\n", $msg); $msg = str_replace('<br />', "\n", $msg);  
  55. $msg = str_ireplace('<3', '<3', $msg); $msg = str_ireplace('<(', '<(', $msg); //## FB Smiles FIX.  
  56. $msg = strip_tags($msg); $msg = nxs_decodeEntitiesFull($msg);  
  57. $msg = str_ireplace(''', "'", $msg); $msg = str_ireplace('&039;', "'", $msg); $msg = str_ireplace(''', "'", $msg); $msg = str_ireplace('<3', '<3', $msg); $msg = str_ireplace('<(', '<(', $msg); //## FB Smiles FIX 2.  
  58.  
  59. if ($isAttachVidFB=='V') {$vids = nsFindVidsInPost($post, false); if (count($vids)>0) {  
  60. if (strlen($vids[0])==11) $options['urlToUse'] = 'http://youtu.be/'.$vids[0]; 
  61. if (strlen($vids[0])==8 || strlen($vids[0])==9) $options['urlToUse'] = 'https://vimeo.com/'.$vids[0]; 
  62. if (strlen($vids[0])==15) $options['urlToUse'] = 'https://www.facebook.com/video.php?v='.$vids[0]; 
  63. }} 
  64.  
  65. if ($isAttachVidFB=='A') {$vids = nsFindVidsInPost($post, false); if (count($vids)>0) { 
  66. if (strlen($vids[0])==11) { $vidURL = 'http://www.youtube.com/v/'.$vids[0]; $imgURL = nsGetYTThumb($vids[0]); } 
  67. if (strlen($vids[0])==8 || strlen($vids[0])==9) { // $vidURL = 'https://secure.vimeo.com/moogaloop.swf?clip_id='.$vids[0].'&autoplay=1';  
  68. $vidURL = 'https://f.vimeocdn.com/p/flash/moogaloop/6.0.37/moogaloop.swf?autoplay=1&clip_id='.$vids[0]; 
  69. $apiURL = "http://vimeo.com/api/v2/video/".$vids[0].".json?callback=showThumb"; $json = wp_remote_get($apiURL, $wprg); 
  70. if (!is_wp_error($json)) { $json = $json['body']; $json = str_replace('/**/', '', $json); 
  71. $json = str_replace('showThumb(', '', $json); $json = str_replace('])', ']', $json); $json = json_decode($json, true); $imgURL = $json[0]['thumbnail_large']; }  
  72. if (strlen($vids[0])==15) { // $vidURL = 'https://secure.vimeo.com/moogaloop.swf?clip_id='.$vids[0].'&autoplay=1';  
  73. $vidURL = 'https://www.facebook.com/video.php?v='.$vids[0]; $apiURL = "https://graph.facebook.com/v2.3/".$vids[0]; $json = wp_remote_get($apiURL, $wprg); 
  74. if (!is_wp_error($json)) { $json = $json['body']; $json = json_decode($json, true); $frmts = $json['format']; $imgURL = array_pop($frmts); $imgURL = $imgURL['picture']; }  
  75. }} 
  76. if (!empty($options['imgToUse'])) $imgURL = $options['imgToUse']; if (preg_match("/noImg.\.png/i", $imgURL)) $imgURL = 'http://www.noimage.faketld';//$imgURL = 'http://cdn.gtln.us/img/t1x1.gif';  
  77.  
  78. $options = nxs_getURL($options, $postID, $addParams); $urlToGo = $options['urlToUse'];  
  79. $options['fbMsgFormat'] = $msg; if (!empty($urlTitle)) $urlTitle = nxs_decodeEntitiesFull(strip_tags(strip_shortcodes($urlTitle))); 
  80. }  
  81.  
  82. $message = array('url'=>$urlToGo, 'urlTitle'=>$urlTitle, 'urlDescr'=>$dsc, 'imageURL'=>$imgURL, 'videoURL'=>$vidURL, 'siteName'=>$blogTitle); // prr($message); die(); 
  83. if (isset($ShownAds)) $ShownAds = $ShownAdsL; // FIX for the quick-adsense plugin 
  84.  
  85. //## Actual Post 
  86. $ntToPost = new nxs_class_SNAP_FB(); $ret = $ntToPost->doPostToNT($options, $message);  
  87. //## Process Results 
  88. if (!is_array($ret) || !isset($ret['isPosted']) || $ret['isPosted']!='1') { //## Error  
  89. if ($postID=='0') prr($ret); nxs_addToLogN('E', 'Error', $logNT, '-=ERROR=- '.print_r($ret, true), $extInfo);  
  90. } else { // ## All Good - log it. 
  91. if ($postID=='0') { nxs_addToLogN('S', 'Test', $logNT, 'OK - TEST Message Posted '); echo _e('OK - Message Posted, please see your '.$logNT.' Page. ', 'social-networks-auto-poster-facebook-twitter-g'); }  
  92. else { nxs_addToRI($postID); nxs_metaMarkAsPosted($postID, $ntCd, $options['ii'], array('isPosted'=>'1', 'pgID'=>$ret['postID'], 'pDate'=>date('Y-m-d H:i:s')));  
  93. $extInfo .= ' | <a href="'.$ret['postURL'].'" target="_blank">Post Link</a>'; nxs_addToLogN('S', 'Posted', $logNT, 'OK - Message Posted ', $extInfo);  
  94. if (!empty($ret['log']) && !empty($ret['log']['Warning'])) nxs_addToLogN('W', 'Warning', $logNT, $ret['log']['Warning'], $extInfo); 
  95. //## Return Result 
  96. if (!empty($ret['isPosted']) && $ret['isPosted']=='1') return 200; else return print_r($ret, true);  
  97. }