Completed
Push — branch-4.5-built ( 997ccf...3ed81f )
by
unknown
68:20 queued 60:23
created

Jetpack_Shortcode_Unavailable::stub_shortcode()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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