From 8936a3f4947c0398cf0a8ef3280711605024ad0c Mon Sep 17 00:00:00 2001 From: SEO Themes Date: Tue, 8 Aug 2023 21:50:32 +0800 Subject: [PATCH 1/6] Add inherit option to Course Outline block and increase max number of Courses shown in dropdown select --- .changelogs/course-outline-block-1.yml | 3 ++ .changelogs/course-outline-block.yml | 4 ++ .../class.llms.shortcode.course.outline.php | 30 ++++++++++--- packages/components/src/post-select/index.js | 45 +++++++++++-------- 4 files changed, 59 insertions(+), 23 deletions(-) create mode 100644 .changelogs/course-outline-block-1.yml create mode 100644 .changelogs/course-outline-block.yml diff --git a/.changelogs/course-outline-block-1.yml b/.changelogs/course-outline-block-1.yml new file mode 100644 index 0000000000..5eb8e5b5c6 --- /dev/null +++ b/.changelogs/course-outline-block-1.yml @@ -0,0 +1,3 @@ +significance: patch +type: changed +entry: Increase maximum number of Courses shown in drop down select to 100. diff --git a/.changelogs/course-outline-block.yml b/.changelogs/course-outline-block.yml new file mode 100644 index 0000000000..209d5188b0 --- /dev/null +++ b/.changelogs/course-outline-block.yml @@ -0,0 +1,4 @@ +significance: patch +type: fixed +entry: Add `inherit from current course` option for Course Outline block when + editing Single Course template. diff --git a/includes/shortcodes/class.llms.shortcode.course.outline.php b/includes/shortcodes/class.llms.shortcode.course.outline.php index 7884afbcc4..2982f703a8 100644 --- a/includes/shortcodes/class.llms.shortcode.course.outline.php +++ b/includes/shortcodes/class.llms.shortcode.course.outline.php @@ -7,7 +7,7 @@ * @package LifterLMS/Shortcodes/Classes * * @since 3.5.1 - * @version 3.19.2 + * @version [version] */ defined( 'ABSPATH' ) || exit; @@ -84,13 +84,33 @@ protected function get_default_attributes() { * $atts & $content are both filtered before being passed to get_output() * output is filtered so the return of get_output() doesn't need its own filter * - * @return string - * @since 3.5.1 - * @version 3.19.2 + * @since 3.5.1 + * @since [version] Added fallback to render first course when none selected in Editor. + * + * @return string */ protected function get_output() { - $course = new LLMS_Course( $this->get_attribute( 'course_id' ) ); + $id = $this->get_attribute( 'course_id' ); + + if ( ! $id && is_singular( 'course' ) ) { + $id = get_the_ID(); + } + + // Show the first course when in Editor if none selected. + if ( ! $id && llms_is_editor_block_rendering() ) { + $courses = get_posts( array( + 'post_type' => 'course', + 'posts_per_page' => 1, + 'post_status' => 'publish', + ) ); + + if ( $courses ) { + $id = $courses[0]->ID; + } + } + + $course = new LLMS_Course( $id ); $student = llms_get_student(); $args = array( diff --git a/packages/components/src/post-select/index.js b/packages/components/src/post-select/index.js index a727aa08ab..d56d438943 100644 --- a/packages/components/src/post-select/index.js +++ b/packages/components/src/post-select/index.js @@ -8,9 +8,9 @@ export const llmsPostTypes = [ 'llms_quiz' ]; -export const getPostTypeName = ( slug, format = 'name' ) => { +export const getPostTypeName = ( slug = 'course', format = 'name' ) => { const name = slug?.replace( 'llms_', '' ); - const title = name.charAt( 0 ).toUpperCase() + name.slice( 1 ); + const title = name?.charAt( 0 )?.toUpperCase() + name?.slice( 1 ); return format === 'name' ? name : title; }; @@ -22,39 +22,39 @@ export const useLlmsPostType = () => { }; export const usePostOptions = ( postType = 'course' ) => { + const queryArgs = { + per_page: 100, + status: 'publish', + }; + const { posts, currentPostType } = useSelect( ( select ) => { return { - posts: select( 'core' ).getEntityRecords( 'postType', postType ), + posts: select( 'core' ).getEntityRecords( 'postType', postType, queryArgs ), currentPostType: select( 'core/editor' )?.getCurrentPostType(), }; }, [] ); - const postTypeName = getPostTypeName( postType ); - const options = []; - if ( ! llmsPostTypes.includes( currentPostType ) ) { + const isSingleCourseTemplate = useSelect( ( select ) => { + return select( 'core/edit-site' )?.getEditedPostId( 'template' )?.includes( 'single-course' ); + } ); + + const postTypeName = getPostTypeName( postType ); + + if ( ! llmsPostTypes.includes( currentPostType ) && ! isSingleCourseTemplate ) { options.push( { - label: __( 'Select course', 'lifterlms' ), + label: __( 'Select ', 'lifterlms' ) + postTypeName, value: 0, } ); } - if ( posts?.length ) { - posts.forEach( ( post ) => { - options.push( { - label: post.title.rendered + ' (ID: ' + post.id + ')', - value: post.id, - } ); - } ); - } - - if ( llmsPostTypes.includes( currentPostType ) ) { + if ( llmsPostTypes.includes( currentPostType ) || isSingleCourseTemplate ) { options.unshift( { label: sprintf( // Translators: %s = Post type name. __( 'Inherit from current %s', 'lifterlms' ), - getPostTypeName( currentPostType ) + postTypeName ), value: 0, } ); @@ -67,6 +67,15 @@ export const usePostOptions = ( postType = 'course' ) => { } ); } + if ( posts?.length ) { + posts.forEach( ( post ) => { + options.push( { + label: post.title.rendered + ' (ID: ' + post.id + ')', + value: post.id, + } ); + } ); + } + return options; }; From 837f1fc88a22f1b597362208c00a3cde0ce13a61 Mon Sep 17 00:00:00 2001 From: SEO Themes Date: Tue, 8 Aug 2023 21:54:09 +0800 Subject: [PATCH 2/6] Coding standards indentation --- .../class.llms.shortcode.course.outline.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/includes/shortcodes/class.llms.shortcode.course.outline.php b/includes/shortcodes/class.llms.shortcode.course.outline.php index 2982f703a8..d380a64f5d 100644 --- a/includes/shortcodes/class.llms.shortcode.course.outline.php +++ b/includes/shortcodes/class.llms.shortcode.course.outline.php @@ -99,11 +99,13 @@ protected function get_output() { // Show the first course when in Editor if none selected. if ( ! $id && llms_is_editor_block_rendering() ) { - $courses = get_posts( array( - 'post_type' => 'course', - 'posts_per_page' => 1, - 'post_status' => 'publish', - ) ); + $courses = get_posts( + array( + 'post_type' => 'course', + 'posts_per_page' => 1, + 'post_status' => 'publish', + ) + ); if ( $courses ) { $id = $courses[0]->ID; From 158dc4bed6be554c0ddf7c811a278d9a512ba8ac Mon Sep 17 00:00:00 2001 From: SEO Themes Date: Mon, 14 Aug 2023 12:49:55 +0800 Subject: [PATCH 3/6] Add fallback course for editor single template block render preview. --- .changelogs/course-outline-block-2.yml | 3 ++ ...abstract.llms.shortcode.course.element.php | 38 +++++++++++++------ .../class.llms.shortcode.course.outline.php | 23 +++++------ .../class.llms.shortcodes.blocks.php | 23 ++++++++++- 4 files changed, 61 insertions(+), 26 deletions(-) create mode 100644 .changelogs/course-outline-block-2.yml diff --git a/.changelogs/course-outline-block-2.yml b/.changelogs/course-outline-block-2.yml new file mode 100644 index 0000000000..43d62fa7cf --- /dev/null +++ b/.changelogs/course-outline-block-2.yml @@ -0,0 +1,3 @@ +significance: patch +type: fixed +entry: Add fallback course for editor single template block render preview. diff --git a/includes/abstracts/abstract.llms.shortcode.course.element.php b/includes/abstracts/abstract.llms.shortcode.course.element.php index 65793e75cf..98b42b0e8a 100644 --- a/includes/abstracts/abstract.llms.shortcode.course.element.php +++ b/includes/abstracts/abstract.llms.shortcode.course.element.php @@ -5,7 +5,7 @@ * @package LifterLMS/Abstracts/Classes * * @since 3.6.0 - * @version 3.6.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; @@ -46,28 +46,40 @@ protected function get_default_attributes() { * $atts & $content are both filtered before being passed to get_output() * output is filtered so the return of get_output() doesn't need its own filter * - * @return string - * @since 3.6.0 - * @version 3.6.0 + * @since 3.6.0 + * @version [version] Add fallback for editor block rendering. + * + * @return string */ protected function get_output() { // Get a reference to the current page where the shortcode is displayed. global $post; - $current_post = $post; - $course = get_post( $this->get_attribute( 'course_id' ) ); + $current_id = $post->ID ?? null; + + $id = $this->get_attribute( 'course_id' ); + + if ( ! $id ) { + $id = $current_id; + } + + if ( ! $id && llms_is_editor_block_rendering() ) { + $id = LLMS_Shortcodes_Blocks::get_placeholder_course_id(); + } + + $course = get_post( $id ); // We don't have a post object to proceed with. if ( ! $course ) { return ''; } - if ( 'course' !== $course->post_type ) { + if ( in_array( $course->post_type, array( 'lesson', 'llms_quiz' ) ) ) { // Get the parent. $parent = llms_get_post_parent_course( $course ); - // Post type doesn't have a parent so we can't display a syllabus. + // Post type doesn't have a parent, so we can't display a syllabus. if ( ! $parent ) { return ''; } @@ -77,18 +89,22 @@ protected function get_output() { } + if ( ! $current_id && llms_is_editor_block_rendering() ) { + $current_id = LLMS_Shortcodes_Blocks::get_placeholder_course_id(); + } + ob_start(); // Hack the global so our syllabus template works. - if ( $course->ID != $current_post->ID ) { + if ( $course->ID !== $current_id ) { $post = $course; } $this->template_function(); // Restore the global. - if ( $course->ID != $current_post->ID ) { - $post = $current_post; + if ( $course->ID !== $current_id ) { + $post = get_post( $current_id ); } return ob_get_clean(); diff --git a/includes/shortcodes/class.llms.shortcode.course.outline.php b/includes/shortcodes/class.llms.shortcode.course.outline.php index d380a64f5d..c43c2da585 100644 --- a/includes/shortcodes/class.llms.shortcode.course.outline.php +++ b/includes/shortcodes/class.llms.shortcode.course.outline.php @@ -85,31 +85,26 @@ protected function get_default_attributes() { * output is filtered so the return of get_output() doesn't need its own filter * * @since 3.5.1 - * @since [version] Added fallback to render first course when none selected in Editor. + * @version [version] Add fallback for editor block rendering. * * @return string */ protected function get_output() { + // Get a reference to the current page where the shortcode is displayed. + global $post; + + $current_id = $post->ID ?? null; + $id = $this->get_attribute( 'course_id' ); - if ( ! $id && is_singular( 'course' ) ) { - $id = get_the_ID(); + if ( ! $id ) { + $id = $current_id; } // Show the first course when in Editor if none selected. if ( ! $id && llms_is_editor_block_rendering() ) { - $courses = get_posts( - array( - 'post_type' => 'course', - 'posts_per_page' => 1, - 'post_status' => 'publish', - ) - ); - - if ( $courses ) { - $id = $courses[0]->ID; - } + $id = LLMS_Shortcodes_Blocks::get_placeholder_course_id(); } $course = new LLMS_Course( $id ); diff --git a/includes/shortcodes/class.llms.shortcodes.blocks.php b/includes/shortcodes/class.llms.shortcodes.blocks.php index a5b25d463e..885131db61 100644 --- a/includes/shortcodes/class.llms.shortcodes.blocks.php +++ b/includes/shortcodes/class.llms.shortcodes.blocks.php @@ -5,7 +5,7 @@ * @package LifterLMS/Classes/Shortcodes * * @since 7.2.0 - * @version 7.2.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; @@ -258,6 +258,27 @@ public function render_block( array $attributes, string $content, WP_Block $bloc trim( $html ) ); } + + /** + * Returns ID of first published course. + * + * @since [version] + * + * @return int + */ + public static function get_placeholder_course_id(): int { + + $courses = get_posts( + array( + 'post_type' => 'course', + 'posts_per_page' => 1, + 'post_status' => 'publish', + ) + ); + + return $courses[0]->ID ?? 0; + + } } return LLMS_Shortcodes_Blocks::instance(); From 326df902eaa88ca848ed7cc2adb6ec1de3719bd9 Mon Sep 17 00:00:00 2001 From: SEO Themes Date: Mon, 14 Aug 2023 12:51:14 +0800 Subject: [PATCH 4/6] Update since tag --- includes/shortcodes/class.llms.shortcode.course.outline.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/shortcodes/class.llms.shortcode.course.outline.php b/includes/shortcodes/class.llms.shortcode.course.outline.php index c43c2da585..70d362c3f2 100644 --- a/includes/shortcodes/class.llms.shortcode.course.outline.php +++ b/includes/shortcodes/class.llms.shortcode.course.outline.php @@ -85,7 +85,7 @@ protected function get_default_attributes() { * output is filtered so the return of get_output() doesn't need its own filter * * @since 3.5.1 - * @version [version] Add fallback for editor block rendering. + * @since [version] Add fallback for editor block rendering. * * @return string */ From 894f7c9ff60348effc237adb1ca3a79e1d6a2661 Mon Sep 17 00:00:00 2001 From: Rocco Aliberti Date: Thu, 26 Oct 2023 09:52:03 +0200 Subject: [PATCH 5/6] Apply suggestions from code review --- includes/abstracts/abstract.llms.shortcode.course.element.php | 2 +- includes/shortcodes/class.llms.shortcode.course.outline.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/abstracts/abstract.llms.shortcode.course.element.php b/includes/abstracts/abstract.llms.shortcode.course.element.php index 98b42b0e8a..3bec1a2bef 100644 --- a/includes/abstracts/abstract.llms.shortcode.course.element.php +++ b/includes/abstracts/abstract.llms.shortcode.course.element.php @@ -47,7 +47,7 @@ protected function get_default_attributes() { * output is filtered so the return of get_output() doesn't need its own filter * * @since 3.6.0 - * @version [version] Add fallback for editor block rendering. + * @since [version] Add fallback for editor block rendering. * * @return string */ diff --git a/includes/shortcodes/class.llms.shortcode.course.outline.php b/includes/shortcodes/class.llms.shortcode.course.outline.php index 70d362c3f2..1b816e6792 100644 --- a/includes/shortcodes/class.llms.shortcode.course.outline.php +++ b/includes/shortcodes/class.llms.shortcode.course.outline.php @@ -85,6 +85,7 @@ protected function get_default_attributes() { * output is filtered so the return of get_output() doesn't need its own filter * * @since 3.5.1 + * @since 3.19.2 Unknown. * @since [version] Add fallback for editor block rendering. * * @return string From 07b78312e3496c2f9ff844bc5c7f2a497cff3a5d Mon Sep 17 00:00:00 2001 From: Rocco Aliberti Date: Thu, 26 Oct 2023 09:52:14 +0200 Subject: [PATCH 6/6] Update includes/abstracts/abstract.llms.shortcode.course.element.php --- includes/abstracts/abstract.llms.shortcode.course.element.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/abstracts/abstract.llms.shortcode.course.element.php b/includes/abstracts/abstract.llms.shortcode.course.element.php index 3bec1a2bef..34568a6882 100644 --- a/includes/abstracts/abstract.llms.shortcode.course.element.php +++ b/includes/abstracts/abstract.llms.shortcode.course.element.php @@ -75,7 +75,7 @@ protected function get_output() { return ''; } - if ( in_array( $course->post_type, array( 'lesson', 'llms_quiz' ) ) ) { + if ( in_array( $course->post_type, array( 'lesson', 'llms_quiz' ), true ) ) { // Get the parent. $parent = llms_get_post_parent_course( $course );