| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Get a specific option from White Label settings. |
| 5 |
* |
| 6 |
* @param [type] $option id of option in settings tab. |
| 7 |
* @param boolean $section id of setting tab. |
| 8 |
* @param string $default default if nothing is found. |
| 9 |
* @return mixed |
| 10 |
*/ |
| 11 |
function white_label_get_option($option, $section, $default = '') |
| 12 |
{ |
| 13 |
global $white_label; |
| 14 |
|
| 15 |
if (isset($white_label->constants['multisite']) && !empty($white_label->constants['multisite']) && $white_label->constants['multisite']['global_settings'] == true && $white_label->constants['multisite']['ignore_global_settings'] != true) { |
| 16 |
$options = get_blog_option($white_label->constants['multisite']['main_site'], $section); |
| 17 |
} else { |
| 18 |
$options = get_option($section); |
| 19 |
} |
| 20 |
|
| 21 |
if (isset($options[$option])) { |
| 22 |
return $options[$option]; |
| 23 |
} |
| 24 |
|
| 25 |
return $default; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Admin Login Preview. |
| 30 |
* |
| 31 |
* @return void |
| 32 |
*/ |
| 33 |
function white_label_login_preview() |
| 34 |
{ |
| 35 |
$login_logo_file = white_label_get_option('login_logo_file', 'white_label_login', false); |
| 36 |
$login_background_file = white_label_get_option('login_background_file', 'white_label_login', false); |
| 37 |
$login_color_background = white_label_get_option('login_background_color', 'white_label_login', '#f1f1f1'); |
| 38 |
|
| 39 |
// Only once a logo is set. |
| 40 |
if (!$login_logo_file) { |
| 41 |
// use default WP logo. |
| 42 |
$login_logo_file = admin_url('/images/wordpress-logo.svg'); |
| 43 |
} |
| 44 |
|
| 45 |
$preview_text = __('Preview Login Page', 'white-label'); |
| 46 |
|
| 47 |
$style = 'background-image: url('.$login_background_file.'); |
| 48 |
background-color:'.$login_color_background.'; |
| 49 |
background-position: center; |
| 50 |
background-size: cover; |
| 51 |
height: 200px; |
| 52 |
display: flex; |
| 53 |
align-items: center; |
| 54 |
justify-content: center; |
| 55 |
'; |
| 56 |
|
| 57 |
$logo = '<img style=" |
| 58 |
max-width: 180px; |
| 59 |
max-height: 180px; |
| 60 |
display: block; |
| 61 |
margin: 0 auto;" |
| 62 |
src="'.$login_logo_file.'"/>'; |
| 63 |
|
| 64 |
$preview_link = get_site_url(null, '/wp-login.php'); |
| 65 |
|
| 66 |
$output = '<div class="postbox white-label-preview-box"> |
| 67 |
|
| 68 |
<div style="%s">%s</div> |
| 69 |
<div style="padding: 14px;">'; |
| 70 |
|
| 71 |
$output .= ' |
| 72 |
<a href="'.$preview_link.'" target="_blank" style="text-align:center;display:block; margin: 0 auto; width:80%%;" class="button-secondary">%s <span style="padding:3px;" class="dashicons dashicons-migrate"></span></a> |
| 73 |
</div> |
| 74 |
|
| 75 |
</div>'; |
| 76 |
|
| 77 |
// Echo the preview on our admin page. |
| 78 |
echo sprintf($output, $style, $logo, $preview_text); // phpcs:ignore |
| 79 |
} |
| 80 |
|
| 81 |
add_action('white_label_above_settings_sidebars', 'white_label_login_preview'); |
| 82 |
/** |
| 83 |
* Display the import and export forms in our settings import/export tab. |
| 84 |
* |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
function white_label_import_export_html() |
| 88 |
{ |
| 89 |
$import_export = new white_label_Import_Export_Options(); |
| 90 |
$import_export->display_html(); |
| 91 |
} |
| 92 |
|
| 93 |
add_action('white_label_settings_tab_white_label_import_export', 'white_label_import_export_html'); |
| 94 |
|
| 95 |
/** |
| 96 |
* Create an array of all administrator. |
| 97 |
* |
| 98 |
* @return array admin_id => name + details. |
| 99 |
*/ |
| 100 |
function white_label_get_regular_admins() |
| 101 |
{ |
| 102 |
$cached_admins = wp_cache_get('admins', 'white_label_settings'); |
| 103 |
|
| 104 |
if ($cached_admins !== false) { |
| 105 |
return $cached_admins; |
| 106 |
} |
| 107 |
|
| 108 |
$admins = []; |
| 109 |
|
| 110 |
$args = [ |
| 111 |
'role' => 'Administrator', |
| 112 |
'orderby' => 'user_nicename', |
| 113 |
'order' => 'ASC', |
| 114 |
]; |
| 115 |
|
| 116 |
$blogusers = get_users($args); |
| 117 |
|
| 118 |
$you_text = __('You', 'white-label'); |
| 119 |
|
| 120 |
if ($blogusers && is_array($blogusers)) { |
| 121 |
// Array of WP_User objects. |
| 122 |
foreach ($blogusers as $user) { |
| 123 |
$current_user_indicator = $user->ID === get_current_user_id() ? '<span style="background-color:red;padding: 1px 7px;color: white;font-weight: 500;border-radius: 7px;font-size: 11px;">'.$you_text.'</span>' : ''; |
| 124 |
|
| 125 |
$admins[$user->ID] = '<b>'.$user->user_nicename."</b><i> ($user->user_email</i>) ".$current_user_indicator; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
// Just enough to prevent duplicate queries. |
| 130 |
wp_cache_set('admins', $admins, 'white_label_settings', 60); |
| 131 |
|
| 132 |
return $admins; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Create an array of all plugins. |
| 137 |
* |
| 138 |
* @return array |
| 139 |
*/ |
| 140 |
function white_label_get_plugins() |
| 141 |
{ |
| 142 |
$all_plugins = get_plugins(); |
| 143 |
|
| 144 |
$plugins_formatted = []; |
| 145 |
|
| 146 |
if ($all_plugins && is_array($all_plugins)) { |
| 147 |
foreach ($all_plugins as $key => $plugin) { |
| 148 |
$plugins_formatted[$key] = $plugin['Name']; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return $plugins_formatted; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Create an array of all themes. |
| 157 |
* |
| 158 |
* @return array |
| 159 |
*/ |
| 160 |
function white_label_get_themes() |
| 161 |
{ |
| 162 |
$all_themes = wp_get_themes(['allowed' => 'network']); |
| 163 |
|
| 164 |
$themes_formatted = []; |
| 165 |
|
| 166 |
if ($all_themes && is_array($all_themes)) { |
| 167 |
foreach ($all_themes as $theme) { |
| 168 |
$themes_formatted[$theme->get_stylesheet()] = $theme->get('Name'); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
return $themes_formatted; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Create an array of all sidebar menu itmes. |
| 177 |
* |
| 178 |
* @return array slug => name. |
| 179 |
*/ |
| 180 |
function white_label_get_sidebar_menus() |
| 181 |
{ |
| 182 |
global $menu; |
| 183 |
|
| 184 |
$selectable_items = []; |
| 185 |
|
| 186 |
if (!empty($menu) && is_array($menu)) { |
| 187 |
foreach ($menu as $menu_entry) { |
| 188 |
$menu_name = !empty($menu_entry[0]) ? $menu_entry[0] : $menu_entry[2]; |
| 189 |
$default_menu_name = $menu_name; |
| 190 |
$menu_name = ($menu_name !== null ? trim(preg_replace("/(<.*>.*?<\/.*>)/is", '', $menu_name)) : ''); |
| 191 |
|
| 192 |
if (preg_match('/separator/i', $menu_name)) { |
| 193 |
$menu_name = 'Separator'; |
| 194 |
} |
| 195 |
|
| 196 |
if (strlen($menu_name) == 0) { |
| 197 |
$menu_name = ($default_menu_name !== null ? strip_tags($default_menu_name) : $default_menu_name); |
| 198 |
} |
| 199 |
|
| 200 |
$parent_slug = $menu_entry[2]; |
| 201 |
|
| 202 |
$selectable_items[$parent_slug] = [ |
| 203 |
'name' => esc_html($menu_name), |
| 204 |
'submenus' => white_label_get_submenus($parent_slug), |
| 205 |
]; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
return $selectable_items; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* @param string $parent_slug Parent menu slug. |
| 214 |
* @return array |
| 215 |
*/ |
| 216 |
function white_label_get_submenus($parent_slug) |
| 217 |
{ |
| 218 |
global $submenu; |
| 219 |
|
| 220 |
$submenu_array = []; |
| 221 |
|
| 222 |
if (!empty($submenu[$parent_slug]) && is_array($submenu[$parent_slug])) { |
| 223 |
foreach ($submenu[$parent_slug] as $key => $menu) { |
| 224 |
if (!empty($menu) && is_array($menu)) { |
| 225 |
$submenu_item = remove_query_arg('return', $menu[2]); |
| 226 |
$submenu_key = sanitize_title($submenu_item); |
| 227 |
|
| 228 |
$slug = $parent_slug.'_whitelabel_'.$submenu_key; |
| 229 |
$menu_name = ($menu[0] !== null) ? $menu[0] : ''; |
| 230 |
$default_menu_name = $menu_name; |
| 231 |
$menu_name = ($menu_name !== null ? trim(preg_replace("/(<.*>.*?<\/.*>)/is", '', $menu_name)) : ''); |
| 232 |
|
| 233 |
if (strlen($menu_name) == 0) { |
| 234 |
$menu_name = ($default_menu_name !== null ? strip_tags($default_menu_name) : $default_menu_name); |
| 235 |
} |
| 236 |
|
| 237 |
// Add the submenu to the array. |
| 238 |
$submenu_array[$slug] = esc_html($menu_name); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
return $submenu_array ? $submenu_array : false; |
| 244 |
} |
| 245 |
|