PageRenderTime 81ms CodeModel.GetById 41ms RepoModel.GetById 7ms app.codeStats 0ms

/code/cake/app/webroot/cp/wp-content/plugins/commentpress-core/commentpress-core/class_commentpress_db.php

https://github.com/DigitalPaulScholtenProject/DPSP-Platform
PHP | 4316 lines | 1358 code | 1731 blank | 1227 comment | 244 complexity | 21ee3acc0aca0c858965c056b799cec3 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, AGPL-1.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php /*
  2. ================================================================================
  3. Class CommentpressCoreDatabase
  4. ================================================================================
  5. AUTHOR: Christian Wach <needle@haystack.co.uk>
  6. --------------------------------------------------------------------------------
  7. NOTES
  8. =====
  9. This class is a wrapper for the majority of database operations.
  10. --------------------------------------------------------------------------------
  11. */
  12. /*
  13. ================================================================================
  14. Class Name
  15. ================================================================================
  16. */
  17. class CommentpressCoreDatabase {
  18. /*
  19. ============================================================================
  20. Properties
  21. ============================================================================
  22. */
  23. // parent object reference
  24. var $parent_obj;
  25. // standard mobile browser
  26. var $is_mobile = false;
  27. // touch-based mobile browser
  28. var $is_mobile_touch = false;
  29. // touch-based tablet browser
  30. var $is_tablet = false;
  31. // ---------- options ----------
  32. var $commentpress_options = array();
  33. // TOC content ('post' or 'page')
  34. var $toc_content = 'page';
  35. // TOC chapters are pages by default
  36. var $toc_chapter_is_page = 1;
  37. // Show extended TOC content for posts lists
  38. var $show_extended_toc = 1;
  39. // TOC shows subpages by default
  40. var $show_subpages = 1;
  41. // show page titles by default
  42. var $title_visibility = 'show';
  43. // hide page meta by default
  44. var $page_meta_visibility = 'hide';
  45. // default editor (tinyMCE)
  46. var $comment_editor = 1;
  47. // promote reading (1) or commenting (0)
  48. var $promote_reading = 0;
  49. // default excerpt length
  50. var $excerpt_length = 55;
  51. // default header background colour (hex, same as in layout.css)
  52. var $header_bg_colour = '2c2622';
  53. // default scroll speed (ms)
  54. var $js_scroll_speed = '800';
  55. // default type of blog - eg, array('0' => 'Poetry','1' => 'Prose')
  56. var $blog_type = false;
  57. // default blog workflow (like translation, for example), off by default
  58. var $blog_workflow = 0;
  59. // default sidebar ('toc' => Contents tab)
  60. var $sidebar_default = 'toc';
  61. // default minimum page width (px)
  62. var $min_page_width = '447';
  63. // "live" comment refreshing off by default
  64. var $para_comments_live = 0;
  65. // prevent save_post hook firing more than once
  66. var $saved_post = false;
  67. /**
  68. * @description: initialises this object
  69. * @param object $parent_obj a reference to the parent object
  70. * @return object
  71. * @todo:
  72. *
  73. */
  74. function __construct( $parent_obj ) {
  75. // store reference to parent
  76. $this->parent_obj = $parent_obj;
  77. // init
  78. $this->_init();
  79. // --<
  80. return $this;
  81. }
  82. /**
  83. * @description: PHP 4 constructor
  84. */
  85. function CommentpressCoreDatabase( $parent_obj ) {
  86. // is this php5?
  87. if ( version_compare( PHP_VERSION, "5.0.0", "<" ) ) {
  88. // call php5 constructor
  89. $this->__construct( $parent_obj );
  90. }
  91. // --<
  92. return $this;
  93. }
  94. /**
  95. * @description: set up all items associated with this object
  96. * @todo:
  97. *
  98. */
  99. function activate() {
  100. // have we already got a modified database?
  101. $modified = $this->db_is_modified( 'comment_text_signature' ) ? 'y' : 'n';
  102. // if we have an existing comment_text_signature column...
  103. if ( $modified == 'y' ) {
  104. // upgrade old Commentpress schema to new
  105. if ( !$this->schema_upgrade() ) {
  106. // kill plugin activation
  107. _cpdie( 'CommentPress Core Error: could not upgrade the database' );
  108. }
  109. } else {
  110. // update db schema
  111. $this->schema_update();
  112. }
  113. // test if we have our version
  114. if ( !$this->option_wp_get( 'commentpress_version' ) ) {
  115. // store CommentPress Core version
  116. $this->option_wp_set( 'commentpress_version', COMMENTPRESS_VERSION );
  117. }
  118. // test that we aren't reactivating
  119. if ( !$this->option_wp_get( 'commentpress_options' ) ) {
  120. // test if we have a existing pre-3.4 Commentpress instance
  121. if ( commentpress_is_legacy_plugin_active() ) {
  122. // yes: add options with existing values
  123. $this->_options_migrate();
  124. } else {
  125. // no: add options with default values
  126. $this->_options_create();
  127. }
  128. }
  129. // retrieve data on special pages
  130. $special_pages = $this->option_get( 'cp_special_pages', array() );
  131. // if we haven't created any...
  132. if ( count( $special_pages ) == 0 ) {
  133. // create special pages
  134. $this->create_special_pages();
  135. }
  136. // turn comment paging option off
  137. $this->_cancel_comment_paging();
  138. // override widgets
  139. $this->_clear_widgets();
  140. }
  141. /**
  142. * @description: upgrade Commentpress plugin from 3.1 options to latest set
  143. * @return boolean $result
  144. * @todo:
  145. *
  146. */
  147. function upgrade() {
  148. // database object
  149. global $wpdb;
  150. // init return
  151. $result = false;
  152. // if we have a commentpress install (or we're forcing)
  153. if ( $this->check_upgrade() ) {
  154. // are we missing the commentpress_options option?
  155. if ( !$this->option_wp_exists( 'commentpress_options' ) ) {
  156. // upgrade to the single array
  157. $this->_options_upgrade();
  158. }
  159. // checkboxes send no value if not checked, so use a default
  160. $cp_blog_workflow = $this->blog_workflow;
  161. // default blog type
  162. $cp_blog_type = $this->blog_type;
  163. // get variables
  164. extract( $_POST );
  165. // Removed in CP 3.4 - do we still have the legacy cp_para_comments_enabled option?
  166. if ( $this->option_exists( 'cp_para_comments_enabled' ) ) {
  167. // delete old cp_para_comments_enabled option
  168. $this->option_delete( 'cp_para_comments_enabled' );
  169. }
  170. // Removed in CP 3.4 - do we still have the legacy cp_minimise_sidebar option?
  171. if ( $this->option_exists( 'cp_minimise_sidebar' ) ) {
  172. // delete old cp_minimise_sidebar option
  173. $this->option_delete( 'cp_minimise_sidebar' );
  174. }
  175. // New in CP 3.4 - has AJAX "live" comment refreshing been migrated?
  176. if ( !$this->option_exists( 'cp_para_comments_live' ) ) {
  177. // "live" comment refreshing, off by default
  178. $this->option_set( 'cp_para_comments_live', $this->para_comments_live );
  179. }
  180. // New in CP 3.3.3 - changed the way the welcome page works
  181. if ( $this->option_exists( 'cp_special_pages' ) ) {
  182. // do we have the cp_welcome_page option?
  183. if ( $this->option_exists( 'cp_welcome_page' ) ) {
  184. // get it
  185. $page_id = $this->option_get( 'cp_welcome_page' );
  186. // retrieve data on special pages
  187. $special_pages = $this->option_get( 'cp_special_pages', array() );
  188. // is it in our special pages array?
  189. if ( in_array( $page_id, $special_pages ) ) {
  190. // remove page id from array
  191. $special_pages = array_diff( $special_pages, array( $page_id ) );
  192. // reset option
  193. $this->option_set( 'cp_special_pages', $special_pages );
  194. }
  195. }
  196. }
  197. // New in CP 3.3.3 - are we missing the cp_sidebar_default option?
  198. if ( !$this->option_exists( 'cp_sidebar_default' ) ) {
  199. // get choice
  200. $_choice = $wpdb->escape( $cp_sidebar_default );
  201. // add chosen cp_page_meta_visibility option
  202. $this->option_set( 'cp_sidebar_default', $_choice );
  203. }
  204. // New in CP 3.3.2 - are we missing the cp_page_meta_visibility option?
  205. if ( !$this->option_exists( 'cp_page_meta_visibility' ) ) {
  206. // get choice
  207. $_choice = $wpdb->escape( $cp_page_meta_visibility );
  208. // add chosen cp_page_meta_visibility option
  209. $this->option_set( 'cp_page_meta_visibility', $_choice );
  210. }
  211. // New in CP 3.3.1 - are we missing the cp_blog_workflow option?
  212. if ( !$this->option_exists( 'cp_blog_workflow' ) ) {
  213. // get choice
  214. $_choice = $wpdb->escape( $cp_blog_workflow );
  215. // add chosen cp_blog_workflow option
  216. $this->option_set( 'cp_blog_workflow', $_choice );
  217. }
  218. // New in CP 3.3.1 - are we missing the cp_blog_type option?
  219. if ( !$this->option_exists( 'cp_blog_type' ) ) {
  220. // get choice
  221. $_choice = $wpdb->escape( $cp_blog_type );
  222. // add chosen cp_blog_type option
  223. $this->option_set( 'cp_blog_type', $_choice );
  224. }
  225. // New in CP 3.3 - are we missing the cp_show_extended_toc option?
  226. if ( !$this->option_exists( 'cp_show_extended_toc' ) ) {
  227. // get choice
  228. $_choice = $wpdb->escape( $cp_show_extended_toc );
  229. // add chosen cp_show_extended_toc option
  230. $this->option_set( 'cp_show_extended_toc', $_choice );
  231. }
  232. // are we missing the cp_comment_editor option?
  233. if ( !$this->option_exists( 'cp_comment_editor' ) ) {
  234. // get choice
  235. $_choice = $wpdb->escape( $cp_comment_editor );
  236. // add chosen cp_comment_editor option
  237. $this->option_set( 'cp_comment_editor', $_choice );
  238. }
  239. // are we missing the cp_promote_reading option?
  240. if ( !$this->option_exists( 'cp_promote_reading' ) ) {
  241. // get choice
  242. $_choice = $wpdb->escape( $cp_promote_reading );
  243. // add chosen cp_promote_reading option
  244. $this->option_set( 'cp_promote_reading', $_choice );
  245. }
  246. // are we missing the cp_title_visibility option?
  247. if ( !$this->option_exists( 'cp_title_visibility' ) ) {
  248. // get choice
  249. $_choice = $wpdb->escape( $cp_title_visibility );
  250. // add chosen cp_title_visibility option
  251. $this->option_set( 'cp_title_visibility', $_choice );
  252. }
  253. // are we missing the cp_header_bg_colour option?
  254. if ( !$this->option_exists( 'cp_header_bg_colour' ) ) {
  255. // get choice
  256. $_choice = $wpdb->escape( $cp_header_bg_colour );
  257. // strip our rgb #
  258. if ( stristr( $_choice, '#' ) ) {
  259. $_choice = substr( $_choice, 1 );
  260. }
  261. // reset to default if blank
  262. if ( $_choice == '' ) {
  263. $_choice = $this->header_bg_colour;
  264. }
  265. // add chosen cp_header_bg_colour option
  266. $this->option_set( 'cp_header_bg_colour', $_choice );
  267. }
  268. // are we missing the cp_js_scroll_speed option?
  269. if ( !$this->option_exists( 'cp_js_scroll_speed' ) ) {
  270. // get choice
  271. $_choice = $wpdb->escape( $cp_js_scroll_speed );
  272. // add chosen cp_js_scroll_speed option
  273. $this->option_set( 'cp_js_scroll_speed', $_choice );
  274. }
  275. // are we missing the cp_min_page_width option?
  276. if ( !$this->option_exists( 'cp_min_page_width' ) ) {
  277. // get choice
  278. $_choice = $wpdb->escape( $cp_min_page_width );
  279. // add chosen cp_min_page_width option
  280. $this->option_set( 'cp_min_page_width', $_choice );
  281. }
  282. // do we still have the legacy cp_allow_users_to_minimize option?
  283. if ( $this->option_exists( 'cp_allow_users_to_minimize' ) ) {
  284. // delete old cp_allow_users_to_minimize option
  285. $this->option_delete( 'cp_allow_users_to_minimize' );
  286. }
  287. // do we have special pages?
  288. if ( $this->option_exists( 'cp_special_pages' ) ) {
  289. // if we don't have the toc page...
  290. if ( !$this->option_exists( 'cp_toc_page' ) ) {
  291. // get special pages array
  292. $special_pages = $this->option_get( 'cp_special_pages', array() );
  293. // create TOC page -> a convenience, let's us define a logo as attachment
  294. $special_pages[] = $this->_create_toc_page();
  295. // store the array of page IDs that were created
  296. $this->option_set( 'cp_special_pages', $special_pages );
  297. }
  298. }
  299. // save new CommentPress Core options
  300. $this->options_save();
  301. // store new CommentPress Core version
  302. $this->option_wp_set( 'commentpress_version', COMMENTPRESS_VERSION );
  303. }
  304. // --<
  305. return $result;
  306. }
  307. /**
  308. * @description: reset Wordpress to prior state, but retain options
  309. * @todo:
  310. *
  311. */
  312. function deactivate() {
  313. // reset comment paging option
  314. $this->_reset_comment_paging();
  315. // restore widgets
  316. $this->_reset_widgets();
  317. // always remove special pages
  318. $this->delete_special_pages();
  319. }
  320. //##############################################################################
  321. /*
  322. ============================================================================
  323. PUBLIC METHODS
  324. ============================================================================
  325. */
  326. /**
  327. * @description: update Wordpress database schema
  328. * @return boolean $result
  329. * @todo:
  330. *
  331. */
  332. function schema_update() {
  333. // database object
  334. global $wpdb;
  335. // include Wordpress upgrade script
  336. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  337. // add the column, if not already there
  338. $result = maybe_add_column(
  339. $wpdb->comments,
  340. 'comment_signature',
  341. "ALTER TABLE `$wpdb->comments` ADD `comment_signature` VARCHAR(255) NULL;"
  342. );
  343. // --<
  344. return $result;
  345. }
  346. /**
  347. * @description: upgrade Wordpress database schema
  348. * @return boolean $result
  349. * @todo:
  350. *
  351. */
  352. function schema_upgrade() {
  353. // database object
  354. global $wpdb;
  355. // init
  356. $return = false;
  357. // construct query
  358. $query = "ALTER TABLE `$wpdb->comments` CHANGE `comment_text_signature` `comment_signature` VARCHAR(255) NULL;";
  359. // do the query to rename the column
  360. $wpdb->query( $query );
  361. // test if we now have the correct column name...
  362. if ( $this->db_is_modified( 'comment_signature' ) ) {
  363. // yes
  364. $result = true;
  365. }
  366. // --<
  367. return $result;
  368. }
  369. /**
  370. * @description: do we have a column in the comments table?
  371. * @return boolean $result
  372. * @todo:
  373. *
  374. */
  375. function db_is_modified( $column_name ) {
  376. // database object
  377. global $wpdb;
  378. // init
  379. $result = false;
  380. // define query
  381. $query = "DESCRIBE $wpdb->comments";
  382. // get columns
  383. $cols = $wpdb->get_results( $query );
  384. // loop
  385. foreach( $cols AS $col ) {
  386. // is it our desired column?
  387. if ( $col->Field == $column_name ) {
  388. // we got it
  389. $result = true;
  390. break;
  391. }
  392. }
  393. // --<
  394. return $result;
  395. }
  396. /**
  397. * @description: check for plugin upgrade
  398. * @return boolean $result
  399. * @todo:
  400. *
  401. */
  402. function check_upgrade() {
  403. // init
  404. $result = false;
  405. // get installed version cast as string
  406. $_version = (string) $this->option_wp_get( 'commentpress_version' );
  407. // if we have a commentpress install and it's lower than this one
  408. if ( $_version !== false AND version_compare( COMMENTPRESS_VERSION, $_version, '>' ) ) {
  409. // override
  410. $result = true;
  411. }
  412. // --<
  413. return $result;
  414. }
  415. /**
  416. * @description: save the settings set by the administrator
  417. * @return boolean success or failure
  418. * @todo: do more error checking?
  419. *
  420. */
  421. function options_update() {
  422. // database object
  423. global $wpdb;
  424. // init result
  425. $result = false;
  426. // was the form submitted?
  427. if( isset( $_POST[ 'commentpress_submit' ] ) ) {
  428. // check that we trust the source of the data
  429. check_admin_referer( 'commentpress_admin_action', 'commentpress_nonce' );
  430. // init vars
  431. $cp_activate = '0';
  432. $cp_upgrade = '';
  433. $cp_reset = '';
  434. $cp_create_pages = '';
  435. $cp_delete_pages = '';
  436. $cp_para_comments_live = 0;
  437. $cp_show_subpages = 0;
  438. $cp_show_extended_toc = 0;
  439. $cp_blog_type = 0;
  440. $cp_blog_workflow = 0;
  441. $cp_sidebar_default = 'comments';
  442. // get variables
  443. extract( $_POST );
  444. // hand off to Multisite first, in case we're deactivating
  445. do_action( 'cpmu_deactivate_commentpress' );
  446. // is Multisite activating CommentPress Core?
  447. if ( $cp_activate == '1' ) { return true; }
  448. // did we ask to upgrade CommentPress Core?
  449. if ( $cp_upgrade == '1' ) {
  450. // do upgrade
  451. $this->upgrade();
  452. // --<
  453. return true;
  454. }
  455. // did we ask to reset?
  456. if ( $cp_reset == '1' ) {
  457. // reset theme options
  458. $this->_options_reset();
  459. // --<
  460. return true;
  461. }
  462. // did we ask to auto-create special pages?
  463. if ( $cp_create_pages == '1' ) {
  464. // remove any existing special pages
  465. $this->delete_special_pages();
  466. // create special pages
  467. $this->create_special_pages();
  468. }
  469. // did we ask to delete special pages?
  470. if ( $cp_delete_pages == '1' ) {
  471. // remove special pages
  472. $this->delete_special_pages();
  473. }
  474. // let's deal with our params now
  475. // individual special pages
  476. //$cp_welcome_page = $wpdb->escape( $cp_welcome_page );
  477. //$cp_blog_page = $wpdb->escape( $cp_blog_page );
  478. //$cp_general_comments_page = $wpdb->escape( $cp_general_comments_page );
  479. //$cp_all_comments_page = $wpdb->escape( $cp_all_comments_page );
  480. //$cp_comments_by_page = $wpdb->escape( $cp_comments_by_page );
  481. //$this->option_set( 'cp_welcome_page', $cp_welcome_page );
  482. //$this->option_set( 'cp_blog_page', $cp_blog_page );
  483. //$this->option_set( 'cp_general_comments_page', $cp_general_comments_page );
  484. //$this->option_set( 'cp_all_comments_page', $cp_all_comments_page );
  485. //$this->option_set( 'cp_comments_by_page', $cp_comments_by_page );
  486. // TOC content
  487. $cp_show_posts_or_pages_in_toc = $wpdb->escape( $cp_show_posts_or_pages_in_toc );
  488. $this->option_set( 'cp_show_posts_or_pages_in_toc', $cp_show_posts_or_pages_in_toc );
  489. // if we have pages in TOC and a value for the next param...
  490. if ( $cp_show_posts_or_pages_in_toc == 'page' AND isset( $cp_toc_chapter_is_page ) ) {
  491. $cp_toc_chapter_is_page = $wpdb->escape( $cp_toc_chapter_is_page );
  492. $this->option_set( 'cp_toc_chapter_is_page', $cp_toc_chapter_is_page );
  493. // if chapters are not pages and we have a value for the next param...
  494. if ( $cp_toc_chapter_is_page == '0' ) {
  495. $cp_show_subpages = $wpdb->escape( $cp_show_subpages );
  496. $this->option_set( 'cp_show_subpages', ( $cp_show_subpages ? 1 : 0 ) );
  497. } else {
  498. // always set to show subpages
  499. $this->option_set( 'cp_show_subpages', 1 );
  500. }
  501. }
  502. // extended or vanilla posts TOC
  503. if ( $cp_show_posts_or_pages_in_toc == 'post' ) {
  504. $cp_show_extended_toc = $wpdb->escape( $cp_show_extended_toc );
  505. $this->option_set( 'cp_show_extended_toc', ( $cp_show_extended_toc ? 1 : 0 ) );
  506. }
  507. // excerpt length
  508. $this->option_set( 'cp_excerpt_length', $cp_excerpt_length );
  509. $cp_excerpt_length = $wpdb->escape( $cp_excerpt_length );
  510. // comment editor
  511. $cp_comment_editor = $wpdb->escape( $cp_comment_editor );
  512. $this->option_set( 'cp_comment_editor', ( $cp_comment_editor ? 1 : 0 ) );
  513. // has AJAX "live" comment refreshing been migrated?
  514. if ( $this->option_exists( 'cp_para_comments_live' ) ) {
  515. // "live" comment refreshing
  516. $cp_para_comments_live = $wpdb->escape( $cp_para_comments_live );
  517. $this->option_set( 'cp_para_comments_live', ( $cp_para_comments_live ? 1 : 0 ) );
  518. }
  519. // behaviour
  520. $cp_promote_reading = $wpdb->escape( $cp_promote_reading );
  521. $this->option_set( 'cp_promote_reading', ( $cp_promote_reading ? 1 : 0 ) );
  522. // title visibility
  523. $cp_title_visibility = $wpdb->escape( $cp_title_visibility );
  524. $this->option_set( 'cp_title_visibility', $cp_title_visibility );
  525. // page meta visibility
  526. $cp_page_meta_visibility = $wpdb->escape( $cp_page_meta_visibility );
  527. $this->option_set( 'cp_page_meta_visibility', $cp_page_meta_visibility );
  528. // header background colour
  529. // strip our rgb #
  530. if ( stristr( $cp_header_bg_colour, '#' ) ) {
  531. $cp_header_bg_colour = substr( $cp_header_bg_colour, 1 );
  532. }
  533. // reset to default if blank
  534. if ( $cp_header_bg_colour == '' ) {
  535. $cp_header_bg_colour = $this->header_bg_colour;
  536. }
  537. // save it
  538. $cp_header_bg_colour = $wpdb->escape( $cp_header_bg_colour );
  539. $this->option_set( 'cp_header_bg_colour', $cp_header_bg_colour );
  540. // save scroll speed
  541. $cp_js_scroll_speed = $wpdb->escape( $cp_js_scroll_speed );
  542. $this->option_set( 'cp_js_scroll_speed', $cp_js_scroll_speed );
  543. // save min page width
  544. $cp_min_page_width = $wpdb->escape( $cp_min_page_width );
  545. $this->option_set( 'cp_min_page_width', $cp_min_page_width );
  546. // save workflow
  547. $cp_blog_workflow = $wpdb->escape( $cp_blog_workflow );
  548. $this->option_set( 'cp_blog_workflow', ( $cp_blog_workflow ? 1 : 0 ) );
  549. // save blog type
  550. $cp_blog_type = $wpdb->escape( $cp_blog_type );
  551. $this->option_set( 'cp_blog_type', $cp_blog_type );
  552. // if it's a groupblog
  553. if ( $this->parent_obj->is_groupblog() ) {
  554. // get the group's id
  555. $group_id = get_groupblog_group_id( get_current_blog_id() );
  556. if ( is_numeric( $group_id ) ) {
  557. // allow plugins to override the blog type - for example if workflow is enabled,
  558. // it might become a new blog type as far as buddypress is concerned
  559. $_blog_type = apply_filters( 'cp_get_group_meta_for_blog_type', $cp_blog_type, $cp_blog_workflow );
  560. // set the type as group meta info
  561. groups_update_groupmeta( $group_id, 'groupblogtype', 'groupblogtype-'.$_blog_type );
  562. }
  563. }
  564. // save default sidebar
  565. $cp_sidebar_default = $wpdb->escape( $cp_sidebar_default );
  566. $this->option_set( 'cp_sidebar_default', $cp_sidebar_default );
  567. // save
  568. $this->options_save();
  569. // set flag
  570. $result = true;
  571. }
  572. // --<
  573. return $result;
  574. }
  575. /**
  576. * @description: upgrade CommentPress Core options to array
  577. * @todo:
  578. *
  579. */
  580. function options_save() {
  581. // set option
  582. return $this->option_wp_set( 'commentpress_options', $this->commentpress_options );
  583. }
  584. /**
  585. * @description: return a value for a specified option
  586. * @todo:
  587. */
  588. function option_exists( $option_name = '' ) {
  589. // test for null
  590. if ( $option_name == '' ) {
  591. // oops
  592. die( __( 'You must supply an option to option_exists()', 'commentpress-core' ) );
  593. }
  594. // get option with unlikely default
  595. return array_key_exists( $option_name, $this->commentpress_options );
  596. }
  597. /**
  598. * @description: return a value for a specified option
  599. * @todo:
  600. */
  601. function option_get( $option_name = '', $default = false ) {
  602. // test for null
  603. if ( $option_name == '' ) {
  604. // oops
  605. die( __( 'You must supply an option to option_get()', 'commentpress-core' ) );
  606. }
  607. // get option
  608. return ( array_key_exists( $option_name, $this->commentpress_options ) ) ? $this->commentpress_options[ $option_name ] : $default;
  609. }
  610. /**
  611. * @description: sets a value for a specified option
  612. * @todo:
  613. */
  614. function option_set( $option_name = '', $value = '' ) {
  615. // test for null
  616. if ( $option_name == '' ) {
  617. // oops
  618. die( __( 'You must supply an option to option_set()', 'commentpress-core' ) );
  619. }
  620. // test for other than string
  621. if ( !is_string( $option_name ) ) {
  622. // oops
  623. die( __( 'You must supply the option as a string to option_set()', 'commentpress-core' ) );
  624. }
  625. // set option
  626. $this->commentpress_options[ $option_name ] = $value;
  627. }
  628. /**
  629. * @description: deletes a specified option
  630. * @todo:
  631. */
  632. function option_delete( $option_name = '' ) {
  633. // test for null
  634. if ( $option_name == '' ) {
  635. // oops
  636. die( __( 'You must supply an option to option_delete()', 'commentpress-core' ) );
  637. }
  638. // unset option
  639. unset( $this->commentpress_options[ $option_name ] );
  640. }
  641. /**
  642. * @description: return a value for a specified option
  643. * @todo:
  644. */
  645. function option_wp_exists( $option_name = '' ) {
  646. // test for null
  647. if ( $option_name == '' ) {
  648. // oops
  649. die( __( 'You must supply an option to option_wp_exists()', 'commentpress-core' ) );
  650. }
  651. // get option with unlikely default
  652. if ( $this->option_wp_get( $option_name, 'fenfgehgejgrkj' ) == 'fenfgehgejgrkj' ) {
  653. // no
  654. return false;
  655. } else {
  656. // yes
  657. return true;
  658. }
  659. }
  660. /**
  661. * @description: return a value for a specified option
  662. * @todo:
  663. */
  664. function option_wp_get( $option_name = '', $default = false ) {
  665. // test for null
  666. if ( $option_name == '' ) {
  667. // oops
  668. die( __( 'You must supply an option to option_wp_get()', 'commentpress-core' ) );
  669. }
  670. // get option
  671. return get_option( $option_name, $default );
  672. }
  673. /**
  674. * @description: sets a value for a specified option
  675. * @todo:
  676. */
  677. function option_wp_set( $option_name = '', $value = null ) {
  678. // test for null
  679. if ( $option_name == '' ) {
  680. // oops
  681. die( __( 'You must supply an option to option_wp_set()', 'commentpress-core' ) );
  682. }
  683. // set option
  684. return update_option( $option_name, $value );
  685. }
  686. /**
  687. * @description: get default header bg colour
  688. * @todo:
  689. */
  690. function option_get_header_bg() {
  691. // test for option
  692. if ( $this->option_exists( 'cp_header_bg_colour' ) ) {
  693. // --<
  694. return $this->option_get( 'cp_header_bg_colour' );
  695. } else {
  696. // --<
  697. return $this->header_bg_colour;
  698. }
  699. }
  700. /**
  701. * @description: when a page is saved, this also saves the CP options
  702. * @param object $post_obj the post object
  703. * @return boolean $result
  704. * @todo:
  705. *
  706. */
  707. function save_meta( $post_obj ) {
  708. // if no post, kick out
  709. if ( !$post_obj ) { return; }
  710. // if page...
  711. if ( $post_obj->post_type == 'page' ) {
  712. $this->save_page_meta( $post_obj );
  713. }
  714. // if post...
  715. if ( $post_obj->post_type == 'post' ) {
  716. $this->save_post_meta( $post_obj );
  717. }
  718. }
  719. /**
  720. * @description: when a page is saved, this also saves the CP options
  721. * @param object $post_obj the post object
  722. * @return boolean $result
  723. * @todo:
  724. *
  725. */
  726. function save_page_meta( $post_obj ) {
  727. //print_r( 'data: '.$_data ); die();
  728. //print_r( '$post_obj->post_type: '.$post_obj->post_type ); die();
  729. //print_r( '$post_obj->ID: '.$post_obj->ID ); die();
  730. // if no post, kick out
  731. if ( !$post_obj ) { return; }
  732. // if not page, kick out
  733. if ( $post_obj->post_type != 'page' ) { return; }
  734. // authenticate
  735. $_nonce = isset( $_POST['commentpress_nonce'] ) ? $_POST['commentpress_nonce'] : '';
  736. if ( !wp_verify_nonce( $_nonce, 'commentpress_page_settings' ) ) { return; }
  737. // is this an auto save routine?
  738. if ( defined('DOING_AUTOSAVE') AND DOING_AUTOSAVE ) { return; }
  739. // check permissions - 'edit_pages' is available to editor+
  740. if ( !current_user_can( 'edit_pages' ) ) { return; }
  741. // OK, we're authenticated
  742. // check for revision
  743. if ( $post_obj->post_type == 'revision' ) {
  744. // get parent
  745. if ( $post_obj->post_parent != 0 ) {
  746. $post = get_post( $post_obj->post_parent );
  747. } else {
  748. $post = $post_obj;
  749. }
  750. } else {
  751. $post = $post_obj;
  752. }
  753. // database object and post
  754. global $wpdb;
  755. // ---------------------------------------------------------------------
  756. // Show or Hide Page Meta
  757. // ---------------------------------------------------------------------
  758. // find and save the data
  759. $_data = ( isset( $_POST['cp_page_meta_visibility'] ) ) ? $_POST['cp_page_meta_visibility'] : 'hide';
  760. //print_r( '$_data: '.$_data ); die();
  761. //print_r( $post ); die();
  762. // set key
  763. $key = '_cp_page_meta_visibility';
  764. // if the custom field already has a value...
  765. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  766. // if empty string...
  767. if ( $_data === '' ) {
  768. // delete the meta_key
  769. delete_post_meta( $post->ID, $key );
  770. } else {
  771. // update the data
  772. update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  773. }
  774. } else {
  775. // add the data
  776. add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  777. }
  778. // ---------------------------------------------------------------------
  779. // Show or Hide Page Title
  780. // ---------------------------------------------------------------------
  781. // find and save the data
  782. $_data = ( isset( $_POST['cp_title_visibility'] ) ) ? $_POST['cp_title_visibility'] : 'show';
  783. //print_r( '$_data: '.$_data ); die();
  784. //print_r( $post ); die();
  785. // set key
  786. $key = '_cp_title_visibility';
  787. // if the custom field already has a value...
  788. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  789. // if empty string...
  790. if ( $_data === '' ) {
  791. // delete the meta_key
  792. delete_post_meta( $post->ID, $key );
  793. } else {
  794. // update the data
  795. update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  796. }
  797. } else {
  798. // add the data
  799. add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  800. }
  801. // ---------------------------------------------------------------------
  802. // Page Numbering - only first top level page is allowed to send this
  803. // ---------------------------------------------------------------------
  804. // was the value sent?
  805. if ( isset( $_POST['cp_number_format'] ) ) {
  806. // set meta key
  807. $key = '_cp_number_format';
  808. if (
  809. // do we need to check this, since only the first top level page
  810. // can now send this data? doesn't hurt to validate, I guess.
  811. $post->post_parent == '0' AND
  812. !$this->is_special_page() AND
  813. $post->ID == $this->parent_obj->nav->get_first_page()
  814. ) { // -->
  815. // get the data
  816. $_data = $_POST['cp_number_format'];
  817. //print_r( $post->ID ); die();
  818. // if the custom field already has a value...
  819. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  820. // if empty string...
  821. if ( $_data === '' ) {
  822. // delete the meta_key
  823. delete_post_meta( $post->ID, $key );
  824. } else {
  825. // update the data
  826. update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  827. }
  828. } else {
  829. // add the data
  830. add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  831. }
  832. }
  833. // delete this meta value from all other pages, because we may have altered
  834. // the relationship between pages, thus causing the page numbering to fail
  835. // get all pages including chapters
  836. $all_pages = $this->parent_obj->nav->get_book_pages( 'structural' );
  837. // if we have any pages...
  838. if ( count( $all_pages ) > 0 ) {
  839. // loop
  840. foreach( $all_pages AS $_page ) {
  841. // exclude first top level page
  842. if ( $post->ID != $_page->ID ) {
  843. // delete the meta value
  844. delete_post_meta( $_page->ID, $key );
  845. }
  846. }
  847. }
  848. }
  849. // ---------------------------------------------------------------------
  850. // Page Layout for Title Page -> to allow for Book Cover image
  851. // ---------------------------------------------------------------------
  852. // is this the title page?
  853. if ( $post->ID == $this->option_get( 'cp_welcome_page' ) ) {
  854. // find and save the data
  855. $_data = ( isset( $_POST['cp_page_layout'] ) ) ? $_POST['cp_page_layout'] : 'text';
  856. // set key
  857. $key = '_cp_page_layout';
  858. // if the custom field already has a value...
  859. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  860. // if empty string...
  861. if ( $_data === '' ) {
  862. // delete the meta_key
  863. delete_post_meta( $post->ID, $key );
  864. } else {
  865. // update the data
  866. update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  867. }
  868. } else {
  869. // add the data
  870. add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  871. }
  872. }
  873. // ---------------------------------------------------------------------
  874. // Override post formatter (override blog_type)
  875. // ---------------------------------------------------------------------
  876. // get the data
  877. $_data = ( isset( $_POST['cp_post_type_override'] ) ) ? $_POST['cp_post_type_override'] : '';
  878. //print_r( '$_data: '.$_data ); die();
  879. //print_r( $post ); die();
  880. // set key
  881. $key = '_cp_post_type_override';
  882. // if the custom field already has a value...
  883. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  884. // if empty string...
  885. if ( $_data === '' ) {
  886. // delete the meta_key
  887. delete_post_meta( $post->ID, $key );
  888. } else {
  889. // update the data
  890. update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  891. }
  892. } else {
  893. // add the data
  894. add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  895. }
  896. // ---------------------------------------------------------------------
  897. // Default Sidebar
  898. // ---------------------------------------------------------------------
  899. // do we have the option to choose the default sidebar (new in 3.3.3)?
  900. if ( $this->option_exists( 'cp_sidebar_default' ) ) {
  901. // find and save the data
  902. $_data = ( isset( $_POST['cp_sidebar_default'] ) ) ?
  903. $_POST['cp_sidebar_default'] :
  904. $this->db->option_get( 'cp_sidebar_default' );
  905. //print_r( '$_data: '.$_data ); die();
  906. //print_r( $post ); die();
  907. // set key
  908. $key = '_cp_sidebar_default';
  909. // if the custom field already has a value...
  910. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  911. // if empty string...
  912. if ( $_data === '' ) {
  913. // delete the meta_key
  914. delete_post_meta( $post->ID, $key );
  915. } else {
  916. // update the data
  917. update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  918. }
  919. } else {
  920. // add the data
  921. add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  922. }
  923. }
  924. }
  925. /**
  926. * @description: when a post is saved, this also saves the CP options
  927. * @param object $post_obj the post object
  928. * @return boolean $result
  929. * @todo:
  930. *
  931. */
  932. function save_post_meta( $post_obj ) {
  933. //print_r( 'data: '.$_data ); die();
  934. //print_r( '$post_obj->post_type: '.$post_obj->post_type ); die();
  935. //print_r( '$post_obj->ID: '.$post_obj->ID ); die();
  936. // if no post, kick out
  937. if ( !$post_obj ) { return; }
  938. // if not page, kick out
  939. if ( $post_obj->post_type != 'post' ) { return; }
  940. // authenticate
  941. $_nonce = isset( $_POST['commentpress_nonce'] ) ? $_POST['commentpress_nonce'] : '';
  942. if ( !wp_verify_nonce( $_nonce, 'commentpress_post_settings' ) ) { return; }
  943. // is this an auto save routine?
  944. if ( defined('DOING_AUTOSAVE') AND DOING_AUTOSAVE ) { return; }
  945. // check permissions - 'edit_posts' is available to contributor+
  946. if ( !current_user_can( 'edit_posts', $post_obj->ID ) ) { return; }
  947. // OK, we're authenticated
  948. // check for revision
  949. if ( $post_obj->post_type == 'revision' ) {
  950. // get parent
  951. if ( $post_obj->post_parent != 0 ) {
  952. $post = get_post( $post_obj->post_parent );
  953. } else {
  954. $post = $post_obj;
  955. }
  956. } else {
  957. $post = $post_obj;
  958. }
  959. // database object and post
  960. global $wpdb;
  961. // ---------------------------------------------------------------------
  962. // Override post formatter (override blog_type)
  963. // ---------------------------------------------------------------------
  964. // get the data
  965. $_formatter = ( isset( $_POST['cp_post_type_override'] ) ) ? $_POST['cp_post_type_override'] : '';
  966. //print_r( '$_data: '.$_data ); die();
  967. //print_r( $post ); die();
  968. // set key
  969. $key = '_cp_post_type_override';
  970. // if the custom field already has a value...
  971. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  972. // if empty string...
  973. if ( $_formatter === '' ) {
  974. // delete the meta_key
  975. delete_post_meta( $post->ID, $key );
  976. } else {
  977. // update the data
  978. update_post_meta( $post->ID, $key, $wpdb->escape( $_formatter ) );
  979. }
  980. } else {
  981. // add the data
  982. add_post_meta( $post->ID, $key, $wpdb->escape( $_formatter ) );
  983. }
  984. // ---------------------------------------------------------------------
  985. // Workflow
  986. // ---------------------------------------------------------------------
  987. // do we have the option to set workflow (new in 3.3.1)?
  988. if ( $this->option_exists( 'cp_blog_workflow' ) ) {
  989. // get workflow setting for the blog
  990. $_workflow = $this->option_get( 'cp_blog_workflow' );
  991. /*
  992. // ----------------
  993. // WORK IN PROGRESS
  994. // set key
  995. $key = '_cp_blog_workflow_override';
  996. // if the custom field already has a value...
  997. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  998. // get existing value
  999. $_workflow = get_post_meta( $post->ID, $key, true );
  1000. }
  1001. // ----------------
  1002. */
  1003. // if it's enabled for the blog or the post...
  1004. if ( $_workflow == '1' ) {
  1005. // notify plugins that workflow stuff needs saving
  1006. do_action( 'cp_workflow_save_post', $post );
  1007. }
  1008. /*
  1009. // ----------------
  1010. // WORK IN PROGRESS
  1011. // get the setting for the post (we do this after saving the extra
  1012. // post data because
  1013. $_formatter = ( isset( $_POST['cp_post_type_override'] ) ) ? $_POST['cp_post_type_override'] : '';
  1014. // if the custom field already has a value...
  1015. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  1016. // if empty string...
  1017. if ( $_data === '' ) {
  1018. // delete the meta_key
  1019. delete_post_meta( $post->ID, $key );
  1020. } else {
  1021. // update the data
  1022. update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  1023. }
  1024. } else {
  1025. // add the data
  1026. add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  1027. }
  1028. // ----------------
  1029. */
  1030. }
  1031. // ---------------------------------------------------------------------
  1032. // Default Sidebar
  1033. // ---------------------------------------------------------------------
  1034. // do we have the option to choose the default sidebar (new in 3.3.3)?
  1035. if ( $this->option_exists( 'cp_sidebar_default' ) ) {
  1036. // find and save the data
  1037. $_data = ( isset( $_POST['cp_sidebar_default'] ) ) ?
  1038. $_POST['cp_sidebar_default'] :
  1039. $this->db->option_get( 'cp_sidebar_default' );
  1040. //print_r( '$_data: '.$_data ); die();
  1041. //print_r( $post ); die();
  1042. // set key
  1043. $key = '_cp_sidebar_default';
  1044. // if the custom field already has a value...
  1045. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  1046. // if empty string...
  1047. if ( $_data === '' ) {
  1048. // delete the meta_key
  1049. delete_post_meta( $post->ID, $key );
  1050. } else {
  1051. // update the data
  1052. update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  1053. }
  1054. } else {
  1055. // add the data
  1056. add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  1057. }
  1058. }
  1059. // ---------------------------------------------------------------------
  1060. // Create new post with content of current
  1061. // ---------------------------------------------------------------------
  1062. // find and save the data
  1063. $_data = ( isset( $_POST['commentpress_new_post'] ) ) ? $_POST['commentpress_new_post'] : '0';
  1064. /*
  1065. print_r( array(
  1066. '$_data' => $_data,
  1067. '$post ' => $post
  1068. ) ); die();
  1069. */
  1070. // do we want to create a new revision?
  1071. if ( $_data == '0' ) { return; }
  1072. // we need to make sure this only runs once
  1073. //print_r( $this->saved_post === false ? 'f' : 't' ); die();
  1074. if ( $this->saved_post === false ) {
  1075. $this->saved_post = true;
  1076. } else {
  1077. return;
  1078. }
  1079. // ---------------------------------------------------------------------
  1080. // we're through: create it
  1081. $new_post_id = $this->_create_new_post( $post );
  1082. // ---------------------------------------------------------------------
  1083. // Store ID of new version in current version
  1084. // ---------------------------------------------------------------------
  1085. // set key
  1086. $key = '_cp_newer_version';
  1087. // if the custom field already has a value...
  1088. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  1089. // if empty string...
  1090. if ( $_data === '' ) {
  1091. // delete the meta_key
  1092. delete_post_meta( $post->ID, $key );
  1093. } else {
  1094. // update the data
  1095. update_post_meta( $post->ID, $key, $new_post_id );
  1096. }
  1097. } else {
  1098. // add the data
  1099. add_post_meta( $post->ID, $key, $new_post_id );
  1100. }
  1101. // ---------------------------------------------------------------------
  1102. // Store incremental version number in new version
  1103. // ---------------------------------------------------------------------
  1104. // set key
  1105. $key = '_cp_version_count';
  1106. // if the custom field of our current post has a value...
  1107. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  1108. // get current value
  1109. $value = get_post_meta( $post->ID, $key, true );
  1110. // increment
  1111. $value++;
  1112. } else {
  1113. // this must be the first new version (Draft 2)
  1114. $value = 2;
  1115. }
  1116. // add the data
  1117. add_post_meta( $new_post_id, $key, $value );
  1118. // ---------------------------------------------------------------------
  1119. // Store formatter in new version
  1120. // ---------------------------------------------------------------------
  1121. // set key
  1122. $key = '_cp_post_type_override';
  1123. // if we have one set...
  1124. if ( $_formatter != '' ) {
  1125. // add the data
  1126. add_post_meta( $new_post_id, $key, $wpdb->escape( $_formatter ) );
  1127. }
  1128. // allow plugins to hook into this
  1129. do_action( 'cp_workflow_save_copy', $new_post_id );
  1130. // get the edit post link
  1131. //$edit_link = get_edit_post_link( $new_post_id );
  1132. // redirect there?
  1133. }
  1134. /**
  1135. * @description: when a page is deleted, this makes sure that the CP options are synced
  1136. * @param object $post_id the post ID
  1137. * @return none
  1138. * @todo:
  1139. *
  1140. */
  1141. function delete_meta( $post_id ) {
  1142. // if no post, kick out
  1143. if ( !$post_id ) { return; }
  1144. // if it's our welcome page...
  1145. if ( $post_id == $this->option_get( 'cp_welcome_page' ) ) {
  1146. // delete option
  1147. $this->option_delete( 'cp_welcome_page' );
  1148. // save
  1149. $this->options_save();
  1150. }
  1151. // for posts with versions, we need to delete the version data for the previous version
  1152. // define key
  1153. $key = '_cp_newer_version';
  1154. // get posts with the about-to-be-deleted post_id (there will be only one, if at all)
  1155. $previous_versions = get_posts( array(
  1156. 'meta_key' => $key,
  1157. 'meta_value' => $post_id
  1158. ) );
  1159. // did we get one?
  1160. if ( count( $previous_versions ) > 0 ) {
  1161. // get it
  1162. $previous_version = $previous_versions[0];
  1163. // if the custom field has a value...
  1164. if ( get_post_meta( $previous_version->ID, $key, true ) !== '' ) {
  1165. // delete it
  1166. delete_post_meta( $previous_version->ID, $key );
  1167. }
  1168. }
  1169. }
  1170. /**
  1171. * @description: create all "special" pages
  1172. * @todo:
  1173. *
  1174. */
  1175. function create_special_pages() {
  1176. // one of the CommentPress Core themes MUST be active...
  1177. // or WordPress will fail to set the page templates for the pages that require them.
  1178. // Also, a user must be logged in for these pages to be associated with them.
  1179. // get special pages array, if it's there
  1180. $special_pages = $this->option_get( 'cp_special_pages', array() );
  1181. // create welcome/title page, but don't add to special pages
  1182. $welcome = $this->_create_title_page();
  1183. // create general comments page
  1184. $special_pages[] = $this->_create_general_comments_page();
  1185. // create all comments page
  1186. $special_pages[] = $this->_create_all_comments_page();
  1187. // create comments by author page
  1188. $special_pages[] = $this->_create_comments_by_author_page();
  1189. // create blog page
  1190. $special_pages[] = $this->_create_blog_page();
  1191. // create blog archive page
  1192. $special_pages[] = $this->_create_blog_archive_page();
  1193. // create TOC page -> a convenience, let's us define a logo as attachment
  1194. $special_pages[] = $this->_create_toc_page();
  1195. // store the array of page IDs that were created
  1196. $this->option_set( 'cp_special_pages', $special_pages );
  1197. // save changes
  1198. $this->options_save();
  1199. }
  1200. /**
  1201. * @description: create a particular "special" page
  1202. * @todo:
  1203. *
  1204. */
  1205. function create_special_page( $_page ) {
  1206. // init
  1207. $new_id = false;
  1208. // get special pages array, if it's there
  1209. $special_pages = $this->option_get( 'cp_special_pages', array() );
  1210. // switch by page
  1211. switch( $_page ) {
  1212. case 'title':
  1213. // create welcome/title page
  1214. $new_id = $this->_create_title_page();
  1215. break;
  1216. case 'general_comments':
  1217. // create general comments page
  1218. $new_id = $this->_create_general_comments_page();
  1219. break;
  1220. case 'all_comments':
  1221. // create all comments page
  1222. $new_id = $this->_create_all_comments_page();
  1223. break;
  1224. case 'comments_by_author':
  1225. // create comments by author page
  1226. $new_id = $this->_create_comments_by_author_page();
  1227. break;
  1228. case 'blog':
  1229. // create blog page
  1230. $new_id = $this->_create_blog_page();
  1231. break;
  1232. case 'blog_archive':
  1233. // create blog page
  1234. $new_id = $this->_create_blog_archive_page();
  1235. break;
  1236. case 'toc':
  1237. // create TOC page
  1238. $new_id = $this->_create_toc_page();
  1239. break;
  1240. }
  1241. // add to special pages
  1242. $special_pages[] = $new_id;
  1243. // reset option
  1244. $this->option_set( 'cp_special_pages', $special_pages );
  1245. // save changes
  1246. $this->options_save();
  1247. // --<
  1248. return $new_id;
  1249. }
  1250. /**
  1251. * @description: delete "special" pages
  1252. * @return boolean $success
  1253. * @todo:
  1254. *
  1255. */
  1256. function delete_special_pages() {
  1257. // init success flag
  1258. $success = true;
  1259. // only delete special pages if we have one of the CommentPress Core themes active
  1260. // because other themes may have a totally different way of presenting the
  1261. // content of the blog
  1262. // retrieve data on special pages
  1263. $special_pages = $this->option_get( 'cp_special_pages', array() );
  1264. // if we have created any...
  1265. if ( is_array( $special_pages ) AND count( $special_pages ) > 0 ) {
  1266. // loop through them
  1267. foreach( $special_pages AS $special_page ) {
  1268. // bypass trash
  1269. $force_delete = true;
  1270. // try and delete each page...
  1271. if ( !wp_delete_post( $special_page, $force_delete ) ) {
  1272. // oops, set success flag to false
  1273. $success = false;
  1274. }
  1275. }
  1276. // delete the corresponding options
  1277. $this->option_delete( 'cp_special_pages' );
  1278. $this->option_delete( 'cp_blog_page' );
  1279. $this->option_delete( 'cp_blog_archive_page' );
  1280. $this->option_delete( 'cp_general_comments_page' );
  1281. $this->option_delete( 'cp_all_comments_page' );
  1282. $this->option_delete( 'cp_comments_by_page' );
  1283. $this->option_delete( 'cp_toc_page' );
  1284. // for now, keep welcome page - delete option when page is deleted
  1285. //$this->option_delete( 'cp_welcome_page' );
  1286. // save changes
  1287. $this->options_save();
  1288. // reset Wordpress internal page references
  1289. $this->_reset_wordpress_option( 'show_on_front' );
  1290. $this->_reset_wordpress_option( 'page_on_front' );
  1291. $this->_reset_wordpress_option( 'page_for_posts' );
  1292. }
  1293. // --<
  1294. return $success;
  1295. }
  1296. /**
  1297. * @description: delete a particular "special" page
  1298. * @return boolean $success
  1299. * @todo:
  1300. *
  1301. */
  1302. function delete_special_page( $_page ) {
  1303. // init success flag
  1304. $success = true;
  1305. // only delete a special page if we have one of the CommentPress Core themes active
  1306. // because other themes may have a totally different way of presenting the
  1307. // content of the blog
  1308. // get id of special page
  1309. switch( $_page ) {
  1310. case 'title':
  1311. // set flag
  1312. $flag = 'cp_welcome_page';
  1313. // reset Wordpress internal page references
  1314. $this->_reset_wordpress_option( 'show_on_front' );
  1315. $this->_reset_wordpress_option( 'page_on_front' );
  1316. break;
  1317. case 'general_comments':
  1318. // set flag
  1319. $flag = 'cp_general_comments_page';
  1320. break;
  1321. case 'all_comments':
  1322. // set flag
  1323. $flag = 'cp_all_comments_page';
  1324. break;
  1325. case 'comments_by_author':
  1326. // set flag
  1327. $flag = 'cp_comments_by_page';
  1328. break;
  1329. case 'blog':
  1330. // set flag
  1331. $flag = 'cp_blog_page';
  1332. // reset Wordpress internal page reference
  1333. $this->_reset_wordpress_option( 'page_for_posts' );
  1334. break;
  1335. case 'blog_archive':
  1336. // set flag
  1337. $flag = 'cp_blog_archive_page';
  1338. break;
  1339. case 'toc':
  1340. // set flag
  1341. $flag = 'cp_toc_page';
  1342. break;
  1343. }
  1344. // get page id
  1345. $page_id = $this->option_get( $flag );
  1346. // kick out if it doesn't exist
  1347. if ( !$page_id ) { return true; }
  1348. // delete option
  1349. $this->option_delete( $flag );
  1350. // bypass trash
  1351. $force_delete = true;
  1352. // try and delete the page...
  1353. if ( !wp_delete_post( $page_id, $force_delete ) ) {
  1354. // oops, set success flag to false
  1355. $success = false;
  1356. }
  1357. // retrieve data on special pages
  1358. $special_pages = $this->option_get( 'cp_special_pages', array() );
  1359. // is it in our special pages array?
  1360. if ( in_array( $page_id, $special_pages ) ) {
  1361. // remove page id from array
  1362. $special_pages = array_diff( $special_pages, array( $page_id ) );
  1363. // reset option
  1364. $this->option_set( 'cp_special_pages', $special_pages );
  1365. }
  1366. // save changes
  1367. $this->options_save();
  1368. // --<
  1369. return $success;
  1370. }
  1371. /**
  1372. * @description: test if a page is a "special" page
  1373. * @return boolean $is_special_page
  1374. * @todo:
  1375. *
  1376. */
  1377. function is_special_page() {
  1378. // init flag
  1379. $is_special_page = false;
  1380. // access post object
  1381. global $post;
  1382. // do we have one?
  1383. if ( !is_object( $post ) ) {
  1384. // --<
  1385. return $is_special_page;
  1386. }
  1387. // get special pages
  1388. $special_pages = $this->option_get( 'cp_special_pages', array() );
  1389. // do we have a special page array?
  1390. if ( is_array( $special_pages ) AND count( $special_pages ) > 0 ) {
  1391. // is the current page one?
  1392. if ( in_array( $post->ID, $special_pages ) ) {
  1393. // it is…

Large files files are truncated, but you can click here to view the full file