array( 'agevow_mark_product', 'agevow_product_nonce' ), 'quick' => array( 'agevow_mark_quick', 'agevow_quick_nonce' ), 'bulk' => array( 'agevow_mark_bulk', 'agevow_bulk_nonce' ), 'term' => array( 'agevow_mark_term', 'agevow_term_nonce' ), ); /** * Each form's field. */ const FIELDS = array( 'product' => 'agevow_product_age', 'quick' => 'agevow_quick_age', 'bulk' => 'agevow_bulk_age', 'term' => 'agevow_term_age', ); /** * Hook the screens. WooCommerce's hooks simply never fire without WooCommerce. * * @return void */ public static function init() { add_action( 'woocommerce_product_options_general_product_data', array( self::class, 'product_panel' ) ); add_action( 'woocommerce_process_product_meta', array( self::class, 'save_product' ), 20 ); add_action( 'woocommerce_product_quick_edit_end', array( self::class, 'quick_edit' ) ); add_action( 'woocommerce_product_quick_edit_save', array( self::class, 'save_quick' ) ); add_action( 'woocommerce_product_bulk_edit_end', array( self::class, 'bulk_edit' ) ); add_action( 'woocommerce_product_bulk_edit_save', array( self::class, 'save_bulk' ) ); add_filter( 'manage_product_posts_columns', array( self::class, 'product_columns' ), 20 ); add_action( 'manage_product_posts_custom_column', array( self::class, 'product_cell' ), 10, 2 ); add_action( 'admin_enqueue_scripts', array( self::class, 'enqueue' ) ); foreach ( Restrictions::TAXONOMIES as $taxonomy ) { add_action( $taxonomy . '_add_form_fields', array( self::class, 'term_add' ) ); add_action( $taxonomy . '_edit_form_fields', array( self::class, 'term_edit' ) ); add_action( 'created_' . $taxonomy, array( self::class, 'save_term' ) ); add_action( 'edited_' . $taxonomy, array( self::class, 'save_term' ) ); add_filter( 'manage_edit-' . $taxonomy . '_columns', array( self::class, 'term_columns' ) ); add_filter( 'manage_' . $taxonomy . '_custom_column', array( self::class, 'term_cell' ), 10, 3 ); } } // ── the control ──────────────────────────────────────────────────────────── /** * The choices: not age-restricted, then 18 to 25 and older ('' = no change, bulk). * * @param bool $no_change Offer "— No change —" first (bulk edit). * @return array */ public static function choices( $no_change = false ) { $out = array(); if ( $no_change ) { $out[''] = __( '— No change —', 'agevow-age-verification-age-gate' ); } $out['0'] = __( 'Not age-restricted', 'agevow-age-verification-age-gate' ); for ( $age = Settings::AGE_MIN; $age <= Settings::AGE_MAX; $age++ ) { /* translators: %d: a minimum age, 18 to 25. */ $out[ (string) $age ] = sprintf( __( '%d or older', 'agevow-age-verification-age-gate' ), $age ); } return $out; } /** * A select of the choices. * * @param string $name Its name. * @param string $id Its id ('' = none). * @param string $selected The chosen value. * @param bool $no_change Offer "— No change —" first. * @param string $described The id of the text that describes it ('' = none). * @return void */ private static function select( $name, $id, $selected, $no_change = false, $described = '' ) { printf( ''; } /** * The value a form sent for one of our fields — after its nonce: null when the * nonce is missing or wrong, when nothing was sent, when "no change" was chosen or * when the value is not a mark; otherwise 0 (not restricted) or 18–25. * * @param string $form product · quick · bulk · term. * @return int|null */ public static function posted( $form ) { if ( ! isset( self::NONCES[ $form ] ) ) { return null; } list( $action, $nonce_field ) = self::NONCES[ $form ]; if ( ! isset( $_REQUEST[ $nonce_field ] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST[ $nonce_field ] ) ), $action ) ) { return null; } $field = self::FIELDS[ $form ]; if ( ! isset( $_REQUEST[ $field ] ) || ! is_string( $_REQUEST[ $field ] ) ) { return null; } $value = sanitize_text_field( wp_unslash( $_REQUEST[ $field ] ) ); return '' !== $value && Restrictions::is_mark( $value ) ? (int) $value : null; } // ── a product: its data panel ────────────────────────────────────────────── /** * The control on the product's General tab, with what its categories ask. * * @return void */ public static function product_panel() { global $post; $id = $post instanceof \WP_Post ? (int) $post->ID : 0; if ( $id <= 0 || ! current_user_can( 'edit_product', $id ) ) { return; } $own = Restrictions::product( $id ); $up = Restrictions::inherited( $id ); list( $action, $nonce_field ) = self::NONCES['product']; ?>

