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