PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 1.54.0
Advanced Ads – Ad Manager & AdSense v1.54.0
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 / classes / plugin.php
advanced-ads / classes Last commit date
Advanced_Ads_Modal.php 2 years ago EDD_SL_Plugin_Updater.php 2 years ago ad-ajax.php 2 years ago ad-debug.php 1 year ago ad-expiration.php 3 years ago ad-health-notices.php 2 years ago ad-model.php 2 years ago ad-select.php 3 years ago ad.php 2 years ago ad_ajax_callbacks.php 2 years ago ad_group.php 1 year ago ad_placements.php 2 years ago ad_type_abstract.php 2 years ago ad_type_content.php 2 years ago ad_type_dummy.php 2 years ago ad_type_group.php 2 years ago ad_type_image.php 2 years ago ad_type_plain.php 2 years ago checks.php 2 years ago class-translation-promo.php 2 years ago compatibility.php 2 years ago display-conditions.php 2 years ago filesystem.php 2 years ago frontend_checks.php 1 year ago in-content-injector.php 1 year ago inline-css.php 2 years ago plugin.php 1 year ago upgrades.php 1 year ago utils.php 3 years ago visitor-conditions.php 2 years ago widget.php 2 years ago
plugin.php
726 lines
1 <?php
2 // phpcs:ignoreFile
3
4 use AdvancedAds\Entities;
5 use AdvancedAds\Installation\Capabilities;
6 use AdvancedAds\Utilities\WordPress;
7
8 /**
9 * WordPress integration and definitions:
10 *
11 * - textdomain
12 */
13 class Advanced_Ads_Plugin {
14 /**
15 * Instance of Advanced_Ads_Plugin
16 *
17 * @var object Advanced_Ads_Plugin
18 */
19 protected static $instance;
20
21 /**
22 * Instance of Advanced_Ads_Model
23 *
24 * @var object Advanced_Ads_Model
25 */
26 protected $model;
27
28 /**
29 * Plugin options
30 *
31 * @var array $options
32 */
33 protected $options;
34
35 /**
36 * Interal plugin options – set by the plugin
37 *
38 * @var array $internal_options
39 */
40 protected $internal_options;
41
42 /**
43 * Default prefix of selectors (id, class) in the frontend
44 * can be changed by options
45 *
46 * @var Advanced_Ads_Plugin
47 */
48 const DEFAULT_FRONTEND_PREFIX = 'advads-';
49
50 /**
51 * Frontend prefix for classes and IDs
52 *
53 * @var string $frontend_prefix
54 */
55 private $frontend_prefix;
56
57 /**
58 * Advanced_Ads_Plugin constructor.
59 */
60 private function __construct() {
61 add_action( 'plugins_loaded', [ $this, 'wp_plugins_loaded' ], 20 );
62 add_action( 'init', [ $this, 'run_upgrades' ], 9 );
63 }
64
65 /**
66 * Get instance of Advanced_Ads_Plugin
67 *
68 * @return Advanced_Ads_Plugin
69 */
70 public static function get_instance() {
71 // If the single instance hasn't been set, set it now.
72 if ( null === self::$instance ) {
73 self::$instance = new self();
74 }
75
76 return self::$instance;
77 }
78
79 /**
80 * Get instance of Advanced_Ads_Model
81 *
82 * @param Advanced_Ads_Model $model model to access data.
83 */
84 public function set_model( Advanced_Ads_Model $model ) {
85 $this->model = $model;
86 }
87
88 /**
89 * Execute various hooks after WordPress and all plugins are available
90 */
91 public function wp_plugins_loaded() {
92 // Load public-facing style sheet and JavaScript.
93 add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
94 add_action( 'wp_head', [ $this, 'print_head_scripts' ], 7 );
95 // higher priority to make sure other scripts are printed before.
96 add_action( 'wp_footer', [ $this, 'print_footer_scripts' ], 100 );
97
98 // add short codes.
99 add_shortcode( 'the_ad', [ $this, 'shortcode_display_ad' ] );
100 add_shortcode( 'the_ad_group', [ $this, 'shortcode_display_ad_group' ] );
101 add_shortcode( 'the_ad_placement', [ $this, 'shortcode_display_ad_placement' ] );
102
103 // load widgets.
104 add_action( 'widgets_init', [ $this, 'widget_init' ] );
105
106 // Call action hooks for ad status changes.
107 add_action( 'transition_post_status', [ $this, 'transition_ad_status' ], 10, 3 );
108
109 // register expired post status.
110 Advanced_Ads_Ad_Expiration::register_post_status();
111
112 // if expired ad gets untrashed, revert it to expired status (instead of draft).
113 add_filter( 'wp_untrash_post_status', [ Advanced_Ads_Ad_Expiration::class, 'wp_untrash_post_status' ], 10, 3 );
114
115 // load display conditions.
116 Advanced_Ads_Display_Conditions::get_instance();
117 new Advanced_Ads_Frontend_Checks();
118 new Advanced_Ads_Compatibility();
119 Advanced_Ads_Ad_Health_Notices::get_instance(); // load to fetch notices.
120 }
121
122 /**
123 * Run upgrades.
124 *
125 * Compatibility with the Piklist plugin that has a function hooked to `posts_where` that access $GLOBALS['wp_query'].
126 * Since `Advanced_Ads_Upgrades` applies `posts_where`: (`Advanced_Ads_Admin_Notices::get_instance()` >
127 * `Advanced_Ads::get_number_of_ads()` > new WP_Query > ... 'posts_where') this function is hooked to `init` so that `$GLOBALS['wp_query']` is instantiated.
128 */
129 public function run_upgrades() {
130 /**
131 * Run upgrades, if this is a new version or version does not exist.
132 */
133 $internal_options = $this->internal_options();
134
135 if ( ! defined( 'DOING_AJAX' ) && ( ! isset( $internal_options['version'] ) || version_compare( $internal_options['version'], ADVADS_VERSION, '<' ) ) ) {
136 new Advanced_Ads_Upgrades();
137 }
138 }
139
140 /**
141 * Return the plugin slug.
142 *
143 * @return string plugin slug variable.
144 */
145 public function get_plugin_slug() {
146 return ADVADS_SLUG;
147 }
148
149 /**
150 * Register and enqueues public-facing JavaScript files.
151 */
152 public function enqueue_scripts() {
153 if ( advads_is_amp() ) {
154 return;
155 }
156
157 wp_register_script(
158 $this->get_plugin_slug() . '-advanced-js',
159 sprintf( '%spublic/assets/js/advanced%s.js', ADVADS_BASE_URL, defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min' ),
160 [ 'jquery' ],
161 ADVADS_VERSION,
162 false
163 );
164
165 $privacy = Advanced_Ads_Privacy::get_instance();
166 $privacy_options = $privacy->options();
167 $privacy_options['enabled'] = ! empty( $privacy_options['enabled'] );
168 $privacy_options['state'] = $privacy->get_state();
169
170 wp_localize_script(
171 $this->get_plugin_slug() . '-advanced-js',
172 'advads_options',
173 [
174 'blog_id' => get_current_blog_id(),
175 'privacy' => $privacy_options,
176 ]
177 );
178
179 $activated_js = apply_filters( 'advanced-ads-activate-advanced-js', isset( $this->options()['advanced-js'] ) );
180
181 if ( $activated_js || ! empty( $_COOKIE['advads_frontend_picker'] ) ) {
182 wp_enqueue_script( $this->get_plugin_slug() . '-advanced-js' );
183 }
184
185 wp_register_script(
186 $this->get_plugin_slug() . '-frontend-picker',
187 sprintf( '%spublic/assets/js/frontend-picker%s.js', ADVADS_BASE_URL, defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min' ),
188 [ 'jquery', $this->get_plugin_slug() . '-advanced-js' ],
189 ADVADS_VERSION,
190 false
191 );
192
193 if ( ! empty( $_COOKIE['advads_frontend_picker'] ) ) {
194 wp_enqueue_script( $this->get_plugin_slug() . '-frontend-picker' );
195 }
196 }
197
198 /**
199 * Print public-facing JavaScript in the HTML head.
200 */
201 public function print_head_scripts() {
202 $short_url = self::get_short_url();
203 $attribution = '<!-- ' . $short_url . ' is managing ads with Advanced Ads%1$s%2$s -->';
204 $version = self::is_new_user( 1585224000 ) ? ' ' . ADVADS_VERSION : '';
205 $plugin_url = self::get_group_by_url( $short_url, 'a' ) ? ' – https://wpadvancedads.com/' : '';
206 // escaping would break HTML comment tags so we disable checks here.
207 // phpcs:ignore
208 echo apply_filters( 'advanced-ads-attribution', sprintf( $attribution, $version, $plugin_url ) );
209
210 if ( advads_is_amp() ) {
211 return;
212 }
213
214 $frontend_prefix = $this->get_frontend_prefix();
215
216 ob_start();
217 ?>
218 <script id="<?php echo esc_attr( $frontend_prefix ); ?>ready">
219 <?php
220 readfile( sprintf(
221 '%spublic/assets/js/ready%s.js',
222 ADVADS_ABSPATH,
223 defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'
224 ) );
225 ?>
226 </script>
227 <?php
228
229 /**
230 * Print inline script in the page header form add-ons.
231 *
232 * @param string $frontend_prefix the prefix used for Advanced Ads related HTML ID-s and classes.
233 */
234 do_action( 'advanced_ads_inline_header_scripts', $frontend_prefix );
235 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaping would break the HTML
236 echo Advanced_Ads_Utils::get_inline_asset( ob_get_clean() );
237 }
238
239 /**
240 * Print inline scripts in wp_footer.
241 */
242 public function print_footer_scripts() {
243 if ( advads_is_amp() ) {
244 return;
245 }
246
247 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaping would break the HTML
248 echo Advanced_Ads_Utils::get_inline_asset(
249 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- we're getting the contents of a local file
250 sprintf( '<script>%s</script>', file_get_contents( sprintf(
251 '%spublic/assets/js/ready-queue%s.js',
252 ADVADS_ABSPATH,
253 defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'
254 ) ) )
255 );
256 }
257
258 /**
259 * Register the Advanced Ads widget
260 */
261 public function widget_init() {
262 register_widget( 'Advanced_Ads_Widget' );
263 }
264
265 /**
266 * Shortcode to include ad in frontend
267 *
268 * @param array $atts shortcode attributes.
269 *
270 * @return string ad content.
271 */
272 public function shortcode_display_ad( $atts ) {
273 $atts = is_array( $atts ) ? $atts : [];
274 $id = isset( $atts['id'] ) ? (int) $atts['id'] : 0;
275 // check if there is an inline attribute with or without value.
276 if ( isset( $atts['inline'] ) || in_array( 'inline', $atts, true ) ) {
277 $atts['inline_wrapper_element'] = true;
278 }
279 $atts = $this->prepare_shortcode_atts( $atts );
280
281 // use the public available function here.
282 return get_ad( $id, $atts );
283 }
284
285 /**
286 * Shortcode to include ad from an ad group in frontend
287 *
288 * @param array $atts shortcode attributes.
289 *
290 * @return string ad group content.
291 */
292 public function shortcode_display_ad_group( $atts ) {
293 $atts = is_array( $atts ) ? $atts : [];
294 $id = isset( $atts['id'] ) ? (int) $atts['id'] : 0;
295 $atts = $this->prepare_shortcode_atts( $atts );
296
297 // use the public available function here.
298 return get_ad_group( $id, $atts );
299 }
300
301 /**
302 * Shortcode to display content of an ad placement in frontend
303 *
304 * @param array $atts shortcode attributes.
305 *
306 * @return string ad placement content.
307 */
308 public function shortcode_display_ad_placement( $atts ) {
309 $atts = is_array( $atts ) ? $atts : [];
310 $id = isset( $atts['id'] ) ? (string) $atts['id'] : '';
311 $atts = $this->prepare_shortcode_atts( $atts );
312
313 // use the public available function here.
314 return get_ad_placement( $id, $atts );
315 }
316
317 /**
318 * Prepare shortcode attributes.
319 *
320 * @param array $atts array with strings.
321 *
322 * @return array
323 */
324 private function prepare_shortcode_atts( $atts ) {
325 $result = [];
326
327 /**
328 * Prepare attributes by converting strings to multi-dimensional array
329 * Example: [ 'output__margin__top' => 1 ] => ['output']['margin']['top'] = 1
330 */
331 if ( ! defined( 'ADVANCED_ADS_DISABLE_CHANGE' ) || ! ADVANCED_ADS_DISABLE_CHANGE ) {
332 foreach ( $atts as $attr => $data ) {
333 $levels = explode( '__', $attr );
334 $last = array_pop( $levels );
335
336 $cur_lvl = &$result;
337
338 foreach ( $levels as $lvl ) {
339 if ( ! isset( $cur_lvl[ $lvl ] ) ) {
340 $cur_lvl[ $lvl ] = [];
341 }
342
343 $cur_lvl = &$cur_lvl[ $lvl ];
344 }
345
346 $cur_lvl[ $last ] = $data;
347 }
348
349 $result = array_diff_key(
350 $result,
351 [
352 'id' => false,
353 'blog_id' => false,
354 'ad_args' => false,
355 ]
356 );
357 }
358
359 // Ad type: 'content' and a shortcode inside.
360 if ( isset( $atts['ad_args'] ) ) {
361 $result = array_merge( $result, json_decode( urldecode( $atts['ad_args'] ), true ) );
362
363 }
364
365 return $result;
366 }
367
368 /**
369 * Return plugin options
370 * these are the options updated by the user
371 *
372 * @return array $options
373 */
374 public function options() {
375 // we can’t store options if WPML String Translations is enabled, or it would not translate the "Ad Label" option.
376 if ( ! isset( $this->options ) || class_exists( 'WPML_ST_String' ) ) {
377 $this->options = get_option( ADVADS_SLUG, [] );
378 }
379
380 // allow to change options dynamically
381 $this->options = apply_filters( 'advanced-ads-options', $this->options );
382
383 return $this->options;
384 }
385
386 /**
387 * Update plugin options (not for settings page, but if automatic options are needed)
388 *
389 * @param array $options new options.
390 */
391 public function update_options( array $options ) {
392 // do not allow to clear options.
393 if ( [] === $options ) {
394 return;
395 }
396
397 $this->options = $options;
398 update_option( ADVADS_SLUG, $options );
399 }
400
401 /**
402 * Return internal plugin options
403 * these are options set by the plugin
404 *
405 * @return array $options
406 */
407 public function internal_options() {
408 if ( ! isset( $this->internal_options ) ) {
409 $defaults = [
410 'version' => ADVADS_VERSION,
411 'installed' => time(), // when was this installed.
412 ];
413 $this->internal_options = get_option( ADVADS_SLUG . '-internal', [] );
414
415 // save defaults.
416 if ( [] === $this->internal_options ) {
417 $this->internal_options = $defaults;
418 $this->update_internal_options( $this->internal_options );
419
420 Capabilities::get_instance()->create_capabilities();
421 }
422
423 // for versions installed prior to 1.5.3 set installed date for now.
424 if ( ! isset( $this->internal_options['installed'] ) ) {
425 $this->internal_options['installed'] = time();
426 $this->update_internal_options( $this->internal_options );
427 }
428 }
429
430 return $this->internal_options;
431 }
432
433 /**
434 * Update internal plugin options
435 *
436 * @param array $options new internal options.
437 */
438 public function update_internal_options( array $options ) {
439 // do not allow to clear options.
440 if ( [] === $options ) {
441 return;
442 }
443
444 $this->internal_options = $options;
445 update_option( ADVADS_SLUG . '-internal', $options );
446 }
447
448 /**
449 * Get prefix used for frontend elements
450 *
451 * @return string
452 */
453 public function get_frontend_prefix() {
454 if ( isset( $this->frontend_prefix ) ) {
455 return $this->frontend_prefix;
456 }
457
458 $options = $this->options();
459
460 if ( ! isset( $options['front-prefix'] ) ) {
461 if ( isset( $options['id-prefix'] ) ) {
462 // deprecated: keeps widgets working that previously received an id based on the front-prefix.
463 $frontend_prefix = $options['id-prefix'];
464 } else {
465 $frontend_prefix = preg_match( '/[A-Za-z][A-Za-z0-9_]{4}/', parse_url( get_home_url(), PHP_URL_HOST ), $result )
466 ? $result[0] . '-'
467 : self::DEFAULT_FRONTEND_PREFIX;
468 }
469 } else {
470 $frontend_prefix = $options['front-prefix'];
471 }
472 /**
473 * Applying the filter here makes sure that it is the same frontend prefix for all
474 * calls on this page impression
475 *
476 * @param string $frontend_prefix
477 */
478 $this->frontend_prefix = (string) apply_filters( 'advanced-ads-frontend-prefix', $frontend_prefix );
479 $this->frontend_prefix = $this->sanitize_frontend_prefix( $frontend_prefix );
480
481 return $this->frontend_prefix;
482 }
483
484 /**
485 * Sanitize the frontend prefix to result in valid HTML classes.
486 * See https://www.w3.org/TR/selectors-3/#grammar for valid tokens.
487 *
488 * @param string $prefix The HTML class to sanitize.
489 * @param string $fallback The fallback if the class is invalid.
490 *
491 * @return string
492 */
493 public function sanitize_frontend_prefix( $prefix, $fallback = '' ) {
494 $prefix = sanitize_html_class( $prefix );
495 $nonascii = '[^\0-\177]';
496 $unicode = '\\[0-9a-f]{1,6}(\r\n|[ \n\r\t\f])?';
497 $escape = sprintf( '%s|\\[^\n\r\f0-9a-f]', $unicode );
498 $nmstart = sprintf( '[_a-z]|%s|%s', $nonascii, $escape );
499 $nmchar = sprintf( '[_a-z0-9-]|%s|%s', $nonascii, $escape );
500
501 if ( ! preg_match( sprintf( '/-?(?:%s)(?:%s)*/i', $nmstart, $nmchar ), $prefix, $matches ) ) {
502 return $fallback;
503 }
504
505 return $matches[0];
506 }
507
508 /**
509 * Get priority used for injection inside content
510 */
511 public function get_content_injection_priority() {
512 $options = $this->options();
513
514 return isset( $options['content-injection-priority'] ) ? (int) $options['content-injection-priority'] : 100;
515 }
516
517 /**
518 * Returns the capability needed to perform an action
519 *
520 * @deprecated 1.47.0
521 *
522 * @param string $capability a capability to check, can be internal to Advanced Ads.
523 *
524 * @return string $capability a valid WordPress capability.
525 */
526 public static function user_cap( $capability = 'manage_options' ) {
527 _deprecated_function( __METHOD__, '1.47.0', '\AdvancedAds\Utilities\WordPress::user_cap()' );
528 return WordPress::user_cap( $capability );
529 }
530
531 /**
532 * Create roles and capabilities
533 *
534 * @deprecated 1.47.0
535 */
536 public function create_capabilities() {
537 _deprecated_function( __METHOD__, '1.47.0', 'AdvancedAds\Installation\Capabilities::get_instance()->create_capabilities();' );
538
539 Capabilities::get_instance()->create_capabilities();
540 }
541
542 /**
543 * Remove roles and capabilities
544 *
545 * @deprecated 1.47.0
546 */
547 public function remove_capabilities() {
548 _deprecated_function( __METHOD__, '1.47.0', 'AdvancedAds\Installation\Capabilities::remove_capabilities()' );
549
550 ( new Capabilities() )->remove_capabilities();
551 }
552
553 /**
554 * Check if any add-on is activated
555 *
556 * @return bool true if there is any add-on activated
557 */
558 public static function any_activated_add_on() {
559 return ( defined( 'AAP_VERSION' ) // Advanced Ads Pro.
560 || defined( 'AAGAM_VERSION' ) // Google Ad Manager.
561 || defined( 'AASA_VERSION' ) // Selling Ads.
562 || defined( 'AAT_VERSION' ) // Tracking.
563 || defined( 'AASADS_VERSION' ) // Sticky Ads.
564 || defined( 'AAR_VERSION' ) // Responsive Ads.
565 || defined( 'AAPLDS_VERSION' ) // PopUp and Layer Ads.
566 );
567 }
568
569 /**
570 * Get the correct support URL: wp.org for free users and website for those with any add-on installed
571 *
572 * @param string $utm add UTM parameter to the link leading to https://wpadvancedads.com, if given.
573 *
574 * @return string URL.
575 */
576 public static function support_url( $utm = '' ) {
577
578 $utm = empty( $utm ) ? '?utm_source=advanced-ads&utm_medium=link&utm_campaign=support' : $utm;
579 if ( self::any_activated_add_on() ) {
580 $url = 'https://wpadvancedads.com/support/' . $utm . '-with-addons';
581 } else {
582 $url = 'https://wpadvancedads.com/support/' . $utm . '-free-user';
583 }
584
585 return $url;
586 }
587
588 /**
589 * Create a random group
590 *
591 * @param string $url optional parameter.
592 * @param string $ex group.
593 *
594 * @return bool
595 */
596 public static function get_group_by_url( $url = '', $ex = 'a' ) {
597
598 $url = self::get_short_url( $url );
599
600 $code = (int)substr( md5( $url ), - 1 );
601
602 switch ( $ex ) {
603 case 'b':
604 return ( $code & 2 ) >> 1; // returns 1 or 0.
605 case 'c':
606 return ( $code & 4 ) >> 2; // returns 1 or 0.
607 case 'd':
608 return ( $code & 8 ) >> 3; // returns 1 or 0.
609 default:
610 return $code & 1; // returns 1 or 0.
611 }
612 }
613
614 /**
615 * Check if user started after a given date
616 *
617 * @param integer $timestamp time stamp.
618 *
619 * @return bool true if user is added after timestamp.
620 */
621 public static function is_new_user( $timestamp = 0 ) {
622
623 // allow admins to see version for new users in any case.
624 if ( WordPress::user_can( 'advanced_ads_manage_options' ) && isset( $_REQUEST['advads-ignore-timestamp'] ) ) {
625 return true;
626 }
627
628 $timestamp = absint( $timestamp );
629
630 $options = self::get_instance()->internal_options();
631 $installed = isset( $options['installed'] ) ? $options['installed'] : 0;
632
633 return ( $installed >= $timestamp );
634 }
635
636 /**
637 * Show stuff to new users only.
638 *
639 * @param integer $timestamp time after which to show whatever.
640 * @param string $group optional group.
641 *
642 * @return bool true if user enabled after given timestamp.
643 */
644 public static function show_to_new_users( $timestamp, $group = 'a' ) {
645
646 return ( self::get_group_by_url( null, $group ) && self::is_new_user( $timestamp ) );
647 }
648
649 /**
650 * Get short version of home_url()
651 * remove protocol and www
652 * remove slash
653 *
654 * @param string $url URL to be shortened.
655 *
656 * @return string
657 */
658 public static function get_short_url( $url = '' ) {
659
660 $url = empty( $url ) ? home_url() : $url;
661
662 // strip protocols.
663 if ( preg_match( '/^(\w[\w\d]*:\/\/)?(www\.)?(.*)$/', trim( $url ), $matches ) ) {
664 $url = $matches[3];
665 }
666
667 // strip slashes.
668 $url = trim( $url, '/' );
669
670 return $url;
671 }
672
673 /**
674 * Fires when a post is transitioned from one status to another.
675 *
676 * @param string $new_status New post status.
677 * @param string $old_status Old post status.
678 * @param WP_Post $post Post object.
679 */
680 public function transition_ad_status( $new_status, $old_status, $post ) {
681 if ( ! isset( $post->post_type ) || Entities::POST_TYPE_AD !== $post->post_type || ! isset( $post->ID ) ) {
682 return;
683 }
684
685 $ad = \Advanced_Ads\Ad_Repository::get( $post->ID );
686
687 if ( $old_status !== $new_status ) {
688 /**
689 * Fires when an ad has transitioned from one status to another.
690 *
691 * @param Advanced_Ads_Ad $ad Ad object.
692 */
693 do_action( "advanced-ads-ad-status-{$old_status}-to-{$new_status}", $ad );
694 }
695
696 if ( 'publish' === $new_status && 'publish' !== $old_status ) {
697 /**
698 * Fires when an ad has transitioned from any other status to `publish`.
699 *
700 * @param Advanced_Ads_Ad $ad Ad object.
701 */
702 do_action( 'advanced-ads-ad-status-published', $ad );
703 }
704
705 if ( 'publish' === $old_status && 'publish' !== $new_status ) {
706 /**
707 * Fires when an ad has transitioned from `publish` to any other status.
708 *
709 * @param Advanced_Ads_Ad $ad Ad object.
710 */
711 do_action( 'advanced-ads-ad-status-unpublished', $ad );
712 }
713
714 if ( $old_status === 'publish' && $new_status === Advanced_Ads_Ad_Expiration::POST_STATUS ) {
715 /**
716 * Fires when an ad is expired.
717 *
718 * @param int $id
719 * @param Advanced_Ads_Ad $ad
720 */
721 do_action( 'advanced-ads-ad-expired', $ad->id, $ad );
722 }
723 }
724
725 }
726