PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 1.16
Advanced Ads – Ad Manager & AdSense v1.16
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
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 8 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 8 years ago ad_type_content.php 6 years ago ad_type_dummy.php 6 years ago ad_type_group.php 8 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_checks.php 6 years ago plugin.php 6 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
838 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 <?php if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
192 readfile( ADVADS_BASE_PATH . 'public/assets/js/ready.js' );
193 } else { ?>
194 advanced_ads_ready = function () {
195 var fns = [], listener, doc = typeof document === 'object' && document,
196 hack = doc && doc.documentElement.doScroll, domContentLoaded = 'DOMContentLoaded',
197 loaded = doc && (hack ? /^loaded|^c/ : /^loaded|^i|^c/).test( doc.readyState )
198 if ( ! loaded && doc ) {
199 listener = function () {
200 doc.removeEventListener( domContentLoaded, listener )
201 window.removeEventListener( 'load', listener )
202 loaded = 1
203 while ( listener = fns.shift() ) listener()
204 }
205 doc.addEventListener( domContentLoaded, listener )
206 window.addEventListener( 'load', listener )
207 }
208 return function ( fn ) {loaded ? setTimeout( fn, 0 ) : fns.push( fn )}
209 }()
210 <?php
211 }
212
213 // Output privacy options.
214 $privacy_options = Advanced_Ads_Privacy::get_instance()->options();
215 if ( ! empty( $privacy_options['enabled'] ) ) {
216 printf( '(advads_options = window.advads_options || {} )["privacy"] = %s;', json_encode( $privacy_options ) );
217 }
218
219 ?></script><?php
220 echo Advanced_Ads_Utils::get_inline_asset( ob_get_clean() );
221
222
223 }
224
225 public function widget_init() {
226 register_widget( 'Advanced_Ads_Widget' );
227 }
228
229 /**
230 * Fired when a new site is activated with a WPMU environment.
231 *
232 * @param int $blog_id ID of the new blog.
233 *
234 * @since 1.0.0
235 */
236 public function activate_new_site( $blog_id ) {
237
238 if ( 1 !== did_action( 'wpmu_new_blog' ) ) {
239 return;
240 }
241
242 switch_to_blog( $blog_id );
243 $this->single_activate();
244 restore_current_blog();
245 }
246
247 /**
248 * Fired for each blog when the plugin is activated.
249 *
250 * @since 1.0.0
251 */
252 protected function single_activate() {
253 // $this->post_types_rewrite_flush();
254 // -TODO inform modules
255 $this->create_capabilities();
256 }
257
258 /**
259 * Fired for each blog when the plugin is deactivated.
260 *
261 * @since 1.0.0
262 */
263 protected function single_deactivate() {
264 // -TODO inform modules
265 $this->remove_capabilities();
266 }
267
268 /**
269 * Load the plugin text domain for translation.
270 *
271 * @since 1.0.0
272 */
273 public function load_plugin_textdomain() {
274 // $locale = apply_filters('advanced-ads-plugin-locale', get_locale(), $domain);
275 load_plugin_textdomain( 'advanced-ads', false, ADVADS_BASE_DIR . '/languages' );
276 }
277
278 /**
279 * Fired when the plugin is activated.
280 *
281 * @param boolean $network_wide True if WPMU superadmin uses
282 * "Network Activate" action, false if
283 * WPMU is disabled or plugin is
284 * activated on an individual blog.
285 *
286 * @since 1.0.0
287 */
288 public function activate( $network_wide ) {
289 if ( function_exists( 'is_multisite' ) && is_multisite() ) {
290
291 if ( $network_wide ) {
292 // Get all blog ids
293 global $wpdb;
294 $blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" );
295 $original_blog_id = $wpdb->blogid;
296
297 foreach ( $blog_ids as $blog_id ) {
298 switch_to_blog( $blog_id );
299 $this->single_activate();
300 }
301
302 switch_to_blog( $original_blog_id );
303 } else {
304 $this->single_activate();
305 }
306 } else {
307 $this->single_activate();
308 }
309 }
310
311 /**
312 * Fired when the plugin is deactivated.
313 *
314 * @param boolean $network_wide
315 *
316 * True if WPMU superadmin uses
317 * "Network Deactivate" action, false if
318 * WPMU is disabled or plugin is
319 * deactivated on an individual blog.
320 *
321 * @since 1.0.0
322 */
323 public function deactivate( $network_wide ) {
324 if ( function_exists( 'is_multisite' ) && is_multisite() ) {
325
326 if ( $network_wide ) {
327 // Get all blog ids
328 global $wpdb;
329 $blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" );
330 $original_blog_id = $wpdb->blogid;
331
332 foreach ( $blog_ids as $blog_id ) {
333 switch_to_blog( $blog_id );
334 $this->single_deactivate();
335 }
336
337 switch_to_blog( $original_blog_id );
338 } else {
339 $this->single_deactivate();
340 }
341 } else {
342 $this->single_deactivate();
343 }
344 }
345
346 /**
347 * flush rewrites on plugin activation so permalinks for them work from the beginning on
348 *
349 * @since 1.0.0
350 * @link http://codex.wordpress.org/Function_Reference/register_post_type#Flushing_Rewrite_on_Activation
351 */
352 /*public function post_types_rewrite_flush(){
353 // load custom post type
354 Advanced_Ads::get_instance()->create_post_types();
355 // flush rewrite rules
356 flush_rewrite_rules();
357 }*/
358
359 /**
360 * remove WP tag edit page for the ad group taxonomy
361 * needed, because we can’t remove it with `show_ui` without also removing the meta box
362 *
363 * @since 1.0.0
364 */
365 public function remove_taxonomy_menu_item() {
366 remove_submenu_page( 'edit.php?post_type=advanced_ads', 'edit-tags.php?taxonomy=advanced_ads_groups&amp;post_type=advanced_ads' );
367 }
368
369 /**
370 * shortcode to include ad in frontend
371 *
372 * @param arr $atts
373 *
374 * @since 1.0.0
375 */
376 public function shortcode_display_ad( $atts ) {
377 $atts = is_array( $atts ) ? $atts : array();
378 $id = isset( $atts['id'] ) ? (int) $atts['id'] : 0;
379 $atts = $this->prepare_shortcode_atts( $atts );
380
381 // use the public available function here
382 return get_ad( $id, $atts );
383 }
384
385 /**
386 * shortcode to include ad from an ad group in frontend
387 *
388 * @param arr $atts
389 *
390 * @since 1.0.0
391 */
392 public function shortcode_display_ad_group( $atts ) {
393 $atts = is_array( $atts ) ? $atts : array();
394 $id = isset( $atts['id'] ) ? (int) $atts['id'] : 0;
395 $atts = $this->prepare_shortcode_atts( $atts );
396
397 // use the public available function here
398 return get_ad_group( $id, $atts );
399 }
400
401 /**
402 * shortcode to display content of an ad placement in frontend
403 *
404 * @param arr $atts
405 *
406 * @since 1.1.0
407 */
408 public function shortcode_display_ad_placement( $atts ) {
409 $atts = is_array( $atts ) ? $atts : array();
410 $id = isset( $atts['id'] ) ? (string) $atts['id'] : '';
411 $atts = $this->prepare_shortcode_atts( $atts );
412
413 // use the public available function here
414 return get_ad_placement( $id, $atts );
415 }
416
417 /**
418 * Prepare shortcode attributes.
419 *
420 * @param array $atts array with strings
421 *
422 * @return array
423 */
424 private function prepare_shortcode_atts( $atts ) {
425 $result = array();
426
427 /**
428 * Prepare attributes by converting strings to multi-dimensional array
429 * Example: [ 'output__margin__top' => 1 ] => ['output']['margin']['top'] = 1
430 */
431 if ( ! defined( 'ADVANCED_ADS_DISABLE_CHANGE' ) || ! ADVANCED_ADS_DISABLE_CHANGE ) {
432 foreach ( $atts as $attr => $data ) {
433 $levels = explode( '__', $attr );
434 $last = array_pop( $levels );
435
436 $cur_lvl = &$result;
437
438 foreach ( $levels as $lvl ) {
439 if ( ! isset( $cur_lvl[ $lvl ] ) ) {
440 $cur_lvl[ $lvl ] = array();
441 }
442
443 $cur_lvl = &$cur_lvl[ $lvl ];
444 }
445
446 $cur_lvl[ $last ] = $data;
447 }
448
449 $result = array_diff_key( $result, array( 'id' => false, 'blog_id' => false, 'ad_args' => false ) );
450 }
451
452 // Ad type: 'content' and a shortcode inside.
453 if ( isset( $atts['ad_args'] ) ) {
454 $result = array_merge( $result, json_decode( urldecode( $atts['ad_args'] ), true ) );
455
456 }
457
458 return $result;
459 }
460
461 /**
462 * Return plugin options
463 * these are the options updated by the user
464 *
465 * @return array $options
466 * @since 1.0.1
467 * @todo parse default options
468 */
469 public function options() {
470 // we can’t store options if WPML String Translations is enabled, or it would not translate the "Ad Label" option.
471 if ( ! isset( $this->options ) || class_exists( 'WPML_ST_String' ) ) {
472 $this->options = get_option( ADVADS_SLUG, array() );
473 }
474
475 // disable Ad Health by default for new users.
476 if ( ! isset( $this->options['disable-notices'] )
477 && self::show_to_new_users( 1575892800, 'b' ) ) {
478 $this->options['disable-notices'] = 1;
479 }
480
481 return $this->options;
482 }
483
484 /**
485 * update plugin options (not for settings page, but if automatic options are needed)
486 *
487 * @param array $options new options
488 *
489 * @since 1.5.1
490 */
491 public function update_options( array $options ) {
492 // do not allow to clear options
493 if ( $options === array() ) {
494 return;
495 }
496
497 $this->options = $options;
498 update_option( ADVADS_SLUG, $options );
499 }
500
501 /**
502 * return internal plugin options
503 * these are options set by the plugin
504 *
505 * @return array $options
506 * @since 1.0.1
507 * @todo parse default options
508 */
509 public function internal_options() {
510 if ( ! isset( $this->internal_options ) ) {
511 $defaults = array(
512 'version' => ADVADS_VERSION,
513 'installed' => time(), // when was this installed
514 );
515 $this->internal_options = get_option( ADVADS_SLUG . '-internal', array() );
516
517 // save defaults
518 if ( $this->internal_options === array() ) {
519 $this->internal_options = $defaults;
520 $this->update_internal_options( $this->internal_options );
521
522 Advanced_Ads_Plugin::get_instance()->create_capabilities();
523 }
524
525 // for versions installed prior to 1.5.3 set installed date for now
526 if ( ! isset( $this->internal_options['installed'] ) ) {
527 $this->internal_options['installed'] = time();
528 $this->update_internal_options( $this->internal_options );
529 }
530 }
531
532 return $this->internal_options;
533 }
534
535 /**
536 * update internal plugin options
537 *
538 * @param array $options new internal options
539 *
540 * @since 1.5.1
541 */
542 public function update_internal_options( array $options ) {
543 // do not allow to clear options
544 if ( $options === array() ) {
545 return;
546 }
547
548 $this->internal_options = $options;
549 update_option( ADVADS_SLUG . '-internal', $options );
550 }
551
552 /**
553 * get prefix used for frontend elements
554 *
555 * @since 1.6.8.2
556 */
557 public function get_frontend_prefix() {
558 if ( ! $this->frontend_prefix ) {
559 $options = $this->options();
560
561 if ( ! isset( $options['front-prefix'] ) ) {
562 if ( isset( $options['id-prefix'] ) ) {
563 // deprecated: keeps widgets working that previously received an id based on the front-prefix
564 $frontend_prefix = esc_attr( $options['id-prefix'] );
565 } else {
566 $host = parse_url( get_home_url(), PHP_URL_HOST );
567 $frontend_prefix = preg_match( '/[A-Za-z][A-Za-z0-9_]{4}/', $host, $result ) ? $result[0] . '-' : Advanced_Ads_Plugin::DEFAULT_FRONTEND_PREFIX;
568 }
569 } else {
570 $frontend_prefix = esc_attr( $options['front-prefix'] );
571 }
572 /**
573 * Applying the filter here makes sure that it is the same frontend prefix for all
574 * calls on this page impression
575 */
576 $this->frontend_prefix = apply_filters( 'advanced-ads-frontend-prefix', $frontend_prefix );
577 }
578
579 return $this->frontend_prefix;
580 }
581
582 /**
583 * get priority used for injection inside content
584 *
585 * @since 1.6.10.2
586 */
587 public function get_content_injection_priority() {
588 $options = $this->options();
589
590 return isset( $options['content-injection-priority'] ) ? intval( $options['content-injection-priority'] ) : 100;
591 }
592
593 /**
594 * returns the capability needed to perform an action
595 *
596 * @param str $capability a capability to check, can be internal to Advanced Ads
597 *
598 * @return str $capability a valid WordPress capability
599 * @since 1.6.14
600 */
601 public static function user_cap( $capability = 'manage_options' ) {
602
603 global $advanced_ads_capabilities;
604
605 // admins can do everything
606 // is also a fallback if no option or more specific capability is given
607 if ( current_user_can( 'manage_options' ) ) {
608 return 'manage_options';
609 }
610
611 return apply_filters( 'advanced-ads-capability', $capability );
612
613 // check, if capability is mapped to an existing WP capability
614 /*if( isset( $advanced_ads_capabilities[ $capability ] ) ){
615 return apply_filters( 'advanced-ads-capability', $advanced_ads_capabilities[ $capability ], $capability );
616 } else {
617 // if not, use 'manage_posts' capability
618 return apply_filters( 'advanced-ads-capability', 'manage_options', $capability );
619 }*/
620
621 }
622
623 /**
624 * Create roles and capabilities
625 *
626 */
627 public function create_capabilities() {
628 if ( $role = get_role( 'administrator' ) ) {
629 $role->add_cap( 'advanced_ads_manage_options' );
630 $role->add_cap( 'advanced_ads_see_interface' );
631 $role->add_cap( 'advanced_ads_edit_ads' );
632 $role->add_cap( 'advanced_ads_manage_placements' );
633 $role->add_cap( 'advanced_ads_place_ads' );
634 }
635 }
636
637 /**
638 * Remove roles and capabilities
639 *
640 */
641 public function remove_capabilities() {
642 if ( $role = get_role( 'administrator' ) ) {
643 $role->remove_cap( 'advanced_ads_manage_options' );
644 $role->remove_cap( 'advanced_ads_see_interface' );
645 $role->remove_cap( 'advanced_ads_edit_ads' );
646 $role->remove_cap( 'advanced_ads_manage_placements' );
647 $role->remove_cap( 'advanced_ads_place_ads' );
648 }
649 }
650
651 /**
652 * Fired when the plugin is uninstalled.
653 */
654 public static function uninstall() {
655 $advads_options = Advanced_Ads::get_instance()->options();
656
657 if ( ! empty( $advads_options['uninstall-delete-data'] ) ) {
658 global $wpdb;
659 $main_blog_id = $wpdb->blogid;
660
661 Advanced_Ads::get_instance()->create_post_types();
662
663 if ( ! is_multisite() ) {
664 Advanced_Ads_Plugin::get_instance()->uninstall_single();
665 } else {
666 $blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" );
667
668 foreach ( $blog_ids as $blog_id ) {
669 switch_to_blog( $blog_id );
670 Advanced_Ads_Plugin::get_instance()->uninstall_single();
671 }
672 switch_to_blog( $main_blog_id );
673 }
674
675 // Delete assets (main blog).
676 Advanced_Ads_Ad_Blocker_Admin::get_instance()->clear_assets();
677 delete_option( ADVADS_AB_SLUG );
678 }
679
680 }
681
682 /**
683 * Fired for each blog when the plugin is uninstalled.
684 *
685 */
686 protected function uninstall_single() {
687 global $wpdb;
688
689 // Ads.
690 $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s", Advanced_Ads::POST_TYPE_SLUG ) );
691
692 if ( $post_ids ) {
693 $wpdb->delete(
694 $wpdb->posts,
695 array( 'post_type' => Advanced_Ads::POST_TYPE_SLUG ),
696 array( '%s' )
697 );
698
699 $wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE post_id IN( " . implode( ',', $post_ids ) . " )" );
700 }
701
702 // Groups.
703 $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 ) );
704
705 foreach ( $term_ids as $term_id ) {
706 wp_delete_term( $term_id, Advanced_Ads::AD_GROUP_TAXONOMY );
707 }
708
709 delete_option( 'advads-ad-groups' );
710 delete_option( Advanced_Ads::AD_GROUP_TAXONOMY . '_children' );
711 delete_option( 'advads-ad-weights' );
712
713 // Placements.
714 delete_option( 'advads-ads-placements' );
715
716 // User metadata.
717 delete_metadata( 'user', null, 'advanced-ads-hide-wizard', '', true );
718 delete_metadata( 'user', null, 'advanced-ads-subscribed', '', true );
719
720 // Post metadata.
721 delete_metadata( 'post', null, '_advads_ad_settings', '', true );
722
723 // Transients.
724 delete_transient( ADVADS_SLUG . '_add-on-updates-checked' );
725
726 delete_option( GADSENSE_OPT_NAME );
727 delete_option( ADVADS_SLUG );
728 delete_option( ADVADS_SLUG . '-internal' );
729 delete_option( ADVADS_SLUG . '-notices' );
730
731 // Widget.
732 $base_widget_id = Advanced_Ads_Widget::get_base_id();
733 delete_option( 'widget_' . $base_widget_id );
734
735 do_action( 'advanced-ads-uninstall' );
736
737 wp_cache_flush();
738 }
739
740 /**
741 * Check if any add-on is activated
742 *
743 * @return bool true if there is any add-on activated
744 */
745 public static function any_activated_add_on() {
746 return ( defined( 'AAP_VERSION' ) // Advanced Ads Pro.
747 || defined( 'AASA_VERSION' ) // Selling Ads.
748 || defined( 'AAT_VERSION' ) // Tracking.
749 || defined( 'AASADS_VERSION' ) // Sticky Ads.
750 || defined( 'AAR_VERSION' ) // Responsive Ads.
751 || defined( 'AAPLDS_VERSION' ) // PopUp and Layer Ads.
752 || defined( 'AAGT_SLUG' ) // Geo-Targeting.
753 );
754 }
755
756 /**
757 * Get the correct support URL: wp.org for free users and website for those with any add-on installed
758 *
759 * @param string $utm add UTM parameter to the link leading to https://wpadvancedads.com, if given.
760 *
761 * @return string URL.
762 */
763 public static function support_url( $utm = '' ) {
764
765 // return self::any_activated_add_on() ? ADVADS_URL . 'support/' .$utm : 'https://wordpress.org/support/plugin/advanced-ads#new-post';
766 // $utm = empty( $utm ) ? '#utm_source=advanced-ads&utm_medium=link&utm_campaign=disable-support' : $utm;
767 if ( self::any_activated_add_on() ) {
768 $url = ADVADS_URL . 'support/' . $utm . '-with-addons';
769 } else {
770 $url = ADVADS_URL . 'support/' . $utm . '-free-user';
771 }
772
773 return $url;
774 }
775
776 /**
777 * Create a random group
778 *
779 * @param string $url optional parameter.
780 * @param string $ex
781 *
782 * @return bool
783 */
784 public static function get_group_by_url( $url = '', $ex = 'a' ) {
785
786 $url = empty( $url ) ? home_url() : $url;
787
788 $code = intval( substr( md5( $url ), - 1 ), 16 );
789
790 switch ( $ex ) {
791 case 'b':
792 return ( $code & 2 ) >> 1; // returns 1 or 0.
793 case 'c':
794 return ( $code & 4 ) >> 2; // returns 1 or 0.
795 case 'd':
796 return ( $code & 8 ) >> 3; // returns 1 or 0.
797 default:
798 return $code & 1; // returns 1 or 0.
799 }
800 }
801
802 /**
803 * Check if user started after a given date
804 *
805 * @param integer $timestamp time stamp.
806 *
807 * @return bool true if user is added after timestamp.
808 */
809 public static function is_new_user( $timestamp = 0 ) {
810
811 $timestamp = absint( $timestamp );
812
813 $options = self::get_instance()->internal_options();
814 $installed = isset( $options['installed'] ) ? $options['installed'] : 0;
815
816 return ( $installed >= $timestamp );
817 }
818
819 /**
820 * Show stuff to new users only.
821 *
822 * @param integer $timestamp time after which to show whatever.
823 * @param string $group optional group.
824 *
825 * @return bool true if user enabled after given timestamp.
826 */
827 public static function show_to_new_users( $timestamp, $group = 'a' ) {
828
829 // allow admins to see the change in any case.
830 if ( current_user_can( self::user_cap( 'advanced_ads_manage_options' ) )
831 && isset( $_REQUEST['advads-ignore-timestamp'] ) ) {
832 return true;
833 }
834
835 return ( self::get_group_by_url( null, $group ) && self::is_new_user( $timestamp ) );
836 }
837 }
838