PluginProbe ʕ •ᴥ•ʔ
Shortcoder — Create Shortcodes for Anything / 5.4
Shortcoder — Create Shortcodes for Anything v5.4
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 5 years ago includes 5 years ago languages 5 years ago readme.txt 5 years ago shortcoder.php 5 years ago
shortcoder.php
376 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.4
8 Author URI: https://www.aakashweb.com/
9 Text Domain: shortcoder
10 Domain Path: /languages
11 */
12
13 define( 'SC_VERSION', '5.4' );
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 $atts = (array) $atts;
52 $shortcodes = self::get_shortcodes();
53
54 if( empty( $shortcodes ) ){
55 return '<!-- No shortcodes are defined -->';
56 }
57
58 $shortcode = self::find_shortcode( $atts, $shortcodes );
59
60 if( !is_array( $shortcode ) ){
61 return $shortcode;
62 }
63
64 $sc_content = $shortcode[ 'content' ];
65 $sc_settings = $shortcode[ 'settings' ];
66
67 if( !self::can_display( $shortcode ) ){
68 return '<!-- Shortcode does not match the conditions -->';
69 }
70
71 $sc_content = self::replace_sc_params( $sc_content, $atts );
72 $sc_content = self::replace_wp_params( $sc_content, $enclosed_content );
73 $sc_content = self::replace_custom_fields( $sc_content );
74 $sc_content = do_shortcode( $sc_content );
75
76 return $sc_content;
77
78 }
79
80 public static function get_shortcodes(){
81
82 if( !empty( self::$shortcodes ) ){
83 return self::$shortcodes;
84 }
85
86 $shortcodes = array();
87 $shortcode_posts = get_posts(array(
88 'post_type' => SC_POST_TYPE,
89 'posts_per_page' => -1,
90 'post_status' => 'publish'
91 ));
92
93 foreach( $shortcode_posts as $index => $post ){
94 $shortcodes[ $post->post_name ] = array(
95 'id' => $post->ID,
96 'content' => $post->post_content,
97 'settings' => self::get_sc_settings( $post->ID )
98 );
99 }
100
101 self::$shortcodes = $shortcodes;
102
103 return $shortcodes;
104
105 }
106
107 public static function default_sc_settings(){
108
109 return array(
110 '_sc_disable_sc' => 'no',
111 '_sc_disable_admin' => 'no',
112 '_sc_editor' => 'code',
113 '_sc_allowed_devices' => 'all'
114 );
115
116 }
117
118 public static function get_sc_settings( $post_id ){
119
120 $meta_vals = get_post_meta( $post_id, '', true );
121 $default_vals = self::default_sc_settings();
122 $settings = array();
123
124 if( !is_array( $meta_vals ) ){
125 return $default_vals;
126 }
127
128 foreach( $default_vals as $key => $val ){
129 $settings[ $key ] = array_key_exists( $key, $meta_vals ) ? $meta_vals[$key][0] : $val;
130 }
131
132 $settings[ '_sc_title' ] = get_the_title( $post_id );
133
134 return $settings;
135
136 }
137
138 public static function get_sc_tag( $post_id ){
139 $post = get_post( $post_id );
140 return '[sc name="' . $post->post_name . '"]';
141 }
142
143 public static function find_shortcode( $atts, $shortcodes ){
144
145 $sc_name = false;
146
147 // Find by shortcode ID
148 if( array_key_exists( 'sc_id', $atts ) ){
149 $sc_id = $atts[ 'sc_id' ];
150 foreach( $shortcodes as $temp_name => $temp_props ){
151 if( $temp_props[ 'id' ] == $sc_id ){
152 $sc_name = $temp_name;
153 break;
154 }
155 }
156 }
157
158 // If shortcode ID is not passed, then get the shortcode name
159 if( !$sc_name ){
160 if( !array_key_exists( 'name', $atts ) ){
161 return '<!-- Shortcode is missing "name" attribute -->';
162 }
163 $sc_name = $atts[ 'name' ];
164 }
165
166 // Check if the shortcode name exists
167 if( !array_key_exists( $sc_name, $shortcodes ) ){
168 $sc_name = sanitize_title_with_dashes( $sc_name );
169 if( !array_key_exists( $sc_name, $shortcodes ) ){
170 return '<!-- Shortcode does not exist -->';
171 }
172 }
173
174 return $shortcodes[ $sc_name ];
175
176 }
177
178 public static function can_display( $sc_props ){
179
180 $settings = $sc_props['settings'];
181
182 if( $settings[ '_sc_disable_sc' ] == 'yes' ){
183 return false;
184 }
185
186 $devices = $settings[ '_sc_allowed_devices' ];
187
188 if( $devices == 'mobile_only' && !wp_is_mobile() ){
189 return false;
190 }
191
192 if( $devices == 'desktop_only' && wp_is_mobile() ){
193 return false;
194 }
195
196 if( current_user_can( 'manage_options' ) && $settings[ '_sc_disable_admin' ] == 'yes' ){
197 return false;
198 }
199
200 return true;
201
202 }
203
204 public static function replace_sc_params( $content, $params ){
205
206 $params = array_change_key_case( $params, CASE_LOWER );
207
208 preg_match_all('/%%([a-zA-Z0-9_\-]+)\:?(.*?)%%/', $content, $matches);
209
210 $cp_tags = $matches[0];
211 $cp_names = $matches[1];
212 $cp_defaults = $matches[2];
213 $to_replace = array();
214
215 for( $i = 0; $i < count( $cp_names ); $i++ ){
216
217 $name = strtolower( $cp_names[ $i ] );
218 $default = $cp_defaults[ $i ];
219 $value = '';
220
221 if( array_key_exists( $name, $params ) ){
222 $value = $params[ $name ];
223
224 // Handle scenario when the attributes are added with paragraph tags by autop
225 if( substr( $value, 0, 4 ) == '</p>' ){
226 $value = substr( $value, 4 );
227 if( substr( $value, -3 ) == '<p>' ){
228 $value = substr( $value, 0, -3 );
229 }
230 }
231
232 }
233
234 if( empty( $value ) ){
235 array_push( $to_replace, $default );
236 }else{
237 array_push( $to_replace, $value );
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 = is_object( $post ) ? 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 'post_slug' => __( 'Post slug', 'shortcoder' ),
326
327 'site_name' => __( 'Site title', 'shortcoder' ),
328 'site_description' => __( 'Site description', 'shortcoder' ),
329 'site_url' => __( 'Site URL', 'shortcoder' ),
330 'site_wpurl' => __( 'WordPress URL', 'shortcoder' ),
331 'site_charset' => __( 'Site character set', 'shortcoder' ),
332 'wp_version' => __( 'WordPress version', 'shortcoder' ),
333 'stylesheet_url' => __( 'Active theme\'s stylesheet URL', 'shortcoder' ),
334 'stylesheet_directory' => __( 'Active theme\'s directory', 'shortcoder' ),
335 'atom_url' => __( 'Atom feed URL', 'shortcoder' ),
336 'rss_url' => __( 'RSS 2.0 feed URL', 'shortcoder' )
337 )
338 ),
339 'date_info' => array(
340 'name' => __( 'Date parameters', 'shortcoder' ),
341 'icon' => 'calendar-alt',
342 'params' => array(
343 'day' => __( 'Day', 'shortcoder' ),
344 'day_lz' => __( 'Day - leading zeros', 'shortcoder' ),
345 'day_ws' => __( 'Day - words - short form', 'shortcoder' ),
346 'day_wf' => __( 'Day - words - full form', 'shortcoder' ),
347 'month' => __( 'Month', 'shortcoder' ),
348 'month_lz' => __( 'Month - leading zeros', 'shortcoder' ),
349 'month_ws' => __( 'Month - words - short form', 'shortcoder' ),
350 'month_wf' => __( 'Month - words - full form', 'shortcoder' ),
351 'year' => __( 'Year', 'shortcoder' ),
352 'year_2d' => __( 'Year - 2 digit', 'shortcoder' ),
353 )
354 ),
355 'sc_cnt' => array(
356 'name' => __( 'Shortcode enclosed content', 'shortcoder' ),
357 'icon' => 'text',
358 'params' => array(
359 'enclosed_content' => __( 'Shortcode enclosed content', 'shortcoder' )
360 )
361 )
362 ));
363
364 }
365
366 public static function load_text_domain(){
367
368 load_plugin_textdomain( 'shortcoder', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
369
370 }
371
372 }
373
374 Shortcoder::init();
375
376 ?>