PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 2.0.24
Advanced Ads – Ad Manager & AdSense v2.0.24
2.0.24 2.0.23 2.0.22 2.0.21 1.38.0 1.39.0 1.39.1 1.39.2 1.39.3 1.39.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.40.0 1.40.1 1.40.2 1.41.0 1.42.0 1.42.1 1.42.2 1.43.0 1.44.0 1.44.1 1.45.0 1.45.1 1.46.0 1.47.0 1.47.1 1.47.2 1.47.3 1.47.4 1.47.5 1.48.0 1.48.1 1.49.0 1.5.0 1.5.0.1 1.5.1 1.5.2 1.5.2.1 1.5.4 1.5.4.1 1.5.5 1.50.0 1.51.0 1.51.1 1.51.2 1.51.3 1.52.0 1.52.1 1.52.2 1.52.3 1.52.4 1.53.0 1.53.1 1.53.2 1.54.0 1.54.1 1.55.0 1.56.0 1.56.1 1.56.2 1.56.3 1.56.4 1.6 1.6.1 1.6.10 1.6.10.1 1.6.10.2 1.6.11 1.6.11.1 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.17.1 1.6.17.2 1.6.2 1.6.2.1 1.6.3 1.6.4 1.6.4.1 1.6.5 1.6.6 1.6.6.1 1.6.7 1.6.7.1 1.6.8 1.6.8.1 1.6.8.2 1.6.8.3 1.6.9 1.6.9.1 1.6.9.2 1.6.9.3 1.6.9.4 1.7 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.1.1 1.7.1.2 1.7.1.3 1.7.1.4 1.7.1.5 1.7.10 trunk 1.7.11 1.0.1 1.7.12 1.0.2 1.7.13 1.0.3 1.7.14 1.1.0 1.7.15 1.1.1 1.7.16 1.1.2 1.7.17 1.1.3 1.7.18 1.10 1.7.19 1.10.1 1.7.2 1.10.10 1.7.2.1 1.10.11 1.7.20 1.10.12 1.7.21 1.10.2 1.7.22 1.10.3 1.7.23 1.10.4 1.7.24 1.10.5 1.7.25 1.10.6 1.7.3 1.10.7 1.7.4 1.10.8 1.7.4.1 1.10.9 1.7.4.2 1.11 1.7.4.3 1.11.1 1.7.4.4 1.11.2 1.7.4.5 1.12 1.7.5 1.13 1.7.5.1 1.13.1 1.7.6 1.13.2 1.7.7 1.13.3 1.7.8 1.13.4 1.7.9 1.13.5 1.7.9.1 1.13.6 1.7.9.2 1.13.7 1.7.9.3 1.13.8 1.8 1.14 1.8.1 1.14.1 1.8.10 1.14.10 1.8.11 1.14.11 1.8.12 1.14.2 1.8.13 1.14.3 1.8.14 1.14.4 1.8.15 1.14.5 1.8.16 1.14.6 1.8.17 1.14.7 1.8.18 1.14.8 1.8.19 1.14.9 1.8.2 1.15 1.8.20 1.16 1.8.21 1.16.1 1.8.22 1.17 1.8.23 1.17.1 1.8.24 1.17.10 1.8.25 1.17.10-rc.1 1.8.26 1.17.11 1.8.27 1.17.12 1.8.28 1.17.12-rc.1 1.8.29 1.17.2 1.8.3 1.17.3 1.8.30 1.17.4 1.8.4 1.17.5 1.8.5 1.17.6 1.8.6 1.17.7 1.8.7 1.17.8 1.8.8 1.17.9 1.8.9 1.17.9-beta.1 1.9 1.18.0 2.0.0 1.19.0 2.0.1 1.19.1 2.0.10 1.2 2.0.11 1.2.1 2.0.12 1.2.2 2.0.13 1.2.3 2.0.14 1.2.4 2.0.15 1.2.5 2.0.16 1.2.6 2.0.17 1.2.7 2.0.18 1.20.0 2.0.19 1.20.0-rc.1 2.0.2 1.20.0-rc.2 2.0.20 1.20.1 2.0.3 1.20.2 2.0.4 1.20.3 2.0.5 1.21.0 2.0.6 1.21.1 2.0.7 1.22.0 2.0.8 1.22.1 2.0.9 1.22.2 1.23.0 1.23.1 1.23.2 1.24.0 1.24.1 1.24.2 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.29.0 1.29.1 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.30.0 1.30.1 1.30.2 1.30.2-rc.1 1.30.3 1.30.4 1.30.4-rc.1 1.30.5 1.31.0 1.31.1 1.32.0 1.32.0-rc.1 1.33.0 1.33.1 1.33.2 1.34.0 1.35.0 1.35.1 1.36.0 1.36.1 1.36.2 1.36.3 1.37.0 1.37.1 1.37.2
advanced-ads / includes / utilities / class-wordpress.php
advanced-ads / includes / utilities Last commit date
class-ad-list-stats.php 1 week ago class-cache.php 1 week ago class-conditional.php 3 months ago class-content-injection.php 1 year ago class-data.php 1 week ago class-sanitize.php 1 year ago class-testing.php 1 year ago class-validation.php 1 year ago class-wordpress.php 1 week ago index.php 2 years ago
class-wordpress.php
498 lines
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 $summaries = wp_advads_get_ad_summaries();
80
81 if ( 'any' === $status ) {
82 return count( $summaries );
83 }
84
85 $count = 0;
86 foreach ( $summaries as $summary ) {
87 if ( $status === $summary['status'] ) {
88 ++$count;
89 }
90 }
91
92 if ( in_array( $status, [ 'publish', 'future', 'draft' ], true ) ) {
93 return $count;
94 }
95
96 $counts = (array) wp_count_posts( Constants::POST_TYPE_AD );
97
98 return $counts[ $status ] ?? 0;
99 }
100
101 /**
102 * Get site domain
103 *
104 * @param string $part Part of domain.
105 *
106 * @return string
107 */
108 public static function get_site_domain( $part = 'host' ): string {
109 $domain = wp_parse_url( home_url( '/' ), PHP_URL_HOST );
110
111 if ( 'name' === $part ) {
112 $domain = explode( '.', $domain );
113 $domain = count( $domain ) > 2 ? $domain[1] : $domain[0];
114 }
115
116 return $domain;
117 }
118
119 /**
120 * Get SVG content as string
121 *
122 * @param string $file File name.
123 * @param string $folder Folder name if not default.
124 *
125 * @return string
126 */
127 public static function get_svg( $file, $folder = '/assets/img/' ): string {
128 $file_path = \untrailingslashit( ADVADS_ABSPATH ) . $folder . $file;
129
130 if ( file_exists( $file_path ) ) {
131 ob_start();
132 include $file_path;
133 return ob_get_clean();
134 }
135
136 return '';
137 }
138
139 /**
140 * Retrieves the timezone of the site as a DateTimeZone object.
141 *
142 * @return DateTimeZone
143 */
144 public static function get_timezone(): DateTimeZone {
145 static $advads_timezone;
146
147 // Early bail!!
148 if ( null !== $advads_timezone ) {
149 return $advads_timezone;
150 }
151
152 if ( function_exists( 'wp_timezone' ) ) {
153 $advads_timezone = wp_timezone();
154 return $advads_timezone;
155 }
156
157 $date_time_zone = new DateTimeZone( self::get_timezone_string() );
158
159 return $date_time_zone;
160 }
161
162 /**
163 * Retrieves the timezone of the site as a string.
164 *
165 * @return string
166 */
167 public static function get_timezone_string(): string {
168 $timezone_string = get_option( 'timezone_string' );
169
170 if ( $timezone_string ) {
171 return $timezone_string;
172 }
173
174 $offset = (float) get_option( 'gmt_offset' );
175 $hours = (int) $offset;
176 $minutes = ( $offset - $hours );
177
178 $sign = ( $offset < 0 ) ? '-' : '+';
179 $abs_hour = abs( $hours );
180 $abs_mins = abs( $minutes * 60 );
181
182 return sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins );
183 }
184
185 /**
186 * Get literal expression of timezone.
187 *
188 * @return string Human readable timezone name.
189 */
190 public static function get_timezone_name(): string {
191 $time_zone = self::get_timezone()->getName();
192 if ( 'UTC' === $time_zone ) {
193 return 'UTC+0';
194 }
195
196 if ( 0 === strpos( $time_zone, '+' ) || 0 === strpos( $time_zone, '-' ) ) {
197 return 'UTC' . $time_zone;
198 }
199
200 /* translators: timezone name */
201 return sprintf( __( 'time of %s', 'advanced-ads' ), $time_zone );
202 }
203
204 /**
205 * Render icon of the type.
206 *
207 * @param string $icon Icon url.
208 *
209 * @return void
210 */
211 public static function render_icon( $icon ): void {
212 printf( '<img src="%s" width="50" height="50" />', esc_url( $icon ) );
213 }
214
215 /**
216 * Applies image loading optimization attributes to an image HTML tag based on WordPress version.
217 *
218 * @param string $img HTML image tag.
219 * @param string $context Image context.
220 *
221 * @return string Updated HTML image tag with loading optimization attributes.
222 */
223 public static function img_tag_add_loading_attr( $img, $context ) {
224 if ( is_array( $context ) ) {
225 $context = end( $context );
226 }
227
228 // Check if the current WordPress version is compatible.
229 if ( is_wp_version_compatible( '6.3' ) ) {
230 return wp_img_tag_add_loading_optimization_attrs( $img, $context );
231 }
232
233 return wp_img_tag_add_loading_attr( $img, $context ); // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_img_tag_add_loading_attrFound
234 }
235
236 /**
237 * Improve WP_Query performance
238 *
239 * @param array $args Query arguments.
240 *
241 * @return array
242 */
243 public static function improve_wp_query( $args ): array {
244 $args['no_found_rows'] = true;
245 $args['update_post_meta_cache'] = false;
246 $args['update_post_term_cache'] = false;
247
248 return $args;
249 }
250
251 /**
252 * Clean variables using sanitize_text_field. Arrays are cleaned recursively.
253 * Non-scalar values are ignored.
254 *
255 * @param string|array $data Data to sanitize.
256 *
257 * @return string|array
258 */
259 public static function sanitize_clean( $data ) {
260 if ( is_array( $data ) ) {
261 return array_map( __CLASS__ . '::sanitize_clean', $data );
262 }
263
264 return is_scalar( $data ) ? sanitize_text_field( $data ) : $data;
265 }
266
267 /**
268 * Sanitize conditions
269 *
270 * @param array $conditions Conditions to sanitize.
271 *
272 * @return array
273 */
274 public static function sanitize_conditions( $conditions ): array {
275 foreach ( $conditions as $index => $condition ) {
276 if ( isset( $condition['operator'] ) && in_array( $condition['operator'], [ 'match', 'match_not' ], true ) ) {
277 continue;
278 }
279 // skip paginated_post from value check.
280 if ( isset( $condition['type'] ) && 'paginated_post' === $condition['type'] ) {
281 continue;
282 }
283
284 // VC - IP address trim each line and drop empties.
285 if (
286 isset( $condition['type'], $condition['value'] )
287 && 'ip_address' === $condition['type']
288 && is_string( $condition['value'] )
289 ) {
290 $condition['value'] = implode(
291 "\n",
292 array_filter( array_map( 'trim', preg_split( '/\r?\n/', $condition['value'] ) ) )
293 );
294 $conditions[ $index ] = $condition;
295 }
296
297 if ( empty( $condition['value'] ) ) {
298 unset( $conditions[ $index ] );
299 }
300 }
301
302 return $conditions;
303 }
304
305 /**
306 * Check if the current user is a bot prepopulating the cache
307 * Ads should be loaded for the bot, because they should show up on the cached site
308 *
309 * @return bool
310 */
311 public static function is_cache_bot(): bool {
312 $user_agent = Params::server( 'HTTP_USER_AGENT', '' );
313 if ( '' !== $user_agent ) {
314 $current = sanitize_text_field( wp_unslash( $user_agent ) );
315
316 // WP Rocket.
317 if ( Str::contains( 'wprocketbot', $current ) ) {
318 return true;
319 }
320
321 // WP Super Cache.
322 $wp_useragent = apply_filters( 'http_headers_useragent', 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ) );
323 if ( $current === $wp_useragent ) {
324 return true;
325 }
326
327 // LiteSpeed Cache: `lscache_runner` and `lscache_walker` user agents.
328 if ( Str::contains( 'lscache_', $current ) ) {
329 return true;
330 }
331 }
332
333 return false;
334 }
335
336 /**
337 * Unserializes data only if it was serialized.
338 *
339 * @link https://patchstack.com/articles/unauthenticated-php-object-injection-in-flatsome-theme-3-17-5/
340 *
341 * @param string $data Data that might be unserialized.
342 *
343 * @return mixed Unserialized data can be any type.
344 */
345 public static function maybe_unserialize( $data ) {
346 if ( is_serialized( $data ) ) { // Don't attempt to unserialize data that wasn't serialized going in.
347 return @unserialize( trim( $data ), [ 'allowed_classes' => false ] ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize
348 }
349 return $data;
350 }
351
352 /**
353 * Renders a setting view.
354 *
355 * @param array $args {
356 * An array of arguments.
357 *
358 * @type string $view The path to the view file to be included.
359 * }
360 *
361 * @return void
362 */
363 public static function render_setting_view( $args ): void {
364 include $args['view'];
365 }
366
367 /**
368 * Create a wrapper for a single option line
369 *
370 * @param string $id internal id of the option wrapper.
371 * @param string $title label of the option.
372 * @param string $content content of the option or full path to template file or custom flag to show a pre-defined information.
373 * @param string $description description of the option.
374 */
375 public static function render_option( $id, $title, $content, $description = '' ) {
376 /**
377 * This filter allows to extend the class dynamically by add-ons
378 * this would allow add-ons to dynamically hide/show only attributes belonging to them, practically not used now
379 */
380 $class = apply_filters( 'advanced-ads-option-class', $id );
381 ?>
382 <div class="advads-option advads-option-<?php echo esc_attr( $class ); ?>">
383 <span><?php echo esc_html( $title ); ?></span>
384 <div>
385 <?php
386 if ( 'is_pro_pitch' === $content ) { // phpcs:ignore
387 // Skip this step and place an upgrade link below the description if there is one.
388 } elseif ( strlen( $content ) < 500 && file_exists( $content ) ) { // Check length of the string because too long content can break `file_exists`.
389 include $content;
390 } else {
391 // phpcs:ignore
392 echo $content; // could include various HTML elements.
393 }
394 ?>
395 <?php
396 if ( $description ) :
397 // phpcs:ignore
398 echo '<p class="description">' . $description . '</p>'; // could include various HTML elements.
399 endif;
400
401 // place an upgrade link below the description if there is one.
402 if ( 'is_pro_pitch' === $content ) {
403 Upgrades::pro_feature_link( 'upgrade-pro-' . $id );
404 }
405 ?>
406 </div>
407 </div>
408 <?php
409 }
410
411 /**
412 * Show a note about a deprecated feature and link to the appropriate page in our manual
413 *
414 * @param string $feature simple string to indicate the deprecated feature. Will be added to the UTM campaign attribute.
415 */
416 public static function show_deprecated_notice( $feature = '' ) {
417 $url = 'https://wpadvancedads.com/manual/deprecated-features/';
418
419 if ( '' !== $feature ) {
420 $url .= '#utm_source=advanced-ads&utm_medium=link&utm_campaign=deprecated-' . sanitize_title_for_query( $feature );
421 }
422
423 echo '<br/><br/><span class="advads-notice-inline advads-error">';
424 printf(
425 /* translators: %1$s is the opening link tag, %2$s is closing link tag */
426 esc_html__( 'This feature is deprecated. Please find the removal schedule %1$shere%2$s', 'advanced-ads' ),
427 '<a href="' . esc_url( $url ) . '" target="_blank">',
428 '</a>'
429 );
430 echo '</span>';
431 }
432
433 /**
434 * Sort visitor and display condition arrays alphabetically by their label.
435 *
436 * @param array $a array to be compared.
437 * @param array $b array to be compared.
438 *
439 * @return mixed
440 */
441 public static function sort_array_by_label( $a, $b ) {
442 if ( ! isset( $a['label'] ) || ! isset( $b['label'] ) ) {
443 return;
444 }
445
446 return strcmp( strtolower( $a['label'] ), strtolower( $b['label'] ) );
447 }
448
449 /**
450 * Render a manual link
451 *
452 * @param string $url target URL.
453 * @param string $utm_campaign utm_campaign value to attach to the URL.
454 * @param string $title link text.
455 *
456 * @return void
457 */
458 public static function manual_link( $url, $utm_campaign, $title = '' ) {
459 $title = ! empty( $title ) ? $title : __( 'Manual', 'advanced-ads' );
460
461 $url = add_query_arg(
462 [
463 'utm_source' => 'advanced-ads',
464 'utm_medium' => 'link',
465 'utm_campaign' => $utm_campaign,
466 ],
467 $url
468 );
469
470 include ADVADS_ABSPATH . 'views/admin/manual-link.php';
471 }
472
473 /**
474 * Get installed plugins.
475 *
476 * @return array
477 */
478 public static function get_wp_plugins(): array {
479 wp_cache_delete( 'plugins', 'plugins' );
480
481 if ( ! function_exists( 'get_plugins' ) ) {
482 require_once ABSPATH . 'wp-admin/includes/plugin.php';
483 }
484
485 $normalized = [];
486 $plugins = \get_plugins();
487
488 foreach ( $plugins as $plugin_file => $plugin_data ) {
489 $normalized[ $plugin_data['TextDomain'] ] = [
490 'file' => $plugin_file,
491 'version' => $plugin_data['Version'] ?? '0.0.1',
492 ];
493 }
494
495 return $normalized;
496 }
497 }
498