Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions assets/css/onboard.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@
display: flex;
align-items: center;
}

.prpl-onboarding-error {
margin: 0 0 1rem;
padding: 0.75rem 1rem;
color: var(--prpl-color-alert-error-text);
background: var(--prpl-background-alert-error);
border: 1px solid var(--prpl-color-border-alert-error);
border-radius: var(--prpl-radius-paper);
}
9 changes: 4 additions & 5 deletions assets/js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,9 @@ const PrplLessonItemsHTML = () => {
if ( ! editor || typeof editor.getEditedPostAttribute !== 'function' ) {
return '';
}
const meta = editor.getEditedPostAttribute( 'meta' );
return meta ? meta.progress_planner_page_todos : '';
return (
editor.getEditedPostAttribute( 'progress_planner_page_todos' ) || ''
);
}, [] );
const pageTodos = pageTodosMeta || '';

Expand Down Expand Up @@ -596,9 +597,7 @@ const PrplCheckListItem = ( item, pageTodos ) =>
typeof editorDispatch.editPost === 'function'
) {
editorDispatch.editPost( {
meta: {
progress_planner_page_todos: toDos.join( ',' ),
},
progress_planner_page_todos: toDos.join( ',' ),
} );
}
}
Expand Down
52 changes: 44 additions & 8 deletions assets/js/onboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* @param {string} licenseKey The license key.
*/
const progressPlannerSaveLicenseKey = ( licenseKey ) => {
console.log( 'License key: ' + licenseKey );
return progressPlannerAjaxRequest( {
url: progressPlanner.ajaxUrl,
data: {
Expand All @@ -24,6 +23,34 @@ const progressPlannerSaveLicenseKey = ( licenseKey ) => {
} );
};

/**
* Show an onboarding error and let the user submit the form again.
*
* @param {Object} response The server response, if any.
*/
const progressPlannerShowOnboardError = ( response ) => {
console.warn( response );

const errorElement = document.getElementById( 'prpl-onboarding-error' );
if ( errorElement ) {
// textContent: the message comes from the remote server.
errorElement.textContent =
response && 'string' === typeof response.message && response.message
? response.message
: progressPlanner.onboardError;
errorElement.hidden = false;
}

document
.querySelectorAll( '#prpl-onboarding-form input[type="submit"]' )
.forEach( ( input ) => {
input.disabled = false;
} );
document
.querySelectorAll( '#prpl-onboarding-submit-wrapper .prpl-spinner' )
.forEach( ( spinner ) => spinner.remove() );
};

/**
* Make the AJAX request.
*
Expand All @@ -35,15 +62,17 @@ const progressPlannerAjaxAPIRequest = ( data ) => {
data,
} )
.then( ( response ) => {
if ( ! response || ! response.license_key ) {
progressPlannerShowOnboardError( response );
return;
}
// Make a local request to save the response data.
progressPlannerSaveLicenseKey( response.license_key ).then( () => {
// Refresh the page.
window.location.reload();
} );
} )
.catch( ( error ) => {
console.warn( error );
} );
.catch( progressPlannerShowOnboardError );
};

/**
Expand All @@ -58,15 +87,19 @@ const progressPlannerOnboardCall = ( data ) => {
progressPlannerAjaxRequest( {
url: progressPlanner.onboardNonceURL,
data,
} ).then( ( response ) => {
if ( 'ok' === response.status ) {
} )
.then( ( response ) => {
if ( ! response || 'ok' !== response.status ) {
progressPlannerShowOnboardError( response );
return;
}
// Add the nonce to our data object.
data.nonce = response.nonce;

// Make the request to the API.
progressPlannerAjaxAPIRequest( data );
}
} );
} )
.catch( progressPlannerShowOnboardError );
};

if ( document.getElementById( 'prpl-onboarding-form' ) ) {
Expand Down Expand Up @@ -133,6 +166,9 @@ if ( document.getElementById( 'prpl-onboarding-form' ) ) {
return;
}

// Hide an error from a previous attempt.
document.getElementById( 'prpl-onboarding-error' ).hidden = true;

// Disable all (both buttons) submit buttons.
document
.querySelectorAll(
Expand Down
1 change: 1 addition & 0 deletions classes/admin/class-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public function enqueue_scripts() {
'onboardAPIUrl' => \progress_planner()->get_utils__onboard()->get_remote_url( 'onboard' ),
'ajaxUrl' => \admin_url( 'admin-ajax.php' ),
'nonce' => \wp_create_nonce( 'progress_planner' ),
'onboardError' => \__( 'We could not connect your site. Please try again, or contact support if this keeps happening.', 'progress-planner' ),
],
];

Expand Down
3 changes: 1 addition & 2 deletions classes/admin/widgets/class-challenge.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ public function get_cache_key() {
public function get_remote_api_url() {
return \add_query_arg(
[
'license_key' => \progress_planner()->get_license_key(),
'site' => \get_site_url(),
'site' => \get_site_url(),
],
\progress_planner()->get_remote_server_root_url() . '/wp-json/progress-planner-saas/v1/challenges'
);
Expand Down
6 changes: 4 additions & 2 deletions classes/admin/widgets/class-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public function get_id() {
* @return string
*/
public function get_range() {
return $this->get_sanitized_get( 'range', '-6 months' );
$range = $this->get_sanitized_get( 'range', '-6 months' );
return \in_array( $range, [ '-3 months', '-6 months', '-12 months', '-18 months', '-24 months' ], true ) ? $range : '-6 months';
}

/**
Expand All @@ -71,7 +72,8 @@ public function get_range() {
* @return string
*/
public function get_frequency() {
return $this->get_sanitized_get( 'frequency', 'monthly' );
$frequency = $this->get_sanitized_get( 'frequency', 'monthly' );
return \in_array( $frequency, [ 'weekly', 'monthly' ], true ) ? $frequency : 'monthly';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion classes/class-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function init() {
// `current_user_can()` is only reached on a branded, unlicensed site.
if ( ! \wp_doing_cron()
&& ! $this->get_license_key()
&& 0 !== (int) \progress_planner()->get_ui__branding()->get_branding_id()
&& 0 !== (int) \progress_planner()->get_ui__branding()->get_branding_id( false )
&& \current_user_can( 'manage_options' )
) {
$prpl_license_key = \progress_planner()->get_utils__onboard()->make_remote_onboarding_request();
Expand Down
111 changes: 97 additions & 14 deletions classes/class-lessons.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
*/
class Lessons {

/**
* Lesson fields that contain HTML; every other string field is plain text.
*/
private const HTML_FIELDS = [ 'text', 'todo_description' ];

/**
* Version of the sanitized lessons cache. Bump it whenever `sanitize_items()` changes.
*/
private const CACHE_VERSION = 2;

/**
* The ID for this API object.
*
Expand Down Expand Up @@ -53,18 +63,10 @@ public function get_items() {
* - Additional fields as provided by remote API
*/
public function get_remote_api_items() {
$url = \progress_planner()->get_remote_server_root_url() . '/wp-json/progress-planner-saas/v1/lessons';
$url = \add_query_arg(
[
'site' => \get_site_url(),
'license_key' => \progress_planner()->get_license_key(),
'locale' => \apply_filters( 'prpl_lesson_locale', \get_locale() ),
],
$url
);

$cache_key = \md5( $url );
$url = $this->get_remote_api_url();
$cache_key = $this->get_cache_key();

// Cached lessons were sanitized before they were stored (see CACHE_VERSION).
$cached = \progress_planner()->get_utils__cache()->get( $cache_key );
if ( \is_array( $cached ) ) {
return $cached;
Expand All @@ -91,10 +93,91 @@ public function get_remote_api_items() {
return [];
}

// Cache successful response for one week.
\progress_planner()->get_utils__cache()->set( $cache_key, $json, WEEK_IN_SECONDS );
// Sanitize once, then cache the successful response for one week.
$items = $this->sanitize_items( $json );
\progress_planner()->get_utils__cache()->set( $cache_key, $items, WEEK_IN_SECONDS );

return $items;
}

/**
* Get the URL of the remote lessons endpoint for this site and locale.
*
* @return string
*/
public function get_remote_api_url() {
return \add_query_arg(
[
'site' => \get_site_url(),
'locale' => \apply_filters( 'prpl_lesson_locale', \get_locale() ),
],
\progress_planner()->get_remote_server_root_url() . '/wp-json/progress-planner-saas/v1/lessons'
);
}

return $json;
/**
* Get the cache key for the lessons.
*
* The key includes CACHE_VERSION, so entries cached before sanitizing (or before
* a sanitizer change) are never read and are fetched again once.
*
* @return string
*/
public function get_cache_key() {
return \md5( $this->get_remote_api_url() . '|v' . self::CACHE_VERSION );
}

/**
* Sanitize remote lesson content before it is cached.
*
* Only fields that carry markup keep (filtered) HTML. Everything else is plain
* text: running it through kses would HTML-encode `&` in names and IDs.
*
* @param array $items Lesson data.
* @return array
*/
public function sanitize_items( array $items ): array {
foreach ( $items as $key => $value ) {
if ( \is_array( $value ) ) {
$items[ $key ] = $this->sanitize_items( $value );
} elseif ( \is_string( $value ) ) {
if ( 'video' === $key ) {
$items[ $key ] = $this->sanitize_video( $value );
} elseif ( \in_array( $key, self::HTML_FIELDS, true ) ) {
$items[ $key ] = \wp_kses_post( $value );
} else {
$items[ $key ] = \sanitize_text_field( $value );
}
}
}
return $items;
}

/**
* Allow video embeds only from known video players, without active attributes.
*
* @param string $html Video markup.
* @return string
*/
public function sanitize_video( string $html ): string {
$allowed = \wp_kses_allowed_html( 'post' );
// `style` is filtered by kses (safecss); `allow` and `loading` are needed by the Cloudflare Stream embed.
$allowed['iframe'] = \array_fill_keys( [ 'src', 'title', 'width', 'height', 'allowfullscreen', 'frameborder', 'style', 'allow', 'loading' ], true );
$html = \wp_kses( $html, $allowed );
$tags = new \WP_HTML_Tag_Processor( $html );
while ( $tags->next_tag( 'iframe' ) ) {
$src = $tags->get_attribute( 'src' );
$host = \is_string( $src ) ? \strtolower( (string) \wp_parse_url( $src, PHP_URL_HOST ) ) : '';
if ( ! \is_string( $src ) || 'https' !== \wp_parse_url( $src, PHP_URL_SCHEME )
|| ( ! \in_array( $host, [ 'www.youtube.com', 'www.youtube-nocookie.com', 'player.vimeo.com', 'iframe.mediadelivery.net' ], true )
// Lesson videos are served from per-account Cloudflare Stream subdomains.
&& ! \str_ends_with( $host, '.cloudflarestream.com' ) )
) {
$tags->remove_attribute( 'src' );
}
$tags->set_attribute( 'sandbox', 'allow-scripts allow-same-origin allow-presentation' );
}
return $tags->get_updated_html();
}

/**
Expand Down
52 changes: 48 additions & 4 deletions classes/class-page-todos.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Page_Todos {
*/
public function __construct() {
\add_action( 'init', [ $this, 'init' ] );
\add_action( 'rest_api_init', [ $this, 'register_rest_fields' ] );
}

/**
Expand All @@ -28,22 +29,65 @@ public function init() {
// Add the post-metas to all post-types.
$post_types = \get_post_types( [ 'public' => true ], 'objects' );
foreach ( $post_types as $post_type ) {
if ( ! \post_type_supports( $post_type->name, 'custom-fields' ) ) {
\add_post_type_support( $post_type->name, 'custom-fields' );
}
\register_post_meta(
$post_type->name,
'progress_planner_page_todos',
[
'type' => 'string',
'single' => true,
'show_in_rest' => true,
'show_in_rest' => false,
'sanitize_callback' => [ $this, 'sanitize_post_meta_progress_planner_page_todos' ],
]
);
}
}

/**
* Expose to-dos only in editable responses without enabling unrelated custom fields.
*
* @return void
*/
public function register_rest_fields() {
\register_rest_field(
\array_keys( \get_post_types( [ 'public' => true ] ) ),
'progress_planner_page_todos',
[
'schema' => [
'type' => 'string',
'context' => [ 'edit' ],
],
'get_callback' => [ $this, 'get_rest_todos' ],
'update_callback' => [ $this, 'update_rest_todos' ],
]
);
}

/**
* Read to-dos for a post the user may edit.
*
* @param array $post Prepared REST post.
* @return string
*/
public function get_rest_todos( $post ) {
return \current_user_can( 'edit_post', $post['id'] )
? (string) \get_post_meta( $post['id'], 'progress_planner_page_todos', true ) : '';
}

/**
* Update to-dos for a post the user may edit.
*
* @param string $value To-dos.
* @param \WP_Post $post Post being updated.
* @return true|\WP_Error
*/
public function update_rest_todos( $value, $post ) {
if ( ! \current_user_can( 'edit_post', $post->ID ) ) {
return new \WP_Error( 'rest_forbidden', \__( 'You cannot edit this post.', 'progress-planner' ), [ 'status' => \rest_authorization_required_code() ] );
}
\update_post_meta( $post->ID, 'progress_planner_page_todos', $this->sanitize_post_meta_progress_planner_page_todos( $value ) );
return true;
}

/**
* Sanitize the `progress_planner_page_todos` meta.
*
Expand Down
Loading
Loading