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; |
|
|
|
|
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
|
|
|
|
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: