暫無描述

inline-pdfs.php 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. wp_embed_register_handler( 'inline-pdfs', '#https?://[^<]*\.pdf$#i', 'jetpack_inline_pdf_embed_handler' );
  11. /**
  12. * Callback to modify the output of embedded PDF files.
  13. *
  14. * @param array $matches Regex partial matches against the URL passed.
  15. * @param array $attr Attributes received in embed response.
  16. * @param array $url Requested URL to be embedded.
  17. */
  18. function jetpack_inline_pdf_embed_handler( $matches, $attr, $url ) {
  19. /** This action is documented in modules/widgets/social-media-icons.php */
  20. do_action( 'jetpack_bump_stats_extras', 'embeds', 'inline-pdf' );
  21. if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
  22. return sprintf(
  23. '<p><a href="%1$s">%2$s</a></p>',
  24. esc_url( $url ),
  25. esc_html__( 'PDF Document', 'jetpack' )
  26. );
  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. return sprintf(
  35. '<object data="%1$s" type="application/pdf" width="100%%" height="800" style="height: 800px;">
  36. <p><a href="%1$s">%2$s</a></p>
  37. </object>',
  38. esc_attr( $url ),
  39. $fallback_text
  40. );
  41. }