PluginProbe ʕ •ᴥ•ʔ
Shortcoder — Create Shortcodes for Anything / 5.0.3
Shortcoder — Create Shortcodes for Anything v5.0.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
344 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.0.3
8 Author URI: https://www.aakashweb.com/
9 */
10
11 define( 'SC_VERSION', '5.0.3' );
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 foreach( $default_vals as $key => $val ){
122 $settings[ $key ] = array_key_exists( $key, $meta_vals ) ? $meta_vals[$key][0] : $val;
123 }
124
125 $settings[ '_sc_title' ] = get_the_title( $post_id );
126
127 return $settings;
128
129 }
130
131 public static function get_sc_tag( $post_id ){
132 $post = get_post( $post_id );
133 return '[sc name="' . $post->post_name . '"]';
134 }
135
136 public static function find_shortcode( $atts, $shortcodes ){
137
138 $sc_name = false;
139
140 // Find by shortcode ID
141 if( array_key_exists( 'sc_id', $atts ) ){
142 $sc_id = $atts[ 'sc_id' ];
143 foreach( $shortcodes as $temp_name => $temp_props ){
144 if( $temp_props[ 'id' ] == $sc_id ){
145 $sc_name = $temp_name;
146 break;
147 }
148 }
149 }
150
151 // If shortcode ID is not passed, then get the shortcode name
152 if( !$sc_name ){
153 if( !array_key_exists( 'name', $atts ) ){
154 return '<!-- Shortcode is missing "name" attribute -->';
155 }
156 $sc_name = $atts[ 'name' ];
157 }
158
159 // Check if the shortcode name exists
160 if( !array_key_exists( $sc_name, $shortcodes ) ){
161 $sc_name = sanitize_title_with_dashes( $sc_name );
162 if( !array_key_exists( $sc_name, $shortcodes ) ){
163 return '<!-- Shortcode does not exist -->';
164 }
165 }
166
167 return $shortcodes[ $sc_name ];
168
169 }
170
171 public static function can_display( $sc_props ){
172
173 $settings = $sc_props['settings'];
174
175 if( $settings[ '_sc_disable_sc' ] == 'yes' ){
176 return false;
177 }
178
179 $devices = $settings[ '_sc_allowed_devices' ];
180
181 if( $devices == 'mobile_only' && !wp_is_mobile() ){
182 return false;
183 }
184
185 if( $devices == 'desktop_only' && wp_is_mobile() ){
186 return false;
187 }
188
189 if( current_user_can( 'manage_options' ) && $settings[ '_sc_disable_admin' ] == 'yes' ){
190 return false;
191 }
192
193 return true;
194
195 }
196
197 public static function replace_sc_params( $content, $params ){
198
199 $keys = array();
200 $values = array();
201
202 foreach( $params as $key => $value ){
203 array_push( $keys, '%%' . $key . '%%' );
204 array_push( $values, $value );
205 }
206
207 // Replace params which are passed
208 $content = str_ireplace( $keys, $values, $content );
209
210 // Replace params which are not passed
211 $content = preg_replace( '/%%[^%\s]+%%/', '', $content );
212
213 return $content;
214
215 }
216
217 public static function replace_wp_params( $content, $enc_content = null ){
218
219 $params = self::wp_params_list();
220 $metadata = Shortcoder_Metadata::metadata();
221 $metadata[ 'enclosed_content' ] = $enc_content;
222 $all_params = array();
223 $to_replace = array();
224
225 foreach( $params as $group => $group_info ){
226 $all_params = array_merge( $group_info[ 'params' ], $all_params );
227 }
228
229 foreach( $all_params as $id => $name ){
230 if( array_key_exists( $id, $metadata ) ){
231 $placeholder = '$$' . $id . '$$';
232 $to_replace[ $placeholder ] = $metadata[ $id ];
233 }
234 }
235
236 $content = strtr( $content, $to_replace );
237
238 return $content;
239
240 }
241
242 public static function replace_custom_fields( $content ){
243
244 global $post;
245
246 preg_match_all('/\$\$[^\s]+\$\$/', $content, $matches );
247
248 $cf_tags = $matches[0];
249
250 if( empty( $cf_tags ) ){
251 return $content;
252 }
253
254 foreach( $cf_tags as $tag ){
255
256 if( strpos( $tag, 'custom_field:' ) === false ){
257 continue;
258 }
259
260 preg_match( '/:[^\s\$]+/', $tag, $match );
261
262 if( empty( $match ) ){
263 continue;
264 }
265
266 $match = substr( $match[0], 1 );
267 $value = get_post_meta( $post->ID, $match, true );
268 $content = str_replace( $tag, $value, $content );
269
270 }
271
272 return $content;
273
274 }
275
276 public static function wp_params_list(){
277
278 return apply_filters( 'sc_mod_wp_params', array(
279 'wp_info' => array(
280 'name' => __( 'WordPress information', 'shortcoder' ),
281 'icon' => 'wordpress-alt',
282 'params' => array(
283 'url' => __( 'URL of the post/location', 'shortcoder' ),
284 'title' => __( 'Title of the post/location', 'shortcoder' ),
285 'short_url' => __( 'Short URL of the post/location', 'shortcoder' ),
286
287 'post_id' => __( 'Post ID', 'shortcoder' ),
288 'post_image' => __( 'Post featured image URL', 'shortcoder' ),
289 'post_excerpt' => __( 'Post excerpt', 'shortcoder' ),
290 'post_author' => __( 'Post author', 'shortcoder' ),
291 'post_date' => __( 'Post date', 'shortcoder' ),
292 'post_modified_date' => __( 'Post modified date', 'shortcoder' ),
293 'post_comments_count' => __( 'Post comments count', 'shortcoder' ),
294
295 'site_name' => __( 'Site title', 'shortcoder' ),
296 'site_description' => __( 'Site description', 'shortcoder' ),
297 'site_url' => __( 'Site URL', 'shortcoder' ),
298 'site_wpurl' => __( 'WordPress URL', 'shortcoder' ),
299 'site_charset' => __( 'Site character set', 'shortcoder' ),
300 'wp_version' => __( 'WordPress version', 'shortcoder' ),
301 'stylesheet_url' => __( 'Active theme\'s stylesheet URL', 'shortcoder' ),
302 'stylesheet_directory' => __( 'Active theme\'s directory', 'shortcoder' ),
303 'atom_url' => __( 'Atom feed URL', 'shortcoder' ),
304 'rss_url' => __( 'RSS 2.0 feed URL', 'shortcoder' )
305 )
306 ),
307 'date_info' => array(
308 'name' => __( 'Date parameters', 'shortcoder' ),
309 'icon' => 'calendar-alt',
310 'params' => array(
311 'day' => __( 'Day', 'shortcoder' ),
312 'day_lz' => __( 'Day - leading zeros', 'shortcoder' ),
313 'day_ws' => __( 'Day - words - short form', 'shortcoder' ),
314 'day_wf' => __( 'Day - words - full form', 'shortcoder' ),
315 'month' => __( 'Month', 'shortcoder' ),
316 'month_lz' => __( 'Month - leading zeros', 'shortcoder' ),
317 'month_ws' => __( 'Month - words - short form', 'shortcoder' ),
318 'month_wf' => __( 'Month - words - full form', 'shortcoder' ),
319 'year' => __( 'Year', 'shortcoder' ),
320 'year_2d' => __( 'Year - 2 digit', 'shortcoder' ),
321 )
322 ),
323 'sc_cnt' => array(
324 'name' => __( 'Shortcode enclosed content', 'shortcoder' ),
325 'icon' => 'text',
326 'params' => array(
327 'enclosed_content' => __( 'Shortcode enclosed content', 'shortcoder' )
328 )
329 )
330 ));
331
332 }
333
334 public static function load_text_domain(){
335
336 load_plugin_textdomain( 'shortcoder', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
337
338 }
339
340 }
341
342 Shortcoder::init();
343
344 ?>