Skip to content

Commit

Permalink
Add support for rich-text type
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnynews committed Apr 16, 2024
1 parent c297137 commit dd07963
Showing 1 changed file with 32 additions and 34 deletions.
66 changes: 32 additions & 34 deletions src/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,52 +94,50 @@ function handle_do_block( array $block, $post_id = 0 ) {
*/
function get_attribute( $attribute, $html, $post_id = 0 ) {
$value = null;
$dom = pQuery::parseStr( trim( $html ) );
$node = isset( $attribute['selector'] ) ? $dom->query( $attribute['selector'] ) : $dom->query();

if ( isset( $attribute['source'] ) ) {
if ( isset( $attribute['selector'] ) ) {
$dom = pQuery::parseStr( trim( $html ) );
if ( 'attribute' === $attribute['source'] ) {
$value = $dom->query( $attribute['selector'] )->attr( $attribute['attribute'] );
} elseif ( 'html' === $attribute['source'] ) {
$value = $dom->query( $attribute['selector'] )->html();
} elseif ( 'text' === $attribute['source'] ) {
$value = $dom->query( $attribute['selector'] )->text();
} elseif ( 'query' === $attribute['source'] && isset( $attribute['query'] ) ) {
$nodes = $dom->query( $attribute['selector'] )->getIterator();
$counter = 0;
foreach ( $nodes as $node ) {
foreach ( $attribute['query'] as $key => $current_attribute ) {
$current_value = get_attribute( $current_attribute, $node->toString(), $post_id );
if ( null !== $current_value ) {
$value[ $counter ][ $key ] = $current_value;
switch ( $attribute['source'] ) {
case 'attribute':
$value = $node->attr( $attribute['attribute'] );
break;
case 'html':
case 'rich-text':
$value = $node->html();
break;
case 'text':
$value = $node->text();
break;
case 'query':
if ( isset( $attribute['query'] ) ) {
$counter = 0;
$nodes = $node->getIterator();
foreach ( $nodes as $v_node ) {
foreach ( $attribute['query'] as $key => $current_attribute ) {
$current_value = get_attribute( $current_attribute, $v_node->toString(), $post_id );
if ( null !== $current_value ) {
$value[ $counter ][ $key ] = $current_value;
}
}
++$counter;
}
++$counter;
}
}
} else {
$dom = pQuery::parseStr( trim( $html ) );
$node = $dom->query();
if ( 'attribute' === $attribute['source'] ) {
$current_value = $node->attr( $attribute['attribute'] );
if ( null !== $current_value ) {
$value = $current_value;
break;
case 'meta':
if ( $post_id && isset( $attribute['meta'] ) ) {
$value = get_post_meta( $post_id, $attribute['meta'], true );
}
} elseif ( 'html' === $attribute['source'] ) {
$value = $node->html();
} elseif ( 'text' === $attribute['source'] ) {
$value = $node->text();
}
}

if ( $post_id && 'meta' === $attribute['source'] && isset( $attribute['meta'] ) ) {
$value = get_post_meta( $post_id, $attribute['meta'], true );
break;
}
}

// Assign default value if value is null and a default exists.
if ( is_null( $value ) && isset( $attribute['default'] ) ) {
$value = $attribute['default'];
}

// If attribute type is set and valid, sanitize value.
if ( isset( $attribute['type'] ) && rest_validate_value_from_schema( $value, $attribute ) ) {
$value = rest_sanitize_value_from_schema( $value, $attribute );
}
Expand Down

0 comments on commit dd07963

Please sign in to comment.