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