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

  1. <?php 
  2. /** 
  3. * This file defines the MS_Controller_Adminbar class. 
  4. * 
  5. * @copyright Incsub (http://incsub.com/) 
  6. * 
  7. * @license http://opensource.org/licenses/GPL-2.0 GNU General Public License, version 2 (GPL-2.0) 
  8. * 
  9. * This program is free software; you can redistribute it and/or modify 
  10. * it under the terms of the GNU General Public License, version 2, as 
  11. * published by the Free Software Foundation. 
  12. * 
  13. * This program is distributed in the hope that it will be useful,  
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  16. * GNU General Public License for more details. 
  17. * 
  18. * You should have received a copy of the GNU General Public License 
  19. * along with this program; if not, write to the Free Software 
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,  
  21. * MA 02110-1301 USA 
  22. * 
  23. */ 
  24.  
  25. /** 
  26. * Controller to add functionality to the admin toolbar. 
  27. * 
  28. * Used extensively for simulating memberships and content access. 
  29. * Adds ability for Membership users to test the behaviour for their end-users. 
  30. * 
  31. * @since 1.0.0 
  32. * 
  33. * @package Membership2 
  34. * @subpackage Controller 
  35. */ 
  36. class MS_Controller_Adminbar extends MS_Controller { 
  37.  
  38. /** 
  39. * Details on current simulation mode 
  40. * 
  41. * @since 1.0.0 
  42. * 
  43. * @var MS_Model_Simulate 
  44. */ 
  45. protected $simulate = null; 
  46.  
  47. /** 
  48. * Prepare the Admin Bar simulator. 
  49. * 
  50. * @since 1.0.0 
  51. */ 
  52. public function __construct() { 
  53. parent::__construct(); 
  54.  
  55. $this->run_action( 'set_current_user', 'init_adminbar' ); 
  56.  
  57. /** 
  58. * Returns the URL to start/switch simulation of a specific membership. 
  59. * 
  60. * @since 1.1.0 
  61. * @param int $id Membership-ID 
  62. * @return string URL 
  63. */ 
  64. static public function get_simulation_url( $id ) { 
  65. $link_url = admin_url( 
  66. '?action=ms_simulate&membership_id=' . $id,  
  67. is_ssl() ? 'https' : 'http' 
  68. ); 
  69. $link_url = wp_nonce_url( 
  70. $link_url,  
  71. 'ms_simulate' 
  72. ); 
  73.  
  74. return $link_url; 
  75.  
  76. /** 
  77. * Returns the URL to end simulation. 
  78. * 
  79. * @since 1.1.0 
  80. * @return string URL 
  81. */ 
  82. static public function get_simulation_exit_url() { 
  83. return self::get_simulation_url( 0 ); 
  84.  
  85. /** 
  86. * Initialize the Admin-Bar after we have determined the current user. 
  87. * 
  88. * @since 1.1.0 
  89. */ 
  90. public function init_adminbar() { 
  91. $this->simulate = MS_Factory::load( 'MS_Model_Simulate' ); 
  92.  
  93. // Hide WP toolbar in front end to not admin users 
  94. if ( ! $this->is_admin_user() && MS_Plugin::instance()->settings->hide_admin_bar ) { 
  95. add_filter( 'show_admin_bar', '__return_false' ); 
  96. $this->add_action( 'wp_before_admin_bar_render', 'customize_toolbar_front', 999 ); 
  97.  
  98. // Customize WP toolbar for admin users 
  99. if ( $this->is_admin_user() ) { 
  100. $this->add_action( 'wp_before_admin_bar_render', 'customize_toolbar', 999 ); 
  101. $this->add_action( 'add_admin_bar_menus', 'admin_bar_manager' ); 
  102. $this->add_action( 'admin_enqueue_scripts', 'enqueue_scripts' ); 
  103. $this->add_action( 'wp_enqueue_scripts', 'enqueue_scripts' ); 
  104.  
  105. /** 
  106. * Customize the Admin Toolbar. 
  107. * 
  108. * Related Action Hooks: 
  109. * - wp_before_admin_bar_render 
  110. * 
  111. * @since 1.0.0 
  112. */ 
  113. public function customize_toolbar() { 
  114. if ( MS_Model_Member::is_admin_user() 
  115. && MS_Plugin::is_enabled() 
  116. && ! is_network_admin() 
  117. && MS_Model_Simulate::can_simulate() 
  118. ) { 
  119. if ( $this->simulate->is_simulating() ) { 
  120. $this->add_detail_nodes(); 
  121. } else { 
  122. $this->add_test_membership_node(); 
  123. } else if ( ! MS_Plugin::is_enabled() ) { 
  124. $this->add_unprotected_node(); 
  125.  
  126. /** 
  127. * Process GET and POST requests 
  128. * 
  129. * Related Action Hooks: 
  130. * - add_admin_bar_menus 
  131. * 
  132. * @since 1.0.0 
  133. */ 
  134. public function admin_bar_manager() { 
  135. $redirect = false; 
  136.  
  137. lib2()->array->equip_get( 'membership_id' ); 
  138.  
  139. if ( $this->verify_nonce( 'ms_simulate', 'any' ) ) { 
  140. /** 
  141. * Check for memberhship id simulation GET request. 
  142. * - Any valid Membership_id will simulate that membership. 
  143. * - An ID of "0" will exit simulation mode. 
  144. */ 
  145. $new_id = absint( $_REQUEST['membership_id'] ); 
  146.  
  147. if ( $new_id != $this->simulate->membership_id ) { 
  148. // Change the simulated membership. 
  149. $this->simulate->membership_id = $new_id; 
  150.  
  151. $target = wp_get_referer(); 
  152. if ( $this->simulate->is_simulating() 
  153. && false !== strpos( $target, 'wp-admin' ) 
  154. ) { 
  155. $redirect = admin_url(); 
  156.  
  157. if ( ! empty( $_POST['simulate_date'] ) ) { 
  158. // Change the simulation date. 
  159. $this->simulate->date = $_POST['simulate_date']; 
  160.  
  161. $this->simulate->save(); 
  162.  
  163. if ( ! $redirect ) { 
  164. if ( ! empty( $_GET['redirect_to'] ) ) { 
  165. $redirect = $_GET['redirect_to']; 
  166. } else { 
  167. $redirect = wp_get_referer(); 
  168.  
  169. if ( ! $redirect ) { 
  170. $redirect = lib2()->net->current_url(); 
  171.  
  172. if ( $redirect ) { 
  173. wp_safe_redirect( $redirect ); 
  174. exit; 
  175.  
  176. /** 
  177. * Remove all Admin Bar nodes. 
  178. * 
  179. * @since 1.0.0 
  180. * 
  181. * @param string[] $exclude The node IDs to exclude. 
  182. */ 
  183. private function remove_admin_bar_nodes( $exclude = array() ) { 
  184. global $wp_admin_bar; 
  185.  
  186. $nodes = $wp_admin_bar->get_nodes(); 
  187.  
  188. $exclude = apply_filters( 
  189. 'ms_controller_adminbar_remove_admin_bar_nodes_exclude',  
  190. $exclude,  
  191. $nodes 
  192. ); 
  193.  
  194. if ( is_array( $nodes ) ) { 
  195. foreach ( $nodes as $node ) { 
  196. if ( is_array( $exclude ) && ! in_array( $node->id, $exclude ) ) { 
  197. $wp_admin_bar->remove_node( $node->id ); 
  198.  
  199. do_action( 
  200. 'ms_controller_adminbar_remove_admin_bar_nodes',  
  201. $nodes,  
  202. $exclude 
  203. ); 
  204.  
  205. /** 
  206. * Add 'Test Memberships' node. 
  207. * 
  208. * @since 1.0.0 
  209. * 
  210. */ 
  211. private function add_test_membership_node() { 
  212. global $wp_admin_bar; 
  213.  
  214. $base_id = MS_Model_Membership::get_base()->id; 
  215.  
  216. if ( $base_id ) { 
  217. $link_url = self::get_simulation_url( $base_id ); 
  218.  
  219. $wp_admin_bar->add_node( 
  220. apply_filters( 
  221. 'ms_controller_adminbar_add_test_membership_node',  
  222. array( 
  223. 'id' => 'ms-test-memberships',  
  224. 'title' => __( 'Test Memberships', MS_TEXT_DOMAIN ),  
  225. 'href' => $link_url,  
  226. 'meta' => array( 
  227. 'class' => 'ms-test-memberships',  
  228. 'title' => __( 'Membership Simulation Menu', MS_TEXT_DOMAIN ),  
  229. 'tabindex' => '1',  
  230. ),  
  231. ); 
  232.  
  233. /** 
  234. * Add 'Unprotected' node. 
  235. * 
  236. * @since 1.0.0 
  237. * 
  238. */ 
  239. private function add_unprotected_node() { 
  240. global $wp_admin_bar; 
  241.  
  242. if ( MS_Plugin::is_enabled() ) { return; } 
  243. if ( MS_Plugin::is_wizard() ) { return; } 
  244.  
  245. $link_url = MS_Controller_Plugin::get_admin_url( 'settings' ); 
  246.  
  247. $wp_admin_bar->add_node( 
  248. apply_filters( 
  249. 'ms_controller_adminbar_add_unprotected_node',  
  250. array( 
  251. 'id' => 'ms-unprotected',  
  252. 'title' => __( 'Content Protection is disabled', MS_TEXT_DOMAIN ),  
  253. 'href' => $link_url,  
  254. 'meta' => array( 
  255. 'class' => 'ms-unprotected',  
  256. 'title' => __( 'Content of this site is unprotected', MS_TEXT_DOMAIN ),  
  257. 'tabindex' => '1',  
  258. ),  
  259. ); 
  260.  
  261. /** 
  262. * Add membership description nodes. 
  263. * 
  264. * @since 1.0.1 
  265. * 
  266. */ 
  267. private function add_detail_nodes() { 
  268. global $wp_admin_bar; 
  269.  
  270. /** 
  271. * Info menu is currently only available on the front-end. 
  272. * 
  273. * @todo add information also for admin side (Admin-Protection/Capabilities) 
  274. */ 
  275. if ( is_admin() ) { return; } 
  276.  
  277. if ( ! $this->simulate->is_simulating() ) { return; } 
  278.  
  279. $membership = MS_Factory::load( 
  280. 'MS_Model_Membership',  
  281. $this->simulate->membership_id 
  282. ); 
  283.  
  284. $wp_admin_bar->add_menu( 
  285. array( 
  286. 'id' => 'membership-details',  
  287. 'title' => __( 'Protection Details', MS_TEXT_DOMAIN ),  
  288. 'href' => '#',  
  289. ); 
  290.  
  291. $details = lib2()->session->get( 'ms-access' ); 
  292. $parent1 = ''; 
  293. $parent2 = ''; 
  294.  
  295. foreach ( $details as $req_ind => $request ) { 
  296. if ( ! is_array( $request ) ) { continue; } 
  297. $parent1 = 'membership-details-' . $req_ind; 
  298.  
  299. $url = explode( '?', $request['url'] ); 
  300. $url = str_replace( site_url(), '', reset( $url ) ); 
  301.  
  302. $wp_admin_bar->add_node( 
  303. array( 
  304. 'id' => $parent1,  
  305. 'parent' => 'membership-details',  
  306. 'title' => (1 + $req_ind) . ': ' . $url,  
  307. 'href' => $request['url'],  
  308. ); 
  309.  
  310. if ( isset( $request['reason'] ) ) { 
  311. foreach ( $request['reason'] as $key => $item ) { 
  312. if ( is_array( $item ) ) { 
  313. foreach ( $item as $child => $note ) { 
  314. $wp_admin_bar->add_node( 
  315. array( 
  316. 'id' => $parent2 . '-' . $child,  
  317. 'parent' => $parent2,  
  318. 'title' => $note,  
  319. ); 
  320. } else { 
  321. $parent2 = $parent1 . '-' . $key; 
  322. $wp_admin_bar->add_node( 
  323. array( 
  324. 'id' => $parent2,  
  325. 'parent' => $parent1,  
  326. 'title' => $item,  
  327. ); 
  328.  
  329. /** 
  330. * Customize the Admin Toolbar for front end users. 
  331. * 
  332. * Related Action Hooks: 
  333. * - wp_before_admin_bar_render 
  334. * 
  335. * @since 1.0.0 
  336. * 
  337. */ 
  338. public function customize_toolbar_front() { 
  339. if ( ! $this->is_admin_user() ) { 
  340. $this->remove_admin_bar_nodes(); 
  341.  
  342. /** 
  343. * Enqueues necessary scripts and styles. 
  344. * 
  345. * Related Action Hooks: 
  346. * - wp_enqueue_scripts 
  347. * - admin_enqueue_scripts 
  348. * 
  349. * @since 1.0.0 
  350. */ 
  351. public function enqueue_scripts() { 
  352. $data = array( 
  353. 'ms_init' => array( 'controller_adminbar' ),  
  354. 'switching_text' => __( 'Switching...', MS_TEXT_DOMAIN ),  
  355. ); 
  356.  
  357. lib2()->ui->add( 'select' ); 
  358. lib2()->ui->data( 'ms_data', $data ); 
  359.  
  360. wp_enqueue_script( 'ms-admin' ); 
  361. wp_enqueue_script( 'jquery-ui-datepicker' ); 
  362.  
  363. wp_enqueue_style( 'ms-public' ); 
  364.  
.