PluginProbe ʕ •ᴥ•ʔ
Shortcoder — Create Shortcodes for Anything / 4.1.7
Shortcoder — Create Shortcodes for Anything v4.1.7
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 8 years ago includes 8 years ago languages 8 years ago readme.txt 8 years ago shortcoder.php 8 years ago
shortcoder.php
292 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.1.7
8 Author URI: https://www.aakashweb.com/
9 */
10
11 define( 'SC_VERSION', '4.1.7' );
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 return '<!-- Start shortcoder -->' . do_shortcode( $sc_content_final ) . '<!-- End shortcoder v' . SC_VERSION . '-->';
115
116 }else{
117 return '<!-- Shortcoder conditions not met -->';
118 }
119 }
120
121 public static function check_conditions( $name ){
122
123 $shortcodes = self::list_all();
124
125 if( array_key_exists( $name, $shortcodes ) ){
126
127 $sc = wp_parse_args( $shortcodes[ $name ], self::defaults() );
128
129 $devices = $sc[ 'devices' ];
130 if( $devices == 'mobile_only' && !wp_is_mobile() ){
131 return false;
132 }
133
134 if( $devices == 'desktop_only' && wp_is_mobile() ){
135 return false;
136 }
137
138 if( $sc[ 'disabled' ] == 0 ){
139 if( current_user_can( 'level_10' ) && $sc[ 'hide_admin' ] == 1 ){
140 return false;
141 }else{
142 return true;
143 }
144 }else{
145 return false;
146 }
147 }else{
148 return false;
149 }
150
151 }
152
153 public static function replace_wp_params( $content, $enc_content ){
154
155 $params = self::wp_params_list();
156 $all_params = array();
157
158 foreach( $params as $group => $group_info ){
159 $all_params = array_merge( $group_info[ 'params' ], $all_params );
160 }
161
162 $metadata = Shortcoder_Metadata::metadata();
163 $to_replace = array();
164
165 $metadata[ 'enclosed_content' ] = $enc_content;
166
167 foreach( $all_params as $id => $name ){
168 if( array_key_exists( $id, $metadata ) ){
169 $placeholder = '$$' . $id . '$$';
170 $to_replace[ $placeholder ] = $metadata[ $id ];
171 }
172 }
173
174 $content = strtr( $content, $to_replace );
175
176 return $content;
177
178 }
179
180 public static function wp_params_list(){
181 return apply_filters( 'sc_mod_wp_params', array(
182 'wp_info' => array(
183 'name' => __( 'WordPress information', 'shortcoder' ),
184 'params' => array(
185 'url' => __( 'URL of the post/location', 'shortcoder' ),
186 'title' => __( 'Title of the post/location', 'shortcoder' ),
187 'short_url' => __( 'Short URL of the post/location', 'shortcoder' ),
188
189 'post_id' => __( 'Post ID', 'shortcoder' ),
190 'post_image' => __( 'Post featured image URL', 'shortcoder' ),
191 'post_excerpt' => __( 'Post excerpt', 'shortcoder' ),
192 'post_author' => __( 'Post author', 'shortcoder' ),
193 'post_date' => __( 'Post date', 'shortcoder' ),
194 'post_comments_count' => __( 'Post comments count', 'shortcoder' ),
195
196 'site_name' => __( 'Site title', 'shortcoder' ),
197 'site_description' => __( 'Site description', 'shortcoder' ),
198 'site_url' => __( 'Site URL', 'shortcoder' ),
199 'site_wpurl' => __( 'WordPress URL', 'shortcoder' ),
200 'site_charset' => __( 'Site character set', 'shortcoder' ),
201 'wp_version' => __( 'WordPress version', 'shortcoder' ),
202 'stylesheet_url' => __( 'Active theme\'s stylesheet URL', 'shortcoder' ),
203 'stylesheet_directory' => __( 'Active theme\'s directory', 'shortcoder' ),
204 'atom_url' => __( 'Atom feed URL', 'shortcoder' ),
205 'rss_url' => __( 'RSS 2.0 feed URL', 'shortcoder' )
206 )
207 ),
208 'date_info' => array(
209 'name' => __( 'Date parameters', 'shortcoder' ),
210 'params' => array(
211 'day' => __( 'Day', 'shortcoder' ),
212 'day_lz' => __( 'Day - leading zeros', 'shortcoder' ),
213 'day_ws' => __( 'Day - words - short form', 'shortcoder' ),
214 'day_wf' => __( 'Day - words - full form', 'shortcoder' ),
215 'month' => __( 'Month', 'shortcoder' ),
216 'month_lz' => __( 'Month - leading zeros', 'shortcoder' ),
217 'month_ws' => __( 'Month - words - short form', 'shortcoder' ),
218 'month_wf' => __( 'Month - words - full form', 'shortcoder' ),
219 'year' => __( 'Year', 'shortcoder' ),
220 'year_2d' => __( 'Year - 2 digit', 'shortcoder' ),
221 )
222 ),
223 'sc_cnt' => array(
224 'name' => __( 'Shortcode enclosed content', 'shortcoder' ),
225 'params' => array(
226 'enclosed_content' => __( 'Shortcode enclosed content', 'shortcoder' )
227 )
228 )
229 ));
230 }
231
232 public static function get_tags(){
233
234 $shortcodes = self::list_all();
235 $all_tags = array();
236
237 foreach( $shortcodes as $id => $opts ){
238 if( isset( $opts['tags'] ) && !empty( $opts['tags'] ) && is_array($opts['tags']) ){
239 $all_tags = array_merge( $all_tags, $opts['tags'] );
240 }
241 }
242
243 return array_unique($all_tags);
244
245 }
246
247 public static function on_activate(){
248
249 $shortcodes = self::list_all();
250 $sc_flags = get_option( 'shortcoder_flags' );
251
252 // Move the flag version fix to sc_flags option
253 if( isset( $shortcodes[ '_version_fix' ] ) ){
254 unset( $shortcodes['_version_fix'] );
255 update_option( 'shortcoder_data', $shortcodes );
256 }
257
258 $sc_flags[ 'version' ] = SC_VERSION;
259 update_option( 'shortcoder_flags', $sc_flags );
260
261 }
262
263 public static function defaults(){
264 return array(
265 'content' => '',
266 'disabled' => 0,
267 'hide_admin' => 0,
268 'devices' => 'all',
269 'editor' => 'text',
270 'tags' => array()
271 );
272 }
273
274 public static function can_edit_sc(){
275 return current_user_can( 'manage_options' );
276 }
277
278 public static function wp_44_workaround( $content ){
279 return str_replace( '[sc:', '[sc name=', $content );
280 }
281
282 public static function load_text_domain(){
283
284 load_plugin_textdomain( 'shortcoder', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
285
286 }
287
288 }
289
290 Shortcoder::init();
291
292 ?>