Init.php
193 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\AdminBarDashboardAccess; |
| 4 | |
| 5 | use ProfilePress\Core\Admin\SettingsPages\AbstractSettingsPage; |
| 6 | use ProfilePress\Custom_Settings_Page_Api; |
| 7 | |
| 8 | class Init |
| 9 | { |
| 10 | public function __construct() |
| 11 | { |
| 12 | add_filter('show_admin_bar', [$this, 'admin_bar_control'], PHP_INT_MAX - 1); |
| 13 | add_action('admin_init', [$this, 'dashboard_access_control'], 1); |
| 14 | |
| 15 | add_action('admin_init', [$this, 'save_options']); |
| 16 | add_filter('ppress_settings_page_submenus_tabs', [$this, 'menu_tab']); |
| 17 | |
| 18 | add_action('ppress_admin_settings_submenu_page_general_admin-bar-dashboard', [$this, 'settings_page_callback']); |
| 19 | add_filter('ppress_general_settings_admin_page_title', [$this, 'change_page_title']); |
| 20 | } |
| 21 | |
| 22 | public function db_options() |
| 23 | { |
| 24 | return get_option('ppress_abdc_options', []); |
| 25 | } |
| 26 | |
| 27 | public function menu_tab($tabs) |
| 28 | { |
| 29 | $tabs[50] = [ |
| 30 | 'parent' => 'general', |
| 31 | 'id' => 'admin-bar-dashboard', |
| 32 | 'label' => esc_html__('Admin Bar & Dashboard', 'wp-user-avatar') |
| 33 | ]; |
| 34 | |
| 35 | return $tabs; |
| 36 | } |
| 37 | |
| 38 | public function change_page_title($title) |
| 39 | { |
| 40 | if (isset($_GET['section']) && $_GET['section'] == 'admin-bar-dashboard') { |
| 41 | $title = esc_html__('Admin Bar & Dashboard Access', 'wp-user-avatar'); |
| 42 | } |
| 43 | |
| 44 | return $title; |
| 45 | } |
| 46 | |
| 47 | public function settings_page_callback() |
| 48 | { |
| 49 | add_filter('wp_cspa_main_content_area', function () { |
| 50 | ob_start(); |
| 51 | require dirname(__FILE__) . '/include.settings-page.php'; |
| 52 | |
| 53 | return ob_get_clean(); |
| 54 | }); |
| 55 | |
| 56 | $instance = Custom_Settings_Page_Api::instance(); |
| 57 | $instance->option_name('ppress_abdc_options'); |
| 58 | $instance->page_header(esc_html__('Admin Bar & Dashboard Access', 'wp-user-avatar')); |
| 59 | AbstractSettingsPage::register_core_settings($instance); |
| 60 | $instance->build(); |
| 61 | } |
| 62 | |
| 63 | public static function save_options() |
| 64 | { |
| 65 | if (isset($_GET['section']) && $_GET['section'] == 'admin-bar-dashboard' && isset($_POST['settings_submit'])) { |
| 66 | |
| 67 | check_admin_referer('ppress_abc_settings_nonce', '_wpnonce'); |
| 68 | |
| 69 | if ( ! current_user_can('manage_options')) return; |
| 70 | |
| 71 | $saved_options = self::sanitize_data($_POST['ppress_abdc_options']); |
| 72 | |
| 73 | update_option('ppress_abdc_options', $saved_options); |
| 74 | |
| 75 | wp_safe_redirect(esc_url_raw(add_query_arg(['settings-updated' => 'true']))); |
| 76 | exit; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Helper function to recursively sanitize POSTed data. |
| 82 | * |
| 83 | * @param $data |
| 84 | * |
| 85 | * @return string|array |
| 86 | */ |
| 87 | public static function sanitize_data($data) |
| 88 | { |
| 89 | if (is_string($data)) return sanitize_text_field($data); |
| 90 | $sanitized_data = array(); |
| 91 | foreach ($data as $key => $value) { |
| 92 | if (is_array($data[$key])) { |
| 93 | $sanitized_data[$key] = self::sanitize_data($data[$key]); |
| 94 | } else { |
| 95 | $sanitized_data[$key] = sanitize_text_field($data[$key]); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return $sanitized_data; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Callback to disable admin bar. |
| 104 | * |
| 105 | * @param $show_admin_bar |
| 106 | * |
| 107 | * @return bool |
| 108 | */ |
| 109 | public function admin_bar_control($show_admin_bar) |
| 110 | { |
| 111 | $is_admin_bar_disabled = ppress_var($this->db_options(), 'disable_admin_bar', '', true); |
| 112 | |
| 113 | // bail if the disable admin bar checkbox isn't checked or we oxygen builder |
| 114 | if ($is_admin_bar_disabled != 'yes' | defined("SHOW_CT_BUILDER")) return $show_admin_bar; |
| 115 | |
| 116 | if (is_user_logged_in()) { |
| 117 | |
| 118 | $disable_admin_bar_roles = ppress_var($this->db_options(), 'disable_admin_bar_roles', [], true); |
| 119 | |
| 120 | $current_user = wp_get_current_user(); |
| 121 | $current_user_roles = $current_user->roles; |
| 122 | |
| 123 | // get current user's admin_bar_front preference |
| 124 | // if value is true, $user_option will has a boolen true value or false otherwise. |
| 125 | $user_option = (get_user_option('show_admin_bar_front', $current_user->ID) == 'true'); |
| 126 | |
| 127 | if (is_super_admin($current_user->ID)) return $user_option; |
| 128 | |
| 129 | // if no role is selected, disable for everyone by return false. |
| 130 | if (empty($disable_admin_bar_roles)) return false; |
| 131 | |
| 132 | $intersect = array_intersect($current_user_roles, $disable_admin_bar_roles); |
| 133 | |
| 134 | return empty($intersect); |
| 135 | } |
| 136 | |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Disable dashboard access. |
| 142 | * |
| 143 | * @return bool|void |
| 144 | */ |
| 145 | public function dashboard_access_control() |
| 146 | { |
| 147 | $current_user = wp_get_current_user(); |
| 148 | $current_user_roles = $current_user->roles; |
| 149 | |
| 150 | $is_dashboard_access_disabled = ppress_var($this->db_options(), 'disable_dashboard_access', '', true); |
| 151 | $disable_dashboard_access_roles = ppress_var($this->db_options(), 'disable_dashboard_access_roles', [], true); |
| 152 | |
| 153 | if (defined('DOING_AJAX') && DOING_AJAX) return; |
| 154 | |
| 155 | if (basename(sanitize_text_field(wp_unslash($_SERVER['SCRIPT_FILENAME']))) == 'admin-post.php') return; |
| 156 | |
| 157 | if ($is_dashboard_access_disabled != 'yes') return; |
| 158 | |
| 159 | if (is_super_admin($current_user->ID)) return; |
| 160 | |
| 161 | // if no role is selected, disable for everyone by return false. |
| 162 | if (empty($disable_dashboard_access_roles)) return $this->disable_dashboard_access(); |
| 163 | |
| 164 | $intersect = array_intersect($current_user_roles, $disable_dashboard_access_roles); |
| 165 | |
| 166 | if ( ! empty($intersect)) $this->disable_dashboard_access(); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Call to disable dashboard access. |
| 171 | */ |
| 172 | public function disable_dashboard_access() |
| 173 | { |
| 174 | $dashboard_redirect_url = ppress_var($this->db_options(), 'dashboard_redirect_url', home_url(), true); |
| 175 | |
| 176 | if (is_admin()) { |
| 177 | nocache_headers(); |
| 178 | wp_safe_redirect(esc_url_raw($dashboard_redirect_url)); |
| 179 | exit; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | public static function get_instance() |
| 184 | { |
| 185 | static $instance = null; |
| 186 | |
| 187 | if (is_null($instance)) { |
| 188 | $instance = new self(); |
| 189 | } |
| 190 | |
| 191 | return $instance; |
| 192 | } |
| 193 | } |