PluginProbe ʕ •ᴥ•ʔ
Shortcoder — Create Shortcodes for Anything / 5.7
Shortcoder — Create Shortcodes for Anything v5.7
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 4 years ago includes 4 years ago languages 4 years ago readme.txt 4 years ago shortcoder.php 4 years ago
shortcoder.php
413 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: 5.7
8 Author URI: https://www.aakashweb.com/
9 Text Domain: shortcoder
10 Domain Path: /languages
11 */
12
13 define( 'SC_VERSION', '5.7' );
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 public static function init(){
27
28 // Include the required
29 self::includes();
30
31 add_action( 'plugins_loaded', array( __CLASS__, 'load_text_domain' ) );
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 if( !is_array( $shortcode ) ){
62 return $shortcode;
63 }
64
65 $sc_content = $shortcode[ 'content' ];
66 $sc_settings = $shortcode[ 'settings' ];
67
68 if( !self::can_display( $shortcode ) ){
69 return '<!-- Shortcode does not match the conditions -->';
70 }
71
72 $sc_content = apply_filters( 'sc_mod_content', $sc_content, $atts, $sc_settings );
73
74 $sc_content = self::replace_sc_params( $sc_content, $atts );
75 $sc_content = self::replace_wp_params( $sc_content, $enclosed_content );
76 $sc_content = self::replace_custom_fields( $sc_content );
77 $sc_content = do_shortcode( $sc_content );
78
79 return $sc_content;
80
81 }
82
83 public static function get_shortcodes(){
84
85 if( !empty( self::$shortcodes ) ){
86 return self::$shortcodes;
87 }
88
89 $shortcodes = array();
90 $shortcode_posts = get_posts(array(
91 'post_type' => SC_POST_TYPE,
92 'posts_per_page' => -1,
93 'post_status' => 'publish'
94 ));
95
96 foreach( $shortcode_posts as $index => $post ){
97 $shortcodes[ $post->post_name ] = array(
98 'id' => $post->ID,
99 'content' => $post->post_content,
100 'settings' => self::get_sc_settings( $post->ID )
101 );
102 }
103
104 self::$shortcodes = $shortcodes;
105
106 return $shortcodes;
107
108 }
109
110 public static function default_sc_settings(){
111
112 return array(
113 '_sc_disable_sc' => 'no',
114 '_sc_disable_admin' => 'no',
115 '_sc_editor' => '',
116 '_sc_allowed_devices' => 'all'
117 );
118
119 }
120
121 public static function default_settings(){
122
123 return array(
124 'default_editor' => 'code',
125 'default_content' => ''
126 );
127
128 }
129
130 public static function get_settings(){
131
132 $settings = get_option( 'sc_settings', array() );
133 $default_settings = self::default_settings();
134
135 return self::set_defaults( $settings, $default_settings );
136
137 }
138
139 public static function get_sc_settings( $post_id ){
140
141 $meta_vals = get_post_meta( $post_id, '', true );
142 $default_vals = self::default_sc_settings();
143 $settings = array();
144
145 if( !is_array( $meta_vals ) ){
146 return $default_vals;
147 }
148
149 foreach( $default_vals as $key => $val ){
150 $settings[ $key ] = array_key_exists( $key, $meta_vals ) ? $meta_vals[$key][0] : $val;
151 }
152
153 $settings[ '_sc_title' ] = get_the_title( $post_id );
154
155 return $settings;
156
157 }
158
159 public static function get_sc_tag( $post_id ){
160 $post = get_post( $post_id );
161 return '[sc name="' . $post->post_name . '"][/sc]';
162 }
163
164 public static function find_shortcode( $atts, $shortcodes ){
165
166 $sc_name = false;
167
168 // Find by shortcode ID
169 if( array_key_exists( 'sc_id', $atts ) ){
170 $sc_id = $atts[ 'sc_id' ];
171 foreach( $shortcodes as $temp_name => $temp_props ){
172 if( $temp_props[ 'id' ] == $sc_id ){
173 $sc_name = $temp_name;
174 break;
175 }
176 }
177 }
178
179 // If shortcode ID is not passed, then get the shortcode name
180 if( !$sc_name ){
181 if( !array_key_exists( 'name', $atts ) ){
182 return '<!-- Shortcode is missing "name" attribute -->';
183 }
184 $sc_name = $atts[ 'name' ];
185 }
186
187 // Check if the shortcode name exists
188 if( !array_key_exists( $sc_name, $shortcodes ) ){
189 $sc_name = sanitize_title_with_dashes( $sc_name );
190 if( !array_key_exists( $sc_name, $shortcodes ) ){
191 return '<!-- Shortcode does not exist -->';
192 }
193 }
194
195 return $shortcodes[ $sc_name ];
196
197 }
198
199 public static function can_display( $sc_props ){
200
201 $settings = $sc_props['settings'];
202
203 if( $settings[ '_sc_disable_sc' ] == 'yes' ){
204 return false;
205 }
206
207 $devices = $settings[ '_sc_allowed_devices' ];
208
209 if( $devices == 'mobile_only' && !wp_is_mobile() ){
210 return false;
211 }
212
213 if( $devices == 'desktop_only' && wp_is_mobile() ){
214 return false;
215 }
216
217 if( current_user_can( 'manage_options' ) && $settings[ '_sc_disable_admin' ] == 'yes' ){
218 return false;
219 }
220
221 return true;
222
223 }
224
225 public static function replace_sc_params( $content, $params ){
226
227 $params = array_change_key_case( $params, CASE_LOWER );
228
229 preg_match_all('/%%([a-zA-Z0-9_\-]+)\:?(.*?)%%/', $content, $matches);
230
231 $cp_tags = $matches[0];
232 $cp_names = $matches[1];
233 $cp_defaults = $matches[2];
234 $to_replace = array();
235
236 for( $i = 0; $i < count( $cp_names ); $i++ ){
237
238 $name = strtolower( $cp_names[ $i ] );
239 $default = $cp_defaults[ $i ];
240 $value = '';
241
242 if( array_key_exists( $name, $params ) ){
243 $value = $params[ $name ];
244
245 // Handle scenario when the attributes are added with paragraph tags by autop
246 if( substr( $value, 0, 4 ) == '</p>' ){
247 $value = substr( $value, 4 );
248 if( substr( $value, -3 ) == '<p>' ){
249 $value = substr( $value, 0, -3 );
250 }
251 }
252
253 }
254
255 if( $value == '' ){
256 array_push( $to_replace, $default );
257 }else{
258 array_push( $to_replace, $value );
259 }
260
261 }
262
263 $content = str_ireplace( $cp_tags, $to_replace, $content );
264
265 return $content;
266
267 }
268
269 public static function replace_wp_params( $content, $enc_content = null ){
270
271 $params = self::wp_params_list();
272 $metadata = Shortcoder_Metadata::metadata();
273 $metadata[ 'enclosed_content' ] = $enc_content;
274 $all_params = array();
275 $to_replace = array();
276
277 foreach( $params as $group => $group_info ){
278 $all_params = array_merge( $group_info[ 'params' ], $all_params );
279 }
280
281 foreach( $all_params as $id => $name ){
282 if( array_key_exists( $id, $metadata ) ){
283 $placeholder = '$$' . $id . '$$';
284 $to_replace[ $placeholder ] = $metadata[ $id ];
285 }
286 }
287
288 $content = strtr( $content, $to_replace );
289
290 return $content;
291
292 }
293
294 public static function replace_custom_fields( $content ){
295
296 global $post;
297
298 preg_match_all('/\$\$[^\s^$]+\$\$/', $content, $matches );
299
300 $cf_tags = $matches[0];
301
302 if( empty( $cf_tags ) ){
303 return $content;
304 }
305
306 foreach( $cf_tags as $tag ){
307
308 if( strpos( $tag, 'custom_field:' ) === false ){
309 continue;
310 }
311
312 preg_match( '/:[^\s\$]+/', $tag, $match );
313
314 if( empty( $match ) ){
315 continue;
316 }
317
318 $match = substr( $match[0], 1 );
319 $value = is_object( $post ) ? get_post_meta( $post->ID, $match, true ) : '';
320 $content = str_replace( $tag, $value, $content );
321
322 }
323
324 return $content;
325
326 }
327
328 public static function wp_params_list(){
329
330 return apply_filters( 'sc_mod_wp_params', array(
331 'wp_info' => array(
332 'name' => __( 'WordPress information', 'shortcoder' ),
333 'icon' => 'wordpress-alt',
334 'params' => array(
335 'url' => __( 'URL of the post/location', 'shortcoder' ),
336 'title' => __( 'Title of the post/location', 'shortcoder' ),
337 'short_url' => __( 'Short URL of the post/location', 'shortcoder' ),
338
339 'post_id' => __( 'Post ID', 'shortcoder' ),
340 'post_image' => __( 'Post featured image URL', 'shortcoder' ),
341 'post_excerpt' => __( 'Post excerpt', 'shortcoder' ),
342 'post_author' => __( 'Post author', 'shortcoder' ),
343 'post_date' => __( 'Post date', 'shortcoder' ),
344 'post_modified_date' => __( 'Post modified date', 'shortcoder' ),
345 'post_comments_count' => __( 'Post comments count', 'shortcoder' ),
346 'post_slug' => __( 'Post slug', 'shortcoder' ),
347
348 'site_name' => __( 'Site title', 'shortcoder' ),
349 'site_description' => __( 'Site description', 'shortcoder' ),
350 'site_url' => __( 'Site URL', 'shortcoder' ),
351 'site_wpurl' => __( 'WordPress URL', 'shortcoder' ),
352 'site_charset' => __( 'Site character set', 'shortcoder' ),
353 'wp_version' => __( 'WordPress version', 'shortcoder' ),
354 'stylesheet_url' => __( 'Active theme\'s stylesheet URL', 'shortcoder' ),
355 'stylesheet_directory' => __( 'Active theme\'s directory', 'shortcoder' ),
356 'atom_url' => __( 'Atom feed URL', 'shortcoder' ),
357 'rss_url' => __( 'RSS 2.0 feed URL', 'shortcoder' )
358 )
359 ),
360 'date_info' => array(
361 'name' => __( 'Date parameters', 'shortcoder' ),
362 'icon' => 'calendar-alt',
363 'params' => array(
364 'day' => __( 'Day', 'shortcoder' ),
365 'day_lz' => __( 'Day - leading zeros', 'shortcoder' ),
366 'day_ws' => __( 'Day - words - short form', 'shortcoder' ),
367 'day_wf' => __( 'Day - words - full form', 'shortcoder' ),
368 'month' => __( 'Month', 'shortcoder' ),
369 'month_lz' => __( 'Month - leading zeros', 'shortcoder' ),
370 'month_ws' => __( 'Month - words - short form', 'shortcoder' ),
371 'month_wf' => __( 'Month - words - full form', 'shortcoder' ),
372 'year' => __( 'Year', 'shortcoder' ),
373 'year_2d' => __( 'Year - 2 digit', 'shortcoder' ),
374 )
375 ),
376 'sc_cnt' => array(
377 'name' => __( 'Shortcode enclosed content', 'shortcoder' ),
378 'icon' => 'text',
379 'params' => array(
380 'enclosed_content' => __( 'Shortcode enclosed content', 'shortcoder' )
381 )
382 )
383 ));
384
385 }
386
387 public static function set_defaults( $a, $b ){
388
389 $a = (array) $a;
390 $b = (array) $b;
391 $result = $b;
392
393 foreach ( $a as $k => &$v ) {
394 if ( is_array( $v ) && isset( $result[ $k ] ) ) {
395 $result[ $k ] = self::set_defaults( $v, $result[ $k ] );
396 } else {
397 $result[ $k ] = $v;
398 }
399 }
400 return $result;
401 }
402
403 public static function load_text_domain(){
404
405 load_plugin_textdomain( 'shortcoder', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
406
407 }
408
409 }
410
411 Shortcoder::init();
412
413 ?>