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