jetpack
/
vendor
/
wp-php-toolkit
/
data-liberation
/
DataFormatConsumer
/
class-blockswithmetadata.php
class-annotatedblockmarkupconsumer.php
6 days ago
class-blockswithmetadata.php
6 days ago
class-markupprocessorconsumer.php
6 days ago
interface-data-format-consumer.php
6 days ago
class-blockswithmetadata.php
87 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\DataLiberation\DataFormatConsumer; |
| 4 | |
| 5 | use WordPress\DataLiberation\DataLiberationException; |
| 6 | |
| 7 | /** |
| 8 | * Represents the result of a {data format} -> block markup conversion. |
| 9 | */ |
| 10 | class BlocksWithMetadata { |
| 11 | |
| 12 | private $block_markup; |
| 13 | private $metadata; |
| 14 | |
| 15 | public function __construct( $block_markup, $metadata = array() ) { |
| 16 | $this->block_markup = $block_markup; |
| 17 | $this->metadata = $metadata; |
| 18 | foreach ( $this->metadata as $key => $values ) { |
| 19 | if ( ! is_array( $values ) ) { |
| 20 | throw new DataLiberationException( sprintf( 'Metadata values for the key %s must be an array but was %s', esc_html( $key ), esc_html( gettype( $values ) ) ) ); |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Gets the first metadata value for a given key. |
| 27 | * |
| 28 | * Example: |
| 29 | * |
| 30 | * Metadata: |
| 31 | * array( |
| 32 | * 'post_title' => array( 'The Name of the Wind' ), |
| 33 | * 'post_author' => array( 'Patrick Rothfuss', 'Betsy Wollheim' ) |
| 34 | * ) |
| 35 | * |
| 36 | * get_first_meta_value( 'post_author' ) returns 'Patrick Rothfuss'. |
| 37 | * |
| 38 | * @param string $key The metadata key. |
| 39 | * |
| 40 | * @return mixed The metadata value. |
| 41 | */ |
| 42 | public function get_first_meta_value( $key ) { |
| 43 | if ( ! array_key_exists( $key, $this->metadata ) ) { |
| 44 | return null; |
| 45 | } |
| 46 | |
| 47 | return $this->metadata[ $key ][0]; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Gets all the metadata sourced from the input document by the convert() method. |
| 52 | * The data format is: |
| 53 | * |
| 54 | * Array( |
| 55 | * 'post_title' => array( 'The Name of the Wind' ), |
| 56 | * 'post_author' => array( 'Patrick Rothfuss', 'Betsy Wollheim' ) |
| 57 | * ) |
| 58 | * |
| 59 | * Note each meta key may have multiple values. The consumer of this interface |
| 60 | * must account for this. |
| 61 | * |
| 62 | * @param array $options Optional parameters. |
| 63 | * @return array The metadata sourced from the input document. |
| 64 | */ |
| 65 | public function get_all_metadata( array $options = array() ) { |
| 66 | if ( isset( $options['first_value_only'] ) && $options['first_value_only'] ) { |
| 67 | $meta = array(); |
| 68 | foreach ( $this->metadata as $key => $values ) { |
| 69 | $meta[ $key ] = $values[0]; |
| 70 | } |
| 71 | |
| 72 | return $meta; |
| 73 | } |
| 74 | |
| 75 | return $this->metadata; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Gets the block markup generated by the convert() method. |
| 80 | * |
| 81 | * @return string The block markup. |
| 82 | */ |
| 83 | public function get_block_markup() { |
| 84 | return $this->block_markup; |
| 85 | } |
| 86 | } |
| 87 |