/app/model/class-ms-model-pages.php

  1. <?php 
  2. /** 
  3. * Plugin Pages model. 
  4. * 
  5. * Main MS Pages class, contains any Membership page functions. 
  6. * 
  7. * @since 1.0.0 
  8. * 
  9. * @package Membership2 
  10. * @subpackage Model 
  11. */ 
  12. class MS_Model_Pages extends MS_Model_Option { 
  13.  
  14. /** 
  15. * Singleton instance. 
  16. * 
  17. * @since 1.0.0 
  18. * 
  19. * @staticvar MS_Model_Settings 
  20. */ 
  21. public static $instance; 
  22.  
  23. /** 
  24. * Plugin pages constants. 
  25. * 
  26. * @since 1.0.0 
  27. * 
  28. * @var string 
  29. */ 
  30. const MS_PAGE_MEMBERSHIPS = 'memberships'; 
  31. const MS_PAGE_PROTECTED_CONTENT = 'protected-content'; 
  32. const MS_PAGE_ACCOUNT = 'account'; 
  33. const MS_PAGE_REGISTER = 'register'; 
  34. const MS_PAGE_REG_COMPLETE = 'registration-complete'; 
  35.  
  36. /** 
  37. * Association between membership page-types and WordPress post_ids. 
  38. * 
  39. * @since 1.0.0 
  40. * 
  41. * @var array 
  42. */ 
  43. public $settings = array(); 
  44.  
  45. /** 
  46. * Returns the singleton instance of the MS_Model_Pages object 
  47. * 
  48. * @since 1.0.0 
  49. * @return MS_Model_Pages 
  50. */ 
  51. static public function get_model() { 
  52. static $Model = null; 
  53.  
  54. if ( null === $Model ) { 
  55. $Model = MS_Factory::load( 'MS_Model_Pages' ); 
  56.  
  57. return $Model; 
  58.  
  59. /** 
  60. * Returns a MS_Model_Pages setting value (these are the association between 
  61. * our Membership Page types and WordPress posts) 
  62. * 
  63. * @since 1.0.0 
  64. * @param string $key The setting key. 
  65. * @return any The setting value. A post_id or 0. 
  66. */ 
  67. static public function get_setting( $key ) { 
  68. $model = self::get_model(); 
  69.  
  70. if ( ! isset( $model->settings[ $key ] ) ) { 
  71. $model->settings[$key] = 0; 
  72.  
  73. return apply_filters( 
  74. 'ms_model_pages_get_setting',  
  75. $model->settings[$key],  
  76. $key 
  77. ); 
  78.  
  79. /** 
  80. * Saves a MS_Model_Pages setting value. 
  81. * 
  82. * @since 1.0.0 
  83. * @param string $key The setting key. 
  84. * @param any $value The new setting value. 
  85. */ 
  86. static public function set_setting( $key, $value ) { 
  87. $model = self::get_model(); 
  88.  
  89. $value = apply_filters( 
  90. 'ms_model_pages_set_setting',  
  91. $value,  
  92. $key 
  93. ); 
  94.  
  95. $model->settings[$key] = $value; 
  96. $model->save(); 
  97.  
  98. /** 
  99. * Get MS Page types 
  100. * 
  101. * @since 1.0.0 
  102. * 
  103. * @return array{ 
  104. * @type string $page_type The ms page type. 
  105. * @type string $title The page type title. 
  106. * } 
  107. */ 
  108. static public function get_page_types() { 
  109. static $Page_types; 
  110.  
  111. if ( empty( $Page_types ) ) { 
  112. $Page_types = array( 
  113. self::MS_PAGE_MEMBERSHIPS => __( 'Membership List', 'membership2' ),  
  114. self::MS_PAGE_PROTECTED_CONTENT => __( 'Protected Content', 'membership2' ),  
  115. self::MS_PAGE_REGISTER => __( 'Registration', 'membership2' ),  
  116. self::MS_PAGE_REG_COMPLETE => __( 'Thank-You Page', 'membership2' ),  
  117. self::MS_PAGE_ACCOUNT => __( 'Account', 'membership2' ),  
  118. ); 
  119.  
  120. $Page_types = apply_filters( 
  121. 'ms_model_pages_get_page_types',  
  122. $Page_types 
  123. ); 
  124.  
  125. return $Page_types; 
  126.  
  127. /** 
  128. * Returns a longer description for a page-type 
  129. * 
  130. * @since 1.0.0 
  131. * @param string $type The page-type 
  132. * @return string The full description 
  133. */ 
  134. static public function get_description( $type ) { 
  135. static $Description = null; 
  136.  
  137. if ( null === $Description ) { 
  138. $Description = array( 
  139. self::MS_PAGE_MEMBERSHIPS => __( '(Only for logged-in users) List the public memberships and signup.', 'membership2' ),  
  140. self::MS_PAGE_PROTECTED_CONTENT => __( 'Displayed when a user cannot access the requested page.', 'membership2' ),  
  141. self::MS_PAGE_REGISTER => __( '(Only for guests) List the public memberships and the registration form.', 'membership2' ),  
  142. self::MS_PAGE_REG_COMPLETE => __( 'Displayed after the user signed up for a membership.', 'membership2' ),  
  143. self::MS_PAGE_ACCOUNT => __( 'Shows details about the current user.', 'membership2' ),  
  144. ); 
  145.  
  146. $Description = apply_filters( 
  147. 'ms_model_pages_get_description',  
  148. $Description 
  149. ); 
  150.  
  151. if ( ! isset( $Description[$type] ) ) { 
  152. $Description[$type] = ''; 
  153.  
  154. return $Description[$type]; 
  155.  
  156.  
  157. /** 
  158. * Validate ms page type. 
  159. * 
  160. * @since 1.0.0 
  161. * 
  162. * @param string $type The page type to validate. 
  163. * @return boolean True if valid. 
  164. */ 
  165. static public function is_valid_type( $type ) { 
  166. static $Res = array(); 
  167.  
  168. if ( ! isset( $Res[$type] ) ) { 
  169. $Res[$type] = array_key_exists( $type, self::get_page_types() ); 
  170.  
  171. $Res[$type] = apply_filters( 
  172. 'ms_model_pages_is_valid_type',  
  173. $Res[$type] 
  174. ); 
  175.  
  176. return $Res[$type]; 
  177.  
  178. /** 
  179. * Get MS Pages. 
  180. * 
  181. * @since 1.0.0 
  182. * 
  183. * @return WP_Post[] The page model objects. 
  184. */ 
  185. static public function get_pages() { 
  186. static $Pages = null; 
  187.  
  188. if ( null === $Pages ) { 
  189. $Pages = array(); 
  190. $page_types = self::get_page_types(); 
  191.  
  192. $site_id = self::get_setting( 'site_id' ); 
  193. MS_Factory::select_blog( $site_id ); 
  194.  
  195. foreach ( $page_types as $page_type => $title ) { 
  196. $page_id = self::get_setting( $page_type ); 
  197. if ( empty( $page_id ) ) { continue; } 
  198.  
  199. $the_page = get_post( $page_id ); 
  200. if ( empty ( $the_page ) ) { continue; } 
  201.  
  202. $Pages[$page_type] = apply_filters( 
  203. 'ms_model_pages_get_pages_item',  
  204. $the_page,  
  205. $page_type,  
  206. $page_id 
  207. ); 
  208.  
  209. MS_Factory::revert_blog(); 
  210.  
  211. $Pages = apply_filters( 
  212. 'ms_model_pages_get_pages',  
  213. $Pages 
  214. ); 
  215.  
  216. return $Pages; 
  217.  
  218. /** 
  219. * Get specific MS Page. 
  220. * 
  221. * @since 1.0.0 
  222. * 
  223. * @param string $page_type The page type to retrieve the page. 
  224. * @return WP_Post The page model object. 
  225. */ 
  226. static public function get_page( $page_type ) { 
  227. $result = null; 
  228.  
  229. if ( self::is_valid_type( $page_type ) ) { 
  230. // Get a list of all WP_Post items. 
  231. $pages = self::get_pages(); 
  232.  
  233. if ( ! empty( $pages[ $page_type ] ) ) { 
  234. $result = $pages[ $page_type ]; 
  235. } else { 
  236. MS_Helper_Debug::log( 'ms_model_pages_get_page error: invalid page type: ' . $page_type ); 
  237.  
  238. return apply_filters( 
  239. 'ms_model_pages_get_page',  
  240. $result 
  241. ); 
  242.  
  243. /** 
  244. * Get specific MS Page using either ID or slug information. 
  245. * 
  246. * @since 1.0.0 
  247. * 
  248. * @param string $field The field to check. [id|slug] 
  249. * @param string $value The field value 
  250. * @return null|WP_Post The page object. 
  251. */ 
  252. static public function get_page_by( $field, $value ) { 
  253. static $Page_list = array(); 
  254.  
  255. if ( ! isset( $Page_list[$field] ) ) { 
  256. $Page_list[$field] = array(); 
  257.  
  258. if ( ! isset( $Page_list[$field][ $value ] ) ) { 
  259. $page_found = null; 
  260.  
  261. switch ( $field ) { 
  262. case 'id': $value = absint( $value ); break; 
  263.  
  264. $ms_pages = self::get_pages(); 
  265. $found = false; 
  266.  
  267. foreach ( $ms_pages as $type => $page ) { 
  268. switch ( $field ) { 
  269. case 'id': $found = ($value === absint( $page->ID ) ); break; 
  270. case 'slug': $found = ($value === $page->post_name ); break; 
  271.  
  272. if ( $found ) { 
  273. $page_found = $page; 
  274. break; 
  275.  
  276. $Page_list[$field][ $value ] = apply_filters( 
  277. 'ms_model_pages_get_page_by_id',  
  278. $page_found,  
  279. $field,  
  280. $value 
  281. ); 
  282.  
  283. return $Page_list[$field][ $value ]; 
  284.  
  285. /** 
  286. * Returns the page_id that is identified by the specified filter. 
  287. * Filter can be either a type-name, post-ID or WP_Post (in that order) 
  288. * 
  289. * @since 1.0.0 
  290. * @param mixed $filter The filter to translate into a post_id 
  291. * @return int 
  292. */ 
  293. static public function get_page_id( $filter ) { 
  294. $page_id = 0; 
  295.  
  296. if ( is_string( $filter ) ) { 
  297. $filter = self::get_page( $filter ); 
  298. } elseif ( is_numeric( $filter ) ) { 
  299. $page_id = $filter; 
  300.  
  301. if ( is_a( $filter, 'WP_Post' ) ) { 
  302. $page_id = $filter->ID; 
  303.  
  304. return apply_filters( 
  305. 'ms_model_pages_get_page_id',  
  306. $page_id,  
  307. $filter 
  308. ); 
  309.  
  310. /** 
  311. * Returns details about the site that contains the Membership2 pages on 
  312. * a network-wide protected network. 
  313. * 
  314. * It will always return current-site data if protection is site-wide. 
  315. * 
  316. * Possible keys: 
  317. * - all keys available in get_bloginfo() 
  318. * 
  319. * @since 1.0.0 
  320. * @param string $key The detail to return. See info for possible values. 
  321. * @return string The requested detail about the site. 
  322. */ 
  323. static public function get_site_info( $key ) { 
  324. static $Site_Info = null; 
  325.  
  326. // On first call get the site-ID and general site-details. 
  327. if ( null === $Site_Info ) { 
  328. $Site_Info = array(); 
  329.  
  330. if ( MS_Plugin::is_network_wide() ) { 
  331. $site_id = self::get_setting( 'site_id' ); 
  332. if ( ! $site_id || ! is_numeric( $site_id ) ) { 
  333. if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) { 
  334. $site_id = BLOG_ID_CURRENT_SITE; 
  335. } else { 
  336. $site_id = 1; 
  337. self::set_setting( 'site_id', $site_id ); 
  338. } else { 
  339. $site_id = get_current_blog_id(); 
  340.  
  341. $Site_Info['id'] = $site_id; 
  342.  
  343. // If a blog-specific setting was requested we lazy-load it now. 
  344. if ( ! isset( $Site_Info[$key] ) ) { 
  345. switch_to_blog( $Site_Info['id'] ); 
  346. $Site_Info[$key] = get_bloginfo( $key ); 
  347. restore_current_blog(); 
  348.  
  349. return $Site_Info[$key]; 
  350.  
  351. /** 
  352. * Checks if the current URL is a MS Page. 
  353. * If yes, then some basic information on this page are returned. 
  354. * 
  355. * @since 1.0.0 
  356. * @param int $page_id Optional. The page_id to fetch. 
  357. * @return WP_Post|null 
  358. */ 
  359. static public function current_page( $page_id = false, $page_type = null ) { 
  360. static $Res = array(); 
  361. $page_id = apply_filters( 
  362. 'ms_model_pages_current_page_id',  
  363. $page_id,  
  364. $page_type 
  365. ); 
  366.  
  367. $key = json_encode( $page_id ) . json_encode( $page_type ); 
  368.  
  369. if ( ! isset( $Res[$key] ) ) { 
  370. $this_page = null; 
  371. $site_id = self::get_site_info( 'id' ); 
  372.  
  373. if ( $site_id == get_current_blog_id() ) { 
  374. if ( ! empty( $page_type ) ) { 
  375. /** 
  376. * We have a page_type: 
  377. * Get infos of that page! 
  378. */ 
  379. $expected_page = self::get_page( $page_type ); 
  380.  
  381. if ( $page_id == $expected_page->ID ) { 
  382. $this_page = $expected_page; 
  383. } else { 
  384. /** 
  385. * We don't have the page_type: 
  386. * Use current page_id or the specified page_id/slug! 
  387. */ 
  388. if ( empty( $page_id ) ) { $page_id = get_the_ID(); } 
  389. if ( empty( $page_id ) ) { $page_id = get_queried_object_id(); } 
  390. if ( empty( $page_id ) && did_action( 'setup_theme' ) ) { 
  391. $url = lib3()->net->current_url(); 
  392. $page_id = url_to_postid( $url ); 
  393.  
  394. if ( ! empty( $page_id ) ) { 
  395. if ( is_numeric( $page_id ) ) { 
  396. $this_page = self::get_page_by( 'id', $page_id ); 
  397. } else { 
  398. $this_page = self::get_page_by( 'slug', $page_id ); 
  399.  
  400. $Res[$key] = apply_filters( 
  401. 'ms_model_pages_current_page',  
  402. $this_page 
  403. ); 
  404. return $Res[$key]; 
  405.  
  406. /** 
  407. * Verify if is a MS Page. 
  408. * 
  409. * Verify if current page, or passed page_id is a plugin special page. 
  410. * 
  411. * @since 1.0.0 
  412. * 
  413. * @param int $page_id Optional. The page id to verify. Default to current page. 
  414. * @param string $page_type Optional. The page type to verify. If null, test it against all ms pages. 
  415. */ 
  416. static public function is_membership_page( $page_id = null, $page_type = null ) { 
  417. $ms_page_type = false; 
  418.  
  419. $page_id = apply_filters( 
  420. 'ms_model_pages_membership_page_id',  
  421. $page_id,  
  422. $page_type 
  423. ); 
  424.  
  425. $page = self::current_page( $page_id ); 
  426. $site_id = self::get_site_info( 'id' ); 
  427.  
  428. if ( $site_id == get_current_blog_id() ) { 
  429. if ( empty( $page_type ) ) { 
  430. if ( $page ) { 
  431. $ms_page_type = self::get_page_type( $page->ID ); 
  432. } else { 
  433. if ( empty( $page_id ) && is_page() ) { 
  434. $page_id = get_the_ID(); 
  435.  
  436. if ( ! empty( $page_id ) ) { 
  437. $ms_page = self::get_page( $page_type ); 
  438. if ( $page_id == $ms_page->id ) { 
  439. $ms_page_type = $page_type; 
  440.  
  441. return apply_filters( 
  442. 'ms_model_pages_is_membership_page',  
  443. $ms_page_type 
  444. ); 
  445.  
  446. /** 
  447. * Get MS Page URL. 
  448. * 
  449. * @since 1.0.0 
  450. * 
  451. * @param string|WP_Post $page_type The page type name or a WP_Post object. 
  452. * @param boolean $ssl If wanted a SSL url. Set to null to use auto detection. 
  453. * @return string The MS Page URL. 
  454. */ 
  455. static public function get_page_url( $page_type, $ssl = null ) { 
  456. static $Urls = array(); 
  457.  
  458. $site_id = self::get_site_info( 'id' ); 
  459. $page_id = self::get_page_id( $page_type ); 
  460.  
  461. if ( ! isset( $Urls[$page_id] ) ) { 
  462. if ( is_multisite() ) { 
  463. $url = get_blog_permalink( $site_id, $page_id ); 
  464. } else { 
  465. $url = get_permalink( $page_id ); 
  466.  
  467. if ( null === $ssl ) { $ssl = is_ssl(); } 
  468. if ( $ssl ) { 
  469. $url = MS_Helper_Utility::get_ssl_url( $url ); 
  470.  
  471. $Urls[$page_id] = apply_filters( 
  472. 'ms_model_pages_get_ms_page_url',  
  473. $url,  
  474. $page_type,  
  475. $ssl,  
  476. $site_id 
  477. ); 
  478.  
  479. return $Urls[$page_id]; 
  480.  
  481. /** 
  482. * Redirect the user the specified membership page. 
  483. * 
  484. * @since 1.0.0 
  485. * @param string $page_type The page-type. 
  486. * @param array $args Optional. Additional URL parameters. 
  487. */ 
  488. static public function redirect_to( $page_type, $args = array() ) { 
  489. self::create_missing_pages(); 
  490. $url = self::get_page_url( $page_type ); 
  491.  
  492. $url = esc_url_raw( add_query_arg( $args, $url ) ); 
  493.  
  494. /** 
  495. * Opportunity for other plugins to redirect to a different page. 
  496. */ 
  497. $url = apply_filters( 
  498. 'ms_model_pages_redirect_to',  
  499. $url,  
  500. $page_type,  
  501. $args 
  502. ); 
  503.  
  504. wp_safe_redirect( $url ); 
  505. exit; 
  506.  
  507. /** 
  508. * Returns the URL to display after successful login. 
  509. * 
  510. * @since 1.0.0 
  511. * 
  512. * @param bool $filter Optional. If set to false then the URL is not 
  513. * filtered and the default value is returned. 
  514. * @return string URL of the page to display after login. 
  515. */ 
  516. static public function get_url_after_login( $filter = true ) { 
  517. if ( isset( $_REQUEST['redirect_to'] ) ) { 
  518. $url = $_REQUEST['redirect_to']; 
  519. $enforce = true; // This redirection was enforced via REUQEST param. 
  520. } else { 
  521. $url = self::get_page_url( self::MS_PAGE_ACCOUNT ); 
  522. $enforce = false; // This is the default redirection. 
  523.  
  524. if ( $filter ) { 
  525. $url = apply_filters( 
  526. 'ms_url_after_login',  
  527. $url,  
  528. $enforce 
  529. ); 
  530.  
  531. return $url; 
  532.  
  533. /** 
  534. * Returns the URL to display after successful logout. 
  535. * 
  536. * @since 1.0.0 
  537. * 
  538. * @param bool $filter Optional. If set to false then the URL is not 
  539. * filtered and the default value is returned. 
  540. * @return string URL of the page to display after logout. 
  541. */ 
  542. static public function get_url_after_logout( $filter = true ) { 
  543. if ( isset( $_REQUEST['redirect_to'] ) ) { 
  544. $url = $_REQUEST['redirect_to']; 
  545. $enforce = true; // This redirection was enforced via REUQEST param. 
  546. } else { 
  547. $url = MS_Helper_Utility::home_url( '/' ); 
  548. $enforce = false; // This is the default redirection. 
  549.  
  550. if ( $filter ) { 
  551. $url = apply_filters( 
  552. 'ms_url_after_logout',  
  553. $url,  
  554. $enforce 
  555. ); 
  556.  
  557. return $url; 
  558.  
  559. /** 
  560. * Get MS Page type by ID. 
  561. * 
  562. * @since 1.0.0 
  563. * 
  564. * @param string|WP_Post $page_type The page type name or a WP_Post object. 
  565. * @return string The MS Page type name. 
  566. */ 
  567. static public function get_page_type( $page_id ) { 
  568. static $Types = array(); 
  569.  
  570. $page_id = self::get_page_id( $page_id ); 
  571. $pages = self::get_pages(); 
  572.  
  573. if ( ! isset( $Types[$page_id] ) ) { 
  574. $type = ''; 
  575. foreach ( $pages as $page_type => $page ) { 
  576. if ( $page->ID === $page_id ) { 
  577. $type = $page_type; 
  578. break; 
  579.  
  580. $Types[$page_id] = apply_filters( 
  581. 'ms_model_pages_get_ms_page_type',  
  582. $type,  
  583. $page_id 
  584. ); 
  585.  
  586. return $Types[$page_id]; 
  587.  
  588. /** 
  589. * Creates any missing Membership pages. 
  590. * 
  591. * @since 1.0.0 
  592. * @return array|false Titles of the created pages 
  593. */ 
  594. static public function create_missing_pages() { 
  595. static $Done = false; 
  596. $res = false; 
  597.  
  598. if ( $Done ) { return $res; } 
  599. $Done = true; 
  600.  
  601. $user_id = get_current_user_id(); 
  602. if ( empty( $user_id ) ) { return $res; } 
  603.  
  604. $types = self::get_page_types(); 
  605.  
  606. $res = array(); 
  607.  
  608. $site_id = self::get_setting( 'site_id' ); 
  609. MS_Factory::select_blog( $site_id ); 
  610.  
  611. foreach ( $types as $type => $title ) { 
  612. $page_id = self::get_setting( $type ); 
  613. $status = get_post_status( $page_id ); 
  614.  
  615. if ( ! $status || 'trash' == $status ) { 
  616. // Page does not exist or was deleted. Create new page. 
  617. $page_id = 0; 
  618. } elseif ( 'publish' != $status ) { 
  619. // The page exists but is not published. Publish now. 
  620. wp_publish_post( $page_id ); 
  621.  
  622. // If the post_id does not exist then create a new page 
  623. if ( empty( $page_id ) ) { 
  624. $data = array( 
  625. 'post_title' => $title,  
  626. 'post_name' => $type,  
  627. 'post_content' => self::get_default_content( $type ),  
  628. 'post_type' => 'page',  
  629. 'post_status' => 'publish',  
  630. 'post_author' => $user_id,  
  631. ); 
  632. $new_id = wp_insert_post( $data ); 
  633.  
  634. /** 
  635. * Filter the new page_id 
  636. * 
  637. * @since 1.0.0 
  638. */ 
  639. $new_id = apply_filters( 
  640. 'ms_model_pages_create_missing_page',  
  641. $new_id,  
  642. $type,  
  643. $data 
  644. ); 
  645.  
  646. if ( is_numeric( $new_id ) ) { 
  647. self::set_setting( $type, $new_id ); 
  648. $res[$new_id] = $title; 
  649.  
  650. /** 
  651. * Trigger action to allow modifications to the page 
  652. * 
  653. * @since 1.0.0 
  654. */ 
  655. do_action( 
  656. 'ms_model_pages_create_wp_page',  
  657. $new_id 
  658. ); 
  659. MS_Factory::revert_blog(); 
  660.  
  661. return apply_filters( 
  662. 'ms_model_pages_create_missing_page',  
  663. $res 
  664. ); 
  665.  
  666. /** 
  667. * Returns true only then, when the current user can edit menu items. 
  668. * 
  669. * Reasons why it might be denied: 
  670. * - There are no menus where items can be added to. 
  671. * - The user is no admin. 
  672. * 
  673. * @since 1.0.0 
  674. * @return bool 
  675. */ 
  676. static public function can_edit_menus() { 
  677. $Can_Edit_Menus = null; 
  678.  
  679. if ( null === $Can_Edit_Menus ) { 
  680. $Can_Edit_Menus = false; 
  681.  
  682. if ( ! MS_Plugin::is_network_wide() ) { 
  683. $menus = wp_get_nav_menus(); 
  684.  
  685. if ( MS_Model_Member::is_admin_user() && ! empty( $menus ) ) { 
  686. $Can_Edit_Menus = true; 
  687.  
  688. $Can_Edit_Menus = apply_filters( 
  689. 'ms_model_pages_can_edit_menus',  
  690. $Can_Edit_Menus 
  691. ); 
  692.  
  693. return $Can_Edit_Menus; 
  694.  
  695. /** 
  696. * Create MS Pages in Menus. 
  697. * 
  698. * @since 1.0.0 
  699. * 
  700. * @param string $page_type The page type to create menu. 
  701. * @param string $update_only Only used by the upgrade class. 
  702. * @param string $type Only used by the upgrade class. 
  703. * @return bool True means that at least one menu item was created. 
  704. */ 
  705. static public function create_menu( $page_type, $update_only = null, $update_type = null ) { 
  706. $res = false; 
  707.  
  708. if ( MS_Plugin::is_network_wide() ) { 
  709. return $res; 
  710.  
  711. if ( self::is_valid_type( $page_type ) ) { 
  712. if ( $update_only && empty( $update_type ) ) { 
  713. self::create_menu( $page_type, true, 'page' ); 
  714. self::create_menu( $page_type, true, 'ms_page' ); 
  715. } else { 
  716. $ms_page = self::get_page( $page_type, true ); 
  717. $navs = wp_get_nav_menus( array( 'orderby' => 'name' ) ); 
  718.  
  719. if ( ! empty( $navs ) ) { 
  720. $object_type = empty( $update_type ) ? 'page' : $update_type; 
  721. $page_url = self::get_page_url( $ms_page ); 
  722.  
  723. foreach ( $navs as $nav ) { 
  724. $args['meta_query'] = array( 
  725. array( 
  726. 'key' => '_menu_item_object_id',  
  727. 'value' => $ms_page->ID,  
  728. ),  
  729. array( 
  730. 'key' => '_menu_item_object',  
  731. 'value' => $object_type,  
  732. ),  
  733. array( 
  734. 'key' => '_menu_item_type',  
  735. 'value' => 'post_type',  
  736. ),  
  737. ); 
  738.  
  739. // Search for existing menu item and create it if not found 
  740. $items = wp_get_nav_menu_items( $nav, $args ); 
  741.  
  742. $menu_item = apply_filters( 
  743. 'ms_model_settings_create_menu_item',  
  744. array( 
  745. 'menu-item-object-id' => $ms_page->ID,  
  746. 'menu-item-object' => 'page',  
  747. 'menu-item-parent-id' => 0,  
  748. 'menu-item-position' => 0,  
  749. 'menu-item-type' => 'post_type',  
  750. 'menu-item-title' => $ms_page->post_title,  
  751. 'menu-item-url' => $page_url,  
  752. 'menu-item-status' => 'publish',  
  753. ); 
  754.  
  755. $item = ! is_array( $items ) ? false : array_shift( $items ); 
  756. $db_id = empty( $item ) ? 0 : $item->db_id; 
  757.  
  758. if ( $db_id || ! $update_only ) { 
  759. wp_update_nav_menu_item( $nav->term_id, $db_id, $menu_item ); 
  760. self::set_setting( 'has_nav_' . $page_type, true ); 
  761. $res = true; 
  762. } else { 
  763. // No menus defined. 
  764. $res = true; 
  765.  
  766. return $res; 
  767.  
  768. /** 
  769. * Remove MS Pages from Menus. 
  770. * 
  771. * @since 1.0.0 
  772. * 
  773. * @param string $page_type The page type to create menu. 
  774. * @return bool True means that at least one menu item was deleted. 
  775. */ 
  776. static public function drop_menu( $page_type ) { 
  777. $res = true; 
  778.  
  779. if ( MS_Plugin::is_network_wide() ) { 
  780. return $res; 
  781.  
  782. if ( self::is_valid_type( $page_type ) ) { 
  783. $ms_page = self::get_page( $page_type, true ); 
  784. $navs = wp_get_nav_menus( array( 'orderby' => 'name' ) ); 
  785.  
  786. if ( ! empty( $navs ) ) { 
  787. foreach ( $navs as $nav ) { 
  788. $args['meta_query'] = array( 
  789. array( 
  790. 'key' => '_menu_item_object_id',  
  791. 'value' => $ms_page->ID,  
  792. ),  
  793. array( 
  794. 'key' => '_menu_item_object',  
  795. 'value' => 'page',  
  796. ),  
  797. array( 
  798. 'key' => '_menu_item_type',  
  799. 'value' => 'post_type',  
  800. ),  
  801. ); 
  802.  
  803. // Search for existing menu item and create it if not found 
  804. $items = wp_get_nav_menu_items( $nav, $args ); 
  805.  
  806. $item = ! is_array( $items ) ? false : array_shift( $items ); 
  807. $db_id = empty( $item ) ? 0 : $item->db_id; 
  808.  
  809. if ( $db_id ) { 
  810. if ( false !== wp_delete_post( $db_id ) ) { 
  811. self::set_setting( 'has_nav_' . $page_type, false ); 
  812. } else { 
  813. $res = false; 
  814. } else { 
  815. $res = false; 
  816.  
  817. return $res; 
  818.  
  819. /** 
  820. * Returns the current menu state: If a specific page is added to the menu,  
  821. * this state is saved in the settings. So when the user removes a menu item 
  822. * manually we still have the "inserted" flag in DB. 
  823. * 
  824. * We do this, because the menu items are added to all existing nav menus 
  825. * and the user might remove them from one nav menu but not from all... 
  826. * 
  827. * @since 1.0.0 
  828. * @param string $page_type 
  829. * @return bool 
  830. */ 
  831. static public function has_menu( $page_type ) { 
  832. $state = false; 
  833.  
  834. if ( ! MS_Plugin::is_network_wide() ) { 
  835. if ( self::is_valid_type( $page_type ) ) { 
  836. $state = self::get_setting( 'has_nav_' . $page_type ); 
  837. $state = lib3()->is_true( $state ); 
  838.  
  839. return $state; 
  840.  
  841.  
  842. /** 
  843. * Get default content for membership pages. 
  844. * 
  845. * @since 1.0.0 
  846. * 
  847. * @param string $type The page type name. 
  848. * @return string The default content. 
  849. */ 
  850. static public function get_default_content( $type ) { 
  851. $lines = array(); 
  852.  
  853. switch ( $type ) { 
  854. case self::MS_PAGE_MEMBERSHIPS: 
  855. $lines[] = sprintf( 
  856. '['. MS_Helper_Shortcode::SCODE_NOTE .' type="info"]%1$s[/'. MS_Helper_Shortcode::SCODE_NOTE .']',  
  857. __( 'We have the following subscriptions available for our site. You can renew, cancel or upgrade your subscriptions by using the forms below.', 'membership2' ) 
  858. ); 
  859. $lines[] = '['. MS_Helper_Shortcode::SCODE_SIGNUP .']'; 
  860. break; 
  861.  
  862. case self::MS_PAGE_PROTECTED_CONTENT: 
  863. $lines[] = '[' . MS_Helper_Shortcode::SCODE_PROTECTED . ']'; 
  864. break; 
  865.  
  866. case self::MS_PAGE_ACCOUNT: 
  867. $lines[] = '['. MS_Helper_Shortcode::SCODE_MS_ACCOUNT .']<hr />'; 
  868. $lines[] = '['. MS_Helper_Shortcode::SCODE_LOGOUT .']'; 
  869. break; 
  870.  
  871. case self::MS_PAGE_REGISTER: 
  872. $lines[] = sprintf( 
  873. '['. MS_Helper_Shortcode::SCODE_NOTE .' type="info"]%1$s[/'. MS_Helper_Shortcode::SCODE_NOTE .']',  
  874. __( 'We have the following subscriptions available for our site. To join, simply click on the Sign Up button and then complete the registration details.', 'membership2' ) 
  875. ); 
  876. $lines[] = '['. MS_Helper_Shortcode::SCODE_SIGNUP .']'; 
  877. break; 
  878.  
  879. case self::MS_PAGE_REG_COMPLETE: 
  880. $lines[] = sprintf( 
  881. '['. MS_Helper_Shortcode::SCODE_NOTE .' type="info"]%1$s<br/>%2$s[/'. MS_Helper_Shortcode::SCODE_NOTE .']',  
  882. __( 'Your request to join the membership was successfully received!', 'membership2' ),  
  883. __( 'The Payment Gateway could take a couple of minutes to process and return the payment status.', 'membership2' ) 
  884. ); 
  885. $lines[] = '['. MS_Helper_Shortcode::SCODE_MS_ACCOUNT_LINK .']'; 
  886. break; 
  887.  
  888. $content = implode( "\n", $lines ); 
  889.  
  890. return apply_filters( 
  891. 'ms_model_pages_get_default_content',  
  892. $content 
  893. ); 
  894.  
  895. /** 
  896. * Creates a new WordPress menu and adds all top level pages to this menu. 
  897. * 
  898. * @since 1.0.0 
  899. */ 
  900. static public function create_default_menu() { 
  901. if ( MS_Plugin::is_network_wide() ) { 
  902. return; 
  903.  
  904. $menu_id = wp_create_nav_menu( __( 'Default Menu', 'membership2' ) ); 
  905.  
  906. if ( ! is_numeric( $menu_id ) || $menu_id <= 0 ) { 
  907. return; 
  908.  
  909. // Use the new menu in the menu-location of the theme. 
  910. $locations = get_theme_mod( 'nav_menu_locations' ); 
  911. if ( is_array( $locations ) && count( $locations ) > 0 ) { 
  912. reset( $locations ); 
  913. $first = key( $locations ); 
  914. $locations[$first] = $menu_id; 
  915. set_theme_mod( 'nav_menu_locations', $locations ); 
  916.  
  917. // Enable the Auto-Add-New-Pages option. 
  918. // Code snippet from wp-admin/includes/nav-menu.php 
  919. $nav_menu_option = (array) get_option( 'nav_menu_options' ); 
  920. if ( ! isset( $nav_menu_option['auto_add'] ) ) { 
  921. $nav_menu_option['auto_add'] = array(); 
  922. if ( ! in_array( $menu_id, $nav_menu_option['auto_add'] ) ) { 
  923. $nav_menu_option['auto_add'][] = $menu_id; 
  924. update_option( 'nav_menu_options', $nav_menu_option ); 
  925.  
  926. // Get a list of all published top-level pages. 
  927. $top_pages = get_pages( 
  928. array( 'parent' => 0 ) 
  929. ); 
  930.  
  931. // List of pages that should not be displayed in the menu. 
  932. $skip_pages = array( 
  933. self::MS_PAGE_PROTECTED_CONTENT,  
  934. self::MS_PAGE_REG_COMPLETE,  
  935. ); 
  936.  
  937. foreach ( $top_pages as $page ) { 
  938. // Skip pages that should not appear in menu. 
  939. $ms_type = self::is_membership_page( $page->ID ); 
  940. if ( in_array( $ms_type, $skip_pages ) ) { 
  941. continue; 
  942.  
  943. // Add the page to our new menu! 
  944. $item = array( 
  945. 'menu-item-object-id' => $page->ID,  
  946. 'menu-item-object' => $page->post_type,  
  947. 'menu-item-type' => 'post_type',  
  948. 'menu-item-status' => $page->post_status,  
  949. ); 
  950. wp_update_nav_menu_item( $menu_id, 0, $item ); 
  951.  
.