-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2024 Theme: Move course grid block to theme (#2578)
It is a presentation component which is tightly coupled to the theme. See #2573
- Loading branch information
1 parent
8858ef4
commit bb9d9bd
Showing
7 changed files
with
94 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
wp-content/themes/pub/wporg-learn-2024/src/course-grid/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "wporg-learn/course-grid", | ||
"title": "Learn Course Grid", | ||
"description": "A query loop block variation which displays a cards grid of courses, filterable by the 'featured' course post meta.", | ||
"textdomain": "wporg-learn", | ||
"editorScript": "file:./index.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
wp-content/themes/pub/wporg-learn-2024/src/course-grid/index.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
namespace WordPressdotorg\Theme\Learn_2024\Course_Grid; | ||
|
||
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_course_grid_assets' ); | ||
|
||
add_filter( 'pre_render_block', __NAMESPACE__ . '\modify_course_query', 10, 2 ); | ||
add_filter( 'rest_course_query', __NAMESPACE__ . '\modify_course_rest_query', 10, 2 ); | ||
|
||
/** | ||
* Enqueue course grid assets. | ||
* | ||
* @throws Error If the build files are not found. | ||
*/ | ||
function enqueue_course_grid_assets() { | ||
$script_asset_path = get_stylesheet_directory() . '/build/course-grid/index.asset.php'; | ||
if ( ! is_readable( $script_asset_path ) ) { | ||
throw new Error( | ||
'You need to run `npm start` or `npm run build` for the "wporg-learn/course-grid" block first.' | ||
); | ||
} | ||
|
||
$script_asset = require $script_asset_path; | ||
wp_enqueue_script( | ||
'wporg-learn-course-grid', | ||
get_stylesheet_directory_uri() . '/build/course-grid/index.js', | ||
$script_asset['dependencies'], | ||
$script_asset['version'], | ||
true | ||
); | ||
} | ||
|
||
/** | ||
* Modify the course query to add the featured course meta query if set. | ||
* | ||
* @param mixed $pre_render The pre-render value. | ||
* @param mixed $parsed_block The parsed block value. | ||
* @return mixed The modified course query. | ||
*/ | ||
function modify_course_query( $pre_render, $parsed_block ) { | ||
if ( isset( $parsed_block['attrs']['namespace'] ) && 'wporg-learn/course-grid' === $parsed_block['attrs']['namespace'] | ||
) { | ||
add_filter( | ||
'query_loop_block_query_vars', | ||
function( $query, $block ) use ( $parsed_block ) { | ||
if ( 'course' !== $query['post_type'] || ! isset( $parsed_block['attrs']['query']['courseFeatured'] ) ) { | ||
return $query; | ||
} | ||
|
||
$course_featured = $parsed_block['attrs']['query']['courseFeatured']; | ||
|
||
if ( true === $course_featured ) { | ||
$query['meta_key'] = '_course_featured'; | ||
$query['meta_value'] = 'featured'; | ||
} | ||
|
||
return $query; | ||
}, | ||
10, | ||
2 | ||
); | ||
} | ||
|
||
return $pre_render; | ||
} | ||
|
||
/** | ||
* Modify the course REST query to add the featured course meta query if set. | ||
* | ||
* @param array $args The query arguments. | ||
* @param WP_REST_Request $request The REST request object. | ||
* @return array The modified query arguments. | ||
*/ | ||
function modify_course_rest_query( $args, $request ) { | ||
$course_featured = $request->get_param( 'courseFeatured' ); | ||
|
||
if ( 'true' === $course_featured ) { | ||
$args['meta_query'][] = array( | ||
'key' => '_course_featured', | ||
'value' => 'featured', | ||
'compare' => '=', | ||
); | ||
} | ||
|
||
return $args; | ||
} |