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

Shortcodes: enable inline PDFs #14960

Merged
merged 5 commits into from
Mar 30, 2020
Merged

Shortcodes: enable inline PDFs #14960

merged 5 commits into from
Mar 30, 2020

Conversation

lancewillett
Copy link
Contributor

@lancewillett lancewillett commented Mar 11, 2020

Fixes #14959

Changes proposed in this Pull Request:

Adds new file to enable inline PDF embed handling.

The block editor view:
editor

Before, frontend:
before

After, frontend:
after

Is this a new feature or does it add/remove features to an existing part of Jetpack?

  • New feature. Internal reference: p7DVsv-8fu-p2

Testing instructions:

  • Edit a post or page
  • Put a PDF file URL on its own line (not a link, just bare URL)
  • Preview the post
  • You should see a visual embed instead of the bare URL

If not supported in a given environment, the embed object should fall back to a plain link.

Proposed changelog entry for your changes:

  • Enable inline PDF embeds

Apologies if I missed a lot of steps any important code flags and formatting... Jetpack development has increased in complexity 10x since I last contributed a patch.

@lancewillett lancewillett added [Feature] Shortcodes / Embeds [Status] Needs Testing We need to add this change to the testing call for this month's release labels Mar 11, 2020
@lancewillett lancewillett requested a review from a team as a code owner March 11, 2020 21:40
@jetpackbot
Copy link

jetpackbot commented Mar 11, 2020

Thank you for the great PR description!

When this PR is ready for review, please apply the [Status] Needs Review label. If you are an a11n, please have someone from your team review the code if possible. The Jetpack team will also review this PR and merge it to be included in the next Jetpack release.

Scheduled Jetpack release: April 7, 2020.
Scheduled code freeze: March 31, 2020

Generated by 🚫 dangerJS against fc617a9

Copy link
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

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

When testing this, what was the steps you followed to get a plain link in the editor? When I first pasted the link into the editor, the embed failed and I got this:
image
I then had to convert to link, and then remove the link.

That makes me wonder if this shouldn't be better handled in a new block. In fact, the Gutenberg team seems to be considering adding its own PDF block:
WordPress/gutenberg#19077


Could you fix all PHPCS warnings for that file? Our pre-commit hook would not let you commit this otherwise:

yarn php:lint modules/shortcodes/inline-pdfs.php                                
yarn run v1.21.1
$ composer php:lint modules/shortcodes/inline-pdfs.php
> vendor/bin/phpcs -p -s 'modules/shortcodes/inline-pdfs.php'
E 1 / 1 (100%)



FILE: ...ev/wp-content/plugins/jetpack/modules/shortcodes/inline-pdfs.php
----------------------------------------------------------------------
FOUND 4 ERRORS AFFECTING 3 LINES
----------------------------------------------------------------------
 1 | ERROR | There must be no blank lines before the file comment
   |       | (Squiz.Commenting.FileComment.SpacingAfterOpen)
 6 | ERROR | There must be exactly one blank line after the file
   |       | comment
   |       | (Squiz.Commenting.FileComment.SpacingAfterComment)
 6 | ERROR | Missing @package tag in file comment
   |       | (Squiz.Commenting.FileComment.MissingPackageTag)
 9 | ERROR | Missing doc comment for function
   |       | inline_pdf_embed_handler()
   |       | (Squiz.Commenting.FunctionComment.Missing)
----------------------------------------------------------------------

Since this is a copy of a WordPress.com feature, I think it would be useful to synchronize the 2 features between the 2 codebases. You can see r191075-wpcom for an example of the file we update to keep those things in sync.


This may be an opportunity to have a wider release of the feature on WordPress.com? Do you know if there is a specific reason why the feature is still restricted to a12s there?

Edit: I found the related thread at p7DVsv-8fu-p2. I update the PR description accordingly.


Here is a quick diff to address some of the feedback I left below:

diff --git a/modules/shortcodes/inline-pdfs.php b/modules/shortcodes/inline-pdfs.php
index 41d76396d..b780082b6 100644
--- a/modules/shortcodes/inline-pdfs.php
+++ b/modules/shortcodes/inline-pdfs.php
@@ -1,16 +1,38 @@
 <?php
-
 /**
  * Inline PDFs
- * Takes one-line embeds of a PDF URL (*.pdf), and attempts to embed it directly in the post instead of leaving it as a link.
+ *
+ * Takes one-line embeds of a PDF URL (*.pdf),
+ * and attempts to embed it directly in the post instead of leaving it as a link.
+ *
+ * @package Jetpack
+ */
+
+wp_embed_register_handler( 'inline-pdfs', '#https?://[^<]*\.pdf$#i', 'jetpack_inline_pdf_embed_handler' );
+
+/**
+ * Callback to modify output of inline PDF URLs.
+ *
+ * @param array $matches Regex partial matches against the URL passed.
+ * @param array $attr    Attributes received in embed response.
+ * @param array $url     Requested URL to be embedded.
  */
-wp_embed_register_handler( 'inline-pdfs', '#https?://[^<]*\.pdf$#i', 'inline_pdf_embed_handler' );
+function jetpack_inline_pdf_embed_handler( $matches, $attr, $url ) {
+	/** This action is documented in modules/widgets/social-media-icons.php */
+	do_action( 'jetpack_bump_stats_extras', 'embeds', 'pdf' );
 
-function inline_pdf_embed_handler( $matches, $attr, $url ) {
-	return sprintf(
-		'<object data="%1$s" type="application/pdf" width="100%%" height="800">
-		  <p><a href="%1$s">%1$s</a></p>
-		</object>',
-		esc_attr( $url )
-	);
+	if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
+		return sprintf(
+			'<p><a href="%1$s">%2$s</a></p>',
+			esc_url( $url ),
+			esc_html__( 'PDF Document', 'jetpack' )
+		);
+	} else {
+		return sprintf(
+			'<object data="%1$s" type="application/pdf" width="100%%" height="800">
+				<p><a href="%1$s">%1$s</a></p>
+			</object>',
+			esc_attr( $url )
+		);
+	}
 }

