Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.57% covered (warning)
78.57%
11 / 14
100.00% covered (success)
100.00%
1 / 1
CRAP
n/a
0 / 0
jetpack_inline_pdf_embed_handler
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Embed support for Inline PDFs
4 *
5 * Takes a plain-text PDF URL (*.pdf), and attempts to embed it directly
6 * in the post instead of leaving it as a bare link.
7 *
8 * @package automattic/jetpack
9 */
10
11if ( ! defined( 'ABSPATH' ) ) {
12    exit( 0 );
13}
14
15wp_embed_register_handler( 'inline-pdfs', '#https?://[^<]*\.pdf$#i', 'jetpack_inline_pdf_embed_handler' );
16
17/**
18 * Callback to modify the output of embedded PDF files.
19 *
20 * @param array $matches Regex partial matches against the URL passed.
21 * @param array $attr    Attributes received in embed response.
22 * @param array $url     Requested URL to be embedded.
23 */
24function jetpack_inline_pdf_embed_handler( $matches, $attr, $url ) {
25    /** This action is documented in modules/widgets/social-media-icons.php */
26    do_action( 'jetpack_bump_stats_extras', 'embeds', 'inline-pdf' );
27
28    $filename      = basename( wp_parse_url( $url, PHP_URL_PATH ) );
29    $fallback_text = sprintf(
30        /* translators: Placeholder is a file name, for example "file.pdf" */
31        esc_html__( 'Click to access %1$s', 'jetpack' ),
32        $filename
33    );
34
35    return sprintf(
36        '<p><a href="%1$s" target="_blank" rel="noopener noreferrer nofollow">%2$s</a></p>',
37        esc_url( $url ),
38        $fallback_text
39    );
40}