Completed
Push — update/lytro-unavailable ( f42e6b...faa739 )
by Jeremy
06:57
created

unavailable.php ➔ jetpack_init_shortcode_unavailable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
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 Jetpack
7
 */
8
9
/**
10
 * Class Jetpack_Shortcode_Unavailable
11
 */
12
class Jetpack_Shortcode_Unavailable {
13
	/**
14
	 * Set up the actions and filters for the class to listen to.
15
	 *
16
	 * @param array $shortcodes An associative array of keys being the shortcodes that are unavailable, and a string explaining why.
17
	 */
18
	public function __construct( $shortcodes ) {
19
		$this->shortcodes = $shortcodes;
0 ignored issues
show
Bug introduced by
The property shortcodes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
21
		add_action( 'template_redirect', array( $this, 'add_shortcodes' ) );
22
	}
23
24
	/**
25
	 * For all of our defined unavailable shortcodes, if something else hasn't
26
	 * already claimed them, add a handler to nullify their output.
27
	 */
28
	public function add_shortcodes() {
29
		foreach ( array_keys( $this->shortcodes ) as $shortcode ) {
30
			if ( ! shortcode_exists( $shortcode ) ) {
31
				add_shortcode( $shortcode, array( $this, 'stub_shortcode' ) );
32
			}
33
		}
34
	}
35
36
	/**
37
	 * Nullify the output of unavailable shortcodes.  Includes a filter to make
38
	 * it easier to notify admins that a shortcode that they used is unavailable.
39
	 *
40
	 * @param array  $atts      Shortcode attributes.
41
	 * @param string $content   Post content.
42
	 * @param string $shortcode Shortcode name.
43
	 *
44
	 * @return mixed|void
45
	 */
46
	public function stub_shortcode( $atts, $content = '', $shortcode = '' ) {
47
		$str = '';
48
		if ( current_user_can( 'edit_posts' ) && ! empty( $this->shortcodes[ $shortcode ] ) ) {
49
			$str = sprintf( '<div><strong>%s</strong></div>', $this->shortcodes[ $shortcode ] );
50
		}
51
		/**
52
		 * Filter the front-end output of unavailable shortcodes.
53
		 *
54
		 * @module shortcodes
55
		 *
56
		 * @since 4.5.0
57
		 *
58
		 * @param string $str The html displayed in lieu of the shortcode.
59
		 * @param array $atts The attributes (numeric or named) passed to the shortcode.
60
		 * @param string $content The content (if any) between the opening and closing tags.
61
		 * @param string $shortcode The shortcode tag used to invoke this.
62
		 */
63
		return apply_filters( 'jetpack_stub_shortcode', $str, $atts, $content, $shortcode );
64
	}
65
}
66
67
/**
68
 * Init class.
69
 */
70
function jetpack_init_shortcode_unavailable() {
71
	new Jetpack_Shortcode_Unavailable(
72
		array(
73
			'blip.tv' => __( 'The Blip.tv service has been shut down since August 20th, 2015.', 'jetpack' ),
74
			'lytro'   => __( 'Lytro has been shut down since March 2019.', 'jetpack' ),
75
		)
76
	);
77
}
78
add_action( 'init', 'jetpack_init_shortcode_unavailable' );
79