|null */ private static $context = null; /** * The minimum age THIS page asks for, 0 for none. * * Decided once per request, after the main query (the `wp` action): before it, * nothing is known about the page and the answer is 0, never guessed. * * @return int */ public static function current() { if ( null !== self::$current ) { return self::$current; } if ( ! did_action( 'wp' ) || ! isset( $GLOBALS['wp_query'] ) || ! $GLOBALS['wp_query'] instanceof \WP_Query ) { return 0; } self::$context = self::context( $GLOBALS['wp_query'] ); self::$current = self::age( self::$context, Settings::get() ); return self::$current; } /** * This request's page (context()), once decided; blank() before. * * @return array */ public static function current_context() { self::current(); return null === self::$context ? self::blank() : self::$context; } /** * Forget this request's answer (the tests decide several pages in one process). * * @return void */ public static function reset() { self::$current = null; self::$context = null; } /** * The minimum age for a page, from its context and the settings. * * @param array $ctx What the page is (context()). * @param array $settings Settings::get(). * @return int 0, or a minimum age from 18 to 25. */ public static function age( array $ctx, array $settings ) { $ctx = array_merge( self::blank(), $ctx ); $age = self::decide( $ctx, $settings ); /** * Filters the minimum age a page asks for (0 = the gate is not shown). * * Decide from the page only — the context holds the page, its categories and * its marks. A page cache keeps ONE copy of a page for every visitor, so an * answer that depends on the visitor (a cookie, the browser, the country) is * served to everyone else. The never-list and the gate's switch are applied * again after the filter: neither can be undone here. * * @param int $age 0, or 18–25. * @param array $ctx The page (see Scope::context()). * @param array $settings The settings. */ $filtered = apply_filters( 'agevow_page_age', $age, $ctx, $settings ); if ( '' !== $ctx['never'] || empty( $settings['gate'] ) || self::owner_never( $ctx, $settings ) ) { return 0; } return 0 === $filtered || Settings::is_age( $filtered ) ? (int) $filtered : $age; } /** * The decision itself, with no filter: pure, so every scope can be proven * without a request. * * @param array $ctx What the page is (context()). * @param array $settings Settings::get(). * @return int */ public static function decide( array $ctx, array $settings ) { $ctx = array_merge( self::blank(), $ctx ); $settings = array_merge( Settings::defaults(), $settings ); if ( empty( $settings['gate'] ) || '' !== $ctx['never'] || self::owner_never( $ctx, $settings ) ) { return 0; } $base = Settings::is_age( $settings['min_age'] ) ? (int) $settings['min_age'] : Settings::AGE_MIN; $marks = Settings::is_age( $ctx['marks'] ) ? (int) $ctx['marks'] : 0; $age = max( $base, $marks ); switch ( $settings['scope'] ) { case 'site': return $age; case 'restricted': return $marks > 0 ? $age : 0; case 'chosen': return self::chosen( $ctx, $settings ) ? $age : 0; } return 0; } /** * Whether the page is one the owner put on the never-list. * * @param array $ctx The page. * @param array $settings The settings. * @return bool */ private static function owner_never( array $ctx, array $settings ) { $never = isset( $settings['never'] ) ? array_map( 'intval', (array) $settings['never'] ) : array(); return (int) $ctx['page_id'] > 0 && in_array( (int) $ctx['page_id'], $never, true ); } /** * Whether a page is one the owner chose: a chosen page, or a post or archive of * a chosen post category (or of a category under one). * * @param array $ctx The page. * @param array $settings The settings. * @return bool */ private static function chosen( array $ctx, array $settings ) { $pages = array_map( 'intval', (array) $settings['pages'] ); if ( (int) $ctx['page_id'] > 0 && in_array( (int) $ctx['page_id'], $pages, true ) ) { return true; } $cats = array_map( 'intval', (array) $settings['post_categories'] ); return array() !== array_intersect( array_map( 'intval', (array) $ctx['cats'] ), $cats ); } /** * An empty context: a page that is nothing in particular. * * @return array */ public static function blank() { return array( 'never' => '', 'kind' => 'other', 'page_id' => 0, 'post_id' => 0, 'term_id' => 0, 'cats' => array(), 'marks' => 0, 'preview' => false, ); } /** * What a request shows, read from its query and from what is the same for every * visitor: the never-list reason ('' when none), the kind of page, the page it * IS (a page, the posts page, the shop page), the post categories it belongs to * (with the categories above them), and the highest mark it carries. * * @param \WP_Query $q The main query. * @return array */ public static function context( \WP_Query $q ) { $ctx = self::blank(); $ctx['never'] = self::never( $q ); if ( '' !== $ctx['never'] ) { return $ctx; } $ctx['preview'] = (bool) $q->is_preview(); if ( $q->is_singular() ) { $post = $q->get_queried_object(); if ( $post instanceof \WP_Post ) { $ctx['kind'] = $post->post_type; $ctx['post_id'] = (int) $post->ID; if ( 'page' === $post->post_type ) { $ctx['page_id'] = (int) $post->ID; } if ( 'post' === $post->post_type ) { $ctx['cats'] = self::with_ancestors( self::term_ids( $post->ID, 'category' ), 'category' ); } $ctx['marks'] = self::post_marks( $post, $ctx['cats'] ); } } elseif ( $q->is_category() || $q->is_tax( 'product_cat' ) ) { $term = $q->get_queried_object(); if ( $term instanceof \WP_Term ) { $ctx['kind'] = $term->taxonomy; $ctx['term_id'] = (int) $term->term_id; $up = self::with_ancestors( array( (int) $term->term_id ), $term->taxonomy ); if ( 'category' === $term->taxonomy ) { $ctx['cats'] = $up; } $ctx['marks'] = self::highest_mark( $up ); } } elseif ( $q->is_home() ) { $ctx['kind'] = 'home'; $ctx['page_id'] = max( 0, (int) get_option( 'page_for_posts' ) ); } elseif ( self::is_shop( $q ) ) { $ctx['kind'] = 'shop'; $ctx['page_id'] = self::wc_page( 'shop' ); } elseif ( $q->is_search() ) { $ctx['kind'] = 'search'; } elseif ( $q->is_404() ) { $ctx['kind'] = '404'; } elseif ( $q->is_archive() ) { $ctx['kind'] = 'archive'; } return $ctx; } /** * Why a request can never carry the gate, or ''. Only the address and the site's * own pages decide it — never who asks. * * @param \WP_Query $q The main query. * @return string One of NEVER (minus 'owner', which needs the settings), or ''. */ public static function never( \WP_Query $q ) { $checks = array( 'admin' => is_admin(), 'ajax' => wp_doing_ajax(), 'cron' => wp_doing_cron(), 'rest' => defined( 'REST_REQUEST' ) && REST_REQUEST, 'xmlrpc' => defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST, 'login' => isset( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'], ); foreach ( $checks as $why => $hit ) { if ( $hit ) { return $why; } } if ( $q->is_feed() ) { return 'feed'; } if ( '' !== (string) $q->get( 'sitemap' ) || '' !== (string) $q->get( 'sitemap-stylesheet' ) ) { return 'sitemap'; } if ( $q->is_robots() ) { return 'robots'; } if ( $q->is_favicon() ) { return 'favicon'; } if ( $q->is_trackback() ) { return 'trackback'; } if ( $q->is_embed() ) { return 'embed'; } if ( $q->is_singular( 'page' ) ) { $id = (int) $q->get_queried_object_id(); $privacy = (int) get_option( 'wp_page_for_privacy_policy' ); if ( $privacy > 0 && $id === $privacy ) { return 'privacy'; } $account = self::wc_page( 'myaccount' ); if ( $account > 0 && $id === $account ) { return 'account'; } } return ''; } /** * Whether the query is WooCommerce's shop page (the product archive). * * @param \WP_Query $q The query. * @return bool */ private static function is_shop( \WP_Query $q ) { return post_type_exists( 'product' ) && $q->is_post_type_archive( 'product' ); } /** * One of WooCommerce's own pages (shop, myaccount …), 0 without WooCommerce. * * @param string $page The page's key. * @return int */ private static function wc_page( $page ) { if ( ! function_exists( 'wc_get_page_id' ) ) { return 0; } $id = (int) wc_get_page_id( $page ); return $id > 0 ? $id : 0; } /** * A post's terms of one taxonomy, as ids. * * @param int $post_id Post id. * @param string $taxonomy Taxonomy. * @return int[] */ private static function term_ids( $post_id, $taxonomy ) { if ( ! taxonomy_exists( $taxonomy ) ) { return array(); } $terms = get_the_terms( (int) $post_id, $taxonomy ); if ( ! is_array( $terms ) ) { return array(); } return array_map( static function ( $t ) { return (int) $t->term_id; }, $terms ); } /** * Terms and every term above them, unique and sorted. * * @param int[] $ids Term ids. * @param string $taxonomy Their taxonomy. * @return int[] */ public static function with_ancestors( array $ids, $taxonomy ) { $all = array(); foreach ( $ids as $id ) { $all[] = (int) $id; foreach ( get_ancestors( (int) $id, $taxonomy, 'taxonomy' ) as $up ) { $all[] = (int) $up; } } $all = array_values( array_unique( $all ) ); sort( $all ); return $all; } /** * The highest mark among terms, 0 when none is marked. * * @param int[] $ids Term ids. * @return int */ private static function highest_mark( array $ids ) { return Restrictions::highest( $ids ); } /** * The highest mark a post carries: a product's own and its product categories' * (and those above them); a post's categories'. * * @param \WP_Post $post The post. * @param int[] $cats Its post categories, ancestors included. * @return int */ private static function post_marks( \WP_Post $post, array $cats ) { if ( 'product' === $post->post_type ) { return Restrictions::effective( (int) $post->ID ); } return self::highest_mark( $cats ); } /** * What the gate would cover with these settings — for the owner's screen * (`agevow/v1/scope/preview`): the pages, categories and products by name, each * with its age; the pages that are never gated; whether the rest of the site is. * * @param array $settings Settings (may be unsaved). * @return array */ public static function preview( array $settings ) { $settings = array_merge( Settings::defaults(), $settings ); $base = Settings::is_age( $settings['min_age'] ) ? (int) $settings['min_age'] : Settings::AGE_MIN; $scope = $settings['scope']; $marked = self::marked( $base ); $covered = array(); if ( 'site' === $scope ) { // Everything is covered at the owner's age; list what a mark raises. foreach ( $marked as $m ) { if ( $m['age'] > $base ) { $covered[] = $m; } } } elseif ( 'restricted' === $scope ) { $covered = $marked; } else { foreach ( (array) $settings['pages'] as $id ) { $post = get_post( (int) $id ); if ( $post && 'page' === $post->post_type ) { $covered[] = self::item( 'page', (int) $post->ID, get_the_title( $post ), get_permalink( $post ), $base ); } } foreach ( (array) $settings['post_categories'] as $id ) { $t = get_term( (int) $id, 'category' ); if ( $t instanceof \WP_Term ) { $age = max( $base, self::highest_mark( self::with_ancestors( array( (int) $t->term_id ), 'category' ) ) ); $covered[] = self::item( 'category', (int) $t->term_id, $t->name, get_term_link( $t ), $age ); } } } // The pages that are never gated, whatever the scope: the site's own, then the owner's. $never = array(); $own = array( 'privacy' => (int) get_option( 'wp_page_for_privacy_policy' ), 'account' => self::wc_page( 'myaccount' ), ); $listed = array(); $owner = array_map( 'intval', (array) $settings['never'] ); $sources = array(); foreach ( $own as $why => $id ) { $sources[] = array( $id, $why ); } foreach ( $owner as $id ) { $sources[] = array( $id, 'owner' ); } foreach ( $sources as $source ) { list( $id, $why ) = $source; $post = $id > 0 ? get_post( $id ) : null; if ( ! $post || in_array( $id, $listed, true ) ) { continue; } $listed[] = $id; $never[] = self::item( 'page', $id, get_the_title( $post ), get_permalink( $post ), 0, $why ); } return array( 'gate' => (bool) $settings['gate'], 'scope' => $scope, 'min_age' => $base, 'whole_site' => 'site' === $scope, 'covered' => array_slice( $covered, 0, self::PREVIEW_MAX ), 'never' => $never, 'always_open' => self::ALWAYS_OPEN, ); } /** * Every marked category and product, each with the age it asks (never below the * owner's). * * @param int $base The owner's minimum age. * @return array> */ private static function marked( $base ) { $marked = array(); foreach ( Restrictions::TAXONOMIES as $taxonomy ) { if ( ! taxonomy_exists( $taxonomy ) ) { continue; } $terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false, 'number' => self::PREVIEW_MAX, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- the marks are term meta by design (§S-ARCH); the owner's screen, never a page view. 'meta_key' => Restrictions::TERM_META, ) ); foreach ( is_array( $terms ) ? $terms : array() as $t ) { $age = Restrictions::term( (int) $t->term_id ); if ( $age > 0 ) { $marked[] = self::item( $taxonomy, (int) $t->term_id, $t->name, get_term_link( $t ), max( $base, $age ) ); } } } if ( post_type_exists( 'product' ) ) { $products = get_posts( array( 'post_type' => 'product', 'post_status' => array( 'publish', 'private', 'draft', 'pending', 'future' ), 'numberposts' => self::PREVIEW_MAX, 'orderby' => 'title', 'order' => 'ASC', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- the marks are product meta by design (§S-ARCH); the owner's screen, never a page view. 'meta_key' => Restrictions::PRODUCT_META, ) ); foreach ( $products as $p ) { $age = Restrictions::product( (int) $p->ID ); if ( $age > 0 ) { $marked[] = self::item( 'product', (int) $p->ID, get_the_title( $p ), get_permalink( $p ), max( $base, $age ) ); } } } return $marked; } /** * One line of the preview. * * @param string $type page · category · product_cat · product. * @param int $id Its id. * @param string $title Its name. * @param string|\WP_Error $url Its address. * @param int $age The age it asks (0 = never asked). * @param string $why For a never-gated page: privacy · account · owner. * @return array */ private static function item( $type, $id, $title, $url, $age, $why = '' ) { $out = array( 'type' => $type, 'id' => (int) $id, 'title' => html_entity_decode( wp_strip_all_tags( (string) $title ), ENT_QUOTES, 'UTF-8' ), 'url' => is_string( $url ) ? esc_url_raw( $url ) : '', 'age' => (int) $age, ); if ( '' !== $why ) { $out['why'] = $why; } return $out; } }