'abc123', * 'mode' => 'entire_site' | 'selected', * 'show' => ['home' => bool, 'post_types' => string[], 'items' => int[]], * 'hide' => ['home' => bool, 'post_types' => string[], 'items' => int[]], * ] * * - `entire_site` matches every front-end request; `selected` matches only * its Show rules, and with none it is shortcode-only. * - Hide is checked first and always wins, in both modes. * - `home` is the front page. A post type covers its singles and its * archives, as the context builder maps them into `archive_of`. * - `items` match the queried object of a singular view only, never the * loop's first post. */ class Placement_Evaluator { /** * @param array $context From Request_Context. * @param array $placements Records in priority order, earliest first. Later wins. * @return string|null The winning namespace, or null when none matches. */ public static function winner(array $context, array $placements): ?string { for ($i = count($placements) - 1; $i >= 0; $i--) { $placement = $placements[$i]; if (is_array($placement) && self::matches($context, $placement)) { // The ?string return type casts a numeric namespace to a string. return $placement['namespace']; } } return null; } /** * Whether one placement matches the request, ignoring every other agent. */ public static function matches(array $context, array $placement): bool { if (!isset($placement['namespace']) || '' === (string) $placement['namespace']) { return false; } $context = $context + [ 'front_page' => false, 'singular' => false, 'object_id' => 0, 'post_type' => '', 'archive_of' => '', ]; if (self::rules_match(isset($placement['hide']) ? $placement['hide'] : [], $context)) { return false; } if (isset($placement['mode']) && 'entire_site' === $placement['mode']) { return true; } return self::rules_match(isset($placement['show']) ? $placement['show'] : [], $context); } private static function rules_match($rules, array $context): bool { if (!is_array($rules)) { return false; } if (!empty($rules['home']) && $context['front_page']) { return true; } $post_types = isset($rules['post_types']) && is_array($rules['post_types']) ? $rules['post_types'] : []; foreach ($post_types as $post_type) { $post_type = (string) $post_type; if ('' === $post_type) { continue; } if ($context['singular'] && $post_type === (string) $context['post_type']) { return true; } if ($post_type === (string) $context['archive_of']) { return true; } } $object_id = (int) $context['object_id']; if ($context['singular'] && $object_id > 0 && isset($rules['items']) && is_array($rules['items'])) { foreach ($rules['items'] as $item) { if ((int) $item === $object_id) { return true; } } } return false; } }