Skip to content

Conversation

@creativecoder
Copy link
Contributor

@creativecoder creativecoder commented Oct 6, 2022

Changes proposed in this Pull Request:

  • Adds an experimental editor extension that displays a daily writing prompt for new posts.
Placeholder Pullquote
image image

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?

Jetpack product discussion

pe7F0s-gI-p2

Does this pull request change what data or activity we track or use?

No.

Testing instructions:

Jetpack Site

  • Be sure the site is connected to WP.com to load Jetpack blocks in the editor
  • Enable beta blocks by adding define( 'JETPACK_BETA_BLOCKS', true ) (or Settings > Jetpack Constants if you're using jurassic.ninja)
  • Set the site as "having a blog" by doing at least one of the following
    • Set the site_intent option to write
    • Set the front page to show posts in Settings > Reading
    • Set a static page for the posts page in Settings > Reading
  • Create a new post, you should see a writing prompt in the placeholder for the first paragraph
  • Create a new post and add the answer_prompt query param with the prompt id for today as the value (e.g. /wp-admin/post-new.php?answer_prompt=2129), and you should see a writing prompt as a pullquote block, the tag dailyprompt added to the post, and the _jetpack_blogging_prompt_key post meta set to the prompt id.
  • Create a new page or custom post type, and you should not see the writing prompt
  • Open an existing post, and you should not see the writing prompt

Simple Site

  • Sandbox a Simple site domain
  • Apply this PR to your sandbox: bin/jetpack-downloader test jetpack try/paragraph-block-writing-prompts
  • Continue with the testing instructions, as above.

@creativecoder creativecoder marked this pull request as ready for review October 6, 2022 20:02
@creativecoder creativecoder self-assigned this Oct 6, 2022
@github-actions github-actions bot added [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] In Progress [Block] Writing Prompts labels Oct 6, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Oct 6, 2022

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ⚠️ All commits were linted before commit.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Once your PR is ready for review, check one last time that all required checks (other than "Required review") appearing at the bottom of this PR are passing or skipped.
Then, add the "[Status] Needs Team review" label and ask someone from your team review the code.
Once you’ve done so, switch to the "[Status] Needs Review" label; someone from Jetpack Crew will then review this PR and merge it to be included in the next Jetpack release.


Jetpack plugin:

  • Next scheduled release: December 6, 2022.
  • Scheduled code freeze: November 28, 2022.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 6, 2022

Are you an Automattician? You can now test your Pull Request on WordPress.com. On your sandbox, run bin/jetpack-downloader test jetpack try/paragraph-block-writing-prompts to get started. More details: p9dueE-5Nn-p2

@creativecoder creativecoder added [Status] Needs Team Review Obsolete. Use Needs Review instead. and removed [Status] In Progress labels Oct 6, 2022
Copy link
Contributor

@roo2 roo2 left a comment

Choose a reason for hiding this comment

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

hmm it seems strange to me that this is implemented as an experimental block. Maybe a standalone package might make more sense? I'm not sure, I wonder what the jetpack crew will say.

Copy link
Contributor

@roo2 roo2 left a comment

Choose a reason for hiding this comment

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

One thing I noticed, if I were to start a post, then save it as a draft and then come back to it, I'd no longer see the writing prompt. I don't think it's worth handling that case specially but just something I noticed. Appart from that I just had very minor nit picks.

Copy link
Contributor

@roo2 roo2 left a comment

Choose a reason for hiding this comment

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

Another thing I noticed with this approach is that when I hit the backspace key when the post is empty, the prompt is removed.

Screen.Recording.2022-10-25.at.11.20.18.am.mov

@creativecoder creativecoder changed the title Editor: add writing prompts to posts Editor: add blogging prompts to posts Oct 31, 2022
@creativecoder
Copy link
Contributor Author

Note that this PR should now be compatible with Simple Sites, as well. I've added testing instructions in the PR description.

There is a small change required to wpcom to get things working, which you can see in D91157-code. This change will need to be merged when this feature is released to wpcom.

roo2
roo2 previously approved these changes Oct 31, 2022
Copy link
Contributor

@roo2 roo2 left a comment

Choose a reason for hiding this comment

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

Tested on simple sites with D91157-code andmy jurassic.tube site with D88262-code. LGTM!

When we merge this we will be seeing the prompt, I think we need to make sure the translation issue is solved at least before we merge.

@roo2 roo2 added [Status] Needs Review This PR is ready for review. and removed [Status] Needs Team Review Obsolete. Use Needs Review instead. labels Oct 31, 2022
@zinigor
Copy link
Contributor

zinigor commented Oct 31, 2022

I have tested this one using my Jetpack development site, and I can't get it to work in the pullquote mode:
изображение
The problem here is that the quote only contains the big " sign, and then it only says "Add a quote".

The other problem that I have experienced is that the quote that I get from the API is always the same:
What does it mean to be a kid at heart?

@zinigor zinigor added [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. and removed [Status] Needs Review This PR is ready for review. labels Oct 31, 2022
@creativecoder
Copy link
Contributor Author

I've added a number of changes today:

Breaks out the blogging site checks so they can be used individually

  • For example, the editor extension in this PR will load for jetpack_has_write_intent or jetpack_has_posts_page, but not jetpack_has_or_will_publish_posts, because we don't want to force the writing prompt just because a site has published posts, which could be used for things other than blogging.

Adds the _locale parameter to the blogging prompts api request, for compatibility with D91251-code and loading translated prompts (when they're ready).

Change the answer_prompt parameter to take a prompt id, so we can link the response to specific prompts, but also default to the first prompt if we can't find that id.

Moved the extension to beta rather than experimental, so we can merge this without having to wait for translations to be finished. We have other wpcom diffs that we want to use some of the blogging prompt functions, so I'd like to try and merge this soon-ish.

@creativecoder creativecoder added [Status] Needs Review This PR is ready for review. and removed [Status] Needs Team Review Obsolete. Use Needs Review instead. labels Nov 17, 2022
@creativecoder
Copy link
Contributor Author

Thanks for the team reviews! I think this is ready for a look by Jetpack Crew.

bindlegirl
bindlegirl previously approved these changes Nov 17, 2022
Copy link
Contributor

@bindlegirl bindlegirl left a comment

Choose a reason for hiding this comment

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

Works as advertised.

@bindlegirl bindlegirl added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review This PR is ready for review. labels Nov 17, 2022
@creativecoder creativecoder enabled auto-merge (squash) November 17, 2022 21:53
@creativecoder
Copy link
Contributor Author

Note that there's a companion PR at #27483 to move this editor extension to experimental. We'd like to make it available to WP.com hosted sites (Simple and Atomic) for next week's release.

* Blogging Prompts: Move to experimental blocks
* Simplifies date generation
@creativecoder creativecoder merged commit 09ac101 into trunk Nov 21, 2022
@creativecoder creativecoder deleted the try/paragraph-block-writing-prompts branch November 21, 2022 17:28
@github-actions github-actions bot removed the [Status] Ready to Merge Go ahead, you can push that green button! label Nov 21, 2022
@github-actions github-actions bot added this to the jetpack/11.5.2 milestone Nov 21, 2022
* @return void
*/
function jetpack_setup_blogging_prompt_response( $post_id ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
Copy link
Contributor

@anomiex anomiex Nov 21, 2022

Choose a reason for hiding this comment

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

Why is nonce verification being ignored here? We try to include a comment explaining it.

See p9dueE-4z8-p2 for more info.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are generating the link to be clicked from a WP.com notification, so it's possible the nonce would change before the user clicks the link. I'll add a comment.

Not ideal, as we are "setting a field," in the form of a tag, post_meta with the prompt id, and a prefilled pull quote block, though no data is saved until the user publishes. Please let me know if you have other thoughts or a better solution!

Copy link
Contributor

Choose a reason for hiding this comment

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

As long as it's well-explained in the comment, I think that's a good start. 👍

Do I have this right? There's a link in the notification, and clicking the link winds up calling this function. You can't put a nonce in the link, because it might be a while in between when the link is generated and when it's clicked. The "attack" that a nonce would prevent would be some third party tricking the blog admin's browser into following the link, maybe repeatedly to flood the site with these posts. Ideally the comment should address why that potential attack isn't something to actually worry about.

*/
function jetpack_get_daily_blogging_prompts( $time = 0 ) {
// Default to the time in the site's timezone.
$timestamp = $time ? $time : current_time( 'timestamp' ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are you ignoring this sniff? Wouldn't it be better to use time() and wp_date() instead?

As it is, you have $time documented as a Unix timestamp on line 57, but then on line 64 you're treating it as a deprecated "WordPress timestamp+offset" value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't realize that wp_date will default to the site's timezone (which is what's desired here, to respect the site settings). Will refactor.

}

$blog_id = \Jetpack_Options::get_option( 'id' );
$path = '/sites/' . $blog_id . '/blogging-prompts?from=' . $yesterday . '&number=10&_locale=' . $locale;
Copy link
Contributor

Choose a reason for hiding this comment

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

While all this is probably safe, it might be better to wrap the variables with rawurlencode() to guarantee it.

return null;
}

$body = json_decode( wp_remote_retrieve_body( $response ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

Should there be error checking here in case the json_decode() returns null?

*
* @return string
*/
function jetpack_get_mag16_locale() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function seems a bit unfortunate to have in Jetpack rather than having the server side do the fix-up. If you were to add fr_BE, for example, you'd have to wait for the monthly Jetpack release (and for people to update to it) for standalone sites to get it. While if this were done on the server side instead, they'd start getting it as soon as you updated it there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good point. The blogging-prompts endpoint is relying on a global _locale, but I'll see about customizing it just for this endpoint and pulling out this function. FYI, some related conversation: D91251-code#1932120

if ( should_load_blogging_prompts() ) {
$daily_prompts = wp_json_encode( jetpack_get_daily_blogging_prompts() );

// See p7H4VZ-2cf-p2 for why prompt data is escaped this way.
Copy link
Contributor

Choose a reason for hiding this comment

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

As the post itself notes, json_encode() is safe to use here with the right flags, and the problem back in February 2019 was that WordPress still supported PHP 5.2 which didn't have those flags. We've been at a minimum of 5.6 now for a long time, so just use wp_json_encode( ..., JSON_HEX_TAG ).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for clarifying what's currently considered safe. I'll refactor.

function should_load_blogging_prompts() {
return jetpack_has_write_intent() ||
jetpack_has_posts_page() ||
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
Copy link
Contributor

Choose a reason for hiding this comment

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

As above, why can we ignore nonce verification here?

@anomiex
Copy link
Contributor

anomiex commented Nov 21, 2022

Hmm, my review came in a few minutes too late. :( Hopefully those comments get addressed in a followup.

@creativecoder
Copy link
Contributor Author

@anomiex Thanks so much for the review and feedback! I'll answer comments and create a follow-up PR to address issues later today.

@creativecoder
Copy link
Contributor Author

Follow-up PR here: #27557

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Blogging Prompts [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] Needs Test Review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants