PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 1.10.12
Advanced Ads – Ad Manager & AdSense v1.10.12
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 7 years ago ad-ajax.php 8 years ago ad-debug.php 8 years ago ad-model.php 8 years ago ad-select.php 9 years ago ad.php 7 years ago ad_ajax_callbacks.php 7 years ago ad_group.php 7 years ago ad_placements.php 7 years ago ad_type_abstract.php 8 years ago ad_type_content.php 8 years ago ad_type_dummy.php 8 years ago ad_type_group.php 8 years ago ad_type_image.php 7 years ago ad_type_plain.php 7 years ago checks.php 7 years ago compatibility.php 7 years ago display-conditions.php 7 years ago filesystem.php 8 years ago frontend_checks.php 7 years ago plugin.php 7 years ago upgrades.php 9 years ago utils.php 7 years ago visitor-conditions.php 7 years ago widget.php 7 years ago
plugin.php
689 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 }
63
64 /**
65 *
66 * @return Advanced_Ads_Plugin
67 */
68 public static function get_instance() {
69 // If the single instance hasn't been set, set it now.
70 if ( null === self::$instance ) {
71 self::$instance = new self;
72 }
73
74 return self::$instance;
75 }
76
77 /**
78 *
79 * @param Advanced_Ads_Model $model
80 */
81 public function set_model(Advanced_Ads_Model $model) {
82 $this->model = $model;
83 }
84
85 public function wp_plugins_loaded() {
86 // Load plugin text domain
87 $this->load_plugin_textdomain();
88
89 $internal_options = $this->internal_options();
90
91 /**
92 * run upgrades, if this is a new version or version does not exist
93 */
94 if ( ! defined( 'DOING_AJAX' ) && ( ! isset( $internal_options['version'] ) || version_compare( $internal_options['version'], ADVADS_VERSION, '<' ) ) ) {
95 new Advanced_Ads_Upgrades();
96 }
97
98 // activate plugin when new blog is added on multisites // -TODO this is admin-only
99 add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) );
100
101 // Load public-facing style sheet and JavaScript.
102 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
103 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
104 add_action( 'wp_head', array( $this, 'print_head_scripts' ), 7 );
105
106 // add short codes
107 add_shortcode( 'the_ad', array( $this, 'shortcode_display_ad' ) );
108 add_shortcode( 'the_ad_group', array( $this, 'shortcode_display_ad_group' ) );
109 add_shortcode( 'the_ad_placement', array( $this, 'shortcode_display_ad_placement' ) );
110
111 // remove default ad group menu item // -TODO only for admin
112 add_action( 'admin_menu', array( $this, 'remove_taxonomy_menu_item' ) );
113 // load widgets
114 add_action( 'widgets_init', array( $this, 'widget_init' ) );
115
116 // load display conditions
117 Advanced_Ads_Display_Conditions::get_instance();
118 new Advanced_Ads_Frontend_Checks;
119 new Advanced_Ads_Compatibility;
120 }
121
122 /**
123 * Register and enqueue public-facing style sheet.
124 *
125 * @since 1.0.0
126 */
127 public function enqueue_styles() {
128 // wp_enqueue_style( $this->get_plugin_slug() . '-plugin-styles', plugins_url('assets/css/public.css', __FILE__), array(), ADVADS_VERSION);
129 }
130
131 /**
132 * Return the plugin slug.
133 *
134 * @since 1.0.0
135 * @return Plugin slug variable.
136 */
137 public function get_plugin_slug() {
138 return ADVADS_SLUG;
139 }
140
141 /**
142 * Register and enqueues public-facing JavaScript files.
143 *
144 * @since 1.0.0
145 */
146 public function enqueue_scripts() {
147 // wp_enqueue_script( $this->get_plugin_slug() . '-plugin-script', plugins_url('assets/js/public.js', __FILE__), array('jquery'), ADVADS_VERSION);
148 $options = $this->options();
149 $activated_js = apply_filters( 'advanced-ads-activate-advanced-js', isset( $options['advanced-js'] ) );
150 if ( $activated_js ){
151 wp_enqueue_script( $this->get_plugin_slug() . '-advanced-js', ADVADS_BASE_URL . 'public/assets/js/advanced.js', array( 'jquery' ), ADVADS_VERSION );
152 }
153 }
154
155 /**
156 * Print public-facing JavaScript in the HTML head.
157 *
158 * @since untagged
159 */
160 public function print_head_scripts() {
161 /**
162 * Usage example in add-ons:
163 * ( window.advanced_ads_ready || jQuery( document ).ready ).call( null, function() {
164 * // Called when DOM is ready.
165 * } );
166 */
167
168 echo apply_filters( 'advanced-ads-attribution', sprintf( '<!-- managing ads with Advanced Ads – %s -->', ADVADS_URL ) );
169
170 ob_start();
171 ?><script>
172 <?php if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
173 readfile( ADVADS_BASE_PATH . 'public/assets/js/ready.js' );
174 } else { ?>
175 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)}}();
176 <?php
177 }
178
179 // Output privacy options.
180 $privacy_options = Advanced_Ads_Privacy::get_instance()->options();
181 if ( ! empty( $privacy_options['enabled'] ) ) {
182 printf( '(advads_options = window.advads_options || {} )["privacy"] = %s;', json_encode( $privacy_options ) );
183 }
184
185 ?></script><?php
186 echo Advanced_Ads_Utils::get_inline_asset( ob_get_clean() );
187
188
189 }
190
191 public function widget_init() {
192 register_widget( 'Advanced_Ads_Widget' );
193 }
194
195 /**
196 * Fired when a new site is activated with a WPMU environment.
197 *
198 * @since 1.0.0
199 * @param int $blog_id ID of the new blog.
200 */
201 public function activate_new_site($blog_id) {
202
203 if ( 1 !== did_action( 'wpmu_new_blog' ) ) {
204 return;
205 }
206
207 switch_to_blog( $blog_id );
208 $this->single_activate();
209 restore_current_blog();
210 }
211
212 /**
213 * Fired for each blog when the plugin is activated.
214 *
215 * @since 1.0.0
216 */
217 protected function single_activate() {
218 // $this->post_types_rewrite_flush();
219 // -TODO inform modules
220 $this->create_capabilities();
221 }
222
223 /**
224 * Fired for each blog when the plugin is deactivated.
225 *
226 * @since 1.0.0
227 */
228 protected function single_deactivate() {
229 // -TODO inform modules
230 $this->remove_capabilities();
231 }
232
233 /**
234 * Load the plugin text domain for translation.
235 *
236 * @since 1.0.0
237 */
238 public function load_plugin_textdomain() {
239 // $locale = apply_filters('advanced-ads-plugin-locale', get_locale(), $domain);
240 load_plugin_textdomain( 'advanced-ads', false, ADVADS_BASE_DIR . '/languages' );
241 }
242
243 /**
244 * Fired when the plugin is activated.
245 *
246 * @since 1.0.0
247 * @param boolean $network_wide True if WPMU superadmin uses
248 * "Network Activate" action, false if
249 * WPMU is disabled or plugin is
250 * activated on an individual blog.
251 */
252 public function activate($network_wide) {
253 if ( function_exists( 'is_multisite' ) && is_multisite() ) {
254
255 if ( $network_wide ) {
256 // Get all blog ids
257 global $wpdb;
258 $blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" );
259 $original_blog_id = $wpdb->blogid;
260
261 foreach ( $blog_ids as $blog_id ) {
262 switch_to_blog( $blog_id );
263 $this->single_activate();
264 }
265
266 switch_to_blog( $original_blog_id );
267 } else {
268 $this->single_activate();
269 }
270 } else {
271 $this->single_activate();
272 }
273 }
274
275 /**
276 * Fired when the plugin is deactivated.
277 *
278 * @since 1.0.0
279 * @param boolean $network_wide
280 *
281 * True if WPMU superadmin uses
282 * "Network Deactivate" action, false if
283 * WPMU is disabled or plugin is
284 * deactivated on an individual blog.
285 */
286 public function deactivate($network_wide) {
287 if ( function_exists( 'is_multisite' ) && is_multisite() ) {
288
289 if ( $network_wide ) {
290 // Get all blog ids
291 global $wpdb;
292 $blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" );
293 $original_blog_id = $wpdb->blogid;
294
295 foreach ( $blog_ids as $blog_id ) {
296 switch_to_blog( $blog_id );
297 $this->single_deactivate();
298 }
299
300 switch_to_blog( $original_blog_id );
301 } else {
302 $this->single_deactivate();
303 }
304 } else {
305 $this->single_deactivate();
306 }
307 }
308
309 /**
310 * flush rewrites on plugin activation so permalinks for them work from the beginning on
311 *
312 * @since 1.0.0
313 * @link http://codex.wordpress.org/Function_Reference/register_post_type#Flushing_Rewrite_on_Activation
314 */
315 /*public function post_types_rewrite_flush(){
316 // load custom post type
317 Advanced_Ads::get_instance()->create_post_types();
318 // flush rewrite rules
319 flush_rewrite_rules();
320 }*/
321
322 /**
323 * remove WP tag edit page for the ad group taxonomy
324 * needed, because we can’t remove it with `show_ui` without also removing the meta box
325 *
326 * @since 1.0.0
327 */
328 public function remove_taxonomy_menu_item() {
329 remove_submenu_page( 'edit.php?post_type=advanced_ads', 'edit-tags.php?taxonomy=advanced_ads_groups&amp;post_type=advanced_ads' );
330 }
331
332 /**
333 * shortcode to include ad in frontend
334 *
335 * @since 1.0.0
336 * @param arr $atts
337 */
338 public function shortcode_display_ad($atts){
339 $atts = is_array( $atts ) ? $atts : array();
340 $id = isset($atts['id']) ? (int) $atts['id'] : 0;
341 $atts = $this->prepare_shortcode_atts( $atts );
342
343 // use the public available function here
344 return get_ad( $id, $atts );
345 }
346
347 /**
348 * shortcode to include ad from an ad group in frontend
349 *
350 * @since 1.0.0
351 * @param arr $atts
352 */
353 public function shortcode_display_ad_group($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_group( $id, $atts );
360 }
361
362 /**
363 * shortcode to display content of an ad placement in frontend
364 *
365 * @since 1.1.0
366 * @param arr $atts
367 */
368 public function shortcode_display_ad_placement($atts){
369 $atts = is_array( $atts ) ? $atts : array();
370 $id = isset($atts['id']) ? (string) $atts['id'] : '';
371 $atts = $this->prepare_shortcode_atts( $atts );
372
373 // use the public available function here
374 return get_ad_placement( $id, $atts );
375 }
376
377 /**
378 * Prepare shortcode attributes.
379 *
380 * @param array $atts array with strings
381 * @return array
382 */
383 private function prepare_shortcode_atts( $atts ) {
384 $result = array();
385
386 /**
387 * Prepare attributes by converting strings to multi-dimensional array
388 * Example: [ 'output__margin__top' => 1 ] => ['output']['margin']['top'] = 1
389 */
390 if ( ! defined( 'ADVANCED_ADS_DISABLE_CHANGE' ) || ! ADVANCED_ADS_DISABLE_CHANGE ) {
391 foreach ( $atts as $attr => $data ) {
392 $levels = explode( '__', $attr );
393 $last = array_pop( $levels );
394
395 $cur_lvl = &$result;
396
397 foreach ( $levels as $lvl ) {
398 if ( ! isset( $cur_lvl[ $lvl ] ) ) {
399 $cur_lvl[ $lvl ] = array();
400 }
401
402 $cur_lvl = &$cur_lvl[ $lvl ];
403 }
404
405 $cur_lvl[ $last ] = $data;
406 }
407
408 $result = array_diff_key( $result, array( 'id' => false, 'blog_id' => false, 'ad_args' => false ) );
409 }
410
411 // Ad type: 'content' and a shortcode inside.
412 if ( isset( $atts['ad_args'] ) ) {
413 $result = array_merge( $result, json_decode( urldecode( $atts['ad_args'] ) ,true) );
414
415 }
416
417 return $result;
418 }
419
420 /**
421 * return plugin options
422 * these are the options updated by the user
423 *
424 * @since 1.0.1
425 * @return array $options
426 * @todo parse default options
427 */
428 public function options() {
429 if ( ! isset( $this->options ) ) {
430 $this->options = get_option( ADVADS_SLUG, array() );
431 }
432
433 return $this->options;
434 }
435
436 /**
437 * update plugin options (not for settings page, but if automatic options are needed)
438 *
439 * @since 1.5.1
440 * @param array $options new options
441 */
442 public function update_options( array $options ) {
443 // do not allow to clear options
444 if ( $options === array() ) {
445 return;
446 }
447
448 $this->options = $options;
449 update_option( ADVADS_SLUG, $options );
450 }
451
452 /**
453 * return internal plugin options
454 * these are options set by the plugin
455 *
456 * @since 1.0.1
457 * @return array $options
458 * @todo parse default options
459 */
460 public function internal_options() {
461 if ( ! isset( $this->internal_options ) ) {
462 $defaults = array(
463 'version' => ADVADS_VERSION,
464 'installed' => time(), // when was this installed
465 );
466 $this->internal_options = get_option( ADVADS_SLUG . '-internal', array() );
467
468 // save defaults
469 if($this->internal_options === array()){
470 $this->internal_options = $defaults;
471 $this->update_internal_options($this->internal_options);
472
473 Advanced_Ads_Plugin::get_instance()->create_capabilities();
474 }
475
476 // for versions installed prior to 1.5.3 set installed date for now
477 if( ! isset( $this->internal_options['installed'] )){
478 $this->internal_options['installed'] = time();
479 $this->update_internal_options($this->internal_options);
480 }
481 }
482
483 return $this->internal_options;
484 }
485
486 /**
487 * update internal plugin options
488 *
489 * @since 1.5.1
490 * @param array $options new internal options
491 */
492 public function update_internal_options( array $options ) {
493 // do not allow to clear options
494 if ( $options === array() ) {
495 return;
496 }
497
498 $this->internal_options = $options;
499 update_option( ADVADS_SLUG . '-internal', $options );
500 }
501
502 /**
503 * get prefix used for frontend elements
504 *
505 * @since 1.6.8.2
506 */
507 public function get_frontend_prefix(){
508 if ( ! $this->frontend_prefix ) {
509 $options = $this->options();
510
511 if ( ! isset( $options['front-prefix'] ) ) {
512 if ( isset( $options['id-prefix'] ) ) {
513 // deprecated: keeps widgets working that previously received an id based on the front-prefix
514 $frontend_prefix = esc_attr( $options['id-prefix'] );
515 } else {
516 $host = parse_url( get_home_url(), PHP_URL_HOST );
517 $frontend_prefix = preg_match( '/[A-Za-z][A-Za-z0-9_]{4}/', $host, $result ) ? $result[0] . '-' : Advanced_Ads_Plugin::DEFAULT_FRONTEND_PREFIX;
518 }
519 } else {
520 $frontend_prefix = esc_attr( $options['front-prefix'] );
521 }
522 /**
523 * Applying the filter here makes sure that it is the same frontend prefix for all
524 * calls on this page impression
525 */
526 $this->frontend_prefix = apply_filters( 'advanced-ads-frontend-prefix', $frontend_prefix );
527 }
528 return $this->frontend_prefix;
529 }
530
531 /**
532 * get priority used for injection inside content
533 *
534 * @since 1.6.10.2
535 */
536 public function get_content_injection_priority(){
537 $options = $this->options();
538
539 return isset( $options['content-injection-priority'] ) ? intval( $options['content-injection-priority'] ) : 100;
540 }
541
542 /**
543 * returns the capability needed to perform an action
544 *
545 * @since 1.6.14
546 * @param str $capability a capability to check, can be internal to Advanced Ads
547 * @return str $capability a valid WordPress capability
548 */
549 public static function user_cap( $capability = 'manage_options' ){
550
551 global $advanced_ads_capabilities;
552
553 // admins can do everything
554 // is also a fallback if no option or more specific capability is given
555 if( current_user_can( 'manage_options' ) ){
556 return 'manage_options';
557 }
558
559 return apply_filters( 'advanced-ads-capability', $capability );
560
561 // check, if capability is mapped to an existing WP capability
562 /*if( isset( $advanced_ads_capabilities[ $capability ] ) ){
563 return apply_filters( 'advanced-ads-capability', $advanced_ads_capabilities[ $capability ], $capability );
564 } else {
565 // if not, use 'manage_posts' capability
566 return apply_filters( 'advanced-ads-capability', 'manage_options', $capability );
567 }*/
568
569 }
570
571 /**
572 * Create roles and capabilities
573 *
574 */
575 public function create_capabilities() {
576 if ( $role = get_role( 'administrator' ) ) {
577 $role->add_cap( 'advanced_ads_manage_options' );
578 $role->add_cap( 'advanced_ads_see_interface' );
579 $role->add_cap( 'advanced_ads_edit_ads' );
580 $role->add_cap( 'advanced_ads_manage_placements' );
581 $role->add_cap( 'advanced_ads_place_ads' );
582 }
583 }
584
585 /**
586 * Remove roles and capabilities
587 *
588 */
589 public function remove_capabilities() {
590 if ( $role = get_role( 'administrator' ) ) {
591 $role->remove_cap( 'advanced_ads_manage_options' );
592 $role->remove_cap( 'advanced_ads_see_interface' );
593 $role->remove_cap( 'advanced_ads_edit_ads' );
594 $role->remove_cap( 'advanced_ads_manage_placements' );
595 $role->remove_cap( 'advanced_ads_place_ads' );
596 }
597 }
598
599 /**
600 * Fired when the plugin is uninstalled.
601 */
602 public static function uninstall() {
603 $advads_options = Advanced_Ads::get_instance()->options();
604
605 if ( ! empty( $advads_options['uninstall-delete-data'] ) ) {
606 global $wpdb;
607 $main_blog_id = $wpdb->blogid;
608
609 Advanced_Ads::get_instance()->create_post_types();
610
611 if ( ! is_multisite() ) {
612 Advanced_Ads_Plugin::get_instance()->uninstall_single();
613 } else {
614 $blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" );
615
616 foreach ( $blog_ids as $blog_id ) {
617 switch_to_blog( $blog_id );
618 Advanced_Ads_Plugin::get_instance()->uninstall_single();
619 }
620 switch_to_blog( $main_blog_id );
621 }
622
623 // Delete assets (main blog).
624 Advanced_Ads_Ad_Blocker_Admin::get_instance()->clear_assets();
625 delete_option( ADVADS_AB_SLUG );
626 }
627
628 }
629
630 /**
631 * Fired for each blog when the plugin is uninstalled.
632 *
633 */
634 protected function uninstall_single() {
635 global $wpdb;
636
637 // Ads.
638 $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s", Advanced_Ads::POST_TYPE_SLUG ) );
639
640 if ( $post_ids ) {
641 $wpdb->delete(
642 $wpdb->posts,
643 array( 'post_type' => Advanced_Ads::POST_TYPE_SLUG ),
644 array( '%s' )
645 );
646
647 $wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE post_id IN( " . implode( ',', $post_ids ) . " )" );
648 }
649
650 // Groups.
651 $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 ) );
652
653 foreach ( $term_ids as $term_id ) {
654 wp_delete_term( $term_id, Advanced_Ads::AD_GROUP_TAXONOMY );
655 }
656
657 delete_option( 'advads-ad-groups' );
658 delete_option( Advanced_Ads::AD_GROUP_TAXONOMY . '_children' );
659 delete_option( 'advads-ad-weights' );
660
661 // Placements.
662 delete_option( 'advads-ads-placements' );
663
664 // User metadata.
665 delete_metadata( 'user', null, 'advanced-ads-hide-wizard', '', true );
666 delete_metadata( 'user', null, 'advanced-ads-subscribed', '', true );
667
668 // Post metadata.
669 delete_metadata( 'post', null, '_advads_ad_settings', '', true );
670
671 // Transients.
672 delete_transient( ADVADS_SLUG . '_add-on-updates-checked' );
673
674 delete_option( GADSENSE_OPT_NAME );
675 delete_option( ADVADS_SLUG );
676 delete_option( ADVADS_SLUG . '-internal' );
677 delete_option( ADVADS_SLUG . '-notices' );
678
679 // Widget.
680 $base_widget_id = Advanced_Ads_Widget::get_base_id();
681 delete_option( 'widget_' . $base_widget_id );
682
683 do_action( 'advanced-ads-uninstall' );
684
685 wp_cache_flush();
686 }
687
688 }
689