PluginProbe ʕ •ᴥ•ʔ
Shortcoder — Create Shortcodes for Anything / 4.1.5
Shortcoder — Create Shortcodes for Anything v4.1.5
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
242 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.5
8 Author URI: https://www.aakashweb.com/
9 */
10
11 define( 'SC_VERSION', '4.1.5' );
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 $metadata = Shortcoder_Metadata::metadata();
157 $to_replace = array();
158
159 foreach( $params as $id => $name ){
160 if( array_key_exists( $id, $metadata ) ){
161 $placeholder = '$$' . $id . '$$';
162 $to_replace[ $placeholder ] = $metadata[ $id ];
163 }
164 }
165
166 $content = strtr( $content, $to_replace );
167
168 return $content;
169
170 }
171
172 public static function wp_params_list(){
173 return apply_filters( 'sc_mod_wp_params', array(
174 'url' => __( 'URL of the post/location', 'shortcoder' ),
175 'title' => __( 'Title of the post/location', 'shortcoder' ),
176 'short_url' => __( 'Short URL of the post/location', 'shortcoder' ),
177
178 'post_id' => __( 'Post ID', 'shortcoder' ),
179 'post_image' => __( 'Post featured image URL', 'shortcoder' ),
180 'post_excerpt' => __( 'Post excerpt', 'shortcoder' ),
181 'post_author' => __( 'Post author', 'shortcoder' ),
182 'post_date' => __( 'Post date', 'shortcoder' ),
183 'post_comments_count' => __( 'Post comments count', 'shortcoder' ),
184
185 'site_name' => __( 'Site title', 'shortcoder' ),
186 'site_description' => __( 'Site description', 'shortcoder' ),
187 'site_url' => __( 'Site URL', 'shortcoder' ),
188 'site_wpurl' => __( 'WordPress URL', 'shortcoder' ),
189 'site_charset' => __( 'Site character set', 'shortcoder' ),
190 'wp_version' => __( 'WordPress version', 'shortcoder' ),
191 'stylesheet_url' => __( 'Active theme\'s stylesheet URL', 'shortcoder' ),
192 'stylesheet_directory' => __( 'Active theme\'s directory', 'shortcoder' ),
193 'atom_url' => __( 'Atom feed URL', 'shortcoder' ),
194 'rss_url' => __( 'RSS 2.0 feed URL', 'shortcoder' )
195
196 ));
197 }
198
199 public static function on_activate(){
200
201 $shortcodes = self::list_all();
202 $sc_flags = get_option( 'shortcoder_flags' );
203
204 // Move the flag version fix to sc_flags option
205 if( isset( $shortcodes[ '_version_fix' ] ) ){
206 unset( $shortcodes['_version_fix'] );
207 update_option( 'shortcoder_data', $shortcodes );
208 }
209
210 $sc_flags[ 'version' ] = SC_VERSION;
211 update_option( 'shortcoder_flags', $sc_flags );
212
213 }
214
215 public static function defaults(){
216 return array(
217 'content' => '',
218 'disabled' => 0,
219 'hide_admin' => 0,
220 'devices' => 'all'
221 );
222 }
223
224 public static function can_edit_sc(){
225 return current_user_can( 'manage_options' );
226 }
227
228 public static function wp_44_workaround( $content ){
229 return str_replace( '[sc:', '[sc name=', $content );
230 }
231
232 public static function load_text_domain(){
233
234 load_plugin_textdomain( 'shortcoder', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
235
236 }
237
238 }
239
240 Shortcoder::init();
241
242 ?>