query-string parameters (url-param tag) */ public array $query; /** @param array $query */ public function __construct(int $post_id = 0, int $user_id = 0, ?object $term = null, array $query = []) { $this->post_id = max(0, $post_id); $this->user_id = max(0, $user_id); $this->term = $term; $this->query = $query; } public static function current(): self { $post_id = function_exists('get_the_ID') ? (int) get_the_ID() : 0; $term = null; if (function_exists('get_queried_object')) { $queried = get_queried_object(); if (is_object($queried) && isset($queried->term_id, $queried->taxonomy)) { $term = $queried; } } return new self($post_id, self::current_user_id(), $term, self::request_query()); } public static function for_post(int $post_id): self { return new self($post_id, self::current_user_id(), null, self::request_query()); } /** @param array $query */ public function with_query(array $query): self { $clone = clone $this; $clone->query = $query; return $clone; } /** @return \WP_Post|null */ public function post(): ?object { if ($this->post_id <= 0 || !function_exists('get_post')) { return null; } $post = get_post($this->post_id); return is_object($post) ? $post : null; } private static function current_user_id(): int { return function_exists('get_current_user_id') ? (int) get_current_user_id() : 0; } /** @return array */ private static function request_query(): array { // Public query-string values for the read-only "URL parameter" dynamic tag (e.g. ?utm_source=x printed on // a landing page). Nothing is changed by them, so there is no nonce; every value is unslashed and // sanitized here, and nested arrays are dropped. $query = map_deep(wp_unslash($_GET), 'sanitize_text_field'); // phpcs:ignore WordPress.Security.NonceVerification.Recommended return is_array($query) ? array_filter($query, 'is_scalar') : []; } }