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

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