Няма описание

unavailable.php 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
  2. /**
  3. * Display a message on the frontend when we retire a shortcode,
  4. * explaining why the shortcode is not available anymore.
  5. *
  6. * @package automattic/jetpack
  7. */
  8. /**
  9. * Class Jetpack_Shortcode_Unavailable
  10. */
  11. class Jetpack_Shortcode_Unavailable {
  12. /**
  13. * Set up the actions and filters for the class to listen to.
  14. *
  15. * @param array $shortcodes An associative array of keys being the shortcodes that are unavailable, and a string explaining why.
  16. */
  17. public function __construct( $shortcodes ) {
  18. $this->shortcodes = $shortcodes;
  19. add_action( 'template_redirect', array( $this, 'add_shortcodes' ) );
  20. }
  21. /**
  22. * For all of our defined unavailable shortcodes, if something else hasn't
  23. * already claimed them, add a handler to nullify their output.
  24. */
  25. public function add_shortcodes() {
  26. foreach ( array_keys( $this->shortcodes ) as $shortcode ) {
  27. if ( ! shortcode_exists( $shortcode ) ) {
  28. add_shortcode( $shortcode, array( $this, 'stub_shortcode' ) );
  29. }
  30. }
  31. }
  32. /**
  33. * Nullify the output of unavailable shortcodes. Includes a filter to make
  34. * it easier to notify admins that a shortcode that they used is unavailable.
  35. *
  36. * @param array $atts Shortcode attributes.
  37. * @param string $content Post content.
  38. * @param string $shortcode Shortcode name.
  39. *
  40. * @return mixed|void
  41. */
  42. public function stub_shortcode( $atts, $content = '', $shortcode = '' ) {
  43. $str = '';
  44. if ( current_user_can( 'edit_posts' ) && ! empty( $this->shortcodes[ $shortcode ] ) ) {
  45. $str = sprintf( '<div><strong>%s</strong></div>', $this->shortcodes[ $shortcode ] );
  46. }
  47. /**
  48. * Filter the front-end output of unavailable shortcodes.
  49. *
  50. * @module shortcodes
  51. *
  52. * @since 4.5.0
  53. *
  54. * @param string $str The html displayed in lieu of the shortcode.
  55. * @param array $atts The attributes (numeric or named) passed to the shortcode.
  56. * @param string $content The content (if any) between the opening and closing tags.
  57. * @param string $shortcode The shortcode tag used to invoke this.
  58. */
  59. return apply_filters( 'jetpack_stub_shortcode', $str, $atts, $content, $shortcode );
  60. }
  61. }
  62. /**
  63. * Init class.
  64. */
  65. function jetpack_init_shortcode_unavailable() {
  66. new Jetpack_Shortcode_Unavailable(
  67. array(
  68. 'digg' => __( 'The Digg API was shut down in 2014.', 'jetpack' ),
  69. 'hulu' => __( 'Hulu no longer allows embedding content.', 'jetpack' ),
  70. 'blip.tv' => __( 'The Blip.tv service has been shut down since August 20th, 2015.', 'jetpack' ),
  71. 'googlevideo' => __( 'The Google Video embed service is not available anymore, it has been replaced by YouTube.', 'jetpack' ),
  72. 'jetpack-email-subscribe' => __( 'The Email Subscribe shortcode is now available as a block in the Block editor.', 'jetpack' ),
  73. 'lytro' => __( 'Lytro has been shut down since March 2019.', 'jetpack' ),
  74. )
  75. );
  76. }
  77. add_action( 'init', 'jetpack_init_shortcode_unavailable' );