MS_Controller_Settings

Controller for managing Plugin Settings.

Defined (1)

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

/app/controller/class-ms-controller-settings.php  
  1. class MS_Controller_Settings extends MS_Controller { 
  2.  
  3. /** 
  4. * AJAX action constants. 
  5. * @since 1.0.0 
  6. * @var string 
  7. */ 
  8. const AJAX_ACTION_TOGGLE_SETTINGS = 'toggle_settings'; 
  9. const AJAX_ACTION_UPDATE_SETTING = 'update_setting'; 
  10. const AJAX_ACTION_UPDATE_CUSTOM_SETTING = 'update_custom_setting'; 
  11. const AJAX_ACTION_UPDATE_PROTECTION_MSG = 'update_protection_msg'; 
  12.  
  13. /** 
  14. * Settings tabs. 
  15. * @since 1.0.1.0 
  16. * @var string 
  17. */ 
  18. const TAB_GENERAL = 'general'; 
  19. const TAB_PAYMENT = 'payment'; 
  20. const TAB_MESSAGES = 'messages'; 
  21. const TAB_EMAILS = 'emails'; 
  22. const TAB_IMPORT = 'import'; 
  23.  
  24. /** 
  25. * The current active tab in the vertical navigation. 
  26. * @since 1.0.0 
  27. * @var string 
  28. */ 
  29. private $active_tab = null; 
  30.  
  31. /** 
  32. * Construct Settings manager. 
  33. * @since 1.0.0 
  34. */ 
  35. public function __construct() { 
  36. parent::__construct(); 
  37.  
  38. /** 
  39. * Check if the user wants to manually run the Cron services. 
  40. * This block calls the action 'ms_run_cron_services' which is defined 
  41. * in MS_Model_Plugin. It will run all cron jobs and re-schedule them. 
  42. * @since 1.0.0 
  43. */ 
  44. if ( isset( $_REQUEST['run_cron'] ) ) { 
  45. $url = esc_url_raw( remove_query_arg( 'run_cron' ) ); 
  46. do_action( 'ms_run_cron_services', $_REQUEST['run_cron'] ); 
  47. wp_safe_redirect( $url ); 
  48. exit; 
  49.  
  50. $this->add_action( 
  51. 'ms_controller_membership_setup_completed',  
  52. 'auto_setup_settings' 
  53. ); 
  54.  
  55. $this->add_ajax_action( self::AJAX_ACTION_TOGGLE_SETTINGS, 'ajax_action_toggle_settings' ); 
  56. $this->add_ajax_action( self::AJAX_ACTION_UPDATE_SETTING, 'ajax_action_update_setting' ); 
  57. $this->add_ajax_action( self::AJAX_ACTION_UPDATE_CUSTOM_SETTING, 'ajax_action_update_custom_setting' ); 
  58. $this->add_ajax_action( self::AJAX_ACTION_UPDATE_PROTECTION_MSG, 'ajax_action_update_protection_msg' ); 
  59.  
  60.  
  61. /** 
  62. * Initialize the admin-side functions. 
  63. * @since 1.0.0 
  64. */ 
  65. public function admin_init() { 
  66. $hook = MS_Controller_Plugin::admin_page_hook( 'settings' ); 
  67.  
  68. $this->run_action( 'load-' . $hook, 'admin_settings_manager' ); 
  69. $this->run_action( 'admin_print_scripts-' . $hook, 'enqueue_scripts' ); 
  70.  
  71. /** 
  72. * Get settings model 
  73. * @since 1.0.0 
  74. * @return MS_Model_Settings 
  75. */ 
  76. public function get_model() { 
  77. return MS_Factory::load( 'MS_Model_Settings' ); 
  78.  
  79. /** 
  80. * Handle Ajax toggle action. 
  81. * Related action hooks: 
  82. * * wp_ajax_toggle_settings 
  83. * @since 1.0.0 
  84. */ 
  85. public function ajax_action_toggle_settings() { 
  86. $msg = 0; 
  87.  
  88. $fields = array( 'setting' ); 
  89. if ( $this->verify_nonce() 
  90. && self::validate_required( $fields ) 
  91. && $this->is_admin_user() 
  92. ) { 
  93. $msg = $this->save_general( 
  94. $_POST['action'],  
  95. array( $_POST['setting'] => 1 ) 
  96. ); 
  97.  
  98. wp_die( $msg ); 
  99.  
  100. /** 
  101. * Handle Ajax update setting action. 
  102. * Related action hooks: 
  103. * * wp_ajax_update_setting 
  104. * @since 1.0.0 
  105. */ 
  106. public function ajax_action_update_setting() { 
  107. $msg = MS_Helper_Settings::SETTINGS_MSG_NOT_UPDATED; 
  108.  
  109. $isset = array( 'field', 'value' ); 
  110. if ( $this->verify_nonce() 
  111. && self::validate_required( $isset, 'POST', false ) 
  112. && $this->is_admin_user() 
  113. ) { 
  114. lib3()->array->strip_slashes( $_POST, 'value' ); 
  115.  
  116. $msg = $this->save_general( 
  117. $_POST['action'],  
  118. array( $_POST['field'] => $_POST['value'] ) 
  119. ); 
  120.  
  121. // Some settings require to flush WP rewrite rules. 
  122. flush_rewrite_rules(); 
  123.  
  124. wp_die( $msg ); 
  125.  
  126. /** 
  127. * Handle Ajax update custom setting action. 
  128. * Related action hooks: 
  129. * * wp_ajax_update_custom_setting 
  130. * @since 1.0.0 
  131. */ 
  132. public function ajax_action_update_custom_setting() { 
  133. $msg = MS_Helper_Settings::SETTINGS_MSG_NOT_UPDATED; 
  134.  
  135. $isset = array( 'group', 'field', 'value' ); 
  136. if ( $this->verify_nonce() 
  137. && self::validate_required( $isset, 'POST', false ) 
  138. && $this->is_admin_user() 
  139. ) { 
  140. $settings = $this->get_model(); 
  141. lib3()->array->strip_slashes( $_POST, 'value' ); 
  142.  
  143. $settings->set_custom_setting( 
  144. $_POST['group'],  
  145. $_POST['field'],  
  146. $_POST['value'] 
  147. ); 
  148. $settings->save(); 
  149. $msg = MS_Helper_Settings::SETTINGS_MSG_UPDATED; 
  150.  
  151. wp_die( $msg ); 
  152.  
  153. /** 
  154. * Handle Ajax update protection msg. 
  155. * Related action hooks: 
  156. * * wp_ajax_update_protection_msg 
  157. * @since 1.0.0 
  158. */ 
  159. public function ajax_action_update_protection_msg() { 
  160. $msg = MS_Helper_Settings::SETTINGS_MSG_NOT_UPDATED; 
  161.  
  162. if ( ! $this->is_admin_user() ) { 
  163. return $msg; 
  164.  
  165. $isset_update = array( 'type', 'value' ); 
  166. $isset_toggle = array( 'field', 'value', 'membership_id' ); 
  167.  
  168. // Update a message. 
  169. if ( $this->verify_nonce() && $this->is_admin_user() ) { 
  170. $settings = $this->get_model(); 
  171.  
  172. if ( self::validate_required( $isset_update, 'POST', false ) ) { 
  173. lib3()->array->strip_slashes( $_POST, 'value' ); 
  174. lib3()->array->equip_post( 'membership_id' ); 
  175.  
  176. $settings->set_protection_message( 
  177. $_POST['type'],  
  178. $_POST['value'],  
  179. $_POST['membership_id'] 
  180. ); 
  181. $settings->save(); 
  182. $msg = MS_Helper_Settings::SETTINGS_MSG_UPDATED; 
  183.  
  184. // Toggle a override message flag. 
  185. elseif ( self::validate_required( $isset_toggle, 'POST', false ) ) { 
  186. $field = $_POST['field']; 
  187.  
  188. if ( 0 === strpos( $field, 'override_' ) ) { 
  189. $type = substr( $field, 9 ); 
  190. if ( lib3()->is_true( $_POST['value'] ) ) { 
  191. $settings->set_protection_message( 
  192. $type,  
  193. $settings->get_protection_message( $type ),  
  194. $_POST['membership_id'] 
  195. ); 
  196. } else { 
  197. $settings->set_protection_message( 
  198. $type,  
  199. null,  
  200. $_POST['membership_id'] 
  201. ); 
  202.  
  203. $settings->save(); 
  204. $msg = MS_Helper_Settings::SETTINGS_MSG_UPDATED; 
  205.  
  206. wp_die( $msg ); 
  207.  
  208. /** 
  209. * Auto setup settings. 
  210. * Fires after a membership setup is completed. 
  211. * This hook is executed every time a new membership is created. 
  212. * Related Action Hooks: 
  213. * - ms_controller_membership_setup_completed 
  214. * @since 1.0.0 
  215. * @param MS_Model_Membership $membership 
  216. */ 
  217. public function auto_setup_settings( $membership ) { 
  218. $settings = $this->get_model(); 
  219.  
  220. // Create special pages. 
  221. MS_Model_Pages::create_missing_pages(); 
  222.  
  223. $pg_prot_cont = MS_Model_Pages::get_page( MS_Model_Pages::MS_PAGE_PROTECTED_CONTENT ); 
  224. $pg_acco = MS_Model_Pages::get_page( MS_Model_Pages::MS_PAGE_ACCOUNT ); 
  225. $pg_regi = MS_Model_Pages::get_page( MS_Model_Pages::MS_PAGE_REGISTER ); 
  226. $pg_regi_comp = MS_Model_Pages::get_page( MS_Model_Pages::MS_PAGE_REG_COMPLETE ); 
  227. $pg_memb = MS_Model_Pages::get_page( MS_Model_Pages::MS_PAGE_MEMBERSHIPS ); 
  228.  
  229. // Publish special pages. 
  230. // Tip: Only pages must be published that are added to the menu. 
  231. wp_publish_post( $pg_acco->ID ); 
  232. if ( ! $membership->private ) { 
  233. wp_publish_post( $pg_memb->ID ); 
  234. wp_publish_post( $pg_regi->ID ); 
  235.  
  236. // Create new WordPress menu-items. 
  237. MS_Model_Pages::create_menu( MS_Model_Pages::MS_PAGE_ACCOUNT ); 
  238. if ( ! $membership->private ) { 
  239. MS_Model_Pages::create_menu( MS_Model_Pages::MS_PAGE_MEMBERSHIPS ); 
  240. MS_Model_Pages::create_menu( MS_Model_Pages::MS_PAGE_REGISTER ); 
  241.  
  242. // Enable Membership2. 
  243. $settings->plugin_enabled = true; 
  244. $settings->save(); 
  245.  
  246. // Enable the "Allow user registration" setting of WordPress 
  247. MS_Model_Member::allow_registration(); 
  248.  
  249. /** 
  250. * Get available tabs for editing the membership. 
  251. * @since 1.0.0 
  252. * @return array The tabs configuration. 
  253. */ 
  254. public function get_tabs() { 
  255. $tabs = array( 
  256. self::TAB_GENERAL => array( 
  257. 'title' => __( 'General', 'membership2' ),  
  258. ),  
  259. self::TAB_PAYMENT => array( 
  260. 'title' => __( 'Payment', 'membership2' ),  
  261. ),  
  262. self::TAB_MESSAGES => array( 
  263. 'title' => __( 'Protection Messages', 'membership2' ),  
  264. ),  
  265. self::TAB_EMAILS => array( 
  266. 'title' => __( 'Automated Email Responses', 'membership2' ),  
  267. ),  
  268. self::TAB_IMPORT => array( 
  269. 'title' => __( 'Import Tool', 'membership2' ),  
  270. ),  
  271. ); 
  272.  
  273. $def_key = MS_Controller_Plugin::MENU_SLUG . '-settings'; 
  274. lib3()->array->equip_get( 'page' ); 
  275. $page = sanitize_html_class( $_GET['page'], $def_key ); 
  276.  
  277. foreach ( $tabs as $key => $tab ) { 
  278. $tabs[ $key ]['url'] = sprintf( 
  279. 'admin.php?page=%1$s&tab=%2$s',  
  280. esc_attr( $page ),  
  281. esc_attr( $key ) 
  282. ); 
  283.  
  284. return apply_filters( 'ms_controller_settings_get_tabs', $tabs, $this ); 
  285.  
  286. /** 
  287. * Get the current active settings page/tab. 
  288. * @since 1.0.0 
  289. */ 
  290. public function get_active_tab() { 
  291. if ( null === $this->active_tab ) { 
  292. if ( ! MS_Controller_Plugin::is_page( 'settings' ) ) { 
  293. $this->active_tab = ''; 
  294. } else { 
  295. $tabs = $this->get_tabs(); 
  296.  
  297. reset( $tabs ); 
  298. $first_key = key( $tabs ); 
  299.  
  300. // Setup navigation tabs. 
  301. lib3()->array->equip_get( 'tab' ); 
  302. $active_tab = sanitize_html_class( $_GET['tab'], $first_key ); 
  303.  
  304. if ( ! array_key_exists( $active_tab, $tabs ) ) { 
  305. $new_url = esc_url_raw( 
  306. add_query_arg( array( 'tab' => $first_key ) ) 
  307. ); 
  308. wp_safe_redirect( $new_url ); 
  309. exit; 
  310. } else { 
  311. $this->active_tab = apply_filters( 
  312. 'ms_controller_settings_get_active_tab',  
  313. $active_tab 
  314. ); 
  315.  
  316. return apply_filters( 
  317. 'ms_controller_settings_get_active_tab',  
  318. $this->active_tab,  
  319. $this 
  320. ); 
  321.  
  322. /** 
  323. * Manages settings actions. 
  324. * Verifies GET and POST requests to manage settings. 
  325. * @since 1.0.0 
  326. */ 
  327. public function admin_settings_manager() { 
  328. MS_Helper_Settings::print_admin_message(); 
  329. $this->get_active_tab(); 
  330. $msg = 0; 
  331. $redirect = false; 
  332.  
  333. if ( $this->is_admin_user() ) { 
  334. if ( $this->verify_nonce() || $this->verify_nonce( null, 'GET' ) ) { 
  335. /** 
  336. * After verifying permissions those filters can be used by Add-ons 
  337. * to process their own settings form. 
  338. * @since 1.0.1.0 
  339. */ 
  340. do_action( 
  341. 'ms_admin_settings_manager-' . $this->active_tab 
  342. ); 
  343. do_action( 
  344. 'ms_admin_settings_manager',  
  345. $this->active_tab 
  346. ); 
  347.  
  348. switch ( $this->active_tab ) { 
  349. case self::TAB_GENERAL: 
  350. lib3()->array->equip_request( 'action', 'network_site' ); 
  351. $action = $_REQUEST['action']; 
  352.  
  353. $redirect = esc_url_raw( 
  354. remove_query_arg( array( 'msg' => $msg ) ) 
  355. ); 
  356.  
  357. // See if we change settings for the network-wide mode. 
  358. if ( MS_Plugin::is_network_wide() ) { 
  359. $new_site_id = intval( $_REQUEST['network_site'] ); 
  360.  
  361. if ( 'network_site' == $action && ! empty( $new_site_id ) ) { 
  362. $old_site_id = MS_Model_Pages::get_setting( 'site_id' ); 
  363. if ( $old_site_id != $new_site_id ) { 
  364. MS_Model_Pages::set_setting( 'site_id', $new_site_id ); 
  365. $msg = MS_Helper_Settings::SETTINGS_MSG_SITE_UPDATED; 
  366. $redirect = esc_url_raw( 
  367. add_query_arg( array( 'msg' => $msg ) ) 
  368. ); 
  369. break; 
  370.  
  371. case self::TAB_IMPORT: 
  372. $tool = MS_Factory::create( 'MS_Controller_Import' ); 
  373.  
  374. // Output is passed to the view via self::_message() 
  375. $tool->process(); 
  376. break; 
  377.  
  378. case self::TAB_PAYMENT: 
  379. case self::TAB_MESSAGES: 
  380. break; 
  381.  
  382. default: 
  383. break; 
  384.  
  385. if ( $redirect ) { 
  386. wp_safe_redirect( $redirect ); 
  387. exit(); 
  388.  
  389. /** 
  390. * Callback function from 'Membership' navigation. 
  391. * Menu Item: Membership > Settings 
  392. * @since 1.0.0 
  393. */ 
  394. public function admin_page() { 
  395. $hook = 'ms_controller_settings-' . $this->active_tab; 
  396.  
  397. do_action( $hook ); 
  398.  
  399. $view = MS_Factory::create( 'MS_View_Settings_Edit' ); 
  400. $view = apply_filters( $hook . '_view', $view ); 
  401.  
  402. $data = array(); 
  403. $data['tabs'] = $this->get_tabs(); 
  404. $data['settings'] = $this->get_model(); 
  405.  
  406. $data['message'] = self::_message(); 
  407.  
  408. if ( isset( $data['message']['error'] ) ) { 
  409. lib3()->ui->admin_message( $data['message']['error'], 'err' ); 
  410.  
  411. switch ( $this->get_active_tab() ) { 
  412. case self::TAB_EMAILS: 
  413. $type = MS_Model_Communication::COMM_TYPE_REGISTRATION; 
  414.  
  415. $temp_type = isset( $_GET['comm_type'] ) ? $_GET['comm_type'] : ''; 
  416. if ( MS_Model_Communication::is_valid_communication_type( $temp_type ) ) { 
  417. $type = $temp_type; 
  418.  
  419. $comm = MS_Model_Communication::get_communication( $type ); 
  420.  
  421. $data['comm'] = $comm; 
  422. break; 
  423.  
  424. $data = array_merge( $data, $view->data ); 
  425. $view->data = apply_filters( $hook . '_data', $data ); 
  426. $view->model = $this->get_model(); 
  427. $view->render(); 
  428.  
  429. /** 
  430. * Save general tab settings. 
  431. * @since 1.0.0 
  432. * @param string $action The action to execute. 
  433. * @param string $settings Array of settings to which action will be taken. 
  434. */ 
  435. public function save_general( $action, $fields ) { 
  436. $msg = MS_Helper_Settings::SETTINGS_MSG_NOT_UPDATED; 
  437.  
  438. if ( ! $this->is_admin_user() ) { 
  439. return $msg; 
  440.  
  441. $settings = $this->get_model(); 
  442.  
  443. if ( is_array( $fields ) ) { 
  444. foreach ( $fields as $field => $value ) { 
  445. switch ( $action ) { 
  446. case 'toggle_activation': 
  447. case 'toggle_settings': 
  448. $settings->$field = ! $settings->$field; 
  449. break; 
  450.  
  451. case 'save_general': 
  452. case 'submit_payment': 
  453. case 'save_downloads': 
  454. case 'save_payment_settings': 
  455. case 'update_setting': 
  456. default: 
  457. $settings->$field = $value; 
  458. break; 
  459. $settings->save(); 
  460.  
  461. $msg = MS_Helper_Settings::SETTINGS_MSG_UPDATED; 
  462.  
  463. return apply_filters( 
  464. 'ms_controller_settings_save_general',  
  465. $msg,  
  466. $action,  
  467. $fields,  
  468. $this 
  469. ); 
  470.  
  471. /** 
  472. * Load Membership admin scripts. 
  473. * @since 1.0.0 
  474. */ 
  475. public function enqueue_scripts() { 
  476. $active_tab = $this->get_active_tab(); 
  477. do_action( 'ms_controller_settings_enqueue_scripts_' . $active_tab ); 
  478.  
  479. $plugin_url = MS_Plugin::instance()->url; 
  480. $version = MS_Plugin::instance()->version; 
  481. $initial_url = MS_Controller_Plugin::get_admin_url(); 
  482.  
  483. $data = array( 
  484. 'ms_init' => array(),  
  485. 'initial_url' => $initial_url,  
  486. ); 
  487.  
  488. $data['ms_init'][] = 'view_settings'; 
  489.  
  490. switch ( $active_tab ) { 
  491. case self::TAB_PAYMENT: 
  492. add_thickbox(); 
  493. $data['ms_init'][] = 'view_settings_payment'; 
  494. break; 
  495.  
  496. case self::TAB_MESSAGES: 
  497. $data['ms_init'][] = 'view_settings_protection'; 
  498. break; 
  499.  
  500. case self::TAB_EMAILS: 
  501. $data['ms_init'][] = 'view_settings_automated_msg'; 
  502. break; 
  503.  
  504. case self::TAB_GENERAL: 
  505. $data['ms_init'][] = 'view_settings_setup'; 
  506. break; 
  507.  
  508. lib3()->ui->data( 'ms_data', $data ); 
  509. wp_enqueue_script( 'ms-admin' );