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