PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 2.0.25
Advanced Ads – Ad Manager & AdSense v2.0.25
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-ad-list-stats.php 1 week ago class-addons.php 1 day ago class-cache.php 1 week ago class-conditional.php 1 day ago class-content-injection.php 1 year ago class-data.php 1 day ago class-sanitize.php 1 year ago class-testing.php 1 year ago class-validation.php 1 day ago class-wordpress.php 1 day ago index.php 2 years ago
class-wordpress.php
415 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 * Applies image loading optimization attributes to an image HTML tag based on WordPress version.
206 *
207 * @param string $img HTML image tag.
208 * @param string $context Image context.
209 *
210 * @return string Updated HTML image tag with loading optimization attributes.
211 */
212 public static function img_tag_add_loading_attr( $img, $context ) {
213 if ( is_array( $context ) ) {
214 $context = end( $context );
215 }
216
217 // Check if the current WordPress version is compatible.
218 if ( is_wp_version_compatible( '6.3' ) ) {
219 return wp_img_tag_add_loading_optimization_attrs( $img, $context );
220 }
221
222 return wp_img_tag_add_loading_attr( $img, $context ); // phpcs:ignore WordPress.WP.DeprecatedFunctions.wp_img_tag_add_loading_attrFound
223 }
224
225 /**
226 * Improve WP_Query performance
227 *
228 * @param array $args Query arguments.
229 *
230 * @return array
231 */
232 public static function improve_wp_query( $args ): array {
233 $args['no_found_rows'] = true;
234 $args['update_post_meta_cache'] = false;
235 $args['update_post_term_cache'] = false;
236
237 return $args;
238 }
239
240 /**
241 * Sanitize conditions
242 *
243 * @param array $conditions Conditions to sanitize.
244 *
245 * @return array
246 */
247 public static function sanitize_conditions( $conditions ): array {
248 foreach ( $conditions as $index => $condition ) {
249 if ( isset( $condition['operator'] ) && in_array( $condition['operator'], [ 'match', 'match_not' ], true ) ) {
250 continue;
251 }
252 // skip paginated_post from value check.
253 if ( isset( $condition['type'] ) && 'paginated_post' === $condition['type'] ) {
254 continue;
255 }
256
257 // VC - IP address trim each line and drop empties.
258 if (
259 isset( $condition['type'], $condition['value'] )
260 && 'ip_address' === $condition['type']
261 && is_string( $condition['value'] )
262 ) {
263 $condition['value'] = implode(
264 "\n",
265 array_filter( array_map( 'trim', preg_split( '/\r?\n/', $condition['value'] ) ) )
266 );
267 $conditions[ $index ] = $condition;
268 }
269
270 if ( empty( $condition['value'] ) ) {
271 unset( $conditions[ $index ] );
272 }
273 }
274
275 return $conditions;
276 }
277
278 /**
279 * Check if the current user is a bot prepopulating the cache
280 * Ads should be loaded for the bot, because they should show up on the cached site
281 *
282 * @return bool
283 */
284 public static function is_cache_bot(): bool {
285 $user_agent = Params::server( 'HTTP_USER_AGENT', '' );
286 if ( '' !== $user_agent ) {
287 $current = sanitize_text_field( wp_unslash( $user_agent ) );
288
289 // WP Rocket.
290 if ( Str::contains( 'wprocketbot', $current ) ) {
291 return true;
292 }
293
294 // WP Super Cache.
295 $wp_useragent = apply_filters( 'http_headers_useragent', 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ) );
296 if ( $current === $wp_useragent ) {
297 return true;
298 }
299
300 // LiteSpeed Cache: `lscache_runner` and `lscache_walker` user agents.
301 if ( Str::contains( 'lscache_', $current ) ) {
302 return true;
303 }
304 }
305
306 return false;
307 }
308
309 /**
310 * Unserializes data only if it was serialized.
311 *
312 * @link https://patchstack.com/articles/unauthenticated-php-object-injection-in-flatsome-theme-3-17-5/
313 *
314 * @param string $data Data that might be unserialized.
315 *
316 * @return mixed Unserialized data can be any type.
317 */
318 public static function maybe_unserialize( $data ) {
319 if ( is_serialized( $data ) ) { // Don't attempt to unserialize data that wasn't serialized going in.
320 return @unserialize( trim( $data ), [ 'allowed_classes' => false ] ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize
321 }
322 return $data;
323 }
324
325 /**
326 * Create a wrapper for a single option line
327 *
328 * @param string $id internal id of the option wrapper.
329 * @param string $title label of the option.
330 * @param string $content content of the option or full path to template file or custom flag to show a pre-defined information.
331 * @param string $description description of the option.
332 */
333 public static function render_option( $id, $title, $content, $description = '' ) {
334 /**
335 * This filter allows to extend the class dynamically by add-ons
336 * this would allow add-ons to dynamically hide/show only attributes belonging to them, practically not used now
337 */
338 $class = apply_filters( 'advanced-ads-option-class', $id );
339 ?>
340 <div class="advads-option advads-option-<?php echo esc_attr( $class ); ?>">
341 <span><?php echo esc_html( $title ); ?></span>
342 <div>
343 <?php
344 if ( 'is_pro_pitch' === $content ) { // phpcs:ignore
345 // Skip this step and place an upgrade link below the description if there is one.
346 } elseif ( strlen( $content ) < 500 && file_exists( $content ) ) { // Check length of the string because too long content can break `file_exists`.
347 include $content;
348 } else {
349 // phpcs:ignore
350 echo $content; // could include various HTML elements.
351 }
352 ?>
353 <?php
354 if ( $description ) :
355 // phpcs:ignore
356 echo '<p class="description">' . $description . '</p>'; // could include various HTML elements.
357 endif;
358
359 // place an upgrade link below the description if there is one.
360 if ( 'is_pro_pitch' === $content ) {
361 Upgrades::pro_feature_link( 'upgrade-pro-' . $id );
362 }
363 ?>
364 </div>
365 </div>
366 <?php
367 }
368
369 /**
370 * Show a note about a deprecated feature and link to the appropriate page in our manual
371 *
372 * @param string $feature simple string to indicate the deprecated feature. Will be added to the UTM campaign attribute.
373 */
374 public static function show_deprecated_notice( $feature = '' ) {
375 $url = 'https://wpadvancedads.com/manual/deprecated-features/';
376
377 if ( '' !== $feature ) {
378 $url .= '#utm_source=advanced-ads&utm_medium=link&utm_campaign=deprecated-' . sanitize_title_for_query( $feature );
379 }
380
381 echo '<br/><br/><span class="advads-notice-inline advads-error">';
382 printf(
383 /* translators: %1$s is the opening link tag, %2$s is closing link tag */
384 esc_html__( 'This feature is deprecated. Please find the removal schedule %1$shere%2$s', 'advanced-ads' ),
385 '<a href="' . esc_url( $url ) . '" target="_blank">',
386 '</a>'
387 );
388 echo '</span>';
389 }
390
391 /**
392 * Render a manual link
393 *
394 * @param string $url target URL.
395 * @param string $utm_campaign utm_campaign value to attach to the URL.
396 * @param string $title link text.
397 *
398 * @return void
399 */
400 public static function manual_link( $url, $utm_campaign, $title = '' ) {
401 $title = ! empty( $title ) ? $title : __( 'Manual', 'advanced-ads' );
402
403 $url = add_query_arg(
404 [
405 'utm_source' => 'advanced-ads',
406 'utm_medium' => 'link',
407 'utm_campaign' => $utm_campaign,
408 ],
409 $url
410 );
411
412 include ADVADS_ABSPATH . 'views/admin/manual-link.php';
413 }
414 }
415