PluginProbe ʕ •ᴥ•ʔ
Shortcoder — Create Shortcodes for Anything / 5.2.1
Shortcoder — Create Shortcodes for Anything v5.2.1
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
363 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.1
8 Author URI: https://www.aakashweb.com/
9 */
10
11 define( 'SC_VERSION', '5.2.1' );
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 $params = array_change_key_case( $params, CASE_LOWER );
204
205 preg_match_all('/%%([a-zA-Z-0-9]+)\:?(.*?)%%/', $content, $matches);
206
207 $cp_tags = $matches[0];
208 $cp_names = $matches[1];
209 $cp_defaults = $matches[2];
210 $to_replace = array();
211
212 for( $i = 0; $i < count( $cp_names ); $i++ ){
213
214 $name = strtolower( $cp_names[ $i ] );
215 $default = $cp_defaults[ $i ];
216 $value = '';
217
218 if( array_key_exists( $name, $params ) ){
219 $value = $params[ $name ];
220 }
221
222 if( empty( $value ) ){
223 array_push( $to_replace, $default );
224 }else{
225 array_push( $to_replace, $value );
226 }
227
228 }
229
230 $content = str_ireplace( $cp_tags, $to_replace, $content );
231
232 return $content;
233
234 }
235
236 public static function replace_wp_params( $content, $enc_content = null ){
237
238 $params = self::wp_params_list();
239 $metadata = Shortcoder_Metadata::metadata();
240 $metadata[ 'enclosed_content' ] = $enc_content;
241 $all_params = array();
242 $to_replace = array();
243
244 foreach( $params as $group => $group_info ){
245 $all_params = array_merge( $group_info[ 'params' ], $all_params );
246 }
247
248 foreach( $all_params as $id => $name ){
249 if( array_key_exists( $id, $metadata ) ){
250 $placeholder = '$$' . $id . '$$';
251 $to_replace[ $placeholder ] = $metadata[ $id ];
252 }
253 }
254
255 $content = strtr( $content, $to_replace );
256
257 return $content;
258
259 }
260
261 public static function replace_custom_fields( $content ){
262
263 global $post;
264
265 preg_match_all('/\$\$[^\s]+\$\$/', $content, $matches );
266
267 $cf_tags = $matches[0];
268
269 if( empty( $cf_tags ) ){
270 return $content;
271 }
272
273 foreach( $cf_tags as $tag ){
274
275 if( strpos( $tag, 'custom_field:' ) === false ){
276 continue;
277 }
278
279 preg_match( '/:[^\s\$]+/', $tag, $match );
280
281 if( empty( $match ) ){
282 continue;
283 }
284
285 $match = substr( $match[0], 1 );
286 $value = get_post_meta( $post->ID, $match, true );
287 $content = str_replace( $tag, $value, $content );
288
289 }
290
291 return $content;
292
293 }
294
295 public static function wp_params_list(){
296
297 return apply_filters( 'sc_mod_wp_params', array(
298 'wp_info' => array(
299 'name' => __( 'WordPress information', 'shortcoder' ),
300 'icon' => 'wordpress-alt',
301 'params' => array(
302 'url' => __( 'URL of the post/location', 'shortcoder' ),
303 'title' => __( 'Title of the post/location', 'shortcoder' ),
304 'short_url' => __( 'Short URL of the post/location', 'shortcoder' ),
305
306 'post_id' => __( 'Post ID', 'shortcoder' ),
307 'post_image' => __( 'Post featured image URL', 'shortcoder' ),
308 'post_excerpt' => __( 'Post excerpt', 'shortcoder' ),
309 'post_author' => __( 'Post author', 'shortcoder' ),
310 'post_date' => __( 'Post date', 'shortcoder' ),
311 'post_modified_date' => __( 'Post modified date', 'shortcoder' ),
312 'post_comments_count' => __( 'Post comments count', 'shortcoder' ),
313
314 'site_name' => __( 'Site title', 'shortcoder' ),
315 'site_description' => __( 'Site description', 'shortcoder' ),
316 'site_url' => __( 'Site URL', 'shortcoder' ),
317 'site_wpurl' => __( 'WordPress URL', 'shortcoder' ),
318 'site_charset' => __( 'Site character set', 'shortcoder' ),
319 'wp_version' => __( 'WordPress version', 'shortcoder' ),
320 'stylesheet_url' => __( 'Active theme\'s stylesheet URL', 'shortcoder' ),
321 'stylesheet_directory' => __( 'Active theme\'s directory', 'shortcoder' ),
322 'atom_url' => __( 'Atom feed URL', 'shortcoder' ),
323 'rss_url' => __( 'RSS 2.0 feed URL', 'shortcoder' )
324 )
325 ),
326 'date_info' => array(
327 'name' => __( 'Date parameters', 'shortcoder' ),
328 'icon' => 'calendar-alt',
329 'params' => array(
330 'day' => __( 'Day', 'shortcoder' ),
331 'day_lz' => __( 'Day - leading zeros', 'shortcoder' ),
332 'day_ws' => __( 'Day - words - short form', 'shortcoder' ),
333 'day_wf' => __( 'Day - words - full form', 'shortcoder' ),
334 'month' => __( 'Month', 'shortcoder' ),
335 'month_lz' => __( 'Month - leading zeros', 'shortcoder' ),
336 'month_ws' => __( 'Month - words - short form', 'shortcoder' ),
337 'month_wf' => __( 'Month - words - full form', 'shortcoder' ),
338 'year' => __( 'Year', 'shortcoder' ),
339 'year_2d' => __( 'Year - 2 digit', 'shortcoder' ),
340 )
341 ),
342 'sc_cnt' => array(
343 'name' => __( 'Shortcode enclosed content', 'shortcoder' ),
344 'icon' => 'text',
345 'params' => array(
346 'enclosed_content' => __( 'Shortcode enclosed content', 'shortcoder' )
347 )
348 )
349 ));
350
351 }
352
353 public static function load_text_domain(){
354
355 load_plugin_textdomain( 'shortcoder', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
356
357 }
358
359 }
360
361 Shortcoder::init();
362
363 ?>