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