nxs_class_SNAP_WP

The NextScripts: Social Networks Auto-Poster nxs class SNAP WP class.

Defined (1)

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

/inc-cl/wp.api.php  
  1. if (!class_exists("nxs_class_SNAP_WP")) { class nxs_class_SNAP_WP { 
  2.  
  3. var $ntCode = 'WP'; 
  4. var $ntLCode = 'wp';  
  5.  
  6. function doPost($options, $message) { if (!is_array($options)) return false; $out = array(); 
  7. foreach ($options as $ii=>$ntOpts) $out[$ii] = $this->doPostToNT($ntOpts, $message); 
  8. return $out; 
  9. }  
  10. function nxs_getLJHeaders($up) { $hdrsArr = array();  
  11. $hdrsArr['Cache-Control']='no-cache'; $hdrsArr['Connection']='keep-alive';  
  12. $hdrsArr['User-Agent']='SNAP for Wordpress; Ver '.NextScripts_SNAP_Version; 
  13. $hdrsArr['Accept']='text/html, application/xhtml+xml, */*'; $hdrsArr['DNT']='1'; 
  14. $hdrsArr['Authorization'] = 'Basic ' . base64_encode("$up"); 
  15. if (function_exists('gzdeflate')) $hdrsArr['Accept-Encoding']='gzip, deflate'; $hdrsArr['Accept-Language']='en-US, en;q=0.8'; $hdrsArr['Accept-Charset']='ISO-8859-1, utf-8;q=0.7, *;q=0.3'; return $hdrsArr; 
  16. function doPostToNT($options, $message) { $badOut = array('pgID'=>'', 'isPosted'=>0, 'pDate'=>date('Y-m-d H:i:s'), 'Error'=>''); 
  17. //## Check settings 
  18. if (!is_array($options)) { $badOut['Error'] = 'No Options'; return $badOut; }  
  19. if (!isset($options['wpUName']) || trim($options['wpPass'])=='') { $badOut['Error'] = 'Not Configured'; return $badOut; }  
  20. $pass = substr($options['wpPass'], 0, 5)=='n5g9a'?nsx_doDecode(substr($options['wpPass'], 5)):$options['wpPass'];  
  21. if (empty($options['imgSize'])) $options['imgSize'] = ''; 
  22. //## Format 
  23. if (!empty($message['pText'])) $msg = $message['pText']; else $msg = nxs_doFormatMsg($options['wpMsgFormat'], $message);  
  24. if (!empty($message['pTitle'])) $msgT = $message['pTitle']; else $msgT = nxs_doFormatMsg($options['wpMsgTFormat'], $message);  
  25. if (isset($message['imageURL'])) $imgURL = trim(nxs_getImgfrOpt($message['imageURL'], $options['imgSize'])); else $imgURL = ''; 
  26. $link = urlencode($message['url']); $ext = substr($msg, 0, 1000);  
  27. //## Fix missing xmlrpc.php 
  28. if (substr($options['wpURL'], -1)=='/') $options['wpURL'] = substr($options['wpURL'], 0, -1); if (substr($options['wpURL'], -10)!='xmlrpc.php') $options['wpURL'] .= "/xmlrpc.php";  
  29. //## Post  
  30. require_once ('apis/xmlrpc-client.php'); $nxsToWPclient = new NXS_XMLRPC_Client($options['wpURL']); $nxsToWPclient->debug = false; 
  31. if ($imgURL!=='' && stripos($imgURL, 'http')!==false) {  
  32. // $handle = fopen($imgURL, "rb"); $filedata = ''; while (!feof($handle)) {$filedata .= fread($handle, 8192);} fclose($handle); 
  33. $filedata = wp_remote_get($imgURL); if (! is_wp_error($filedata) ) $filedata = $filedata['body']; // echo "AWC?"; 
  34. $data = array('name' => 'image-'.$message['orID'].'.jpg', 'type' => 'image/jpg', 'bits' => new NXS_XMLRPC_Base64($filedata), true);  
  35. $status = $nxsToWPclient->query('metaWeblog.newMediaObject', $message['orID'], $options['wpUName'], $pass, $data); $imgResp = $nxsToWPclient->getResponse(); $gid = $imgResp['id']; 
  36. } else $gid = ''; 
  37.  
  38. $params = array(0, $options['wpUName'], $pass, array('software_version'));  
  39. if (!$nxsToWPclient->query('wp.getOptions', $params)) { $ret = 'Something went wrong - '.$nxsToWPclient->getErrorCode().' : '.$nxsToWPclient->getErrorMessage();} else $ret = 'OK';  
  40. $rwpOpt = $nxsToWPclient->getResponse(); if (!empty($rwpOpt['software_version'])) { $rwpOpt = $rwpOpt['software_version']['value']; $rwpOpt = floatval($rwpOpt); } else $rwpOpt = 0; //prr($rwpOpt);prr($nxsToWPclient); 
  41. //## MAIN Post 
  42. if ($rwpOpt==0) {  
  43. $errMsg = $nxsToWPclient->getErrorMessage(); if ($errMsg!='') $ret = $errMsg; else $ret = 'XMLRPC is not found or not active. WP admin - Settings - Writing - Enable XML-RPC';  
  44. } else if ($rwpOpt<3.0) $ret = 'XMLRPC is too OLD - '.$rwpOpt.' You need at least 3.0'; else { 
  45.  
  46. if ($rwpOpt>3.3) { 
  47. $nxsToWPContent = array('title'=>$msgT, 'description'=>$msg, 'post_status'=>'draft', 'mt_excerpt'=>$ext, 'mt_allow_comments'=>1, 'mt_allow_pings'=>1, 'post_type'=>'post', 'mt_keywords'=>$message['tags'], 'categories'=>$message['cats'], 'custom_fields' => ''); 
  48. $params = array(0, $options['wpUName'], $pass, $nxsToWPContent, true); 
  49. if (!$nxsToWPclient->query('metaWeblog.newPost', $params)) { $ret = 'Something went wrong - '.$nxsToWPclient->getErrorCode().' : '.$nxsToWPclient->getErrorMessage();} else $ret = 'OK'; 
  50. $pid = $nxsToWPclient->getResponse();  
  51.  
  52. if ($gid!='') {  
  53. $nxsToWPContent = array('post_thumbnail'=>$gid); $params = array(0, $options['wpUName'], $pass, $pid, $nxsToWPContent, true);  
  54. if (!$nxsToWPclient->query('wp.editPost', $params)) { $ret = 'Something went wrong - '.$nxsToWPclient->getErrorCode().' : '.$nxsToWPclient->getErrorMessage();} else $ret = 'OK'; 
  55. $nxsToWPContent = array('post_status'=>'publish'); $params = array(0, $options['wpUName'], $pass, $pid, $nxsToWPContent, true);  
  56. if (!$nxsToWPclient->query('wp.editPost', $params)) { $ret = 'Something went wrong - '.$nxsToWPclient->getErrorCode().' : '.$nxsToWPclient->getErrorMessage();} else $ret = 'OK'; 
  57. } else { 
  58. $nxsToWPContent = array('title'=>$msgT, 'description'=>$msg, 'post_status'=>'publish', 'mt_allow_comments'=>1, 'mt_allow_pings'=>1, 'post_type'=>'post', 'mt_keywords'=>$message['tags'], 'categories'=>$message['cats'], 'custom_fields' => ''); 
  59. $params = array(0, $options['wpUName'], $pass, $nxsToWPContent, true); 
  60. if (!$nxsToWPclient->query('metaWeblog.newPost', $params)) { $ret = 'Something went wrong - '.$nxsToWPclient->getErrorCode().' : '.$nxsToWPclient->getErrorMessage();} else $ret = 'OK'; 
  61. $pid = $nxsToWPclient->getResponse();  
  62. }  
  63. if ($ret!='OK') $badOut['Error'] .= '-=ERROR=- '.print_r($ret, true); else {  
  64. $wpURL = str_ireplace('/xmlrpc.php', '', $options['wpURL']); if(substr($wpURL, -1)=='/') $wpURL=substr($wpURL, 0, -1); $wpURL .= '/?p='.$pid; return array('postID'=>$pid, 'isPosted'=>1, 'postURL'=>$wpURL, 'pDate'=>date('Y-m-d H:i:s')); 
  65. } return $badOut;  
  66. }  
  67. }}