1
|
|
|
<?php |
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // security check |
3
|
|
|
/** |
4
|
|
|
* Sensei Shortcode Loader Class |
5
|
|
|
* |
6
|
|
|
* This class handles the api for all Sensei shortcodes. It does not |
7
|
|
|
* execute on the shortcodes directly but relies on a class that responds |
8
|
|
|
* to each shortcode. Whe WordPress calls do_shortcode for a shortcode registered |
9
|
|
|
* in this function, the functions load_shortcode will be called and it will |
10
|
|
|
* instantiate the correct shortcode handling class as it was registered. |
11
|
|
|
* |
12
|
|
|
* @package Sensei |
13
|
|
|
* @category Shortcodes |
14
|
|
|
* @since 1.9.0 |
15
|
|
|
*/ |
16
|
|
|
class Sensei_Shortcode_Loader{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array { |
20
|
|
|
* type string $shortcode |
21
|
|
|
* type Sensei_Shortcode |
22
|
|
|
* } all the shortcodes and which class to instantiate when they are called from |
23
|
|
|
* WordPress's do_shortcode() function. |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
protected $shortcode_classes; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Run all the functions that needs to be hooked into WordPress |
30
|
|
|
* |
31
|
|
|
* @since 1.9.0 |
32
|
|
|
*/ |
33
|
|
|
public function __construct(){ |
34
|
|
|
|
35
|
|
|
// create a list of shortcodes and the class that handles them |
36
|
|
|
$this->setup_shortcode_class_map(); |
37
|
|
|
|
38
|
|
|
// setup all the shortcodes and load the listener into WP |
39
|
|
|
$this->initialize_shortcodes(); |
40
|
|
|
|
41
|
|
|
// add sensei body class for shortcodes |
42
|
|
|
add_filter( 'body_class', array( $this, 'possibly_add_body_class' )); |
43
|
|
|
|
44
|
|
|
// array( $this, 'add_body_class') |
|
|
|
|
45
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Array of shortcode classes that should be instantiated when WordPress loads |
50
|
|
|
* a Sensei specific shortcode. |
51
|
|
|
* This list contains: |
52
|
|
|
* $shortcode => $class_name |
53
|
|
|
* |
54
|
|
|
* $shortcode is the actual shortcode the user will add to the editor |
55
|
|
|
* $class_name is the name of the class that will be instantiated to handle |
56
|
|
|
* the rendering of the shortcode. |
57
|
|
|
* |
58
|
|
|
* NOTE: When adding a new shortcode here be sure to load your shortcodes class |
59
|
|
|
* in class-sensei-autoloader class_file_map function |
60
|
|
|
*/ |
61
|
|
|
public function setup_shortcode_class_map(){ |
62
|
|
|
|
63
|
|
|
$this->shortcode_classes = array( |
64
|
|
|
'sensei_featured_courses' => 'Sensei_Shortcode_Featured_Courses', |
65
|
|
|
'sensei_user_courses' => 'Sensei_Shortcode_User_Courses', |
66
|
|
|
'sensei_courses' => 'Sensei_Shortcode_Courses', |
67
|
|
|
'sensei_teachers' => 'Sensei_Shortcode_Teachers', |
68
|
|
|
'sensei_user_messages' => 'Sensei_Shortcode_User_Messages', |
69
|
|
|
'sensei_course_page' => 'Sensei_Shortcode_Course_Page', |
70
|
|
|
'sensei_lesson_page' => 'Sensei_Shortcode_Lesson_Page', |
71
|
|
|
'sensei_course_categories' => 'Sensei_Shortcode_Course_Categories', |
72
|
|
|
'sensei_unpurchased_courses' => 'Sensei_Shortcode_Unpurchased_Courses', |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
// legacy shortcode handling: |
76
|
|
|
Sensei_Legacy_Shortcodes::init(); |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Add all shortcodes here |
82
|
|
|
* |
83
|
|
|
* This function adds shortcodes to WP that links to other functionality. |
84
|
|
|
* @since 1.9.0 |
85
|
|
|
*/ |
86
|
|
|
public function initialize_shortcodes(){ |
87
|
|
|
|
88
|
|
|
// shortcodes should only respond to front end calls |
89
|
|
|
if( is_admin() || defined( 'DOING_AJAX' ) ){ |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Tell WP to run this classes load_shortcode function for all the |
95
|
|
|
* shortcodes registered here in. |
96
|
|
|
* |
97
|
|
|
* With this method we only load shortcode classes when we need them. |
98
|
|
|
*/ |
99
|
|
|
foreach( $this->shortcode_classes as $shortcode => $class ){ |
100
|
|
|
|
101
|
|
|
// all Sensei shortcodes are rendered by this loader class |
102
|
|
|
// it acts as an interface between wp and the shortcodes registered |
103
|
|
|
// above |
104
|
|
|
add_shortcode( $shortcode, array( $this,'render_shortcode' ) ); |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Respond to WordPress do_shortcode calls |
112
|
|
|
* for shortcodes registered in the initialize_shortcodes function. |
113
|
|
|
* |
114
|
|
|
* @since 1.9.0 |
115
|
|
|
* |
116
|
|
|
* @param $attributes |
117
|
|
|
* @param $content |
118
|
|
|
* @param $code the shortcode that is being requested |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function render_shortcode( $attributes='', $content='', $code ){ |
123
|
|
|
|
124
|
|
|
// only respond if the shortcode that we've added shortcode |
125
|
|
|
// classes for. |
126
|
|
|
if( ! isset( $this->shortcode_classes[ $code ] ) ){ |
127
|
|
|
return ''; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// create an instances of the current requested shortcode |
131
|
|
|
$shortcode_handling_class = $this->shortcode_classes[ $code ]; |
132
|
|
|
$shortcode = new $shortcode_handling_class( $attributes, $content, $code ); |
133
|
|
|
|
134
|
|
|
// we expect the sensei class instantiated to implement the Sensei_Shortcode interface |
135
|
|
|
if( ! in_array( 'Sensei_Shortcode_Interface', class_implements( $shortcode) ) ){ |
136
|
|
|
|
137
|
|
|
$message = "The rendering class for your shortcode: $code, must implement the Sensei_Shortcode interface"; |
138
|
|
|
_doing_it_wrong('Sensei_Shortcode_Loader::render_shortcode',$message, '1.9.0' ); |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $shortcode->render(); |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Add the Sensei body class if |
148
|
|
|
* the current page has a Sensei shortcode. |
149
|
|
|
* |
150
|
|
|
* Note: legacy shortcodes not supported here. |
151
|
|
|
* |
152
|
|
|
* @since 1.9.0 |
153
|
|
|
* |
154
|
|
|
* @param array $classes |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
|
public function possibly_add_body_class ( $classes ) { |
158
|
|
|
|
159
|
|
|
global $post; |
160
|
|
|
|
161
|
|
|
$has_sensei_shortcode = false; |
162
|
|
|
|
163
|
|
|
if ( is_a( $post, 'WP_Post' ) ) { |
164
|
|
|
|
165
|
|
|
// check all registered Sensei shortcodes (not legacy shortcodes) |
166
|
|
|
foreach ( $this->shortcode_classes as $shortcode => $class ){ |
167
|
|
|
|
168
|
|
|
if ( has_shortcode( $post->post_content, $shortcode ) ) { |
169
|
|
|
|
170
|
|
|
$has_sensei_shortcode = true; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if( $has_sensei_shortcode ) { |
177
|
|
|
$classes[] = 'sensei' ; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
return $classes; |
182
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
} // end class Sensei_Shortcodes |
186
|
|
|
new Sensei_Shortcode_Loader(); |
187
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.