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

Patterns API: Use the requesting site's version to filter out incompatible patterns #536

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,23 @@ function register_post_type_data() {
),
)
);

register_post_meta(
POST_TYPE,
'wpop_wp_version',
array(
'type' => 'string',
'description' => 'The earliest WordPress version compatible with this pattern.',
'single' => true,
'sanitize_callback' => 'sanitize_text_field',
'auth_callback' => __NAMESPACE__ . '\can_edit_this_pattern',
'show_in_rest' => array(
'schema' => array(
'type' => 'string',
),
),
)
);
}

/**
Expand Down Expand Up @@ -610,6 +627,11 @@ function filter_patterns_collection_params( $query_params ) {
$query_params['orderby']['enum'][] = 'favorite_count';
}

$query_params['wp-version'] = array(
'description' => __( 'The version of the requesting site, used to filter out newer patterns.', 'wporg-patterns' ),
'type' => 'string',
);

return $query_params;
}

Expand Down Expand Up @@ -650,6 +672,31 @@ function filter_patterns_rest_query( $args, $request ) {
$args['meta_key'] = 'wporg-pattern-favorites';
}

// Use the passed-in version information to skip over any patterns that
// require newer block features.
// See https://github.com/WordPress/gutenberg/issues/45179.
$version = $request->get_param( 'wp-version' );
if ( $version && preg_match( '/^\d+\.\d+/', $version, $matches ) ) {
// $version is the full WP version, for example `6.0.2` or `6.2-alpha-54642-src`.
// Parse out just the major version section, `6.0` or `6.2`, respectively,
// so that the math comparison works.
$major_version = $matches[0];
$args['meta_query']['version'] = array(
// Fetch patterns with no version info, or only those with a lower
// or equal version.
'relation' => 'OR',
array(
'key' => 'wpop_wp_version',
'compare' => '<=',
'value' => $major_version,
),
array(
'key' => 'wpop_wp_version',
'compare' => 'NOT EXISTS',
),
);
}

return $args;
}

Expand Down