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