Codice PHP:
<?php
class FrontEndUsers {
public $plugin_file_path = '';
public $plugin_url = '';
public $settings = array();
private $views = array();
private $action_key = 'feu_action';
private $administrator_role_key = 'administrator';
private $user_avatar_enabled = false;
private $initialized = false;
private $debug = false;
static function activate_plugin() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
add_option('front_end_users_url_path', 'profile');
add_option('front_end_users_display_custom_profile_settings', 0);
}
public function init() {
$this->plugin_file_path = preg_replace('/lib\/?$/', '', dirname(__FILE__));
$this->plugin_url = site_url('/').str_replace(ABSPATH, '', $this->plugin_file_path);
/*
Settings for a view:
title (string) Title displayed in the menu and at the top of the page
in_menu (bool) Whether the view is shown in the menu
url (string) The URL path for the view (e.g. 'settings' will cause the view to be rendered at site_url().'/user/settings/'
file (string) Name of the view's file (e.g. 'settings' will render [views_directory]/settings.php). If
this is omitted, the view's key will be used here
action (string) The action value used in the WP rewrite rule; no need to customize this, as it's mainly only relevant
for the 'index' action
items (array) A list of view keys; these views will be listed in a dropdown submenu below the view title
in the menu.
*/
$default_views = array(
'index' => array(
'title' => 'Settings',
'file' => 'settings'
),
'not-logged-in' => array(
'title' => 'Please sign in',
'in_menu' => false
)
);
$options = get_option('front_end_users_options');
$defaults = array(
'url_path' => 'profile',
'roles_with_admin_access' => array($this->administrator_role_key),
'display_custom_profile_settings' => 0
);
if (empty($options)) {
$options = $defaults;
update_option('front_end_users_options', $options);
} else {
$options = array_merge($defaults, $options);
}
$default_settings = array(
'404_include_path' => get_theme_root().'/'.get_template().'/404.php',
'display_custom_profile_settings' => $options['display_custom_profile_settings'],
'roles_with_admin_access' => $options['roles_with_admin_access'],
'url_path' => $options['url_path'],
'views' => $default_views,
'views_directory' => $this->plugin_file_path.'views/'
);
$this->settings = apply_filters('feu_settings', $default_settings);
$this->init_views();
$this->register_css();
$this->set_user_avatar_enabled();
if (!$this->has_admin_access()) {
$this->disable_wp_admin_bar();
}
$this->initialized = true;
}
public function admin_init() {
register_setting('front-end-users', 'front_end_users_options', array($this, 'validate_options'));
add_settings_section('front-end-users-main', 'Settings', array($this, 'settings_text'), 'front-end-users');
add_settings_field('roles_with_admin_access', 'Roles with Admin Access', array($this, 'settings_input_roles_with_admin_access'), 'front-end-users', 'front-end-users-main');
add_settings_field('url_path', 'URL Base', array($this, 'settings_input_url_path'), 'front-end-users', 'front-end-users-main');
add_settings_field('display_custom_profile_settings', 'Display Custom Profile Settings', array($this, 'settings_input_display_custom_profile_settings'), 'front-end-users', 'front-end-users-main');
}
private function init_views() {
$this->views = $this->settings['views'];
// A view with key 'not-logged-in' needs to be available to display to the user if they aren't
// logged in and try to access a feu page
if (empty($this->views['not-logged-in'])) {
$this->views['not-logged-in'] = array(
'title' => 'Please sign in',
'in_menu' => false
);
}
foreach($this->views as $key => $view) {
$view['key'] = $key;
if (empty($view['action'])) {
$view['action'] = $key;
}
if (empty($view['file'])) {
$view['file'] = $key;
}
if (empty($view['url'])) {
// The index view should have an empty url path by default
$view['url'] = $key == 'index' ? '' : $key;
}
$this->views[$key] = $view;
}
}
public function flush_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
private function register_css() {
$css_url = $this->plugin_url.'css/feu.css';
$css_url = apply_filters('feu_css_url', $css_url);
wp_deregister_style('feu');
wp_register_style('feu', $css_url);
}
public function is_user_avatar_enabled() {
return $this->user_avatar_enabled;
}
public function set_user_avatar_enabled($boolean=null) {
if ($boolean === null) {
$active_plugins = get_option('active_plugins', array());
$plugin_path = 'user-avatar/user-avatar.php';
if (in_array($plugin_path, (array)$active_plugins )) {
$this->user_avatar_enabled = true;
}
} else {
$this->user_avatar_enabled = $boolean;
}
}
private function disable_wp_admin_bar() {
remove_action('init','wp_admin_bar_init');
remove_filter('init','wp_admin_bar_init');
remove_action('wp_head','wp_admin_bar_render',1000);
remove_filter('wp_head','wp_admin_bar_render',1000);
remove_action('wp_footer','wp_admin_bar_render',1000);
remove_filter('wp_footer','wp_admin_bar_render',1000);
remove_action('admin_head','wp_admin_bar_render',1000);
remove_filter('admin_head','wp_admin_bar_render',1000);
remove_action('admin_footer','wp_admin_bar_render',1000);
remove_filter('admin_footer','wp_admin_bar_render',1000);
remove_action('wp_before_admin_bar_render','wp_admin_bar_me_separator',10);
remove_action('wp_before_admin_bar_render','wp_admin_bar_my_account_menu',20);
remove_action('wp_before_admin_bar_render','wp_admin_bar_my_blogs_menu',30);
remove_action('wp_before_admin_bar_render','wp_admin_bar_blog_separator',40);
remove_action('wp_before_admin_bar_render','wp_admin_bar_bloginfo_menu',50);
remove_action('wp_before_admin_bar_render','wp_admin_bar_edit_menu',100);
remove_action('wp_head','wp_admin_bar_css');
remove_action('wp_head','wp_admin_bar_dev_css');
remove_action('wp_head','wp_admin_bar_rtl_css');
remove_action('wp_head','wp_admin_bar_rtl_dev_css');
remove_action('admin_head','wp_admin_bar_css');
remove_action('admin_head','wp_admin_bar_dev_css');
remove_action('admin_head','wp_admin_bar_rtl_css');
remove_action('admin_head','wp_admin_bar_rtl_dev_css');
remove_action('wp_footer','wp_admin_bar_js');
remove_action('wp_footer','wp_admin_bar_dev_js');
remove_action('admin_footer','wp_admin_bar_js');
remove_action('admin_footer','wp_admin_bar_dev_js');
remove_action('wp_ajax_adminbar_render','wp_admin_bar_ajax_render');
remove_action('personal_options',' _admin_bar_preferences');
remove_filter('personal_options',' _admin_bar_preferences');
remove_action('personal_options',' _get_admin_bar_preferences');
remove_filter('personal_options',' _get_admin_bar_preferences');
remove_filter('locale','wp_admin_bar_lang');
add_filter('show_admin_bar','__return_false');
wp_deregister_script('admin-bar');
wp_deregister_style('admin-bar');
}
// Routing functions
public function add_rewrite_rules($rules) {
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag('%'.$this->action_key.'%', '(.+)', $this->action_key.'=');
$action_structure = $wp_rewrite->root.$this->settings['url_path'].'/%'.$this->action_key.'%/';
$new_rules = $wp_rewrite->generate_rewrite_rules($action_structure);
$new_rules[$this->settings['url_path'].'$'] = 'index.php?'.$this->action_key.'=index';
return array_merge($new_rules, $rules);
}
public function add_query_vars($vars) {
$vars[] = $this->action_key;
return $vars;
}
public function get_action() {
global $wp_query;
return $wp_query->get($this->action_key);
}
public function template_redirect() {
global $wp_query;
$action = $this->get_action();
if ($action) {
$wp_query->is_home = false;
if (!$this->is_logged_in()) {
$this->render_page('not-logged-in');
} else {
$view_key = null;
foreach($this->views as $view) {
if ($view['action'] == $action) {
$view_key = $view['key'];
}
}
$this->render_page($view_key);
}
}
}
// View-related functions
private function render_page($view_key) {
global $feu_current_view;
$view = $this->get_view($view_key);
if (!empty($view)) {
$feu_current_view = $view;
wp_enqueue_style('feu');
do_action('feu_before_view', $view);
$this->render_view($view);
do_action('feu_after_view', $view);
} else {
$this->render_404();
}
die();
}
?>