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