| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Adminimize |
| 4 |
* @subpackage Dashboard Setup |
| 5 |
* @author Frank Bültge |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! function_exists( 'add_action' ) ) { |
| 9 |
echo "Hi there! I'm just a part of plugin, not much I can do when called directly."; |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! is_admin() ) { |
| 14 |
return; |
| 15 |
} |
| 16 |
|
| 17 |
// If is AJAX Call. |
| 18 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
add_action( 'wp_dashboard_setup', '_mw_adminimize_update_dashboard_widgets', 9998 ); |
| 23 |
/** |
| 24 |
* Write dashboard widgets in settings. |
| 25 |
* |
| 26 |
* @return bool |
| 27 |
*/ |
| 28 |
function _mw_adminimize_update_dashboard_widgets() { |
| 29 |
|
| 30 |
// Only manage options users have the chance to update the settings. |
| 31 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
$adminimizeoptions = _mw_adminimize_get_option_value(); |
| 36 |
$adminimizeoptions['mw_adminimize_dashboard_widgets'] = _mw_adminimize_get_dashboard_widgets(); |
| 37 |
|
| 38 |
return _mw_adminimize_update_option( $adminimizeoptions ); |
| 39 |
} |
| 40 |
|
| 41 |
// Return registered widgets; only on page index/dashboard :( |
| 42 |
add_action( 'wp_dashboard_setup', '_mw_adminimize_dashboard_setup', PHP_INT_MAX ); |
| 43 |
/** |
| 44 |
* Set dashboard widget options. |
| 45 |
*/ |
| 46 |
function _mw_adminimize_dashboard_setup() { |
| 47 |
|
| 48 |
// exclude super admin |
| 49 |
if ( _mw_adminimize_exclude_super_admin() ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
// Get all widgets. |
| 54 |
$widgets = _mw_adminimize_get_dashboard_widgets(); |
| 55 |
$user_roles = _mw_adminimize_get_all_user_roles(); |
| 56 |
$disabled_dashboard_option = array(); |
| 57 |
$disabled_dashboard_option_ = array(); |
| 58 |
$user = wp_get_current_user(); |
| 59 |
|
| 60 |
// Get settings for each role. |
| 61 |
foreach ( $user_roles as $role ) { |
| 62 |
$disabled_dashboard_option_[ $role ] = (array) _mw_adminimize_get_option_value( |
| 63 |
'mw_adminimize_disabled_dashboard_option_' . $role . '_items' |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
foreach ( $user_roles as $role ) { |
| 68 |
if ( in_array( $role, $user->roles, false ) && _mw_adminimize_current_user_has_role( $role ) |
| 69 |
) { |
| 70 |
// Create array about all items with all affected roles, important for multiple roles. |
| 71 |
foreach ( (array) $disabled_dashboard_option_[ $role ] as $dashboard_item ) { |
| 72 |
$disabled_dashboard_option[] = $dashboard_item; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// Support Multiple Roles for users, if option is active. |
| 78 |
if ( _mw_adminimize_get_option_value( 'mw_adminimize_multiple_roles' ) && 1 < count( $user->roles ) ) { |
| 79 |
$disabled_dashboard_option = _mw_adminimize_get_duplicate( $disabled_dashboard_option ); |
| 80 |
} |
| 81 |
|
| 82 |
// Remove the dashboards widgets for the current active role. |
| 83 |
foreach ( $disabled_dashboard_option as $widget ) { |
| 84 |
if ( isset( $widgets[ $widget ]['context'] ) ) { |
| 85 |
remove_meta_box( $widget, 'dashboard', $widgets[ $widget ]['context'] ); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
add_action( 'admin_head-index.php', '_mw_adminimize_remove_custom_panels', 99 ); |
| 92 |
/** |
| 93 |
* Add custom options to the head head to hide it via css. |
| 94 |
* |
| 95 |
* @since 2017-01-05 |
| 96 |
*/ |
| 97 |
function _mw_adminimize_remove_custom_panels() { |
| 98 |
|
| 99 |
// exclude super admin |
| 100 |
if ( _mw_adminimize_exclude_super_admin() ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
$options = _mw_adminimize_get_option_value( '_mw_adminimize_own_dashboard_values' ); |
| 105 |
|
| 106 |
if ( empty( $options ) ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
// Get current user data. |
| 111 |
$user = wp_get_current_user(); |
| 112 |
if ( ! $user->roles ) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
// Get settings for the roles. |
| 117 |
$disabled_dashboard_option_ = array(); |
| 118 |
foreach ( $user->roles as $role ) { |
| 119 |
$disabled_dashboard_option_[] = _mw_adminimize_get_option_value( 'mw_adminimize_disabled_dashboard_option_' . $role . '_items' ); |
| 120 |
} |
| 121 |
|
| 122 |
// Support Multiple Roles for users. |
| 123 |
if ( _mw_adminimize_get_option_value( 'mw_adminimize_multiple_roles' ) && 1 < count( $user->roles ) ) { |
| 124 |
$disabled_dashboard_option_ = _mw_adminimize_get_duplicate( $disabled_dashboard_option_ ); |
| 125 |
} |
| 126 |
|
| 127 |
if ( empty( $disabled_dashboard_option_[0] ) ) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
$selectors = implode( ', ', $disabled_dashboard_option_[0] ); |
| 132 |
echo '<!-- Set Adminimize dashboard options -->' . "\n"; |
| 133 |
echo '<style type="text/css">' . esc_attr( $selectors ) . ' {display:none !important;}</style>' . "\n"; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get all registered dashboard widgets. |
| 138 |
* |
| 139 |
* @return array |
| 140 |
*/ |
| 141 |
function _mw_adminimize_get_dashboard_widgets() { |
| 142 |
|
| 143 |
global $wp_meta_boxes; |
| 144 |
|
| 145 |
$widgets = array(); |
| 146 |
if ( ! isset( $wp_meta_boxes['dashboard'] ) ) { |
| 147 |
return $widgets; |
| 148 |
} |
| 149 |
|
| 150 |
foreach ( (array) $wp_meta_boxes['dashboard'] as $context => $datas ) { |
| 151 |
foreach ( (array) $datas as $priority => $data ) { |
| 152 |
foreach ( (array) $data as $widget => $value ) { |
| 153 |
|
| 154 |
|
| 155 |
if ( ! isset( $value['title'])) { |
| 156 |
$value['title'] = ''; |
| 157 |
} |
| 158 |
|
| 159 |
// Some plugins create a title that contains an array, we create an empty string to prevent an error in strip_tags |
| 160 |
if (is_array ( $value['title'])) { |
| 161 |
$value['title'] = ''; |
| 162 |
} |
| 163 |
|
| 164 |
$widgets[ $widget ] = array( |
| 165 |
'id' => $widget, |
| 166 |
'title' => strip_tags( |
| 167 |
preg_replace( '/( |)<span.*span>/im', '', $value['title'] ) |
| 168 |
), |
| 169 |
'context' => $context, |
| 170 |
'priority' => $priority, |
| 171 |
); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
return $widgets; |
| 177 |
} |
| 178 |
|