MS_Model_Pages

Plugin Pages model.

Defined (1)

The class is defined in the following location(s).

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