PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 2.0.5
Advanced Ads – Ad Manager & AdSense v2.0.5
2.0.26 2.0.25 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-conditional.php 1 year ago class-content-injection.php 1 year ago class-data.php 1 year ago class-sanitize.php 1 year ago class-testing.php 1 year ago class-validation.php 1 year ago class-wordpress.php 1 year ago index.php 2 years ago
class-wordpress.php
468 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 $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
267 if ( empty( $condition['value'] ) ) {
268 unset( $conditions[ $index ] );
269 }
270 }
271
272 return $conditions;
273 }
274
275 /**
276 * Check if the current user is a bot prepopulating the cache
277 * Ads should be loaded for the bot, because they should show up on the cached site
278 *
279 * @return bool
280 */
281 public static function is_cache_bot(): bool {
282 $user_agent = Params::server( 'HTTP_USER_AGENT', '' );
283 if ( '' !== $user_agent ) {
284 $current = sanitize_text_field( wp_unslash( $user_agent ) );
285
286 // WP Rocket.
287 if ( Str::contains( 'wprocketbot', $current ) ) {
288 return true;
289 }
290
291 // WP Super Cache.
292 $wp_useragent = apply_filters( 'http_headers_useragent', 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ) );
293 if ( $current === $wp_useragent ) {
294 return true;
295 }
296
297 // LiteSpeed Cache: `lscache_runner` and `lscache_walker` user agents.
298 if ( Str::contains( 'lscache_', $current ) ) {
299 return true;
300 }
301 }
302
303 return false;
304 }
305
306 /**
307 * Unserializes data only if it was serialized.
308 *
309 * @link https://patchstack.com/articles/unauthenticated-php-object-injection-in-flatsome-theme-3-17-5/
310 *
311 * @param string $data Data that might be unserialized.
312 *
313 * @return mixed Unserialized data can be any type.
314 */
315 public static function maybe_unserialize( $data ) {
316 if ( is_serialized( $data ) ) { // Don't attempt to unserialize data that wasn't serialized going in.
317 return @unserialize( trim( $data ), [ 'allowed_classes' => false ] ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize
318 }
319 return $data;
320 }
321
322 /**
323 * Renders a setting view.
324 *
325 * @param array $args {
326 * An array of arguments.
327 *
328 * @type string $view The path to the view file to be included.
329 * }
330 *
331 * @return void
332 */
333 public static function render_setting_view( $args ): void {
334 include $args['view'];
335 }
336
337 /**
338 * Create a wrapper for a single option line
339 *
340 * @param string $id internal id of the option wrapper.
341 * @param string $title label of the option.
342 * @param string $content content of the option or full path to template file or custom flag to show a pre-defined information.
343 * @param string $description description of the option.
344 */
345 public static function render_option( $id, $title, $content, $description = '' ) {
346 /**
347 * This filter allows to extend the class dynamically by add-ons
348 * this would allow add-ons to dynamically hide/show only attributes belonging to them, practically not used now
349 */
350 $class = apply_filters( 'advanced-ads-option-class', $id );
351 ?>
352 <div class="advads-option advads-option-<?php echo esc_attr( $class ); ?>">
353 <span><?php echo esc_html( $title ); ?></span>
354 <div>
355 <?php
356 if ( 'is_pro_pitch' === $content ) { // phpcs:ignore
357 // Skip this step and place an upgrade link below the description if there is one.
358 } elseif ( strlen( $content ) < 500 && file_exists( $content ) ) { // Check length of the string because too long content can break `file_exists`.
359 include $content;
360 } else {
361 // phpcs:ignore
362 echo $content; // could include various HTML elements.
363 }
364 ?>
365 <?php
366 if ( $description ) :
367 // phpcs:ignore
368 echo '<p class="description">' . $description . '</p>'; // could include various HTML elements.
369 endif;
370
371 // place an upgrade link below the description if there is one.
372 if ( 'is_pro_pitch' === $content ) {
373 Upgrades::pro_feature_link( 'upgrade-pro-' . $id );
374 }
375 ?>
376 </div>
377 </div>
378 <?php
379 }
380
381 /**
382 * Show a note about a deprecated feature and link to the appropriate page in our manual
383 *
384 * @param string $feature simple string to indicate the deprecated feature. Will be added to the UTM campaign attribute.
385 */
386 public static function show_deprecated_notice( $feature = '' ) {
387 $url = 'https://wpadvancedads.com/manual/deprecated-features/';
388
389 if ( '' !== $feature ) {
390 $url .= '#utm_source=advanced-ads&utm_medium=link&utm_campaign=deprecated-' . sanitize_title_for_query( $feature );
391 }
392
393 echo '<br/><br/><span class="advads-notice-inline advads-error">';
394 printf(
395 /* translators: %1$s is the opening link tag, %2$s is closing link tag */
396 esc_html__( 'This feature is deprecated. Please find the removal schedule %1$shere%2$s', 'advanced-ads' ),
397 '<a href="' . esc_url( $url ) . '" target="_blank">',
398 '</a>'
399 );
400 echo '</span>';
401 }
402
403 /**
404 * Sort visitor and display condition arrays alphabetically by their label.
405 *
406 * @param array $a array to be compared.
407 * @param array $b array to be compared.
408 *
409 * @return mixed
410 */
411 public static function sort_array_by_label( $a, $b ) {
412 if ( ! isset( $a['label'] ) || ! isset( $b['label'] ) ) {
413 return;
414 }
415
416 return strcmp( strtolower( $a['label'] ), strtolower( $b['label'] ) );
417 }
418
419 /**
420 * Render a manual link
421 *
422 * @param string $url target URL.
423 * @param string $utm_campaign utm_campaign value to attach to the URL.
424 * @param string $title link text.
425 *
426 * @return void
427 */
428 public static function manual_link( $url, $utm_campaign, $title = '' ) {
429 $title = ! empty( $title ) ? $title : __( 'Manual', 'advanced-ads' );
430
431 $url = add_query_arg(
432 [
433 'utm_source' => 'advanced-ads',
434 'utm_medium' => 'link',
435 'utm_campaign' => $utm_campaign,
436 ],
437 $url
438 );
439
440 include ADVADS_ABSPATH . 'views/admin/manual-link.php';
441 }
442
443 /**
444 * Get installed plugins.
445 *
446 * @return array
447 */
448 public static function get_wp_plugins(): array {
449 wp_cache_delete( 'plugins', 'plugins' );
450
451 if ( ! function_exists( 'get_plugins' ) ) {
452 require_once ABSPATH . 'wp-admin/includes/plugin.php';
453 }
454
455 $normalized = [];
456 $plugins = \get_plugins();
457
458 foreach ( $plugins as $plugin_file => $plugin_data ) {
459 $normalized[ $plugin_data['TextDomain'] ] = [
460 'file' => $plugin_file,
461 'version' => $plugin_data['Version'] ?? '0.0.1',
462 ];
463 }
464
465 return $normalized;
466 }
467 }
468