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

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