settings-config.php
332 lines
| 1 | <?php |
| 2 | |
| 3 | namespace cookiebot_addons\config; |
| 4 | |
| 5 | use cookiebot_addons\lib\Settings_Service_Interface; |
| 6 | |
| 7 | class Settings_Config { |
| 8 | |
| 9 | /** |
| 10 | * @var Settings_Service_Interface |
| 11 | */ |
| 12 | protected $settings_service; |
| 13 | |
| 14 | /** |
| 15 | * Settings_Config constructor. |
| 16 | * |
| 17 | * @param Settings_Service_Interface $settings_service |
| 18 | * |
| 19 | * @since 1.3.0 |
| 20 | */ |
| 21 | public function __construct( Settings_Service_Interface $settings_service ) { |
| 22 | $this->settings_service = $settings_service; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Load data for settings page |
| 27 | * |
| 28 | * @since 1.3.0 |
| 29 | */ |
| 30 | public function load() { |
| 31 | add_action( 'admin_menu', array( $this, 'add_submenu' ) ); |
| 32 | add_action( 'admin_init', array( $this, 'register_settings' ) ); |
| 33 | add_action( 'admin_enqueue_scripts', array( $this, 'add_wp_admin_style_script' ) ); |
| 34 | add_action( 'update_option_cookiebot_available_addons', array( |
| 35 | $this, |
| 36 | 'post_hook_available_addons_update_option' |
| 37 | ), 10, 3 ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Registers submenu in options menu. |
| 42 | * |
| 43 | * @since 1.3.0 |
| 44 | */ |
| 45 | public function add_submenu() { |
| 46 | /*add_submenu_page('cookiebot', 'Prior Consent', __( 'Prior Consent', 'cookiebot' ), 'manage_options', 'cookiebot_addons', array( |
| 47 | $this, |
| 48 | 'setting_page' |
| 49 | ) );*/ |
| 50 | |
| 51 | add_submenu_page( 'cookiebot', __( 'Prior Consent', 'cookiebot' ), __( 'Prior Consent', 'cookiebot' ), 'manage_options', 'cookiebot-addons', array( |
| 52 | $this, |
| 53 | 'setting_page' |
| 54 | ) ); |
| 55 | |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Load css styling to the settings page |
| 60 | * |
| 61 | * @since 1.3.0 |
| 62 | */ |
| 63 | public function add_wp_admin_style_script( $hook ) { |
| 64 | if ( $hook != 'cookiebot_page_cookiebot-addons' ) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | wp_enqueue_script( 'cookiebot_tiptip_js', plugins_url( 'js/jquery.tipTip.js', dirname( __FILE__ ) ), array( 'jquery' ), '1.8', true ); |
| 69 | wp_enqueue_script( 'cookiebot_addons_custom_js', plugins_url( 'js/settings.js', dirname( __FILE__ ) ), array( 'jquery' ), '1.8', true ); |
| 70 | wp_localize_script( 'cookiebot_addons_custom_js', 'php', array( 'remove_link' => ' <a href="" class="submitdelete deletion">' . __( 'Remove language', 'cookiebot-addons' ) . '</a>' ) ); |
| 71 | wp_enqueue_style( 'cookiebot_addons_custom_css', plugins_url( 'style/css/admin_styles.css', dirname( __FILE__ ) ) ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Registers addons for settings page. |
| 76 | * |
| 77 | * @since 1.3.0 |
| 78 | */ |
| 79 | public function register_settings() { |
| 80 | global $pagenow; |
| 81 | |
| 82 | if ( ( isset( $_GET['page'] ) && $_GET['page'] == 'cookiebot-addons' ) || $pagenow == 'options.php' ) { |
| 83 | if ( isset( $_GET['tab'] ) && 'unavailable_addons' === $_GET['tab'] ) { |
| 84 | $this->register_unavailable_addons(); |
| 85 | } elseif ( ( isset( $_GET['tab'] ) && 'jetpack' === $_GET['tab'] ) ) { |
| 86 | $this->register_jetpack_addon(); |
| 87 | } else { |
| 88 | $this->register_available_addons(); |
| 89 | } |
| 90 | |
| 91 | if ( $pagenow == 'options.php' ) { |
| 92 | $this->register_jetpack_addon(); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Register available addons |
| 99 | * |
| 100 | * @since 1.3.0 |
| 101 | */ |
| 102 | private function register_available_addons() { |
| 103 | add_settings_section( "available_addons", "Available plugins", array( |
| 104 | $this, |
| 105 | "header_available_addons" |
| 106 | ), "cookiebot-addons" ); |
| 107 | |
| 108 | foreach ( $this->settings_service->get_addons() as $addon ) { |
| 109 | if ( $addon->is_addon_installed() && $addon->is_addon_activated() ) { |
| 110 | add_settings_field( |
| 111 | $addon->get_option_name(), |
| 112 | $addon->get_addon_name() . $this->get_extra_information( $addon ), |
| 113 | array( |
| 114 | $this, |
| 115 | "available_addon_callback" |
| 116 | ), |
| 117 | "cookiebot-addons", |
| 118 | "available_addons", |
| 119 | array( |
| 120 | 'addon' => $addon |
| 121 | ) |
| 122 | ); |
| 123 | |
| 124 | register_setting( 'cookiebot_available_addons', "cookiebot_available_addons", array( |
| 125 | $this, |
| 126 | 'sanitize_cookiebot' |
| 127 | ) ); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Register jetpack addon - new tab for jetpack specific settings |
| 134 | * |
| 135 | * @since 1.3.0 |
| 136 | */ |
| 137 | private function register_jetpack_addon() { |
| 138 | add_settings_section( "jetpack_addon", "Jetpack", array( |
| 139 | $this, |
| 140 | "header_jetpack_addon" |
| 141 | ), "cookiebot-addons" ); |
| 142 | |
| 143 | foreach ( $this->settings_service->get_addons() as $addon ) { |
| 144 | if ( 'Jetpack' === ( new \ReflectionClass( $addon ) )->getShortName() ) { |
| 145 | if ( $addon->is_addon_installed() && $addon->is_addon_activated() ) { |
| 146 | |
| 147 | foreach ( $addon->get_widgets() as $widget ) { |
| 148 | add_settings_field( |
| 149 | $widget->get_widget_option_name(), |
| 150 | $widget->get_label() . $this->get_extra_information( $widget ), |
| 151 | array( |
| 152 | $this, |
| 153 | "jetpack_addon_callback" |
| 154 | ), |
| 155 | "cookiebot-addons", |
| 156 | "jetpack_addon", |
| 157 | array( |
| 158 | 'widget' => $widget, |
| 159 | 'addon' => $addon |
| 160 | ) |
| 161 | ); |
| 162 | |
| 163 | register_setting( 'cookiebot_jetpack_addon', 'cookiebot_jetpack_addon' ); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Registers unavailabe addons |
| 172 | * |
| 173 | * @since 1.3.0 |
| 174 | * @version 2.1.3 |
| 175 | */ |
| 176 | private function register_unavailable_addons() { |
| 177 | add_settings_section( "unavailable_addons", "Unavailable plugins", array( |
| 178 | $this, |
| 179 | "header_unavailable_addons" |
| 180 | ), "cookiebot-addons" ); |
| 181 | |
| 182 | $addons = $this->settings_service->get_addons(); |
| 183 | |
| 184 | foreach ( $addons as $addon ) { |
| 185 | if ( ( ! $addon->is_addon_installed() || ! $addon->is_addon_activated() ) |
| 186 | && $this->settings_service->is_latest_plugin_version( $addon ) |
| 187 | && ! $this->settings_service->is_previous_version_active( |
| 188 | $addons, |
| 189 | get_class( $addon ) |
| 190 | ) |
| 191 | ) { |
| 192 | // not installed plugins |
| 193 | add_settings_field( |
| 194 | $addon->get_addon_name(), |
| 195 | $addon->get_addon_name() . $this->get_extra_information( $addon ), |
| 196 | array( |
| 197 | $this, |
| 198 | "unavailable_addon_callback" |
| 199 | ), |
| 200 | "cookiebot-addons", |
| 201 | "unavailable_addons", |
| 202 | array( 'addon' => $addon ) |
| 203 | ); |
| 204 | register_setting( $addon->get_option_name(), "cookiebot_unavailable_addons" ); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Adds extra information under the label. |
| 211 | * |
| 212 | * @param $addon |
| 213 | * |
| 214 | * @return string |
| 215 | */ |
| 216 | private function get_extra_information( $addon ) { |
| 217 | return ( $addon->get_extra_information() !== false ) ? '<div class="extra_information">' . $addon->get_extra_information() . '</div>' : ''; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Jetpack tab - header |
| 222 | * |
| 223 | * @since 1.3.0 |
| 224 | */ |
| 225 | public function header_jetpack_addon() { |
| 226 | echo '<p>' . __( 'Jetpack settings.', 'cookiebot' ) . '</p>'; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Jetpack tab - widget callback |
| 231 | * |
| 232 | * @param $args array Information about the widget addon and the option |
| 233 | * |
| 234 | * @since 1.3.0 |
| 235 | */ |
| 236 | public function jetpack_addon_callback( $args ) { |
| 237 | include COOKIEBOT_ADDONS_DIR . 'view/admin/settings/jetpack-addon-callback.php'; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Returns header for installed plugins |
| 242 | * |
| 243 | * @since 1.3.0 |
| 244 | */ |
| 245 | public function header_available_addons() { |
| 246 | ?> |
| 247 | <p> |
| 248 | <?php _e( 'Below is a list of addons for Cookiebot. Addons help you make installed plugins GDPR compliant.', 'cookiebot' ); ?> |
| 249 | <br/> |
| 250 | <?php _e( 'These addons are available because you have the corresponding plugins installed and activated.', 'cookiebot' ); ?> |
| 251 | <br/> |
| 252 | <?php _e( 'Deactivate an addon if you want to handle GDPR compliance yourself, or through another plugin.', 'cookiebot' ); ?> |
| 253 | </p> |
| 254 | <?php |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Available addon callback: |
| 259 | * - checkbox to enable |
| 260 | * - select field for cookie type |
| 261 | * |
| 262 | * @param $args |
| 263 | * |
| 264 | * @since 1.3.0 |
| 265 | */ |
| 266 | public function available_addon_callback( $args ) { |
| 267 | include COOKIEBOT_ADDONS_DIR . 'view/admin/settings/available-addon-callback.php'; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Returns header for unavailable plugins |
| 272 | * |
| 273 | * @since 1.3.0 |
| 274 | */ |
| 275 | public function header_unavailable_addons() { |
| 276 | echo '<p>' . __( 'The following addons are unavailable. This is because the corresponding plugin is not installed.', 'cookiebot' ) . '</p>'; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Unavailable addon callback |
| 281 | * |
| 282 | * @param $args |
| 283 | * |
| 284 | * @since 1.3.0 |
| 285 | */ |
| 286 | public function unavailable_addon_callback( $args ) { |
| 287 | $addon = $args['addon']; |
| 288 | |
| 289 | ?> |
| 290 | <div class="postbox cookiebot-addon"> |
| 291 | <i><?php |
| 292 | if ( ! $addon->is_addon_installed() ) { |
| 293 | _e( 'The plugin is not installed.', 'cookiebot' ); |
| 294 | } else if ( ! $addon->is_addon_activated() ) { |
| 295 | _e( 'The plugin is not activated.', 'cookiebot' ); |
| 296 | } |
| 297 | ?></i> |
| 298 | </div> |
| 299 | <?php |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Build up settings page |
| 304 | * |
| 305 | * @param string $active_tab |
| 306 | * |
| 307 | * @since 1.3.0 |
| 308 | */ |
| 309 | public function setting_page( $active_tab = '' ) { |
| 310 | include COOKIEBOT_ADDONS_DIR . 'view/admin/settings/setting-page.php'; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Post action hook after enabling the addon on the settings page. |
| 315 | * |
| 316 | * @param $old_value |
| 317 | * @param $value |
| 318 | * @param $option_name |
| 319 | * |
| 320 | * @since 2.2.0 |
| 321 | */ |
| 322 | public function post_hook_available_addons_update_option( $old_value, $value, $option_name ) { |
| 323 | if ( is_array( $value ) ) { |
| 324 | foreach ( $value as $addon_option_name => $addon_settings ) { |
| 325 | if ( isset( $addon_settings['enabled'] ) ) { |
| 326 | $this->settings_service->post_hook_after_enabling_addon_on_settings_page( $addon_option_name ); |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 |