'', 'domain_name' => '', 'api_key' => '', 'enabled' => true, 'script_position' => 'head', 'excluded_posts' => '', ); } /** * Get the per-domain API key for authenticated requests. * * Checks the COOKIEFRAME_SECRET constant first (wp-config.php, backward compat), * then falls back to the api_key stored in plugin options, and finally the * legacy 'secret' option key for sites upgrading from an older version. * * @return string The API key, or empty string if not configured. */ public static function get_api_key() { if ( defined( 'COOKIEFRAME_SECRET' ) && COOKIEFRAME_SECRET ) { return COOKIEFRAME_SECRET; } $options = self::get_options(); if ( ! empty( $options['api_key'] ) ) { return $options['api_key']; } // Legacy fallback for sites that still have 'secret' in their options. return isset( $options['secret'] ) ? $options['secret'] : ''; } /** * Merged options with defaults. * * @return array */ public static function get_options() { return wp_parse_args( get_option( self::OPTION_NAME, array() ), self::get_defaults() ); } /** * Register hooks. */ public function init() { new CookieFrame_Shortcodes(); if ( is_admin() ) { new CookieFrame_Admin(); new CookieFrame_Dashboard_Widget(); } if ( class_exists( 'WP_Consent_API' ) ) { new CookieFrame_WP_Consent_API(); } add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_widget_script' ) ); } /** * Enqueue the CookieFrame widget script. */ public function enqueue_widget_script() { if ( ! $this->should_inject() ) { return; } $options = self::get_options(); $domain_id = $options['domain_id']; $in_footer = ( 'footer' === $options['script_position'] ); $script_url = COOKIEFRAME_API_URL . '/api/widget/' . rawurlencode( $domain_id ) . '/script.js'; wp_enqueue_script( 'cookieframe-widget', $script_url, array(), COOKIEFRAME_VERSION, $in_footer ); wp_script_add_data( 'cookieframe-widget', 'async', true ); } /** * Determine whether the script should be injected on the current request. * * @return bool */ private function should_inject() { $options = self::get_options(); if ( empty( $options['domain_id'] ) ) { return false; } if ( empty( $options['enabled'] ) ) { return false; } if ( is_admin() ) { return false; } if ( wp_doing_ajax() ) { return false; } if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { return false; } $excluded = array_filter( array_map( 'intval', explode( ',', $options['excluded_posts'] ) ) ); if ( ! empty( $excluded ) && is_singular() && in_array( get_the_ID(), $excluded, true ) ) { return false; } return true; } }