MS_Controller_Pages

Controller for managing Membership Pages.

Defined (1)

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

/app/controller/class-ms-controller-pages.php  
  1. class MS_Controller_Pages extends MS_Controller { 
  2.  
  3. /** 
  4. * AJAX action constants. 
  5. * @since 1.0.0 
  6. * @var string 
  7. */ 
  8. const AJAX_ACTION_UPDATE_PAGES = 'pages_update'; 
  9. const AJAX_ACTION_TOGGLE_MENU = 'pages_toggle_menu'; 
  10. const AJAX_ACTION_CREATE_MENU = 'pages_create_menu'; 
  11.  
  12. /** 
  13. * Construct Settings manager. 
  14. * @since 1.0.0 
  15. */ 
  16. public function __construct() { 
  17. parent::__construct(); 
  18.  
  19. $this->add_ajax_action( self::AJAX_ACTION_UPDATE_PAGES, 'ajax_action_update_pages' ); 
  20. $this->add_ajax_action( self::AJAX_ACTION_TOGGLE_MENU, 'ajax_action_toggle_menu' ); 
  21. $this->add_ajax_action( self::AJAX_ACTION_CREATE_MENU, 'ajax_action_create_menu' ); 
  22.  
  23. /** 
  24. * Initialize the admin-side functions. 
  25. * @since 1.0.0 
  26. */ 
  27. public function admin_init() { 
  28. // Add a special "state" to membership pages in the WP Page-list 
  29. $this->add_filter( 'display_post_states', 'post_states', 10, 2 ); 
  30.  
  31. /** 
  32. * Get MS_Model_Pages model 
  33. * @since 1.0.0 
  34. * @return MS_Model_Settings 
  35. */ 
  36. public function get_model() { 
  37. return MS_Factory::load( 'MS_Model_Pages' ); 
  38.  
  39. /** 
  40. * Handle Ajax update pages setting. 
  41. * Related action hooks: 
  42. * - wp_ajax_update_pages 
  43. * @since 1.0.0 
  44. */ 
  45. public function ajax_action_update_pages() { 
  46. $msg = MS_Helper_Settings::SETTINGS_MSG_NOT_UPDATED; 
  47.  
  48. $isset = array( 'field', 'value' ); 
  49. if ( $this->verify_nonce() 
  50. && self::validate_required( $isset, 'POST', false ) 
  51. && $this->is_admin_user() 
  52. ) { 
  53. $ms_pages = $this->get_model(); 
  54. $ms_pages->set_setting( $_POST['field'], $_POST['value'] ); 
  55. $msg = MS_Helper_Settings::SETTINGS_MSG_UPDATED; 
  56.  
  57. echo $msg; 
  58. exit; 
  59.  
  60. /** 
  61. * Handle Ajax toggle menu items. 
  62. * Related action hooks: 
  63. * - wp_ajax_toggle_menu 
  64. * @since 1.0.0 
  65. */ 
  66. public function ajax_action_toggle_menu() { 
  67. $msg = MS_Helper_Settings::SETTINGS_MSG_NOT_UPDATED; 
  68.  
  69. $isset = array( 'item', 'value' ); 
  70. if ( $this->verify_nonce() 
  71. && self::validate_required( $isset, 'POST', false ) 
  72. && $this->is_admin_user() 
  73. ) { 
  74. $item = $_POST['item']; 
  75. $res = false; 
  76.  
  77. if ( lib3()->is_true( $_POST['value'] ) ) { 
  78. $res = MS_Model_Pages::create_menu( $item ); 
  79. } else { 
  80. $res = MS_Model_Pages::drop_menu( $item ); 
  81.  
  82. if ( $res ) { 
  83. $msg = MS_Helper_Settings::SETTINGS_MSG_UPDATED; 
  84.  
  85. echo $msg; 
  86. exit; 
  87.  
  88. /** 
  89. * Creates a new menu (not menu-item but whole menu). 
  90. * All top-level pages are added automatically to this new menu. 
  91. * Related action hooks: 
  92. * - wp_ajax_create_menu 
  93. * @since 1.0.0 
  94. */ 
  95. public function ajax_action_create_menu() { 
  96. $msg = MS_Helper_Settings::SETTINGS_MSG_NOT_UPDATED; 
  97. $content = ''; 
  98.  
  99. if ( $this->verify_nonce() && $this->is_admin_user() ) { 
  100. MS_Model_Pages::create_default_menu(); 
  101.  
  102. $setup = MS_Factory::create( 'MS_View_Settings_Page_Setup' ); 
  103. $content = $setup->show_menu_controls(); 
  104.  
  105. $msg = MS_Helper_Settings::SETTINGS_MSG_UPDATED; 
  106.  
  107. echo $msg . ':' . $content; 
  108. exit; 
  109.  
  110. /** 
  111. * This filter is called by WordPress when the page-listtable is created to 
  112. * display all available Posts/Pages. We use this filter to add a note 
  113. * to all pages that are special membership pages. 
  114. * @since 1.0.0 
  115. * @param array $states 
  116. * @param WP_Post $post 
  117. * @return array 
  118. */ 
  119. public function post_states( $states, $post ) { 
  120. if ( 'page' == $post->post_type ) { 
  121. if ( MS_Model_Pages::is_membership_page( $post->ID ) ) { 
  122. $url = MS_Controller_Plugin::get_admin_url( 'settings' ); 
  123. $states['membership2'] = sprintf( 
  124. '<a style="%2$s" href="%3$s">%1$s</a>',  
  125. __( 'Membership2 Page', 'membership2' ),  
  126. 'background:#aaa;color:#fff;padding:1px 4px;border-radius:4px;font-size:0.8em',  
  127. $url 
  128. ); 
  129.  
  130. return $states; 
  131.