Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve missing data fallback #258

Merged
merged 10 commits into from
Jan 3, 2025
44 changes: 38 additions & 6 deletions inc/Editor/DataBinding/BlockBindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,19 @@ public static function execute_query( array $block_context, string $operation_na
}
}

public static function get_value( array $source_args, WP_Block|array $block ): string {
public static function get_value( array $source_args, WP_Block|array $block ): ?string {
// We may be passed a block instance (by core block bindings) or a block
// array (by our hooks into the Block Data API).
if ( $block instanceof WP_Block ) {
$fallback_content = $block->attributes['content'] ?? '';
$block_context = $block->context[ self::$context_name ] ?? [];
$block_attributes = $block->attributes;
} else {
$fallback_content = $block['attributes']['content'] ?? '';
$block_context = $block['context'][ self::$context_name ] ?? [];
$block_attributes = $block['attributes'];
}

$fallback_content = self::get_block_fallback_content( $source_args, $block_context, $block_attributes );

// Fallback to the content if we don't have the expected context.
if ( ! isset( $block_context['blockName'] ) || ! isset( $block_context['queryInput'] ) ) {
self::log_error( sprintf( 'Missing block context for block binding %s', self::$context_name ), 'unknown' );
Expand All @@ -212,6 +214,29 @@ public static function get_value( array $source_args, WP_Block|array $block ): s
return $value;
}

private static function get_block_fallback_content( array $source_args, array $block_context, array $block_attributes ): ?string {
// Returning null from get_value() cancels the binding and allows the default saved content to show.
$fallback_content = null;

$source_field = $source_args['field'] ?? null;
if ( null === $source_field ) {
return $fallback_content;
}

$result_index = $source_args['index'] ?? 0;
$result = $block_context['results'][ $result_index ] ?? null;

if ( isset( $result[ $source_field ] ) ) {
$fallback_content = $result[ $source_field ];
}

if ( null === $fallback_content ) {
$fallback_content = $block_attributes['content'] ?? null;
}

return $fallback_content;
}

public static function get_remote_value( array $block_context, array $source_args ): string|null {
$block_name = $block_context['blockName'];
$field_name = $source_args['field'];
Expand Down Expand Up @@ -256,9 +281,16 @@ public static function loop_block_render_callback( array $attributes, string $co
$loop_template_content = $block->parsed_block['innerContent'];
$query_results = self::execute_query( $block_context, 'loop' );

if ( ! isset( $query_results['results'] ) ) {
if ( isset( $query_results['results'] ) ) {
$results = $query_results['results'];
} else {
self::log_error( 'Cannot load results for data loop', $block->name, 'loop' );
return $content;

if ( isset( $block_context['results'] ) ) {
$results = $block_context['results'];
} else {
return $content;
}
}

$block->parsed_block['innerBlocks'] = [];
Expand All @@ -267,7 +299,7 @@ public static function loop_block_render_callback( array $attributes, string $co
// Loop through the query results and make a copy of the template for each
// result, updating the bindings with the result index. This will be used
// by the binding source to render the correct result.
foreach ( array_keys( $query_results['results'] ) as $index ) {
foreach ( array_keys( $results ) as $index ) {

// Loop over the inner blocks of the template and update the bindings to
// include the current index.
Expand Down
2 changes: 1 addition & 1 deletion inc/Integrations/SalesforceB2C/Auth/SalesforceB2CAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static function get_token_using_refresh_token(
string $client_secret,
string $endpoint,
string $organization_id,
): ?string {
): string|WP_Error {
$client_auth_url = sprintf( '%s/shopper/auth/v1/organizations/%s/oauth2/token', $endpoint, $organization_id );

// Even though we're using a refresh token, authentication is still required to receive a new secret
Expand Down
Loading