unavailable.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Class Jetpack_Shortcode_Unavailable
  4. */
  5. class Jetpack_Shortcode_Unavailable {
  6. /**
  7. * Set up the actions and filters for the class to listen to.
  8. *
  9. * @param array $shortcodes An associative array of keys being the shortcodes that are unavailable, and a string explaining why.
  10. */
  11. public function __construct( $shortcodes ) {
  12. $this->shortcodes = $shortcodes;
  13. add_action( 'template_redirect', array( $this, 'add_shortcodes' ) );
  14. }
  15. /**
  16. * For all of our defined unavailable shortcodes, if something else hasn't
  17. * already claimed them, add a handler to nullify their output.
  18. */
  19. public function add_shortcodes() {
  20. foreach ( $this->shortcodes as $shortcode => $message ) {
  21. if ( ! shortcode_exists( $shortcode ) ) {
  22. add_shortcode( $shortcode, array( $this, 'stub_shortcode' ) );
  23. }
  24. }
  25. }
  26. /**
  27. * Nullify the output of unavailable shortcodes. Includes a filter to make
  28. * it easier to notify admins that a shortcode that they used is unavailable.
  29. *
  30. * @param $atts
  31. * @param string $content
  32. * @param string $shortcode
  33. * @return mixed|void
  34. */
  35. public function stub_shortcode( $atts, $content = '', $shortcode = '' ) {
  36. $str = '';
  37. if ( current_user_can( 'edit_posts' ) && ! empty( $this->shortcodes[ $shortcode ] ) ) {
  38. $str = sprintf( '<div><strong>%s</strong></div>', $this->shortcodes[ $shortcode ] );
  39. }
  40. /**
  41. * Filter the front-end output of unavailable shortcodes.
  42. *
  43. * @module shortcodes
  44. *
  45. * @since 4.5.0
  46. *
  47. * @param string $str The html displayed in lieu of the shortcode.
  48. * @param array $atts The attributes (numeric or named) passed to the shortcode.
  49. * @param string $content The content (if any) between the opening and closing tags.
  50. * @param string $shortcode The shortcode tag used to invoke this.
  51. */
  52. return apply_filters( 'jetpack_stub_shortcode', $str, $atts, $content, $shortcode );
  53. }
  54. }
  55. new Jetpack_Shortcode_Unavailable( array(
  56. 'blip.tv' => __( 'The Blip.tv service has been shut down since August 20th, 2015.', 'jetpack' ),
  57. ) );