PluginProbe ʕ •ᴥ•ʔ
Shortcoder — Create Shortcodes for Anything / 5.2
Shortcoder — Create Shortcodes for Anything v5.2
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 6 years ago includes 6 years ago languages 6 years ago readme.txt 6 years ago shortcoder.php 6 years ago
shortcoder.php
375 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.2
8 Author URI: https://www.aakashweb.com/
9 */
10
11 define( 'SC_VERSION', '5.2' );
12 define( 'SC_PATH', plugin_dir_path( __FILE__ ) ); // All have trailing slash
13 define( 'SC_URL', plugin_dir_url( __FILE__ ) );
14 define( 'SC_ADMIN_URL', trailingslashit( plugin_dir_url( __FILE__ ) . 'admin' ) );
15 define( 'SC_BASE_NAME', plugin_basename( __FILE__ ) );
16 define( 'SC_POST_TYPE', 'shortcoder' );
17
18 // error_reporting(E_ALL);
19
20 final class Shortcoder{
21
22 static public $shortcodes = array();
23
24 public static function init(){
25
26 // Include the required
27 self::includes();
28
29 add_action( 'plugins_loaded', array( __class__, 'load_text_domain' ) );
30
31 add_shortcode( 'sc', array( __class__, 'execute_shortcode' ) );
32
33 }
34
35 public static function includes(){
36
37 include_once( SC_PATH . 'includes/updates.php' );
38 include_once( SC_PATH . 'includes/metadata.php' );
39 include_once( SC_PATH . 'admin/admin.php' );
40 include_once( SC_PATH . 'admin/form.php' );
41 include_once( SC_PATH . 'admin/edit.php' );
42 include_once( SC_PATH . 'admin/manage.php' );
43 include_once( SC_PATH . 'admin/tools.php' );
44
45 }
46
47 public static function execute_shortcode( $atts, $enclosed_content = null ){
48
49 $shortcodes = self::get_shortcodes();
50
51 if( empty( $shortcodes ) ){
52 return '<!-- No shortcodes are defined -->';
53 }
54
55 $shortcode = self::find_shortcode( $atts, $shortcodes );
56
57 if( !is_array( $shortcode ) ){
58 return $shortcode;
59 }
60
61 $sc_content = $shortcode[ 'content' ];
62 $sc_settings = $shortcode[ 'settings' ];
63
64 if( !self::can_display( $shortcode ) ){
65 return '<!-- Shortcode does not match the conditions -->';
66 }
67
68 $sc_content = self::replace_sc_params( $sc_content, $atts );
69 $sc_content = self::replace_wp_params( $sc_content, $enclosed_content );
70 $sc_content = self::replace_custom_fields( $sc_content );
71 $sc_content = do_shortcode( $sc_content );
72
73 return $sc_content;
74
75 }
76
77 public static function get_shortcodes(){
78
79 if( !empty( self::$shortcodes ) ){
80 return self::$shortcodes;
81 }
82
83 $shortcodes = array();
84 $shortcode_posts = get_posts(array(
85 'post_type' => SC_POST_TYPE,
86 'posts_per_page' => -1,
87 'post_status' => 'publish'
88 ));
89
90 foreach( $shortcode_posts as $index => $post ){
91 $shortcodes[ $post->post_name ] = array(
92 'id' => $post->ID,
93 'content' => $post->post_content,
94 'settings' => self::get_sc_settings( $post->ID )
95 );
96 }
97
98 self::$shortcodes = $shortcodes;
99
100 return $shortcodes;
101
102 }
103
104 public static function default_sc_settings(){
105
106 return array(
107 '_sc_disable_sc' => 'no',
108 '_sc_disable_admin' => 'no',
109 '_sc_editor' => 'visual',
110 '_sc_allowed_devices' => 'all'
111 );
112
113 }
114
115 public static function get_sc_settings( $post_id ){
116
117 $meta_vals = get_post_meta( $post_id, '', true );
118 $default_vals = self::default_sc_settings();
119 $settings = array();
120
121 if( !is_array( $meta_vals ) ){
122 return $default_vals;
123 }
124
125 foreach( $default_vals as $key => $val ){
126 $settings[ $key ] = array_key_exists( $key, $meta_vals ) ? $meta_vals[$key][0] : $val;
127 }
128
129 $settings[ '_sc_title' ] = get_the_title( $post_id );
130
131 return $settings;
132
133 }
134
135 public static function get_sc_tag( $post_id ){
136 $post = get_post( $post_id );
137 return '[sc name="' . $post->post_name . '"]';
138 }
139
140 public static function find_shortcode( $atts, $shortcodes ){
141
142 $sc_name = false;
143
144 // Find by shortcode ID
145 if( array_key_exists( 'sc_id', $atts ) ){
146 $sc_id = $atts[ 'sc_id' ];
147 foreach( $shortcodes as $temp_name => $temp_props ){
148 if( $temp_props[ 'id' ] == $sc_id ){
149 $sc_name = $temp_name;
150 break;
151 }
152 }
153 }
154
155 // If shortcode ID is not passed, then get the shortcode name
156 if( !$sc_name ){
157 if( !array_key_exists( 'name', $atts ) ){
158 return '<!-- Shortcode is missing "name" attribute -->';
159 }
160 $sc_name = $atts[ 'name' ];
161 }
162
163 // Check if the shortcode name exists
164 if( !array_key_exists( $sc_name, $shortcodes ) ){
165 $sc_name = sanitize_title_with_dashes( $sc_name );
166 if( !array_key_exists( $sc_name, $shortcodes ) ){
167 return '<!-- Shortcode does not exist -->';
168 }
169 }
170
171 return $shortcodes[ $sc_name ];
172
173 }
174
175 public static function can_display( $sc_props ){
176
177 $settings = $sc_props['settings'];
178
179 if( $settings[ '_sc_disable_sc' ] == 'yes' ){
180 return false;
181 }
182
183 $devices = $settings[ '_sc_allowed_devices' ];
184
185 if( $devices == 'mobile_only' && !wp_is_mobile() ){
186 return false;
187 }
188
189 if( $devices == 'desktop_only' && wp_is_mobile() ){
190 return false;
191 }
192
193 if( current_user_can( 'manage_options' ) && $settings[ '_sc_disable_admin' ] == 'yes' ){
194 return false;
195 }
196
197 return true;
198
199 }
200
201 public static function replace_sc_params( $content, $params ){
202
203 $keys = array();
204 $values = array();
205
206 preg_match_all('/%%(.*?)%%/', $content, $matches);
207
208 $cp_tags = $matches[0];
209 $cp_data = $matches[1];
210
211 if( empty( $matches[0] ) ){
212 return $content;
213 }
214
215 $to_replace = array();
216
217 foreach( $cp_data as $data ){
218
219 $data = trim( $data );
220 $colon_pos = strpos( $data, ':' );
221 $cp_name = '';
222
223 if( $colon_pos === false ){
224 $cp_name = $data;
225 }else{
226 $cp_name = substr( $data, 0, $colon_pos );
227 }
228
229 if( array_key_exists( $cp_name, $params ) ){
230 array_push( $to_replace, $params[ $cp_name ] );
231 }else{
232 if( $colon_pos === false ){
233 array_push( $to_replace, '' ); # Has no default parameter
234 }else{
235 $cp_value = substr( $data, $colon_pos+1 );
236 array_push( $to_replace, $cp_value );
237 }
238 }
239
240 }
241
242 $content = str_ireplace( $cp_tags, $to_replace, $content );
243
244 return $content;
245
246 }
247
248 public static function replace_wp_params( $content, $enc_content = null ){
249
250 $params = self::wp_params_list();
251 $metadata = Shortcoder_Metadata::metadata();
252 $metadata[ 'enclosed_content' ] = $enc_content;
253 $all_params = array();
254 $to_replace = array();
255
256 foreach( $params as $group => $group_info ){
257 $all_params = array_merge( $group_info[ 'params' ], $all_params );
258 }
259
260 foreach( $all_params as $id => $name ){
261 if( array_key_exists( $id, $metadata ) ){
262 $placeholder = '$$' . $id . '$$';
263 $to_replace[ $placeholder ] = $metadata[ $id ];
264 }
265 }
266
267 $content = strtr( $content, $to_replace );
268
269 return $content;
270
271 }
272
273 public static function replace_custom_fields( $content ){
274
275 global $post;
276
277 preg_match_all('/\$\$[^\s]+\$\$/', $content, $matches );
278
279 $cf_tags = $matches[0];
280
281 if( empty( $cf_tags ) ){
282 return $content;
283 }
284
285 foreach( $cf_tags as $tag ){
286
287 if( strpos( $tag, 'custom_field:' ) === false ){
288 continue;
289 }
290
291 preg_match( '/:[^\s\$]+/', $tag, $match );
292
293 if( empty( $match ) ){
294 continue;
295 }
296
297 $match = substr( $match[0], 1 );
298 $value = get_post_meta( $post->ID, $match, true );
299 $content = str_replace( $tag, $value, $content );
300
301 }
302
303 return $content;
304
305 }
306
307 public static function wp_params_list(){
308
309 return apply_filters( 'sc_mod_wp_params', array(
310 'wp_info' => array(
311 'name' => __( 'WordPress information', 'shortcoder' ),
312 'icon' => 'wordpress-alt',
313 'params' => array(
314 'url' => __( 'URL of the post/location', 'shortcoder' ),
315 'title' => __( 'Title of the post/location', 'shortcoder' ),
316 'short_url' => __( 'Short URL of the post/location', 'shortcoder' ),
317
318 'post_id' => __( 'Post ID', 'shortcoder' ),
319 'post_image' => __( 'Post featured image URL', 'shortcoder' ),
320 'post_excerpt' => __( 'Post excerpt', 'shortcoder' ),
321 'post_author' => __( 'Post author', 'shortcoder' ),
322 'post_date' => __( 'Post date', 'shortcoder' ),
323 'post_modified_date' => __( 'Post modified date', 'shortcoder' ),
324 'post_comments_count' => __( 'Post comments count', 'shortcoder' ),
325
326 'site_name' => __( 'Site title', 'shortcoder' ),
327 'site_description' => __( 'Site description', 'shortcoder' ),
328 'site_url' => __( 'Site URL', 'shortcoder' ),
329 'site_wpurl' => __( 'WordPress URL', 'shortcoder' ),
330 'site_charset' => __( 'Site character set', 'shortcoder' ),
331 'wp_version' => __( 'WordPress version', 'shortcoder' ),
332 'stylesheet_url' => __( 'Active theme\'s stylesheet URL', 'shortcoder' ),
333 'stylesheet_directory' => __( 'Active theme\'s directory', 'shortcoder' ),
334 'atom_url' => __( 'Atom feed URL', 'shortcoder' ),
335 'rss_url' => __( 'RSS 2.0 feed URL', 'shortcoder' )
336 )
337 ),
338 'date_info' => array(
339 'name' => __( 'Date parameters', 'shortcoder' ),
340 'icon' => 'calendar-alt',
341 'params' => array(
342 'day' => __( 'Day', 'shortcoder' ),
343 'day_lz' => __( 'Day - leading zeros', 'shortcoder' ),
344 'day_ws' => __( 'Day - words - short form', 'shortcoder' ),
345 'day_wf' => __( 'Day - words - full form', 'shortcoder' ),
346 'month' => __( 'Month', 'shortcoder' ),
347 'month_lz' => __( 'Month - leading zeros', 'shortcoder' ),
348 'month_ws' => __( 'Month - words - short form', 'shortcoder' ),
349 'month_wf' => __( 'Month - words - full form', 'shortcoder' ),
350 'year' => __( 'Year', 'shortcoder' ),
351 'year_2d' => __( 'Year - 2 digit', 'shortcoder' ),
352 )
353 ),
354 'sc_cnt' => array(
355 'name' => __( 'Shortcode enclosed content', 'shortcoder' ),
356 'icon' => 'text',
357 'params' => array(
358 'enclosed_content' => __( 'Shortcode enclosed content', 'shortcoder' )
359 )
360 )
361 ));
362
363 }
364
365 public static function load_text_domain(){
366
367 load_plugin_textdomain( 'shortcoder', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
368
369 }
370
371 }
372
373 Shortcoder::init();
374
375 ?>