admin.php
168 lines
| 1 | <?php |
| 2 | |
| 3 | class Advanced_Ads_Privacy_Admin |
| 4 | { |
| 5 | /** |
| 6 | * Singleton instance of the plugin |
| 7 | * |
| 8 | * @var Advanced_Ads_Privacy_Admin |
| 9 | */ |
| 10 | protected static $instance; |
| 11 | |
| 12 | /** |
| 13 | * module options |
| 14 | * |
| 15 | * @var array (if loaded) |
| 16 | */ |
| 17 | protected $options; |
| 18 | |
| 19 | /** |
| 20 | * Initialize the module |
| 21 | * |
| 22 | */ |
| 23 | private function __construct() { |
| 24 | |
| 25 | // add module settings to Advanced Ads settings page |
| 26 | add_action( 'advanced-ads-settings-init', array( $this, 'settings_init' ), 20, 1 ); |
| 27 | add_filter('advanced-ads-setting-tabs', array($this, 'setting_tabs'), 20 ); |
| 28 | |
| 29 | // additional ad options |
| 30 | add_action('advanced-ads-ad-params-after', array($this, 'render_ad_options'), 20, 2); |
| 31 | add_filter( 'advanced-ads-save-options', array( $this, 'save_ad_options' ), 10, 2 ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Return an instance of Advanced_Ads_Privacy_Admin |
| 36 | * |
| 37 | * @return Advanced_Ads_Privacy_Admin |
| 38 | */ |
| 39 | public static function get_instance() { |
| 40 | // If the single instance hasn't been set, set it now. |
| 41 | if (null === self::$instance) |
| 42 | { |
| 43 | self::$instance = new self; |
| 44 | } |
| 45 | |
| 46 | return self::$instance; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * add tracking settings tab |
| 51 | * |
| 52 | * @since 1.8.30 |
| 53 | * @param arr $tabs existing setting tabs |
| 54 | * @return arr $tabs setting tabs with AdSense tab attached |
| 55 | */ |
| 56 | public function setting_tabs(array $tabs) { |
| 57 | |
| 58 | $tabs['privacy'] = array( |
| 59 | // TODO abstract string |
| 60 | 'page' => ADVADS_PRIVACY_SLUG . '-settings', |
| 61 | 'group' => ADVADS_PRIVACY_SLUG, |
| 62 | 'tabid' => 'privacy', |
| 63 | 'title' => __( 'Privacy', 'advanced-ads' ) |
| 64 | ); |
| 65 | |
| 66 | return $tabs; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * add settings to settings page |
| 71 | * |
| 72 | * @param string $hook settings page hook |
| 73 | */ |
| 74 | public function settings_init($hook) { |
| 75 | |
| 76 | register_setting( ADVADS_PRIVACY_SLUG, Advanced_Ads_Privacy::OPTION_KEY ); |
| 77 | |
| 78 | // add new section |
| 79 | add_settings_section( |
| 80 | ADVADS_PRIVACY_SLUG . '_settings_section', '', array($this, 'render_settings_section'), ADVADS_PRIVACY_SLUG . '-settings' |
| 81 | ); |
| 82 | |
| 83 | add_settings_field( |
| 84 | 'enable-privacy-module', __('Enable Privacy module', 'advanced-ads'), array($this, 'render_settings_enable_module'), ADVADS_PRIVACY_SLUG . '-settings', ADVADS_PRIVACY_SLUG . '_settings_section' |
| 85 | ); |
| 86 | add_settings_field( |
| 87 | 'consent-method', __('Consent method', 'advanced-ads'), array($this, 'render_settings_consent_method'), ADVADS_PRIVACY_SLUG . '-settings', ADVADS_PRIVACY_SLUG . '_settings_section' |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | /** |
| 93 | * render settings section |
| 94 | */ |
| 95 | public function render_settings_section() { |
| 96 | |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Render enable module setting |
| 101 | */ |
| 102 | public function render_settings_enable_module() { |
| 103 | $options = Advanced_Ads_Privacy::get_instance()->options(); |
| 104 | $module_enabled = isset( $options['enabled']) ? $options['enabled'] : false; |
| 105 | require ADVADS_BASE_PATH . 'modules/privacy/admin/views/setting-enable.php'; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Render setting to choose the cookie method to hide ads |
| 110 | */ |
| 111 | public function render_settings_consent_method() { |
| 112 | $options = Advanced_Ads_Privacy::get_instance()->options(); |
| 113 | $methods = Advanced_Ads_Privacy::get_instance()->get_consent_methods(); |
| 114 | $current_method = isset( $options['consent-method']) ? $options['consent-method'] : '0'; |
| 115 | $custom_cookie_name = isset( $options['custom-cookie-name'] ) ? $options['custom-cookie-name'] : ''; |
| 116 | $custom_cookie_value = isset( $options['custom-cookie-value'] ) ? $options['custom-cookie-value'] : ''; |
| 117 | $show_non_personalized_adsense = isset( $options['show-non-personalized-adsense']) ? 1 : false; |
| 118 | require ADVADS_BASE_PATH . 'modules/privacy/admin/views/setting-consent-method.php'; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * add options to ad edit page |
| 123 | * |
| 124 | * @param obj $ad ad object |
| 125 | * @param arr $types ad types |
| 126 | */ |
| 127 | public function render_ad_options( $ad, $types ) { |
| 128 | |
| 129 | if (!isset($ad->id) || empty($ad->id)) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | $ad = new Advanced_Ads_Ad($ad->id); |
| 134 | $ad_options = $ad->options(); |
| 135 | $ad_privacy_options = isset( $ad_options['privacy'] ) ? $ad_options['privacy'] : array(); |
| 136 | $ignore_consent = isset( $ad_privacy_options['ignore-consent']) ? true : false; |
| 137 | |
| 138 | $privacy_options = Advanced_Ads_Privacy::get_instance()->options(); |
| 139 | $module_enabled = ! empty( $privacy_options['enabled'] ); |
| 140 | |
| 141 | // If the module is not enabled and the option wasn't checked before. |
| 142 | if ( ! $module_enabled && ! $ignore_consent ) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | include ADVADS_BASE_PATH . 'modules/privacy/admin/views/setting-ad-ignore-consent.php'; |
| 147 | |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Save ad options. |
| 152 | * |
| 153 | * @param arr $options |
| 154 | * @param obj $ad Advanced_Ads_Ad |
| 155 | * @return arr $options |
| 156 | */ |
| 157 | public function save_ad_options( $options = array(), Advanced_Ads_Ad $ad ) { |
| 158 | if ( isset( $_POST['advanced_ad']['privacy'] ) ) { |
| 159 | $options['privacy'] = $_POST['advanced_ad']['privacy']; |
| 160 | } else { |
| 161 | unset( $options['privacy'] ); |
| 162 | } |
| 163 | |
| 164 | return $options; |
| 165 | } |
| 166 | |
| 167 | } |
| 168 |