| 1 |
<?php |
| 2 |
/** |
| 3 |
* The class provides utility functions related to WordPress. |
| 4 |
* |
| 5 |
* @package AdvancedAds |
| 6 |
* @author Advanced Ads <info@wpadvancedads.com> |
| 7 |
* @since 1.47.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace AdvancedAds\Utilities; |
| 11 |
|
| 12 |
use DateTimeZone; |
| 13 |
use AdvancedAds\Constants; |
| 14 |
use AdvancedAds\Admin\Upgrades; |
| 15 |
use AdvancedAds\Framework\Utilities\Str; |
| 16 |
use AdvancedAds\Framework\Utilities\Params; |
| 17 |
|
| 18 |
defined( 'ABSPATH' ) || exit; |
| 19 |
|
| 20 |
/** |
| 21 |
* Utilities WordPress. |
| 22 |
*/ |
| 23 |
class WordPress { |
| 24 |
|
| 25 |
/** |
| 26 |
* Debug function |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public static function dd(): void { |
| 31 |
echo '<pre>'; |
| 32 |
foreach ( func_get_args() as $arg ) { |
| 33 |
print_r( $arg ); // phpcs:ignore |
| 34 |
} |
| 35 |
echo '</pre>'; |
| 36 |
die(); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Function to calculate percentage |
| 41 |
* |
| 42 |
* @param int $part Part of total. |
| 43 |
* @param int $total Total value. |
| 44 |
* |
| 45 |
* @return string |
| 46 |
*/ |
| 47 |
public static function calculate_percentage( $part, $total ): string { |
| 48 |
$percentage = ( $part / $total ) * 100; |
| 49 |
return number_format( $percentage, 2 ) . '%'; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Get the current action selected from the bulk actions dropdown. |
| 54 |
* |
| 55 |
* @return string|false The action name or False if no action was selected |
| 56 |
*/ |
| 57 |
public static function current_action() { |
| 58 |
$action = Params::request( 'action' ); |
| 59 |
if ( '-1' !== $action ) { |
| 60 |
return sanitize_key( $action ); |
| 61 |
} |
| 62 |
|
| 63 |
$action = Params::request( 'action2' ); |
| 64 |
if ( '-1' !== $action ) { |
| 65 |
return sanitize_key( $action ); |
| 66 |
} |
| 67 |
|
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Get count of ads |
| 73 |
* |
| 74 |
* @param string $status Status need count for. |
| 75 |
* |
| 76 |
* @return int |
| 77 |
*/ |
| 78 |
public static function get_count_ads( $status = 'any' ): int { |
| 79 |
$counts = (array) wp_count_posts( Constants::POST_TYPE_AD ); |
| 80 |
|
| 81 |
if ( 'any' === $status ) { |
| 82 |
return array_sum( $counts ); |
| 83 |
} |
| 84 |
|
| 85 |
return $counts[ $status ] ?? 0; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get site domain |
| 90 |
* |
| 91 |
* @param string $part Part of domain. |
| 92 |
* |
| 93 |
* @return string |
| 94 |
*/ |
| 95 |
public static function get_site_domain( $part = 'host' ): string { |
| 96 |
$domain = wp_parse_url( home_url( '/' ), PHP_URL_HOST ); |
| 97 |
|
| 98 |
if ( 'name' === $part ) { |
| 99 |
$domain = explode( '.', $domain ); |
| 100 |
$domain = count( $domain ) > 2 ? $domain[1] : $domain[0]; |
| 101 |
} |
| 102 |
|
| 103 |
return $domain; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get SVG content as string |
| 108 |
* |
| 109 |
* @param string $file File name. |
| 110 |
* @param string $folder Folder name if not default. |
| 111 |
* |
| 112 |
* @return string |
| 113 |
*/ |
| 114 |
public static function get_svg( $file, $folder = '/assets/img/' ): string { |
| 115 |
$file_path = \untrailingslashit( ADVADS_ABSPATH ) . $folder . $file; |
| 116 |
|
| 117 |
if ( file_exists( $file_path ) ) { |
| 118 |
ob_start(); |
| 119 |
include $file_path; |
| 120 |
return ob_get_clean(); |
| 121 |
} |
| 122 |
|
| 123 |
return ''; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Retrieves the timezone of the site as a DateTimeZone object. |
| 128 |
* |
| 129 |
* @return DateTimeZone |
| 130 |
*/ |
| 131 |
public static function get_timezone(): DateTimeZone { |
| 132 |
static $advads_timezone; |
| 133 |
|
| 134 |
// Early bail!! |
| 135 |
if ( null !== $advads_timezone ) { |
| 136 |
return $advads_timezone; |
| 137 |
} |
| 138 |
|
| 139 |
if ( function_exists( 'wp_timezone' ) ) { |
| 140 |
$advads_timezone = wp_timezone(); |
| 141 |
return $advads_timezone; |
| 142 |
} |
| 143 |
|
| 144 |
$date_time_zone = new DateTimeZone( self::get_timezone_string() ); |
| 145 |
|
| 146 |
return $date_time_zone; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Retrieves the timezone of the site as a string. |
| 151 |
* |
| 152 |
* @return string |
| 153 |
*/ |
| 154 |
public static function get_timezone_string(): string { |
| 155 |
$timezone_string = get_option( 'timezone_string' ); |
| 156 |
|
| 157 |
if ( $timezone_string ) { |
| 158 |
return $timezone_string; |
| 159 |
} |
| 160 |
|
| 161 |
$offset = (float) get_option( 'gmt_offset' ); |
| 162 |
$hours = (int) $offset; |
| 163 |
$minutes = ( $offset - $hours ); |
| 164 |
|
| 165 |
$sign = ( $offset < 0 ) ? '-' : '+'; |
| 166 |
$abs_hour = abs( $hours ); |
| 167 |
$abs_mins = abs( $minutes * 60 ); |
| 168 |
|
| 169 |
return sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Get literal expression of timezone. |
| 174 |
* |
| 175 |
* @return string Human readable timezone name. |
| 176 |
*/ |
| 177 |
public static function get_timezone_name(): string { |
| 178 |
$time_zone = self::get_timezone()->getName(); |
| 179 |
if ( 'UTC' === $time_zone ) { |
| 180 |
return 'UTC+0'; |
| 181 |
} |
| 182 |
|
| 183 |
if ( 0 === strpos( $time_zone, '+' ) || 0 === strpos( $time_zone, '-' ) ) { |
| 184 |
return 'UTC' . $time_zone; |
| 185 |
} |
| 186 |
|
| 187 |
/* translators: timezone name */ |
| 188 |
return sprintf( __( 'time of %s', 'advanced-ads' ), $time_zone ); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Render icon of the type. |
| 193 |
* |
| 194 |
* @param string $icon Icon url. |
| 195 |
* |
| 196 |
* @return void |
| 197 |
*/ |
| 198 |
public static function render_icon( $icon ): void { |
| 199 |
printf( '<img src="%s" width="50" height="50" />', esc_url( $icon ) ); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Applies image loading optimization attributes to an image HTML tag based on WordPress version. |
| 204 |
* |
| 205 |
* @param string $img HTML image tag. |
| 206 |
* @param string $context Image context. |
| 207 |
* |
| 208 |
* @return string Updated HTML image tag with loading optimization attributes. |
| 209 |
*/ |
| 210 |
public static function img_tag_add_loading_attr( $img, $context ) { |
| 211 |
if ( is_array( $context ) ) { |
| 212 |
$context = end( $context ); |
| 213 |
} |
| 214 |
|
| 215 |
// Check if the current WordPress version is compatible. |
| 216 |
if ( is_wp_version_compatible( '6.3' ) ) { |
| 217 |
return wp_img_tag_add_loading_optimization_attrs( $img, $context ); |
| 218 |
} |
| 219 |
|
| 220 |
return wp_img_tag_add_loading_attr( $img, $context ); // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_img_tag_add_loading_attrFound |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Improve WP_Query performance |
| 225 |
* |
| 226 |
* @param array $args Query arguments. |
| 227 |
* |
| 228 |
* @return array |
| 229 |
*/ |
| 230 |
public static function improve_wp_query( $args ): array { |
| 231 |
$args['no_found_rows'] = true; |
| 232 |
$args['update_post_meta_cache'] = false; |
| 233 |
$args['update_post_term_cache'] = false; |
| 234 |
|
| 235 |
return $args; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Clean variables using sanitize_text_field. Arrays are cleaned recursively. |
| 240 |
* Non-scalar values are ignored. |
| 241 |
* |
| 242 |
* @param string|array $data Data to sanitize. |
| 243 |
* |
| 244 |
* @return string|array |
| 245 |
*/ |
| 246 |
public static function sanitize_clean( $data ) { |
| 247 |
if ( is_array( $data ) ) { |
| 248 |
return array_map( __CLASS__ . '::sanitize_clean', $data ); |
| 249 |
} |
| 250 |
|
| 251 |
return is_scalar( $data ) ? sanitize_text_field( $data ) : $data; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Sanitize conditions |
| 256 |
* |
| 257 |
* @param array $conditions Conditions to sanitize. |
| 258 |
* |
| 259 |
* @return array |
| 260 |
*/ |
| 261 |
public static function sanitize_conditions( $conditions ): array { |
| 262 |
foreach ( $conditions as $index => $condition ) { |
| 263 |
if ( isset( $condition['operator'] ) && in_array( $condition['operator'], [ 'match', 'match_not' ], true ) ) { |
| 264 |
continue; |
| 265 |
} |
| 266 |
// skip paginated_post from value check. |
| 267 |
if ( isset( $condition['type'] ) && 'paginated_post' === $condition['type'] ) { |
| 268 |
continue; |
| 269 |
} |
| 270 |
|
| 271 |
// VC - IP address trim each line and drop empties. |
| 272 |
if ( |
| 273 |
isset( $condition['type'], $condition['value'] ) |
| 274 |
&& 'ip_address' === $condition['type'] |
| 275 |
&& is_string( $condition['value'] ) |
| 276 |
) { |
| 277 |
$condition['value'] = implode( |
| 278 |
"\n", |
| 279 |
array_filter( array_map( 'trim', preg_split( '/\r?\n/', $condition['value'] ) ) ) |
| 280 |
); |
| 281 |
$conditions[ $index ] = $condition; |
| 282 |
} |
| 283 |
|
| 284 |
if ( empty( $condition['value'] ) ) { |
| 285 |
unset( $conditions[ $index ] ); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
return $conditions; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Check if the current user is a bot prepopulating the cache |
| 294 |
* Ads should be loaded for the bot, because they should show up on the cached site |
| 295 |
* |
| 296 |
* @return bool |
| 297 |
*/ |
| 298 |
public static function is_cache_bot(): bool { |
| 299 |
$user_agent = Params::server( 'HTTP_USER_AGENT', '' ); |
| 300 |
if ( '' !== $user_agent ) { |
| 301 |
$current = sanitize_text_field( wp_unslash( $user_agent ) ); |
| 302 |
|
| 303 |
// WP Rocket. |
| 304 |
if ( Str::contains( 'wprocketbot', $current ) ) { |
| 305 |
return true; |
| 306 |
} |
| 307 |
|
| 308 |
// WP Super Cache. |
| 309 |
$wp_useragent = apply_filters( 'http_headers_useragent', 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ) ); |
| 310 |
if ( $current === $wp_useragent ) { |
| 311 |
return true; |
| 312 |
} |
| 313 |
|
| 314 |
// LiteSpeed Cache: `lscache_runner` and `lscache_walker` user agents. |
| 315 |
if ( Str::contains( 'lscache_', $current ) ) { |
| 316 |
return true; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
return false; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Unserializes data only if it was serialized. |
| 325 |
* |
| 326 |
* @link https://patchstack.com/articles/unauthenticated-php-object-injection-in-flatsome-theme-3-17-5/ |
| 327 |
* |
| 328 |
* @param string $data Data that might be unserialized. |
| 329 |
* |
| 330 |
* @return mixed Unserialized data can be any type. |
| 331 |
*/ |
| 332 |
public static function maybe_unserialize( $data ) { |
| 333 |
if ( is_serialized( $data ) ) { // Don't attempt to unserialize data that wasn't serialized going in. |
| 334 |
return @unserialize( trim( $data ), [ 'allowed_classes' => false ] ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
| 335 |
} |
| 336 |
return $data; |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Renders a setting view. |
| 341 |
* |
| 342 |
* @param array $args { |
| 343 |
* An array of arguments. |
| 344 |
* |
| 345 |
* @type string $view The path to the view file to be included. |
| 346 |
* } |
| 347 |
* |
| 348 |
* @return void |
| 349 |
*/ |
| 350 |
public static function render_setting_view( $args ): void { |
| 351 |
include $args['view']; |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Create a wrapper for a single option line |
| 356 |
* |
| 357 |
* @param string $id internal id of the option wrapper. |
| 358 |
* @param string $title label of the option. |
| 359 |
* @param string $content content of the option or full path to template file or custom flag to show a pre-defined information. |
| 360 |
* @param string $description description of the option. |
| 361 |
*/ |
| 362 |
public static function render_option( $id, $title, $content, $description = '' ) { |
| 363 |
/** |
| 364 |
* This filter allows to extend the class dynamically by add-ons |
| 365 |
* this would allow add-ons to dynamically hide/show only attributes belonging to them, practically not used now |
| 366 |
*/ |
| 367 |
$class = apply_filters( 'advanced-ads-option-class', $id ); |
| 368 |
?> |
| 369 |
<div class="advads-option advads-option-<?php echo esc_attr( $class ); ?>"> |
| 370 |
<span><?php echo esc_html( $title ); ?></span> |
| 371 |
<div> |
| 372 |
<?php |
| 373 |
if ( 'is_pro_pitch' === $content ) { // phpcs:ignore |
| 374 |
// Skip this step and place an upgrade link below the description if there is one. |
| 375 |
} elseif ( strlen( $content ) < 500 && file_exists( $content ) ) { // Check length of the string because too long content can break `file_exists`. |
| 376 |
include $content; |
| 377 |
} else { |
| 378 |
// phpcs:ignore |
| 379 |
echo $content; // could include various HTML elements. |
| 380 |
} |
| 381 |
?> |
| 382 |
<?php |
| 383 |
if ( $description ) : |
| 384 |
// phpcs:ignore |
| 385 |
echo '<p class="description">' . $description . '</p>'; // could include various HTML elements. |
| 386 |
endif; |
| 387 |
|
| 388 |
// place an upgrade link below the description if there is one. |
| 389 |
if ( 'is_pro_pitch' === $content ) { |
| 390 |
Upgrades::pro_feature_link( 'upgrade-pro-' . $id ); |
| 391 |
} |
| 392 |
?> |
| 393 |
</div> |
| 394 |
</div> |
| 395 |
<?php |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
* Show a note about a deprecated feature and link to the appropriate page in our manual |
| 400 |
* |
| 401 |
* @param string $feature simple string to indicate the deprecated feature. Will be added to the UTM campaign attribute. |
| 402 |
*/ |
| 403 |
public static function show_deprecated_notice( $feature = '' ) { |
| 404 |
$url = 'https://wpadvancedads.com/manual/deprecated-features/'; |
| 405 |
|
| 406 |
if ( '' !== $feature ) { |
| 407 |
$url .= '#utm_source=advanced-ads&utm_medium=link&utm_campaign=deprecated-' . sanitize_title_for_query( $feature ); |
| 408 |
} |
| 409 |
|
| 410 |
echo '<br/><br/><span class="advads-notice-inline advads-error">'; |
| 411 |
printf( |
| 412 |
/* translators: %1$s is the opening link tag, %2$s is closing link tag */ |
| 413 |
esc_html__( 'This feature is deprecated. Please find the removal schedule %1$shere%2$s', 'advanced-ads' ), |
| 414 |
'<a href="' . esc_url( $url ) . '" target="_blank">', |
| 415 |
'</a>' |
| 416 |
); |
| 417 |
echo '</span>'; |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* Sort visitor and display condition arrays alphabetically by their label. |
| 422 |
* |
| 423 |
* @param array $a array to be compared. |
| 424 |
* @param array $b array to be compared. |
| 425 |
* |
| 426 |
* @return mixed |
| 427 |
*/ |
| 428 |
public static function sort_array_by_label( $a, $b ) { |
| 429 |
if ( ! isset( $a['label'] ) || ! isset( $b['label'] ) ) { |
| 430 |
return; |
| 431 |
} |
| 432 |
|
| 433 |
return strcmp( strtolower( $a['label'] ), strtolower( $b['label'] ) ); |
| 434 |
} |
| 435 |
|
| 436 |
/** |
| 437 |
* Render a manual link |
| 438 |
* |
| 439 |
* @param string $url target URL. |
| 440 |
* @param string $utm_campaign utm_campaign value to attach to the URL. |
| 441 |
* @param string $title link text. |
| 442 |
* |
| 443 |
* @return void |
| 444 |
*/ |
| 445 |
public static function manual_link( $url, $utm_campaign, $title = '' ) { |
| 446 |
$title = ! empty( $title ) ? $title : __( 'Manual', 'advanced-ads' ); |
| 447 |
|
| 448 |
$url = add_query_arg( |
| 449 |
[ |
| 450 |
'utm_source' => 'advanced-ads', |
| 451 |
'utm_medium' => 'link', |
| 452 |
'utm_campaign' => $utm_campaign, |
| 453 |
], |
| 454 |
$url |
| 455 |
); |
| 456 |
|
| 457 |
include ADVADS_ABSPATH . 'views/admin/manual-link.php'; |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* Get installed plugins. |
| 462 |
* |
| 463 |
* @return array |
| 464 |
*/ |
| 465 |
public static function get_wp_plugins(): array { |
| 466 |
wp_cache_delete( 'plugins', 'plugins' ); |
| 467 |
|
| 468 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 469 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 470 |
} |
| 471 |
|
| 472 |
$normalized = []; |
| 473 |
$plugins = \get_plugins(); |
| 474 |
|
| 475 |
foreach ( $plugins as $plugin_file => $plugin_data ) { |
| 476 |
$normalized[ $plugin_data['TextDomain'] ] = [ |
| 477 |
'file' => $plugin_file, |
| 478 |
'version' => $plugin_data['Version'] ?? '0.0.1', |
| 479 |
]; |
| 480 |
} |
| 481 |
|
| 482 |
return $normalized; |
| 483 |
} |
| 484 |
} |
| 485 |
|