Advanced_Ads_Modal.php
2 years ago
EDD_SL_Plugin_Updater.php
4 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
3 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
728 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' ) ? ' – ' . ADVADS_URL : ''; |
| 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 = ADVADS_URL . 'support/' . $utm . '-with-addons'; |
| 582 | } else { |
| 583 | $url = ADVADS_URL . '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 ( current_user_can( self::user_cap( 'advanced_ads_manage_options' ) ) |
| 626 | && isset( $_REQUEST['advads-ignore-timestamp'] ) ) { |
| 627 | return true; |
| 628 | } |
| 629 | |
| 630 | $timestamp = absint( $timestamp ); |
| 631 | |
| 632 | $options = self::get_instance()->internal_options(); |
| 633 | $installed = isset( $options['installed'] ) ? $options['installed'] : 0; |
| 634 | |
| 635 | return ( $installed >= $timestamp ); |
| 636 | } |
| 637 | |
| 638 | /** |
| 639 | * Show stuff to new users only. |
| 640 | * |
| 641 | * @param integer $timestamp time after which to show whatever. |
| 642 | * @param string $group optional group. |
| 643 | * |
| 644 | * @return bool true if user enabled after given timestamp. |
| 645 | */ |
| 646 | public static function show_to_new_users( $timestamp, $group = 'a' ) { |
| 647 | |
| 648 | return ( self::get_group_by_url( null, $group ) && self::is_new_user( $timestamp ) ); |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * Get short version of home_url() |
| 653 | * remove protocol and www |
| 654 | * remove slash |
| 655 | * |
| 656 | * @param string $url URL to be shortened. |
| 657 | * |
| 658 | * @return string |
| 659 | */ |
| 660 | public static function get_short_url( $url = '' ) { |
| 661 | |
| 662 | $url = empty( $url ) ? home_url() : $url; |
| 663 | |
| 664 | // strip protocols. |
| 665 | if ( preg_match( '/^(\w[\w\d]*:\/\/)?(www\.)?(.*)$/', trim( $url ), $matches ) ) { |
| 666 | $url = $matches[3]; |
| 667 | } |
| 668 | |
| 669 | // strip slashes. |
| 670 | $url = trim( $url, '/' ); |
| 671 | |
| 672 | return $url; |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Fires when a post is transitioned from one status to another. |
| 677 | * |
| 678 | * @param string $new_status New post status. |
| 679 | * @param string $old_status Old post status. |
| 680 | * @param WP_Post $post Post object. |
| 681 | */ |
| 682 | public function transition_ad_status( $new_status, $old_status, $post ) { |
| 683 | if ( ! isset( $post->post_type ) || Entities::POST_TYPE_AD !== $post->post_type || ! isset( $post->ID ) ) { |
| 684 | return; |
| 685 | } |
| 686 | |
| 687 | $ad = \Advanced_Ads\Ad_Repository::get( $post->ID ); |
| 688 | |
| 689 | if ( $old_status !== $new_status ) { |
| 690 | /** |
| 691 | * Fires when an ad has transitioned from one status to another. |
| 692 | * |
| 693 | * @param Advanced_Ads_Ad $ad Ad object. |
| 694 | */ |
| 695 | do_action( "advanced-ads-ad-status-{$old_status}-to-{$new_status}", $ad ); |
| 696 | } |
| 697 | |
| 698 | if ( 'publish' === $new_status && 'publish' !== $old_status ) { |
| 699 | /** |
| 700 | * Fires when an ad has transitioned from any other status to `publish`. |
| 701 | * |
| 702 | * @param Advanced_Ads_Ad $ad Ad object. |
| 703 | */ |
| 704 | do_action( 'advanced-ads-ad-status-published', $ad ); |
| 705 | } |
| 706 | |
| 707 | if ( 'publish' === $old_status && 'publish' !== $new_status ) { |
| 708 | /** |
| 709 | * Fires when an ad has transitioned from `publish` to any other status. |
| 710 | * |
| 711 | * @param Advanced_Ads_Ad $ad Ad object. |
| 712 | */ |
| 713 | do_action( 'advanced-ads-ad-status-unpublished', $ad ); |
| 714 | } |
| 715 | |
| 716 | if ( $old_status === 'publish' && $new_status === Advanced_Ads_Ad_Expiration::POST_STATUS ) { |
| 717 | /** |
| 718 | * Fires when an ad is expired. |
| 719 | * |
| 720 | * @param int $id |
| 721 | * @param Advanced_Ads_Ad $ad |
| 722 | */ |
| 723 | do_action( 'advanced-ads-ad-expired', $ad->id, $ad ); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | } |
| 728 |