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