/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. $site_id = get_current_blog_id(); 
  331.  
  332. $Site_Info['id'] = $site_id; 
  333.  
  334. // If a blog-specific setting was requested we lazy-load it now. 
  335. if ( ! isset( $Site_Info[$key] ) ) { 
  336. switch_to_blog( $Site_Info['id'] ); 
  337. $Site_Info[$key] = get_bloginfo( $key ); 
  338. restore_current_blog(); 
  339.  
  340. return $Site_Info[$key]; 
  341.  
  342. /** 
  343. * Checks if the current URL is a MS Page. 
  344. * If yes, then some basic information on this page are returned. 
  345. * 
  346. * @since 1.0.0 
  347. * @param int $page_id Optional. The page_id to fetch. 
  348. * @return WP_Post|null 
  349. */ 
  350. static public function current_page( $page_id = false, $page_type = null ) { 
  351. static $Res = array(); 
  352. $page_id = apply_filters( 
  353. 'ms_model_pages_current_page_id',  
  354. $page_id,  
  355. $page_type 
  356. ); 
  357.  
  358. $key = json_encode( $page_id ) . json_encode( $page_type ); 
  359.  
  360. if ( ! isset( $Res[$key] ) ) { 
  361. $this_page = null; 
  362. $site_id = self::get_site_info( 'id' ); 
  363.  
  364. if ( $site_id == get_current_blog_id() ) { 
  365. if ( ! empty( $page_type ) ) { 
  366. /** 
  367. * We have a page_type: 
  368. * Get infos of that page! 
  369. */ 
  370. $expected_page = self::get_page( $page_type ); 
  371.  
  372. if ( $page_id == $expected_page->ID ) { 
  373. $this_page = $expected_page; 
  374. } else { 
  375. /** 
  376. * We don't have the page_type: 
  377. * Use current page_id or the specified page_id/slug! 
  378. */ 
  379. if ( empty( $page_id ) ) { $page_id = get_the_ID(); } 
  380. if ( empty( $page_id ) ) { $page_id = get_queried_object_id(); } 
  381. if ( empty( $page_id ) && did_action( 'setup_theme' ) ) { 
  382. $url = lib3()->net->current_url(); 
  383. $page_id = url_to_postid( $url ); 
  384.  
  385. if ( ! empty( $page_id ) ) { 
  386. if ( is_numeric( $page_id ) ) { 
  387. $this_page = self::get_page_by( 'id', $page_id ); 
  388. } else { 
  389. $this_page = self::get_page_by( 'slug', $page_id ); 
  390.  
  391. $Res[$key] = apply_filters( 
  392. 'ms_model_pages_current_page',  
  393. $this_page 
  394. ); 
  395. return $Res[$key]; 
  396.  
  397. /** 
  398. * Verify if is a MS Page. 
  399. * 
  400. * Verify if current page, or passed page_id is a plugin special page. 
  401. * 
  402. * @since 1.0.0 
  403. * 
  404. * @param int $page_id Optional. The page id to verify. Default to current page. 
  405. * @param string $page_type Optional. The page type to verify. If null, test it against all ms pages. 
  406. */ 
  407. static public function is_membership_page( $page_id = null, $page_type = null ) { 
  408. $ms_page_type = false; 
  409.  
  410. $page_id = apply_filters( 
  411. 'ms_model_pages_membership_page_id',  
  412. $page_id,  
  413. $page_type 
  414. ); 
  415.  
  416. $page = self::current_page( $page_id ); 
  417. $site_id = self::get_site_info( 'id' ); 
  418.  
  419. if ( $site_id == get_current_blog_id() ) { 
  420. if ( empty( $page_type ) ) { 
  421. if ( $page ) { 
  422. $ms_page_type = self::get_page_type( $page->ID ); 
  423. } else { 
  424. if ( empty( $page_id ) && is_page() ) { 
  425. $page_id = get_the_ID(); 
  426.  
  427. if ( ! empty( $page_id ) ) { 
  428. $ms_page = self::get_page( $page_type ); 
  429. if ( $page_id == $ms_page->id ) { 
  430. $ms_page_type = $page_type; 
  431.  
  432. return apply_filters( 
  433. 'ms_model_pages_is_membership_page',  
  434. $ms_page_type 
  435. ); 
  436.  
  437. /** 
  438. * Get MS Page URL. 
  439. * 
  440. * @since 1.0.0 
  441. * 
  442. * @param string|WP_Post $page_type The page type name or a WP_Post object. 
  443. * @param boolean $ssl If wanted a SSL url. Set to null to use auto detection. 
  444. * @return string The MS Page URL. 
  445. */ 
  446. static public function get_page_url( $page_type, $ssl = null ) { 
  447. static $Urls = array(); 
  448.  
  449. $site_id = self::get_site_info( 'id' ); 
  450. $page_id = self::get_page_id( $page_type ); 
  451.  
  452. if ( ! isset( $Urls[$page_id] ) ) { 
  453. if ( is_multisite() ) { 
  454. $url = get_blog_permalink( $site_id, $page_id ); 
  455. } else { 
  456. $url = get_permalink( $page_id ); 
  457.  
  458. if ( null === $ssl ) { $ssl = is_ssl(); } 
  459. if ( $ssl ) { 
  460. $url = MS_Helper_Utility::get_ssl_url( $url ); 
  461.  
  462. $Urls[$page_id] = apply_filters( 
  463. 'ms_model_pages_get_ms_page_url',  
  464. $url,  
  465. $page_type,  
  466. $ssl,  
  467. $site_id 
  468. ); 
  469.  
  470. return $Urls[$page_id]; 
  471.  
  472. /** 
  473. * Redirect the user the specified membership page. 
  474. * 
  475. * @since 1.0.0 
  476. * @param string $page_type The page-type. 
  477. * @param array $args Optional. Additional URL parameters. 
  478. */ 
  479. static public function redirect_to( $page_type, $args = array() ) { 
  480. self::create_missing_pages(); 
  481. $url = self::get_page_url( $page_type ); 
  482.  
  483. $url = esc_url_raw( add_query_arg( $args, $url ) ); 
  484.  
  485. /** 
  486. * Opportunity for other plugins to redirect to a different page. 
  487. */ 
  488. $url = apply_filters( 
  489. 'ms_model_pages_redirect_to',  
  490. $url,  
  491. $page_type,  
  492. $args 
  493. ); 
  494.  
  495. wp_safe_redirect( $url ); 
  496. exit; 
  497.  
  498. /** 
  499. * Returns the URL to display after successful login. 
  500. * 
  501. * @since 1.0.0 
  502. * 
  503. * @param bool $filter Optional. If set to false then the URL is not 
  504. * filtered and the default value is returned. 
  505. * @return string URL of the page to display after login. 
  506. */ 
  507. static public function get_url_after_login( $filter = true ) { 
  508. if ( isset( $_REQUEST['redirect_to'] ) ) { 
  509. $url = $_REQUEST['redirect_to']; 
  510. $enforce = true; // This redirection was enforced via REUQEST param. 
  511. } else { 
  512. $url = self::get_page_url( self::MS_PAGE_ACCOUNT ); 
  513. $enforce = false; // This is the default redirection. 
  514.  
  515. if ( $filter ) { 
  516. $url = apply_filters( 
  517. 'ms_url_after_login',  
  518. $url,  
  519. $enforce 
  520. ); 
  521.  
  522. return $url; 
  523.  
  524. /** 
  525. * Returns the URL to display after successful logout. 
  526. * 
  527. * @since 1.0.0 
  528. * 
  529. * @param bool $filter Optional. If set to false then the URL is not 
  530. * filtered and the default value is returned. 
  531. * @return string URL of the page to display after logout. 
  532. */ 
  533. static public function get_url_after_logout( $filter = true ) { 
  534. if ( isset( $_REQUEST['redirect_to'] ) ) { 
  535. $url = $_REQUEST['redirect_to']; 
  536. $enforce = true; // This redirection was enforced via REUQEST param. 
  537. } else { 
  538. $url = MS_Helper_Utility::home_url( '/' ); 
  539. $enforce = false; // This is the default redirection. 
  540.  
  541. if ( $filter ) { 
  542. $url = apply_filters( 
  543. 'ms_url_after_logout',  
  544. $url,  
  545. $enforce 
  546. ); 
  547.  
  548. return $url; 
  549.  
  550. /** 
  551. * Get MS Page type by ID. 
  552. * 
  553. * @since 1.0.0 
  554. * 
  555. * @param string|WP_Post $page_type The page type name or a WP_Post object. 
  556. * @return string The MS Page type name. 
  557. */ 
  558. static public function get_page_type( $page_id ) { 
  559. static $Types = array(); 
  560.  
  561. $page_id = self::get_page_id( $page_id ); 
  562. $pages = self::get_pages(); 
  563.  
  564. if ( ! isset( $Types[$page_id] ) ) { 
  565. $type = ''; 
  566. foreach ( $pages as $page_type => $page ) { 
  567. if ( $page->ID === $page_id ) { 
  568. $type = $page_type; 
  569. break; 
  570.  
  571. $Types[$page_id] = apply_filters( 
  572. 'ms_model_pages_get_ms_page_type',  
  573. $type,  
  574. $page_id 
  575. ); 
  576.  
  577. return $Types[$page_id]; 
  578.  
  579. /** 
  580. * Creates any missing Membership pages. 
  581. * 
  582. * @since 1.0.0 
  583. * @return array|false Titles of the created pages 
  584. */ 
  585. static public function create_missing_pages() { 
  586. static $Done = false; 
  587. $res = false; 
  588.  
  589. if ( $Done ) { return $res; } 
  590. $Done = true; 
  591.  
  592. $user_id = get_current_user_id(); 
  593. if ( empty( $user_id ) ) { return $res; } 
  594.  
  595. $types = self::get_page_types(); 
  596.  
  597. $res = array(); 
  598.  
  599. $site_id = self::get_setting( 'site_id' ); 
  600. MS_Factory::select_blog( $site_id ); 
  601.  
  602. foreach ( $types as $type => $title ) { 
  603. $page_id = self::get_setting( $type ); 
  604. $status = get_post_status( $page_id ); 
  605.  
  606. if ( ! $status || 'trash' == $status ) { 
  607. // Page does not exist or was deleted. Create new page. 
  608. $page_id = 0; 
  609. } elseif ( 'publish' != $status ) { 
  610. // The page exists but is not published. Publish now. 
  611. wp_publish_post( $page_id ); 
  612.  
  613. // If the post_id does not exist then create a new page 
  614. if ( empty( $page_id ) ) { 
  615. $data = array( 
  616. 'post_title' => $title,  
  617. 'post_name' => $type,  
  618. 'post_content' => self::get_default_content( $type ),  
  619. 'post_type' => 'page',  
  620. 'post_status' => 'publish',  
  621. 'post_author' => $user_id,  
  622. ); 
  623. $new_id = wp_insert_post( $data ); 
  624.  
  625. /** 
  626. * Filter the new page_id 
  627. * 
  628. * @since 1.0.0 
  629. */ 
  630. $new_id = apply_filters( 
  631. 'ms_model_pages_create_missing_page',  
  632. $new_id,  
  633. $type,  
  634. $data 
  635. ); 
  636.  
  637. if ( is_numeric( $new_id ) ) { 
  638. self::set_setting( $type, $new_id ); 
  639. $res[$new_id] = $title; 
  640.  
  641. /** 
  642. * Trigger action to allow modifications to the page 
  643. * 
  644. * @since 1.0.0 
  645. */ 
  646. do_action( 
  647. 'ms_model_pages_create_wp_page',  
  648. $new_id 
  649. ); 
  650. MS_Factory::revert_blog(); 
  651.  
  652. return apply_filters( 
  653. 'ms_model_pages_create_missing_page',  
  654. $res 
  655. ); 
  656.  
  657. /** 
  658. * Returns true only then, when the current user can edit menu items. 
  659. * 
  660. * Reasons why it might be denied: 
  661. * - There are no menus where items can be added to. 
  662. * - The user is no admin. 
  663. * 
  664. * @since 1.0.0 
  665. * @return bool 
  666. */ 
  667. static public function can_edit_menus() { 
  668. $Can_Edit_Menus = null; 
  669.  
  670. if ( null === $Can_Edit_Menus ) { 
  671. $Can_Edit_Menus = false; 
  672.  
  673. if ( ! MS_Plugin::is_network_wide() ) { 
  674. $menus = wp_get_nav_menus(); 
  675.  
  676. if ( MS_Model_Member::is_admin_user() && ! empty( $menus ) ) { 
  677. $Can_Edit_Menus = true; 
  678.  
  679. $Can_Edit_Menus = apply_filters( 
  680. 'ms_model_pages_can_edit_menus',  
  681. $Can_Edit_Menus 
  682. ); 
  683.  
  684. return $Can_Edit_Menus; 
  685.  
  686. /** 
  687. * Create MS Pages in Menus. 
  688. * 
  689. * @since 1.0.0 
  690. * 
  691. * @param string $page_type The page type to create menu. 
  692. * @param string $update_only Only used by the upgrade class. 
  693. * @param string $type Only used by the upgrade class. 
  694. * @return bool True means that at least one menu item was created. 
  695. */ 
  696. static public function create_menu( $page_type, $update_only = null, $update_type = null ) { 
  697. $res = false; 
  698.  
  699.  
  700. if ( self::is_valid_type( $page_type ) ) { 
  701. if ( $update_only && empty( $update_type ) ) { 
  702. self::create_menu( $page_type, true, 'page' ); 
  703. self::create_menu( $page_type, true, 'ms_page' ); 
  704. } else { 
  705. $ms_page = self::get_page( $page_type, true ); 
  706. $navs = wp_get_nav_menus( array( 'orderby' => 'name' ) ); 
  707.  
  708. if ( ! empty( $navs ) ) { 
  709. $object_type = empty( $update_type ) ? 'page' : $update_type; 
  710. $page_url = self::get_page_url( $ms_page ); 
  711.  
  712. foreach ( $navs as $nav ) { 
  713. $args['meta_query'] = array( 
  714. array( 
  715. 'key' => '_menu_item_object_id',  
  716. 'value' => $ms_page->ID,  
  717. ),  
  718. array( 
  719. 'key' => '_menu_item_object',  
  720. 'value' => $object_type,  
  721. ),  
  722. array( 
  723. 'key' => '_menu_item_type',  
  724. 'value' => 'post_type',  
  725. ),  
  726. ); 
  727.  
  728. // Search for existing menu item and create it if not found 
  729. $items = wp_get_nav_menu_items( $nav, $args ); 
  730.  
  731. $menu_item = apply_filters( 
  732. 'ms_model_settings_create_menu_item',  
  733. array( 
  734. 'menu-item-object-id' => $ms_page->ID,  
  735. 'menu-item-object' => 'page',  
  736. 'menu-item-parent-id' => 0,  
  737. 'menu-item-position' => 0,  
  738. 'menu-item-type' => 'post_type',  
  739. 'menu-item-title' => $ms_page->post_title,  
  740. 'menu-item-url' => $page_url,  
  741. 'menu-item-status' => 'publish',  
  742. ); 
  743.  
  744. $item = ! is_array( $items ) ? false : array_shift( $items ); 
  745. $db_id = empty( $item ) ? 0 : $item->db_id; 
  746.  
  747. if ( $db_id || ! $update_only ) { 
  748. wp_update_nav_menu_item( $nav->term_id, $db_id, $menu_item ); 
  749. self::set_setting( 'has_nav_' . $page_type, true ); 
  750. $res = true; 
  751. } else { 
  752. // No menus defined. 
  753. $res = true; 
  754.  
  755. return $res; 
  756.  
  757. /** 
  758. * Remove MS Pages from Menus. 
  759. * 
  760. * @since 1.0.0 
  761. * 
  762. * @param string $page_type The page type to create menu. 
  763. * @return bool True means that at least one menu item was deleted. 
  764. */ 
  765. static public function drop_menu( $page_type ) { 
  766. $res = true; 
  767.  
  768.  
  769. if ( self::is_valid_type( $page_type ) ) { 
  770. $ms_page = self::get_page( $page_type, true ); 
  771. $navs = wp_get_nav_menus( array( 'orderby' => 'name' ) ); 
  772.  
  773. if ( ! empty( $navs ) ) { 
  774. foreach ( $navs as $nav ) { 
  775. $args['meta_query'] = array( 
  776. array( 
  777. 'key' => '_menu_item_object_id',  
  778. 'value' => $ms_page->ID,  
  779. ),  
  780. array( 
  781. 'key' => '_menu_item_object',  
  782. 'value' => 'page',  
  783. ),  
  784. array( 
  785. 'key' => '_menu_item_type',  
  786. 'value' => 'post_type',  
  787. ),  
  788. ); 
  789.  
  790. // Search for existing menu item and create it if not found 
  791. $items = wp_get_nav_menu_items( $nav, $args ); 
  792.  
  793. $item = ! is_array( $items ) ? false : array_shift( $items ); 
  794. $db_id = empty( $item ) ? 0 : $item->db_id; 
  795.  
  796. if ( $db_id ) { 
  797. if ( false !== wp_delete_post( $db_id ) ) { 
  798. self::set_setting( 'has_nav_' . $page_type, false ); 
  799. } else { 
  800. $res = false; 
  801. } else { 
  802. $res = false; 
  803.  
  804. return $res; 
  805.  
  806. /** 
  807. * Returns the current menu state: If a specific page is added to the menu,  
  808. * this state is saved in the settings. So when the user removes a menu item 
  809. * manually we still have the "inserted" flag in DB. 
  810. * 
  811. * We do this, because the menu items are added to all existing nav menus 
  812. * and the user might remove them from one nav menu but not from all... 
  813. * 
  814. * @since 1.0.0 
  815. * @param string $page_type 
  816. * @return bool 
  817. */ 
  818. static public function has_menu( $page_type ) { 
  819. $state = false; 
  820.  
  821. if ( ! MS_Plugin::is_network_wide() ) { 
  822. if ( self::is_valid_type( $page_type ) ) { 
  823. $state = self::get_setting( 'has_nav_' . $page_type ); 
  824. $state = lib3()->is_true( $state ); 
  825.  
  826. return $state; 
  827.  
  828.  
  829. /** 
  830. * Get default content for membership pages. 
  831. * 
  832. * @since 1.0.0 
  833. * 
  834. * @param string $type The page type name. 
  835. * @return string The default content. 
  836. */ 
  837. static public function get_default_content( $type ) { 
  838. $lines = array(); 
  839.  
  840. switch ( $type ) { 
  841. case self::MS_PAGE_MEMBERSHIPS: 
  842. $lines[] = sprintf( 
  843. '['. MS_Helper_Shortcode::SCODE_NOTE .' type="info"]%1$s[/'. MS_Helper_Shortcode::SCODE_NOTE .']',  
  844. __( 'We have the following subscriptions available for our site. You can renew, cancel or upgrade your subscriptions by using the forms below.', 'membership2' ) 
  845. ); 
  846. $lines[] = '['. MS_Helper_Shortcode::SCODE_SIGNUP .']'; 
  847. break; 
  848.  
  849. case self::MS_PAGE_PROTECTED_CONTENT: 
  850. $lines[] = '[' . MS_Helper_Shortcode::SCODE_PROTECTED . ']'; 
  851. break; 
  852.  
  853. case self::MS_PAGE_ACCOUNT: 
  854. $lines[] = '['. MS_Helper_Shortcode::SCODE_MS_ACCOUNT .']<hr />'; 
  855. $lines[] = '['. MS_Helper_Shortcode::SCODE_LOGOUT .']'; 
  856. break; 
  857.  
  858. case self::MS_PAGE_REGISTER: 
  859. $lines[] = sprintf( 
  860. '['. MS_Helper_Shortcode::SCODE_NOTE .' type="info"]%1$s[/'. MS_Helper_Shortcode::SCODE_NOTE .']',  
  861. __( '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' ) 
  862. ); 
  863. $lines[] = '['. MS_Helper_Shortcode::SCODE_SIGNUP .']'; 
  864. break; 
  865.  
  866. case self::MS_PAGE_REG_COMPLETE: 
  867. $lines[] = sprintf( 
  868. '['. MS_Helper_Shortcode::SCODE_NOTE .' type="info"]%1$s<br/>%2$s[/'. MS_Helper_Shortcode::SCODE_NOTE .']',  
  869. __( 'Your request to join the membership was successfully received!', 'membership2' ),  
  870. __( 'The Payment Gateway could take a couple of minutes to process and return the payment status.', 'membership2' ) 
  871. ); 
  872. $lines[] = '['. MS_Helper_Shortcode::SCODE_MS_ACCOUNT_LINK .']'; 
  873. break; 
  874.  
  875. $content = implode( "\n", $lines ); 
  876.  
  877. return apply_filters( 
  878. 'ms_model_pages_get_default_content',  
  879. $content 
  880. ); 
  881.  
  882. /** 
  883. * Creates a new WordPress menu and adds all top level pages to this menu. 
  884. * 
  885. * @since 1.0.0 
  886. */ 
  887. static public function create_default_menu() { 
  888.  
  889. $menu_id = wp_create_nav_menu( __( 'Default Menu', 'membership2' ) ); 
  890.  
  891. if ( ! is_numeric( $menu_id ) || $menu_id <= 0 ) { 
  892. return; 
  893.  
  894. // Use the new menu in the menu-location of the theme. 
  895. $locations = get_theme_mod( 'nav_menu_locations' ); 
  896. if ( is_array( $locations ) && count( $locations ) > 0 ) { 
  897. reset( $locations ); 
  898. $first = key( $locations ); 
  899. $locations[$first] = $menu_id; 
  900. set_theme_mod( 'nav_menu_locations', $locations ); 
  901.  
  902. // Enable the Auto-Add-New-Pages option. 
  903. // Code snippet from wp-admin/includes/nav-menu.php 
  904. $nav_menu_option = (array) get_option( 'nav_menu_options' ); 
  905. if ( ! isset( $nav_menu_option['auto_add'] ) ) { 
  906. $nav_menu_option['auto_add'] = array(); 
  907. if ( ! in_array( $menu_id, $nav_menu_option['auto_add'] ) ) { 
  908. $nav_menu_option['auto_add'][] = $menu_id; 
  909. update_option( 'nav_menu_options', $nav_menu_option ); 
  910.  
  911. // Get a list of all published top-level pages. 
  912. $top_pages = get_pages( 
  913. array( 'parent' => 0 ) 
  914. ); 
  915.  
  916. // List of pages that should not be displayed in the menu. 
  917. $skip_pages = array( 
  918. self::MS_PAGE_PROTECTED_CONTENT,  
  919. self::MS_PAGE_REG_COMPLETE,  
  920. ); 
  921.  
  922. foreach ( $top_pages as $page ) { 
  923. // Skip pages that should not appear in menu. 
  924. $ms_type = self::is_membership_page( $page->ID ); 
  925. if ( in_array( $ms_type, $skip_pages ) ) { 
  926. continue; 
  927.  
  928. // Add the page to our new menu! 
  929. $item = array( 
  930. 'menu-item-object-id' => $page->ID,  
  931. 'menu-item-object' => $page->post_type,  
  932. 'menu-item-type' => 'post_type',  
  933. 'menu-item-status' => $page->post_status,  
  934. ); 
  935. wp_update_nav_menu_item( $menu_id, 0, $item ); 
  936.  
.