PluginProbe
Advanced Ads – Ad Manager & AdSense / 1.40.2
Advanced Ads – Ad Manager & AdSense v1.40.2
2.0.26 2.0.25 2.0.24 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 All 348 releases
advanced-ads / admin / includes / class-settings.php
class-settings.php
679 lines 19.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class Advanced_Ads_Admin_Settings
5 */
6 class Advanced_Ads_Admin_Settings {
7 /**
8 * Instance of this class.
9 *
10 * @var object
11 */
12 protected static $instance = null;
13
14 /**
15 * Advanced_Ads_Admin_Settings constructor.
16 */
17 private function __construct() {
18 // settings handling.
19 add_action( 'admin_init', [ $this, 'settings_init' ] );
20
21 // add ad admin capabilities for settings.
22 add_action( 'admin_init', [ $this, 'settings_capabilities' ], 20 );
23 }
24
25 /**
26 * Return an instance of this class.
27 *
28 * @return object A single instance of this class.
29 */
30 public static function get_instance() {
31 // If the single instance hasn't been set, set it now.
32 if ( null === self::$instance ) {
33 self::$instance = new self();
34 }
35
36 return self::$instance;
37 }
38
39 /**
40 * Initialize settings
41 *
42 * @since 1.0.1
43 */
44 public function settings_init() {
45
46 // get settings page hook.
47 $hook = Advanced_Ads_Admin::get_instance()->plugin_screen_hook_suffix;
48
49 // register settings.
50 register_setting( ADVADS_SLUG, ADVADS_SLUG, [ $this, 'sanitize_settings' ] );
51
52 // "Management" settings section.
53 add_settings_section(
54 'advanced_ads_setting_section',
55 __( 'Admin', 'advanced-ads' ),
56 [ $this, 'render_settings_section_callback' ],
57 $hook
58 );
59
60 // "Disable ads" settings section.
61 add_settings_section(
62 'advanced_ads_setting_section_disable_ads',
63 __( 'Disable ads', 'advanced-ads' ),
64 [ $this, 'render_settings_section_disable_ads_callback' ],
65 $hook
66 );
67
68 // "Layout / Output" settings section.
69 add_settings_section(
70 'advanced_ads_setting_section_output',
71 __( 'Layout / Output', 'advanced-ads' ),
72 [ $this, 'render_settings_section_output_callback' ],
73 $hook
74 );
75
76 // "Content injection" settings section.
77 add_settings_section(
78 'advanced_ads_setting_section_injection',
79 __( 'Content injection', 'advanced-ads' ),
80 [ $this, 'render_settings_section_injection_callback' ],
81 $hook
82 );
83
84 // Pro pitch section.
85 if ( ! defined( 'AAP_VERSION' ) ) {
86 add_settings_section(
87 'advanced_ads_settings_pro_pitch_section',
88 '',
89 [ $this, 'render_settings_pro_pitch_section_callback' ],
90 'advanced-ads-settings-pro-pitch-page'
91 );
92
93 add_filter( 'advanced-ads-setting-tabs', [ $this, 'pro_pitch_tab' ] );
94 }
95
96 // Tracking pitch section.
97 if ( ! defined( 'AAT_VERSION' ) ) {
98 add_settings_section(
99 'advanced_ads_settings_tracking_pitch_section',
100 '',
101 [ $this, 'render_settings_tracking_pitch_section_callback' ],
102 'advanced-ads-settings-tracking-pitch-page'
103 );
104
105 add_filter( 'advanced-ads-setting-tabs', [ $this, 'tracking_pitch_tab' ] );
106 }
107
108 // licenses section.
109 // register license settings.
110 register_setting( ADVADS_SLUG . '-licenses', ADVADS_SLUG . '-licenses' );
111
112 /**
113 * Allow Ad Admin to save license settings..
114 *
115 * @param string[] $options Array with allowed options.
116 *
117 * @return string[]
118 */
119 add_filter( 'advanced-ads-ad-admin-options', function( $options ) {
120 $options[] = ADVADS_SLUG . '-licenses';
121
122 return $options;
123 } );
124
125 add_settings_section(
126 'advanced_ads_settings_license_section',
127 '',
128 [ $this, 'render_settings_licenses_section_callback' ],
129 'advanced-ads-settings-license-page'
130 );
131
132 add_filter( 'advanced-ads-setting-tabs', [ $this, 'license_tab' ] );
133
134 add_settings_section(
135 'advanced_ads_settings_license_pitch_section',
136 '',
137 [ $this, 'render_settings_licenses_pitch_section_callback' ],
138 'advanced-ads-settings-license-page'
139 );
140
141 // add setting fields to disable ads.
142 add_settings_field(
143 'disable-ads',
144 __( 'Disable ads', 'advanced-ads' ),
145 [ $this, 'render_settings_disable_ads' ],
146 $hook,
147 'advanced_ads_setting_section_disable_ads'
148 );
149 // add setting fields for user role.
150 add_settings_field(
151 'hide-for-user-role',
152 __( 'Hide ads for user roles', 'advanced-ads' ),
153 [ $this, 'render_settings_hide_for_users' ],
154 $hook,
155 'advanced_ads_setting_section_disable_ads'
156 );
157 // add setting fields for content injection protection.
158 add_settings_field(
159 'content-injection-everywhere',
160 __( 'Unlimited ad injection', 'advanced-ads' ),
161 [ $this, 'render_settings_content_injection_everywhere' ],
162 $hook,
163 'advanced_ads_setting_section_injection'
164 );
165 // add setting fields for content injection priority.
166 add_settings_field(
167 'content-injection-priority',
168 __( 'Priority of content injection filter', 'advanced-ads' ),
169 [ $this, 'render_settings_content_injection_priority' ],
170 $hook,
171 'advanced_ads_setting_section_injection'
172 );
173 // add setting fields to remove injection level limitation.
174 add_settings_field(
175 'content-injection-level-limitation',
176 __( 'Disable level limitation', 'advanced-ads' ),
177 [ $this, 'render_settings_content_injection_level_limitation' ],
178 $hook,
179 'advanced_ads_setting_section_injection'
180 );
181 // add setting fields for hiding ads from bots.
182 add_settings_field(
183 'block-bots',
184 __( 'Hide ads from bots', 'advanced-ads' ),
185 [ $this, 'render_settings_block_bots' ],
186 $hook,
187 'advanced_ads_setting_section_disable_ads'
188 );
189 // dummy setting field to hide ads for post types.
190 if ( ! defined( 'AAP_VERSION' ) ) {
191 add_settings_field(
192 'disable-by-post-types-pro',
193 __( 'Disable ads for post types', 'advanced-ads' ),
194 [ $this, 'render_settings_disable_post_types' ],
195 $hook,
196 'advanced_ads_setting_section_disable_ads'
197 );
198 }
199 // opt out from internal notices.
200 add_settings_field(
201 'disable-notices',
202 __( 'Disable Ad Health and other notices', 'advanced-ads' ),
203 [ $this, 'render_settings_disabled_notices' ],
204 $hook,
205 'advanced_ads_setting_section'
206 );
207 // opt out from internal notices.
208 add_settings_field(
209 'front-prefix',
210 __( 'ID prefix', 'advanced-ads' ),
211 [ $this, 'render_settings_front_prefix' ],
212 $hook,
213 'advanced_ads_setting_section_output'
214 );
215 // allow editors to manage ads.
216 add_settings_field(
217 'editors-manage-ads',
218 __( 'Allow editors to manage ads', 'advanced-ads' ),
219 [ $this, 'render_settings_editors_manage_ads' ],
220 $hook,
221 'advanced_ads_setting_section'
222 );
223
224 // add unfiltered_html option to multisites
225 if ( is_multisite() && current_user_can( 'unfiltered_html' ) ) {
226 add_settings_field(
227 'allow-unfiltered-html',
228 /* translators: unfiltered_html */
229 sprintf( __( 'Add the %s capability to user roles on multisite', 'advanced-ads' ), '<code>unfiltered_html</code>' ),
230 [ $this, 'renders_settings_allow_unfiltered_html' ],
231 $hook,
232 'advanced_ads_setting_section'
233 );
234 }
235
236 // ad label.
237 add_settings_field(
238 'add-custom-label',
239 __( 'Ad label', 'advanced-ads' ),
240 [ $this, 'render_settings_add_custom_label' ],
241 $hook,
242 'advanced_ads_setting_section_output'
243 );
244
245 // add setting fields.
246 add_settings_field(
247 'link-target',
248 __( 'Open links in a new window', 'advanced-ads' ),
249 [ $this, 'render_settings_link_target_callback' ],
250 $hook,
251 'advanced_ads_setting_section_output'
252 );
253 // add setting fields for advanced js.
254 add_settings_field(
255 'activate-advanced-js',
256 __( 'Use advanced JavaScript', 'advanced-ads' ),
257 [ $this, 'render_settings_advanced_js' ],
258 $hook,
259 'advanced_ads_setting_section_output'
260 );
261
262 // only for main blog.
263 if ( is_main_site( get_current_blog_id() ) ) {
264 add_settings_field(
265 'uninstall-delete-data',
266 __( 'Delete data on uninstall', 'advanced-ads' ),
267 [ $this, 'render_settings_uninstall_delete_data' ],
268 $hook,
269 'advanced_ads_setting_section'
270 );
271 }
272
273 // hook for additional settings from add-ons.
274 do_action( 'advanced-ads-settings-init', $hook );
275 }
276
277 /**
278 * Make sure ad admin can save options.
279 * Add a filter on `admin_init` priority 20 to allow other modules/add-ons to add their options.
280 * Filter option_page_capability_ with the appropriate slug in return to allow the Ad Admin user role to save these settings/options.
281 */
282 public function settings_capabilities() {
283 $ad_admin_options = [ ADVADS_SLUG ];
284 /**
285 * Filters all options that the Ad Admin Role should have access to.
286 *
287 * @param array $ad_admin_options Array with option names.
288 */
289 $ad_admin_options = apply_filters( 'advanced-ads-ad-admin-options', $ad_admin_options );
290 foreach ( $ad_admin_options as $ad_admin_option ) {
291 add_filter( 'option_page_capability_' . $ad_admin_option, function() {
292 return Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options' );
293 } );
294 }
295 }
296
297 /**
298 * Add license tab
299 *
300 * @param array $tabs setting tabs.
301 * @return array
302 */
303 public function license_tab( array $tabs ) {
304
305 $tabs['licenses'] = [
306 'page' => 'advanced-ads-settings-license-page',
307 'group' => ADVADS_SLUG . '-licenses',
308 'tabid' => 'licenses',
309 'title' => __( 'Licenses', 'advanced-ads' ),
310 ];
311
312 return $tabs;
313 }
314
315 /**
316 * Add pro pitch tab
317 *
318 * @param array $tabs setting tabs.
319 *
320 * @return array $tabs
321 */
322 public function pro_pitch_tab( array $tabs ) {
323
324 $tabs['pro_pitch'] = [
325 'page' => 'advanced-ads-settings-pro-pitch-page',
326 // 'group' => ADVADS_SLUG . '-pro-pitch',
327 'tabid' => 'pro-pitch',
328 'title' => __( 'Pro', 'advanced-ads' ),
329 ];
330
331 return $tabs;
332 }
333
334 /**
335 * Add tracking pitch tab
336 *
337 * @param array $tabs setting tabs.
338 *
339 * @return array $tabs
340 */
341 public function tracking_pitch_tab( array $tabs ) {
342
343 $tabs['tracking_pitch'] = [
344 'page' => 'advanced-ads-settings-tracking-pitch-page',
345 'tabid' => 'tracking-pitch',
346 'title' => __( 'Tracking', 'advanced-ads' ),
347 ];
348
349 return $tabs;
350 }
351
352 /**
353 * Render settings section
354 */
355 public function render_settings_section_callback() {
356 // for whatever purpose there might come.
357 }
358
359 /**
360 * Render "Disable Ads" settings section
361 */
362 public function render_settings_section_disable_ads_callback() {
363 // for whatever purpose there might come.
364 }
365
366 /**
367 * Render "Content Injection" settings section
368 */
369 public function render_settings_section_output_callback() {
370 // for whatever purpose there might come.
371 }
372
373 /**
374 * Render "Content Injection" settings section
375 */
376 public function render_settings_section_injection_callback() {
377 // for whatever purpose there might come.
378 }
379
380 /**
381 * Render licenses settings section
382 */
383 public function render_settings_licenses_section_callback() {
384 include ADVADS_BASE_PATH . 'admin/views/settings/license/section.php';
385 }
386
387 /**
388 * Render licenses pithces settings section
389 */
390 public function render_settings_licenses_pitch_section_callback() {
391
392 echo '<h3>' . esc_attr__( 'Are you missing something?', 'advanced-ads' ) . '</h3>';
393
394 Advanced_Ads_Overview_Widgets_Callbacks::render_addons( true );
395 }
396
397 /**
398 * Render pro pitch settings section
399 */
400 public function render_settings_pro_pitch_section_callback() {
401 echo '<br/>';
402 include ADVADS_BASE_PATH . 'admin/views/upgrades/pro-tab.php';
403 }
404
405 /**
406 * Render tracking pitch settings section
407 */
408 public function render_settings_tracking_pitch_section_callback() {
409 echo '<br/>';
410 include ADVADS_BASE_PATH . 'admin/views/upgrades/tracking.php';
411 }
412
413 /**
414 * Options to disable ads
415 */
416 public function render_settings_disable_ads() {
417 $options = Advanced_Ads::get_instance()->options();
418
419 // set the variables.
420 $disable_all = isset( $options['disabled-ads']['all'] ) ? 1 : 0;
421 $disable_404 = isset( $options['disabled-ads']['404'] ) ? 1 : 0;
422 $disable_archives = isset( $options['disabled-ads']['archives'] ) ? 1 : 0;
423 $disable_secondary = isset( $options['disabled-ads']['secondary'] ) ? 1 : 0;
424 $disable_feed = ( ! isset( $options['disabled-ads']['feed'] ) || $options['disabled-ads']['feed'] ) ? 1 : 0;
425 $disable_rest_api = isset( $options['disabled-ads']['rest-api'] ) ? 1 : 0;
426
427 // load the template.
428 include ADVADS_BASE_PATH . 'admin/views/settings/general/disable-ads.php';
429 }
430
431 /**
432 * Render setting to hide ads from logged in users
433 */
434 public function render_settings_hide_for_users() {
435 $options = Advanced_Ads::get_instance()->options();
436 if ( isset( $options['hide-for-user-role'] ) ) {
437 $hide_for_roles = Advanced_Ads_Utils::maybe_translate_cap_to_role( $options['hide-for-user-role'] );
438 } else {
439 $hide_for_roles = [];
440 }
441
442 global $wp_roles;
443 $roles = $wp_roles->get_names();
444
445 include ADVADS_BASE_PATH . 'admin/views/settings/general/hide-for-user-role.php';
446 }
447
448 /**
449 * Render setting to display advanced js file
450 */
451 public function render_settings_advanced_js() {
452 $options = Advanced_Ads::get_instance()->options();
453 $checked = ( ! empty( $options['advanced-js'] ) ) ? 1 : 0;
454
455 include ADVADS_BASE_PATH . 'admin/views/settings/general/advanced-js.php';
456 }
457
458 /**
459 * Render setting for content injection protection
460 */
461 public function render_settings_content_injection_everywhere() {
462 $options = Advanced_Ads::get_instance()->options();
463
464 if ( ! isset( $options['content-injection-everywhere'] ) ) {
465 $everywhere = 0;
466 } elseif ( 'true' === $options['content-injection-everywhere'] ) {
467 $everywhere = - 1;
468 } else {
469 $everywhere = absint( $options['content-injection-everywhere'] );
470 }
471
472 include ADVADS_BASE_PATH . 'admin/views/settings/general/content-injection-everywhere.php';
473 }
474
475 /**
476 * Render setting for content injection priority
477 */
478 public function render_settings_content_injection_priority() {
479 $options = Advanced_Ads::get_instance()->options();
480 $priority = ( isset( $options['content-injection-priority'] ) ) ? (int) $options['content-injection-priority'] : 100;
481
482 include ADVADS_BASE_PATH . 'admin/views/settings/general/content-injection-priority.php';
483 }
484
485 /**
486 * Render setting to disable content injection level limitation
487 */
488 public function render_settings_content_injection_level_limitation() {
489 $options = Advanced_Ads::get_instance()->options();
490 $checked = ( ! empty( $options['content-injection-level-disabled'] ) ) ? 1 : 0;
491
492 include ADVADS_BASE_PATH . 'admin/views/settings/general/content-injection-level-limitation.php';
493 }
494
495 /**
496 * Render setting for blocking bots
497 */
498 public function render_settings_block_bots() {
499 $options = Advanced_Ads::get_instance()->options();
500 $checked = ( ! empty( $options['block-bots'] ) ) ? 1 : 0;
501
502 include ADVADS_BASE_PATH . 'admin/views/settings/general/block-bots.php';
503 }
504
505 /**
506 * Render setting to disable ads by post types
507 */
508 public function render_settings_disable_post_types() {
509
510 $post_types = get_post_types(
511 [
512 'public' => true,
513 'publicly_queryable' => true,
514 ],
515 'objects',
516 'or'
517 );
518 $type_label_counts = array_count_values( wp_list_pluck( $post_types, 'label' ) );
519
520 require ADVADS_BASE_PATH . '/admin/views/settings/general/disable-post-types.php';
521 }
522
523 /**
524 * Render setting to disable notices and Ad Health
525 */
526 public function render_settings_disabled_notices() {
527 $options = Advanced_Ads::get_instance()->options();
528 $checked = ( ! empty( $options['disable-notices'] ) ) ? 1 : 0;
529
530 require ADVADS_BASE_PATH . '/admin/views/settings/general/disable-notices.php';
531 }
532
533 /**
534 * Render setting for frontend prefix
535 */
536 public function render_settings_front_prefix() {
537 $options = Advanced_Ads::get_instance()->options();
538
539 $prefix = Advanced_Ads_Plugin::get_instance()->get_frontend_prefix();
540 $old_prefix = ( isset( $options['id-prefix'] ) ) ? esc_attr( $options['id-prefix'] ) : '';
541
542 require ADVADS_BASE_PATH . '/admin/views/settings/general/frontend-prefix.php';
543 }
544
545 /**
546 * Render setting to allow editors to manage ads
547 */
548 public function render_settings_editors_manage_ads() {
549 $options = Advanced_Ads::get_instance()->options();
550
551 // is false by default if no options where previously set.
552 if ( isset( $options['editors-manage-ads'] ) && $options['editors-manage-ads'] ) {
553 $allow = true;
554 } else {
555 $allow = false;
556 }
557
558 require ADVADS_BASE_PATH . '/admin/views/settings/general/editors-manage-ads.php';
559 }
560
561 /**
562 * Prepare the template for multisite allow unfiltered_html settings.
563 *
564 * @return void
565 */
566 public function renders_settings_allow_unfiltered_html() {
567 $options = Advanced_Ads::get_instance()->options();
568 $user_roles_to_display = array_filter( wp_roles()->role_objects, static function( WP_Role $role ) {
569 return $role->has_cap( 'advanced_ads_edit_ads' );
570 } );
571 if ( empty( $user_roles_to_display ) ) {
572 return;
573 }
574 if ( ! isset( $options['allow-unfiltered-html'] ) ) {
575 $options['allow-unfiltered-html'] = [];
576 }
577 $allowed_roles = $options['allow-unfiltered-html'];
578
579 require ADVADS_BASE_PATH . '/admin/views/settings/general/allow-unfiltered-html.php';
580 }
581
582 /**
583 * Render setting to add an "Advertisement" label before ads
584 */
585 public function render_settings_add_custom_label() {
586 $options = Advanced_Ads::get_instance()->options();
587
588 $enabled = isset( $options['custom-label']['enabled'] );
589 $label = ! empty( $options['custom-label']['text'] ) ? esc_html( $options['custom-label']['text'] ) : _x( 'Advertisements', 'label before ads', 'advanced-ads' );
590
591 require ADVADS_BASE_PATH . '/admin/views/settings/general/custom-label.php';
592 }
593
594 /**
595 * Render link target="_blank" setting
596 *
597 * @since 1.8.4 – moved here from Tracking add-on
598 */
599 public function render_settings_link_target_callback() {
600
601 // get option if saved for tracking.
602 $options = Advanced_Ads::get_instance()->options();
603 if ( ! isset( $options['target-blank'] ) && class_exists( 'Advanced_Ads_Tracking_Plugin' ) ) {
604 $tracking_options = Advanced_Ads_Tracking_Plugin::get_instance()->options();
605 if ( isset( $tracking_options['target'] ) ) {
606 $options['target-blank'] = $tracking_options['target'];
607 }
608 }
609
610 $target = isset( $options['target-blank'] ) ? $options['target-blank'] : 0;
611 include ADVADS_BASE_PATH . 'admin/views/settings/general/link-target.php';
612 }
613
614 /**
615 * Render setting 'Delete data on uninstall"
616 */
617 public function render_settings_uninstall_delete_data() {
618 $options = Advanced_Ads::get_instance()->options();
619 $enabled = ! empty( $options['uninstall-delete-data'] );
620
621 include ADVADS_BASE_PATH . 'admin/views/settings/general/uninstall-delete-data.php';
622 }
623
624 /**
625 * Sanitize plugin settings
626 *
627 * @param array $options all the options.
628 *
629 * @return array sanitized options.
630 */
631 public function sanitize_settings( $options ) {
632
633 // sanitize whatever option one wants to sanitize.
634 if ( isset( $options['front-prefix'] ) ) {
635 $options['front-prefix'] = Advanced_Ads_Plugin::get_instance()->sanitize_frontend_prefix(
636 $options['front-prefix'],
637 Advanced_Ads_Plugin::DEFAULT_FRONTEND_PREFIX
638 );
639 }
640
641 $options = apply_filters( 'advanced-ads-sanitize-settings', $options );
642
643 // check if editors can edit ads now and set the rights
644 // else, remove that right.
645 $editor_role = get_role( 'editor' );
646 if ( null === $editor_role ) {
647 return $options;
648 }
649 if ( isset( $options['editors-manage-ads'] ) && $options['editors-manage-ads'] ) {
650 $editor_role->add_cap( 'advanced_ads_see_interface' );
651 $editor_role->add_cap( 'advanced_ads_edit_ads' );
652 $editor_role->add_cap( 'advanced_ads_manage_placements' );
653 $editor_role->add_cap( 'advanced_ads_place_ads' );
654 } else {
655 $editor_role->remove_cap( 'advanced_ads_see_interface' );
656 $editor_role->remove_cap( 'advanced_ads_edit_ads' );
657 $editor_role->remove_cap( 'advanced_ads_manage_placements' );
658 $editor_role->remove_cap( 'advanced_ads_place_ads' );
659 }
660
661 // we need 3 states: ! empty, 1, 0.
662 $options['disabled-ads']['feed'] = ! empty( $options['disabled-ads']['feed'] ) ? 1 : 0;
663
664 if ( isset( $options['content-injection-everywhere'] ) ) {
665 if ( '0' === $options['content-injection-everywhere'] ) {
666 unset( $options['content-injection-everywhere'] );
667 } elseif ( $options['content-injection-everywhere'] === 'true' || $options['content-injection-everywhere'] <= - 1 ) {
668 // Note: the option may be already set 'true' during import.
669 $options['content-injection-everywhere'] = 'true';
670 } else {
671 $options['content-injection-everywhere'] = absint( $options['content-injection-everywhere'] );
672 }
673 }
674
675 return $options;
676 }
677
678 }
679