MS_Controller_Protection

Controller for managing Protecion Rules.

Defined (1)

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

/app/controller/class-ms-controller-protection.php  
  1. class MS_Controller_Protection extends MS_Controller { 
  2.  
  3. /** 
  4. * The model to use for loading/saving Membership data. 
  5. * Access this value via $this->load_membership() 
  6. * @since 1.0.0 
  7. * @var MS_Model_Membership 
  8. */ 
  9. private $model = null; 
  10.  
  11. /** 
  12. * The active page tab. 
  13. * @since 1.0.0 
  14. * @var string 
  15. */ 
  16. protected $active_tab; 
  17.  
  18. /** 
  19. * Prepare the Membership manager. 
  20. * @since 1.0.0 
  21. */ 
  22. public function __construct() { 
  23. parent::__construct(); 
  24.  
  25. /** 
  26. * Initialize the admin-side functions. 
  27. * @since 1.0.0 
  28. */ 
  29. public function admin_init() { 
  30. $hook = MS_Controller_Plugin::admin_page_hook( 'protection' ); 
  31.  
  32. $this->run_action( 'load-' . $hook, 'admin_page_process' ); 
  33. $this->run_action( 'admin_print_scripts-' . $hook, 'enqueue_scripts' ); 
  34. $this->run_action( 'admin_print_styles-' . $hook, 'enqueue_styles' ); 
  35.  
  36. /** 
  37. * Load membership from request. 
  38. * @since 1.0.0 
  39. * @return MS_Model_Membership The membership model object. 
  40. */ 
  41. public function load_membership() { 
  42. $membership_id = 0; 
  43.  
  44. if ( empty( $this->model ) ) { 
  45. if ( ! empty( $_REQUEST['membership_id'] ) ) { 
  46. $membership_id = absint( $_REQUEST['membership_id'] ); 
  47.  
  48. if ( $membership_id == MS_Model_Membership::get_base()->id ) { 
  49. wp_safe_redirect( 
  50. esc_url_raw( 
  51. remove_query_arg( array( 'membership_id' ) ) 
  52. ); 
  53. } else { 
  54. $membership_id = MS_Model_Membership::get_base()->id; 
  55.  
  56. $this->model = MS_Factory::load( 
  57. 'MS_Model_Membership',  
  58. $membership_id 
  59. ); 
  60.  
  61. $this->model = apply_filters( 
  62. 'ms_controller_protection_load_membership',  
  63. $this->model,  
  64. $this 
  65. ); 
  66.  
  67. return $this->model; 
  68.  
  69. /** 
  70. * Process membership pages requests 
  71. * Verifies GET and POST requests to manage memberships. 
  72. * Redirect to next step after processing. 
  73. * @since 1.0.0 
  74. */ 
  75. public function admin_page_process() { 
  76. $membership = $this->load_membership(); 
  77.  
  78. do_action( 
  79. 'ms_controller_protection_admin_page_process',  
  80. $this->get_active_tab() 
  81. ); 
  82.  
  83. // Only accessible to admin users 
  84. if ( ! $this->is_admin_user() ) { return false; } 
  85.  
  86. if ( $this->verify_nonce( 'bulk' ) ) { 
  87. // Bulk-edit 
  88.  
  89. lib3()->array->equip_post( 'action', 'action2', 'item', 'rule_type' ); 
  90. $action = $_POST['action']; 
  91. if ( empty( $action ) || $action == '-1' ) { 
  92. $action = $_POST['action2']; 
  93. $items = $_POST['item']; 
  94. $rule_type = $_POST['rule_type']; 
  95.  
  96. /** 
  97. * The Bulk-Edit action is built like 'cmd-id' 
  98. * e.g. 'add-123' will add membership 123 to the selected items. 
  99. */ 
  100. if ( empty( $action ) ) { 
  101. $cmd = array(); 
  102. } elseif ( empty( $items ) ) { 
  103. $cmd = array(); 
  104. } elseif ( empty( $rule_type ) ) { 
  105. $cmd = array(); 
  106. } elseif ( '-1' == $action ) { 
  107. $cmd = array(); 
  108. } else { 
  109. $cmd = explode( '-', $action ); 
  110.  
  111. if ( 2 == count( $cmd ) ) { 
  112. $action = $cmd[0]; 
  113. $action_id = $cmd[1]; 
  114.  
  115. // Get a list of specified memberships... 
  116. if ( is_numeric( $action_id ) ) { 
  117. // ... either a single membership. 
  118. $memberships = array( 
  119. MS_Factory::load( 'MS_Model_Membership', $action_id ),  
  120. ); 
  121. } elseif ( 'all' == $action_id ) { 
  122. // ... or all memberships. 
  123. $memberships = MS_Model_Membership::get_memberships(); 
  124.  
  125. // Loop specified memberships and add the selected items. 
  126. foreach ( $memberships as $membership ) { 
  127. $rule = $membership->get_rule( $rule_type ); 
  128. foreach ( $items as $item ) { 
  129. switch ( $action ) { 
  130. case 'add': 
  131. $rule->give_access( $item ); 
  132. break; 
  133.  
  134. case 'rem': 
  135. $rule->remove_access( $item ); 
  136. break; 
  137. $membership->set_rule( $rule_type, $rule ); 
  138. $membership->save(); 
  139. } else { 
  140. // No action request found. 
  141.  
  142. /** 
  143. * Display Setup Membership2 page. 
  144. * @since 1.0.0 
  145. */ 
  146. public function admin_page() { 
  147. do_action( 'ms_controller_protection_admin_page' ); 
  148.  
  149. $data = array(); 
  150. $data['tabs'] = $this->get_available_tabs(); 
  151. $data['active_tab'] = $this->get_active_tab(); 
  152.  
  153. $view = MS_Factory::create( 'MS_View_Protection' ); 
  154. $view->data = apply_filters( 'ms_view_protection_data', $data, $this ); 
  155. $view->render(); 
  156.  
  157. /** 
  158. * Get available tabs for Membership2 page. 
  159. * @since 1.0.0 
  160. * @return array The tabs configuration. 
  161. */ 
  162. public function get_available_tabs() { 
  163. static $Tabs = null; 
  164.  
  165. if ( null === $Tabs ) { 
  166. $membership = $this->load_membership(); 
  167. $membership_id = $membership->id; 
  168. $is_base = $membership->is_base(); 
  169. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  170.  
  171. // First create a list including all possible tabs. 
  172. $tabs = array( 
  173. MS_Rule_Page::RULE_ID => true,  
  174. MS_Rule_Post::RULE_ID => true,  
  175. MS_Rule_Category::RULE_ID => true,  
  176. MS_Rule_Content::RULE_ID => true,  
  177. MS_Rule_Media::RULE_ID => true,  
  178. MS_Rule_MenuItem::RULE_ID => true,  
  179. MS_Rule_ReplaceMenu::RULE_ID => true,  
  180. MS_Rule_ReplaceLocation::RULE_ID => true,  
  181. MS_Rule_Shortcode::RULE_ID => true,  
  182. MS_Rule_Url::RULE_ID => true,  
  183. MS_Rule_Special::RULE_ID => true,  
  184. MS_Rule_MemberCaps::RULE_ID => true,  
  185. MS_Rule_MemberRoles::RULE_ID => true,  
  186. ); 
  187.  
  188. // Now remove items from the list that are not available. 
  189.  
  190. // Optionally show "Posts" 
  191. if ( ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_POST_BY_POST ) ) { 
  192. $tabs[MS_Rule_Post::RULE_ID] = false; 
  193.  
  194. // Optionally show "Category" 
  195. if ( ! MS_Model_Addon::is_enabled( MS_Addon_Category::ID ) ) { 
  196. $tabs[MS_Rule_Category::RULE_ID] = false; 
  197.  
  198. // Optionally show "Media" 
  199. if ( ! MS_Model_Addon::is_enabled( MS_Addon_Mediafiles::ID ) ) { 
  200. $tabs[MS_Rule_Media::RULE_ID] = false; 
  201.  
  202. // Either "Menu Item" or "Menus" or "Menu Location" 
  203. switch ( $settings->menu_protection ) { 
  204. case 'menu': 
  205. $tabs[MS_Rule_MenuItem::RULE_ID] = false; 
  206. $tabs[MS_Rule_ReplaceLocation::RULE_ID] = false; 
  207. break; 
  208.  
  209. case 'location': 
  210. $tabs[MS_Rule_MenuItem::RULE_ID] = false; 
  211. $tabs[MS_Rule_ReplaceMenu::RULE_ID] = false; 
  212. break; 
  213.  
  214. case 'item': 
  215. default: 
  216. $tabs[MS_Rule_ReplaceMenu::RULE_ID] = false; 
  217. $tabs[MS_Rule_ReplaceLocation::RULE_ID] = false; 
  218. break; 
  219.  
  220. // Maybe "Special Pages". 
  221. if ( ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_SPECIAL_PAGES ) ) { 
  222. $tabs[MS_Rule_Special::RULE_ID] = false; 
  223.  
  224. // Maybe "URLs" 
  225. if ( ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_URL_GROUPS ) ) { 
  226. $tabs[MS_Rule_Url::RULE_ID] = false; 
  227.  
  228. // Maybe "Shortcodes" 
  229. if ( ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_SHORTCODE ) ) { 
  230. $tabs[MS_Rule_Shortcode::RULE_ID] = false; 
  231.  
  232. // Maybe "Membercaps" 
  233. if ( MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_MEMBERCAPS ) ) { 
  234. if ( MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_MEMBERCAPS_ADV ) ) { 
  235. $tabs[MS_Rule_MemberRoles::RULE_ID] = false; 
  236. } else { 
  237. $tabs[MS_Rule_MemberCaps::RULE_ID] = false; 
  238. } else { 
  239. $tabs[MS_Rule_MemberRoles::RULE_ID] = false; 
  240. $tabs[MS_Rule_MemberCaps::RULE_ID] = false; 
  241.  
  242. lib3()->array->equip( $_GET, 'page' ); 
  243.  
  244. // Allow Add-ons to add or remove rule tabs 
  245. $tabs = apply_filters( 
  246. 'ms_controller_protection_tabs',  
  247. $tabs,  
  248. $membership_id 
  249. ); 
  250.  
  251. $page = sanitize_html_class( $_GET['page'], MS_Controller_Plugin::MENU_SLUG . '-memberships' ); 
  252. $rule_titles = MS_Model_Rule::get_rule_type_titles(); 
  253.  
  254. $result = array(); 
  255. foreach ( $tabs as $rule_type => $state ) { 
  256. if ( ! $state ) { continue; } 
  257.  
  258. $url = sprintf( 
  259. '%s?page=%s&tab=%s',  
  260. admin_url( 'admin.php' ),  
  261. $page,  
  262. $rule_type 
  263. ); 
  264.  
  265. // Try to keep the selected Membership and Status filter. 
  266. if ( ! empty( $_REQUEST['membership_id'] ) ) { 
  267. $url = esc_url_raw( 
  268. add_query_arg( 
  269. array( 'membership_id' => $_REQUEST['membership_id'] ),  
  270. $url 
  271. ); 
  272. if ( ! empty( $_REQUEST['status'] ) ) { 
  273. $url = esc_url_raw( 
  274. add_query_arg( 
  275. array( 'status' => $_REQUEST['status'] ),  
  276. $url 
  277. ); 
  278.  
  279. $result[ $rule_type ] = array( 
  280. 'title' => $rule_titles[ $rule_type ],  
  281. 'url' => $url,  
  282. ); 
  283.  
  284. $Tabs = apply_filters( 
  285. 'ms_controller_protection_get_available_tabs',  
  286. $result,  
  287. $membership_id,  
  288. $this 
  289. ); 
  290.  
  291. return $Tabs; 
  292.  
  293. /** 
  294. * Get the current membership page's active tab. 
  295. * @since 1.0.0 
  296. * @return string The active tab. 
  297. */ 
  298. public function get_active_tab() { 
  299. $tabs = $this->get_available_tabs(); 
  300.  
  301. reset( $tabs ); 
  302. $first_key = key( $tabs ); 
  303.  
  304. // Setup navigation tabs. 
  305. $active_tab = isset( $_REQUEST['tab'] ) ? $_REQUEST['tab'] : ''; 
  306. $active_tab = sanitize_html_class( $active_tab, $first_key ); 
  307.  
  308. if ( ! array_key_exists( $active_tab, $tabs ) ) { 
  309. $active_tab = $first_key; 
  310.  
  311. $this->active_tab = apply_filters( 
  312. 'ms_controller_protection_get_active_tab',  
  313. $active_tab 
  314. ); 
  315.  
  316. return $this->active_tab; 
  317.  
  318. /** 
  319. * Load Membership manager specific styles. 
  320. * @since 1.0.0 
  321. */ 
  322. public function enqueue_styles() { 
  323. lib3()->ui->add( 'jquery-ui' ); 
  324.  
  325. do_action( 'ms_controller_protection_enqueue_styles', $this ); 
  326.  
  327. /** 
  328. * Load Membership manager specific scripts. 
  329. * @since 1.0.0 
  330. */ 
  331. public function enqueue_scripts() { 
  332. /** 
  333. * Get a list of the dripped memberships: 
  334. * We need this info in the javascript. 
  335. */ 
  336. $dripped = array(); 
  337. foreach ( MS_Model_Membership::get_dripped_memberships() as $item ) { 
  338. $dripped[ $item->id ] = $item->name; 
  339.  
  340. $data = array( 
  341. 'ms_init' => array(),  
  342. 'lang' => array( 
  343. 'quickedit_error' => __( 'Error while saving changes.', 'membership2' ),  
  344. ),  
  345. 'dripped' => $dripped,  
  346. ); 
  347.  
  348. $data['ms_init'][] = 'view_protected_content'; 
  349.  
  350. switch ( $this->get_active_tab() ) { 
  351. case 'url': 
  352. $data['valid_rule_msg'] = __( 'Valid', 'membership2' ); 
  353. $data['invalid_rule_msg'] = __( 'Invalid', 'membership2' ); 
  354. $data['empty_msg'] = __( 'Before testing you have to first enter one or more Page URLs above.', 'membership2' ); 
  355. $data['ms_init'][] = 'view_membership_urlgroup'; 
  356. break; 
  357.  
  358. default: 
  359. wp_enqueue_script( 'jquery-ui-datepicker' ); 
  360. wp_enqueue_script( 'jquery-validate' ); 
  361. break; 
  362.  
  363. lib3()->ui->data( 'ms_data', $data ); 
  364. wp_enqueue_script( 'ms-admin' ); 
  365.  
  366. do_action( 'ms_controller_protection_enqueue_scripts', $this ); 
  367.