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