| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPMUDEV Black Friday Admin Menu class |
| 4 |
* |
| 5 |
* Handles the addition of admin menu page for Black Friday campaign. |
| 6 |
* |
| 7 |
* @since 2.0.0 |
| 8 |
* @author WPMUDEV |
| 9 |
* @package WPMUDEV\Modules\BlackFriday |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace WPMUDEV\Modules\BlackFriday; |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Class Admin_Menu |
| 21 |
* |
| 22 |
* Manages Black Friday admin menu page. |
| 23 |
* |
| 24 |
* @since 2.0.0 |
| 25 |
*/ |
| 26 |
class Admin_Menu { |
| 27 |
|
| 28 |
/** |
| 29 |
* Campaign URL. |
| 30 |
* |
| 31 |
* @since 2.0.0 |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
private $campaign_url = ''; |
| 35 |
|
| 36 |
/** |
| 37 |
* Menu title. |
| 38 |
* |
| 39 |
* @since 2.0.0 |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
private $menu_title = ''; |
| 43 |
|
| 44 |
/** |
| 45 |
* Page title. |
| 46 |
* |
| 47 |
* @since 2.0.0 |
| 48 |
* @var string |
| 49 |
*/ |
| 50 |
private $page_title = ''; |
| 51 |
|
| 52 |
/** |
| 53 |
* Menu slug. |
| 54 |
* |
| 55 |
* @since 2.0.0 |
| 56 |
* @var string |
| 57 |
*/ |
| 58 |
private $menu_slug = 'wpmudev-black-friday'; |
| 59 |
|
| 60 |
/** |
| 61 |
* UTM content for the menu. |
| 62 |
* |
| 63 |
* @since 2.0.0 |
| 64 |
* @var string |
| 65 |
*/ |
| 66 |
private $utm_content = 'BF-Plugins-2025'; |
| 67 |
|
| 68 |
/** |
| 69 |
* UTM campaign for the menu. |
| 70 |
* |
| 71 |
* @since 2.0.0 |
| 72 |
* @var string |
| 73 |
*/ |
| 74 |
private $utm_campaign = 'BF-Plugins-2025-menu |
| 75 |
'; |
| 76 |
|
| 77 |
/** |
| 78 |
* Constructor. |
| 79 |
* |
| 80 |
* @since 2.0.0 |
| 81 |
* |
| 82 |
* @param array $args Optional. Admin menu configuration arguments. |
| 83 |
*/ |
| 84 |
public function __construct( $args = array() ) { |
| 85 |
$defaults = array( |
| 86 |
'campaign_url' => 'https://wpmudev.com/black-friday/', |
| 87 |
'menu_title' => __( 'Get Black Friday Deal', 'wpmudev-black-friday' ), |
| 88 |
'page_title' => __( 'WPMUDEV Black Friday Deal', 'wpmudev-black-friday' ), |
| 89 |
'menu_slug' => 'wpmudev-black-friday', |
| 90 |
); |
| 91 |
|
| 92 |
$args = wp_parse_args( $args, $defaults ); |
| 93 |
|
| 94 |
$this->campaign_url = esc_url( $args['campaign_url'] ); |
| 95 |
$this->menu_title = sanitize_text_field( $args['menu_title'] ); |
| 96 |
$this->page_title = sanitize_text_field( $args['page_title'] ); |
| 97 |
$this->menu_slug = sanitize_key( $args['menu_slug'] ); |
| 98 |
|
| 99 |
$this->init_hooks(); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Initialize WordPress hooks. |
| 104 |
* |
| 105 |
* @since 2.0.0 |
| 106 |
* |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
private function init_hooks() { |
| 110 |
$hook = ( is_multisite() && is_network_admin() ) ? 'network_admin_menu' : 'admin_menu'; |
| 111 |
add_action( $hook, array( $this, 'add_admin_page' ), 99999 ); |
| 112 |
// Normally we would remove the admin pages on admin_menu hook, we will use admin_head though which is used in some plugins. |
| 113 |
add_action( 'admin_head', array( $this, 'remove_admin_pages' ), 99999 ); |
| 114 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_menu_styles' ), 999999 ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Add admin menu page. |
| 119 |
* |
| 120 |
* @since 2.0.0 |
| 121 |
* |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
public function add_admin_page() { |
| 125 |
// Only show to administrators. |
| 126 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
// Get all WPMU DEV plugins data. |
| 131 |
$plugins_list = Utils::get_plugins_list(); |
| 132 |
|
| 133 |
foreach ( $plugins_list as $plugin_key => $plugin_data ) { |
| 134 |
$utm_source = $plugin_data['utm_source'] ?? 'admin_menu'; |
| 135 |
$parent_slug = $plugin_data['admin_parent_page'] ?? ''; |
| 136 |
|
| 137 |
if ( empty( $parent_slug ) ) { |
| 138 |
continue; |
| 139 |
} |
| 140 |
|
| 141 |
$menu_url = add_query_arg( |
| 142 |
array( |
| 143 |
'utm_source' => $utm_source, |
| 144 |
'utm_medium' => 'plugin', |
| 145 |
'utm_campaign' => $utm_source . '-' . $this->utm_campaign, |
| 146 |
'utm_content' => $this->utm_content, |
| 147 |
), |
| 148 |
$this->campaign_url |
| 149 |
); |
| 150 |
|
| 151 |
add_submenu_page( |
| 152 |
$parent_slug, |
| 153 |
$this->page_title, |
| 154 |
$this->menu_title, |
| 155 |
'manage_options', |
| 156 |
$menu_url, |
| 157 |
null, |
| 158 |
PHP_INT_MAX // Position as last submenu item. |
| 159 |
); |
| 160 |
|
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Remove sales/offers and cross-sell admin menus to replace them with Black Friday campaign. |
| 166 |
* |
| 167 |
* @since 2.0.0 |
| 168 |
* |
| 169 |
* @return void |
| 170 |
*/ |
| 171 |
public function remove_admin_pages() { |
| 172 |
global $submenu; |
| 173 |
|
| 174 |
$parent_menu_slugs = Utils::get_plugin_parent_screens(); |
| 175 |
|
| 176 |
foreach ( $parent_menu_slugs as $parent_slug ) { |
| 177 |
if ( empty( $parent_slug ) || empty( $submenu[ $parent_slug ] ) ) { |
| 178 |
continue; |
| 179 |
} |
| 180 |
|
| 181 |
$prefix = 'https://wpmudev.com/project/'; |
| 182 |
|
| 183 |
foreach ( $submenu[ $parent_slug ] as $index => $item ) { |
| 184 |
$slug = $item[2] ?? ''; |
| 185 |
|
| 186 |
// Match the submenu URL. |
| 187 |
if ( is_string( $slug ) && ( strpos( $slug, $prefix ) === 0 || Utils::str_ends_with( $slug, '_cross_sell' ) || Utils::str_ends_with( $slug, 'cross-sell' ) ) ) { |
| 188 |
unset( $submenu[ $parent_slug ][ $index ] ); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Get campaign URL. |
| 196 |
* |
| 197 |
* @since 2.0.0 |
| 198 |
* |
| 199 |
* @return string The campaign URL. |
| 200 |
*/ |
| 201 |
public function get_campaign_url() { |
| 202 |
return $this->campaign_url; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Get menu slug. |
| 207 |
* |
| 208 |
* @since 2.0.0 |
| 209 |
* |
| 210 |
* @return string The menu slug. |
| 211 |
*/ |
| 212 |
public function get_menu_slug() { |
| 213 |
return $this->menu_slug; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Enqueue custom styles for the Black Friday menu items. |
| 218 |
* |
| 219 |
* @since 2.0.0 |
| 220 |
* |
| 221 |
* @return void |
| 222 |
*/ |
| 223 |
public function enqueue_menu_styles() { |
| 224 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
$custom_css = ' |
| 229 |
#adminmenu #toplevel_page_branding ul.wp-submenu li:last-child a[href*="/black-friday"], |
| 230 |
#adminmenu #toplevel_page_wds_wizard ul.wp-submenu li:last-child a[href*="/black-friday"], |
| 231 |
#adminmenu #toplevel_page_wphb ul.wp-submenu li:last-child a[href*="/black-friday"], |
| 232 |
#adminmenu #toplevel_page_forminator ul.wp-submenu li:last-child a[href*="/black-friday"], |
| 233 |
#adminmenu #toplevel_page_hustle .wp-submenu li:last-child a[href*="/black-friday"], |
| 234 |
#toplevel_page_smush li:last-child a[href*="/black-friday"], |
| 235 |
#adminmenu a[href="' . esc_attr( $this->campaign_url ) . '"], |
| 236 |
#adminmenu a[href*="/black-friday"] { |
| 237 |
background: #B1FF65 !important; |
| 238 |
color: #000000 !important; |
| 239 |
font-weight: 500 !important; |
| 240 |
} |
| 241 |
|
| 242 |
#adminmenu #toplevel_page_branding ul.wp-submenu li:last-child a[href*="/black-friday"]:hover, |
| 243 |
#adminmenu #toplevel_page_branding ul.wp-submenu li:last-child a[href*="/black-friday"]:focus, |
| 244 |
#adminmenu #toplevel_page_wds_wizard ul.wp-submenu li:last-child a[href*="/black-friday"]:hover, |
| 245 |
#adminmenu #toplevel_page_wds_wizard ul.wp-submenu li:last-child a[href*="/black-friday"]:focus, |
| 246 |
#adminmenu #toplevel_page_wphb ul.wp-submenu li:last-child a[href*="/black-friday"]:hover, |
| 247 |
#adminmenu #toplevel_page_wphb ul.wp-submenu li:last-child a[href*="/black-friday"]:focus, |
| 248 |
#adminmenu #toplevel_page_forminator ul.wp-submenu li:last-child a[href*="/black-friday"]:hover, |
| 249 |
#adminmenu #toplevel_page_forminator ul.wp-submenu li:last-child a[href*="/black-friday"]:focus, |
| 250 |
#adminmenu #toplevel_page_hustle .wp-submenu li:last-child a[href*="/black-friday"]:hover, |
| 251 |
#adminmenu #toplevel_page_hustle .wp-submenu li:last-child a[href*="/black-friday"]:focus, |
| 252 |
#toplevel_page_smush li:last-child a[href*="/black-friday"]:hover, |
| 253 |
#toplevel_page_smush li:last-child a[href*="/black-friday"]:focus, |
| 254 |
#adminmenu a[href*="/black-friday"]:hover, |
| 255 |
#adminmenu a[href*="/black-friday"]:focus, |
| 256 |
#toplevel_page_smush li:last-child a[href*="' . esc_attr( $this->campaign_url ) . '"]:hover, |
| 257 |
#toplevel_page_smush li:last-child a[href*="' . esc_attr( $this->campaign_url ) . '"]:focus, |
| 258 |
#adminmenu a[href*="' . esc_attr( $this->campaign_url ) . '"]:hover, |
| 259 |
#adminmenu a[href*="' . esc_attr( $this->campaign_url ) . '"]:focus { |
| 260 |
background: #71A341 !important; |
| 261 |
transition: background 0.3s ease !important; |
| 262 |
} |
| 263 |
'; |
| 264 |
|
| 265 |
wp_add_inline_style( 'admin-menu', $custom_css ); |
| 266 |
} |
| 267 |
} |
| 268 |
|