PageRenderTime 68ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/src/library/whv_actions.php

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