0 ) { /* translators: %d: a minimum age, 18 to 25. */ return sprintf( __( 'Its category asks %d or older. The higher age of the two is the one asked.', 'agevow-age-verification-age-gate' ), (int) $inherited ); } return __( 'Shoppers are asked their age for this product, at checkout and, if the gate covers restricted products, on its page.', 'agevow-age-verification-age-gate' ); } /** * Save the product's control (WooCommerce has checked its own nonce; ours and the * user's right to edit THIS product are checked here). * * @param int $post_id The product. * @return void */ public static function save_product( $post_id ) { $age = self::posted( 'product' ); if ( null === $age || ! Security::can_edit_product( (int) $post_id ) ) { return; } Restrictions::set_product( (int) $post_id, $age ); } // ── the Products list: quick edit, bulk edit, the column ───────────────── /** * The control in quick edit. It opens on "— No change —" and admin/js/products.js * sets it to the row's own mark — so a quick edit made where that script never ran * can never clear a mark by accident. * * @return void */ public static function quick_edit() { list( $action, $nonce_field ) = self::NONCES['quick']; ?>
get_id() : 0; $age = self::posted( 'quick' ); if ( null === $age || ! Security::can_edit_product( $id ) ) { return; } Restrictions::set_product( $id, $age ); } /** * The control in bulk edit, "— No change —" first. * * @return void */ public static function bulk_edit() { list( $action, $nonce_field ) = self::NONCES['bulk']; ?> get_id() : 0; $age = self::posted( 'bulk' ); if ( null === $age || ! Security::can_edit_product( $id ) ) { return; } Restrictions::set_product( $id, $age ); } /** * The "Age" column, before the date (or last). * * @param array $columns The list's columns. * @return array */ public static function product_columns( $columns ) { return self::with_column( (array) $columns ); } /** * Insert our column before the date column, or at the end. * * @param array $columns Columns. * @return array */ private static function with_column( array $columns ) { $label = __( 'Age', 'agevow-age-verification-age-gate' ); $out = array(); foreach ( $columns as $key => $name ) { if ( 'date' === $key || 'posts' === $key ) { $out[ self::COLUMN ] = $label; } $out[ $key ] = $name; } if ( ! isset( $out[ self::COLUMN ] ) ) { $out[ self::COLUMN ] = $label; } return $out; } /** * One product's cell: "21+", where it comes from, and the product's own mark for * quick edit. * * @param string $column The column. * @param int $post_id The product. * @return void */ public static function product_cell( $column, $post_id ) { if ( self::COLUMN !== $column ) { return; } $own = Restrictions::product( (int) $post_id ); $up = Restrictions::inherited( (int) $post_id ); echo wp_kses( self::cell_html( $own, $up ), self::cell_tags() ); printf( '', esc_attr( (string) $own ) ); } /** * A cell's markup: the age a product asks (its own or its category's, whichever is * higher), and words for a screen reader. * * @param int $own Its own mark. * @param int $inherited Its categories' mark. * @return string */ public static function cell_html( $own, $inherited ) { $age = max( (int) $own, (int) $inherited ); if ( $age <= 0 ) { return '' . esc_html__( 'Not age-restricted', 'agevow-age-verification-age-gate' ) . ''; } /* translators: %d: a minimum age, 18 to 25. */ $badge = '' . esc_html( sprintf( __( '%d+', 'agevow-age-verification-age-gate' ), $age ) ) . ''; if ( (int) $inherited > (int) $own ) { $badge .= ' ' . esc_html__( 'from its category', 'agevow-age-verification-age-gate' ) . ''; } return $badge; } /** * The tags a cell may hold. * * @return array> */ private static function cell_tags() { return array( 'span' => array( 'class' => true, 'aria-hidden' => true, ), 'strong' => array( 'class' => true ), ); } // ── a category: adding one, editing one, the list ─────────────────────── /** * The words under a category's control. * * @param string $taxonomy product_cat or category. * @return string */ public static function term_help( $taxonomy ) { if ( 'product_cat' === $taxonomy ) { return __( 'Shoppers are asked their age for every product in this category and in the categories under it. A product\'s own higher age wins.', 'agevow-age-verification-age-gate' ); } return __( 'When the gate covers only age-restricted content, visitors are asked their age on the posts of this category and of the categories under it.', 'agevow-age-verification-age-gate' ); } /** * The control on "Add new category". * * @param string $taxonomy The taxonomy. * @return void */ public static function term_add( $taxonomy ) { list( $action, $nonce_field ) = self::NONCES['term']; ?>

term_id ) ) { return; } list( $action, $nonce_field ) = self::NONCES['term']; ?> term_id ), false, 'agevow-term-age-help' ); ?>

taxonomy ) ); ?>

$columns Columns. * @return array */ public static function term_columns( $columns ) { return self::with_column( (array) $columns ); } /** * One category's cell. * * @param string $content What other code put there. * @param string $column The column. * @param int $term_id The category. * @return string */ public static function term_cell( $content, $column, $term_id ) { if ( self::COLUMN !== $column ) { return $content; } return wp_kses( self::cell_html( Restrictions::term( (int) $term_id ), 0 ), self::cell_tags() ); } // ── the Products list's script and style ───────────────────────────────── /** * On the Products list only: the script that fills quick edit, and the column's width. * * @param string $hook_suffix The screen being loaded. * @return void */ public static function enqueue( $hook_suffix ) { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; $list = 'edit.php' === $hook_suffix && $screen && 'product' === $screen->post_type; $terms = 'edit-tags.php' === $hook_suffix && $screen && in_array( $screen->taxonomy, Restrictions::TAXONOMIES, true ); if ( ! $list && ! $terms ) { return; } wp_enqueue_style( 'agevow-products', AGEVOW_URL . 'admin/css/products.css', array(), AGEVOW_VERSION ); if ( $list ) { wp_enqueue_script( 'agevow-products', AGEVOW_URL . 'admin/js/products.js', array(), AGEVOW_VERSION, true ); } } }