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