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

Add facilitator note #3052

Open
wants to merge 26 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
689dce7
Restore the first breadcrumb to 'Learn WordPress'
renintw Oct 14, 2024
b82ac05
Don't render separator bar when on mobile view
renintw Oct 14, 2024
6b59103
Add Lesson Facilitator Notes Block
renintw Dec 4, 2024
aceed81
Add editor script
renintw Dec 4, 2024
73b644e
Add front end rendering
renintw Dec 4, 2024
9457bf4
Fix rich text style
renintw Dec 5, 2024
2efb656
Add error handler
renintw Dec 5, 2024
1a189f3
Fix empty content after refreshing
renintw Dec 5, 2024
b3da315
Fix the error message when selecting 'Select a plan'
renintw Dec 5, 2024
5c655ca
Change to use ComboboxControl
renintw Dec 5, 2024
e0bc302
Show content in collapsible container
renintw Dec 5, 2024
6ec0274
Do nothing if no there's no search results
renintw Dec 5, 2024
1e2d4b0
Enhance RichText styles
renintw Dec 5, 2024
64335b8
Add lessonPlanTitle
renintw Dec 5, 2024
f2c40d9
Add success/failure handler for save
renintw Dec 5, 2024
66377c0
Add icon-chevron-down
renintw Dec 9, 2024
d0dc853
Add header mobile view
renintw Dec 9, 2024
7424cf2
Adjust the label position.
renintw Dec 10, 2024
afb7fad
Add mobile view styles for the label
renintw Dec 10, 2024
0f260b3
Remove unused styles
renintw Dec 10, 2024
9ec5620
Adjust label style for standalone lesson
renintw Dec 10, 2024
359594b
Refresh the page to open the link
renintw Dec 11, 2024
affb77e
Fix lesson title too long making layout go wrong
renintw Dec 11, 2024
ff05a67
Adjust label position for mobile, desktop, unenrolled
renintw Dec 11, 2024
55d8b14
Make label in standalone lesson always show up
renintw Dec 11, 2024
81bce2f
Update standalone style
renintw Dec 12, 2024
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 1 addition & 5 deletions wp-content/themes/pub/wporg-learn-2024/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require_once __DIR__ . '/src/learning-pathway-cards/index.php';
require_once __DIR__ . '/src/learning-pathway-header/index.php';
require_once __DIR__ . '/src/lesson-course-info/index.php';
require_once __DIR__ . '/src/lesson-facilitator-notes/index.php';
require_once __DIR__ . '/src/lesson-grid/index.php';
require_once __DIR__ . '/src/lesson-standalone/index.php';
require_once __DIR__ . '/src/search-results-context/index.php';
Expand Down Expand Up @@ -443,11 +444,6 @@ function get_learning_pathway_level_content( $learning_pathway ) {
* @return array The modified breadcrumbs.
*/
function set_site_breadcrumbs( $breadcrumbs ) {
if ( isset( $breadcrumbs[0] ) ) {
// Change the title of the first breadcrumb to 'Home'.
$breadcrumbs[0]['title'] = 'Home';
}

$post_id = get_the_ID();
$post_type = get_post_type();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,19 @@
<!-- /wp:group -->

<!-- wp:sensei-lms/course-theme-course-progress-bar /-->

<!-- wp:group {"className":"wporg-learn-lesson-header-mobile-view","style":{"spacing":{"blockGap":"16px"}},"layout":{"type":"flex","flexWrap":"nowrap"}} -->
<div class="wp-block-group wporg-learn-lesson-header-mobile-view">
<?php if ( $is_user_enrolled ) : ?>
<!-- wp:sensei-lms/course-theme-course-progress-counter {"fontSize":"small"} /-->
<?php endif; ?>

<!-- wp:group {"className":"wporg-learn-lesson-sidebar-toggle-wrapper","layout":{"type":"flex","flexWrap":"nowrap"}} -->
<div class="wp-block-group wporg-learn-lesson-sidebar-toggle-wrapper">
<!-- wp:sensei-lms/sidebar-toggle-button /-->
</div>
<!-- /wp:group -->
</div>
<!-- /wp:group -->
</div>
<!-- /wp:sensei-lms/ui -->
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

<!-- wp:wporg/site-breadcrumbs {"fontSize":"small","style":{"spacing":{"padding":{"top":"18px","bottom":"18px"}}}} /-->

<!-- wp:group {"style":{"spacing":{"blockGap":"12px"}},"layout":{"type":"flex","flexWrap":"nowrap"}} -->
<div class="wp-block-group">
<!-- wp:group {"className":"wp-block-sensei-lms-exit-lesson","style":{"spacing":{"blockGap":"12px"}},"layout":{"type":"flex","flexWrap":"nowrap"}} -->
<div class="wp-block-group wp-block-sensei-lms-exit-lesson">

<!-- wp:paragraph {"className":"is-style-short-text","fontSize":"small"} -->
<p class="is-style-short-text has-small-font-size"><a href="<?php echo esc_url( get_post_type_archive_link( 'lesson' ) ); ?>"><?php echo esc_html_e( 'Exit lesson', 'wporg-learn' ); ?></a></p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "wporg-learn/lesson-facilitator-notes",
"version": "0.1.0",
"title": "Lesson Facilitator Notes",
"category": "widgets",
"icon": "text",
"description": "Shows the facilitator notes for a lesson",
"usesContext": [],
"attributes": {
"lessonPlanId": {
"type": "number",
"default": null
},
"lessonPlanContent": {
"type": "string",
"default": ""
},
"lessonPlanTitle": {
"type": "string",
"default": ""
}
},
"textdomain": "wporg-learn",
"editorScript": "file:./index.js",
"viewScript": "file:./view.js",
"style": "file:./style-index.css"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/**
* WordPress dependencies
*/
import { registerBlockType } from '@wordpress/blocks';
import { RichText, useBlockProps } from '@wordpress/block-editor';
import { Button, ComboboxControl, Icon, Spinner } from '@wordpress/components';
import { check } from '@wordpress/icons';
import { useEffect, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import apiFetch from '@wordpress/api-fetch';
import { debounce } from 'lodash';

/**
* Internal dependencies
*/
import metadata from './block.json';
import './style.scss';

registerBlockType( metadata.name, {
edit: function Edit( { attributes, setAttributes } ) {
const { lessonPlanId, lessonPlanContent, lessonPlanTitle } = attributes;
const [ searchResults, setSearchResults ] = useState( [] );
const [ isExpanded, setIsExpanded ] = useState( false );
const [ isSaving, setIsSaving ] = useState( false );
const [ saveSuccess, setSaveSuccess ] = useState( null );
const [ errorMessage, setErrorMessage ] = useState( null );

useEffect( () => {
// Fetch the initial lesson plan options if lessonPlanId is set
if ( lessonPlanId ) {
apiFetch( {
path: `/wp/v2/lesson-plan/${ lessonPlanId }`,
} ).then( ( plan ) => {
setSearchResults( [
{
value: plan.id,
label: plan.title.rendered,
},
] );
} );
}
}, [] );

Check warning on line 42 in wp-content/themes/pub/wporg-learn-2024/src/lesson-facilitator-notes/index.js

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has a missing dependency: 'lessonPlanId'. Either include it or remove the dependency array

const fetchLessonPlanContent = ( id ) => {
apiFetch( { path: `/wp/v2/lesson-plan/${ id }` } ).then( ( plan ) => {
const cleanedContent = plan.content.rendered.replace( /\s+/g, ' ' ).trim();
setAttributes( {
lessonPlanContent: cleanedContent,
lessonPlanTitle: plan.title.rendered,
} );
} );
};

const fetchLessonPlans = debounce( ( searchTerm ) => {
apiFetch( {
path: `/wp/v2/lesson-plan?search=${ encodeURIComponent( searchTerm ) }&per_page=10`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a lot of trouble finding specific lesson plans in my local site - I was typing exact keywords in the lesson title, but nothing would show up. I think because this search is default WP search, so it's returning when the keywords match in title or content, but the combobox control only matches results on title.

So the API is limited to 10 items, and my search "block directory" returned more than 10 items, pushing the page I wanted out of the options. And nothing would show up when I typed, because "block directory" is not a match in any of the results I had:

  • Create a Basic Child Theme for Block Themes
  • WebP images in WordPress
  • Υποβολή Μοτίβων μπλοκ (Block Patterns) στον κατάλογο.
  • Πώς να χρησιμοποιήσετε και να συνεισφέρετε στον κατάλογο φωτογραφιών του WordPress αλλά και στο Openverse
  • Submitting Block Patterns to the Directory
  • Template Editor
  • How to Use and Contribute to the WordPress Photo Directory and Openverse
  • How to Build Low-Code Block Patterns
  • How to Create and Register a Block Pattern
  • Difference between Reusable Blocks, Block Pattern, Templates, Template Parts

Maybe increase the result number? Or see if you can adjust the search for this endpoint to only do title searches.

You could preload the ID/title pairs, and not use an API request for this at all. With 150 published lesson plans, it shouldn't be that slow. Or if you'd rather not do that on the server side, fire off an API request to get all the lesson plans when the block loads (instead of just if ( lessonPlanId )).

} ).then( ( plans ) => {
if ( plans.length === 0 ) {
return;
}
const options = plans.map( ( plan ) => ( {
value: plan.id,
label: plan.title.rendered,
} ) );
setSearchResults( options );
} );
}, 300 );

const saveLessonPlanContent = () => {
setIsSaving( true );
setSaveSuccess( null );
apiFetch( {
path: `/wp/v2/lesson-plan/${ lessonPlanId }`,
method: 'POST',
data: {
content: lessonPlanContent,
title: lessonPlanTitle,
},
} )
.then( () => {
setIsSaving( false );
setSaveSuccess( true );
setTimeout( () => setSaveSuccess( null ), 2000 );
} )
.catch( ( error ) => {
setIsSaving( false );
setSaveSuccess( false );
setErrorMessage( error.message || __( 'An error occurred while saving', 'wporg-learn' ) );
} );
};

const getSaveButton = () => {
if ( isSaving ) {
return <Spinner />;
}
if ( saveSuccess === true ) {
return <Icon icon={ check } />;
}
if ( saveSuccess === false ) {
return errorMessage;
}
return __( 'Save Changes', 'wporg-learn' );
};

const getSaveButtonClassName = () => {
if ( saveSuccess === true ) {
return 'is-success';
}
if ( saveSuccess === false ) {
return 'is-failure';
}
};

return (
<div { ...useBlockProps() }>
<ComboboxControl
label={ __( 'Select Lesson Plan', 'wporg-learn' ) }
value={ lessonPlanId || '' }
options={ searchResults }
onFilterValueChange={ ( inputValue ) => {
if ( inputValue ) {
fetchLessonPlans( inputValue );
}
} }
onChange={ ( newValue ) => {
setAttributes( { lessonPlanId: newValue } );
if ( newValue ) {
fetchLessonPlanContent( newValue );
}
} }
placeholder={ __( 'Search for a lesson plan…', 'wporg-learn' ) }
/>
{ lessonPlanId && (
<>
<RichText
tagName="h1"
value={ lessonPlanTitle }
onChange={ ( newTitle ) => setAttributes( { lessonPlanTitle: newTitle } ) }
/>
<Button variant="secondary" onClick={ () => setIsExpanded( ! isExpanded ) }>
{ isExpanded
? __( 'Collapse Content', 'wporg-learn' )
: __( 'Expand Content', 'wporg-learn' ) }
</Button>
<RichText
className={ isExpanded ? 'is-expanded' : 'is-collapsed' }
value={ lessonPlanContent }
onChange={ ( newContent ) => setAttributes( { lessonPlanContent: newContent } ) }
/>
</>
) }
{ lessonPlanId && (
<Button
variant="primary"
onClick={ saveLessonPlanContent }
disabled={ isSaving }
className={ getSaveButtonClassName() }
>
{ getSaveButton() }
</Button>
) }
</div>
);
},
save: () => null,
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Block Name: Lesson Facilitator Notes
* Description: Displays the facilitator notes for a lesson.
*
* @package wporg
*/

namespace WordPressdotorg\Theme\Learn_2024\Lesson_Facilitator_Notes;

use Error;
use function WPOrg_Learn\{get_build_path};

defined( 'WPINC' ) || die();

/**
* Actions and filters.
*/
add_action( 'init', __NAMESPACE__ . '\init' );

/**
* Add a script to control elements in the sensei lesson facilitator notes pattern.
*
* The dependencies are autogenerated in block.json, and can be read with
* `wp_json_file_decode` & `register_block_script_handle.
*/
function init() {
register_block_type(
dirname( dirname( __DIR__ ) ) . '/build/lesson-facilitator-notes',
array(
'render_callback' => __NAMESPACE__ . '\render',
)
);
}

/**
* Render the block content.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
*
* @throws Error If the script asset file is not readable.
* @return string Returns the block markup.
*/
function render( $attributes, $content, $block ) {
$script_asset_path = get_stylesheet_directory() . '/build/lesson-facilitator-notes/index.asset.php';
if ( ! is_readable( $script_asset_path ) ) {
throw new Error(
'You need to run `yarn start` or `yarn run build` for the "wporg-learn/lesson-facilitator-notes" block first.'
);
}

if ( empty( $attributes['lessonPlanId'] ) ) {
return;
}

$lesson_plan = get_post( $attributes['lessonPlanId'] );

if ( ! $lesson_plan ) {
return;
}

$lesson_plan_url = get_permalink( $lesson_plan->ID );
return sprintf(
// If changing classname here, you'd also need to update it in style.scss and view.js.
'<div class="wporg-learn-lesson-facilitator-notes-label"><a href="%s">%s</a></div>',
esc_url( $lesson_plan_url ),
esc_html__( 'Facilitator Note', 'wporg-learn' )
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.wp-block-wporg-learn-lesson-facilitator-notes {
.components-base-control__field {
margin-bottom: var(--wp--preset--spacing--10);
}

.block-editor-rich-text__editable {
white-space: unset !important;
overflow: scroll;
border: 1px solid var(--wp--preset--color--charcoal-5);
margin: var(--wp--preset--spacing--10) 0;
padding: var(--wp--preset--spacing--10);

&.is-expanded {
max-height: none;
}

&.is-collapsed {
max-height: 300px;
}
}

h1.block-editor-rich-text__editable {
padding: 0 var(--wp--preset--spacing--10);
}

button {
min-width: 118px;
justify-content: center;

&.is-success {
background-color: var(--wp--custom--color--green-50) !important;

&:hover {
opacity: 0.9;
}
}

&.is-failure {
background-color: var(--wp--preset--color--vivid-red) !important;

&:hover {
opacity: 0.9;
}
}
}

.components-spinner {
margin-top: 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
document.addEventListener( 'DOMContentLoaded', function () {
const facilitatorNotes = document.querySelector( '.wporg-learn-lesson-facilitator-notes-label' );
const exitCourse = document.querySelector( '.wp-block-sensei-lms-exit-course' );
const exitLesson = document.querySelector( '.wp-block-sensei-lms-exit-lesson' );
const sidebarToggle = document.querySelector( '.wporg-learn-lesson-sidebar-toggle-wrapper' );

// for desktop view
if ( facilitatorNotes && exitCourse ) {
exitCourse.insertAdjacentElement( 'beforebegin', facilitatorNotes.cloneNode( true ) );
}

// for mobile view
if ( facilitatorNotes && sidebarToggle ) {
sidebarToggle.insertAdjacentElement( 'beforebegin', facilitatorNotes.cloneNode( true ) );
}

// for standalone lesson
if ( facilitatorNotes && exitLesson ) {
exitLesson.insertAdjacentElement( 'beforebegin', facilitatorNotes.cloneNode( true ) );
}

facilitatorNotes.remove();
} );
Loading
Loading