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