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
Show file tree
Hide file tree
Changes from 4 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
30 changes: 30 additions & 0 deletions modules/shortcodes/inline-pdfs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Embed support for Inline PDFs
*
* Takes a plain-text PDF URL (*.pdf), and attempts to embed it directly
* in the post instead of leaving it as a bare link.
*
* @package Jetpack
*/

wp_embed_register_handler( 'inline-pdfs', '#https?://[^<]*\.pdf$#i', 'jetpack_inline_pdf_embed_handler' );

/**
* Callback to modify the output of embedded PDF files.
*
* @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.
*/
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', 'inline-pdf' );

return sprintf(
jeherve marked this conversation as resolved.
Show resolved Hide resolved
'<object data="%1$s" type="application/pdf" width="100%%" height="800">
<p><a href="%1$s">%1$s</a></p>
</object>',
esc_attr( $url )
);
}
37 changes: 37 additions & 0 deletions tests/php/modules/shortcodes/test-class.inline-pdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Unit test for Inline PDF embeds.
*
* @package Jetpack
* @since 8.4
*/
class WP_Test_Jetpack_Shortcodes_Inline_Pdfs extends WP_UnitTestCase {

/**
* Unit test for Inline PDF embeds.
*
* @author lancewillett
* @covers ::jetpack_inline_pdf_embed_handler
* @since 8.4
*/
public function test_shortcodes_inline_pdf() {
global $post;

$url = 'https://jetpackme.files.wordpress.com/2017/08/jetpack-tips-for-hosts.pdf';
$post = $this->factory()->post->create_and_get( array( 'post_content' => $url ) );

setup_postdata( $post );

// Test HTML version.
ob_start();
the_content();
$actual = ob_get_clean();
$this->assertContains(
sprintf(
'<p><object data="%1$s" type="application/pdf" width="100 height="800"><p><a href="%1$s">%1$s</a></p></object></p>' . "\n",
jeherve marked this conversation as resolved.
Show resolved Hide resolved
$url
),
$actual
);
}
}