EDD_SL_Plugin_Updater.php
6 years ago
ad-ajax.php
6 years ago
ad-debug.php
6 years ago
ad-health-notices.php
6 years ago
ad-model.php
5 years ago
ad-select.php
9 years ago
ad.php
5 years ago
ad_ajax_callbacks.php
5 years ago
ad_group.php
6 years ago
ad_placements.php
5 years ago
ad_type_abstract.php
5 years ago
ad_type_content.php
5 years ago
ad_type_dummy.php
5 years ago
ad_type_group.php
6 years ago
ad_type_image.php
5 years ago
ad_type_plain.php
5 years ago
checks.php
6 years ago
compatibility.php
5 years ago
display-conditions.php
5 years ago
filesystem.php
8 years ago
frontend-notices.php
6 years ago
frontend_checks.php
5 years ago
plugin.php
5 years ago
upgrades.php
6 years ago
utils.php
6 years ago
visitor-conditions.php
6 years ago
widget.php
6 years ago
plugin.php
836 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * WordPress integration and definitions: |
| 5 | * |
| 6 | * - posttypes |
| 7 | * - taxonomy |
| 8 | * - textdomain |
| 9 | */ |
| 10 | class Advanced_Ads_Plugin { |
| 11 | /** |
| 12 | * Instance of Advanced_Ads_Plugin |
| 13 | * |
| 14 | * @var object Advanced_Ads_Plugin |
| 15 | */ |
| 16 | protected static $instance; |
| 17 | |
| 18 | /** |
| 19 | * Instance of Advanced_Ads_Model |
| 20 | * |
| 21 | * @var object Advanced_Ads_Model |
| 22 | */ |
| 23 | protected $model; |
| 24 | |
| 25 | /** |
| 26 | * Plugin options |
| 27 | * |
| 28 | * @var array $options |
| 29 | */ |
| 30 | protected $options; |
| 31 | |
| 32 | /** |
| 33 | * Interal plugin options – set by the plugin |
| 34 | * |
| 35 | * @var array $internal_options |
| 36 | */ |
| 37 | protected $internal_options; |
| 38 | |
| 39 | /** |
| 40 | * Default prefix of selectors (id, class) in the frontend |
| 41 | * can be changed by options |
| 42 | * |
| 43 | * @var Advanced_Ads_Plugin |
| 44 | */ |
| 45 | const DEFAULT_FRONTEND_PREFIX = 'advads-'; |
| 46 | |
| 47 | /** |
| 48 | * Frontend prefix for classes and IDs |
| 49 | * |
| 50 | * @var string $frontend_prefix |
| 51 | */ |
| 52 | private $frontend_prefix; |
| 53 | |
| 54 | /** |
| 55 | * Advanced_Ads_Plugin constructor. |
| 56 | */ |
| 57 | private function __construct() { |
| 58 | register_activation_hook( ADVADS_BASE, array( $this, 'activate' ) ); |
| 59 | register_deactivation_hook( ADVADS_BASE, array( $this, 'deactivate' ) ); |
| 60 | register_uninstall_hook( ADVADS_BASE, array( 'Advanced_Ads_Plugin', 'uninstall' ) ); |
| 61 | |
| 62 | add_action( 'plugins_loaded', array( $this, 'wp_plugins_loaded' ), 10 ); |
| 63 | add_action( 'init', array( $this, 'run_upgrades' ), 9 ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get instance of Advanced_Ads_Plugin |
| 68 | * |
| 69 | * @return Advanced_Ads_Plugin |
| 70 | */ |
| 71 | public static function get_instance() { |
| 72 | // If the single instance hasn't been set, set it now. |
| 73 | if ( null === self::$instance ) { |
| 74 | self::$instance = new self(); |
| 75 | } |
| 76 | |
| 77 | return self::$instance; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get instance of Advanced_Ads_Model |
| 82 | * |
| 83 | * @param Advanced_Ads_Model $model model to access data. |
| 84 | */ |
| 85 | public function set_model( Advanced_Ads_Model $model ) { |
| 86 | $this->model = $model; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Execute various hooks after WordPress and all plugins are available |
| 91 | */ |
| 92 | public function wp_plugins_loaded() { |
| 93 | // Load plugin text domain. |
| 94 | $this->load_plugin_textdomain(); |
| 95 | |
| 96 | // activate plugin when new blog is added on multisites // -TODO this is admin-only. |
| 97 | add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) ); |
| 98 | |
| 99 | // Load public-facing style sheet and JavaScript. |
| 100 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); |
| 101 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 102 | add_action( 'wp_head', array( $this, 'print_head_scripts' ), 7 ); |
| 103 | |
| 104 | // add short codes. |
| 105 | add_shortcode( 'the_ad', array( $this, 'shortcode_display_ad' ) ); |
| 106 | add_shortcode( 'the_ad_group', array( $this, 'shortcode_display_ad_group' ) ); |
| 107 | add_shortcode( 'the_ad_placement', array( $this, 'shortcode_display_ad_placement' ) ); |
| 108 | |
| 109 | // remove default ad group menu item // -TODO only for admin. |
| 110 | add_action( 'admin_menu', array( $this, 'remove_taxonomy_menu_item' ) ); |
| 111 | // load widgets. |
| 112 | add_action( 'widgets_init', array( $this, 'widget_init' ) ); |
| 113 | |
| 114 | // load display conditions. |
| 115 | Advanced_Ads_Display_Conditions::get_instance(); |
| 116 | new Advanced_Ads_Frontend_Checks(); |
| 117 | new Advanced_Ads_Compatibility(); |
| 118 | Advanced_Ads_Ad_Health_Notices::get_instance(); // load to fetch notices. |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Run upgrades. |
| 123 | * |
| 124 | * Compatibility with the Piklist plugin that has a function hooked to `posts_where` that access $GLOBALS['wp_query']. |
| 125 | * Since `Advanced_Ads_Upgrades` applies `posts_where`: (`Advanced_Ads_Admin_Notices::get_instance()` > |
| 126 | * `Advanced_Ads::get_number_of_ads()` > new WP_Query > ... 'posts_where') this function is hooked to `init` so that `$GLOBALS['wp_query']` is instantiated. |
| 127 | */ |
| 128 | public function run_upgrades() { |
| 129 | /** |
| 130 | * Run upgrades, if this is a new version or version does not exist. |
| 131 | */ |
| 132 | $internal_options = $this->internal_options(); |
| 133 | |
| 134 | if ( ! defined( 'DOING_AJAX' ) && ( ! isset( $internal_options['version'] ) || version_compare( $internal_options['version'], ADVADS_VERSION, '<' ) ) ) { |
| 135 | new Advanced_Ads_Upgrades(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Register and enqueue public-facing style sheet. |
| 141 | */ |
| 142 | public function enqueue_styles() { |
| 143 | // wp_enqueue_style( $this->get_plugin_slug() . '-plugin-styles', plugins_url('assets/css/public.css', __FILE__), array(), ADVADS_VERSION); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Return the plugin slug. |
| 148 | * |
| 149 | * @return string plugin slug variable. |
| 150 | */ |
| 151 | public function get_plugin_slug() { |
| 152 | return ADVADS_SLUG; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Register and enqueues public-facing JavaScript files. |
| 157 | */ |
| 158 | public function enqueue_scripts() { |
| 159 | if ( advads_is_amp() ) { |
| 160 | return; |
| 161 | } |
| 162 | // wp_enqueue_script( $this->get_plugin_slug() . '-plugin-script', plugins_url('assets/js/public.js', __FILE__), array('jquery'), ADVADS_VERSION); |
| 163 | $options = $this->options(); |
| 164 | $activated_js = apply_filters( 'advanced-ads-activate-advanced-js', isset( $options['advanced-js'] ) ); |
| 165 | |
| 166 | if ( $activated_js || ! empty( $_COOKIE['advads_frontend_picker'] ) ) { |
| 167 | wp_enqueue_script( $this->get_plugin_slug() . '-advanced-js', ADVADS_BASE_URL . 'public/assets/js/advanced.js', array( 'jquery' ), ADVADS_VERSION, false ); |
| 168 | $data = array( |
| 169 | 'blog_id' => get_current_blog_id(), |
| 170 | ); |
| 171 | wp_localize_script( $this->get_plugin_slug() . '-advanced-js', 'advanced_ads_data', $data ); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Print public-facing JavaScript in the HTML head. |
| 177 | */ |
| 178 | public function print_head_scripts() { |
| 179 | /** |
| 180 | * Usage example in add-ons: |
| 181 | * ( window.advanced_ads_ready || jQuery( document ).ready ).call( null, function() { |
| 182 | * // Called when DOM is ready. |
| 183 | * } ); |
| 184 | */ |
| 185 | |
| 186 | $short_url = self::get_short_url(); |
| 187 | $attribution = '<!-- ' . $short_url . ' is managing ads with Advanced Ads%1$s%2$s -->'; |
| 188 | $version = self::is_new_user( 1585224000 ) ? ' ' . ADVADS_VERSION : ''; |
| 189 | $plugin_url = self::get_group_by_url( $short_url, 'a' ) ? ' – ' . ADVADS_URL : ''; |
| 190 | // escaping would break HTML comment tags so we disable checks here. |
| 191 | // phpcs:ignore |
| 192 | echo apply_filters( 'advanced-ads-attribution', sprintf( $attribution, $version, $plugin_url ) ); |
| 193 | |
| 194 | if ( advads_is_amp() ) { |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | ob_start(); |
| 199 | // @formatter:off |
| 200 | ?> |
| 201 | <script> |
| 202 | <?php |
| 203 | if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { |
| 204 | readfile( ADVADS_BASE_PATH . 'public/assets/js/ready.js' ); |
| 205 | } else { |
| 206 | ?> |
| 207 | advanced_ads_ready=function(){var fns=[],listener,doc=typeof document==="object"&&document,hack=doc&&doc.documentElement.doScroll,domContentLoaded="DOMContentLoaded",loaded=doc&&(hack?/^loaded|^c/:/^loaded|^i|^c/).test(doc.readyState);if(!loaded&&doc){listener=function(){doc.removeEventListener(domContentLoaded,listener);window.removeEventListener("load",listener);loaded=1;while(listener=fns.shift())listener()};doc.addEventListener(domContentLoaded,listener);window.addEventListener("load",listener)}return function(fn){loaded?setTimeout(fn,0):fns.push(fn)}}(); |
| 208 | <?php |
| 209 | } |
| 210 | |
| 211 | // Output privacy options. |
| 212 | $privacy_options = Advanced_Ads_Privacy::get_instance()->options(); |
| 213 | if ( ! empty( $privacy_options['enabled'] ) ) { |
| 214 | printf( '(advads_options = window.advads_options || {} )["privacy"] = %s;', wp_json_encode( $privacy_options ) ); |
| 215 | } |
| 216 | |
| 217 | ?> |
| 218 | </script> |
| 219 | <?php |
| 220 | // @formatter:on |
| 221 | // escaping would break our HTML here. |
| 222 | // phpcs:ignore |
| 223 | echo Advanced_Ads_Utils::get_inline_asset( ob_get_clean() ); |
| 224 | |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Register the Advanced Ads widget |
| 229 | */ |
| 230 | public function widget_init() { |
| 231 | register_widget( 'Advanced_Ads_Widget' ); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Fired when a new site is activated with a WPMU environment. |
| 236 | * |
| 237 | * @param int $blog_id ID of the new blog. |
| 238 | */ |
| 239 | public function activate_new_site( $blog_id ) { |
| 240 | |
| 241 | if ( 1 !== did_action( 'wpmu_new_blog' ) ) { |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | switch_to_blog( $blog_id ); |
| 246 | $this->single_activate(); |
| 247 | restore_current_blog(); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Fired for each blog when the plugin is activated. |
| 252 | */ |
| 253 | protected function single_activate() { |
| 254 | // $this->post_types_rewrite_flush(); |
| 255 | // -TODO inform modules |
| 256 | $this->create_capabilities(); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Fired for each blog when the plugin is deactivated. |
| 261 | */ |
| 262 | protected function single_deactivate() { |
| 263 | // -TODO inform modules |
| 264 | $this->remove_capabilities(); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Load the plugin text domain for translation. |
| 269 | */ |
| 270 | public function load_plugin_textdomain() { |
| 271 | load_plugin_textdomain( 'advanced-ads', false, ADVADS_BASE_DIR . '/languages' ); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Fired when the plugin is activated. |
| 276 | * |
| 277 | * @param boolean $network_wide True if WPMU superadmin uses |
| 278 | * "Network Activate" action, false if |
| 279 | * WPMU is disabled or plugin is |
| 280 | * activated on an individual blog. |
| 281 | */ |
| 282 | public function activate( $network_wide ) { |
| 283 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
| 284 | |
| 285 | if ( $network_wide ) { |
| 286 | // get all blog ids. |
| 287 | global $wpdb; |
| 288 | $blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" ); |
| 289 | $original_blog_id = $wpdb->blogid; |
| 290 | |
| 291 | foreach ( $blog_ids as $blog_id ) { |
| 292 | switch_to_blog( $blog_id ); |
| 293 | $this->single_activate(); |
| 294 | } |
| 295 | |
| 296 | switch_to_blog( $original_blog_id ); |
| 297 | } else { |
| 298 | $this->single_activate(); |
| 299 | } |
| 300 | } else { |
| 301 | $this->single_activate(); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Fired when the plugin is deactivated. |
| 307 | * |
| 308 | * @param boolean $network_wide true if Advanced Ads should be disabled network-wide. |
| 309 | * |
| 310 | * True if WPMU superadmin uses |
| 311 | * "Network Deactivate" action, false if |
| 312 | * WPMU is disabled or plugin is |
| 313 | * deactivated on an individual blog. |
| 314 | */ |
| 315 | public function deactivate( $network_wide ) { |
| 316 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
| 317 | |
| 318 | if ( $network_wide ) { |
| 319 | // get all blog ids. |
| 320 | global $wpdb; |
| 321 | $blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" ); |
| 322 | $original_blog_id = $wpdb->blogid; |
| 323 | |
| 324 | foreach ( $blog_ids as $blog_id ) { |
| 325 | switch_to_blog( $blog_id ); |
| 326 | $this->single_deactivate(); |
| 327 | } |
| 328 | |
| 329 | switch_to_blog( $original_blog_id ); |
| 330 | } else { |
| 331 | $this->single_deactivate(); |
| 332 | } |
| 333 | } else { |
| 334 | $this->single_deactivate(); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Remove WP tag edit page for the ad group taxonomy |
| 340 | * needed, because we can’t remove it with `show_ui` without also removing the meta box |
| 341 | */ |
| 342 | public function remove_taxonomy_menu_item() { |
| 343 | remove_submenu_page( 'edit.php?post_type=advanced_ads', 'edit-tags.php?taxonomy=advanced_ads_groups&post_type=advanced_ads' ); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Shortcode to include ad in frontend |
| 348 | * |
| 349 | * @param array $atts shortcode attributes. |
| 350 | * |
| 351 | * @return string ad content. |
| 352 | */ |
| 353 | public function shortcode_display_ad( $atts ) { |
| 354 | $atts = is_array( $atts ) ? $atts : array(); |
| 355 | $id = isset( $atts['id'] ) ? (int) $atts['id'] : 0; |
| 356 | $atts = $this->prepare_shortcode_atts( $atts ); |
| 357 | |
| 358 | // use the public available function here. |
| 359 | return get_ad( $id, $atts ); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Shortcode to include ad from an ad group in frontend |
| 364 | * |
| 365 | * @param array $atts shortcode attributes. |
| 366 | * |
| 367 | * @return string ad group content. |
| 368 | */ |
| 369 | public function shortcode_display_ad_group( $atts ) { |
| 370 | $atts = is_array( $atts ) ? $atts : array(); |
| 371 | $id = isset( $atts['id'] ) ? (int) $atts['id'] : 0; |
| 372 | $atts = $this->prepare_shortcode_atts( $atts ); |
| 373 | |
| 374 | // use the public available function here. |
| 375 | return get_ad_group( $id, $atts ); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Shortcode to display content of an ad placement in frontend |
| 380 | * |
| 381 | * @param array $atts shortcode attributes. |
| 382 | * |
| 383 | * @return string ad placement content. |
| 384 | */ |
| 385 | public function shortcode_display_ad_placement( $atts ) { |
| 386 | $atts = is_array( $atts ) ? $atts : array(); |
| 387 | $id = isset( $atts['id'] ) ? (string) $atts['id'] : ''; |
| 388 | $atts = $this->prepare_shortcode_atts( $atts ); |
| 389 | |
| 390 | // use the public available function here. |
| 391 | return get_ad_placement( $id, $atts ); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Prepare shortcode attributes. |
| 396 | * |
| 397 | * @param array $atts array with strings. |
| 398 | * |
| 399 | * @return array |
| 400 | */ |
| 401 | private function prepare_shortcode_atts( $atts ) { |
| 402 | $result = array(); |
| 403 | |
| 404 | /** |
| 405 | * Prepare attributes by converting strings to multi-dimensional array |
| 406 | * Example: [ 'output__margin__top' => 1 ] => ['output']['margin']['top'] = 1 |
| 407 | */ |
| 408 | if ( ! defined( 'ADVANCED_ADS_DISABLE_CHANGE' ) || ! ADVANCED_ADS_DISABLE_CHANGE ) { |
| 409 | foreach ( $atts as $attr => $data ) { |
| 410 | $levels = explode( '__', $attr ); |
| 411 | $last = array_pop( $levels ); |
| 412 | |
| 413 | $cur_lvl = &$result; |
| 414 | |
| 415 | foreach ( $levels as $lvl ) { |
| 416 | if ( ! isset( $cur_lvl[ $lvl ] ) ) { |
| 417 | $cur_lvl[ $lvl ] = array(); |
| 418 | } |
| 419 | |
| 420 | $cur_lvl = &$cur_lvl[ $lvl ]; |
| 421 | } |
| 422 | |
| 423 | $cur_lvl[ $last ] = $data; |
| 424 | } |
| 425 | |
| 426 | $result = array_diff_key( |
| 427 | $result, |
| 428 | array( |
| 429 | 'id' => false, |
| 430 | 'blog_id' => false, |
| 431 | 'ad_args' => false, |
| 432 | ) |
| 433 | ); |
| 434 | } |
| 435 | |
| 436 | // Ad type: 'content' and a shortcode inside. |
| 437 | if ( isset( $atts['ad_args'] ) ) { |
| 438 | $result = array_merge( $result, json_decode( urldecode( $atts['ad_args'] ), true ) ); |
| 439 | |
| 440 | } |
| 441 | |
| 442 | return $result; |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Return plugin options |
| 447 | * these are the options updated by the user |
| 448 | * |
| 449 | * @return array $options |
| 450 | */ |
| 451 | public function options() { |
| 452 | // we can’t store options if WPML String Translations is enabled, or it would not translate the "Ad Label" option. |
| 453 | if ( ! isset( $this->options ) || class_exists( 'WPML_ST_String' ) ) { |
| 454 | $this->options = get_option( ADVADS_SLUG, array() ); |
| 455 | } |
| 456 | |
| 457 | // default options for new users |
| 458 | if ( array() === $this->options ) { |
| 459 | // disable the shortcode button by default for users who never stored the settings. |
| 460 | if ( ! isset( $this->options['disable-shortcode-button'] ) ) { |
| 461 | $this->options['disable-shortcode-button'] = true; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | // allow to change options dynamically |
| 466 | $this->options = apply_filters( 'advanced-ads-options', $this->options ); |
| 467 | |
| 468 | return $this->options; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Update plugin options (not for settings page, but if automatic options are needed) |
| 473 | * |
| 474 | * @param array $options new options. |
| 475 | */ |
| 476 | public function update_options( array $options ) { |
| 477 | // do not allow to clear options. |
| 478 | if ( array() === $options ) { |
| 479 | return; |
| 480 | } |
| 481 | |
| 482 | $this->options = $options; |
| 483 | update_option( ADVADS_SLUG, $options ); |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Return internal plugin options |
| 488 | * these are options set by the plugin |
| 489 | * |
| 490 | * @return array $options |
| 491 | */ |
| 492 | public function internal_options() { |
| 493 | if ( ! isset( $this->internal_options ) ) { |
| 494 | $defaults = array( |
| 495 | 'version' => ADVADS_VERSION, |
| 496 | 'installed' => time(), // when was this installed. |
| 497 | ); |
| 498 | $this->internal_options = get_option( ADVADS_SLUG . '-internal', array() ); |
| 499 | |
| 500 | // save defaults. |
| 501 | if ( array() === $this->internal_options ) { |
| 502 | $this->internal_options = $defaults; |
| 503 | $this->update_internal_options( $this->internal_options ); |
| 504 | |
| 505 | self::get_instance()->create_capabilities(); |
| 506 | } |
| 507 | |
| 508 | // for versions installed prior to 1.5.3 set installed date for now. |
| 509 | if ( ! isset( $this->internal_options['installed'] ) ) { |
| 510 | $this->internal_options['installed'] = time(); |
| 511 | $this->update_internal_options( $this->internal_options ); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | return $this->internal_options; |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Update internal plugin options |
| 520 | * |
| 521 | * @param array $options new internal options. |
| 522 | */ |
| 523 | public function update_internal_options( array $options ) { |
| 524 | // do not allow to clear options. |
| 525 | if ( array() === $options ) { |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | $this->internal_options = $options; |
| 530 | update_option( ADVADS_SLUG . '-internal', $options ); |
| 531 | } |
| 532 | |
| 533 | /** |
| 534 | * Get prefix used for frontend elements |
| 535 | */ |
| 536 | public function get_frontend_prefix() { |
| 537 | if ( ! $this->frontend_prefix ) { |
| 538 | $options = $this->options(); |
| 539 | |
| 540 | if ( ! isset( $options['front-prefix'] ) ) { |
| 541 | if ( isset( $options['id-prefix'] ) ) { |
| 542 | // deprecated: keeps widgets working that previously received an id based on the front-prefix. |
| 543 | $frontend_prefix = esc_attr( $options['id-prefix'] ); |
| 544 | } else { |
| 545 | $host = parse_url( get_home_url(), PHP_URL_HOST ); |
| 546 | $frontend_prefix = preg_match( '/[A-Za-z][A-Za-z0-9_]{4}/', $host, $result ) ? $result[0] . '-' : self::DEFAULT_FRONTEND_PREFIX; |
| 547 | } |
| 548 | } else { |
| 549 | $frontend_prefix = esc_attr( $options['front-prefix'] ); |
| 550 | } |
| 551 | /** |
| 552 | * Applying the filter here makes sure that it is the same frontend prefix for all |
| 553 | * calls on this page impression |
| 554 | */ |
| 555 | $this->frontend_prefix = apply_filters( 'advanced-ads-frontend-prefix', $frontend_prefix ); |
| 556 | } |
| 557 | |
| 558 | return $this->frontend_prefix; |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * Get priority used for injection inside content |
| 563 | */ |
| 564 | public function get_content_injection_priority() { |
| 565 | $options = $this->options(); |
| 566 | |
| 567 | return isset( $options['content-injection-priority'] ) ? intval( $options['content-injection-priority'] ) : 100; |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Returns the capability needed to perform an action |
| 572 | * |
| 573 | * @param string $capability a capability to check, can be internal to Advanced Ads. |
| 574 | * |
| 575 | * @return string $capability a valid WordPress capability. |
| 576 | */ |
| 577 | public static function user_cap( $capability = 'manage_options' ) { |
| 578 | |
| 579 | global $advanced_ads_capabilities; |
| 580 | |
| 581 | // admins can do everything. |
| 582 | // is also a fallback if no option or more specific capability is given. |
| 583 | if ( current_user_can( 'manage_options' ) ) { |
| 584 | return 'manage_options'; |
| 585 | } |
| 586 | |
| 587 | return apply_filters( 'advanced-ads-capability', $capability ); |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Create roles and capabilities |
| 592 | */ |
| 593 | public function create_capabilities() { |
| 594 | if ( $role = get_role( 'administrator' ) ) { |
| 595 | $role->add_cap( 'advanced_ads_manage_options' ); |
| 596 | $role->add_cap( 'advanced_ads_see_interface' ); |
| 597 | $role->add_cap( 'advanced_ads_edit_ads' ); |
| 598 | $role->add_cap( 'advanced_ads_manage_placements' ); |
| 599 | $role->add_cap( 'advanced_ads_place_ads' ); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * Remove roles and capabilities |
| 605 | */ |
| 606 | public function remove_capabilities() { |
| 607 | if ( $role = get_role( 'administrator' ) ) { |
| 608 | $role->remove_cap( 'advanced_ads_manage_options' ); |
| 609 | $role->remove_cap( 'advanced_ads_see_interface' ); |
| 610 | $role->remove_cap( 'advanced_ads_edit_ads' ); |
| 611 | $role->remove_cap( 'advanced_ads_manage_placements' ); |
| 612 | $role->remove_cap( 'advanced_ads_place_ads' ); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | /** |
| 617 | * Fired when the plugin is uninstalled. |
| 618 | */ |
| 619 | public static function uninstall() { |
| 620 | $advads_options = Advanced_Ads::get_instance()->options(); |
| 621 | |
| 622 | if ( ! empty( $advads_options['uninstall-delete-data'] ) ) { |
| 623 | global $wpdb; |
| 624 | $main_blog_id = $wpdb->blogid; |
| 625 | |
| 626 | Advanced_Ads::get_instance()->create_post_types(); |
| 627 | |
| 628 | if ( ! is_multisite() ) { |
| 629 | self::get_instance()->uninstall_single(); |
| 630 | } else { |
| 631 | $blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" ); |
| 632 | |
| 633 | foreach ( $blog_ids as $blog_id ) { |
| 634 | switch_to_blog( $blog_id ); |
| 635 | self::get_instance()->uninstall_single(); |
| 636 | } |
| 637 | switch_to_blog( $main_blog_id ); |
| 638 | } |
| 639 | |
| 640 | // Delete assets (main blog). |
| 641 | Advanced_Ads_Ad_Blocker_Admin::get_instance()->clear_assets(); |
| 642 | delete_option( ADVADS_AB_SLUG ); |
| 643 | } |
| 644 | |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Fired for each blog when the plugin is uninstalled. |
| 649 | */ |
| 650 | protected function uninstall_single() { |
| 651 | global $wpdb; |
| 652 | |
| 653 | // Ads. |
| 654 | $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s", Advanced_Ads::POST_TYPE_SLUG ) ); |
| 655 | |
| 656 | if ( $post_ids ) { |
| 657 | $wpdb->delete( |
| 658 | $wpdb->posts, |
| 659 | array( 'post_type' => Advanced_Ads::POST_TYPE_SLUG ), |
| 660 | array( '%s' ) |
| 661 | ); |
| 662 | |
| 663 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE post_id IN( %s )", implode( ',', $post_ids ) ) ); |
| 664 | } |
| 665 | |
| 666 | // Groups. |
| 667 | $term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT t.term_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s", Advanced_Ads::AD_GROUP_TAXONOMY ) ); |
| 668 | |
| 669 | foreach ( $term_ids as $term_id ) { |
| 670 | wp_delete_term( $term_id, Advanced_Ads::AD_GROUP_TAXONOMY ); |
| 671 | } |
| 672 | |
| 673 | delete_option( 'advads-ad-groups' ); |
| 674 | delete_option( Advanced_Ads::AD_GROUP_TAXONOMY . '_children' ); |
| 675 | delete_option( 'advads-ad-weights' ); |
| 676 | |
| 677 | // Placements. |
| 678 | delete_option( 'advads-ads-placements' ); |
| 679 | |
| 680 | // User metadata. |
| 681 | delete_metadata( 'user', null, 'advanced-ads-hide-wizard', '', true ); |
| 682 | delete_metadata( 'user', null, 'advanced-ads-subscribed', '', true ); |
| 683 | |
| 684 | // Post metadata. |
| 685 | delete_metadata( 'post', null, '_advads_ad_settings', '', true ); |
| 686 | |
| 687 | // Transients. |
| 688 | delete_transient( ADVADS_SLUG . '_add-on-updates-checked' ); |
| 689 | |
| 690 | delete_option( GADSENSE_OPT_NAME ); |
| 691 | delete_option( ADVADS_SLUG ); |
| 692 | delete_option( ADVADS_SLUG . '-internal' ); |
| 693 | delete_option( ADVADS_SLUG . '-notices' ); |
| 694 | |
| 695 | // Widget. |
| 696 | $base_widget_id = Advanced_Ads_Widget::get_base_id(); |
| 697 | delete_option( 'widget_' . $base_widget_id ); |
| 698 | |
| 699 | do_action( 'advanced-ads-uninstall' ); |
| 700 | |
| 701 | wp_cache_flush(); |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * Check if any add-on is activated |
| 706 | * |
| 707 | * @return bool true if there is any add-on activated |
| 708 | */ |
| 709 | public static function any_activated_add_on() { |
| 710 | return ( defined( 'AAP_VERSION' ) // Advanced Ads Pro. |
| 711 | || defined( 'AAGAM_VERSION' ) // Google Ad Manager. |
| 712 | || defined( 'AASA_VERSION' ) // Selling Ads. |
| 713 | || defined( 'AAT_VERSION' ) // Tracking. |
| 714 | || defined( 'AASADS_VERSION' ) // Sticky Ads. |
| 715 | || defined( 'AAR_VERSION' ) // Responsive Ads. |
| 716 | || defined( 'AAPLDS_VERSION' ) // PopUp and Layer Ads. |
| 717 | || defined( 'AAGT_SLUG' ) // Geo-Targeting. |
| 718 | ); |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * Get the correct support URL: wp.org for free users and website for those with any add-on installed |
| 723 | * |
| 724 | * @param string $utm add UTM parameter to the link leading to https://wpadvancedads.com, if given. |
| 725 | * |
| 726 | * @return string URL. |
| 727 | */ |
| 728 | public static function support_url( $utm = '' ) { |
| 729 | |
| 730 | $utm = empty( $utm ) ? '#utm_source=advanced-ads&utm_medium=link&utm_campaign=support' : $utm; |
| 731 | if ( self::any_activated_add_on() ) { |
| 732 | $url = ADVADS_URL . 'support/' . $utm . '-with-addons'; |
| 733 | } else { |
| 734 | $url = ADVADS_URL . 'support/' . $utm . '-free-user'; |
| 735 | } |
| 736 | |
| 737 | return $url; |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * Create a random group |
| 742 | * |
| 743 | * @param string $url optional parameter. |
| 744 | * @param string $ex group. |
| 745 | * |
| 746 | * @return bool |
| 747 | */ |
| 748 | public static function get_group_by_url( $url = '', $ex = 'a' ) { |
| 749 | |
| 750 | $url = self::get_short_url( $url ); |
| 751 | |
| 752 | $code = intval( substr( md5( $url ), - 1 ), 16 ); |
| 753 | |
| 754 | switch ( $ex ) { |
| 755 | case 'b': |
| 756 | return ( $code & 2 ) >> 1; // returns 1 or 0. |
| 757 | case 'c': |
| 758 | return ( $code & 4 ) >> 2; // returns 1 or 0. |
| 759 | case 'd': |
| 760 | return ( $code & 8 ) >> 3; // returns 1 or 0. |
| 761 | default: |
| 762 | return $code & 1; // returns 1 or 0. |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | /** |
| 767 | * Check if user started after a given date |
| 768 | * |
| 769 | * @param integer $timestamp time stamp. |
| 770 | * |
| 771 | * @return bool true if user is added after timestamp. |
| 772 | */ |
| 773 | public static function is_new_user( $timestamp = 0 ) { |
| 774 | |
| 775 | // allow admins to see version for new users in any case. |
| 776 | if ( current_user_can( self::user_cap( 'advanced_ads_manage_options' ) ) |
| 777 | && isset( $_REQUEST['advads-ignore-timestamp'] ) ) { |
| 778 | return true; |
| 779 | } |
| 780 | |
| 781 | $timestamp = absint( $timestamp ); |
| 782 | |
| 783 | $options = self::get_instance()->internal_options(); |
| 784 | $installed = isset( $options['installed'] ) ? $options['installed'] : 0; |
| 785 | |
| 786 | return ( $installed >= $timestamp ); |
| 787 | } |
| 788 | |
| 789 | /** |
| 790 | * Show stuff to new users only. |
| 791 | * |
| 792 | * @param integer $timestamp time after which to show whatever. |
| 793 | * @param string $group optional group. |
| 794 | * |
| 795 | * @return bool true if user enabled after given timestamp. |
| 796 | */ |
| 797 | public static function show_to_new_users( $timestamp, $group = 'a' ) { |
| 798 | |
| 799 | return ( self::get_group_by_url( null, $group ) && self::is_new_user( $timestamp ) ); |
| 800 | } |
| 801 | |
| 802 | /** |
| 803 | * Get short version of home_url() |
| 804 | * remove protocol and www |
| 805 | * remove slash |
| 806 | * |
| 807 | * @param string $url URL to be shortened. |
| 808 | * |
| 809 | * @return string |
| 810 | */ |
| 811 | public static function get_short_url( $url = '' ) { |
| 812 | |
| 813 | $url = empty( $url ) ? home_url() : $url; |
| 814 | |
| 815 | // strip protocols. |
| 816 | if ( preg_match( '/^(\w[\w\d]*:\/\/)?(www\.)?(.*)$/', trim( $url ), $matches ) ) { |
| 817 | $url = $matches[3]; |
| 818 | } |
| 819 | |
| 820 | // strip slashes. |
| 821 | $url = trim( $url, '/' ); |
| 822 | |
| 823 | return $url; |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * Return Advanced Ads logo in base64 format for use in WP Admin menu. |
| 828 | * |
| 829 | * @return string |
| 830 | */ |
| 831 | public static function get_icon_svg() { |
| 832 | return 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/Pg0KPCEtLSBHZW5lcmF0b3I6IEFkb2JlIElsbHVzdHJhdG9yIDE4LjEuMSwgU1ZHIEV4cG9ydCBQbHVnLUluIC4gU1ZHIFZlcnNpb246IDYuMDAgQnVpbGQgMCkgIC0tPg0KPHN2ZyB2ZXJzaW9uPSIxLjEiIGlkPSJFYmVuZV8xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB4PSIwcHgiIHk9IjBweCINCgkgdmlld0JveD0iMCAwIDY0Ljk5MyA2NS4wMjQiIHN0eWxlPSJlbmFibGUtYmFja2dyb3VuZDpuZXcgMCAwIDY0Ljk5MyA2NS4wMjQ7IiB4bWw6c3BhY2U9InByZXNlcnZlIj4NCjxwYXRoIHN0eWxlPSJmaWxsOiNFNEU0RTQ7IiBkPSJNNDYuNTcxLDI3LjY0MXYyMy4xMzNIMTQuMjVWMTguNDUzaDIzLjExOGMtMC45NTYtMi4xODMtMS40OTQtNC41OS0xLjQ5NC03LjEyNg0KCWMwLTIuNTM1LDAuNTM4LTQuOTQyLDEuNDk0LTcuMTI0aC02Ljk1N0gwdjQ5LjQ5M2wxLjYxOCwxLjYxOEwwLDUzLjY5NmMwLDYuMjU2LDUuMDY4LDExLjMyNiwxMS4zMjQsMTEuMzI4djBoMTkuMDg3aDMwLjQxMlYyNy42MTENCgljLTIuMTkxLDAuOTY0LTQuNjA5LDEuNTA5LTcuMTU3LDEuNTA5QzUxLjE0MiwyOS4xMiw0OC43NDYsMjguNTg4LDQ2LjU3MSwyNy42NDF6Ii8+DQo8Y2lyY2xlIHN0eWxlPSJmaWxsOiM5ODk4OTg7IiBjeD0iNTMuNjY2IiBjeT0iMTEuMzI4IiByPSIxMS4zMjgiLz4NCjwvc3ZnPg0K'; |
| 833 | } |
| 834 | |
| 835 | } |
| 836 |