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