| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPAdminify\Inc\Classes; |
| 4 |
|
| 5 |
use WPAdminify\Inc\Admin\AdminSettings; |
| 6 |
use WPAdminify\Inc\Admin\AdminSettingsModel; |
| 7 |
|
| 8 |
use WPAdminify\Inc\Utils; |
| 9 |
// no direct access allowed |
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
/** |
| 14 |
* WPAdminify |
| 15 |
* Dashboard Widgets |
| 16 |
* |
| 17 |
* @author Jewel Theme <support@jeweltheme.com> |
| 18 |
*/ |
| 19 |
class Remove_DashboardWidgets extends AdminSettingsModel |
| 20 |
{ |
| 21 |
public $widget_list; |
| 22 |
|
| 23 |
public function __construct() |
| 24 |
{ |
| 25 |
$this->widget_list = (array) AdminSettings::get_instance()->get('remove_widgets')['dashboard_widgets_list']; |
| 26 |
// $this->restrict_for = (array) AdminSettings::get_instance()->get('dashboard_widgets_user_roles'); |
| 27 |
|
| 28 |
// Dashboard widgets by WP Adminify |
| 29 |
add_action('admin_init', [$this, 'jltwp_adminify_add_hooks']); |
| 30 |
|
| 31 |
// add_action('admin_enqueue_scripts', [$this, 'dashboard_styles'], 100); |
| 32 |
} |
| 33 |
|
| 34 |
public function jltwp_adminify_add_hooks() |
| 35 |
{ |
| 36 |
add_action('wp_dashboard_setup', [$this, 'disable_dashboard_widgets'], 100); |
| 37 |
// add_action('wp_network_dashboard_setup', [$this, 'disable_dashboard_widgets'], 100); |
| 38 |
} |
| 39 |
|
| 40 |
public static function get_dashboard_widgets_checkbox() |
| 41 |
{ |
| 42 |
$widgets = (new self())->get_default_dashboard_widgets(); |
| 43 |
$flat_widgets = []; |
| 44 |
foreach ($widgets as $context => $priority) { |
| 45 |
foreach ($priority as $data) { |
| 46 |
foreach ($data as $id => $widget) { |
| 47 |
if (!$widget) { |
| 48 |
continue; |
| 49 |
} |
| 50 |
$key = $id . '|' . $context; |
| 51 |
$widget['title'] = (isset($widget['title']) ? $widget['title'] : ''); |
| 52 |
$flat_widgets[$key] = wp_strip_all_tags($widget['title']); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
$widgets = wp_list_sort( $flat_widgets, [ 'title_stripped' => 'ASC' ], null, true ); |
| 57 |
return $widgets; |
| 58 |
} |
| 59 |
|
| 60 |
public static function render_dashboard_checkboxes() |
| 61 |
{ |
| 62 |
(new self())->get_default_dashboard_widgets(); |
| 63 |
$dom = new \DOMDocument(); |
| 64 |
$widgets = get_option('dashboard_widgets', []); |
| 65 |
$new_list = []; |
| 66 |
foreach ($widgets as $key => $value) { |
| 67 |
if ($value != $value) { |
| 68 |
libxml_use_internal_errors(true); |
| 69 |
$dom->loadHTML($value); |
| 70 |
$value = $dom->getElementsByTagName('span')->item(0)->nodeValue; |
| 71 |
} |
| 72 |
$new_list[$key] = $value; |
| 73 |
} |
| 74 |
return $new_list; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get the default dashboard widgets. |
| 79 |
* |
| 80 |
* @return array Sidebar widgets. |
| 81 |
*/ |
| 82 |
public function get_default_dashboard_widgets() |
| 83 |
{ |
| 84 |
global $wp_meta_boxes; |
| 85 |
$screen = (is_network_admin() ? 'dashboard-network' : 'dashboard'); |
| 86 |
$current_screen = get_current_screen(); |
| 87 |
if (!isset($wp_meta_boxes[$screen]) || !is_array($wp_meta_boxes[$screen])) { |
| 88 |
require_once ABSPATH . '/wp-admin/includes/dashboard.php'; |
| 89 |
set_current_screen($screen); |
| 90 |
wp_dashboard_setup(); |
| 91 |
if (is_callable(['Antispam_Bee', 'add_dashboard_chart'])) { |
| 92 |
\Antispam_Bee::add_dashboard_chart(); |
| 93 |
} |
| 94 |
} |
| 95 |
if (isset($wp_meta_boxes[$screen][0])) { |
| 96 |
unset($wp_meta_boxes[$screen][0]); |
| 97 |
} |
| 98 |
$widgets = []; |
| 99 |
if (isset($wp_meta_boxes[$screen])) { |
| 100 |
$widgets = $wp_meta_boxes[$screen]; |
| 101 |
} |
| 102 |
set_current_screen($current_screen); |
| 103 |
/** |
| 104 |
* Filters the available dashboard widgets. |
| 105 |
* |
| 106 |
* @param array $widgets The globally available dashboard widgets. |
| 107 |
*/ |
| 108 |
return apply_filters('wp_adminify_dashboard_widgets', $widgets); |
| 109 |
} |
| 110 |
|
| 111 |
public function disable_dashboard_widgets() |
| 112 |
{ |
| 113 |
$widgets = $this->widget_list; |
| 114 |
$_widgets = self::get_dashboard_widgets_checkbox(); |
| 115 |
update_option('dashboard_widgets', $_widgets); |
| 116 |
if (!$widgets) { |
| 117 |
return; |
| 118 |
} |
| 119 |
foreach ($widgets as $meta_box) { |
| 120 |
$widget_data = explode('|', $meta_box); |
| 121 |
$widget_id = $widget_data[0]; |
| 122 |
$widget_pos = ''; |
| 123 |
if (!empty($widget_data[1])) { |
| 124 |
$widget_pos = $widget_data[1]; |
| 125 |
} |
| 126 |
if ('dashboard_welcome_panel' === $widget_id) { |
| 127 |
remove_action('welcome_panel', 'wp_welcome_panel'); |
| 128 |
continue; |
| 129 |
} |
| 130 |
if ('try_gutenberg_panel' === $widget_id) { |
| 131 |
remove_action('try_gutenberg_panel', 'wp_try_gutenberg_panel'); |
| 132 |
continue; |
| 133 |
} |
| 134 |
if ('dashboard_browser_nag' === $widget_id || 'dashboard_php_nag' === $widget_id) { |
| 135 |
continue; |
| 136 |
} |
| 137 |
/** |
| 138 |
* Docs: https://developer.wordpress.org/reference/functions/remove_meta_box/ |
| 139 |
* Example: remove_meta_box('dashboard_right_now', 'dashboard', 'normal'); // Right Now |
| 140 |
*/ |
| 141 |
remove_meta_box($widget_id, get_current_screen()->base, $widget_pos); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|