PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/wc_actions.php

https://github.com/soverc/WriteCrowd-WP-Plugin
PHP | 1735 lines | 683 code | 324 blank | 728 comment | 110 complexity | 22260f2246f1bc941ae5a22e7cd9454e MD5 | raw file
  1. <?php
  2. /**
  3. * This class is responsible for setting up all
  4. * all the configurations and functions surrounding
  5. * the \syndicate and allowing them to function
  6. * inside of a local installation of WordPress
  7. *
  8. * @author WriteCrowd <support@writecrowd.com>
  9. * @version 0.9
  10. * @link https://www.writecrowd.com/
  11. * @copyright WriteCrowd https://www.writecrowd.com/
  12. * @license GPL 3.0
  13. *
  14. * WriteCrowd is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * WriteCrowd is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with WriteCrowd. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. class Wc_Actions {
  29. ////////////////////////////////////////////////////////////////////////
  30. ////////// Properties ///////////////////////////////////////
  31. //////////////////////////////////////////////////////////////////////
  32. protected static $oInstance = null; // Singleton instance container
  33. protected $oAccount = null; // The current 180Create account in use
  34. protected $oArticle = null; // The current article in use
  35. protected $aArticles = null; // The current article set in use
  36. protected $aCategories = null; // Our categories
  37. protected $aComments = null; // Our comments
  38. protected $oDatabase = null; // Our database object
  39. protected $sError = null; // Container for the current error
  40. protected $aGroups = null; // Container for the current user's groups
  41. protected $sNamespace = null; // Our application namespace
  42. protected $sPluginPath = null; // Path to our Plugin
  43. protected $sPluginWebPath = null; // URL to plugin
  44. protected $oPost = null; // The current WordPress Post in use
  45. protected $aPosts = null; // WordPress posts
  46. protected $oPostData = null; // POST data container
  47. protected $mResponse = null; // RPC response container
  48. protected $oScope = null; // A variable scope to pass to the template
  49. protected $aSearchResults = null; // Results container for when a search is run
  50. protected $aSubcategories = null; // Our subcategories
  51. ////////////////////////////////////////////////////////////////////////
  52. ////////// Singleton ////////////////////////////////////////
  53. //////////////////////////////////////////////////////////////////////
  54. /**
  55. * This sets the singleton pattern instance
  56. *
  57. * @return object self::$oInstance
  58. */
  59. public static function setInstance() {
  60. try {
  61. // Set instance to new self
  62. self::$oInstance = new Wc_Actions();
  63. //self::$oInstance = new Wc_Actions();
  64. //self::$oInstance = new StdClass();
  65. } catch (Exception $oException) {
  66. // Set error string
  67. // $this->setError("Error: {$oException->getMessage()}");
  68. }
  69. // Return instance of class
  70. return self::$oInstance;
  71. }
  72. /**
  73. * This gets the singleton instance
  74. *
  75. * @return object self::$oInstance
  76. */
  77. public static function getInstance() {
  78. // Check to see if an instance has already
  79. // been created
  80. if (is_null(self::$oInstance)) {
  81. return self::setInstance();
  82. } else {
  83. // If so, return the previously created
  84. // instance
  85. return self::$oInstance;
  86. }
  87. }
  88. /**
  89. * This method resets the sinleton instance
  90. *
  91. * @return void
  92. */
  93. public static function resetInstance() {
  94. self::$oInstance = null;
  95. }
  96. ////////////////////////////////////////////////////////////////////////
  97. ////////// Private Construct //////////////////////////////
  98. /////////////////////////////////////////////////////////////////////
  99. /**
  100. * This method sets up all of the WordPress features we wish to use
  101. *
  102. * @return Wc_Actions $this for a fluid and chain-loadable interface
  103. */
  104. private function __construct() {
  105. /*
  106. // Check to see if an admin
  107. // has logged into the system
  108. if (is_admin()) {
  109. // If so, setup all of the
  110. // admin functions and controls
  111. // WordPress Init
  112. add_action('init', array($this, 'loadAssets'));
  113. // Create the sidebar menu for 180 Create
  114. add_action('admin_menu', array($this, 'loadAdminMenu'));
  115. // Meta Box
  116. // WordPress >= 3.0
  117. // add_action('add_meta_boxes', array($this, 'loadMetaBox'));
  118. // WordPress < 3.0
  119. add_action('admin_init', array($this, 'loadMetaBox'), 1);
  120. } else {
  121. // Load scripts and styles
  122. // for the client
  123. add_action('wp_head', array($this, 'loadAssets'), 1);
  124. }
  125. // Display Post Author
  126. add_filter('the_author', array($this, 'renderPostAuthor'));
  127. // Display Post Content
  128. add_filter('the_content', array($this, 'renderPostContent'));
  129. // Set all of the functions and controls
  130. // Display Post Title
  131. add_filter('the_title', array($this, 'renderPostTitle'));
  132. // Determine if we want to display our comments
  133. if (Wc_Config::Get('variables', 'enableCommentEngine')) {
  134. // Run when displaying Comments for a single post
  135. add_filter('comments_array', array($this, 'handleComments'));
  136. // Use Our Comments Template
  137. add_filter('comments_template', array($this, 'renderComments'));
  138. }
  139. */
  140. // Return instance
  141. return $this;
  142. }
  143. ////////////////////////////////////////////////////////////////////////
  144. ////////// Public Methods ///////////////////////////////////
  145. //////////////////////////////////////////////////////////////////////
  146. /**
  147. * This method is responsible for logging data
  148. *
  149. * @param string $sData is a json encoded string of properties
  150. * @return boolean based on the success or failure of the log
  151. */
  152. public function doLog($sData = null) {
  153. // Make sure we have data
  154. if (is_null($sData)) {
  155. // If there is no data,
  156. // return false
  157. return false;
  158. // We have data
  159. } else {
  160. // Setup the SQL query
  161. $sQuery = "INSERT INTO `{$this->getDatabase()->prefix}{$this->getNamespace()}_log` VALUES (NOW(), ".mysql_real_escape_string($sData).");";
  162. // Try to log the data
  163. try {
  164. // Log the data
  165. $this->getDatabase()->query($sQuery);
  166. // Return true because there
  167. // were no errors
  168. return true;
  169. // Catch any exceptions
  170. } catch (Exception $oException) {
  171. // Set system error to exception message
  172. $this->setError($oException->getMessage());
  173. // Return false because there
  174. // was an exception
  175. return false;
  176. }
  177. }
  178. }
  179. /**
  180. * This method handles the login process
  181. * for the 180Create Account
  182. *
  183. * @return Wc_Actions $this for a fluid and chain-loadable interface
  184. */
  185. public function doLogin() {
  186. // Check for POST data
  187. if ($this->checkForData('getPostData')) {
  188. // POST data was found, make the
  189. // JSON-RPC service call
  190. $aPostNameSpace = $this->getPostData()->{$this->getNameSpace()};
  191. $this->feedJson(array(
  192. '_method' => 'logon',
  193. 'username' => $this->doSanitize($aPostNameSpace['txtDisplayName']),
  194. 'passwd' => $this->doSanitize($aPostNameSpace['txtPasswd'])
  195. ));
  196. // Check for a JSON-RPC error
  197. if (is_object($this->getRpcResponse()) && property_exists($this->getRpcResponse(), 'error')) {
  198. // Set the system error to the
  199. // JSON-RPC service error
  200. $this->setError($this->getRpcResponse()->error);
  201. } else {
  202. // No error was found, now
  203. // set the system account
  204. $this->setAccount($this->getRpcResponse());
  205. // Store the current user
  206. $oUser = wp_get_current_user();
  207. // Make sure a user is logged in
  208. if (empty($oUser->ID)) {
  209. // No user is logged in, so
  210. // set the system error
  211. $this->setError(Wc_Config::Get('errorMessages', 'noWordPressUserLoggedIn'));
  212. } else {
  213. // Store the account
  214. $this->getDatabase()->query(str_replace(array(
  215. '{wpdbPrefix}',
  216. '{nameSpace}',
  217. '{wpId}',
  218. '{returnedAccountId}',
  219. '{returnedAccountObject}'
  220. ), array(
  221. $this->getDatabase()->prefix,
  222. $this->getNamespace(),
  223. $oUser->ID,
  224. $this->getRpcResponse()->id,
  225. mysql_real_escape_string(json_encode($this->getRpcResponse()))
  226. ), Wc_Config::Get('sqlMiscQueries', 'storeOneightyAccount')));
  227. }
  228. }
  229. } else {
  230. // No POST data was found,
  231. // set the system error
  232. $this->setError(Wc_Config::Get('errorMessages', 'noPostData'));
  233. }
  234. // Return instance
  235. return $this;
  236. }
  237. /**
  238. * This method logs a user out of the syndicate
  239. *
  240. * @return bool
  241. */
  242. public function doLogout() {
  243. // Grab the current user
  244. $oUser = wp_get_current_user();
  245. // Setup the sql
  246. $sSql = str_replace(array(
  247. '{wpdbPrefix}',
  248. '{nameSpace}',
  249. '{wpId}'
  250. ), array(
  251. $this->getDatabase()->prefix,
  252. $this->getNamespace(),
  253. $oUser->ID
  254. ), Wc_Config::Get('sqlMiscQueries', 'removeOneightyAccount'));
  255. // Try to run the query
  256. if ($this->getDatabase()->query($sSql)) {
  257. // The user has been
  258. // logged out, return
  259. return true;
  260. } else {
  261. // The user was not
  262. // logged out, return
  263. return false;
  264. }
  265. }
  266. /**
  267. * This method handles the removal process
  268. *
  269. * @param integer $aid article ID
  270. * @return boolean based on the success or failure of the removal
  271. */
  272. public function doRemoveArticle($aid) {
  273. $aid = intval($aid);
  274. // Check for a 180Create Account
  275. if (!$user = wp_oneighty_user_details()) {
  276. // if (!$this->checkForOneightyAccount()) {
  277. // No 180Create account was found
  278. return false;
  279. }
  280. $this->setAccount($user);
  281. // Make sure this is actually a
  282. // 180Create article
  283. if ($wcid = $this->checkIfOneightyArticle($aid)) {
  284. // Check to see if the author of
  285. // the article is logged into this
  286. // local WordPress instance
  287. //
  288. // MK this is severly unsafe if this is the only check protecting the server's data.
  289. //if ($this->checkIfArticleBelongsToOneightyAccount() === true) {
  290. $article = $this->getDatabase()->get_results("SELECT * FROM {$wpdb->prefix}posts WHERE ID = '".addslashes($aid)."'");
  291. $article = $article[0];
  292. $this->getDatabase()->query("DELETE FROM {$this->getDatabase()->prefix}mediaplace_posts WHERE post_id = '".addslashes($aid)."'");
  293. // Update Post
  294. wp_update_post(array(
  295. 'ID' => $aid,
  296. 'post_content' => $this->getArticle()->aSyndicationData[str_replace('{nameSpace}', $this->getNameSpace(), Wc_Config::Get('wordPress', 'postMetaKeyData'))]->content
  297. ));
  298. // Remove post meta
  299. delete_post_meta($aid, str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeyData')));
  300. delete_post_meta($aid, str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeyId')));
  301. delete_post_meta($aid, str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeyPullComments')));
  302. delete_post_meta($aid, str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeySyndicated')));
  303. delete_post_meta($aid, str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeySyndicationDate')));
  304. // Send the deactivation signal
  305. // to the RPC server
  306. $this->feedJson(array(
  307. '_method' => 'deactivate_article',
  308. '_key' => $user->data->account_key,
  309. 'article_id' => $wcid
  310. ));
  311. // We're done, return
  312. return true;
  313. //} else {
  314. //}
  315. } else {
  316. // Try to remove the article
  317. // from the WordPress DB
  318. try {
  319. // Delete the post from,
  320. // the WordPress DB
  321. wp_delete_post($aid, true);
  322. // Catch all errors and exceptions
  323. } catch (Exception $oException) {
  324. // Set the system error to the
  325. // exception error message
  326. $this->setError($oException->getMessage());
  327. // Return false because
  328. // there was an error
  329. return false;
  330. }
  331. // Return true because the artice
  332. // was successfully removed
  333. return true;
  334. }
  335. /*
  336. } else {
  337. // Set system error
  338. $this->setError(Wc_Config::Get('errorMessages', 'noPostData'));
  339. // Return false because there
  340. // was an error
  341. return false;
  342. }
  343. */
  344. }
  345. /**
  346. * This method sanitizes and strips unwanted
  347. * characters from the provided string
  348. *
  349. * @param string $sData is the string to sanitize
  350. * @param string $sCustom is a custom expression to run
  351. * @return string $sSanitized is the sanitized string
  352. */
  353. public function doSanitize($sData, $sCustom = null) {
  354. // Sanitized string placeholder
  355. $sSanitized = null;
  356. // Check for a custom expression
  357. if (is_null($sCustom)) {
  358. // There is no custom expression,
  359. // run the default sanitization
  360. $sSanitized = mysql_real_escape_string(strip_tags($sData));
  361. } else {
  362. // Run the custom expression
  363. $sSanitized = mysql_real_escape_string(preg_replace($sCustom, '', $sData));
  364. }
  365. // Return the sanitized string
  366. return $sSanitized;
  367. }
  368. /**
  369. * This method handles the searching of
  370. * the 180Create database
  371. *
  372. * @return Wc_Actions $this for a fluid and chain-loadable interface
  373. */
  374. public function doSearch() {
  375. // Check for POST data
  376. if ($this->checkForData('getPostData')) {
  377. // POST data was found, check for
  378. // a user account
  379. if ($this->checkForOneightyAccount()) {
  380. // POST data was found, make the
  381. // JSON-RPC service call
  382. $aPostNameSpace = $this->getPostData()->{$this->getNameSpace()};
  383. $this->feedJson(array(
  384. '_method' => 'search',
  385. '_key' => $this->getAccount()->account_key,
  386. 'criteria' => $this->doSanitize($aPostNameSpace['txtCriteria'])
  387. ));
  388. // Check for a JSON-RPC error
  389. if (is_object($this->getRpcResponse()) && property_exists($this->getRpcResponse(), 'error')) {
  390. // Set the system error to the
  391. // JSON-RPC service error
  392. $this->setError($this->getRpcResponse()->error);
  393. // Return instance
  394. return $this;
  395. } else {
  396. // No error was found, now
  397. // Check for actual results
  398. if (count($this->getRpcResponse())) {
  399. // Grab the current articles
  400. // in the local WordPress DB
  401. $sArticlesQuery = str_replace(array(
  402. '{wpdbPrefix}', '{nameSpace}'
  403. ), array(
  404. $this->getDatabase()->prefix, $this->getNamespace()
  405. ), Wc_Config::Get('sqlMiscQueries', 'retrieveOneightyArticles'));
  406. // Try to execute the SQL
  407. try {
  408. // Execute the SQL
  409. $aArticles = $this->getDatabase()->get_results($sArticlesQuery);
  410. // Check for results
  411. if (count($aArticles)) {
  412. // We have results, create
  413. // the exclusion array
  414. $aExclude = array();
  415. // Loop through the local IDs
  416. // and append them
  417. foreach ($aArticles as $oArticle) {
  418. // Append the ID to the exclusion
  419. // array
  420. $aExclude[] = $oArticle->iOneightyId;
  421. }
  422. // Now loop through the search
  423. // results and check id the ID
  424. // is in the exclusion array
  425. $aSearchResults = $this->getRpcResponse();
  426. foreach ($aSearchResults as $iIndex => $oArticle) {
  427. // Check for ID
  428. if (in_array($oArticle->id, $aExclude)) {
  429. // If it is in the exclude array
  430. // unset the array index
  431. unset($aSearchResults[$iIndex]);
  432. }
  433. }
  434. // Set the system search results
  435. $this->setSearchResults($aSearchResults);
  436. // Return instance
  437. return $this;
  438. // Nothing has been syndicated yet
  439. } else {
  440. // Set the system search results
  441. $this->setSearchResults($this->getRpcResponse());
  442. // Return instance
  443. return $this;
  444. }
  445. // Catch all exceptions
  446. } catch (Exception $oException) {
  447. // Set the system error to the
  448. // exception message
  449. $this->setError($oException->getMessage());
  450. // Return instance
  451. return $this;
  452. }
  453. } else {
  454. // No articles were found,
  455. // set the system error
  456. $this->setError(Wc_Config::Get('errorMessages', 'noSearchResults'));
  457. // Return instance
  458. return $this;
  459. }
  460. }
  461. // No account was found
  462. } else {
  463. // Set the system error
  464. $this->setError(Wc_Config::Get('errorMessages', 'noOneightyAccount'));
  465. // Return instance
  466. return $this;
  467. }
  468. } else {
  469. // No POST data was found,
  470. // set the system error
  471. $this->setError(Wc_Config::Get('errorMessages', 'noPostData'));
  472. // Return instance
  473. return $this;
  474. }
  475. }
  476. /**
  477. * This method increments the syndication count of the articles
  478. *
  479. * @param string $sApiKey is the account holder's API key
  480. * @param integer $iArticleId is the article id to increment statistics
  481. * @param integer $iUserId is the account holder's account ID
  482. * @return bool based on success or failure
  483. */
  484. public function doSyndicatePlusOne($sApiKey, $iArticleId, $iUserId) {
  485. // Run the feed
  486. $this->feedJson(array(
  487. '_method' => 'syndicate_plus_one',
  488. '_key' => $sApiKey,
  489. 'article_id' => $iArticleId,
  490. 'user_id' => $iUserId
  491. ));
  492. // Check for an error
  493. if (empty($this->getRpcResponse()->error)) {
  494. // All is well, return
  495. return true;
  496. } else {
  497. // We have an error, set it
  498. // into the system
  499. $this->setError($this->getRpcResponse()->error);
  500. // Return
  501. return false;
  502. }
  503. }
  504. /**
  505. * This method is responsible syndicating
  506. * content between WordPress and 180Create
  507. *
  508. * @param boolean $bToWordPress determines whether we are syndicating to WordPress or 180Create
  509. * @return boolean based on the success or failure of the syndication
  510. */
  511. public function doSyndicateToRpc() {
  512. // Check for an account
  513. if ($this->checkForOneightyAccount()) {
  514. // We now have data, process
  515. // and syndicate it
  516. $aJsonRpcParams = array(
  517. '_method' => 'post',
  518. '_key' => $this->getAccount()->account_key,
  519. 'author_id' => $this->getAccount()->id,
  520. 'content' => $this->getArticle()->sContent,
  521. 'title' => $this->getArticle()->sTitle,
  522. 'description' => $this->getArticle()->sExcerpt,
  523. 'category_id' => $this->getArticle()->iCategoryId,
  524. 'secondcategory_id' => $this->getArticle()->iSecondCategoryId,
  525. 'subcategory_id' => $this->getArticle()->iSubcategoryId,
  526. 'secondsubcategory_id' => $this->getArticle()->iSecondSubcategoryId,
  527. 'group_id' => $this->getArticle()->iGroupId,
  528. 'private' => $this->getArticle()->iPrivate,
  529. 'tag_words' => $this->getArticle()->sTagWords,
  530. 'cost' => $this->getArticle()->iCost,
  531. 'allow_free' => $this->getArticle()->iFree,
  532. 'name' => $this->getArticle()->sName,
  533. 'from_blog' => get_bloginfo('name'),
  534. 'from_url' => get_bloginfo('url')
  535. );
  536. // Try to run the RPC request
  537. if ($this->feedJson($aJsonRpcParams)) {
  538. // Check fro RPC errors
  539. if (!empty($this->getRpcResponse()->error)) {
  540. // Set the system error
  541. $this->setError($this->getRpcResponse()->error);
  542. // Return
  543. return false;
  544. }
  545. // No errors were found, all signs
  546. // point to the article being syndicated
  547. // now check for a WordPress Post ID
  548. if (empty($this->getArticle()->iWordPressId)) {
  549. // Set system error
  550. $this->setError(Wc_Config::Get('errorMessages', 'noArticleToSyndicate'));
  551. // Return
  552. return false;
  553. } else {
  554. // We have a WordPress Post
  555. // no update the post
  556. $this->getDatabase()->query(str_replace(array(
  557. '{wpdbPrefix}',
  558. '{iWordPressId}',
  559. '{sPostStatus}',
  560. '{sPostTitle}',
  561. '{sPostType}',
  562. '{sPostName}',
  563. '{sPostContent}'
  564. ), array(
  565. $this->getDatabase()->prefix,
  566. $this->getArticle()->iWordPressId,
  567. 'publish',
  568. $this->getRpcResponse()->title,
  569. 'post',
  570. $this->getRpcResponse()->name,
  571. mysql_real_escape_string(json_encode(array(
  572. 'iArticleId' => $this->getRpcResponse()->id
  573. )))
  574. ), Wc_Config::Get('sqlMiscQueries', 'updateWordPressPost')));
  575. // Add our post meta data
  576. add_post_meta($this->getArticle()->iWordPressId, str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeyData')), mysql_real_escape_string(json_encode($this->getRpcResponse())), true);
  577. add_post_meta($this->getArticle()->iWordPressId, str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeyId')), $this->doSanitize($this->getRpcResponse()->id), true);
  578. add_post_meta($this->getArticle()->iWordPressId, str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeyPullComments')), true, true);
  579. add_post_meta($this->getArticle()->iWordPressId, str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeySyndicationDate')), date('Y-m-d H:i:s'), true);
  580. add_post_meta($this->getArticle()->iWordPressId, str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeySyndicated')), true, true);
  581. // One up the syndication count
  582. if ($this->doSyndicatePlusOne($this->getAccount()->account_key, $this->getRpcResponse()->id, $this->getAccount()->id)) {
  583. // All is well, return
  584. return true;
  585. } else {
  586. // Something got fubar'd, return
  587. return false;
  588. }
  589. }
  590. } else {
  591. // Return
  592. return false;
  593. }
  594. } else {
  595. // Set the system error
  596. $this->setError(Wc_Config::Get('errorMessages', 'noOneightyAccount'));
  597. // Return
  598. return false;
  599. }
  600. }
  601. public function doSyndicateToWordPress() {
  602. // Check for POST
  603. if (!empty($_POST) && !empty($_POST[$this->getNameSpace()])) {
  604. // Set our POST data
  605. $aPostData = (array) $_POST[$this->getNameSpace()];
  606. // Check for user account
  607. if ($this->checkForOneightyAccount()) {
  608. // Make the JSON-RPC call
  609. $this->feedJson(array(
  610. '_method' => 'fetch',
  611. '_key' => $this->getAccount()->account_key,
  612. 'article_id' => $this->doSanitize($aPostData['iArticleId'])
  613. ));
  614. // Check for JSON-RPC errors
  615. if (property_exists($this->getRpcResponse(), 'error')) {
  616. // Set the system error
  617. // to the JSON-RPC error
  618. $this->setError($this->getRpcResponse()->error);
  619. // Return false because
  620. // there was an error
  621. return false;
  622. }
  623. // Create a new WordPress
  624. // post and store the ID
  625. $iWordPressId = wp_insert_post(array(
  626. 'post_status' => 'publish',
  627. 'post_title' => $this->doSanitize($this->getRpcResponse()->title),
  628. 'post_type' => 'post',
  629. 'post_name' => $this->doSanitize($this->getRpcResponse()->name),
  630. 'post_content' => json_encode(array(
  631. 'iOneightyId' => $this->doSanitize($this->getRpcResponse()->id)
  632. ))
  633. ));
  634. // Add the 180Create post meta data
  635. // into the local WordPress system
  636. add_post_meta($iWordPressId, str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeyData')), mysql_real_escape_string(json_encode($this->getRpcResponse())), true);
  637. add_post_meta($iWordPressId, str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeyId')), $this->doSanitize($this->getRpcResponse()->id), true);
  638. add_post_meta($iWordPressId, str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeyPullComments')), true, true);
  639. add_post_meta($iWordPressId, str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeySyndicationDate')), date('Y-m-d H:i:s'), true);
  640. add_post_meta($iWordPressId, str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeySyndicated')), true, true);
  641. // All is well, now update
  642. // the syndication count
  643. $this->feedJson(array(
  644. '_method' => 'syndicate_plus_one',
  645. '_key' => $this->doSanitize($this->getAccount()->account_key),
  646. 'article_id' => $this->doSanitize($aPostData['iArticleId']),
  647. 'user_id' => $this->doSanitize($this->getAccount()->id)
  648. ));
  649. // Check for JSON-RPC errors
  650. if (property_exists($this->getRpcResponse(), 'error')) {
  651. // Set the system error to the
  652. // JSON-RPC error message
  653. $this->setError($this->getRpcResponse()->error);
  654. // Return false because
  655. // there was an error
  656. return false;
  657. }
  658. // Check for JSON-RPC success
  659. if (property_exists($this->getRpcResponse(), 'success') && $this->getRpcResponse()->success == true) {
  660. // There are no errors
  661. // and all is well,
  662. // return true
  663. return true;
  664. } else {
  665. // Set the system error
  666. $this->setError(Wc_Config::Get('errorMessages', 'noSyndicatePlusOne'));
  667. // Return false because
  668. // there was an error
  669. return false;
  670. }
  671. }else {
  672. // Set the system error
  673. $this->setError(Wc_Config::Get('errorMessages', 'noOneightyAccount'));
  674. // Return
  675. return false;
  676. }
  677. } else {
  678. // Set the system error
  679. $this->setError(Wc_Config::Get('errorMessages', 'noPostData'));
  680. // Return
  681. return false;
  682. }
  683. }
  684. /**
  685. * This method runs all of the actions necessary to install
  686. * our plugin and use it with WordPress
  687. *
  688. * @return void
  689. */
  690. public function runInstall() {
  691. // Create our accounts table
  692. dbDelta(str_replace(array(
  693. '{wpdbPrefix}',
  694. '{nameSpace}'
  695. ), array(
  696. $this->getDatabase()->prefix,
  697. $this->getNamespace()
  698. ), Wc_Config::Get('sqlInstallQueries', 'createAccountsTable')));
  699. }
  700. /**
  701. * This method runs all of the actions necessary to uninstall
  702. * our plugin from WordPress
  703. *
  704. * @return void
  705. */
  706. public function runUninstall() {
  707. // Delete our accounts table
  708. dbDelta(str_replace(array(
  709. '{wpdbPrefix}',
  710. '{nameSpace}'
  711. ), array(
  712. $this->getDatabase()->prefix,
  713. $this->getNamespace()
  714. ), Wc_Config::Get('sqlUninstallQueries', 'dropAccountsTable')));
  715. }
  716. ////////////////////////////////////////////////////////////////////////
  717. ////////// Checks ///////////////////////////////////////////
  718. //////////////////////////////////////////////////////////////////////
  719. /**
  720. * This method run a specified getter method
  721. * and check to see if any data was returned
  722. *
  723. * @package Checks
  724. * @param string $sMethod the name of the method
  725. * @return boolean
  726. */
  727. public function checkForData($sMethod) {
  728. // Store the response
  729. $mResponse = call_user_func(array($this, $sMethod));
  730. // Call the method and
  731. // check for returned
  732. // dataset
  733. if (empty($mResponse)) {
  734. // If the dataset is
  735. // null or unset,
  736. // return false
  737. return false;
  738. } else {
  739. // If there is data,
  740. // return true
  741. return true;
  742. }
  743. }
  744. /**
  745. * This method checks for any set errors
  746. * and throws an exception if an error is
  747. * set with the current error message as
  748. * the exception message
  749. *
  750. * @package Checks
  751. * @return Wc_Actions $this for a fluid and chain-loadable interface
  752. */
  753. public function checkForErrors() {
  754. // Check for an error
  755. if ($this->checkForData('getError')) {
  756. // If there is an error,
  757. // throw an exception
  758. throw new Exception($this->getError());
  759. }
  760. // Return instance
  761. return $this;
  762. }
  763. /**
  764. * This method determines if there is a
  765. * 180Create account stored in the local
  766. * WordPress database
  767. *
  768. * @package Checks
  769. * @return boolean
  770. */
  771. public function checkForOneightyAccount() {
  772. // Grab the current user
  773. $oUser = wp_get_current_user();
  774. // Query for the account
  775. $aAccount = $this->getDatabase()->get_results(str_replace(array(
  776. '{wpdbPrefix}',
  777. '{nameSpace}',
  778. '{wpId}'
  779. ), array(
  780. $this->getDatabase()->prefix,
  781. $this->getNamespace(),
  782. $oUser->ID
  783. ), Wc_Config::Get('sqlMiscQueries', 'retrieveOneightyAccount')));
  784. // Check to see if the user has
  785. // a 180Create account
  786. if (count($aAccount)) {
  787. // This user has an account, set
  788. // the system account and
  789. // return true
  790. $this->setAccount(json_decode($aAccount[0]->oAccount));
  791. // Return
  792. return true;
  793. } else {
  794. // There is no user account
  795. // set the system error and
  796. // return false
  797. $this->setError(Wc_Config::Get('errorMessages', 'noOneightyAccount'));
  798. // Return
  799. return false;
  800. }
  801. }
  802. /**
  803. * This method checks to see if an article
  804. * belongs to a currently stored account
  805. *
  806. * @return bool true for yes, false for no
  807. */
  808. public function checkIfArticleBelongsToOneightyAccount() {
  809. // Query for the account
  810. $aAccount = $this->getDatabase()->get_results(str_replace(array(
  811. '{wpdbPrefix}',
  812. '{nameSpace}',
  813. '{iOneightyId}'
  814. ), array(
  815. $this->getDatabase()->prefix,
  816. $this->getNamespace(),
  817. $this->getArticle()->aSyndicationData[str_replace('{nameSpace}', $this->getNameSpace(), Wc_Config::Get('wordPress', 'postMetaKeyData'))]->author_id
  818. ), Wc_Config::Get('sqlMiscQueries', 'retrieveOneightyAccountByOneightyId')));
  819. // Check to see if the
  820. // account array is empty
  821. if (empty($aAccount)) {
  822. // This article does not belong to
  823. // a currently stored account,
  824. // return
  825. return false;
  826. } else {
  827. // This article does belong to
  828. // a currently stored account
  829. // return
  830. return true;
  831. }
  832. }
  833. /**
  834. * This method determines if the
  835. * provided WordPress post ID is
  836. * a valid 180Create article
  837. *
  838. * @package Checks
  839. * @return boolean
  840. */
  841. public function checkIfOneightyArticle($iWordPressId) {
  842. global $wpdb;
  843. $iWordPressId = (int)$iWordPressId;
  844. //$something = get_post_meta($iWordPressId, str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeySyndicated')), true);
  845. //if (!empty($iWordPressId) && intval($something)) {
  846. // Check for an article count
  847. $syndicated = $wpdb->get_results("SELECT mediaplace_id FROM {$wpdb->prefix}mediaplace_posts WHERE post_id = '{$iWordPressId}'");
  848. if (!empty($iWordPressId) && count($syndicated)) {
  849. // Grab the article from
  850. // the local database
  851. $oArticle = get_post($iWordPressId);
  852. if ($oArticle === NULL) {
  853. return false;
  854. }
  855. return $syndicated[0]->mediaplace_id;
  856. // Create the 180Create data property
  857. $oArticle->aSyndicationData = array();
  858. // Loop through each of the
  859. // custom meta keys and check
  860. // to see if they are one of
  861. // our meta keys
  862. /*
  863. foreach (get_post_custom_keys($iWordPressId) as $sMetaKey) {
  864. // Make sure it is one of ours
  865. if (strpos($sMetaKey, $this->getNameSpace()) !== false) {
  866. // It's one of ours, now
  867. // grab and set the value
  868. $oArticle->aSyndicationData[$sMetaKey] = get_post_meta($iWordPressId, $sMetaKey, true);
  869. }
  870. }
  871. */
  872. // Decode the syndication data
  873. //$oArticle->aSyndicationData[str_replace('{nameSpace}', $this->getNameSpace(), Wc_Config::Get('wordPress', 'postMetaKeyData'))] = json_decode($oArticle->aSyndicationData[str_replace('{nameSpace}', $this->getNameSpace(), Wc_Config::Get('wordPress', 'postMetaKeyData'))]);
  874. // Set the article into the system
  875. $this->setArticle($oArticle);
  876. // This is a 180Create
  877. // article, return true
  878. return true;
  879. } else {
  880. // This is not a 180Create
  881. // article, return false
  882. return false;
  883. }
  884. }
  885. ////////////////////////////////////////////////////////////////////////
  886. ////////// Load /////////////////////////////////////////////
  887. //////////////////////////////////////////////////////////////////////
  888. /**
  889. * This method is responsible for loading the
  890. * menu into the WordPress admin interface
  891. *
  892. * @package Load
  893. * @return Wc_Actions $this for a fluid and chain-loadable interface
  894. */
  895. public function loadAdminMenu() {
  896. // Setup the parent menu
  897. add_menu_page(
  898. Wc_Config::Get('variables', 'appName'), // Page title
  899. Wc_Config::Get('variables', 'appName'), // Menu title
  900. 'administrator', // Permissions
  901. $this->getNameSpace(), // Page slug
  902. array($this, 'renderOneighty'), // Function to render HTML
  903. "{$this->getPluginWebPath()}/".Wc_Config::Get('folders', 'images')."/{$this->getNamespace()}.png" // Logo
  904. );
  905. // Setup Sub pages
  906. //Login/Accont
  907. add_submenu_page(
  908. $this->getNameSpace(), // Parent slug
  909. Wc_Config::Get('variables', 'appName'), // Page title
  910. ($this->checkForOneightyAccount() ? 'Account' : 'Login'), // Menu title
  911. 'administrator', // Permissions
  912. $this->getNameSpace(), // Slug
  913. array($this, 'renderOneighty') // Function to render HTML
  914. );
  915. // Existing Content
  916. add_submenu_page(
  917. $this->getNameSpace(), // Parent slug
  918. Wc_Config::Get('variables', 'appName').' Existing Content', // Page title
  919. 'Existing Content', // Menu title
  920. 'administrator', // Permissions
  921. $this->getNameSpace().'_existing', // Slug
  922. array($this, 'renderExistingContent') // Function to render HTML
  923. );
  924. // Search
  925. add_submenu_page(
  926. $this->getNameSpace(), // Parent slug
  927. Wc_Config::Get('variables', 'appName').' Article Search', // Page title
  928. 'Article Search', // Menu title
  929. 'administrator', // Permissions
  930. $this->getNameSpace().'_search', // Slug
  931. array($this, 'renderArticleSearch') // Function to render HTML
  932. );
  933. // Syndicated Content
  934. add_submenu_page(
  935. $this->getNameSpace(), // Parent slug
  936. Wc_Config::Get('variables', 'appName').' Syndicated Content', // Page title
  937. 'Syndicated Content', // Menu title
  938. 'administrator', // Permissions
  939. $this->getNameSpace().'_syndicated', // Slug
  940. array($this, 'renderSyndicatedContent') // Function to render HTML
  941. );
  942. // Logout
  943. add_submenu_page(
  944. $this->getNameSpace(), // Parent slug
  945. Wc_Config::Get('variables', 'appName').' Logout', // Page title
  946. 'Logout', // Menu title
  947. 'administrator', // Permissions
  948. $this->getNameSpace().'_logout', // Slug
  949. array($this, 'renderLogout') // Function to render HTML
  950. );
  951. // Return instance
  952. return $this;
  953. }
  954. /**
  955. * This method retrieves an article from the 180Create
  956. * table based on its associated WordPress ID
  957. *
  958. * @param integer $iWordPressId is the WordPress ID for the article
  959. * @return Wc_Actions $this for a fluid and chain-loadable interface
  960. */
  961. public function loadArticle($iWordPressId) {
  962. // Load the article
  963. $aArticles = $this->getDatabase()->get_results(str_replace(array(
  964. '{wpdbPrefix}',
  965. '{nameSpace}',
  966. '{wpId}'
  967. ), array(
  968. $this->getDatabase()->prefix,
  969. $this->getNameSpace(),
  970. $this->doSanitize($iWordPressId)
  971. ), Wc_Config::Get('sqlMiscQueries', 'retrieveOneightyArticle')));
  972. // Check to make sure we have
  973. // an article or article set
  974. if (count($aArticles)) {
  975. // Set the article into the system
  976. $this->setArticle(json_decode($aArticles[0]->oArticle));
  977. // Return true
  978. return true;
  979. } else {
  980. // There was no article,
  981. // so return false
  982. return false;
  983. }
  984. }
  985. /**
  986. * This method loads all of the syndicated
  987. * articles into the system
  988. *
  989. * @package Load
  990. * @return Wc_Actions $this for a fluid and chain-loadable interface
  991. */
  992. public function loadArticles() {
  993. // Check for a 180Create Account
  994. if (!$this->checkForOneightyAccount()) {
  995. return $this;
  996. }
  997. // Try to execute the query
  998. try {
  999. // Execute the SQL and store the results
  1000. $aArticles = get_posts(array(
  1001. 'post_type' => 'post',
  1002. 'post_status' => 'publish',
  1003. 'meta_query' => array(
  1004. array(
  1005. 'key' => str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeySyndicated')),
  1006. 'value' => 1,
  1007. 'compare' => '='
  1008. )
  1009. )
  1010. ));
  1011. // Set the articles to display
  1012. // array placeholder
  1013. $aDisplayArticles = array();
  1014. // Catch all errors and exceptions
  1015. } catch (Exception $oException) {
  1016. // Set the system error
  1017. $this->setError($oException->getMessage());
  1018. // Return instance
  1019. return $this;
  1020. }
  1021. // Make sure we have articles
  1022. if (count($aArticles)) {
  1023. // Loop through each of the articles
  1024. // and grab the meta data from them
  1025. foreach ($aArticles as $oArticle) {
  1026. // Make sure this is one
  1027. // of our articles
  1028. if ($this->checkIfOneightyArticle($oArticle->ID) === true) {
  1029. // Add the article to the
  1030. // array of articles
  1031. $aArticles[] = $this->getArticle();
  1032. }
  1033. }
  1034. // Set the system articles
  1035. $this->setArticles($aArticles);
  1036. } else {
  1037. // There are no articles, so
  1038. // set the system error
  1039. $this->setError(Wc_Config::Get('errorMessages', 'noSyndicatedContent'));
  1040. }
  1041. // Return instance
  1042. return $this;
  1043. }
  1044. /**
  1045. * This method is responsible for loadin the
  1046. * JavaScripts and Stylesheets into the system
  1047. *
  1048. * @package Load
  1049. * @return Wc_Actions $this for a fluid and chain-loadable interface
  1050. */
  1051. public function loadAssets() {
  1052. // Create our custom post type
  1053. // register_post_type($this->getNamespace(), array(
  1054. // 'publicly_queryable' => true,
  1055. // 'exclude_from_search' => false,
  1056. // 'show_ui' => false,
  1057. // 'show_in_menu' => $this->getNameSpace(),
  1058. // 'capability_type' => 'post',
  1059. // 'show_in_nav_menus' => true,
  1060. // 'rewrite' => true,
  1061. // 'query_var' => true,
  1062. // ));
  1063. // Register the latest jQueryUI Stylesheet from Google
  1064. wp_register_style("{$this->getNamespace()}-jquery-ui-all-css", "{$this->getPluginWebPath()}/".Wc_Config::Get('styleSheets', 'jQueryUi'));
  1065. // Load jQueryUI Stylesheet
  1066. wp_enqueue_style("{$this->getNamespace()}-jquery-ui-all-css");
  1067. if (is_admin() == false) {
  1068. // Load jQuery libraries
  1069. wp_enqueue_script('jquery');
  1070. // Register the latest jQueryUI Library
  1071. wp_register_script("{$this->getNamespace()}-jquery-ui-all", "{$this->getPluginWebPath()}/".Wc_Config::Get('javaScripts', 'jQueryUi'));
  1072. // Register the latest jQueryUI Select Menu Plugin
  1073. wp_register_script("{$this->getNamespace()}-jquery-ui-selectmenu", "{$this->getPluginWebPath()}/".Wc_Config::Get('javaScripts', 'jQueryUiSelectMenu'));
  1074. // Register the latest jQuery Validation
  1075. // plugin from the jQuery repository
  1076. wp_register_script("{$this->getNamespace()}-jquery-validate", "{$this->getPluginWebPath()}/".Wc_Config::Get('javaScripts', 'jQueryValidate'));
  1077. // Load our base functions
  1078. wp_enqueue_script("{$this->getNamespace()}-base");
  1079. // Load jQueryUI
  1080. wp_enqueue_script("{$this->getNamespace()}-jquery-ui-all");
  1081. // Load jQueryUi Select Menu
  1082. wp_enqueue_script("{$this->getNamespace()}-jquery-ui-selectmenu");
  1083. // Load jQuery Validate
  1084. wp_enqueue_script("{$this->getNamespace()}-jquery-validate");
  1085. // See if we are on post page
  1086. if (!is_null(get_the_ID())) {
  1087. if ($this->checkIfOneightyArticle(get_the_ID()) === true) {
  1088. // Meta property
  1089. $sMetaProperty = str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeyData'));
  1090. // Add in our meta tags
  1091. echo("<meta name=\"syndication-source\" content=\"{$this->getArticle()->aSyndicationData[$sMetaProperty]->from_url}\">\n");
  1092. }
  1093. }
  1094. } else {
  1095. // Register our plugin's extra styles
  1096. wp_register_style("{$this->getNamespace()}-extra-css", "{$this->getPluginWebPath()}/".Wc_Config::Get('styleSheets', 'base'));
  1097. // Load our plugin's extra styles
  1098. wp_enqueue_style("{$this->getNameSpace()}-extra-css");
  1099. // Register our base functions
  1100. wp_register_script("{$this->getNamespace()}-base", "{$this->getPluginWebPath()}/".Wc_Config::Get('javaScripts', 'base'));
  1101. // Register the latest jQueryUI Library
  1102. wp_register_script("{$this->getNamespace()}-jquery-ui-all", "{$this->getPluginWebPath()}/".Wc_Config::Get('javaScripts', 'jQueryUi'));
  1103. // Register the latest jQueryUI Select Menu Plugin
  1104. wp_register_script("{$this->getNamespace()}-jquery-ui-selectmenu", "{$this->getPluginWebPath()}/".Wc_Config::Get('javaScripts', 'jQueryUiSelectMenu'));
  1105. // Register the latest jQuery Validation
  1106. // plugin from the jQuery repository
  1107. wp_register_script("{$this->getNamespace()}-jquery-validate", "{$this->getPluginWebPath()}/".Wc_Config::Get('javaScripts', 'jQueryValidate'));
  1108. // Load our base functions
  1109. wp_enqueue_script("{$this->getNamespace()}-base");
  1110. // Load jQueryUI
  1111. wp_enqueue_script("{$this->getNamespace()}-jquery-ui-all");
  1112. // Load jQueryUi Select Menu
  1113. wp_enqueue_script("{$this->getNamespace()}-jquery-ui-selectmenu");
  1114. // Load jQuery Validate
  1115. wp_enqueue_script("{$this->getNamespace()}-jquery-validate");
  1116. }
  1117. // Return instance
  1118. return $this;
  1119. }
  1120. /**
  1121. * This method load the categories from the
  1122. * 180Create RPC server
  1123. *
  1124. * @package Load
  1125. * @return Wc_Actions $this for a fluid and chain-loadable interface
  1126. */
  1127. public function loadCategories() {
  1128. // Check to see if there is a user logged in
  1129. if ($this->checkForOneightyAccount()) {
  1130. // If there is an account, make
  1131. // the call to the RPC server
  1132. $this->feedJson(array(
  1133. '_method' => 'categories',
  1134. '_key' => $this->getAccount()->account_key
  1135. ));
  1136. // Check for RPC errors
  1137. if (is_object($this->getRpcResponse()) && property_exists($this->getRpcResponse(), 'error')) {
  1138. // If there is an error,
  1139. // set the system error
  1140. $this->setError($this->getRpcResponse()->error);
  1141. // Return instance
  1142. return $this;
  1143. } else {
  1144. // If there are no errors,
  1145. // set the system categories
  1146. $this->setCategories($this->getRpcResponse());
  1147. // Return instance
  1148. return $this;
  1149. }
  1150. } else {
  1151. // Return instance
  1152. return $this;
  1153. }
  1154. }
  1155. /**
  1156. * This method loads the comments from 180Create
  1157. * into the system for the current post being viewed
  1158. *
  1159. * @package Load
  1160. * @param integer $iOneightyId is the article ID
  1161. * @return Wc_Actions $this for a fluid and chain-loadable interface
  1162. */
  1163. public function loadComments() {
  1164. // Check to see if the user is logged in
  1165. if ($this->checkForOneightyAccount()) {
  1166. // If there was a 180Create account
  1167. // load the comments from rpc
  1168. $this->feedJson(array(
  1169. '_method' => 'grab_comments',
  1170. '_key' => $this->getAccount()->account_key,
  1171. 'article_id' => $this->getArticle()->id
  1172. ));
  1173. // Check for RPC errors
  1174. if (is_object($this->getRpcResponse()) && property_exists($this->getRpcResponse(), 'error')) {
  1175. // If there is an error,
  1176. // set the system error
  1177. $this->setError($this->getRpcResponse()->error);
  1178. // Return instance
  1179. return $this;
  1180. } else {
  1181. // If there are no errors,
  1182. // check to see if we have
  1183. // any comments
  1184. if (count($this->getRpcResponse())) {
  1185. // If so, set them into the system
  1186. $this->setComments($this->getRpcResponse());
  1187. } else {
  1188. // If not, return an error
  1189. $this->setError(Wc_Config::Get('errorMessages', 'noCommentsFound'));
  1190. }
  1191. // Return instance
  1192. return $this;
  1193. }
  1194. } else {
  1195. // Return instance
  1196. return $this;
  1197. }
  1198. }
  1199. /**
  1200. * This method loads the current stored account's
  1201. * groups from 180Create into the system
  1202. *
  1203. * @package Load
  1204. * @return Wc_Actions $this for a fluid and chain-loadable interface
  1205. */
  1206. public function loadGroups() {
  1207. // Check for user account
  1208. if ($this->checkForData('getAccount')) {
  1209. // We have a user account,
  1210. // now make the call to the
  1211. // RPC server for the groups
  1212. $this->feedJson(array(
  1213. '_method' => 'groups',
  1214. '_key' => $this->doSanitize($this->getAccount()->account_key),
  1215. 'user_id' => $this->doSanitize($this->getAccount()->id)
  1216. ));
  1217. // Check for JSON-RPC server errors
  1218. if (is_object($this->getRpcResponse()) && property_exists($this->getRpcResponse(), 'error')) {
  1219. // Check to see that we have
  1220. // actual results
  1221. if (is_array($this->getRpcResponse()) && count($this->getRpcResponse())) {
  1222. // We have results, now
  1223. // set the groups into
  1224. // the system
  1225. $this->setGroups($this->getRpcResponse());
  1226. // Return instance
  1227. return $this;
  1228. } else {
  1229. // No groups were found,
  1230. // set the system notification
  1231. $this->setError(Wc_Config::Get('notificationMessages', 'noGroupsFound'));
  1232. // Return instance
  1233. return $this;
  1234. }
  1235. // An error is present
  1236. } else {
  1237. // Set the system error
  1238. $this->setError($this->getRpcResponse()->error);
  1239. // Return false because there
  1240. // was an error from the JSON
  1241. // RPC server
  1242. return $this;
  1243. }
  1244. } else {
  1245. // Return false because
  1246. // we have not categories
  1247. return $this;
  1248. }
  1249. }
  1250. /**
  1251. * This method loads our custom meta box
  1252. * into the WordPress interface
  1253. *
  1254. * @package Load
  1255. * @return Wc_Actions $this for a fluid and chain-loadable interface
  1256. */
  1257. public function loadMetaBox() {
  1258. if (function_exists('add_meta_box')) {
  1259. // Add the meta_box to the system
  1260. add_meta_box("{$this->getNamespace()}-post-meta-box", __(Wc_Config::Get('variables', 'appName').' Article Details', "{$this->getNameSpace()}-post-meta-box"), array($this, 'renderMetaBox'), 'post', 'side', 'high');
  1261. }
  1262. }
  1263. /**
  1264. * This method loads all of the posts
  1265. * from the local WordPress DB
  1266. * into the system
  1267. *
  1268. * @package Load
  1269. * @return Wc_Actions $this for a fluid and chain-loadable interface
  1270. */
  1271. public function loadPosts() {
  1272. // Load the WordPress posts
  1273. // into the system
  1274. $aPosts = get_posts(array(
  1275. 'post_type' => 'post',
  1276. 'post_status' => 'publish'
  1277. ));
  1278. // Indices to remove array
  1279. $aIndices = array();
  1280. // Loop through the posts,
  1281. // weeding out the ones
  1282. // that are ours
  1283. for ($iPost = 0; $iPost < count($aPosts); $iPost ++) {
  1284. // Check to see if the post is ours
  1285. if (get_post_meta($aPosts[$iPost]->ID, str_replace('{nameSpace}', $this->getNamespace(), Wc_Config::Get('wordPress', 'postMetaKeySyndicated')))) {
  1286. // Add the index to the posts to remove
  1287. $aIndices[] = $iPost;
  1288. }
  1289. }
  1290. // Loop through the indices
  1291. // and unset them
  1292. foreach ($aIndices as $iIndex) {
  1293. // Unset the index
  1294. unset($aPosts[$iIndex]);
  1295. }
  1296. // Set the posts into the system
  1297. $this->setPosts($aPosts);
  1298. // Return instance
  1299. return $this;
  1300. }
  1301. /**
  1302. * This method loads the subcategories
  1303. * from the 180Create RPC server
  1304. *
  1305. * @package Load
  1306. * @param integer $iCategoryId is the ID of the parent Category
  1307. * @return Wc_Actions $this for a fluid and chain-loadable interface
  1308. */
  1309. public function loadSubcategories($iCategoryId) {
  1310. // Check to see if there is a user logged in
  1311. if ($this->checkForOneightyAccount()) {
  1312. // If there is an account, make
  1313. // the call to the RPC server
  1314. $this->feedJson(array(
  1315. '_method' => 'subcategories',
  1316. '_key' => $this->getAccount()->account_key,
  1317. 'category_id' => $this->doSanitize($iCategoryId)
  1318. ));
  1319. // Check for RPC errors
  1320. if (is_object($this->getRpcResponse()) && property_exists($this->getRpcResponse(), 'error')) {
  1321. // If there is an error,
  1322. // set the system error
  1323. $this->setError($this->getRpcResponse()->error);
  1324. // Return instance
  1325. return $this;
  1326. } else {
  1327. // If there are no errors,
  1328. // set the system categories
  1329. $this->setSubcategories($this->getRpcResponse());
  1330. // Return instance
  1331. return $this;
  1332. }
  1333. } else {
  1334. // If there is no account,
  1335. // set the system error
  1336. $this->setError(Wc_Config::Get('errorMessages', 'noOneightyAccount'));
  1337. // Return instance
  1338. return $this;
  1339. }
  1340. }
  1341. /**
  1342. * This method loads and renders a template file.
  1343. * The scope of the template file is that of @var $this
  1344. *
  1345. * @package Load
  1346. * @param string $sTemplateFile is the name of the template file
  1347. * @return Wc_Actions $this for a fluid and chain-loadable interface
  1348. */
  1349. public function loadTemplate($sTemplateName) {
  1350. // Make sure a template file was
  1351. // actually specified
  1352. if (is_null($sTemplateName)) {
  1353. // If not set the system error
  1354. $this->setError(Wc_Config::Get('errorMessages', 'nullTemplateFile'));
  1355. } else {
  1356. // Set full filename
  1357. $sTemplateFile = (string) $this->getPluginPath().'/'.Wc_Config::Get('folders', 'templates').'/'.$sTemplateName.'.tpl';
  1358. // Check to see if the file exists
  1359. if (!include_once($sTemplateFile)) {
  1360. // If the file doesn't exist,
  1361. // set the system error
  1362. $this->setError(Wc_Config::Get('errorMessages', 'templateFileNoExist'));
  1363. if ($sTemplateName != 'error')
  1364. // Load error template
  1365. $this->loadTemplate("error");
  1366. }
  1367. }
  1368. return $this;
  1369. }
  1370. ////////////////////////////////////////////////////////////////////////
  1371. ////////// Render ///////////////////////////////////////////
  1372. //////////////////////////////////////////////////////////////////////
  1373. /**
  1374. * This method is responsible for rendering
  1375. * the article search form and results
  1376. *
  1377. * @package Render
  1378. * @return Wc_Actions $this for a fluid and chain-loadable interface
  1379. */
  1380. public function renderArticleSearch() {
  1381. // Check for POST data
  1382. if ($this->checkForData('getPostData') && property_exists($this->getPostData(), $this->getNamespace())) {
  1383. // If a form has been submitted
  1384. // run the search actions
  1385. $this->doSearch();
  1386. }
  1387. // Check to see if there is a 180Create
  1388. // account stored in the local DB
  1389. if ($this->checkForOneightyAccount()) {
  1390. // If so, load our template
  1391. // for logged in users
  1392. $this->loadTemplate("search");
  1393. } else {
  1394. // No account is there, load our
  1395. // template for non-log