admin.php
394 lines
| 1 | <?php |
| 2 | |
| 3 | class Advanced_Ads_AdSense_Admin { |
| 4 | |
| 5 | private $data; |
| 6 | private $nonce; |
| 7 | private static $instance = null; |
| 8 | protected $notice = null; |
| 9 | private $settings_page_hook = 'advanced-ads-adsense-settings-page'; |
| 10 | |
| 11 | private function __construct() { |
| 12 | $this->data = Advanced_Ads_AdSense_Data::get_instance(); |
| 13 | add_action( 'advanced-ads-settings-init', array($this, 'settings_init') ); |
| 14 | // add_action( 'advanced-ads-additional-settings-form', array($this, 'settings_init') ); |
| 15 | add_filter('advanced-ads-setting-tabs', array($this, 'setting_tabs')); |
| 16 | |
| 17 | add_action( 'admin_enqueue_scripts', array($this, 'enqueue_scripts') ); |
| 18 | add_action( 'admin_print_scripts', array($this, 'print_scripts') ); |
| 19 | add_filter( 'advanced-ads-list-ad-size', array($this, 'ad_details_column'), 10, 2 ); |
| 20 | add_filter( 'advanced-ads-ad-settings-pre-save', array($this, 'sanitize_ad_settings') ); |
| 21 | add_filter( 'advanced-ads-ad-notices', array($this, 'ad_notices'), 10, 3 ); |
| 22 | } |
| 23 | |
| 24 | public function ad_details_column($size, $the_ad) { |
| 25 | if ( 'adsense' == $the_ad->type ) { |
| 26 | $content = json_decode( $the_ad->content ); |
| 27 | if ( $content && 'responsive' == $content->unitType ) { $size = __( 'Responsive', 'advanced-ads' ); } |
| 28 | } |
| 29 | return $size; |
| 30 | } |
| 31 | |
| 32 | public function print_scripts() { |
| 33 | global $pagenow, $post_type; |
| 34 | if ( |
| 35 | ('post-new.php' == $pagenow && Advanced_Ads::POST_TYPE_SLUG == $post_type) || |
| 36 | ('post.php' == $pagenow && Advanced_Ads::POST_TYPE_SLUG == $post_type && isset($_GET['action']) && 'edit' == $_GET['action']) |
| 37 | ) { |
| 38 | $db = Advanced_Ads_AdSense_Data::get_instance(); |
| 39 | $pub_id = $db->get_adsense_id(); |
| 40 | ?> |
| 41 | <script type="text/javascript"> |
| 42 | var gadsenseData = { |
| 43 | pubId : '<?php echo $pub_id; ?>', |
| 44 | pageLevelEnabled: '<?php echo $db->is_page_level_enabled(); ?>', |
| 45 | msg : { |
| 46 | unknownAd : '<?php esc_attr_e( "The ad details couldn't be retrieved from the ad code", 'advanced-ads' ); ?>', |
| 47 | pubIdMismatch : '<?php _e( 'Warning : The AdSense account from this code does not match the one set with the Advanced Ads Plugin. This ad might cause troubles when used in the front end.', 'advanced-ads' ); ?>', |
| 48 | pageLevelEnabled: '<?php _e( sprintf(__( 'Page-Level ads are already activated in the <a href="%s">AdSense settings</a>. No need to add them manually.', 'advanced-ads' ), admin_url( 'admin.php?page=advanced-ads-settings#top#adsense' ) ) ); ?>', |
| 49 | pageLevelDisabled: '<?php |
| 50 | printf( '%s <button id="adsense_enable_pla" type="button" class="button">%s</button>', |
| 51 | esc_attr__( 'This type of ad code is set up in the AdSense settings. Click on the following button to enable it now.', 'advanced-ads' ), |
| 52 | esc_attr__( 'Activate', 'advanced-ads' ) ); ?>' |
| 53 | } |
| 54 | }; |
| 55 | </script> |
| 56 | <?php |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public function enqueue_scripts() { |
| 61 | global $gadsense_globals, $pagenow, $post_type; |
| 62 | $screen = get_current_screen(); |
| 63 | $plugin = Advanced_Ads_Admin::get_instance(); |
| 64 | if ( |
| 65 | ('post-new.php' == $pagenow && Advanced_Ads::POST_TYPE_SLUG == $post_type) || |
| 66 | ('post.php' == $pagenow && Advanced_Ads::POST_TYPE_SLUG == $post_type && isset($_GET['action']) && 'edit' == $_GET['action']) |
| 67 | ) { |
| 68 | $default_script = array( |
| 69 | 'path' => GADSENSE_BASE_URL . 'admin/assets/js/new-ad.js', |
| 70 | 'dep' => array('jquery'), |
| 71 | 'version' => null, |
| 72 | ); |
| 73 | |
| 74 | $scripts = array( |
| 75 | 'gadsense-new-ad' => $default_script, |
| 76 | ); |
| 77 | |
| 78 | // Allow modifications of script files to enqueue |
| 79 | $scripts = apply_filters( 'advanced-ads-gadsense-ad-param-script', $scripts ); |
| 80 | |
| 81 | foreach ( $scripts as $handle => $value ) { |
| 82 | if ( empty($handle) ) { |
| 83 | continue; |
| 84 | } |
| 85 | if ( ! empty($handle) && empty($value) ) { |
| 86 | // Allow inclusion of WordPress's built-in script like jQuery |
| 87 | wp_enqueue_script( $handle ); |
| 88 | } else { |
| 89 | if ( ! isset($value['version']) ) { $value['version'] = null; } |
| 90 | wp_enqueue_script( $handle, $value['path'], $value['dep'], $value['version'] ); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | $styles = array(); |
| 95 | |
| 96 | // Allow modifications of default style files to enqueue |
| 97 | $styles = apply_filters( 'advanced-ads-gadsense-ad-param-style', $styles ); |
| 98 | |
| 99 | foreach ( $styles as $handle => $value ) { |
| 100 | if ( ! isset($value['path']) || |
| 101 | ! isset($value['dep']) || |
| 102 | empty($handle) |
| 103 | ) { |
| 104 | continue; |
| 105 | } |
| 106 | if ( ! isset($value['version']) ) { |
| 107 | $value['version'] = null; } |
| 108 | wp_enqueue_style( $handle, $value['path'], $value['dep'], $value['version'] ); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | public static function get_instance() { |
| 114 | if ( null == self::$instance ) { |
| 115 | self::$instance = new self; |
| 116 | } |
| 117 | return self::$instance; |
| 118 | } |
| 119 | |
| 120 | public function settings_init() { |
| 121 | |
| 122 | // get settings page hook |
| 123 | $hook = $this->settings_page_hook; |
| 124 | |
| 125 | register_setting( ADVADS_SLUG . '-adsense', ADVADS_SLUG . '-adsense', array($this, 'sanitize_settings') ); |
| 126 | |
| 127 | // add new section |
| 128 | add_settings_section( |
| 129 | 'advanced_ads_adsense_setting_section', |
| 130 | '', //__( 'AdSense', 'advanced-ads' ), |
| 131 | array($this, 'render_settings_section_callback'), |
| 132 | $hook |
| 133 | ); |
| 134 | |
| 135 | // add setting field to disable ads |
| 136 | add_settings_field( |
| 137 | 'adsense-id', |
| 138 | __( 'AdSense ID', 'advanced-ads' ), |
| 139 | array($this, 'render_settings_adsense_id'), |
| 140 | $hook, |
| 141 | 'advanced_ads_adsense_setting_section' |
| 142 | ); |
| 143 | |
| 144 | // add setting field for adsense limit |
| 145 | add_settings_field( |
| 146 | 'adsense-limit', |
| 147 | __( 'Limit to 3 ads', 'advanced-ads' ), |
| 148 | array($this, 'render_settings_adsense_limit'), |
| 149 | $hook, |
| 150 | 'advanced_ads_adsense_setting_section' |
| 151 | ); |
| 152 | |
| 153 | // activate page-level ads |
| 154 | add_settings_field( |
| 155 | 'adsense-page-level', |
| 156 | __( 'Activate Page-Level ads', 'advanced-ads' ), |
| 157 | array($this, 'render_settings_adsense_page_level'), |
| 158 | $hook, |
| 159 | 'advanced_ads_adsense_setting_section' |
| 160 | ); |
| 161 | |
| 162 | // disable AdSense violation warnings |
| 163 | add_settings_field( |
| 164 | 'adsense-warnings-disable', |
| 165 | __( 'Disable violation warnings', 'advanced-ads' ), |
| 166 | array($this, 'render_settings_adsense_warnings_disable'), |
| 167 | $hook, |
| 168 | 'advanced_ads_adsense_setting_section' |
| 169 | ); |
| 170 | |
| 171 | add_settings_field( |
| 172 | 'adsense-background', |
| 173 | __( 'Transparent background', 'advanced-ads' ), |
| 174 | array( $this, 'render_settings_adsense_background' ), |
| 175 | $hook, |
| 176 | 'advanced_ads_adsense_setting_section' |
| 177 | ); |
| 178 | |
| 179 | // hook for additional settings from add-ons |
| 180 | do_action( 'advanced-ads-adsense-settings-init', $hook ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * render adsense settings section |
| 185 | * |
| 186 | * @since 1.5.1 |
| 187 | */ |
| 188 | public function render_settings_section_callback(){ |
| 189 | // for whatever purpose there might come |
| 190 | $adsense_id = $this->data->get_adsense_id(); |
| 191 | if( ! $adsense_id ){ |
| 192 | ?><p class="advads-error-message"><?php |
| 193 | printf(__( 'Please enter your Publisher ID in order to use AdSense on your page. See the <a href="%s" target="_blank">manual</a> for more information.', 'advanced-ads' ), ADVADS_URL . 'manual/ad-types/adsense-ads/#utm_source=advanced-ads&utm_medium=link&utm_campaign=edit-adsense' ); |
| 194 | ?></p><?php |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * render adsense id setting |
| 200 | * |
| 201 | * @since 1.5.1 |
| 202 | */ |
| 203 | public function render_settings_adsense_id(){ |
| 204 | $adsense_id = $this->data->get_adsense_id(); |
| 205 | |
| 206 | ?><input type="text" name="<?php echo GADSENSE_OPT_NAME; ?>[adsense-id]" id="adsense-id" size="32" value="<?php echo $adsense_id; ?>" /> |
| 207 | <p class="description"><?php _e( 'Your AdSense Publisher ID <em>(pub-xxxxxxxxxxxxxx)</em>', 'advanced-ads' ) ?></p><?php |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * render adsense limit setting |
| 212 | * |
| 213 | * @since 1.5.1 |
| 214 | */ |
| 215 | public function render_settings_adsense_limit(){ |
| 216 | $limit_per_page = $this->data->get_limit_per_page(); |
| 217 | |
| 218 | ?><label><input type="checkbox" name="<?php echo GADSENSE_OPT_NAME; ?>[limit-per-page]" value="1" <?php checked( $limit_per_page ); ?> /> |
| 219 | <?php printf( __( 'Limit to %d AdSense ads', 'advanced-ads' ), 3 ); ?></label> |
| 220 | <p class="description"> |
| 221 | <?php |
| 222 | printf( |
| 223 | __( 'There is no explicit limit for AdSense ads anymore, but you can still use this setting to prevent too many AdSense ads to show accidentally on your site.', 'advanced-ads' ), |
| 224 | esc_url( 'https://www.google.com/adsense/terms' ) |
| 225 | ); ?></p> |
| 226 | <?php if( defined( 'AAP_VERSION' ) ) : /* give warning when cache-busting in Pro is active */ ?> |
| 227 | <p class="advads-error-message"><?php _e( 'Due to technical restrictions, the limit does not work on placements with cache-busting enabled.', 'advanced-ads' ); ?></p> |
| 228 | <?php endif; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * render page-level ads setting |
| 233 | * |
| 234 | * @since 1.6.9 |
| 235 | */ |
| 236 | public function render_settings_adsense_page_level(){ |
| 237 | $options = $this->data->get_options(); |
| 238 | $page_level = $options['page-level-enabled']; |
| 239 | |
| 240 | ?><label><input type="checkbox" name="<?php echo GADSENSE_OPT_NAME; ?>[page-level-enabled]" value="1" <?php checked( $page_level ); ?> /> |
| 241 | <?php _e( 'Insert Page-Level ads code on all pages.', 'advanced-ads' ); ?></label> |
| 242 | <p class="description"><?php _e( 'You still need to enable Page-Level ads in your AdSense account. See <a href="https://support.google.com/adsense/answer/6245304" target="_blank">AdSense Help</a> (requires AdSense-login) for more information.', 'advanced-ads' ); ?></p> |
| 243 | <p class="description"><?php printf(__( 'Please notice that this code might also activate QuickStart ads. Please read <a href="%s" target="_blank">this article</a> if <strong>ads appear in random places</strong>.', 'advanced-ads' ), ADVADS_URL . 'adsense-in-random-positions-quickstart/#utm_source=advanced-ads&utm_medium=link&utm_campaign=backend-quickstart-ads' ); ?></p><?php |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * render AdSense violation warnings setting |
| 248 | * |
| 249 | * @since 1.6.9 |
| 250 | */ |
| 251 | public function render_settings_adsense_warnings_disable(){ |
| 252 | $options = $this->data->get_options(); |
| 253 | $disable_violation_warnings = isset( $options['violation-warnings-disable'] ) ? 1 : 0; |
| 254 | |
| 255 | ?><label><input type="checkbox" name="<?php echo GADSENSE_OPT_NAME; ?>[violation-warnings-disable]" value="1" <?php checked( 1, $disable_violation_warnings ); ?> /> |
| 256 | <?php _e( 'Disable warnings about potential violations of the AdSense terms.', 'advanced-ads' ); ?></label> |
| 257 | <p class="description"><?php printf(__( 'Our <a href="%s" target="_blank">Ad Health</a> feature monitors if AdSense is implemented correctly on your site. It also considers ads not managed with Advanced Ads. Enable this option to remove these checks', 'advanced-ads' ), ADVADS_URL . 'adsense-in-random-positions-quickstart/#utm_source=advanced-ads&utm_medium=link&utm_campaign=backend-quickstart-ads' ); ?></p><?php |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Render transparent background setting. |
| 262 | */ |
| 263 | public function render_settings_adsense_background() { |
| 264 | $options = $this->data->get_options(); |
| 265 | $background = $options['background']; |
| 266 | |
| 267 | ?><label><input type="checkbox" name="<?php echo GADSENSE_OPT_NAME; ?>[background]" value="1" <?php checked( $background ); ?> /> |
| 268 | <?php _e( 'Enable this option in case your theme adds an unfortunate background color to AdSense ads.', 'advanced-ads' ); ?></label><?php |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * sanitize adsense settings |
| 273 | * |
| 274 | * @since 1.5.1 |
| 275 | * @param array $options all the options |
| 276 | */ |
| 277 | public function sanitize_settings($options){ |
| 278 | |
| 279 | // sanitize whatever option one wants to sanitize |
| 280 | if(isset($options['adsense-id']) && $options['adsense-id'] != ''){ |
| 281 | if(0 !== strpos( $options['adsense-id'], 'pub-' )){ |
| 282 | // add settings error |
| 283 | add_settings_error( |
| 284 | 'adsense-limit', |
| 285 | 'settings_updated', |
| 286 | __( 'The Publisher ID has an incorrect format. (must start with "pub-")', 'advanced-ads' )); |
| 287 | } |
| 288 | // trim publisher id |
| 289 | $options['adsense-id'] = trim($options['adsense-id']); |
| 290 | } |
| 291 | |
| 292 | return $options; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * add adsense setting tab |
| 297 | * |
| 298 | * @since 1.5.1 |
| 299 | * @param arr $tabs existing setting tabs |
| 300 | * @return arr $tabs setting tabs with AdSense tab attached |
| 301 | */ |
| 302 | public function setting_tabs(array $tabs){ |
| 303 | |
| 304 | $tabs['adsense'] = array( |
| 305 | 'page' => $this->settings_page_hook, |
| 306 | 'group' => ADVADS_SLUG . '-adsense', |
| 307 | 'tabid' => 'adsense', |
| 308 | 'title' => __( 'AdSense', 'advanced-ads' ) |
| 309 | ); |
| 310 | |
| 311 | return $tabs; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * sanitize ad settings |
| 316 | * save publisher id from new ad unit if not given in main options |
| 317 | * |
| 318 | * @since 1.6.2 |
| 319 | * @param arr $ad_settings_post |
| 320 | * @return arr $ad_settings_post |
| 321 | */ |
| 322 | public function sanitize_ad_settings( array $ad_settings_post ){ |
| 323 | |
| 324 | // check ad type |
| 325 | if( ! isset( $ad_settings_post['type'] ) || 'adsense' !== $ad_settings_post['type'] ){ |
| 326 | return $ad_settings_post; |
| 327 | } |
| 328 | |
| 329 | // save AdSense publisher ID if given and remove it from options |
| 330 | if ( ! empty($ad_settings_post['output']['adsense-pub-id']) ) { |
| 331 | // get options |
| 332 | $adsense_options = get_option( 'advanced-ads-adsense', array() ); |
| 333 | $adsense_options['adsense-id'] = $ad_settings_post['output']['adsense-pub-id']; |
| 334 | |
| 335 | // save adsense options including publisher id |
| 336 | update_option( 'advanced-ads-adsense', $adsense_options ); |
| 337 | |
| 338 | } |
| 339 | unset( $ad_settings_post['output']['adsense-pub-id'] ); |
| 340 | |
| 341 | return $ad_settings_post; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * show AdSense ad specific notices in parameters box |
| 346 | * |
| 347 | * @since 1.7.22 |
| 348 | */ |
| 349 | public function ad_notices( $notices, $box, $post ){ |
| 350 | |
| 351 | $ad = new Advanced_Ads_Ad( $post->ID ); |
| 352 | |
| 353 | // $content = json_decode( stripslashes( $ad->content ) ); |
| 354 | |
| 355 | switch ($box['id']){ |
| 356 | case 'ad-parameters-box' : |
| 357 | // add warning if this is a responsive ad unit without custom sizes and position is set to left or right |
| 358 | // hidden by default and made visible with JS |
| 359 | $notices[] = array( |
| 360 | 'text' => sprintf(__( 'Responsive AdSense ads don’t work reliably with <em>Position</em> set to left or right. Either switch the <em>Type</em> to "normal" or follow <a href="%s" target="_blank">this tutorial</a> if you want the ad to be wrapped in text.', 'advanced-ads' ), ADVADS_URL . 'adsense-responsive-custom-sizes/#utm_source=advanced-ads&utm_medium=link&utm_campaign=adsense-custom-sizes-tutorial' ), |
| 361 | 'class' => 'advads-ad-notice-responsive-position error hidden', |
| 362 | ); |
| 363 | // show hint about AdSense In-feed add-on |
| 364 | if( ! class_exists( 'Advanced_Ads_In_Feed', false ) ){ |
| 365 | $notices[] = array( |
| 366 | 'text' => sprintf(__( '<a href="%s" target="_blank">Install the free AdSense In-feed add-on</a> in order to place ads between posts.', 'advanced-ads' ), wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=' . 'advanced-ads-adsense-in-feed'), 'install-plugin_' . 'advanced-ads-adsense-in-feed') ), |
| 367 | 'class' => 'advads-ad-notice-in-feed-add-on hidden', |
| 368 | ); |
| 369 | } |
| 370 | // show message about Responsive add-on |
| 371 | if ( ! defined( 'AAR_SLUG' ) ) { |
| 372 | $notices[] = array( |
| 373 | 'text' => sprintf( __( 'Use the <a href="%s" target="_blank">Responsive add-on</a> in order to define the exact size for each browser width or choose between horizontal, vertical, or rectangle formats.', 'advanced-ads' ), ADVADS_URL . 'add-ons/responsive-ads/#utm_source=advanced-ads&utm_medium=link&utm_campaign=edit-adsense' ), |
| 374 | 'class' => 'advads-ad-notice-responsive-add-on', |
| 375 | ); |
| 376 | } |
| 377 | |
| 378 | // show hint about Content ad, Link unit or Matched content being defined in AdSense account |
| 379 | // disabled since it might no longer be needed with the new ad types |
| 380 | /* if( 'adsense' === $ad->type ){ |
| 381 | $notices[] = array( |
| 382 | 'text' => sprintf( __( 'The type of your AdSense ad unit (content unit, link unit or matched content) needs to be defined in <a href="%s" target="_blank">your AdSense account</a>.', 'advanced-ads' ), 'https://www.google.com/adsense' ), |
| 383 | 'class' => 'advads-ad-notice-adsense-ad-unit-type', |
| 384 | ); |
| 385 | }*/ |
| 386 | break; |
| 387 | } |
| 388 | |
| 389 | |
| 390 | return $notices; |
| 391 | } |
| 392 | |
| 393 | } |
| 394 |