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