'array', 'items' => array( 'type' => 'string', 'enum' => self::TYPES, ), ); } /** * Gets the default value for a newly created condition of this type. * * @return mixed */ public function default_value() { return array(); } /** * Validates a value against this condition's schema. * * Every entry must be a known page type (the legacy alias included). * * @param mixed $value Value to validate. * @return bool */ public function is_valid_value( $value ): bool { $items = is_array( $value ) ? $value : array( $value ); foreach ( $items as $item ) { if ( ! is_string( $item ) || ( self::LEGACY_ANY !== $item && ! in_array( $item, self::TYPES, true ) ) ) { return false; } } return true; } /** * Evaluates the condition against the given context. * * @param Context $context Request context value object. * @param string $operator Operator identifier. * @param mixed $value Configured value. * @return bool */ public function evaluate( Context $context, string $operator, $value ): bool { $match = false; foreach ( $this->string_list( $value ) as $type ) { if ( $this->matches( $context, $type ) ) { $match = true; break; } } return 'is_not' === $operator ? ! $match : $match; } /** * Resolves whether the current view matches the chosen page type. * * @param Context $context Request context. * @param string $type Chosen page type. * @return bool */ private function matches( Context $context, string $type ): bool { switch ( $type ) { case 'home': return $context->is_home; case 'front_page': return $context->is_front_page; case 'search': return $context->is_search; case '404': return $context->is_404; case 'single': return $context->is_single; case 'archive': case self::LEGACY_ANY: return $context->is_archive; default: return $context->is_archive && $context->archive_type === $type; } } }