PluginProbe ʕ •ᴥ•ʔ
Shortcoder — Create Shortcodes for Anything / 4.1.2
Shortcoder — Create Shortcodes for Anything v4.1.2
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
226 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.2
8 Author URI: https://www.aakashweb.com/
9 */
10
11 define( 'SC_VERSION', '4.1.2' );
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',
175 'title' => 'Title of the post/location',
176 'short_url' => 'Short URL of the post/location',
177
178 'post_id' => 'Post ID',
179 'post_image' => 'Post featured image URL',
180 'post_excerpt' => 'Post excerpt',
181 'post_author' => 'Post author',
182 'post_date' => 'Post date',
183 'post_comments_count' => 'Post comments count'
184 ));
185 }
186
187 public static function on_activate(){
188
189 $shortcodes = self::list_all();
190 $sc_flags = get_option( 'shortcoder_flags' );
191
192 // Move the flag version fix to sc_flags option
193 if( isset( $shortcodes[ '_version_fix' ] ) ){
194 unset( $shortcodes['_version_fix'] );
195 update_option( 'shortcoder_data', $shortcodes );
196 }
197
198 $sc_flags[ 'version' ] = SC_VERSION;
199 update_option( 'shortcoder_flags', $sc_flags );
200
201 }
202
203 public static function defaults(){
204 return array(
205 'content' => '',
206 'disabled' => 0,
207 'hide_admin' => 0,
208 'devices' => 'all'
209 );
210 }
211
212 public static function wp_44_workaround( $content ){
213 return str_replace( '[sc:', '[sc name=', $content );
214 }
215
216 public static function load_text_domain(){
217
218 load_plugin_textdomain( 'shortcoder', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
219
220 }
221
222 }
223
224 Shortcoder::init();
225
226 ?>