PluginProbe ʕ •ᴥ•ʔ
Shortcoder — Create Shortcodes for Anything / 4.1.6
Shortcoder — Create Shortcodes for Anything v4.1.6
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
267 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.6
8 Author URI: https://www.aakashweb.com/
9 */
10
11 define( 'SC_VERSION', '4.1.6' );
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, $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 );
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 ){
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 foreach( $all_params as $id => $name ){
166 if( array_key_exists( $id, $metadata ) ){
167 $placeholder = '$$' . $id . '$$';
168 $to_replace[ $placeholder ] = $metadata[ $id ];
169 }
170 }
171
172 $content = strtr( $content, $to_replace );
173
174 return $content;
175
176 }
177
178 public static function wp_params_list(){
179 return apply_filters( 'sc_mod_wp_params', array(
180 'wp_info' => array(
181 'name' => __( 'WordPress information', 'shortcoder' ),
182 'params' => array(
183 'url' => __( 'URL of the post/location', 'shortcoder' ),
184 'title' => __( 'Title of the post/location', 'shortcoder' ),
185 'short_url' => __( 'Short URL of the post/location', 'shortcoder' ),
186
187 'post_id' => __( 'Post ID', 'shortcoder' ),
188 'post_image' => __( 'Post featured image URL', 'shortcoder' ),
189 'post_excerpt' => __( 'Post excerpt', 'shortcoder' ),
190 'post_author' => __( 'Post author', 'shortcoder' ),
191 'post_date' => __( 'Post date', 'shortcoder' ),
192 'post_comments_count' => __( 'Post comments count', 'shortcoder' ),
193
194 'site_name' => __( 'Site title', 'shortcoder' ),
195 'site_description' => __( 'Site description', 'shortcoder' ),
196 'site_url' => __( 'Site URL', 'shortcoder' ),
197 'site_wpurl' => __( 'WordPress URL', 'shortcoder' ),
198 'site_charset' => __( 'Site character set', 'shortcoder' ),
199 'wp_version' => __( 'WordPress version', 'shortcoder' ),
200 'stylesheet_url' => __( 'Active theme\'s stylesheet URL', 'shortcoder' ),
201 'stylesheet_directory' => __( 'Active theme\'s directory', 'shortcoder' ),
202 'atom_url' => __( 'Atom feed URL', 'shortcoder' ),
203 'rss_url' => __( 'RSS 2.0 feed URL', 'shortcoder' )
204 )
205 ),
206 'date_info' => array(
207 'name' => __( 'Date parameters', 'shortcoder' ),
208 'params' => array(
209 'day' => __( 'Day', 'shortcoder' ),
210 'day_lz' => __( 'Day - leading zeros', 'shortcoder' ),
211 'day_ws' => __( 'Day - words - short form', 'shortcoder' ),
212 'day_wf' => __( 'Day - words - full form', 'shortcoder' ),
213 'month' => __( 'Month', 'shortcoder' ),
214 'month_lz' => __( 'Month - leading zeros', 'shortcoder' ),
215 'month_ws' => __( 'Month - words - short form', 'shortcoder' ),
216 'month_wf' => __( 'Month - words - full form', 'shortcoder' ),
217 'year' => __( 'Year', 'shortcoder' ),
218 'year_2d' => __( 'Year - 2 digit', 'shortcoder' ),
219 )
220 )
221 ));
222 }
223
224 public static function on_activate(){
225
226 $shortcodes = self::list_all();
227 $sc_flags = get_option( 'shortcoder_flags' );
228
229 // Move the flag version fix to sc_flags option
230 if( isset( $shortcodes[ '_version_fix' ] ) ){
231 unset( $shortcodes['_version_fix'] );
232 update_option( 'shortcoder_data', $shortcodes );
233 }
234
235 $sc_flags[ 'version' ] = SC_VERSION;
236 update_option( 'shortcoder_flags', $sc_flags );
237
238 }
239
240 public static function defaults(){
241 return array(
242 'content' => '',
243 'disabled' => 0,
244 'hide_admin' => 0,
245 'devices' => 'all'
246 );
247 }
248
249 public static function can_edit_sc(){
250 return current_user_can( 'manage_options' );
251 }
252
253 public static function wp_44_workaround( $content ){
254 return str_replace( '[sc:', '[sc name=', $content );
255 }
256
257 public static function load_text_domain(){
258
259 load_plugin_textdomain( 'shortcoder', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
260
261 }
262
263 }
264
265 Shortcoder::init();
266
267 ?>