MS_Controller_Addon

Controller for Membership add-ons.

Defined (1)

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

/app/controller/class-ms-controller-addon.php  
  1. class MS_Controller_Addon extends MS_Controller { 
  2.  
  3. /** 
  4. * AJAX action constants. 
  5. * @since 1.0.0 
  6. * @var string 
  7. */ 
  8. const AJAX_ACTION_TOGGLE_ADDON = 'toggle_addon'; 
  9.  
  10. /** 
  11. * Prepare the Add-on manager. 
  12. * @since 1.0.0 
  13. */ 
  14. public function __construct() { 
  15. parent::__construct(); 
  16.  
  17. $this->add_action( 
  18. 'ms_controller_membership_setup_completed',  
  19. 'auto_setup_addons' 
  20. ); 
  21.  
  22. $this->add_ajax_action( 
  23. self::AJAX_ACTION_TOGGLE_ADDON,  
  24. 'ajax_action_toggle_addon' 
  25. ); 
  26.  
  27. /** 
  28. * Initialize the admin-side functions. 
  29. * @since 1.0.0 
  30. */ 
  31. public function admin_init() { 
  32. $hook = MS_Controller_Plugin::admin_page_hook( 'addon' ); 
  33.  
  34. $this->run_action( 'load-' . $hook, 'admin_addon_process' ); 
  35. $this->run_action( 'admin_print_scripts-' . $hook, 'enqueue_scripts' ); 
  36.  
  37. /** 
  38. * Handle Ajax toggle action. 
  39. * Related Action Hooks: 
  40. * - wp_ajax_toggle_gateway 
  41. * @since 1.0.0 
  42. */ 
  43. public function ajax_action_toggle_addon() { 
  44. $msg = 0; 
  45.  
  46. if ( $this->verify_nonce() 
  47. && ! empty( $_POST['addon'] ) 
  48. && $this->is_admin_user() 
  49. ) { 
  50. $addon = array( $_POST['addon'] ); 
  51.  
  52. if ( isset( $_POST['value'] ) ) { 
  53. if ( lib3()->is_true( $_POST['value'] ) ) { 
  54. $msg = $this->save_addon( 'enable', $addon ); 
  55. } else { 
  56. $msg = $this->save_addon( 'disable', $addon ); 
  57. } else { 
  58. $msg = $this->save_addon( 'toggle_activation', $addon ); 
  59.  
  60. // Some Add-ons require to flush WP rewrite rules. 
  61. flush_rewrite_rules(); 
  62.  
  63. echo $msg; 
  64. exit; 
  65.  
  66. /** 
  67. * Auto setup addons when membership setup is completed. 
  68. * Related Action Hooks: 
  69. * - ms_controller_membership_setup_completed 
  70. * @since 1.0.0 
  71. */ 
  72. public function auto_setup_addons( $membership ) { 
  73. $addon = MS_Factory::load( 'MS_Model_Addon' ); 
  74. $addon->auto_config( $membership ); 
  75.  
  76. /** 
  77. * Handles Add-on admin actions. 
  78. * Handles activation/deactivation toggles and bulk update actions, then saves the model. 
  79. * @since 1.0.0 
  80. */ 
  81. public function admin_addon_process() { 
  82. /** 
  83. * Hook into the Addon request handler before processing. 
  84. * **Note:** 
  85. * This action uses the "raw" request objects which could lead to SQL injections / XSS. 
  86. * By hooking this action you need to take **responsibility** for filtering user input. 
  87. * @since 1.0.0 
  88. * @param object $this The MS_Controller_Addon object. 
  89. */ 
  90. do_action( 'ms_controller_addon_admin_addon_process', $this ); 
  91.  
  92. $msg = 0; 
  93. $fields = array( 'addon', 'action', 'action2' ); 
  94.  
  95. if ( $this->verify_nonce( 'bulk' ) 
  96. && self::validate_required( $fields ) 
  97. ) { 
  98. $action = $_POST['action'] != -1 ? $_POST['action'] : $_POST['action2']; 
  99. $msg = $this->save_addon( $action, $_POST['addon'] ); 
  100. wp_safe_redirect( 
  101. esc_url_raw( add_query_arg( array( 'msg' => $msg ) ) ) 
  102. ); 
  103. exit; 
  104.  
  105.  
  106. /** 
  107. * Load and render the Add-on manager view. 
  108. * @since 1.0.0 
  109. */ 
  110. public function admin_page() { 
  111. // Reload the add-on list. 
  112. do_action( 'ms_model_addon_flush' ); 
  113.  
  114. /** 
  115. * Create / Filter the Addon admin view. 
  116. * @since 1.0.0 
  117. * @param object $this The MS_Controller_Addon object. 
  118. */ 
  119. $view = MS_Factory::create( 'MS_View_Addon' ); 
  120. $data = array( 
  121. 'addon' => MS_Factory::load( 'MS_Model_Addon' ),  
  122. ); 
  123.  
  124. $view->data = apply_filters( 'ms_view_addon_data', $data ); 
  125. $view->render(); 
  126.  
  127. /** 
  128. * Call the model to save the addon settings. 
  129. * Saves activation/deactivation settings. 
  130. * @since 1.0.0 
  131. * @param string $action The action to perform on the add-on 
  132. * @param object[] $addon_types The add-on or add-ons types to update. 
  133. */ 
  134. public function save_addon( $action, $addon_types ) { 
  135. if ( ! $this->is_admin_user() ) { 
  136. return; 
  137.  
  138. $addon = MS_Factory::load( 'MS_Model_Addon' ); 
  139.  
  140. foreach ( $addon_types as $addon_type ) { 
  141. switch ( $action ) { 
  142. case 'enable': 
  143. $addon->enable( $addon_type ); 
  144. break; 
  145.  
  146. case 'disable': 
  147. $addon->disable( $addon_type ); 
  148. break; 
  149.  
  150. case 'toggle_activation': 
  151. $addon->toggle_activation( $addon_type ); 
  152. break; 
  153.  
  154. return true; 
  155.  
  156. /** 
  157. * Load Add-on specific scripts. 
  158. * @since 1.0.0 
  159. */ 
  160. public function enqueue_scripts() { 
  161. $data = array( 
  162. 'ms_init' => array( 'view_addons' ),  
  163. ); 
  164.  
  165. lib3()->ui->data( 'ms_data', $data ); 
  166. wp_enqueue_script( 'ms-admin' ); 
  167.