Skip to content

Commit

Permalink
2024 Theme: Move course grid block to theme (#2578)
Browse files Browse the repository at this point in the history
It is a presentation component which is tightly coupled to the theme.

See #2573
  • Loading branch information
adamwoodnz authored Jun 30, 2024
1 parent 8858ef4 commit bb9d9bd
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 83 deletions.
24 changes: 0 additions & 24 deletions wp-content/plugins/wporg-learn/inc/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
*/
add_action( 'init', __NAMESPACE__ . '\register_types' );
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_block_style_assets' );
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_course_grid_assets' );
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_block_style_assets' );

/**
Expand Down Expand Up @@ -449,29 +448,6 @@ function enqueue_block_style_assets() {
);
}

/**
* Enqueue course grid assets.
*
* @throws Error If the build files are not found.
*/
function enqueue_course_grid_assets() {
$script_asset_path = get_build_path() . 'course-grid.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_build_url() . 'course-grid.js',
$script_asset['dependencies'],
$script_asset['version'],
true
);
}

/**
* Register the learning duration block.
*/
Expand Down
57 changes: 0 additions & 57 deletions wp-content/plugins/wporg-learn/inc/sensei.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
add_action( 'sensei_before_main_content', __NAMESPACE__ . '\theme_wrapper_start' );
add_action( 'sensei_after_main_content', __NAMESPACE__ . '\theme_wrapper_end' );
add_action( 'init', __NAMESPACE__ . '\wporg_correct_sensei_slugs', 9 );
add_filter( 'pre_render_block', __NAMESPACE__ . '\modify_course_query', 10, 2 );
add_filter( 'rest_course_query', __NAMESPACE__ . '\modify_course_rest_query', 10, 2 );

/**
* Slugs in Sensei are translatable, which won't work for our site and the language switcher.
Expand Down Expand Up @@ -260,58 +258,3 @@ function get_my_courses_page_url() {

return get_permalink( $page_id );
}

/**
* 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;
}
1 change: 0 additions & 1 deletion wp-content/plugins/wporg-learn/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const config = require( '@wordpress/scripts/config/webpack.config' );
*/
config.entry = {
'block-styles': './js/block-styles/index.js',
'course-grid': './js/course-grid/index.js',
'course-status': './js/course-status/src/index.js',
'duration-meta': './js/duration-meta/index.js',
'expiration-date': './js/expiration-date/index.js',
Expand Down
1 change: 1 addition & 0 deletions wp-content/themes/pub/wporg-learn-2024/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use function WPOrg_Learn\Sensei\{get_my_courses_page_url};

// Block files
require_once __DIR__ . '/src/course-grid/index.php';
require_once __DIR__ . '/src/learning-pathway-cards/index.php';
require_once __DIR__ . '/src/learning-pathway-header/index.php';
require_once __DIR__ . '/src/search-results-context/index.php';
Expand Down
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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ registerBlockVariation( 'core/query', {
},
description: __( 'Displays a cards grid of courses.', 'wporg-learn' ),
attributes: {
className: 'wporg-learn-course-grid',
className: 'wporg-learn-course-grid wporg-learn-card-grid',
namespace: VARIATION_NAME,
query: {
perPage: 6,
Expand Down
85 changes: 85 additions & 0 deletions wp-content/themes/pub/wporg-learn-2024/src/course-grid/index.php
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;
}

0 comments on commit bb9d9bd

Please sign in to comment.