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; |
|
|
|
|
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
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: