| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPMUDEV Black Friday Utils class |
| 4 |
* |
| 5 |
* Common utility methods for the Black Friday module. |
| 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 Utils |
| 21 |
* |
| 22 |
* Common utility methods for the Black Friday module. |
| 23 |
* |
| 24 |
* @since 2.0.0 |
| 25 |
*/ |
| 26 |
class Utils { |
| 27 |
|
| 28 |
/** |
| 29 |
* Gets the script data from assets php file. |
| 30 |
* |
| 31 |
* @since 2.0.0 |
| 32 |
* |
| 33 |
* @param string $handle The script handle (e.g., 'banner'). |
| 34 |
* |
| 35 |
* @return array The script data. |
| 36 |
*/ |
| 37 |
public static function raw_script_data( string $handle = '' ): array { |
| 38 |
static $script_data = array(); |
| 39 |
|
| 40 |
if ( empty( $handle ) ) { |
| 41 |
return array(); |
| 42 |
} |
| 43 |
|
| 44 |
if ( isset( $script_data[ $handle ] ) ) { |
| 45 |
return $script_data[ $handle ]; |
| 46 |
} |
| 47 |
|
| 48 |
$file_path = WPMUDEV_MODULE_BLACK_FRIDAY_DIR . 'build/' . $handle . '.min.asset.php'; |
| 49 |
|
| 50 |
if ( file_exists( $file_path ) ) { |
| 51 |
$script_data[ $handle ] = include $file_path; |
| 52 |
} else { |
| 53 |
$script_data[ $handle ] = array(); |
| 54 |
} |
| 55 |
|
| 56 |
return (array) $script_data[ $handle ]; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Gets assets data for given key. |
| 61 |
* |
| 62 |
* @since 2.0.0 |
| 63 |
* |
| 64 |
* @param string $handle The script handle (e.g., 'banner'). |
| 65 |
* @param string $key The requested portion of data, an array key, usually version or dependencies. |
| 66 |
* |
| 67 |
* @return string|array The script data or specific key value. |
| 68 |
*/ |
| 69 |
public static function script_data( string $handle = '', string $key = '' ) { |
| 70 |
$raw_script_data = self::raw_script_data( $handle ); |
| 71 |
|
| 72 |
return ! empty( $key ) && ! empty( $raw_script_data[ $key ] ) ? $raw_script_data[ $key ] : $raw_script_data; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get's all plugin data from plugins_list file. |
| 77 |
* |
| 78 |
* @since 2.0.0 |
| 79 |
* |
| 80 |
* @return array The plugins list. |
| 81 |
*/ |
| 82 |
public static function get_plugins_list(): array { |
| 83 |
static $plugins_list = null; |
| 84 |
|
| 85 |
if ( is_null( $plugins_list ) ) { |
| 86 |
$plugins_file = untrailingslashit( WPMUDEV_MODULE_BLACK_FRIDAY_DIR ) . '/includes/plugins-list.php'; |
| 87 |
|
| 88 |
if ( file_exists( $plugins_file ) ) { |
| 89 |
$plugins_list = require_once $plugins_file; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return is_array( $plugins_list ) ? $plugins_list : array(); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Get's the admin screens of each WPMU DEV plugin. |
| 98 |
* |
| 99 |
* @since 2.0.0 |
| 100 |
* |
| 101 |
* @return array The plugin screen slugs. |
| 102 |
*/ |
| 103 |
public static function get_plugin_screens(): array { |
| 104 |
static $plugins_screens = null; |
| 105 |
|
| 106 |
if ( is_null( $plugins_screens ) ) { |
| 107 |
$plugins_list = self::get_plugins_list(); |
| 108 |
$plugins_screens = array(); |
| 109 |
|
| 110 |
// Extract admin_url_page from each plugin. |
| 111 |
$admin_pages = wp_list_pluck( $plugins_list, 'admin_url_page' ); |
| 112 |
|
| 113 |
// Flatten in case admin_url_page is an array. |
| 114 |
foreach ( $admin_pages as $page ) { |
| 115 |
if ( is_array( $page ) ) { |
| 116 |
// If it's an array, add each item. |
| 117 |
$plugins_screens = array_merge( $plugins_screens, $page ); |
| 118 |
} elseif ( ! empty( $page ) ) { |
| 119 |
// If it's a single value, add it directly. |
| 120 |
$plugins_screens[] = $page; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
// Remove duplicates and reindex. |
| 125 |
$plugins_screens = array_values( array_unique( $plugins_screens ) ); |
| 126 |
} |
| 127 |
|
| 128 |
return $plugins_screens; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Get's the parent admin screens of each WPMU DEV plugin. |
| 133 |
* |
| 134 |
* @since 2.0.0 |
| 135 |
* |
| 136 |
* @return array The plugin parent screen slugs. |
| 137 |
*/ |
| 138 |
public static function get_plugin_parent_screens(): array { |
| 139 |
$plugins_list = self::get_plugins_list(); |
| 140 |
return array_values( |
| 141 |
wp_list_pluck( $plugins_list, 'admin_parent_page' ) |
| 142 |
); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Get's the parent menu slugs of each WPMU DEV plugin. |
| 147 |
* |
| 148 |
* @since 2.0.0 |
| 149 |
* |
| 150 |
* @return array The parent menu slugs. |
| 151 |
*/ |
| 152 |
public static function get_parent_menu_slugs(): array { |
| 153 |
return self::get_plugin_screens(); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Polyfill for str_ends_with function for PHP versions < 8.0 and WP versions < 5.9. BLC supports from WP 5.2+. |
| 158 |
* |
| 159 |
* @since 2.0.0 |
| 160 |
* |
| 161 |
* @param string $haystack The string to search in. |
| 162 |
* @param string $needle The substring to search for. |
| 163 |
* |
| 164 |
* @return bool True if $haystack ends with $needle, false otherwise. |
| 165 |
*/ |
| 166 |
public static function str_ends_with( string $haystack, string $needle ): bool { |
| 167 |
if ( function_exists( 'str_ends_with' ) ) { |
| 168 |
return str_ends_with( $haystack, $needle ); |
| 169 |
} |
| 170 |
|
| 171 |
$length = strlen( $needle ); |
| 172 |
if ( 0 === $length ) { |
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
return substr( $haystack, -$length ) === $needle; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Get UTM source for given plugin key. |
| 181 |
* |
| 182 |
* @since 2.0.0 |
| 183 |
* |
| 184 |
* @param string $key The plugin key. |
| 185 |
* |
| 186 |
* @return string The UTM source. |
| 187 |
*/ |
| 188 |
public static function get_utm_source( string $key = '' ): string { |
| 189 |
$plugins_list = self::get_plugins_list(); |
| 190 |
$utm_source = ''; |
| 191 |
|
| 192 |
if ( isset( $plugins_list[ $key ] ) && isset( $plugins_list[ $key ]['utm_source'] ) ) { |
| 193 |
$utm_source = $plugins_list[ $key ]['utm_source']; |
| 194 |
} |
| 195 |
|
| 196 |
return $utm_source; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Get UTM source for the current admin page. |
| 201 |
* |
| 202 |
* @since 2.0.0 |
| 203 |
* |
| 204 |
* @return string The UTM source. |
| 205 |
*/ |
| 206 |
public static function get_current_page_utm_source(): string { |
| 207 |
$current_screen = get_current_screen(); |
| 208 |
$plugins_list = self::get_plugins_list(); |
| 209 |
|
| 210 |
if ( empty( $current_screen ) || empty( $current_screen->id ) ) { |
| 211 |
return ''; |
| 212 |
} |
| 213 |
|
| 214 |
$current_page = $current_screen->id; |
| 215 |
|
| 216 |
foreach ( $plugins_list as $plugin_key => $plugin_data ) { |
| 217 |
$admin_url_page = $plugin_data['admin_url_page'] ?? ''; |
| 218 |
|
| 219 |
if ( is_string( $admin_url_page ) && str_contains( $current_page, $admin_url_page ) ) { |
| 220 |
return $plugin_data['utm_source'] ?? ''; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
return ''; |
| 225 |
} |
| 226 |
} |
| 227 |
|