modules/shortcodes/inline-pdfs.php Outdated Show resolved Hide resolved
modules/shortcodes/inline-pdfs.php Outdated Show resolved Hide resolved
modules/shortcodes/inline-pdfs.php Outdated Show resolved Hide resolved
modules/shortcodes/inline-pdfs.php Show resolved Hide resolved
@jeherve jeherve added [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! [Type] Feature Request [Tests] Needs Tests Some Unit Tests would be really useful to include with this PR. and removed [Status] In Progress [Status] Needs Testing We need to add this change to the testing call for this month's release labels Mar 12, 2020
@lancewillett
Copy link
Contributor Author

Thank you, @jeherve! That's a massive boost to my contribution. I'll make the suggested changes, keep testing, and ping back for another review.

That makes me wonder if this shouldn't be better handled in a new block

I'd like to suggest we push this now, separately — as it'll still serve a purpose for many people. Just a plain URL embed, easy.

@simison
Copy link
Member

simison commented Mar 14, 2020

This seems pretty major change to how people's content might change...

Should you have this as a setting and default off to old site, or not do this for posts before feature release date?

@lancewillett
Copy link
Contributor Author

@simison Thank you for the important note on how front-end content appearance might change when this ships and sites are updated.

I considered the behavior change and opted to proceed with the improvement for several reasons.

  1. It is a much better user experience.
  2. Authors who've used PDF links are unlikely to use a bare URL on one line — they'd typically wrap in an anchor element.
  3. The PDF viewer is native to browsers, robust, and adaptive to devices.
  4. We'll closely watch the adoption and feedback on WordPress.com to inform the Atomic and Jetpack site impact.
  5. We can use the Jetpack release notes, blog post, and other announcement channels to showcase the improvement as it is much better (see point 1).
  6. I believe that it is a fairly quick fix to adapt content (see point 2).

@lancewillett
Copy link
Contributor Author

Hi @jeherve All feedback address except for the AMP support. Choosing to not add that for now.

✅ Unit test added, passes
✅ Fixed prefix and stats bump
✅ Lint errors fixed (except for "missing doc comment" in unit test)
✅ Tested the behavior — it only works if a PDF is on its own line and not wrapped in an anchor element (bare URL)

Screen Shot 2020-03-29 at 16 42 39

Screen Shot 2020-03-29 at 16 42 51

@lancewillett
Copy link
Contributor Author

This file should be synced with WordPress.com. How do we accomplish that?

@jeherve jeherve added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels Mar 30, 2020
Copy link
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

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

This works well in my tests. Merging.

This file should be synced with WordPress.com. How do we accomplish that?

I'll take care of this in D41074-code.

@jeherve jeherve merged commit a749f09 into master Mar 30, 2020
@jeherve jeherve deleted the add/inline-pdf-embed branch March 30, 2020 13:26
@matticbot matticbot added [Status] Needs Changelog and removed [Status] Ready to Merge Go ahead, you can push that green button! labels Mar 30, 2020
@jeherve jeherve removed the [Tests] Needs Tests Some Unit Tests would be really useful to include with this PR. label Mar 30, 2020
@jeherve jeherve added this to the 8.4 milestone Mar 30, 2020
@jeherve
Copy link
Member

jeherve commented Mar 30, 2020

r205041-wpcom

jeherve added a commit that referenced this pull request Mar 31, 2020
jeherve added a commit that referenced this pull request Mar 31, 2020
* Initial changelog entry

* Changelog: add #14904

* Changelog: add #14910

* Changelog: add #14913

* Changelog: add #14916

* Changelog: add #14922

* Changelog: add #14924

* Changelog: add #14925

* Changelog: add #14928

* Changelog: add #14840

* Changelog: add #14841

* Changelog: add #14842

* Changelog: add #14826

* Changelog: add #14835

* Changelog: add #14859

* Changelog: add #14884

* Changelog: add #14888

* Changelog: add #14817

* Changelog: add #14814

* Changelog: add #14819

* Changelog;: add #14797

* Changelog: add #14798

* Changelog: add #14802

* Changelog: add #13676

* Changelog: add #13744

* Changelog: add #13777

* Changelog: add #14446

* Changelog: add #14739

* Changelog: add #14770

* Changelog: add #14784

* Changelog: add #14897

* Changelog: add #14898

* Changelog: add #14968

* Changelog: add #14985

* Changelog: add #15044

* Changelog: add #15052

* Update to remove Podcast since it remains in Beta

* Changelog: add #14803

* Changelog: add #15028

* Changelog: add #15065

* Changelog:add #14886

* Changelog: add #15118

* Changelog: add #14990

* Changelog: add #14528

* Changelog: add #15120

* Changelog: add #15126

* Changelog: add #15049

* Chanegelog: add #14852

* Changelog: add #15090

* Changelog: add #15138

* Changelog: add #15124

* Changelog:add #15055

* Changelog: add #15017

* Changelog: add #15109

* Changelog: add #15145

* Changelog:add #15096

* Changelog:add #15153

* Changelog: add #15133

* Changelog: add #14960

* Changelog: add #15127

* Changelog: add #15056

* Copy current changelog to changelog archive.

* Clarify changelog description
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Shortcodes: allow inline visual PDF file embeds directly in post/page content
5 participants