nxs_snapPublishTo

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

Description

nxs_snapPublishTo( $postArr, (string) $type = '', (constant) $aj = false ); 

Parameters (3)

0. $postarr
The postarr.
1. $type — Optional. (string) => ''
The type.
2. $aj — Optional. (constant) => false
The aj.

Usage

  1. if ( !function_exists( 'nxs_snapPublishTo' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'nextscripts-social-networks-auto-poster/NextScripts_SNAP.php'; 
  3.  
  4. // The postarr. 
  5. $postarr = null; 
  6.  
  7. // The type. 
  8. $type = ''; 
  9.  
  10. // The aj. 
  11. $aj = false; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = nxs_snapPublishTo($postarr, $type, $aj); 
  15.  

Defined (1)

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

/NextScripts_SNAP.php  
  1. if (!function_exists("nxs_snapPublishTo")) { function nxs_snapPublishTo($postArr, $type='', $aj=false) { global $plgn_NS_SNAutoPoster, $nxs_snapAvNts, $blog_id, $nxs_tpWMPU; // echo " | nxs_doSMAS2 | "; prr($postArr); 
  2. if (!isset($plgn_NS_SNAutoPoster)) return; $options = $plgn_NS_SNAutoPoster->nxs_options;  
  3. if (!empty($_POST['nxs_snapPostOptions'])) { $NXS_POSTX = $_POST['nxs_snapPostOptions']; $NXS_POST = array(); $NXS_POST = NXS_parseQueryStr($NXS_POSTX); } else $NXS_POST = $_POST; 
  4. if(is_object($postArr)) $postID = $postArr->ID; else { $postID = $postArr; $postArr = get_post($postID); } $isPost = isset($NXS_POST["snapEdIT"]); $post = get_post($postID);  
  5. if ($post->post_status != 'publish') { sleep(5); $post = get_post($postID); $postArr = $post; 
  6. if ($post->post_status != 'publish') { nxs_addToLogN('I', 'Cancelled', '', 'Autopost Cancelled - Post is not "Published" Right now - Post ID:('.$postID.') - Current Post status -'.$post->post_status ); return; } 
  7. }  
  8. //nxs_addToLogN('BG', 'Post Status Changed', '', '-=## Autopost requested.'.($blog_id>1?'BlogID:'.$blog_id:'').' PostID:('.$postID.') Post Type: '.$post->post_type.' ##=-');  
  9. //$args=array('public'=>true, '_builtin'=>false); $output = 'names'; $operator = 'and'; $post_types = array(); ## Removed because some post types are not available from WP Cron 
  10. // if (function_exists('get_post_types')) { $post_types=get_post_types($args, $output, $operator); ## Removed because some post types are not available from WP Cron 
  11. if ( isset($options['nxsCPTSeld']) && $options['nxsCPTSeld']!='') $nxsCPTSeld = unserialize($options['nxsCPTSeld']); else $nxsCPTSeld = array();  
  12. // if ($post->post_type == 'post' || ($options['useForPages']=='1' && $post->post_type == 'page') || (in_array($post->post_type, $post_types) && in_array($post->post_type, $nxsCPTSeld))) { ## Removed because some post types are not available from WP Cron 
  13. $post_types = array(); 
  14. if ($post->post_type == 'post' || ($options['useForPages']=='1' && $post->post_type == 'page') || (in_array($post->post_type, $nxsCPTSeld))) {  
  15. if ($isPost && $options['skipSecurity']!='1' && !current_user_can("make_snap_posts") && !current_user_can("manage_options")) { nxs_addToLogN('I', 'Skipped', '', 'Current user can\'t autopost - Post ID:('.$postID.')' ); return; } 
  16. $postUser = $postArr->post_author;  
  17. if ($options['skipSecurity']!='1' && !user_can( $postUser, "make_snap_posts" ) && !user_can( $postUser, "manage_options")) { nxs_addToLogN('I', 'Skipped', '', '', 'User ID '.$postUser.' can\'t autopost (please see <a target="_blank" href="http://www.nextscripts.com/support-faq/#a17">FAQ #1.7</a> for more info/solution) - Post ID:('.$postID.')' ); return; }  
  18. if ($isPost) $plgn_NS_SNAutoPoster->NS_SNAP_SavePostMetaTags($postID);  
  19. if (function_exists('nxs_doSMAS2')) { nxs_doSMAS2($postArr, $type, $aj); return; } else { 
  20. $options = $plgn_NS_SNAutoPoster->nxs_options; $ltype=strtolower($type); 
  21. if ($nxs_tpWMPU=='S') { switch_to_blog(1); $plgn_NS_SNAutoPoster = new NS_SNAutoPoster(); $options = $plgn_NS_SNAutoPoster->nxs_options; restore_current_blog(); } 
  22. if (!isset($options['nxsHTDP']) || $options['nxsHTDP']=='S') { if(isset($NXS_POST["snapEdIT"]) && $NXS_POST["snapEdIT"]=='1') { $publtype='S'; $delay = rand(2, 10); } else $publtype='A'; } else $publtype = 'I'; 
  23. nxs_addToLogN('BG', 'Start =- ', '', '------=========#### NEW AUTO-POST REQUEST '.($blog_id>1?'BlogID:'.$blog_id:'').' PostID:('.$postID.') '.($publtype=='S'?'Scheduled +'.$delay:($publtype=='A'?'Automated':'Immediate')).' ####=========------'); 
  24.  
  25. $snap_isAutoPosted = get_post_meta($postID, 'snap_isAutoPosted', true); if ($snap_isAutoPosted=='1') { nxs_addToLogN('W', 'Skipped', '', 'Already Autoposted - Post ID:('.$postID.')' ); return; }  
  26. $snap_isEdIT = get_post_meta($postID, 'snapEdIT', true); if ($snap_isEdIT!='1') { $doPost = true; $exclCats = maybe_unserialize($options['exclCats']); $postCats = wp_get_post_categories($postID); 
  27. foreach ($postCats as $pCat) { if ( (is_array($exclCats)) && in_array($pCat, $exclCats)) $doPost = false; else {$doPost = true; break;}} 
  28. if (!$doPost) { nxs_addToLogN('I', 'Skipped', '', 'Automated Post - Category Excluded - Post ID:('.$postID.')' ); return; } 
  29. }  
  30. foreach ($nxs_snapAvNts as $avNt) {  
  31. if (isset($options[$avNt['lcode']]) && count($options[$avNt['lcode']])>0 ) { $clName = 'nxs_snapClass'.$avNt['code'];  
  32. if ($isPost && isset($NXS_POST[$avNt['lcode']])) $po = $NXS_POST[$avNt['lcode']]; else { $po = get_post_meta($postID, 'snap'.$avNt['code'], true); $po = maybe_unserialize($po);}  
  33. if (isset($po) && is_array($po)) $isPostMeta = true; else { $isPostMeta = false; $po = $options[$avNt['lcode']]; } 
  34. delete_post_meta($postID, 'snap_isAutoPosted'); add_post_meta($postID, 'snap_isAutoPosted', '1'); 
  35. $optMt = $options[$avNt['lcode']][0]; if ($isPostMeta) { $ntClInst = new $clName(); $optMt = $ntClInst->adjMetaOpt($optMt, $po[0]); }  
  36. if ($snap_isEdIT!='1') { $doPost = true;  
  37. if ( $optMt['catSel']=='1' && trim($optMt['catSelEd'])!='' ) { $inclCats = explode(', ', $optMt['catSelEd']);  
  38. foreach ($postCats as $pCat) { if (!in_array($pCat, $inclCats)) $doPost = false; else {$doPost = true; break;}}  
  39. if (!$doPost) { nxs_addToLogN('I', 'Skipped', $avNt['name'].' ('.$optMt['nName'].')', '[Automated Post] - Individual Category Excluded - Post ID:('.$postID.')' ); continue; } 
  40. //## Get tags 
  41. if (!empty($optMt['tagsSel'])) { $inclTags = explode(', ', strtolower($optMt['tagsSel'])); $postTags = wp_get_post_tags( $postID, array( 'fields' => 'slugs' ) ); $postCust = array(); 
  42. //## Get all custom post types 
  43. foreach ($inclTags as $iTag) {  
  44. if (strpos($iTag, '|')!==false) { $dd=explode('|', $itag); if (empty($postCust[$dd[0]])) $postCust[$dd[0]]=wp_get_object_terms($postID, $dd[0], array('fields'=>'slugs'));  
  45. if (!in_array(strtolower($dd[1]), $postCust[$dd[0]])) $doPost = false; else {$doPost = true; break;} 
  46. } else if (!in_array(strtolower($iTag), $postTags)) $doPost = false; else {$doPost = true; break;}  
  47. if (!$doPost) { nxs_addToLogN('I', 'Skipped', $avNt['name'].' ('.$optMt['nName'].')', '[Automated Post] - Tag Excluded - Post ID:('.$postID.') - Included Tags: '.$optMt['tagsSel'].' | Post Tags: '.print_r($postTags, true)." | ".print_r($postCust, true) ); continue; } 
  48. }  
  49. if ($optMt['do'.$avNt['code']]=='1') { $optMt['ii'] = 0;  
  50. if ($publtype=='A' && ($optMt['nMin']>0 || $optMt['nHrs']>0 || $optMt['nTime']!='')) $publtype='S';  
  51. if ($publtype=='S') { if (isset($optMt['nHrs']) && isset($optMt['nMin']) && ($optMt['nHrs']>0 || $optMt['nMin']>0) ) { $delay = $optMt['nMin']*60+$optMt['nHrs']*3600; 
  52. nxs_addToLogN('I', 'Delayed', $avNt['name'].' ('.$optMt['nName'].')', 'Post has been delayed for '.$delay.' Seconds ('.($optMt['nHrs']>0?$optMt['nHrs'].' Hours':'')." ".($optMt['nMin']>0?$optMt['nMin'].' Minutes':'').')' ); 
  53. } else $delay = rand(2, 10); $optMt['timeToRun'] = time()+$delay;  
  54. if ($options['ver']>300.330) { $shName = '_nxs_snap_sh_'.$avNt['code'].'0_'.$optMt['timeToRun']; delete_post_meta($postID, $shName); add_post_meta($postID, $shName, $optMt); $args = array($postID, $shName); } 
  55. else $args = array($postID, $optMt);  
  56. wp_schedule_single_event($optMt['timeToRun'], 'ns_doPublishTo'.$avNt['code'], $args);  
  57. nxs_addToLogN('BI', 'Scheduled', $avNt['name'].' ('.$optMt['nName'].') for '.$optMt['timeToRun']."(".date_i18n('Y-m-d H:i:s', $optMt['timeToRun'] + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS )).")", ' PostID:('.$postID.')' ); 
  58. } else { $fname = 'nxs_doPublishTo'.$avNt['code']; $fname($postID, $optMt); } 
  59. } else { nxs_addToLogN('GR', 'Skipped', $avNt['name'].' ('.$optMt['nName'].')', '-=[Unchecked Account]=- - PostID:'.$postID.'' ); } 
  60. }  
  61. } } } else { nxs_addToLogN('I', 'Skipped', '', 'Excluded Post Type: '.$post->post_type.' (Post ID: '.$postID.')| NOT IN ('.print_r($nxsCPTSeld, true).')| ALL ('.print_r($post_types, true).')' ); return; } 
  62. global $isS; if ($isS && function_exists("restore_current_blog")) restore_current_blog();  
  63. }}