'post_title', 'number' => 500 ) ); if ( is_array( $pages_for_map ) ) { foreach ( $pages_for_map as $page_for_map ) { $page_id = (int) $page_for_map->ID; $page_url = get_permalink( $page_id ); if ( ! is_string( $page_url ) || $page_url === '' ) { continue; } $entrance_pages_map[ (string) $page_id ] = array( 'title' => (string) $page_for_map->post_title, 'url' => $page_url, ); } } wp_localize_script( 'ageonce-admin-settings', 'ageonceAdmin', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'testNonce' => wp_create_nonce( 'ageonce_test_connection' ), 'testingText' => __( 'Testing connection...', 'age-verification-with-ageonce' ), 'missingFieldsText' => __( 'Enter Client ID and Client Secret first.', 'age-verification-with-ageonce' ), 'genericErrorText' => __( 'Connection test failed. Please try again.', 'age-verification-with-ageonce' ), 'successTitleText' => __( 'Connection verified', 'age-verification-with-ageonce' ), 'successHintText' => __( 'Next: configure rules and run your first checkout/content test below.', 'age-verification-with-ageonce' ), 'errorTitleText' => __( 'Connection failed', 'age-verification-with-ageonce' ), 'saveNonce' => wp_create_nonce( 'ageonce_save_settings' ), 'saveTitleText' => __( 'Settings saved', 'age-verification-with-ageonce' ), 'saveHintText' => __( 'Changes were saved without page reload.', 'age-verification-with-ageonce' ), 'savingText' => __( 'Saving...', 'age-verification-with-ageonce' ), 'selectedCountText' => __( '%d selected', 'age-verification-with-ageonce' ), 'settingsUpdated' => isset( $_GET['settings-updated'] ) && sanitize_text_field( wp_unslash( $_GET['settings-updated'] ) ) === 'true', 'mediaTitle' => __( 'Select gate logo', 'age-verification-with-ageonce' ), 'mediaButton' => __( 'Use this logo', 'age-verification-with-ageonce' ), 'resetAppearanceNonce' => wp_create_nonce( 'ageonce_reset_appearance' ), 'resetAppearanceConfirm' => __( 'Reset entrance appearance to defaults? This clears the logo, background image, and entrance colours. This cannot be undone.', 'age-verification-with-ageonce' ), 'resetAppearanceTitle' => __( 'Entrance appearance reset', 'age-verification-with-ageonce' ), 'resetAppearanceHint' => __( 'Logo, background image, and entrance colours are back to defaults.', 'age-verification-with-ageonce' ), 'resetInlineConfirm' => __( 'Reset inline banner colours and text to defaults? This cannot be undone.', 'age-verification-with-ageonce' ), 'resetInlineTitle' => __( 'Inline appearance reset', 'age-verification-with-ageonce' ), 'resetInlineHint' => __( 'Inline colours, heading, and message are back to defaults.', 'age-verification-with-ageonce' ), 'resettingAppearanceText' => __( 'Resetting...', 'age-verification-with-ageonce' ), 'bgMediaTitle' => __( 'Select entrance background image', 'age-verification-with-ageonce' ), 'bgMediaButton' => __( 'Use this image', 'age-verification-with-ageonce' ), 'linkLabelPlaceholder' => __( 'Label (e.g. Privacy Policy)', 'age-verification-with-ageonce' ), 'linkTargetPlaceholder' => __( 'Select a page or type a URL', 'age-verification-with-ageonce' ), 'linkRemoveLabel' => __( 'Remove link', 'age-verification-with-ageonce' ), 'entrancePages' => $entrance_pages_map, ) ); } public function clean_admin_notices() { $screen = get_current_screen(); if ( $screen && $screen->id === 'toplevel_page_ageonce-settings' ) { remove_all_actions( 'admin_notices' ); remove_all_actions( 'all_admin_notices' ); } } public function register_menu_page() { add_menu_page( __( 'AgeOnce Settings', 'age-verification-with-ageonce' ), __( 'AgeOnce', 'age-verification-with-ageonce' ), 'manage_options', 'ageonce-settings', array( $this, 'render_settings_page' ), 'dashicons-shield', 56 ); } public function register_settings() { register_setting( 'ageonce_group', 'ageonce_enabled', array( 'type' => 'string', 'default' => 'no', 'sanitize_callback' => function ( $value ) { return in_array( $value, array( 'yes', 'no' ), true ) ? $value : 'no'; }, ) ); register_setting( 'ageonce_group', 'ageonce_client_id', array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ) ); register_setting( 'ageonce_group', 'ageonce_client_secret', array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ) ); register_setting( 'ageonce_group', 'ageonce_rules', array( 'type' => 'array', 'default' => array(), 'sanitize_callback' => array( $this, 'sanitize_rules' ), ) ); register_setting( 'ageonce_group', 'ageonce_registration_enabled', array( 'type' => 'string', 'default' => 'no', 'sanitize_callback' => function ( $value ) { return in_array( $value, array( 'yes', 'no' ), true ) ? $value : 'no'; }, ) ); register_setting( 'ageonce_group', 'ageonce_registration_age', array( 'type' => 'string', 'default' => '18', 'sanitize_callback' => function ( $value ) { $age = (string) absint( $value ); return in_array( $age, array( '16', '18', '21' ), true ) ? $age : '18'; }, ) ); register_setting( 'ageonce_group', AgeOnce_API::OPTION_CONTENT_DURATION, array( 'type' => 'string', 'default' => AgeOnce_API::DEFAULT_PASS_DURATION, 'sanitize_callback' => function ( $value ) { return $this->sanitize_pass_duration( $value, AgeOnce_API::SURFACE_CONTENT ); }, ) ); register_setting( 'ageonce_group', AgeOnce_API::OPTION_CHECKOUT_DURATION, array( 'type' => 'string', 'default' => AgeOnce_API::DEFAULT_PASS_DURATION, 'sanitize_callback' => function ( $value ) { return $this->sanitize_pass_duration( $value, AgeOnce_API::SURFACE_CHECKOUT ); }, ) ); register_setting( 'ageonce_group', AgeOnce_Entrance_Gate::OPTION_ENABLED, array( 'type' => 'string', 'default' => 'no', 'sanitize_callback' => array( $this, 'sanitize_yes_no' ), ) ); register_setting( 'ageonce_group', AgeOnce_Entrance_Gate::OPTION_AGE, array( 'type' => 'string', 'default' => '18', 'sanitize_callback' => array( $this, 'sanitize_entrance_age' ), ) ); register_setting( 'ageonce_group', AgeOnce_Entrance_Gate::OPTION_ALLOWED_PAGES, array( 'type' => 'array', 'default' => array(), 'sanitize_callback' => array( $this, 'sanitize_entrance_pages' ), ) ); register_setting( 'ageonce_group', AgeOnce_Entrance_Gate::OPTION_LINKS, array( 'type' => 'array', 'default' => array(), 'sanitize_callback' => array( $this, 'sanitize_entrance_links' ), ) ); register_setting( 'ageonce_group', AgeOnce_Entrance_Gate::OPTION_BYPASS_LOGGED_IN, array( 'type' => 'string', 'default' => 'yes', 'sanitize_callback' => array( $this, 'sanitize_yes_no' ), ) ); register_setting( 'ageonce_group', AgeOnce_Entrance_Gate::OPTION_ALLOW_BOTS, array( 'type' => 'string', 'default' => 'no', 'sanitize_callback' => array( $this, 'sanitize_yes_no' ), ) ); register_setting( 'ageonce_group', AgeOnce_Entrance_Gate::OPTION_HEADING, array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ) ); register_setting( 'ageonce_group', AgeOnce_Entrance_Gate::OPTION_MESSAGE, array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_textarea_field', ) ); register_setting( 'ageonce_group', AgeOnce_Appearance::OPTION_LOGO_ID, array( 'type' => 'integer', 'default' => 0, 'sanitize_callback' => 'absint', ) ); register_setting( 'ageonce_group', AgeOnce_Appearance::OPTION_LOGO_WIDTH, array( 'type' => 'integer', 'default' => 0, 'sanitize_callback' => 'absint', ) ); register_setting( 'ageonce_group', AgeOnce_Appearance::OPTION_BG_IMAGE_ID, array( 'type' => 'integer', 'default' => 0, 'sanitize_callback' => 'absint', ) ); register_setting( 'ageonce_group', AgeOnce_Appearance::OPTION_CUSTOM_CSS, array( 'type' => 'string', 'default' => '', 'sanitize_callback' => array( $this, 'sanitize_custom_css' ), ) ); register_setting( 'ageonce_group', AgeOnce_Appearance::OPTION_INLINE_HEADING, array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ) ); register_setting( 'ageonce_group', AgeOnce_Appearance::OPTION_INLINE_MESSAGE, array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_textarea_field', ) ); foreach ( array( 'entrance', 'inline' ) as $surface ) { foreach ( AgeOnce_Appearance::COLOR_KEYS as $color_key ) { $defaults = AgeOnce_Appearance::get_default_colors( $surface ); register_setting( 'ageonce_group', AgeOnce_Appearance::color_option_name( $surface, $color_key ), array( 'type' => 'string', 'default' => isset( $defaults[ $color_key ] ) ? $defaults[ $color_key ] : '', 'sanitize_callback' => array( $this, 'sanitize_appearance_color' ), ) ); } } } /** * @param mixed $value Raw colour. * @return string */ public function sanitize_appearance_color( $value ) { $hex = AgeOnce_Appearance::sanitize_hex_color( $value ); return ( $hex !== '' ) ? $hex : ''; } /** * @param mixed $value Raw CSS. * @return string */ public function sanitize_custom_css( $value ) { return wp_strip_all_tags( (string) $value ); } /** * Sanitize a yes/no switch value. * * @param mixed $value Raw value. * @return string 'yes' or 'no'. */ public function sanitize_yes_no( $value ) { return ( $value === 'yes' ) ? 'yes' : 'no'; } /** * Sanitize the entrance age against the allowed thresholds. * * @param mixed $value Raw value. * @return string One of 16/18/21. */ public function sanitize_entrance_age( $value ) { $age = (string) absint( $value ); return in_array( $age, AgeOnce_Entrance_Gate::ALLOWED_AGES, true ) ? $age : '18'; } /** * Sanitize the entrance allowlist: keep only IDs of existing pages. * * @param mixed $value Raw value (array of IDs). * @return int[] */ public function sanitize_entrance_pages( $value ) { if ( ! is_array( $value ) ) { return array(); } $out = array(); foreach ( $value as $id ) { $id = absint( $id ); if ( $id > 0 && get_post_type( $id ) === 'page' ) { $out[] = $id; } } return array_values( array_unique( $out ) ); } /** * Sanitize configurable gate links (array of label/url rows). * * @param mixed $value Raw value. * @return array */ public function sanitize_entrance_links( $value ) { if ( ! is_array( $value ) ) { return array(); } $out = array(); foreach ( $value as $row ) { if ( ! is_array( $row ) ) { continue; } $label = isset( $row['label'] ) ? sanitize_text_field( (string) $row['label'] ) : ''; $raw_url = isset( $row['url'] ) ? trim( (string) $row['url'] ) : ''; if ( $raw_url === '' ) { continue; } // Resolve page:ID leftovers from the picker into a real permalink. if ( preg_match( '/^page:(\d+)$/', $raw_url, $page_match ) ) { $page_id = absint( $page_match[1] ); $permalink = $page_id > 0 ? get_permalink( $page_id ) : false; if ( ! is_string( $permalink ) || $permalink === '' ) { continue; } $raw_url = $permalink; if ( $label === '' ) { $title = get_the_title( $page_id ); $label = is_string( $title ) ? sanitize_text_field( $title ) : ''; } } if ( strpos( $raw_url, '/' ) === 0 ) { $url = esc_url_raw( home_url( $raw_url ) ); } elseif ( preg_match( '#^https?://#i', $raw_url ) ) { $url = esc_url_raw( $raw_url ); } else { $url = esc_url_raw( 'https://' . ltrim( $raw_url, '/' ) ); } if ( $url === '' ) { continue; } if ( $label === '' ) { $label = $url; } $out[] = array( 'label' => $label, 'url' => $url, ); if ( count( $out ) >= AgeOnce_Entrance_Gate::MAX_LINKS ) { break; } } return $out; } /** * Accept only the pass durations offered for a surface. Anything else keeps the saved choice. * * @param mixed $value Raw submitted value. * @param string $surface AgeOnce_API::SURFACE_CONTENT or AgeOnce_API::SURFACE_CHECKOUT. * @return string Duration key. */ private function sanitize_pass_duration( $value, $surface ) { $key = is_scalar( $value ) ? sanitize_key( (string) $value ) : ''; $allowed = AgeOnce_API::get_allowed_pass_durations( $surface ); return isset( $allowed[ $key ] ) ? $key : AgeOnce_API::get_pass_duration_key( $surface ); } /** * Label for a pass duration choice. * * @param string $key Duration key from AgeOnce_API::get_allowed_pass_durations(). * @return string */ private function get_pass_duration_label( $key ) { switch ( $key ) { case 'session': return __( 'Until the browser is closed (24 hours at most)', 'age-verification-with-ageonce' ); case '1h': return __( '1 hour', 'age-verification-with-ageonce' ); case '7d': return __( '7 days', 'age-verification-with-ageonce' ); case '30d': return __( '30 days', 'age-verification-with-ageonce' ); case '24h': default: return __( '24 hours (default)', 'age-verification-with-ageonce' ); } } /** * Human labels for appearance colour tokens. * * @return array */ /** * Colour field labels for a surface. * * @param string $surface entrance|inline. * @return array */ private function get_appearance_color_labels( $surface = 'entrance' ) { $labels = array( 'bg' => __( 'Background colour', 'age-verification-with-ageonce' ), 'card' => __( 'Card / panel', 'age-verification-with-ageonce' ), 'text' => __( 'Text', 'age-verification-with-ageonce' ), 'muted' => __( 'Muted text', 'age-verification-with-ageonce' ), 'button' => __( 'Button', 'age-verification-with-ageonce' ), 'button_text' => __( 'Button text', 'age-verification-with-ageonce' ), 'accent' => __( 'Accent (18+ badge)', 'age-verification-with-ageonce' ), ); if ( 'inline' === $surface ) { $labels['bg'] = __( 'Banner background', 'age-verification-with-ageonce' ); $labels['text'] = __( 'Text (optional)', 'age-verification-with-ageonce' ); $labels['muted'] = __( 'Muted text (optional)', 'age-verification-with-ageonce' ); $labels['button'] = __( 'Button (optional)', 'age-verification-with-ageonce' ); $labels['button_text'] = __( 'Button text (optional)', 'age-verification-with-ageonce' ); $labels['accent'] = __( 'Accent', 'age-verification-with-ageonce' ); } return $labels; } /** * Render one entrance-screen link row: label + single page/URL picker. * * @param int $index Row index. * @param array $row Keys: label, url. * @param array $allowed_pages Pages currently allowed without the gate. * @param string $field Option name for links. */ private function render_entrance_link_row( $index, array $row, array $allowed_pages, $field ) { $label = isset( $row['label'] ) ? (string) $row['label'] : ''; $url = isset( $row['url'] ) ? (string) $row['url'] : ''; $matched_page_id = 0; $url_norm = $url !== '' ? untrailingslashit( $url ) : ''; if ( $url_norm !== '' ) { foreach ( $allowed_pages as $page ) { $page_url = untrailingslashit( (string) get_permalink( $page->ID ) ); if ( $page_url !== '' && $page_url === $url_norm ) { $matched_page_id = (int) $page->ID; break; } } } $target_value = $matched_page_id > 0 ? 'page:' . $matched_page_id : $url; ?> $colors Current colours. * @param array|null $only_keys Optional subset of COLOR_KEYS. */ private function render_appearance_color_fields( $surface, array $colors, $only_keys = null ) { $surface = AgeOnce_Appearance::normalize_surface( $surface ); $labels = $this->get_appearance_color_labels( $surface ); $defaults = AgeOnce_Appearance::get_default_colors( $surface ); $keys = AgeOnce_Appearance::COLOR_KEYS; if ( is_array( $only_keys ) && ! empty( $only_keys ) ) { $keys = array_values( array_intersect( $keys, $only_keys ) ); } elseif ( 'inline' === $surface ) { // Inline banners are a single panel — no separate page bg vs card. $keys = array( 'bg', 'text', 'muted', 'button', 'button_text', 'accent' ); } echo '
'; foreach ( $keys as $key ) { $opt = AgeOnce_Appearance::color_option_name( $surface, $key ); $value = isset( $colors[ $key ] ) ? $colors[ $key ] : ''; $label = isset( $labels[ $key ] ) ? $labels[ $key ] : $key; $default_hex = isset( $defaults[ $key ] ) ? $defaults[ $key ] : $value; ?>
'; } /** * Sanitize content rules option (nested array). * * @param mixed $value Raw option value. * @return array Sanitized rules. */ public function sanitize_rules( $value ) { if ( ! is_array( $value ) ) { return array(); } $allowed_ages = array( '16', '18', '21' ); $out = array(); foreach ( $value as $pt_raw => $data ) { $pt = sanitize_key( (string) $pt_raw ); if ( $pt === '' || ! post_type_exists( $pt ) || ! is_array( $data ) ) { continue; } $out[ $pt ] = array(); if ( isset( $data['enabled'] ) ) { $out[ $pt ]['enabled'] = ( $data['enabled'] === 'yes' ) ? 'yes' : 'no'; } if ( isset( $data['manual_lock_enabled'] ) ) { $out[ $pt ]['manual_lock_enabled'] = ( $data['manual_lock_enabled'] === 'yes' ) ? 'yes' : 'no'; } if ( $pt === 'product' ) { $out[ $pt ]['gate_product_pages'] = ( isset( $data['gate_product_pages'] ) && $data['gate_product_pages'] === 'yes' ) ? 'yes' : 'no'; } if ( isset( $data['enabled_ages'] ) && is_array( $data['enabled_ages'] ) ) { $clean_ages = array(); foreach ( $data['enabled_ages'] as $a ) { $a = (string) absint( $a ); if ( in_array( $a, $allowed_ages, true ) ) { $clean_ages[] = $a; } } $clean_ages = array_values( array_unique( $clean_ages ) ); if ( ! empty( $clean_ages ) ) { $out[ $pt ]['enabled_ages'] = $clean_ages; } } if ( isset( $data['ages'] ) && is_array( $data['ages'] ) ) { $tax_for_pt = get_object_taxonomies( $pt, 'names' ); $out[ $pt ]['ages'] = array(); foreach ( $data['ages'] as $age_key => $age_block ) { $age_str = (string) absint( $age_key ); if ( ! in_array( $age_str, $allowed_ages, true ) || ! is_array( $age_block ) ) { continue; } if ( empty( $age_block['taxonomies'] ) || ! is_array( $age_block['taxonomies'] ) ) { continue; } $out[ $pt ]['ages'][ $age_str ] = array( 'taxonomies' => array() ); foreach ( $age_block['taxonomies'] as $tax_raw => $term_ids ) { $tax = sanitize_key( (string) $tax_raw ); if ( $tax === '' || ! in_array( $tax, $tax_for_pt, true ) || ! is_array( $term_ids ) ) { continue; } $valid_ids = array(); foreach ( $term_ids as $tid ) { $tid = absint( $tid ); if ( $tid <= 0 ) { continue; } $term = get_term( $tid, $tax ); if ( $term && ! is_wp_error( $term ) ) { $valid_ids[] = $tid; } } $valid_ids = array_values( array_unique( $valid_ids ) ); if ( ! empty( $valid_ids ) ) { $out[ $pt ]['ages'][ $age_str ]['taxonomies'][ $tax ] = $valid_ids; } } } } } return $out; } private function get_formatted_terms( $taxonomy ) { $terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false, 'suppress_filter' => true, ) ); if ( empty( $terms ) || is_wp_error( $terms ) ) { return array(); } $term_list = array(); $id_map = array(); foreach ( $terms as $term ) { $id_map[ $term->term_id ] = $term; } foreach ( $terms as $term ) { $name_parts = array( $term->name ); $current_parent = $term->parent; while ( $current_parent && isset( $id_map[ $current_parent ] ) ) { $parent_term = $id_map[ $current_parent ]; array_unshift( $name_parts, $parent_term->name ); $current_parent = $parent_term->parent; } $term_list[ $term->term_id ] = implode( ' > ', $name_parts ); } asort( $term_list ); return $term_list; } private function has_configured_rules( array $rules ) { foreach ( $rules as $data ) { if ( ! is_array( $data ) || ! isset( $data['enabled'] ) || $data['enabled'] !== 'yes' ) { continue; } if ( ! empty( $data['manual_lock_enabled'] ) && $data['manual_lock_enabled'] === 'yes' ) { return true; } if ( ! empty( $data['ages'] ) && is_array( $data['ages'] ) ) { foreach ( $data['ages'] as $age_block ) { if ( ! empty( $age_block['taxonomies'] ) && is_array( $age_block['taxonomies'] ) ) { foreach ( $age_block['taxonomies'] as $term_ids ) { if ( ! empty( $term_ids ) ) { return true; } } } } } } return false; } private function get_connection_signature( $client_id, $client_secret ) { return hash( 'sha256', (string) $client_id . '|' . (string) $client_secret . '|' . AgeOnce_API::get_redirect_uri() ); } private function is_connection_tested( $client_id, $client_secret ) { if ( trim( (string) $client_id ) === '' || trim( (string) $client_secret ) === '' ) { return false; } return get_option( 'ageonce_connection_test_signature', '' ) === $this->get_connection_signature( $client_id, $client_secret ); } private function render_setup_step( $is_done, $label, $description, $link_url = '', $link_label = '', $step_key = '' ) { $classes = 'ageonce-setup-step' . ( $is_done ? ' is-done' : '' ); $icon = $is_done ? 'dashicons-yes' : 'dashicons-marker'; $step_attr = $step_key ? ' data-ageonce-setup-step="' . esc_attr( $step_key ) . '"' : ''; ?>
  • > ↗
  • array( 'product_cat', 'product_tag' ), 'post' => array( 'category', 'post_tag' ), ); return isset( $primary_map[ $post_type ] ) && in_array( $taxonomy, $primary_map[ $post_type ], true ); } private function render_taxonomy_rule_field( $pt_slug, $age, $tax_slug, $tax_obj, array $terms_list, array $selected_terms ) { $selected_count = count( array_unique( array_map( 'absint', $selected_terms ) ) ); $select_id = 'ageonce-select-' . sanitize_html_class( $pt_slug . '-' . $age . '-' . $tax_slug ); ?>
    __( 'You do not have permission to test this connection.', 'age-verification-with-ageonce' ) ), 403 ); } check_ajax_referer( 'ageonce_test_connection', 'nonce' ); $client_id = isset( $_POST['client_id'] ) ? sanitize_text_field( wp_unslash( $_POST['client_id'] ) ) : ''; $client_secret = isset( $_POST['client_secret'] ) ? sanitize_text_field( wp_unslash( $_POST['client_secret'] ) ) : ''; if ( $client_id === '' || $client_secret === '' ) { wp_send_json_error( array( 'message' => __( 'Enter Client ID and Client Secret first.', 'age-verification-with-ageonce' ) ), 400 ); } $response = wp_remote_post( AGEONCE_API_URL . '/oauth/token', array( 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $client_id . ':' . $client_secret ), 'Content-Type' => 'application/x-www-form-urlencoded', ), 'body' => array( 'grant_type' => 'authorization_code', 'code' => 'ageonce_connection_test', 'redirect_uri' => AgeOnce_API::get_redirect_uri(), 'state' => 'ageonce_connection_test', ), 'timeout' => 15, ) ); if ( is_wp_error( $response ) ) { wp_send_json_error( array( 'message' => sprintf( /* translators: %s: WordPress HTTP API error message. */ __( 'Unable to reach AgeOnce API: %s', 'age-verification-with-ageonce' ), $response->get_error_message() ), ), 500 ); } $response_code = wp_remote_retrieve_response_code( $response ); $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body, true ); $error_code = is_array( $data ) && isset( $data['error'] ) ? sanitize_key( (string) $data['error'] ) : ''; if ( in_array( $error_code, array( 'invalid_grant', 'expired_code', 'code_already_used', 'verification_not_found' ), true ) ) { update_option( 'ageonce_client_id', $client_id, false ); update_option( 'ageonce_client_secret', $client_secret, false ); update_option( 'ageonce_connection_test_signature', $this->get_connection_signature( $client_id, $client_secret ), false ); wp_send_json_success( array( 'message' => __( 'Connection looks good. Credentials were saved and the Redirect URL was accepted by AgeOnce.', 'age-verification-with-ageonce' ) ) ); } if ( $error_code === 'invalid_client' ) { delete_option( 'ageonce_connection_test_signature' ); wp_send_json_error( array( 'message' => __( 'Invalid Client ID or Client Secret. Copy fresh credentials from the AgeOnce dashboard.', 'age-verification-with-ageonce' ) ), 401 ); } if ( $error_code === 'invalid_redirect_uri' ) { delete_option( 'ageonce_connection_test_signature' ); wp_send_json_error( array( 'message' => __( 'AgeOnce rejected the Redirect URL. Copy the Redirect URL below into your AgeOnce dashboard and try again.', 'age-verification-with-ageonce' ) ), 400 ); } wp_send_json_error( array( 'message' => sprintf( /* translators: 1: HTTP status code, 2: API error code. */ __( 'Unexpected API response (%1$d%2$s). Check credentials and try again.', 'age-verification-with-ageonce' ), absint( $response_code ), $error_code ? ', ' . $error_code : '' ), ), $response_code ? $response_code : 500 ); } /** * AJAX: reset Gate appearance options to plugin defaults. */ public function ajax_reset_appearance() { if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to reset appearance.', 'age-verification-with-ageonce' ) ), 403 ); } check_ajax_referer( 'ageonce_reset_appearance', 'nonce' ); $scope = isset( $_POST['scope'] ) ? sanitize_key( wp_unslash( (string) $_POST['scope'] ) ) : 'all'; if ( ! in_array( $scope, array( 'entrance', 'inline', 'all' ), true ) ) { $scope = 'all'; } $defaults = AgeOnce_Appearance::reset_to_defaults( $scope ); $message = __( 'Appearance reset to defaults.', 'age-verification-with-ageonce' ); if ( 'entrance' === $scope ) { $message = __( 'Entrance appearance reset to defaults.', 'age-verification-with-ageonce' ); } elseif ( 'inline' === $scope ) { $message = __( 'Inline appearance reset to defaults.', 'age-verification-with-ageonce' ); } wp_send_json_success( array( 'message' => $message, 'defaults' => $defaults, ) ); } public function ajax_save_settings() { if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to save settings.', 'age-verification-with-ageonce' ) ), 403 ); } check_ajax_referer( 'ageonce_save_settings', 'nonce' ); $form_data = isset( $_POST['form_data'] ) ? (string) wp_unslash( $_POST['form_data'] ) : ''; if ( $form_data === '' ) { wp_send_json_error( array( 'message' => __( 'No form data received.', 'age-verification-with-ageonce' ) ), 400 ); } $parsed = array(); parse_str( $form_data, $parsed ); $enabled = isset( $parsed['ageonce_enabled'] ) && $parsed['ageonce_enabled'] === 'yes' ? 'yes' : 'no'; $client_id = isset( $parsed['ageonce_client_id'] ) ? sanitize_text_field( (string) $parsed['ageonce_client_id'] ) : ''; $client_secret = isset( $parsed['ageonce_client_secret'] ) ? sanitize_text_field( (string) $parsed['ageonce_client_secret'] ) : ''; $rules = isset( $parsed['ageonce_rules'] ) ? $this->sanitize_rules( $parsed['ageonce_rules'] ) : array(); $registration_enabled = isset( $parsed['ageonce_registration_enabled'] ) && $parsed['ageonce_registration_enabled'] === 'yes' ? 'yes' : 'no'; $registration_age_raw = isset( $parsed['ageonce_registration_age'] ) ? (string) absint( $parsed['ageonce_registration_age'] ) : '18'; $registration_age = in_array( $registration_age_raw, array( '16', '18', '21' ), true ) ? $registration_age_raw : '18'; update_option( 'ageonce_enabled', $enabled, false ); update_option( 'ageonce_client_id', $client_id, false ); update_option( 'ageonce_client_secret', $client_secret, false ); update_option( 'ageonce_rules', $rules, false ); update_option( 'ageonce_registration_enabled', $registration_enabled, false ); update_option( 'ageonce_registration_age', $registration_age, false ); // Only change a duration when its field was submitted. The checkout field is absent without WooCommerce. if ( isset( $parsed[ AgeOnce_API::OPTION_CONTENT_DURATION ] ) ) { update_option( AgeOnce_API::OPTION_CONTENT_DURATION, $this->sanitize_pass_duration( $parsed[ AgeOnce_API::OPTION_CONTENT_DURATION ], AgeOnce_API::SURFACE_CONTENT ), false ); } if ( isset( $parsed[ AgeOnce_API::OPTION_CHECKOUT_DURATION ] ) ) { update_option( AgeOnce_API::OPTION_CHECKOUT_DURATION, $this->sanitize_pass_duration( $parsed[ AgeOnce_API::OPTION_CHECKOUT_DURATION ], AgeOnce_API::SURFACE_CHECKOUT ), false ); } // Site entrance gate settings. update_option( AgeOnce_Entrance_Gate::OPTION_ENABLED, ( isset( $parsed[ AgeOnce_Entrance_Gate::OPTION_ENABLED ] ) && $parsed[ AgeOnce_Entrance_Gate::OPTION_ENABLED ] === 'yes' ) ? 'yes' : 'no', false ); update_option( AgeOnce_Entrance_Gate::OPTION_AGE, $this->sanitize_entrance_age( isset( $parsed[ AgeOnce_Entrance_Gate::OPTION_AGE ] ) ? $parsed[ AgeOnce_Entrance_Gate::OPTION_AGE ] : '18' ), false ); update_option( AgeOnce_Entrance_Gate::OPTION_ALLOWED_PAGES, $this->sanitize_entrance_pages( isset( $parsed[ AgeOnce_Entrance_Gate::OPTION_ALLOWED_PAGES ] ) ? $parsed[ AgeOnce_Entrance_Gate::OPTION_ALLOWED_PAGES ] : array() ), false ); update_option( AgeOnce_Entrance_Gate::OPTION_LINKS, $this->sanitize_entrance_links( isset( $parsed[ AgeOnce_Entrance_Gate::OPTION_LINKS ] ) ? $parsed[ AgeOnce_Entrance_Gate::OPTION_LINKS ] : array() ), false ); update_option( AgeOnce_Entrance_Gate::OPTION_BYPASS_LOGGED_IN, ( isset( $parsed[ AgeOnce_Entrance_Gate::OPTION_BYPASS_LOGGED_IN ] ) && $parsed[ AgeOnce_Entrance_Gate::OPTION_BYPASS_LOGGED_IN ] === 'yes' ) ? 'yes' : 'no', false ); update_option( AgeOnce_Entrance_Gate::OPTION_ALLOW_BOTS, ( isset( $parsed[ AgeOnce_Entrance_Gate::OPTION_ALLOW_BOTS ] ) && $parsed[ AgeOnce_Entrance_Gate::OPTION_ALLOW_BOTS ] === 'yes' ) ? 'yes' : 'no', false ); update_option( AgeOnce_Entrance_Gate::OPTION_HEADING, isset( $parsed[ AgeOnce_Entrance_Gate::OPTION_HEADING ] ) ? sanitize_text_field( (string) $parsed[ AgeOnce_Entrance_Gate::OPTION_HEADING ] ) : '', false ); update_option( AgeOnce_Entrance_Gate::OPTION_MESSAGE, isset( $parsed[ AgeOnce_Entrance_Gate::OPTION_MESSAGE ] ) ? sanitize_textarea_field( (string) $parsed[ AgeOnce_Entrance_Gate::OPTION_MESSAGE ] ) : '', false ); update_option( AgeOnce_Appearance::OPTION_LOGO_ID, isset( $parsed[ AgeOnce_Appearance::OPTION_LOGO_ID ] ) ? absint( $parsed[ AgeOnce_Appearance::OPTION_LOGO_ID ] ) : 0, false ); update_option( AgeOnce_Appearance::OPTION_LOGO_WIDTH, isset( $parsed[ AgeOnce_Appearance::OPTION_LOGO_WIDTH ] ) ? absint( $parsed[ AgeOnce_Appearance::OPTION_LOGO_WIDTH ] ) : 0, false ); update_option( AgeOnce_Appearance::OPTION_BG_IMAGE_ID, isset( $parsed[ AgeOnce_Appearance::OPTION_BG_IMAGE_ID ] ) ? absint( $parsed[ AgeOnce_Appearance::OPTION_BG_IMAGE_ID ] ) : 0, false ); update_option( AgeOnce_Appearance::OPTION_CUSTOM_CSS, isset( $parsed[ AgeOnce_Appearance::OPTION_CUSTOM_CSS ] ) ? $this->sanitize_custom_css( $parsed[ AgeOnce_Appearance::OPTION_CUSTOM_CSS ] ) : '', false ); update_option( AgeOnce_Appearance::OPTION_INLINE_HEADING, isset( $parsed[ AgeOnce_Appearance::OPTION_INLINE_HEADING ] ) ? sanitize_text_field( (string) $parsed[ AgeOnce_Appearance::OPTION_INLINE_HEADING ] ) : '', false ); update_option( AgeOnce_Appearance::OPTION_INLINE_MESSAGE, isset( $parsed[ AgeOnce_Appearance::OPTION_INLINE_MESSAGE ] ) ? sanitize_textarea_field( (string) $parsed[ AgeOnce_Appearance::OPTION_INLINE_MESSAGE ] ) : '', false ); foreach ( array( 'entrance', 'inline' ) as $surface ) { $defaults = AgeOnce_Appearance::get_default_colors( $surface ); foreach ( AgeOnce_Appearance::COLOR_KEYS as $color_key ) { $opt = AgeOnce_Appearance::color_option_name( $surface, $color_key ); $default = isset( $defaults[ $color_key ] ) ? $defaults[ $color_key ] : ''; $raw = isset( $parsed[ $opt ] ) ? $parsed[ $opt ] : $default; $hex = AgeOnce_Appearance::sanitize_hex_color( $raw ); // Keep empty when the surface default is empty (inline text/button = theme). if ( $hex === '' && $default !== '' ) { $hex = $default; } update_option( $opt, $hex, false ); } } if ( ! $this->is_connection_tested( $client_id, $client_secret ) ) { delete_option( 'ageonce_connection_test_signature' ); } wp_send_json_success( array( 'message' => __( 'Settings saved.', 'age-verification-with-ageonce' ), ) ); } public function render_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $enabled = get_option( 'ageonce_enabled', 'yes' ); $client_id = get_option( 'ageonce_client_id', '' ); $client_secret = get_option( 'ageonce_client_secret', '' ); $saved_rules = (array) get_option( 'ageonce_rules', array() ); $registration_enabled = get_option( 'ageonce_registration_enabled', 'no' ); $registration_age = (string) absint( get_option( 'ageonce_registration_age', '18' ) ); if ( ! in_array( $registration_age, array( '16', '18', '21' ), true ) ) { $registration_age = '18'; } $missing_credentials = ( trim( (string) $client_id ) === '' || trim( (string) $client_secret ) === '' ); $has_rules = $this->has_configured_rules( $saved_rules ); $connection_tested = $this->is_connection_tested( $client_id, $client_secret ); $plugin_base = defined( 'AGEONCE_VERIFICATION_PLUGIN_FILE' ) ? AGEONCE_VERIFICATION_PLUGIN_FILE : dirname( __DIR__ ) . '/ageonce-verification.php'; $onboarding_image_url = plugins_url( 'assets/images/plan.png', $plugin_base ); $has_woocommerce = class_exists( 'WooCommerce' ); $content_duration = AgeOnce_API::get_pass_duration_key( AgeOnce_API::SURFACE_CONTENT ); $checkout_duration = AgeOnce_API::get_pass_duration_key( AgeOnce_API::SURFACE_CHECKOUT ); $entrance_enabled = get_option( AgeOnce_Entrance_Gate::OPTION_ENABLED, 'no' ); $entrance_age = AgeOnce_Entrance_Gate::get_entrance_age(); $entrance_pages = AgeOnce_Entrance_Gate::get_allowed_pages(); $entrance_links = AgeOnce_Entrance_Gate::get_links(); $entrance_bypass = get_option( AgeOnce_Entrance_Gate::OPTION_BYPASS_LOGGED_IN, 'yes' ); $entrance_bots = get_option( AgeOnce_Entrance_Gate::OPTION_ALLOW_BOTS, 'no' ); $entrance_heading = (string) get_option( AgeOnce_Entrance_Gate::OPTION_HEADING, '' ); $entrance_message = (string) get_option( AgeOnce_Entrance_Gate::OPTION_MESSAGE, '' ); $entrance_all_pages = get_pages( array( 'sort_column' => 'post_title', 'number' => 500 ) ); if ( ! is_array( $entrance_all_pages ) ) { $entrance_all_pages = array(); } $entrance_allowed_page_objects = array(); foreach ( $entrance_all_pages as $entrance_page ) { if ( in_array( (int) $entrance_page->ID, $entrance_pages, true ) ) { $entrance_allowed_page_objects[] = $entrance_page; } } $entrance_links_rows = ! empty( $entrance_links ) ? $entrance_links : array( array( 'label' => '', 'url' => '' ) ); $appearance_logo_id = AgeOnce_Appearance::get_logo_id(); $appearance_logo_width = AgeOnce_Appearance::get_logo_width(); $appearance_logo_url = $appearance_logo_id > 0 ? wp_get_attachment_image_url( $appearance_logo_id, 'medium' ) : ''; $appearance_bg_id = AgeOnce_Appearance::get_bg_image_id(); $appearance_bg_url = $appearance_bg_id > 0 ? wp_get_attachment_image_url( $appearance_bg_id, 'medium' ) : ''; $appearance_custom_css = AgeOnce_Appearance::get_custom_css(); $inline_heading = AgeOnce_Appearance::get_inline_heading(); $inline_message = AgeOnce_Appearance::get_inline_message(); $entrance_colors = AgeOnce_Appearance::get_colors( 'entrance' ); $inline_colors = AgeOnce_Appearance::get_colors( 'inline' ); if ( ! is_string( $appearance_logo_url ) ) { $appearance_logo_url = ''; } if ( ! is_string( $appearance_bg_url ) ) { $appearance_bg_url = ''; } $all_post_types = get_post_types( array( 'public' => true ), 'objects' ); unset( $all_post_types['attachment'] ); $ordered_types = array(); $priority_slugs = array( 'product', 'post', 'page' ); foreach ( $priority_slugs as $slug ) { if ( isset( $all_post_types[ $slug ] ) ) { $ordered_types[ $slug ] = $all_post_types[ $slug ]; unset( $all_post_types[ $slug ] ); } } ksort( $all_post_types ); $final_post_types = $ordered_types + $all_post_types; $age_groups = array( '16', '18', '21' ); ?>

      render_setup_step( ! $missing_credentials, __( 'Create account', 'age-verification-with-ageonce' ), __( 'Start free and get OAuth keys.', 'age-verification-with-ageonce' ), '', '', 'account' ); $this->render_setup_step( ! $missing_credentials, __( 'Paste credentials', 'age-verification-with-ageonce' ), __( 'Add Client ID and Secret below.', 'age-verification-with-ageonce' ), '', '', 'credentials' ); $this->render_setup_step( $connection_tested, __( 'Add Redirect URL', 'age-verification-with-ageonce' ), __( 'Copy this callback into AgeOnce.', 'age-verification-with-ageonce' ), '', '', 'redirect' ); $this->render_setup_step( $has_rules, __( 'Protect content', 'age-verification-with-ageonce' ), __( 'Choose products, posts, or pages.', 'age-verification-with-ageonce' ), '', '', 'rules' ); ?>
    <?php esc_attr_e( 'AgeOnce', 'age-verification-with-ageonce' ); ?>

    render_appearance_color_fields( 'entrance', $entrance_colors, array( 'bg' ) ); ?>

    render_appearance_color_fields( 'entrance', $entrance_colors, array( 'card', 'text', 'muted', 'button', 'button_text', 'accent' ) ); ?>

    render_appearance_color_fields( 'inline', $inline_colors ); ?>

    $pt_obj ) : $taxonomies = get_object_taxonomies( $pt_slug, 'objects' ); $is_enabled = isset( $saved_rules[ $pt_slug ]['enabled'] ) && $saved_rules[ $pt_slug ]['enabled'] === 'yes'; $enabled_ages = isset( $saved_rules[ $pt_slug ]['enabled_ages'] ) ? (array) $saved_rules[ $pt_slug ]['enabled_ages'] : array( '18' ); ?>

    labels->name ); ?>

    ' . esc_html( $age ) . '' ); ?>

    $tax_obj ) : $terms_list = $this->get_formatted_terms( $tax_slug ); if ( empty( $terms_list ) ) { continue; } $selected_terms = isset( $saved_rules[ $pt_slug ]['ages'][ $age ]['taxonomies'][ $tax_slug ] ) ? $saved_rules[ $pt_slug ]['ages'][ $age ]['taxonomies'][ $tax_slug ] : array(); $field = array( 'slug' => $tax_slug, 'object' => $tax_obj, 'terms' => $terms_list, 'selected' => (array) $selected_terms, ); $taxonomy_fields[] = $field; endforeach; foreach ( $taxonomy_fields as $field ) { if ( $this->is_primary_taxonomy( $pt_slug, $field['slug'] ) ) { $primary_taxonomies[] = $field; } else { $advanced_taxonomies[] = $field; } } if ( empty( $primary_taxonomies ) && ! empty( $advanced_taxonomies ) ) { $primary_taxonomies = array_splice( $advanced_taxonomies, 0, 1 ); } ?>
    render_taxonomy_rule_field( $pt_slug, $age, $field['slug'], $field['object'], $field['terms'], $field['selected'] ); } ?>

    render_taxonomy_rule_field( $pt_slug, $age, $field['slug'], $field['object'], $field['terms'], $field['selected'] ); } ?>