| 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.381'; |
| 37 |
|
| 38 |
/** |
| 39 |
* Holds admin messages. |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
private static $messages = array(); |
| 43 |
|
| 44 |
/** |
| 45 |
* Sets up action hooks. |
| 46 |
*/ |
| 47 |
public static function init() { |
| 48 |
add_action( 'admin_init', array( __CLASS__, 'admin_init' ) ); |
| 49 |
add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) ); |
| 50 |
add_action( 'admin_head', array( __CLASS__, 'admin_head' ) ); |
| 51 |
add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) ); |
| 52 |
add_action( 'network_admin_menu', array( __CLASS__, 'network_admin_menu' ) ); |
| 53 |
add_filter( 'plugin_action_links_'. plugin_basename( GROUPS_FILE ), array( __CLASS__, 'plugin_action_links' ) ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Hooks into admin_init. |
| 58 |
* @see Groups_Admin::admin_menu() |
| 59 |
* @see Groups_Admin::admin_print_styles() |
| 60 |
* @link http://codex.wordpress.org/Function_Reference/wp_enqueue_script#Load_scripts_only_on_plugin_pages |
| 61 |
*/ |
| 62 |
public static function admin_init() { |
| 63 |
global $groups_version; |
| 64 |
wp_register_style( 'groups_admin', GROUPS_PLUGIN_URL . 'css/groups_admin.css', array(), $groups_version ); |
| 65 |
require_once GROUPS_VIEWS_LIB . '/class-groups-uie.php'; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Loads styles for the Groups admin section. |
| 70 |
* |
| 71 |
* @see Groups_Admin::admin_menu() |
| 72 |
*/ |
| 73 |
public static function admin_print_styles() { |
| 74 |
wp_enqueue_style( 'groups_admin' ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Loads scripts. |
| 79 |
*/ |
| 80 |
public static function admin_print_scripts() { |
| 81 |
global $groups_version; |
| 82 |
// this one's currently empty |
| 83 |
//wp_enqueue_script( 'groups_admin', GROUPS_PLUGIN_URL . 'js/groups_admin.js', array( ), $groups_version ); |
| 84 |
Groups_UIE::enqueue( 'select' ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Add a message to the list of messages displayed in the admin sections. |
| 89 |
* The message is filtered using wp_filter_kses() and wrapped in a div |
| 90 |
* with class 'updated' for messages of type 'info' and 'error' for |
| 91 |
* those of type 'error'. |
| 92 |
* |
| 93 |
* @param string $message the message |
| 94 |
* @param string $type type of message, defaults to 'info' |
| 95 |
* @uses wp_filter_kses() |
| 96 |
*/ |
| 97 |
public static function add_message( $message, $type = 'info' ) { |
| 98 |
$class = 'updated'; |
| 99 |
switch( $type ) { |
| 100 |
case 'error' : |
| 101 |
$class = 'error'; |
| 102 |
} |
| 103 |
self::$messages[] = '<div class="'.$class.'">' . balanceTags( stripslashes( wp_filter_kses( $message ) ), true ) . '</div>'; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Returns the list of messages as a string. |
| 108 |
* An empty string is returned if there are no messages. |
| 109 |
* |
| 110 |
* @return string |
| 111 |
*/ |
| 112 |
public static function render_messages() { |
| 113 |
$output = ''; |
| 114 |
if ( !empty( self::$messages ) ) { |
| 115 |
$output .= '<div class="groups messages">'; |
| 116 |
$output .= implode( '', self::$messages ); |
| 117 |
$output .= '</div>'; |
| 118 |
} |
| 119 |
return $output; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Prints admin notices. |
| 124 |
*/ |
| 125 |
public static function admin_notices() { |
| 126 |
global $groups_admin_messages; |
| 127 |
if ( !empty( $groups_admin_messages ) ) { |
| 128 |
foreach ( $groups_admin_messages as $msg ) { |
| 129 |
echo $msg; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Use a context-sensitive menu item title. |
| 136 |
*/ |
| 137 |
public static function admin_head() { |
| 138 |
global $submenu; |
| 139 |
if ( isset( $submenu['groups-admin'] ) ) { |
| 140 |
$submenu['groups-admin'][0][0] = _x( 'Groups', 'menu item title', GROUPS_PLUGIN_DOMAIN ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Admin menu. |
| 146 |
*/ |
| 147 |
public static function admin_menu() { |
| 148 |
|
| 149 |
include_once( GROUPS_ADMIN_LIB . '/groups-admin-groups.php'); |
| 150 |
include_once( GROUPS_ADMIN_LIB . '/groups-admin-capabilities.php'); |
| 151 |
include_once( GROUPS_ADMIN_LIB . '/groups-admin-options.php'); |
| 152 |
include_once( GROUPS_ADMIN_LIB . '/groups-admin-add-ons.php'); |
| 153 |
|
| 154 |
$pages = array(); |
| 155 |
|
| 156 |
// main |
| 157 |
$page = add_menu_page( |
| 158 |
_x( 'Groups', 'page-title', GROUPS_PLUGIN_DOMAIN ), |
| 159 |
'Groups', // don't translate, reasons: a) Groups menu title consistency and b) http://core.trac.wordpress.org/ticket/18857 translation affects $screen->id |
| 160 |
GROUPS_ADMINISTER_GROUPS, |
| 161 |
'groups-admin', |
| 162 |
apply_filters( 'groups_add_menu_page_function', 'groups_admin_groups' ), |
| 163 |
GROUPS_PLUGIN_URL . '/images/groups.png', |
| 164 |
self::MENU_POSITION |
| 165 |
); |
| 166 |
$pages[] = $page; |
| 167 |
add_action( 'admin_print_styles-' . $page, array( __CLASS__, 'admin_print_styles' ) ); |
| 168 |
add_action( 'admin_print_scripts-' . $page, array( __CLASS__, 'admin_print_scripts' ) ); |
| 169 |
|
| 170 |
if ( isset( $_POST[GROUPS_ADMIN_OPTIONS_NONCE] ) && wp_verify_nonce( $_POST[GROUPS_ADMIN_OPTIONS_NONCE], 'admin' ) ) { |
| 171 |
$show_tree_view = !empty( $_POST[GROUPS_SHOW_TREE_VIEW] ); |
| 172 |
} else { |
| 173 |
$show_tree_view = Groups_Options::get_option( GROUPS_SHOW_TREE_VIEW, GROUPS_SHOW_TREE_VIEW_DEFAULT ); |
| 174 |
} |
| 175 |
|
| 176 |
if ( $show_tree_view ) { |
| 177 |
include_once( GROUPS_ADMIN_LIB . '/groups-admin-tree-view.php'); |
| 178 |
$page = add_submenu_page( |
| 179 |
'groups-admin', |
| 180 |
__( 'Tree', GROUPS_PLUGIN_DOMAIN ), |
| 181 |
__( 'Tree', GROUPS_PLUGIN_DOMAIN ), |
| 182 |
GROUPS_ACCESS_GROUPS, |
| 183 |
'groups-admin-tree-view', |
| 184 |
apply_filters( 'groups_add_submenu_page_function', 'groups_admin_tree_view' ) |
| 185 |
); |
| 186 |
$pages[] = $page; |
| 187 |
add_action( 'admin_print_styles-' . $page, array( __CLASS__, 'admin_print_styles' ) ); |
| 188 |
add_action( 'admin_print_scripts-' . $page, array( __CLASS__, 'admin_print_scripts' ) ); |
| 189 |
} |
| 190 |
|
| 191 |
// capabilities |
| 192 |
$page = add_submenu_page( |
| 193 |
'groups-admin', |
| 194 |
__( 'Groups Capabilities', GROUPS_PLUGIN_DOMAIN ), |
| 195 |
__( 'Capabilities', GROUPS_PLUGIN_DOMAIN ), |
| 196 |
GROUPS_ADMINISTER_GROUPS, |
| 197 |
'groups-admin-capabilities', |
| 198 |
apply_filters( 'groups_add_submenu_page_function', 'groups_admin_capabilities' ) |
| 199 |
); |
| 200 |
$pages[] = $page; |
| 201 |
add_action( 'admin_print_styles-' . $page, array( __CLASS__, 'admin_print_styles' ) ); |
| 202 |
add_action( 'admin_print_scripts-' . $page, array( __CLASS__, 'admin_print_scripts' ) ); |
| 203 |
|
| 204 |
// options |
| 205 |
$page = add_submenu_page( |
| 206 |
'groups-admin', |
| 207 |
__( 'Groups options', GROUPS_PLUGIN_DOMAIN ), |
| 208 |
__( 'Options', GROUPS_PLUGIN_DOMAIN ), |
| 209 |
GROUPS_ADMINISTER_OPTIONS, |
| 210 |
'groups-admin-options', |
| 211 |
apply_filters( 'groups_add_submenu_page_function', 'groups_admin_options' ) |
| 212 |
); |
| 213 |
$pages[] = $page; |
| 214 |
add_action( 'admin_print_styles-' . $page, array( __CLASS__, 'admin_print_styles' ) ); |
| 215 |
add_action( 'admin_print_scripts-' . $page, array( __CLASS__, 'admin_print_scripts' ) ); |
| 216 |
|
| 217 |
// add-ons |
| 218 |
$page = add_submenu_page( |
| 219 |
'groups-admin', |
| 220 |
__( 'Groups Add-Ons', GROUPS_PLUGIN_DOMAIN ), |
| 221 |
__( 'Add-Ons', GROUPS_PLUGIN_DOMAIN ), |
| 222 |
GROUPS_ACCESS_GROUPS, |
| 223 |
'groups-admin-add-ons', |
| 224 |
apply_filters( 'groups_add_submenu_page_function', 'groups_admin_add_ons' ) |
| 225 |
); |
| 226 |
$pages[] = $page; |
| 227 |
add_action( 'admin_print_styles-' . $page, array( __CLASS__, 'admin_print_styles' ) ); |
| 228 |
add_action( 'admin_print_scripts-' . $page, array( __CLASS__, 'admin_print_scripts' ) ); |
| 229 |
|
| 230 |
do_action( 'groups_admin_menu', $pages ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Network admin menu. |
| 235 |
*/ |
| 236 |
public static function network_admin_menu() { |
| 237 |
|
| 238 |
include_once( GROUPS_ADMIN_LIB . '/groups-admin-options.php'); |
| 239 |
|
| 240 |
$pages = array(); |
| 241 |
|
| 242 |
// main |
| 243 |
$page = add_menu_page( |
| 244 |
__( 'Groups', GROUPS_PLUGIN_DOMAIN ), |
| 245 |
__( 'Groups', GROUPS_PLUGIN_DOMAIN ), |
| 246 |
GROUPS_ADMINISTER_GROUPS, |
| 247 |
'groups-network-admin', |
| 248 |
apply_filters( 'groups_add_menu_page_function', 'groups_network_admin_options' ), |
| 249 |
GROUPS_PLUGIN_URL . '/images/groups.png' |
| 250 |
); |
| 251 |
$pages[] = $page; |
| 252 |
add_action( 'admin_print_styles-' . $page, array( __CLASS__, 'admin_print_styles' ) ); |
| 253 |
add_action( 'admin_print_scripts-' . $page, array( __CLASS__, 'admin_print_scripts' ) ); |
| 254 |
|
| 255 |
do_action( 'groups_network_admin_menu', $pages ); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Adds plugin links. |
| 260 |
* |
| 261 |
* @param array $links |
| 262 |
* @param array $links with additional links |
| 263 |
*/ |
| 264 |
public static function plugin_action_links( $links ) { |
| 265 |
if ( current_user_can( GROUPS_ADMINISTER_OPTIONS ) ) { |
| 266 |
array_unshift( |
| 267 |
$links, |
| 268 |
'<a href="' . get_admin_url( null, 'admin.php?page=groups-admin-options' ) . '">' . __( 'Options', GROUPS_PLUGIN_DOMAIN ) . '</a>' |
| 269 |
); |
| 270 |
} |
| 271 |
if ( current_user_can( GROUPS_ADMINISTER_GROUPS ) ) { |
| 272 |
array_unshift( |
| 273 |
$links, |
| 274 |
'<a href="' . get_admin_url( null, 'admin.php?page=groups-admin' ) . '">' . __( 'Groups', GROUPS_PLUGIN_DOMAIN ) . '</a>' |
| 275 |
); |
| 276 |
} |
| 277 |
return $links; |
| 278 |
} |
| 279 |
} |
| 280 |
Groups_Admin::init(); |
| 281 |
|