/app/controller/class-ms-controller-addon.php

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