block_name = $block_name; $this->path = $path; $this->model = $model; $this->source_kind = $source_kind; $this->source_id = $source_id; $this->source_title = $source_title; $this->source_label = $source_label; $this->edit_url = $edit_url; $this->post_id = $post_id; $this->post_type = $post_type; $this->warnings = $warnings; $this->preview = $preview; } /** * Returns a copy carrying the given source information. * * @param string $kind Source kind (a SOURCE_* constant). * @param string $id Identifier within the source kind. * @param string $title Human-readable location title. * @param string $label Translated label for the source kind. * @param string $edit_url Admin edit URL. * @param int $post_id Post id, when applicable. * @param string $type Post type, when applicable. * @return self */ public function with_source( string $kind, string $id, string $title, string $label, string $edit_url = '', int $post_id = 0, string $type = '' ): self { return new self( $this->block_name, $this->path, $this->model, $kind, $id, $title, $label, $edit_url, $post_id, $type, $this->warnings, $this->preview ); } /** * Returns a copy carrying analyzer output. * * @param string[] $warnings Warning messages. * @param array $preview Preview verdicts. * @return self */ public function with_analysis( array $warnings, array $preview ): self { return new self( $this->block_name, $this->path, $this->model, $this->source_kind, $this->source_id, $this->source_title, $this->source_label, $this->edit_url, $this->post_id, $this->post_type, $warnings, $preview ); } /** * Reports whether the visibility model is switched on. * * @return bool */ public function is_enabled(): bool { return ! empty( $this->model['enabled'] ); } /** * Returns the combinator used to join the conditions ('all' or 'any'). * * @return string */ public function relation(): string { return isset( $this->model['relation'] ) && 'any' === $this->model['relation'] ? 'any' : 'all'; } /** * Returns the stored conditions, filtered to well-formed entries. * * @return array */ public function conditions(): array { $conditions = $this->model['conditions'] ?? array(); if ( ! is_array( $conditions ) ) { return array(); } return array_values( array_filter( $conditions, 'is_array' ) ); } /** * Returns the condition type ids used by this block. * * @return string[] */ public function condition_types(): array { $types = array(); foreach ( $this->conditions() as $condition ) { if ( isset( $condition['type'] ) && is_string( $condition['type'] ) ) { $types[] = $condition['type']; } } return $types; } /** * Reports whether the analyzer attached any warnings. * * @return bool */ public function has_warnings(): bool { return array() !== $this->warnings; } /** * Returns a stable identifier for this occurrence. * * @return string */ public function key(): string { return $this->source_kind . ':' . $this->source_id . ':' . $this->path; } }