post_type && 'trash' !== $post->post_status; } /** * Whether an id names a category a mark may sit on. * * @param mixed $id Term id. * @return bool */ public static function is_term( $id ) { if ( ! Settings::int_between( $id, 1, PHP_INT_MAX ) ) { return false; } $term = get_term( (int) $id ); return $term && ! is_wp_error( $term ) && in_array( $term->taxonomy, self::TAXONOMIES, true ); } /** * A product's own minimum age, 0 when it is not marked. * * @param int $id Product id. * @return int */ public static function product( $id ) { $age = get_post_meta( (int) $id, self::PRODUCT_META, true ); return Settings::is_age( $age ) ? (int) $age : 0; } /** * A category's minimum age, 0 when it is not marked. * * @param int $id Term id. * @return int */ public static function term( $id ) { $age = get_term_meta( (int) $id, self::TERM_META, true ); return Settings::is_age( $age ) ? (int) $age : 0; } /** * The highest mark among categories, 0 when none is marked. * * @param int[] $term_ids Term ids. * @return int */ public static function highest( array $term_ids ) { if ( array() === $term_ids ) { return 0; } update_termmeta_cache( array_map( 'intval', $term_ids ) ); $max = 0; foreach ( $term_ids as $id ) { $max = max( $max, self::term( (int) $id ) ); } return $max; } /** * The product a variation belongs to (a mark sits on the product), or the id itself. * * @param int $id Product or variation id. * @return int */ public static function parent_product( $id ) { $post = get_post( (int) $id ); if ( $post && 'product_variation' === $post->post_type && $post->post_parent > 0 ) { return (int) $post->post_parent; } return (int) $id; } /** * The age a product inherits from its product categories and the categories above * them — the highest mark among them, 0 when none is marked (§S-WOO). * * @param int $id Product (or variation) id. * @return int */ public static function inherited( $id ) { $id = self::parent_product( $id ); if ( ! taxonomy_exists( 'product_cat' ) ) { return 0; } $terms = get_the_terms( $id, 'product_cat' ); if ( ! is_array( $terms ) ) { return 0; } $ids = array(); foreach ( $terms as $t ) { $ids[] = (int) $t->term_id; } return self::highest( Scope::with_ancestors( $ids, 'product_cat' ) ); } /** * The minimum age a product asks: its own mark or its categories', whichever is * higher; 0 when it is not age-restricted (§S-WOO: "the highest age wins"). * * @param int $id Product (or variation) id. * @return int */ public static function effective( $id ) { $id = self::parent_product( $id ); return max( self::product( $id ), self::inherited( $id ) ); } /** * Mark a product (18–25) or clear its mark (0), and write the change to the history. * * @param int $id Product id. * @param mixed $age 0, or a minimum age. * @param int|null $user_id Who changed it (default: the current user). * @return bool Whether the product now carries what was asked. */ public static function set_product( $id, $age, $user_id = null ) { if ( ! self::is_product( $id ) || ! self::is_mark( $age ) ) { return false; } $id = (int) $id; $before = self::product( $id ); $after = (int) $age; if ( $before === $after ) { return true; } if ( 0 === $after ) { delete_post_meta( $id, self::PRODUCT_META ); } else { update_post_meta( $id, self::PRODUCT_META, $after ); } self::changed( 'product' ); History::record( 'product', array( 'id' => $id, 'age' => $before, ), array( 'id' => $id, 'age' => $after, ), $user_id ); return true; } /** * Mark a category (18–25) or clear its mark (0), and write the change to the history. * * @param int $id Term id. * @param mixed $age 0, or a minimum age. * @param int|null $user_id Who changed it (default: the current user). * @return bool Whether the category now carries what was asked. */ public static function set_term( $id, $age, $user_id = null ) { if ( ! self::is_term( $id ) || ! self::is_mark( $age ) ) { return false; } $id = (int) $id; $before = self::term( $id ); $after = (int) $age; if ( $before === $after ) { return true; } if ( 0 === $after ) { delete_term_meta( $id, self::TERM_META ); } else { update_term_meta( $id, self::TERM_META, $after ); } $taxonomy = get_term( $id )->taxonomy; self::changed( 'term' ); History::record( 'term', array( 'id' => $id, 'taxonomy' => $taxonomy, 'age' => $before, ), array( 'id' => $id, 'taxonomy' => $taxonomy, 'age' => $after, ), $user_id ); return true; } /** * A mark changed: while the gate is on, the pages it covers have changed, and a * page cache's copies of them are cleared (§S-CACHE). * * @param string $what product or term. * @return void */ private static function changed( $what ) { $settings = Settings::get(); // The gate covers marked pages; the product page's line names the age (Woo). if ( $settings['gate'] || Woo::note_on( $settings ) ) { Cache::purge( $what ); } } /** * How many products and categories carry a mark (the Gate screen says it). * * @return int */ public static function count() { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- one count of this plugin's own marks, for one screen. $products = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i AS m INNER JOIN %i AS p ON p.ID = m.post_id WHERE m.meta_key = %s AND m.meta_value <> '' AND p.post_status <> 'trash'", $wpdb->postmeta, $wpdb->posts, self::PRODUCT_META ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- one count of this plugin's own marks, for one screen. $terms = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE meta_key = %s AND meta_value <> ''", $wpdb->termmeta, self::TERM_META ) ); return $products + $terms; } /** * Whether a value is a mark: 0 (none) or a minimum age. * * @param mixed $age Value. * @return bool */ public static function is_mark( $age ) { return 0 === $age || '0' === $age || Settings::is_age( $age ); } }