PluginProbe
Groups – Memberships and Access Control / trunk
Groups – Memberships and Access Control vtrunk
4.6.0 4.5.0 4.4.0 4.3.0 trunk 1.0.0-beta-1 1.0.0-beta-2 1.0.0-beta-3 1.0.0-beta-3b 1.0.0-beta-3c 1.0.0-beta-3d 1.1.4 1.1.5 1.10.0 1.10.1 1.10.2 1.10.3 1.11.0 1.11.1 1.11.2 1.11.3 1.12.0 1.13.0 1.13.1 1.2.0 All 129 releases
groups / lib / admin / class-groups-admin.php

class-groups-admin.php in Groups – Memberships and Access Control trunk, at lib/admin/class-groups-admin.php

343 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * class-groups-admin.php
4 *
5 * Copyright (c) 2011 "kento" Karim Rahimpur www.itthinx.com
6 *
7 * This code is released under the GNU General Public License.
8 * See COPYRIGHT.txt and LICENSE.txt.
9 *
10 * This code is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * This header and all notices must be kept intact.
16 *
17 * @author Karim Rahimpur
18 * @package groups
19 * @since groups 1.0.0
20 */
21
22 if ( !defined( 'ABSPATH' ) ) {
23 exit;
24 }
25
26 /**
27 * Groups admin sections initialization.
28 */
29 class Groups_Admin {
30
31 /**
32 * The position of the Groups menu.
33 *
34 * @var int
35 */
36 const MENU_POSITION = 38;
37
38 /**
39 * Holds admin messages.
40 *
41 * @var string
42 */
43 private static $messages = array();
44
45 /**
46 * Sets up action hooks.
47 */
48 public static function init() {
49 add_action( 'admin_init', array( __CLASS__, 'admin_init' ) );
50 add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) );
51 add_action( 'admin_head', array( __CLASS__, 'admin_head' ) );
52 add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) );
53 add_action( 'network_admin_menu', array( __CLASS__, 'network_admin_menu' ) );
54 add_filter( 'plugin_action_links_'. plugin_basename( GROUPS_FILE ), array( __CLASS__, 'plugin_action_links' ) );
55 add_action( 'after_plugin_row_' . plugin_basename( GROUPS_FILE ), array( __CLASS__, 'after_plugin_row' ), 10, 3 );
56 }
57
58 /**
59 * Hooks into admin_init.
60 *
61 * @see Groups_Admin::admin_menu()
62 * @see Groups_Admin::admin_print_styles()
63 * @link http://codex.wordpress.org/Function_Reference/wp_enqueue_script#Load_scripts_only_on_plugin_pages
64 */
65 public static function admin_init() {
66 global $groups_version;
67 wp_register_style( 'groups_admin', GROUPS_PLUGIN_URL . 'css/groups_admin.css', array(), $groups_version );
68 wp_register_style( 'groups_admin_post', GROUPS_PLUGIN_URL . 'css/groups_admin_post.css', array(), $groups_version );
69 wp_register_style( 'groups_admin_user', GROUPS_PLUGIN_URL . 'css/groups_admin_user.css', array(), $groups_version );
70 require_once GROUPS_VIEWS_LIB . '/class-groups-uie.php';
71 add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) );
72 }
73
74 /**
75 * Loads styles for the Groups admin section.
76 *
77 * @see Groups_Admin::admin_menu()
78 */
79 public static function admin_print_styles() {
80 if ( !wp_style_is( 'groups_admin' ) ) {
81 wp_enqueue_style( 'groups_admin' );
82 }
83 }
84
85 /**
86 * Enqueue admin scripts and styles.
87 *
88 * @since 4.4.0
89 *
90 * @param string $hook_suffix the current admin page
91 */
92 public static function admin_enqueue_scripts( $hook_suffix ) {
93 // Note that we keep using self::admin_print_styles() and self::admin_print_scripts() via actions registered in self::admin_menu().
94 // So all extension-added children have these loaded automatically, rely on match.
95 if ( strpos( $hook_suffix, '_page_groups-' ) !== false ) {
96 if ( !wp_style_is( 'groups_admin' ) ) {
97 wp_enqueue_style( 'groups_admin' );
98 }
99 Groups_UIE::enqueue( 'select' );
100 }
101 }
102
103 /**
104 * Loads scripts.
105 */
106 public static function admin_print_scripts() {
107 // wp_enqueue_script( 'groups-admin', GROUPS_PLUGIN_URL . 'js/groups-admin.js', array( ), $groups_version );
108 Groups_UIE::enqueue( 'select' );
109 }
110
111 /**
112 * Add a message to the list of messages displayed in the admin sections.
113 * The message is filtered using wp_filter_kses() and wrapped in a div
114 * with class 'updated' for messages of type 'info' and 'error' for
115 * those of type 'error'.
116 *
117 * @param string $message the message
118 * @param string $type type of message, defaults to 'info'
119 *
120 * @uses wp_filter_kses()
121 */
122 public static function add_message( $message, $type = 'info' ) {
123 if ( is_string( $message ) ) {
124 $class = 'updated';
125 switch ( $type ) {
126 case 'error' :
127 $class = 'error';
128 }
129 self::$messages[] = '<div class="'.$class.'">' . balanceTags( stripslashes( wp_filter_kses( $message ) ), true ) . '</div>';
130 }
131 }
132
133 /**
134 * Returns the list of messages as a string.
135 * An empty string is returned if there are no messages.
136 *
137 * @return string
138 */
139 public static function render_messages() {
140 $output = '';
141 if ( !empty( self::$messages ) ) {
142 $output .= '<div class="groups messages">';
143 $output .= implode( '', self::$messages ); // messages are already escaped when added using self::add_message()
144 $output .= '</div>';
145 }
146 return $output;
147 }
148
149 /**
150 * Prints admin notices.
151 */
152 public static function admin_notices() {
153 global $groups_admin_messages;
154 if ( !empty( $groups_admin_messages ) ) {
155 foreach ( $groups_admin_messages as $msg ) {
156 echo $msg; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
157 }
158 }
159 }
160
161 /**
162 * Use a context-sensitive menu item title.
163 */
164 public static function admin_head() {
165 global $submenu;
166 if ( isset( $submenu['groups-admin'] ) ) {
167 $submenu['groups-admin'][0][0] = _x( 'Groups', 'menu item title', 'groups' );
168 }
169 }
170
171 /**
172 * Admin menu.
173 */
174 public static function admin_menu() {
175
176 include_once GROUPS_ADMIN_LIB . '/groups-admin-groups.php';
177 include_once GROUPS_ADMIN_LIB . '/groups-admin-capabilities.php';
178 include_once GROUPS_ADMIN_LIB . '/groups-admin-options.php';
179 include_once GROUPS_ADMIN_LIB . '/groups-admin-add-ons.php';
180
181 $pages = array();
182
183 // main
184 $page = add_menu_page(
185 _x( 'Groups', 'page-title', 'groups' ),
186 'Groups', // don't translate, reasons: a) Groups menu title consistency and b) http://core.trac.wordpress.org/ticket/18857 translation affects $screen->id
187 GROUPS_ADMINISTER_GROUPS,
188 'groups-admin',
189 apply_filters( 'groups_add_menu_page_function', 'groups_admin_groups' ),
190 'none', // @since 2.16.0 CSS icon from SVG
191 self::MENU_POSITION
192 );
193 $pages[] = $page;
194
195 if ( groups_verify_post_nonce( GROUPS_ADMIN_OPTIONS_NONCE, 'admin' ) ) {
196 $show_tree_view = !empty( groups_sanitize_post( GROUPS_SHOW_TREE_VIEW ) );
197 } else {
198 $show_tree_view = Groups_Options::get_option( GROUPS_SHOW_TREE_VIEW, GROUPS_SHOW_TREE_VIEW_DEFAULT );
199 }
200
201 if ( $show_tree_view ) {
202 include_once GROUPS_ADMIN_LIB . '/groups-admin-tree-view.php';
203 $page = add_submenu_page(
204 'groups-admin',
205 __( 'Tree', 'groups' ),
206 __( 'Tree', 'groups' ),
207 GROUPS_ACCESS_GROUPS,
208 'groups-admin-tree-view',
209 apply_filters( 'groups_add_submenu_page_function', 'groups_admin_tree_view' )
210 );
211 $pages[] = $page;
212 }
213
214 // capabilities
215 $page = add_submenu_page(
216 'groups-admin',
217 __( 'Groups Capabilities', 'groups' ),
218 __( 'Capabilities', 'groups' ),
219 GROUPS_ADMINISTER_GROUPS,
220 'groups-admin-capabilities',
221 apply_filters( 'groups_add_submenu_page_function', 'groups_admin_capabilities' )
222 );
223 $pages[] = $page;
224
225 // options
226 $page = add_submenu_page(
227 'groups-admin',
228 __( 'Groups options', 'groups' ),
229 __( 'Options', 'groups' ),
230 GROUPS_ADMINISTER_OPTIONS,
231 'groups-admin-options',
232 apply_filters( 'groups_add_submenu_page_function', 'groups_admin_options' )
233 );
234 $pages[] = $page;
235
236 // add-ons
237 $page = add_submenu_page(
238 'groups-admin',
239 __( 'Groups Add-Ons', 'groups' ),
240 __( 'Add-Ons', 'groups' ),
241 GROUPS_ACCESS_GROUPS,
242 'groups-admin-add-ons',
243 apply_filters( 'groups_add_submenu_page_function', 'groups_admin_add_ons' )
244 );
245 $pages[] = $page;
246
247 foreach ( $pages as $page ) {
248 add_action( 'admin_print_styles-' . $page, array( __CLASS__, 'admin_print_styles' ) );
249 add_action( 'admin_print_scripts-' . $page, array( __CLASS__, 'admin_print_scripts' ) );
250 }
251
252 do_action( 'groups_admin_menu', $pages );
253 }
254
255 /**
256 * Network admin menu.
257 */
258 public static function network_admin_menu() {
259
260 include_once GROUPS_ADMIN_LIB . '/groups-admin-options.php';
261
262 $pages = array();
263
264 // main
265 $page = add_menu_page(
266 _x( 'Groups', 'Network menu page title', 'groups' ),
267 'Groups', // don't translate, see note on same in self::admin_menu()
268 GROUPS_ADMINISTER_GROUPS,
269 'groups-network-admin',
270 apply_filters( 'groups_add_menu_page_function', 'groups_network_admin_options' ),
271 'none' // @since 2.16.0 CSS icon from SVG
272 );
273 $pages[] = $page;
274 add_action( 'admin_print_styles-' . $page, array( __CLASS__, 'admin_print_styles' ) );
275 add_action( 'admin_print_scripts-' . $page, array( __CLASS__, 'admin_print_scripts' ) );
276
277 do_action( 'groups_network_admin_menu', $pages );
278 }
279
280 /**
281 * Adds plugin links.
282 *
283 * @param array $links
284 * @param array $links with additional links
285 *
286 * @return array
287 */
288 public static function plugin_action_links( $links ) {
289 if ( Groups_User::current_user_can( GROUPS_ADMINISTER_OPTIONS ) ) {
290 array_unshift(
291 $links,
292 '<a href="' . get_admin_url( null, 'admin.php?page=groups-admin-options' ) . '">' .
293 _x( 'Options', 'Plugin action link', 'groups' ) .
294 '</a>'
295 );
296 }
297 if ( Groups_User::current_user_can( GROUPS_ADMINISTER_GROUPS ) ) {
298 array_unshift(
299 $links,
300 '<a href="' . get_admin_url( null, 'admin.php?page=groups-admin' ) . '">' .
301 _x( 'Groups', 'Plugin action link', 'groups' ) .
302 '</a>'
303 );
304 }
305 return $links;
306 }
307
308 /**
309 * Prints a warning when data is deleted on deactivation.
310 *
311 * @param string $plugin_file
312 * @param array $plugin_data
313 * @param string $status
314 */
315 public static function after_plugin_row( $plugin_file, $plugin_data, $status ) {
316 if ( $plugin_file == plugin_basename( GROUPS_FILE ) ) {
317 $delete_data = Groups_Options::get_option( 'groups_delete_data', false );
318 $delete_network_data = Groups_Options::get_option( 'groups_network_delete_data', false );
319 if (
320 ( is_plugin_active( $plugin_file ) && $delete_data && Groups_User::current_user_can( 'install_plugins' ) ) ||
321 ( is_plugin_active_for_network( $plugin_file ) && $delete_network_data && Groups_User::current_user_can( 'manage_network_plugins' ) )
322 ) {
323 echo '<tr class="active">';
324 echo '<td>&nbsp;</td>';
325 echo '<td colspan="2">';
326 echo '<div style="border: 2px solid #dc3232; padding: 1em">';
327 echo '<p>';
328 echo '<strong>';
329 echo esc_html__( 'Warning!', 'groups' );
330 echo '</strong>';
331 echo '</p>';
332 echo '<p>';
333 echo esc_html__( 'Groups is configured to delete its plugin data on deactivation.', 'groups' );
334 echo '</p>';
335 echo '</div>';
336 echo '</td>';
337 echo '</tr>';
338 }
339 }
340 }
341 }
342 Groups_Admin::init();
343