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