-
Notifications
You must be signed in to change notification settings - Fork 799
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shortcodes: enable inline PDFs (#14960)
* Shortcodes: enable inline PDFs * Fix sprintf replacement * Add unit tests and fix lint errors * Fix lint errors, add prefix, and other fixes Add unit test * Fix unit test Co-authored-by: Jeremy Herve <jeremy@jeremy.hu>
- Loading branch information
1 parent
ad6a639
commit a749f09
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
'<object data="%1$s" type="application/pdf" width="100%%" height="800"> | ||
<p><a href="%1$s">%1$s</a></p> | ||
</object>', | ||
esc_attr( $url ) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?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(); | ||
|
||
wp_reset_postdata(); | ||
|
||
$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", | ||
$url | ||
), | ||
$actual | ||
); | ||
} | ||
} |