$restricted_term_ids ) { if ( empty( $restricted_term_ids ) ) { continue; } $post_terms = wp_get_post_terms( $post_id, $tax_slug, array( 'fields' => 'ids' ) ); if ( is_wp_error( $post_terms ) ) { continue; } foreach ( $post_terms as $term_id ) { if ( self::post_term_matches_restricted_list( $term_id, $tax_slug, $restricted_term_ids ) ) { return $age; } } } } } return 0; } /** * Load content gate styles when the current singular view may show the gate. */ public function enqueue_content_gate_assets() { if ( ! is_singular() ) { return; } if ( 'yes' !== get_option( 'ageonce_enabled' ) ) { return; } $post_id = get_queried_object_id(); if ( ! $post_id ) { return; } if ( ! self::should_replace_front_content( $post_id ) ) { return; } $required_age = self::get_required_age_for_content( $post_id ); if ( $required_age <= 0 || AgeOnce_API::is_verified( true, $required_age ) ) { return; } $version = defined( 'AGEONCE_VERIFICATION_VERSION' ) ? AGEONCE_VERIFICATION_VERSION : '1.0.0'; $base = defined( 'AGEONCE_VERIFICATION_PLUGIN_FILE' ) ? AGEONCE_VERIFICATION_PLUGIN_FILE : dirname( __DIR__ ) . '/ageonce-verification.php'; wp_enqueue_style( 'ageonce-content-gate', plugins_url( 'assets/css/content-gate.css', $base ), array(), $version ); } public static function is_product_restricted( $product_id ) { return self::get_required_age_for_content( $product_id ) > 0; } /** * WooCommerce product pages stay visible by default. Product rules still * require Verify Age at checkout. Posts and pages keep the content gate. * * @param int $post_id Post or product ID. * @return bool */ public static function should_replace_front_content( $post_id ) { $post_id = absint( $post_id ); if ( $post_id <= 0 ) { return false; } if ( get_post_type( $post_id ) !== 'product' ) { return true; } $rules = (array) get_option( 'ageonce_rules', array() ); return isset( $rules['product']['gate_product_pages'] ) && $rules['product']['gate_product_pages'] === 'yes'; } public function filter_content( $content ) { global $post; if ( ! $post ) { return $content; } if ( ! self::should_replace_front_content( $post->ID ) ) { return $content; } $required_age = self::get_required_age_for_content( $post->ID ); if ( $required_age <= 0 || AgeOnce_API::is_verified( true, $required_age ) ) { return $content; } return $this->get_gate_html( $required_age, $post->ID ); } private function get_gate_html( $age, $post_id ) { $verification_url = $this->get_verification_url( $age, $post_id ); ob_start(); ?>
ID, '_ageonce_restricted', true ); wp_nonce_field( 'ageonce_save', 'ageonce_nonce' ); echo ''; } public function save_universal_meta_box( $post_id ) { if ( ! isset( $_POST['ageonce_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['ageonce_nonce'] ) ), 'ageonce_save' ) ) { return; } if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } update_post_meta( $post_id, '_ageonce_restricted', isset( $_POST['_ageonce_restricted'] ) ? 'yes' : 'no' ); } public function add_product_meta_field() { echo ''; } public function save_product_meta_field( $post_id ) { if ( ! isset( $_POST['ageonce_product_meta_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['ageonce_product_meta_nonce'] ) ), 'ageonce_product_meta' ) ) { return; } if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } $is_restricted = isset( $_POST['_ageonce_restricted'] ) ? 'yes' : 'no'; update_post_meta( $post_id, '_ageonce_restricted', $is_restricted ); } }