← All changes
|
inc/Gutenberg/Blocks/Courses/CourseItemTemplateBlock.php
+97
-97
4.3.9
→
4.4.8
View file →
| @@ -1,97 +1,97 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace LearnPress\Gutenberg\Blocks\Courses; | |
| 4 | - | |
| 5 | -use LearnPress\Gutenberg\Blocks\AbstractBlockType; | |
| 6 | -use LearnPress\Helpers\Template; | |
| 7 | -use LearnPress\Models\CourseModel; | |
| 8 | -use LP_Debug; | |
| 9 | -use Throwable; | |
| 10 | -use WP_Block; | |
| 11 | - | |
| 12 | -/** | |
| 13 | - * Class SingleCourseBlock | |
| 14 | - * | |
| 15 | - * Handle register, render block template | |
| 16 | - */ | |
| 17 | -class CourseItemTemplateBlock extends AbstractBlockType { | |
| 18 | - public $block_name = 'course-item-template'; | |
| 19 | - | |
| 20 | - public function get_attributes() { | |
| 21 | - return [ | |
| 22 | - 'columns' => [ | |
| 23 | - 'type' => 'number', | |
| 24 | - 'default' => 3, | |
| 25 | - ], | |
| 26 | - ]; | |
| 27 | - } | |
| 28 | - | |
| 29 | - public function get_ancestor() { | |
| 30 | - return [ 'learnpress/list-courses' ]; | |
| 31 | - } | |
| 32 | - | |
| 33 | - /** | |
| 34 | - * Render content of block tag | |
| 35 | - * | |
| 36 | - * @param array $attributes | Attributes of block tag. | |
| 37 | - * | |
| 38 | - * @return false|string | |
| 39 | - */ | |
| 40 | - public function render_content_block_template( array $attributes, $content, $block ): string { | |
| 41 | - $html = ''; | |
| 42 | - | |
| 43 | - try { | |
| 44 | - $layout = sanitize_text_field( $attributes['layout'] ?? 'list' ); | |
| 45 | - $allowed_layout = [ 'list', 'grid' ]; | |
| 46 | - $layout = in_array( $layout, $allowed_layout, true ) ? $layout : 'list'; | |
| 47 | - $columns = absint( $attributes['columns'] ?? 3 ); | |
| 48 | - $columns = min( max( $columns, 0 ), 20 ); | |
| 49 | - | |
| 50 | - $wrapper_attributes = 'learn-press-courses lp-list-courses-no-css wp-block-learn-press-courses'; | |
| 51 | - | |
| 52 | - if ( $layout == 'grid' ) { | |
| 53 | - $wrapper_attributes .= ' grid-template-columns'; | |
| 54 | - } | |
| 55 | - | |
| 56 | - $courses = $block->context['courses'] ?? []; | |
| 57 | - $html_pagination = $block->context['pagination'] ?? ''; | |
| 58 | - $settings = $block->context['settings'] ?? []; | |
| 59 | - | |
| 60 | - if ( empty( $courses ) ) { | |
| 61 | - $html = Template::print_message( __( 'No courses found', 'learnpress' ), 'info', false ); | |
| 62 | - return $html; | |
| 63 | - } | |
| 64 | - | |
| 65 | - foreach ( $courses as $course ) { | |
| 66 | - $courseModel = CourseModel::find( $course->ID, true ); | |
| 67 | - | |
| 68 | - $filter_block_context = static function ( $context ) use ( $courseModel, $settings ) { | |
| 69 | - $context['courseModel'] = $courseModel; | |
| 70 | - $context['settings'] = $settings; | |
| 71 | - return $context; | |
| 72 | - }; | |
| 73 | - | |
| 74 | - // Add filter with priority 1 so other filters have access to these values | |
| 75 | - add_filter( 'render_block_context', $filter_block_context, 1 ); | |
| 76 | - $block_render = new WP_Block( $block->parsed_block ); | |
| 77 | - $block_content = $block_render->render( [ 'dynamic' => false ] ); | |
| 78 | - remove_filter( 'render_block_context', $filter_block_context, 1 ); | |
| 79 | - | |
| 80 | - $html .= '<li class="course">' . $block_content . '</li>'; | |
| 81 | - } | |
| 82 | - | |
| 83 | - return sprintf( | |
| 84 | - '<ul class="%1$s" data-layout="%2$s" style="--columns: %3$d;">%4$s</ul>%5$s', | |
| 85 | - $wrapper_attributes, | |
| 86 | - $layout, | |
| 87 | - $columns, | |
| 88 | - $html, | |
| 89 | - $html_pagination | |
| 90 | - ); | |
| 91 | - } catch ( Throwable $e ) { | |
| 92 | - LP_Debug::error_log( $e ); | |
| 93 | - } | |
| 94 | - | |
| 95 | - return $html; | |
| 96 | - } | |
| 97 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace LearnPress\Gutenberg\Blocks\Courses; | |
| 4 | + | |
| 5 | +use LearnPress\Gutenberg\Blocks\AbstractBlockType; | |
| 6 | +use LearnPress\Helpers\Template; | |
| 7 | +use LearnPress\Models\CourseModel; | |
| 8 | +use LP_Debug; | |
| 9 | +use Throwable; | |
| 10 | +use WP_Block; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * Class SingleCourseBlock | |
| 14 | + * | |
| 15 | + * Handle register, render block template | |
| 16 | + */ | |
| 17 | +class CourseItemTemplateBlock extends AbstractBlockType { | |
| 18 | + public $block_name = 'course-item-template'; | |
| 19 | + | |
| 20 | + public function get_attributes() { | |
| 21 | + return [ | |
| 22 | + 'columns' => [ | |
| 23 | + 'type' => 'number', | |
| 24 | + 'default' => 3, | |
| 25 | + ], | |
| 26 | + ]; | |
| 27 | + } | |
| 28 | + | |
| 29 | + public function get_ancestor() { | |
| 30 | + return [ 'learnpress/list-courses' ]; | |
| 31 | + } | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Render content of block tag | |
| 35 | + * | |
| 36 | + * @param array $attributes | Attributes of block tag. | |
| 37 | + * | |
| 38 | + * @return false|string | |
| 39 | + */ | |
| 40 | + public function render_content_block_template( array $attributes, $content, $block ): string { | |
| 41 | + $html = ''; | |
| 42 | + | |
| 43 | + try { | |
| 44 | + $layout = sanitize_text_field( $attributes['layout'] ?? 'list' ); | |
| 45 | + $allowed_layout = [ 'list', 'grid' ]; | |
| 46 | + $layout = in_array( $layout, $allowed_layout, true ) ? $layout : 'list'; | |
| 47 | + $columns = absint( $attributes['columns'] ?? 3 ); | |
| 48 | + $columns = min( max( $columns, 0 ), 20 ); | |
| 49 | + | |
| 50 | + $wrapper_attributes = 'learn-press-courses lp-list-courses-no-css wp-block-learn-press-courses'; | |
| 51 | + | |
| 52 | + if ( $layout == 'grid' ) { | |
| 53 | + $wrapper_attributes .= ' grid-template-columns'; | |
| 54 | + } | |
| 55 | + | |
| 56 | + $courses = $block->context['courses'] ?? []; | |
| 57 | + $html_pagination = $block->context['pagination'] ?? ''; | |
| 58 | + $settings = $block->context['settings'] ?? []; | |
| 59 | + | |
| 60 | + if ( empty( $courses ) ) { | |
| 61 | + $html = Template::print_message( __( 'No courses found', 'learnpress' ), 'info', false ); | |
| 62 | + return $html; | |
| 63 | + } | |
| 64 | + | |
| 65 | + foreach ( $courses as $course ) { | |
| 66 | + $courseModel = CourseModel::find( $course->ID, true ); | |
| 67 | + | |
| 68 | + $filter_block_context = static function ( $context ) use ( $courseModel, $settings ) { | |
| 69 | + $context['courseModel'] = $courseModel; | |
| 70 | + $context['settings'] = $settings; | |
| 71 | + return $context; | |
| 72 | + }; | |
| 73 | + | |
| 74 | + // Add filter with priority 1 so other filters have access to these values | |
| 75 | + add_filter( 'render_block_context', $filter_block_context, 1 ); | |
| 76 | + $block_render = new WP_Block( $block->parsed_block ); | |
| 77 | + $block_content = $block_render->render( [ 'dynamic' => false ] ); | |
| 78 | + remove_filter( 'render_block_context', $filter_block_context, 1 ); | |
| 79 | + | |
| 80 | + $html .= '<li class="course">' . $block_content . '</li>'; | |
| 81 | + } | |
| 82 | + | |
| 83 | + return sprintf( | |
| 84 | + '<ul class="%1$s" data-layout="%2$s" style="--columns: %3$d;">%4$s</ul>%5$s', | |
| 85 | + $wrapper_attributes, | |
| 86 | + $layout, | |
| 87 | + $columns, | |
| 88 | + $html, | |
| 89 | + $html_pagination | |
| 90 | + ); | |
| 91 | + } catch ( Throwable $e ) { | |
| 92 | + LP_Debug::error_log( $e ); | |
| 93 | + } | |
| 94 | + | |
| 95 | + return $html; | |
| 96 | + } | |
| 97 | +} | |