/** * Add the JS required by the editor to the page header * * @return void */ function enqueue_scripts() { //jQuery JSON plugin wp_register_auto_versioned_script('jquery-json', plugins_url('js/jquery.json.js', $this->plugin_file), array('jquery')); //jQuery sort plugin wp_register_auto_versioned_script('jquery-sort', plugins_url('js/jquery.sort.js', $this->plugin_file), array('jquery')); //qTip2 - jQuery tooltip plugin wp_register_auto_versioned_script('jquery-qtip', plugins_url('js/jquery.qtip.min.js', $this->plugin_file), array('jquery')); //jQuery Form plugin. This is a more recent version than the one included with WP. wp_register_auto_versioned_script('ame-jquery-form', plugins_url('js/jquery.form.js', $this->plugin_file), array('jquery')); //jQuery cookie plugin wp_register_auto_versioned_script('jquery-cookie', plugins_url('js/jquery.cookie.js', $this->plugin_file), array('jquery')); //Editor's scripts wp_register_auto_versioned_script('menu-editor', plugins_url('js/menu-editor.js', $this->plugin_file), array('jquery', 'jquery-ui-sortable', 'jquery-ui-dialog', 'ame-jquery-form', 'jquery-ui-droppable', 'jquery-qtip', 'jquery-sort', 'jquery-json', 'jquery-cookie', 'wp-color-picker')); //Add scripts to our editor page, but not the settings sub-section //that shares the same page slug. Some of the scripts would crash otherwise. if (!$this->is_editor_page()) { return; } wp_enqueue_script('menu-editor'); //We use WordPress media uploader to let the user upload custom menu icons (WP 3.5+). if (function_exists('wp_enqueue_media')) { wp_enqueue_media(); } //Remove the default jQuery Form plugin to prevent conflicts with our custom version. wp_dequeue_script('jquery-form'); //Actors (roles and users) are used in the permissions UI, so we need to pass them along. $actors = array(); $roles = array(); $wp_roles = ameRoleUtils::get_roles(); foreach ($wp_roles->roles as $role_id => $role) { $actors['role:' . $role_id] = $role['name']; $role['capabilities'] = $this->castValuesToBool($role['capabilities']); $roles[$role_id] = $role; } if (is_multisite() && is_super_admin()) { $actors['special:super_admin'] = 'Super Admin'; } //Known users. Right now, this is limited to the current user only. $users = array(); $current_user = wp_get_current_user(); $users[$current_user->get('user_login')] = array('user_login' => $current_user->get('user_login'), 'id' => $current_user->ID, 'roles' => array_values($current_user->roles), 'capabilities' => $this->castValuesToBool($current_user->caps), 'is_super_admin' => is_multisite() && is_super_admin()); $actors['user:'******'user_login')] = sprintf('Current user (%s)', $current_user->get('user_login')); //Note: Users do NOT get added to the actor list because that feature //is not fully implemented. $showExtraIcons = (bool) $this->options['show_extra_icons']; if (isset($_COOKIE['ame-show-extra-icons']) && is_numeric($_COOKIE['ame-show-extra-icons'])) { $showExtraIcons = intval($_COOKIE['ame-show-extra-icons']) > 0; } //The editor will need access to some of the plugin data and WP data. wp_localize_script('menu-editor', 'wsEditorData', array('imagesUrl' => plugins_url('images', $this->plugin_file), 'adminAjaxUrl' => admin_url('admin-ajax.php'), 'hideAdvancedSettings' => (bool) $this->options['hide_advanced_settings'], 'showExtraIcons' => $showExtraIcons, 'submenuIconsEnabled' => $this->options['submenu_icons_enabled'], 'hideAdvancedSettingsNonce' => wp_create_nonce('ws_ame_save_screen_options'), 'dashiconsAvailable' => wp_style_is('dashicons', 'registered'), 'captionShowAdvanced' => 'Show advanced options', 'captionHideAdvanced' => 'Hide advanced options', 'wsMenuEditorPro' => false, 'menuFormatName' => ameMenu::format_name, 'menuFormatVersion' => ameMenu::format_version, 'blankMenuItem' => ameMenuItem::blank_menu(), 'itemTemplates' => $this->item_templates, 'customItemTemplate' => array('name' => '< Custom >', 'defaults' => ameMenuItem::custom_item_defaults()), 'unclickableTemplateId' => ameMenuItem::unclickableTemplateId, 'unclickableTemplateClass' => ameMenuItem::unclickableTemplateClass, 'actors' => $actors, 'roles' => $roles, 'users' => $users, 'currentUserLogin' => $current_user->get('user_login'), 'selectedActor' => isset($this->get['selected_actor']) ? strval($this->get['selected_actor']) : null, 'showHints' => $this->get_hint_visibility(), 'dashboardHidingConfirmationEnabled' => $this->options['dashboard_hiding_confirmation_enabled'], 'disableDashboardConfirmationNonce' => wp_create_nonce('ws_ame_disable_dashboard_hiding_confirmation'), 'isDemoMode' => defined('IS_DEMO_MODE'), 'isMasterMode' => defined('IS_MASTER_MODE'))); }
/** * Get user roles by parsing their capabilities. * * This method is reliable because it determines user roles the same way that WordPress does. However, it's also * relatively "slow" (~ 25 microseconds on my dev. system). Don't call it directly. Use get_user_roles() instead - * it caches results. * * @see WP_User::get_role_caps * * @param WP_User $user * @return array */ private function extract_user_roles($user) { if (empty($user->caps) || !is_array($user->caps)) { return !empty($user->roles) ? $user->roles : array(); } $wp_roles = ameRoleUtils::get_roles(); return array_filter(array_keys($user->caps), array($wp_roles, 'is_role')); }
public function getScriptData() { //Pass the list of installed plugins and their state (active/inactive) to UI JavaScript. $installedPlugins = get_plugins(); $activePlugins = array_map('plugin_basename', wp_get_active_and_valid_plugins()); $activeNetworkPlugins = array(); if (function_exists('wp_get_active_network_plugins')) { //This function is only available on Multisite. $activeNetworkPlugins = array_map('plugin_basename', wp_get_active_network_plugins()); } $plugins = array(); foreach ($installedPlugins as $pluginFile => $header) { $isActiveForNetwork = in_array($pluginFile, $activeNetworkPlugins); $isActive = in_array($pluginFile, $activePlugins); $plugins[] = array('fileName' => $pluginFile, 'name' => $header['Name'], 'description' => isset($header['Description']) ? $header['Description'] : '', 'isActive' => $isActive || $isActiveForNetwork); } //Flag roles that can manage plugins. $canManagePlugins = array(); $wpRoles = ameRoleUtils::get_roles(); foreach ($wpRoles->role_objects as $id => $role) { $canManagePlugins[$id] = $this->canManagePlugins($id, $role); } return array('settings' => $this->getSettings(), 'installedPlugins' => $plugins, 'canManagePlugins' => $canManagePlugins, 'isMultisite' => is_multisite(), 'isProVersion' => $this->menuEditor->is_pro_version(), 'dismissNoticeNonce' => wp_create_nonce('ws_ame_dismiss_pv_usage_notice'), 'adminAjaxUrl' => admin_url('admin-ajax.php')); }
/** * Add the JS required by the editor to the page header * * @return void */ function enqueue_scripts() { //jQuery JSON plugin wp_register_auto_versioned_script('jquery-json', plugins_url('js/jquery.json-1.3.js', $this->plugin_file), array('jquery')); //jQuery sort plugin wp_register_auto_versioned_script('jquery-sort', plugins_url('js/jquery.sort.js', $this->plugin_file), array('jquery')); //qTip2 - jQuery tooltip plugin wp_register_auto_versioned_script('jquery-qtip', plugins_url('js/jquery.qtip.min.js', $this->plugin_file), array('jquery')); //Editor's scripts wp_register_auto_versioned_script('menu-editor', plugins_url('js/menu-editor.js', $this->plugin_file), array('jquery', 'jquery-ui-sortable', 'jquery-ui-dialog', 'jquery-form', 'jquery-ui-droppable', 'jquery-qtip', 'jquery-sort', 'jquery-json')); wp_enqueue_script('menu-editor'); //Actors (roles and users) are used in the permissions UI, so we need to pass them along. $actors = array(); $roles = array(); $wp_roles = ameRoleUtils::get_roles(); foreach ($wp_roles->roles as $role_id => $role) { $actors['role:' . $role_id] = $role['name']; $role['capabilities'] = $this->castValuesToBool($role['capabilities']); $roles[$role_id] = $role; } if (is_multisite() && is_super_admin()) { $actors['special:super_admin'] = 'Super Admin'; } //Known users. Right now, this is limited to the current user only. $users = array(); $current_user = wp_get_current_user(); $users[$current_user->user_login] = array('user_login' => $current_user->user_login, 'id' => $current_user->ID, 'roles' => array_values($current_user->roles), 'capabilities' => $this->castValuesToBool($current_user->caps), 'is_super_admin' => is_multisite() && is_super_admin()); $actors['user:'******'Current user (%s)', $current_user->user_login); //Note: Users do NOT get added to the actor list because that feature //is not fully implemented. //The editor will need access to some of the plugin data and WP data. wp_localize_script('menu-editor', 'wsEditorData', array('imagesUrl' => plugins_url('images', $this->plugin_file), 'adminAjaxUrl' => admin_url('admin-ajax.php'), 'hideAdvancedSettings' => (bool) $this->options['hide_advanced_settings'], 'hideAdvancedSettingsNonce' => wp_create_nonce('ws_ame_save_screen_options'), 'captionShowAdvanced' => 'Show advanced options', 'captionHideAdvanced' => 'Hide advanced options', 'wsMenuEditorPro' => false, 'menuFormatName' => ameMenu::format_name, 'menuFormatVersion' => ameMenu::format_version, 'blankMenuItem' => ameMenuItem::blank_menu(), 'itemTemplates' => $this->item_templates, 'customItemTemplate' => array('name' => '< Custom >', 'defaults' => ameMenuItem::custom_item_defaults()), 'actors' => $actors, 'roles' => $roles, 'users' => $users, 'currentUserLogin' => $current_user->user_login, 'showHints' => $this->get_hint_visibility())); }
/** * Add the JS required by the editor to the page header * * @return void */ function enqueue_scripts() { //jQuery JSON plugin wp_register_script('jquery-json', $this->plugin_dir_url . '/js/jquery.json-1.3.js', array('jquery'), '1.3'); //jQuery sort plugin wp_register_script('jquery-sort', $this->plugin_dir_url . '/js/jquery.sort.js', array('jquery')); //qTip2 - jQuery tooltip plugin wp_register_script('jquery-qtip', $this->plugin_dir_url . '/js/jquery.qtip.min.js', array('jquery'), '20120513', true); //Editor's scripts wp_enqueue_script('menu-editor', $this->plugin_dir_url . '/js/menu-editor.js', array('jquery', 'jquery-ui-sortable', 'jquery-ui-dialog', 'jquery-form', 'jquery-ui-droppable', 'jquery-qtip', 'jquery-sort', 'jquery-json'), '20120609'); //The editor will need access to some of the plugin data and WP data. $wp_roles = ameRoleUtils::get_roles(); wp_localize_script('menu-editor', 'wsEditorData', array('imagesUrl' => $this->plugin_dir_url . '/images', 'adminAjaxUrl' => admin_url('admin-ajax.php'), 'hideAdvancedSettings' => (bool) $this->options['hide_advanced_settings'], 'hideAdvancedSettingsNonce' => wp_create_nonce('ws_ame_save_screen_options'), 'captionShowAdvanced' => 'Show advanced options', 'captionHideAdvanced' => 'Hide advanced options', 'wsMenuEditorPro' => false, 'menuFormatName' => ameMenu::format_name, 'menuFormatVersion' => ameMenu::format_version, 'blankMenuItem' => ameMenuItem::blank_menu(), 'itemTemplates' => $this->item_templates, 'customItemTemplate' => array('name' => '< Custom >', 'defaults' => ameMenuItem::custom_item_defaults()), 'roles' => $wp_roles->roles, 'showHints' => $this->get_hint_visibility())); }
/** * Check if a role has a specific capability, ignoring virtual capabilities. * * @param string $role_id * @param string $capability * @return bool */ private function role_has_real_cap($role_id, $capability) { $roles = ameRoleUtils::get_roles(); $role = $roles->get_role($role_id); /** @var WP_Role $role */ $this->disable_virtual_caps = true; $has_cap = $role && ($role->has_cap($capability) || $capability == $role->name); $this->disable_virtual_caps = false; return $has_cap; }