PluginProbe ʕ •ᴥ•ʔ
Shortcoder — Create Shortcodes for Anything / 6.0
Shortcoder — Create Shortcodes for Anything v6.0
trunk 3.0 3.0.1 3.1 3.2 3.3 3.4 3.4.1 4.0 4.0.1 4.0.2 4.0.3 4.1 4.1.1 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2 4.3 4.4 4.5 4.6 5.0 5.0.1 5.0.2 5.0.3 5.0.4 5.1 5.2 5.2.1 5.3 5.3.1 5.3.2 5.3.3 5.3.4 5.4 5.5 5.6 5.7 5.8 6.0 6.1 6.2 6.3 6.3.1 6.3.2 6.4 6.5 6.5.1 6.5.2 6.5.3
shortcoder / shortcoder.php
shortcoder Last commit date
admin 3 years ago includes 3 years ago languages 3 years ago readme.txt 3 years ago shortcoder.php 3 years ago
shortcoder.php
421 lines
1 <?php
2 /*
3 Plugin Name: Shortcoder
4 Plugin URI: https://www.aakashweb.com/wordpress-plugins/shortcoder/
5 Description: Shortcoder plugin allows to create a custom shortcodes for HTML, JavaScript and other snippets. Now the shortcodes can be used in posts/pages and the snippet will be replaced in place.
6 Author: Aakash Chakravarthy
7 Version: 6.0
8 Author URI: https://www.aakashweb.com/
9 Text Domain: shortcoder
10 Domain Path: /languages
11 */
12
13 define( 'SC_VERSION', '6.0' );
14 define( 'SC_PATH', plugin_dir_path( __FILE__ ) ); // All have trailing slash
15 define( 'SC_URL', plugin_dir_url( __FILE__ ) );
16 define( 'SC_ADMIN_URL', trailingslashit( plugin_dir_url( __FILE__ ) . 'admin' ) );
17 define( 'SC_BASE_NAME', plugin_basename( __FILE__ ) );
18 define( 'SC_POST_TYPE', 'shortcoder' );
19
20 // error_reporting(E_ALL);
21
22 final class Shortcoder{
23
24 static public $shortcodes = array();
25
26 static public $current_shortcode = false;
27
28 public static function init(){
29
30 // Include the required
31 self::includes();
32
33 add_shortcode( 'sc', array( __CLASS__, 'execute_shortcode' ) );
34
35 }
36
37 public static function includes(){
38
39 include_once( SC_PATH . 'includes/updates.php' );
40 include_once( SC_PATH . 'includes/metadata.php' );
41 include_once( SC_PATH . 'admin/admin.php' );
42 include_once( SC_PATH . 'admin/form.php' );
43 include_once( SC_PATH . 'admin/edit.php' );
44 include_once( SC_PATH . 'admin/settings.php' );
45 include_once( SC_PATH . 'admin/manage.php' );
46 include_once( SC_PATH . 'admin/tools.php' );
47
48 }
49
50 public static function execute_shortcode( $atts, $enclosed_content = null ){
51
52 $atts = (array) $atts;
53 $shortcodes = self::get_shortcodes();
54
55 if( empty( $shortcodes ) ){
56 return '<!-- No shortcodes are defined -->';
57 }
58
59 $shortcode = self::find_shortcode( $atts, $shortcodes );
60
61 $shortcode = apply_filters( 'sc_mod_shortcode', $shortcode, $atts, $enclosed_content );
62 do_action( 'sc_do_before', $shortcode, $atts );
63
64 if( !is_array( $shortcode ) ){
65 return $shortcode;
66 }
67
68 // Prevent same shortcode nested loop
69 if( self::$current_shortcode == $shortcode[ 'name' ] ){
70 return '';
71 }
72 self::$current_shortcode = $shortcode[ 'name' ];
73
74 $sc_content = $shortcode[ 'content' ];
75 $sc_settings = $shortcode[ 'settings' ];
76
77 if( !self::can_display( $shortcode ) ){
78 $sc_content = '<!-- Shortcode does not match the conditions -->';
79 }else{
80 $sc_content = self::replace_sc_params( $sc_content, $atts );
81 $sc_content = self::replace_wp_params( $sc_content, $enclosed_content );
82 $sc_content = self::replace_custom_fields( $sc_content );
83 $sc_content = do_shortcode( $sc_content );
84 }
85
86 $sc_content = apply_filters( 'sc_mod_output', $sc_content, $atts, $sc_settings, $enclosed_content );
87 do_action( 'sc_do_after', $shortcode, $atts );
88
89 self::$current_shortcode = false;
90
91 return $sc_content;
92
93 }
94
95 public static function get_shortcodes(){
96
97 if( !empty( self::$shortcodes ) ){
98 return self::$shortcodes;
99 }
100
101 $shortcodes = array();
102 $shortcode_posts = get_posts(array(
103 'post_type' => SC_POST_TYPE,
104 'posts_per_page' => -1,
105 'post_status' => 'publish'
106 ));
107
108 foreach( $shortcode_posts as $index => $post ){
109 $shortcodes[ $post->post_name ] = array(
110 'id' => $post->ID,
111 'name' => $post->post_name,
112 'content' => $post->post_content,
113 'settings' => self::get_sc_settings( $post->ID )
114 );
115 }
116
117 self::$shortcodes = $shortcodes;
118
119 return $shortcodes;
120
121 }
122
123 public static function default_sc_settings(){
124
125 return apply_filters( 'sc_mod_sc_settings', array(
126 '_sc_description' => '',
127 '_sc_disable_sc' => 'no',
128 '_sc_disable_admin' => 'no',
129 '_sc_editor' => '',
130 '_sc_allowed_devices' => 'all'
131 ));
132
133 }
134
135 public static function default_settings(){
136
137 return apply_filters( 'sc_mod_settings', array(
138 'default_editor' => 'code',
139 'default_content' => ''
140 ));
141
142 }
143
144 public static function get_settings(){
145
146 $settings = get_option( 'sc_settings', array() );
147 $default_settings = self::default_settings();
148
149 return self::set_defaults( $settings, $default_settings );
150
151 }
152
153 public static function get_sc_settings( $post_id ){
154
155 $meta_vals = get_post_meta( $post_id, '', true );
156 $default_vals = self::default_sc_settings();
157 $settings = array();
158
159 if( !is_array( $meta_vals ) ){
160 return $default_vals;
161 }
162
163 foreach( $default_vals as $key => $val ){
164 $settings[ $key ] = array_key_exists( $key, $meta_vals ) ? $meta_vals[$key][0] : $val;
165 }
166
167 $settings[ '_sc_title' ] = get_the_title( $post_id );
168
169 return $settings;
170
171 }
172
173 public static function get_sc_tag( $post_id ){
174 $post = get_post( $post_id );
175 return '[sc name="' . $post->post_name . '"][/sc]';
176 }
177
178 public static function find_shortcode( $atts, $shortcodes ){
179
180 $sc_name = false;
181
182 // Find by shortcode ID
183 if( array_key_exists( 'sc_id', $atts ) ){
184 $sc_id = $atts[ 'sc_id' ];
185 foreach( $shortcodes as $temp_name => $temp_props ){
186 if( $temp_props[ 'id' ] == $sc_id ){
187 $sc_name = $temp_name;
188 break;
189 }
190 }
191 }
192
193 // If shortcode ID is not passed, then get the shortcode name
194 if( !$sc_name ){
195 if( !array_key_exists( 'name', $atts ) ){
196 return '<!-- Shortcode is missing "name" attribute -->';
197 }
198 $sc_name = $atts[ 'name' ];
199 }
200
201 // Check if the shortcode name exists
202 if( !array_key_exists( $sc_name, $shortcodes ) ){
203 $sc_name = sanitize_title_with_dashes( $sc_name );
204 if( !array_key_exists( $sc_name, $shortcodes ) ){
205 return '<!-- Shortcode does not exist -->';
206 }
207 }
208
209 return $shortcodes[ $sc_name ];
210
211 }
212
213 public static function can_display( $sc_props ){
214
215 $settings = $sc_props['settings'];
216
217 if( $settings[ '_sc_disable_sc' ] == 'yes' ){
218 return false;
219 }
220
221 $devices = $settings[ '_sc_allowed_devices' ];
222
223 if( $devices == 'mobile_only' && !wp_is_mobile() ){
224 return false;
225 }
226
227 if( $devices == 'desktop_only' && wp_is_mobile() ){
228 return false;
229 }
230
231 if( current_user_can( 'manage_options' ) && $settings[ '_sc_disable_admin' ] == 'yes' ){
232 return false;
233 }
234
235 return true;
236
237 }
238
239 public static function replace_sc_params( $content, $params ){
240
241 $params = array_change_key_case( $params, CASE_LOWER );
242
243 preg_match_all('/%%([a-zA-Z0-9_\-]+)\:?(.*?)%%/', $content, $matches);
244
245 $cp_tags = $matches[0];
246 $cp_names = $matches[1];
247 $cp_defaults = $matches[2];
248 $to_replace = array();
249
250 for( $i = 0; $i < count( $cp_names ); $i++ ){
251
252 $name = strtolower( $cp_names[ $i ] );
253 $default = $cp_defaults[ $i ];
254 $value = '';
255
256 if( array_key_exists( $name, $params ) ){
257 $value = $params[ $name ];
258
259 // Handle scenario when the attributes are added with paragraph tags by autop
260 if( substr( $value, 0, 4 ) == '</p>' ){
261 $value = substr( $value, 4 );
262 if( substr( $value, -3 ) == '<p>' ){
263 $value = substr( $value, 0, -3 );
264 }
265 }
266
267 }
268
269 if( $value == '' ){
270 array_push( $to_replace, $default );
271 }else{
272 array_push( $to_replace, $value );
273 }
274
275 }
276
277 $content = str_ireplace( $cp_tags, $to_replace, $content );
278
279 return $content;
280
281 }
282
283 public static function replace_wp_params( $content, $enc_content = null ){
284
285 $params = self::wp_params_list();
286 $metadata = Shortcoder_Metadata::metadata();
287 $metadata[ 'enclosed_content' ] = $enc_content;
288 $all_params = array();
289 $to_replace = array();
290
291 foreach( $params as $group => $group_info ){
292 $all_params = array_merge( $group_info[ 'params' ], $all_params );
293 }
294
295 foreach( $all_params as $id => $name ){
296 if( array_key_exists( $id, $metadata ) ){
297 $placeholder = '$$' . $id . '$$';
298 $to_replace[ $placeholder ] = $metadata[ $id ];
299 }
300 }
301
302 $content = strtr( $content, $to_replace );
303
304 return $content;
305
306 }
307
308 public static function replace_custom_fields( $content ){
309
310 global $post;
311
312 preg_match_all('/\$\$[^\s^$]+\$\$/', $content, $matches );
313
314 $cf_tags = $matches[0];
315
316 if( empty( $cf_tags ) ){
317 return $content;
318 }
319
320 foreach( $cf_tags as $tag ){
321
322 if( strpos( $tag, 'custom_field:' ) === false ){
323 continue;
324 }
325
326 preg_match( '/:[^\s\$]+/', $tag, $match );
327
328 if( empty( $match ) ){
329 continue;
330 }
331
332 $match = substr( $match[0], 1 );
333 $value = is_object( $post ) ? get_post_meta( $post->ID, $match, true ) : '';
334 $content = str_replace( $tag, $value, $content );
335
336 }
337
338 return $content;
339
340 }
341
342 public static function wp_params_list(){
343
344 return apply_filters( 'sc_mod_wp_params', array(
345 'wp_info' => array(
346 'name' => __( 'WordPress information', 'shortcoder' ),
347 'icon' => 'wordpress-alt',
348 'params' => array(
349 'url' => __( 'URL of the post/location', 'shortcoder' ),
350 'title' => __( 'Title of the post/location', 'shortcoder' ),
351 'short_url' => __( 'Short URL of the post/location', 'shortcoder' ),
352
353 'post_id' => __( 'Post ID', 'shortcoder' ),
354 'post_image' => __( 'Post featured image URL', 'shortcoder' ),
355 'post_excerpt' => __( 'Post excerpt', 'shortcoder' ),
356 'post_author' => __( 'Post author', 'shortcoder' ),
357 'post_date' => __( 'Post date', 'shortcoder' ),
358 'post_modified_date' => __( 'Post modified date', 'shortcoder' ),
359 'post_comments_count' => __( 'Post comments count', 'shortcoder' ),
360 'post_slug' => __( 'Post slug', 'shortcoder' ),
361
362 'site_name' => __( 'Site title', 'shortcoder' ),
363 'site_description' => __( 'Site description', 'shortcoder' ),
364 'site_url' => __( 'Site URL', 'shortcoder' ),
365 'site_wpurl' => __( 'WordPress URL', 'shortcoder' ),
366 'site_charset' => __( 'Site character set', 'shortcoder' ),
367 'wp_version' => __( 'WordPress version', 'shortcoder' ),
368 'stylesheet_url' => __( 'Active theme\'s stylesheet URL', 'shortcoder' ),
369 'stylesheet_directory' => __( 'Active theme\'s directory', 'shortcoder' ),
370 'atom_url' => __( 'Atom feed URL', 'shortcoder' ),
371 'rss_url' => __( 'RSS 2.0 feed URL', 'shortcoder' )
372 )
373 ),
374 'date_info' => array(
375 'name' => __( 'Date parameters', 'shortcoder' ),
376 'icon' => 'calendar-alt',
377 'params' => array(
378 'day' => __( 'Day', 'shortcoder' ),
379 'day_lz' => __( 'Day - leading zeros', 'shortcoder' ),
380 'day_ws' => __( 'Day - words - short form', 'shortcoder' ),
381 'day_wf' => __( 'Day - words - full form', 'shortcoder' ),
382 'month' => __( 'Month', 'shortcoder' ),
383 'month_lz' => __( 'Month - leading zeros', 'shortcoder' ),
384 'month_ws' => __( 'Month - words - short form', 'shortcoder' ),
385 'month_wf' => __( 'Month - words - full form', 'shortcoder' ),
386 'year' => __( 'Year', 'shortcoder' ),
387 'year_2d' => __( 'Year - 2 digit', 'shortcoder' ),
388 )
389 ),
390 'sc_cnt' => array(
391 'name' => __( 'Shortcode enclosed content', 'shortcoder' ),
392 'icon' => 'text',
393 'params' => array(
394 'enclosed_content' => __( 'Shortcode enclosed content', 'shortcoder' )
395 )
396 )
397 ));
398
399 }
400
401 public static function set_defaults( $a, $b ){
402
403 $a = (array) $a;
404 $b = (array) $b;
405 $result = $b;
406
407 foreach ( $a as $k => &$v ) {
408 if ( is_array( $v ) && isset( $result[ $k ] ) ) {
409 $result[ $k ] = self::set_defaults( $v, $result[ $k ] );
410 } else {
411 $result[ $k ] = $v;
412 }
413 }
414 return $result;
415 }
416
417 }
418
419 Shortcoder::init();
420
421 ?>