/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_CptItem::RULE_ID => true,  
  197. MS_Rule_CptGroup::RULE_ID => true,  
  198. MS_Rule_Content::RULE_ID => true,  
  199. MS_Rule_Media::RULE_ID => true,  
  200. MS_Rule_MenuItem::RULE_ID => true,  
  201. MS_Rule_ReplaceMenu::RULE_ID => true,  
  202. MS_Rule_ReplaceLocation::RULE_ID => true,  
  203. MS_Rule_Shortcode::RULE_ID => true,  
  204. MS_Rule_Url::RULE_ID => true,  
  205. MS_Rule_Special::RULE_ID => true,  
  206. MS_Rule_Adminside::RULE_ID => true,  
  207. MS_Rule_MemberCaps::RULE_ID => true,  
  208. MS_Rule_MemberRoles::RULE_ID => true,  
  209. ); 
  210.  
  211. // Now remove items from the list that are not available. 
  212.  
  213. // Optionally show "Posts" 
  214. if ( ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_POST_BY_POST ) ) { 
  215. $tabs[MS_Rule_Post::RULE_ID] = false; 
  216.  
  217. // Optionally show "Category" 
  218. if ( ! MS_Model_Addon::is_enabled( MS_Addon_Category::ID ) ) { 
  219. $tabs[MS_Rule_Category::RULE_ID] = false; 
  220.  
  221. // Optionally show "Media" 
  222. if ( ! MS_Model_Addon::is_enabled( MS_Addon_Mediafiles::ID ) ) { 
  223. $tabs[MS_Rule_Media::RULE_ID] = false; 
  224.  
  225. // Either "CPT Group" or "CPT Posts" 
  226. if ( MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_CPT_POST_BY_POST ) ) { 
  227. $tabs[MS_Rule_CptGroup::RULE_ID] = false; 
  228. } else { 
  229. $tabs[MS_Rule_CptItem::RULE_ID] = false; 
  230.  
  231. // Either "Menu Item" or "Menus" or "Menu Location" 
  232. switch ( $settings->menu_protection ) { 
  233. case 'menu': 
  234. $tabs[MS_Rule_MenuItem::RULE_ID] = false; 
  235. $tabs[MS_Rule_ReplaceLocation::RULE_ID] = false; 
  236. break; 
  237.  
  238. case 'location': 
  239. $tabs[MS_Rule_MenuItem::RULE_ID] = false; 
  240. $tabs[MS_Rule_ReplaceMenu::RULE_ID] = false; 
  241. break; 
  242.  
  243. case 'item': 
  244. default: 
  245. $tabs[MS_Rule_ReplaceMenu::RULE_ID] = false; 
  246. $tabs[MS_Rule_ReplaceLocation::RULE_ID] = false; 
  247. break; 
  248.  
  249. // Maybe "Special Pages". 
  250. if ( ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_SPECIAL_PAGES ) ) { 
  251. $tabs[MS_Rule_Special::RULE_ID] = false; 
  252.  
  253. // Maybe "URLs" 
  254. if ( ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_URL_GROUPS ) ) { 
  255. $tabs[MS_Rule_Url::RULE_ID] = false; 
  256.  
  257. // Maybe "Shortcodes" 
  258. if ( ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_SHORTCODE ) ) { 
  259. $tabs[MS_Rule_Shortcode::RULE_ID] = false; 
  260.  
  261. // Maybe "Admin-Side" 
  262. if ( ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_ADMINSIDE ) ) { 
  263. $tabs[MS_Rule_Adminside::RULE_ID] = false; 
  264.  
  265. // Maybe "Membercaps" 
  266. if ( MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_MEMBERCAPS ) ) { 
  267. if ( MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_MEMBERCAPS_ADV ) ) { 
  268. $tabs[MS_Rule_MemberRoles::RULE_ID] = false; 
  269. } else { 
  270. $tabs[MS_Rule_MemberCaps::RULE_ID] = false; 
  271. } else { 
  272. $tabs[MS_Rule_MemberRoles::RULE_ID] = false; 
  273. $tabs[MS_Rule_MemberCaps::RULE_ID] = false; 
  274.  
  275. lib3()->array->equip( $_GET, 'page' ); 
  276.  
  277. // Allow Add-ons to add or remove rule tabs 
  278. $tabs = apply_filters( 
  279. 'ms_controller_protection_tabs',  
  280. $tabs,  
  281. $membership_id 
  282. ); 
  283.  
  284. $page = sanitize_html_class( $_GET['page'], MS_Controller_Plugin::MENU_SLUG . '-memberships' ); 
  285. $rule_titles = MS_Model_Rule::get_rule_type_titles(); 
  286.  
  287. $result = array(); 
  288. foreach ( $tabs as $rule_type => $state ) { 
  289. if ( ! $state ) { continue; } 
  290.  
  291. $url = sprintf( 
  292. '%s?page=%s&tab=%s',  
  293. admin_url( 'admin.php' ),  
  294. $page,  
  295. $rule_type 
  296. ); 
  297.  
  298. // Try to keep the selected Membership and Status filter. 
  299. if ( ! empty( $_REQUEST['membership_id'] ) ) { 
  300. $url = esc_url_raw( 
  301. add_query_arg( 
  302. array( 'membership_id' => $_REQUEST['membership_id'] ),  
  303. $url 
  304. ); 
  305. if ( ! empty( $_REQUEST['status'] ) ) { 
  306. $url = esc_url_raw( 
  307. add_query_arg( 
  308. array( 'status' => $_REQUEST['status'] ),  
  309. $url 
  310. ); 
  311.  
  312. $result[ $rule_type ] = array( 
  313. 'title' => $rule_titles[ $rule_type ],  
  314. 'url' => $url,  
  315. ); 
  316.  
  317. $Tabs = apply_filters( 
  318. 'ms_controller_protection_get_available_tabs',  
  319. $result,  
  320. $membership_id,  
  321. $this 
  322. ); 
  323.  
  324. return $Tabs; 
  325.  
  326. /** 
  327. * Get the current membership page's active tab. 
  328. * 
  329. * @since 1.0.0 
  330. * 
  331. * @return string The active tab. 
  332. */ 
  333. public function get_active_tab() { 
  334. $tabs = $this->get_available_tabs(); 
  335.  
  336. reset( $tabs ); 
  337. $first_key = key( $tabs ); 
  338.  
  339. // Setup navigation tabs. 
  340. $active_tab = isset( $_REQUEST['tab'] ) ? $_REQUEST['tab'] : ''; 
  341. $active_tab = sanitize_html_class( $active_tab, $first_key ); 
  342.  
  343. if ( ! array_key_exists( $active_tab, $tabs ) ) { 
  344. $active_tab = $first_key; 
  345.  
  346. $this->active_tab = apply_filters( 
  347. 'ms_controller_protection_get_active_tab',  
  348. $active_tab 
  349. ); 
  350.  
  351. return $this->active_tab; 
  352.  
  353. /** 
  354. * Load Membership manager specific styles. 
  355. * 
  356. * @since 1.0.0 
  357. */ 
  358. public function enqueue_styles() { 
  359. lib3()->ui->add( 'jquery-ui' ); 
  360.  
  361. do_action( 'ms_controller_protection_enqueue_styles', $this ); 
  362.  
  363. /** 
  364. * Load Membership manager specific scripts. 
  365. * 
  366. * @since 1.0.0 
  367. */ 
  368. public function enqueue_scripts() { 
  369. /** 
  370. * Get a list of the dripped memberships: 
  371. * We need this info in the javascript. 
  372. */ 
  373. $dripped = array(); 
  374. foreach ( MS_Model_Membership::get_dripped_memberships() as $item ) { 
  375. $dripped[ $item->id ] = $item->name; 
  376.  
  377. $data = array( 
  378. 'ms_init' => array(),  
  379. 'lang' => array( 
  380. 'quickedit_error' => __( 'Error while saving changes.', 'membership2' ),  
  381. ),  
  382. 'dripped' => $dripped,  
  383. ); 
  384.  
  385. $data['ms_init'][] = 'view_protected_content'; 
  386.  
  387. switch ( $this->get_active_tab() ) { 
  388. case 'url': 
  389. $data['valid_rule_msg'] = __( 'Valid', 'membership2' ); 
  390. $data['invalid_rule_msg'] = __( 'Invalid', 'membership2' ); 
  391. $data['empty_msg'] = __( 'Before testing you have to first enter one or more Page URLs above.', 'membership2' ); 
  392. $data['ms_init'][] = 'view_membership_urlgroup'; 
  393. break; 
  394.  
  395. default: 
  396. wp_enqueue_script( 'jquery-ui-datepicker' ); 
  397. wp_enqueue_script( 'jquery-validate' ); 
  398. break; 
  399.  
  400. lib3()->ui->data( 'ms_data', $data ); 
  401. wp_enqueue_script( 'ms-admin' ); 
  402.  
  403. do_action( 'ms_controller_protection_enqueue_scripts', $this ); 
  404.  
.