PluginProbe ʕ •ᴥ•ʔ
Shortcoder — Create Shortcodes for Anything / 4.3
Shortcoder — Create Shortcodes for Anything v4.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 7 years ago includes 7 years ago languages 7 years ago readme.txt 7 years ago shortcoder.php 7 years ago
shortcoder.php
330 lines
1 <?php
2 /*
3 Plugin Name: Shortcoder
4 Plugin URI: https://www.aakashweb.com/
5 Description: Shortcoder is a plugin which allows to create a custom shortcode and store HTML, JavaScript and other snippets in it. So if that shortcode is used in any post or pages, then the code stored in the shortcode get executed in that place. You can create a shortcode for Youtube videos, adsense ads, buttons and more.
6 Author: Aakash Chakravarthy
7 Version: 4.3
8 Author URI: https://www.aakashweb.com/
9 */
10
11 define( 'SC_VERSION', '4.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
17 class Shortcoder{
18
19 public static function init(){
20
21 add_action( 'plugins_loaded', array( __class__, 'load_text_domain' ) );
22
23 register_activation_hook( __FILE__, array( __class__, 'on_activate' ) );
24
25 add_filter( 'the_content', array( __class__, 'wp_44_workaround' ), 5 );
26
27 // Register the shortcode
28 add_shortcode( 'sc', array( __class__, 'execute_shortcode' ) );
29
30 // Include the required
31 self::includes();
32
33 }
34
35 public static function list_all(){
36
37 $shortcodes = get_option( 'shortcoder_data' );
38
39 return empty( $shortcodes ) ? array() : $shortcodes;
40
41 }
42
43 public static function includes(){
44
45 include_once( SC_PATH . 'includes/metadata.php' );
46 include_once( SC_PATH . 'includes/import.php' );
47 include_once( SC_PATH . 'admin/sc-admin.php' );
48
49 }
50
51 public static function execute_shortcode( $atts, $enc_content ) {
52
53 $shortcodes = self::list_all();
54
55 if( empty( $shortcodes ) ){
56 return '';
57 }
58
59 // Get the Shortcode name
60 if(isset($atts[0])){
61 $sc_name = str_replace(array('"', "'", ":"), '', $atts[0]);
62 unset($atts[0]);
63 }else{
64 // Old version with "name" param support
65 if(array_key_exists("name", $atts)){
66 $tVal = $atts['name'];
67 if(array_key_exists($tVal, $shortcodes)){
68 $sc_name = $tVal;
69 unset($atts['name']);
70 }
71 }
72 }
73
74 if(!isset($sc_name)){
75 return '';
76 }
77
78 // Check whether shortcoder can execute
79 if( self::check_conditions( $sc_name ) ){
80
81 $sc_content_final = '';
82
83 // If SC has parameters, then replace it
84 if( !empty( $atts ) ){
85
86 $keys = array();
87 $values = array();
88 $i = 0;
89
90 // Seperate key and value from atts
91 foreach( $atts as $k => $v ){
92 if( $k !== 0 ){
93 $keys[$i] = "%%" . $k . "%%";
94 $values[$i] = $v;
95 }
96 $i++;
97 }
98
99 // Replace the params
100 $sc_content = $shortcodes[ $sc_name ][ 'content' ];
101 $sc_content_rep1 = str_ireplace( $keys, $values, $sc_content );
102 $sc_content_final = preg_replace( '/%%[^%\s]+%%/', '', $sc_content_rep1 );
103
104 }
105 else{
106
107 // If the SC has no params, then replace the %%vars%%
108 $sc_content = $shortcodes[ $sc_name ][ 'content' ];
109 $sc_content_final = preg_replace( '/%%[^%\s]+%%/', '', $sc_content );
110
111 }
112
113 $sc_content_final = self::replace_wp_params( $sc_content_final, $enc_content );
114
115 $sc_content_final = self::replace_custom_fields( $sc_content_final );
116
117 return do_shortcode( $sc_content_final );
118
119 }else{
120 return '<!-- Shortcoder conditions not met -->';
121 }
122 }
123
124 public static function check_conditions( $name ){
125
126 $shortcodes = self::list_all();
127
128 if( array_key_exists( $name, $shortcodes ) ){
129
130 $sc = wp_parse_args( $shortcodes[ $name ], self::defaults() );
131
132 $devices = $sc[ 'devices' ];
133 if( $devices == 'mobile_only' && !wp_is_mobile() ){
134 return false;
135 }
136
137 if( $devices == 'desktop_only' && wp_is_mobile() ){
138 return false;
139 }
140
141 if( $sc[ 'disabled' ] == 0 ){
142 if( current_user_can( 'level_10' ) && $sc[ 'hide_admin' ] == 1 ){
143 return false;
144 }else{
145 return true;
146 }
147 }else{
148 return false;
149 }
150 }else{
151 return false;
152 }
153
154 }
155
156 public static function replace_wp_params( $content, $enc_content ){
157
158 $params = self::wp_params_list();
159 $all_params = array();
160
161 foreach( $params as $group => $group_info ){
162 $all_params = array_merge( $group_info[ 'params' ], $all_params );
163 }
164
165 $metadata = Shortcoder_Metadata::metadata();
166 $to_replace = array();
167
168 $metadata[ 'enclosed_content' ] = $enc_content;
169
170 foreach( $all_params as $id => $name ){
171 if( array_key_exists( $id, $metadata ) ){
172 $placeholder = '$$' . $id . '$$';
173 $to_replace[ $placeholder ] = $metadata[ $id ];
174 }
175 }
176
177 $content = strtr( $content, $to_replace );
178
179 return $content;
180
181 }
182
183 public static function replace_custom_fields( $content ){
184
185 global $post;
186
187 preg_match_all('/\$\$[^\s]+\$\$/', $content, $matches );
188
189 $cf_tags = $matches[0];
190
191 if( empty( $cf_tags ) ){
192 return $content;
193 }
194
195 foreach( $cf_tags as $tag ){
196
197 if( strpos( $tag, 'custom_field:' ) === false ){
198 continue;
199 }
200
201 preg_match( '/:[^\s\$]+/', $tag, $match );
202
203 if( empty( $match ) ){
204 continue;
205 }
206
207 $match = substr( $match[0], 1 );
208 $value = get_post_meta( $post->ID, $match, true );
209 $content = str_replace( $tag, $value, $content );
210
211 }
212
213 return $content;
214
215 }
216
217 public static function wp_params_list(){
218 return apply_filters( 'sc_mod_wp_params', array(
219 'wp_info' => array(
220 'name' => __( 'WordPress information', 'shortcoder' ),
221 'params' => array(
222 'url' => __( 'URL of the post/location', 'shortcoder' ),
223 'title' => __( 'Title of the post/location', 'shortcoder' ),
224 'short_url' => __( 'Short URL of the post/location', 'shortcoder' ),
225
226 'post_id' => __( 'Post ID', 'shortcoder' ),
227 'post_image' => __( 'Post featured image URL', 'shortcoder' ),
228 'post_excerpt' => __( 'Post excerpt', 'shortcoder' ),
229 'post_author' => __( 'Post author', 'shortcoder' ),
230 'post_date' => __( 'Post date', 'shortcoder' ),
231 'post_modified_date' => __( 'Post modified date', 'shortcoder' ),
232 'post_comments_count' => __( 'Post comments count', 'shortcoder' ),
233
234 'site_name' => __( 'Site title', 'shortcoder' ),
235 'site_description' => __( 'Site description', 'shortcoder' ),
236 'site_url' => __( 'Site URL', 'shortcoder' ),
237 'site_wpurl' => __( 'WordPress URL', 'shortcoder' ),
238 'site_charset' => __( 'Site character set', 'shortcoder' ),
239 'wp_version' => __( 'WordPress version', 'shortcoder' ),
240 'stylesheet_url' => __( 'Active theme\'s stylesheet URL', 'shortcoder' ),
241 'stylesheet_directory' => __( 'Active theme\'s directory', 'shortcoder' ),
242 'atom_url' => __( 'Atom feed URL', 'shortcoder' ),
243 'rss_url' => __( 'RSS 2.0 feed URL', 'shortcoder' )
244 )
245 ),
246 'date_info' => array(
247 'name' => __( 'Date parameters', 'shortcoder' ),
248 'params' => array(
249 'day' => __( 'Day', 'shortcoder' ),
250 'day_lz' => __( 'Day - leading zeros', 'shortcoder' ),
251 'day_ws' => __( 'Day - words - short form', 'shortcoder' ),
252 'day_wf' => __( 'Day - words - full form', 'shortcoder' ),
253 'month' => __( 'Month', 'shortcoder' ),
254 'month_lz' => __( 'Month - leading zeros', 'shortcoder' ),
255 'month_ws' => __( 'Month - words - short form', 'shortcoder' ),
256 'month_wf' => __( 'Month - words - full form', 'shortcoder' ),
257 'year' => __( 'Year', 'shortcoder' ),
258 'year_2d' => __( 'Year - 2 digit', 'shortcoder' ),
259 )
260 ),
261 'sc_cnt' => array(
262 'name' => __( 'Shortcode enclosed content', 'shortcoder' ),
263 'params' => array(
264 'enclosed_content' => __( 'Shortcode enclosed content', 'shortcoder' )
265 )
266 )
267 ));
268 }
269
270 public static function get_tags(){
271
272 $shortcodes = self::list_all();
273 $all_tags = array();
274
275 foreach( $shortcodes as $id => $opts ){
276 if( isset( $opts['tags'] ) && !empty( $opts['tags'] ) && is_array($opts['tags']) ){
277 $all_tags = array_merge( $all_tags, $opts['tags'] );
278 }
279 }
280
281 return array_unique($all_tags);
282
283 }
284
285 public static function on_activate(){
286
287 $shortcodes = self::list_all();
288 $sc_flags = get_option( 'shortcoder_flags' );
289
290 // Move the flag version fix to sc_flags option
291 if( isset( $shortcodes[ '_version_fix' ] ) ){
292 unset( $shortcodes['_version_fix'] );
293 update_option( 'shortcoder_data', $shortcodes );
294 }
295
296 $sc_flags[ 'version' ] = SC_VERSION;
297 update_option( 'shortcoder_flags', $sc_flags );
298
299 }
300
301 public static function defaults(){
302 return array(
303 'content' => '',
304 'disabled' => 0,
305 'hide_admin' => 0,
306 'devices' => 'all',
307 'editor' => 'text',
308 'tags' => array()
309 );
310 }
311
312 public static function can_edit_sc(){
313 return current_user_can( 'manage_options' );
314 }
315
316 public static function wp_44_workaround( $content ){
317 return str_replace( '[sc:', '[sc name=', $content );
318 }
319
320 public static function load_text_domain(){
321
322 load_plugin_textdomain( 'shortcoder', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
323
324 }
325
326 }
327
328 Shortcoder::init();
329
330 ?>