*/ class Age_Sentry_Public { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of the plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; } /** * Register the stylesheets for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_styles() { wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/age-sentry-public.css', array(), $this->version, 'all' ); wp_enqueue_style( 'flatpickr', plugin_dir_url( __FILE__ ) . 'lib/flatpickr/flatpickr.min.css', array(), $this->version, 'all' ); wp_enqueue_style( 'flatpickr-theme-dark', plugin_dir_url( __FILE__ ) . 'lib/flatpickr/themes/dark.css', array( 'flatpickr' ), $this->version, 'all' ); $bg_color = sanitize_hex_color( get_option( 'age_sentry_color_popup_bg', '#1a1b20' ) ); $primary_color = sanitize_hex_color( get_option( 'age_sentry_color_primary', '#10b981' ) ); $text_color = sanitize_hex_color( get_option( 'age_sentry_color_text', '#ffffff' ) ); $muted_color = sanitize_hex_color( get_option( 'age_sentry_color_text_muted', '#9ca3af' ) ); $custom_css = " :root { --age_sentry-bg-popup: {$bg_color}; --age_sentry-primary: {$primary_color}; --age_sentry-text-main: {$text_color}; --age_sentry-text-muted: {$muted_color}; } "; wp_add_inline_style( $this->plugin_name, $custom_css ); } /** * Register the JavaScript for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_scripts() { wp_enqueue_script( 'flatpickr', plugin_dir_url( __FILE__ ) . 'lib/flatpickr/flatpickr.min.js', array( 'jquery' ), $this->version, true ); wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/age-sentry-public.js', array( 'jquery', 'flatpickr' ), $this->version, true ); $min_age = absint( get_option( 'age_sentry_min_age', 18 ) ); wp_localize_script( $this->plugin_name, 'age_sentryData', array( 'cookieDuration' => absint( get_option( 'age_sentry_cookie_duration', 30 ) ), 'redirectUrl' => esc_url( get_option( 'age_sentry_redirect_url', 'https://google.com' ) ), 'minAge' => $min_age, 'errorSelectDate' => __( 'Please select your date of birth.', 'age-sentry' ), /* translators: %d: Minimum age requirement */ 'errorUnderage' => sprintf( __( 'You must be %d or older to enter.', 'age-sentry' ), $min_age ), ) ); } /** * Determine whether the current visitor is a known search engine crawler. * * Used to keep the age gate from blocking indexing, since a popup that * hides the page content from crawlers can hurt SEO. * * @since 1.0.0 * @return bool True if the user agent matches a known crawler. */ private function is_crawler() { $crawlers = 'Googlebot|Bingbot|Slurp|DuckDuckBot|Baiduspider|YandexBot|Sogou|Exabot|facebot|facebookexternalhit|twitterbot'; $user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : ''; return (bool) preg_match( '/(' . $crawlers . ')/i', $user_agent ); } /** * Output the age verification popup markup in the site footer. * * Skips rendering entirely when the visitor already carries the * "age_verified" cookie, is a logged-in user and bypass is enabled, or * is a search engine crawler and crawler bypass is enabled. * * @since 1.0.0 */ public function render_popup() { if ( isset( $_COOKIE['age_verified'] ) && 'yes' === $_COOKIE['age_verified'] ) { return; } if ( get_option( 'age_sentry_bypass_logged_in', '1' ) && is_user_logged_in() ) { return; } if ( get_option( 'age_sentry_bypass_crawlers', '1' ) && $this->is_crawler() ) { return; } require plugin_dir_path( __FILE__ ) . 'partials/age-sentry-public-display.php'; } }