shortcoder
Last commit date
admin
2 years ago
includes
2 years ago
languages
2 years ago
readme.txt
2 years ago
shortcoder.php
2 years ago
shortcoder.php
426 lines
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: Shortcoder |
| 4 | Plugin URI: https://www.aakashweb.com/wordpress-plugins/shortcoder/ |
| 5 | Description: Shortcoder plugin allows to create a custom shortcodes for HTML, JavaScript and other snippets. Now the shortcodes can be used in posts/pages and the snippet will be replaced in place. |
| 6 | Author: Aakash Chakravarthy |
| 7 | Version: 6.3.1 |
| 8 | Author URI: https://www.aakashweb.com/ |
| 9 | Text Domain: shortcoder |
| 10 | Domain Path: /languages |
| 11 | */ |
| 12 | |
| 13 | define( 'SC_VERSION', '6.3.1' ); |
| 14 | define( 'SC_PATH', plugin_dir_path( __FILE__ ) ); // All have trailing slash |
| 15 | define( 'SC_URL', plugin_dir_url( __FILE__ ) ); |
| 16 | define( 'SC_ADMIN_URL', trailingslashit( plugin_dir_url( __FILE__ ) . 'admin' ) ); |
| 17 | define( 'SC_BASE_NAME', plugin_basename( __FILE__ ) ); |
| 18 | define( 'SC_POST_TYPE', 'shortcoder' ); |
| 19 | |
| 20 | // error_reporting(E_ALL); |
| 21 | |
| 22 | final class Shortcoder{ |
| 23 | |
| 24 | static public $shortcodes = array(); |
| 25 | |
| 26 | static public $current_shortcode = false; |
| 27 | |
| 28 | public static function init(){ |
| 29 | |
| 30 | // Include the required |
| 31 | self::includes(); |
| 32 | |
| 33 | add_shortcode( 'sc', array( __CLASS__, 'execute_shortcode' ) ); |
| 34 | |
| 35 | } |
| 36 | |
| 37 | public static function includes(){ |
| 38 | |
| 39 | include_once( SC_PATH . 'includes/updates.php' ); |
| 40 | include_once( SC_PATH . 'includes/metadata.php' ); |
| 41 | include_once( SC_PATH . 'admin/admin.php' ); |
| 42 | include_once( SC_PATH . 'admin/form.php' ); |
| 43 | include_once( SC_PATH . 'admin/edit.php' ); |
| 44 | include_once( SC_PATH . 'admin/settings.php' ); |
| 45 | include_once( SC_PATH . 'admin/manage.php' ); |
| 46 | include_once( SC_PATH . 'admin/tools.php' ); |
| 47 | |
| 48 | } |
| 49 | |
| 50 | public static function execute_shortcode( $atts, $enclosed_content = null ){ |
| 51 | |
| 52 | $atts = (array) $atts; |
| 53 | $shortcodes = self::get_shortcodes(); |
| 54 | |
| 55 | if( empty( $shortcodes ) ){ |
| 56 | return '<!-- No shortcodes are defined -->'; |
| 57 | } |
| 58 | |
| 59 | $shortcode = self::find_shortcode( $atts, $shortcodes ); |
| 60 | |
| 61 | $shortcode = apply_filters( 'sc_mod_shortcode', $shortcode, $atts, $enclosed_content ); |
| 62 | do_action( 'sc_do_before', $shortcode, $atts ); |
| 63 | |
| 64 | if( !is_array( $shortcode ) ){ |
| 65 | return $shortcode; |
| 66 | } |
| 67 | |
| 68 | // Prevent same shortcode nested loop |
| 69 | if( self::$current_shortcode == $shortcode[ 'name' ] ){ |
| 70 | return ''; |
| 71 | } |
| 72 | self::$current_shortcode = $shortcode[ 'name' ]; |
| 73 | |
| 74 | $sc_content = $shortcode[ 'content' ]; |
| 75 | $sc_settings = $shortcode[ 'settings' ]; |
| 76 | |
| 77 | if( !self::can_display( $shortcode ) ){ |
| 78 | $sc_content = sprintf( '<!-- Shortcode [%s] does not match the conditions -->', self::$current_shortcode ); |
| 79 | }else{ |
| 80 | $sc_content = self::replace_sc_params( $sc_content, $atts ); |
| 81 | $sc_content = self::replace_wp_params( $sc_content, $enclosed_content ); |
| 82 | $sc_content = self::replace_custom_fields( $sc_content ); |
| 83 | $sc_content = do_shortcode( $sc_content ); |
| 84 | } |
| 85 | |
| 86 | $sc_content = apply_filters( 'sc_mod_output', $sc_content, $atts, $sc_settings, $enclosed_content ); |
| 87 | do_action( 'sc_do_after', $shortcode, $atts ); |
| 88 | |
| 89 | self::$current_shortcode = false; |
| 90 | |
| 91 | return $sc_content; |
| 92 | |
| 93 | } |
| 94 | |
| 95 | public static function get_shortcodes(){ |
| 96 | |
| 97 | if( !empty( self::$shortcodes ) ){ |
| 98 | return self::$shortcodes; |
| 99 | } |
| 100 | |
| 101 | $shortcodes = array(); |
| 102 | $shortcode_posts = get_posts(array( |
| 103 | 'post_type' => SC_POST_TYPE, |
| 104 | 'posts_per_page' => -1, |
| 105 | 'post_status' => 'publish' |
| 106 | )); |
| 107 | |
| 108 | foreach( $shortcode_posts as $index => $post ){ |
| 109 | $shortcodes[ $post->post_name ] = array( |
| 110 | 'id' => $post->ID, |
| 111 | 'name' => $post->post_name, |
| 112 | 'content' => $post->post_content, |
| 113 | 'settings' => self::get_sc_settings( $post->ID ) |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | self::$shortcodes = $shortcodes; |
| 118 | |
| 119 | return $shortcodes; |
| 120 | |
| 121 | } |
| 122 | |
| 123 | public static function default_sc_settings(){ |
| 124 | |
| 125 | return apply_filters( 'sc_mod_sc_settings', array( |
| 126 | '_sc_description' => '', |
| 127 | '_sc_disable_sc' => 'no', |
| 128 | '_sc_disable_admin' => 'no', |
| 129 | '_sc_editor' => '', |
| 130 | '_sc_allowed_devices' => 'all' |
| 131 | )); |
| 132 | |
| 133 | } |
| 134 | |
| 135 | public static function default_settings(){ |
| 136 | |
| 137 | return apply_filters( 'sc_mod_settings', array( |
| 138 | 'default_editor' => 'code', |
| 139 | 'default_content' => '', |
| 140 | 'list_content' => 'no' |
| 141 | )); |
| 142 | |
| 143 | } |
| 144 | |
| 145 | public static function get_settings(){ |
| 146 | |
| 147 | $settings = get_option( 'sc_settings', array() ); |
| 148 | $default_settings = self::default_settings(); |
| 149 | |
| 150 | return self::set_defaults( $settings, $default_settings ); |
| 151 | |
| 152 | } |
| 153 | |
| 154 | public static function get_sc_settings( $post_id ){ |
| 155 | |
| 156 | $meta_vals = get_post_meta( $post_id, '', true ); |
| 157 | $default_vals = self::default_sc_settings(); |
| 158 | $settings = array(); |
| 159 | |
| 160 | if( !is_array( $meta_vals ) ){ |
| 161 | return $default_vals; |
| 162 | } |
| 163 | |
| 164 | foreach( $default_vals as $key => $val ){ |
| 165 | $settings[ $key ] = array_key_exists( $key, $meta_vals ) ? $meta_vals[$key][0] : $val; |
| 166 | } |
| 167 | |
| 168 | $settings[ '_sc_title' ] = get_the_title( $post_id ); |
| 169 | |
| 170 | return $settings; |
| 171 | |
| 172 | } |
| 173 | |
| 174 | public static function get_sc_tag( $post_id ){ |
| 175 | $post = get_post( $post_id ); |
| 176 | return '[sc name="' . $post->post_name . '"][/sc]'; |
| 177 | } |
| 178 | |
| 179 | public static function find_shortcode( $atts, $shortcodes ){ |
| 180 | |
| 181 | $sc_name = false; |
| 182 | |
| 183 | // Find by shortcode ID |
| 184 | if( array_key_exists( 'sc_id', $atts ) ){ |
| 185 | $sc_id = $atts[ 'sc_id' ]; |
| 186 | foreach( $shortcodes as $temp_name => $temp_props ){ |
| 187 | if( $temp_props[ 'id' ] == $sc_id ){ |
| 188 | $sc_name = $temp_name; |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // If shortcode ID is not passed, then get the shortcode name |
| 195 | if( !$sc_name ){ |
| 196 | if( !array_key_exists( 'name', $atts ) ){ |
| 197 | return '<!-- Shortcode is missing "name" attribute -->'; |
| 198 | } |
| 199 | $sc_name = $atts[ 'name' ]; |
| 200 | } |
| 201 | |
| 202 | // Check if the shortcode name exists |
| 203 | if( !array_key_exists( $sc_name, $shortcodes ) ){ |
| 204 | $sc_name = sanitize_title_with_dashes( $sc_name ); |
| 205 | if( !array_key_exists( $sc_name, $shortcodes ) ){ |
| 206 | return sprintf( '<!-- Shortcode [%s] does not exist -->', $sc_name ); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return $shortcodes[ $sc_name ]; |
| 211 | |
| 212 | } |
| 213 | |
| 214 | public static function can_display( $sc_props ){ |
| 215 | |
| 216 | $settings = $sc_props['settings']; |
| 217 | |
| 218 | if( $settings[ '_sc_disable_sc' ] == 'yes' ){ |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | $devices = $settings[ '_sc_allowed_devices' ]; |
| 223 | |
| 224 | if( $devices == 'mobile_only' && !wp_is_mobile() ){ |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | if( $devices == 'desktop_only' && wp_is_mobile() ){ |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | if( current_user_can( 'manage_options' ) && $settings[ '_sc_disable_admin' ] == 'yes' ){ |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | return true; |
| 237 | |
| 238 | } |
| 239 | |
| 240 | public static function replace_sc_params( $content, $params ){ |
| 241 | |
| 242 | $params = array_change_key_case( $params, CASE_LOWER ); |
| 243 | |
| 244 | preg_match_all('/%%([a-zA-Z0-9_\-]+)\:?(.*?)%%/', $content, $matches); |
| 245 | |
| 246 | $cp_tags = $matches[0]; |
| 247 | $cp_names = $matches[1]; |
| 248 | $cp_defaults = $matches[2]; |
| 249 | $to_replace = array(); |
| 250 | |
| 251 | for( $i = 0; $i < count( $cp_names ); $i++ ){ |
| 252 | |
| 253 | $name = strtolower( $cp_names[ $i ] ); |
| 254 | $default = $cp_defaults[ $i ]; |
| 255 | $value = ''; |
| 256 | |
| 257 | if( array_key_exists( $name, $params ) ){ |
| 258 | $value = $params[ $name ]; |
| 259 | |
| 260 | // Handle scenario when the attributes are added with paragraph tags by autop |
| 261 | if( substr( $value, 0, 4 ) == '</p>' ){ |
| 262 | $value = substr( $value, 4 ); |
| 263 | if( substr( $value, -3 ) == '<p>' ){ |
| 264 | $value = substr( $value, 0, -3 ); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | } |
| 269 | |
| 270 | if( $value == '' ){ |
| 271 | array_push( $to_replace, $default ); |
| 272 | }else{ |
| 273 | array_push( $to_replace, $value ); |
| 274 | } |
| 275 | |
| 276 | } |
| 277 | |
| 278 | $content = str_ireplace( $cp_tags, $to_replace, $content ); |
| 279 | |
| 280 | return $content; |
| 281 | |
| 282 | } |
| 283 | |
| 284 | public static function replace_wp_params( $content, $enc_content = null ){ |
| 285 | |
| 286 | $params = self::wp_params_list(); |
| 287 | $metadata = Shortcoder_Metadata::metadata(); |
| 288 | $metadata[ 'enclosed_content' ] = $enc_content; |
| 289 | $all_params = array(); |
| 290 | $to_replace = array(); |
| 291 | |
| 292 | foreach( $params as $group => $group_info ){ |
| 293 | $all_params = array_merge( $group_info[ 'params' ], $all_params ); |
| 294 | } |
| 295 | |
| 296 | foreach( $all_params as $id => $name ){ |
| 297 | if( array_key_exists( $id, $metadata ) ){ |
| 298 | $placeholder = '$$' . $id . '$$'; |
| 299 | $to_replace[ $placeholder ] = $metadata[ $id ]; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | $content = strtr( $content, $to_replace ); |
| 304 | |
| 305 | return $content; |
| 306 | |
| 307 | } |
| 308 | |
| 309 | public static function replace_custom_fields( $content ){ |
| 310 | |
| 311 | global $post; |
| 312 | |
| 313 | preg_match_all('/\$\$custom_field\:(.*?)\$\$/', $content, $matches ); |
| 314 | |
| 315 | if( empty( $matches[0] ) ){ |
| 316 | return $content; |
| 317 | } |
| 318 | |
| 319 | $cf_tags = $matches[1]; |
| 320 | |
| 321 | foreach( $cf_tags as $cf_tag ){ |
| 322 | |
| 323 | $cf_data = explode( ':', $cf_tag, 2 ); |
| 324 | $cf_name = trim( $cf_data[0] ); |
| 325 | $cf_default_val = count( $cf_data ) == 2 ? trim( $cf_data[1] ) : ''; |
| 326 | |
| 327 | if( empty( $cf_name ) ){ |
| 328 | continue; |
| 329 | } |
| 330 | |
| 331 | if( is_object( $post ) ){ |
| 332 | $cf_actual_val = get_post_meta( $post->ID, $cf_name, true ); |
| 333 | $value = $cf_actual_val == '' ? $cf_default_val : $cf_actual_val; |
| 334 | }else{ |
| 335 | $value = $cf_default_val; |
| 336 | } |
| 337 | |
| 338 | $full_tag = '$$custom_field:' . $cf_tag . '$$'; |
| 339 | $content = str_replace( $full_tag, $value, $content ); |
| 340 | |
| 341 | } |
| 342 | |
| 343 | return $content; |
| 344 | |
| 345 | } |
| 346 | |
| 347 | public static function wp_params_list(){ |
| 348 | |
| 349 | return apply_filters( 'sc_mod_wp_params', array( |
| 350 | 'wp_info' => array( |
| 351 | 'name' => __( 'WordPress information', 'shortcoder' ), |
| 352 | 'icon' => 'wordpress-alt', |
| 353 | 'params' => array( |
| 354 | 'url' => __( 'URL of the post/location', 'shortcoder' ), |
| 355 | 'title' => __( 'Title of the post/location', 'shortcoder' ), |
| 356 | 'short_url' => __( 'Short URL of the post/location', 'shortcoder' ), |
| 357 | |
| 358 | 'post_id' => __( 'Post ID', 'shortcoder' ), |
| 359 | 'post_image' => __( 'Post featured image URL', 'shortcoder' ), |
| 360 | 'post_excerpt' => __( 'Post excerpt', 'shortcoder' ), |
| 361 | 'post_author' => __( 'Post author', 'shortcoder' ), |
| 362 | 'post_date' => __( 'Post date', 'shortcoder' ), |
| 363 | 'post_modified_date' => __( 'Post modified date', 'shortcoder' ), |
| 364 | 'post_comments_count' => __( 'Post comments count', 'shortcoder' ), |
| 365 | 'post_slug' => __( 'Post slug', 'shortcoder' ), |
| 366 | |
| 367 | 'site_name' => __( 'Site title', 'shortcoder' ), |
| 368 | 'site_description' => __( 'Site description', 'shortcoder' ), |
| 369 | 'site_url' => __( 'Site URL', 'shortcoder' ), |
| 370 | 'site_wpurl' => __( 'WordPress URL', 'shortcoder' ), |
| 371 | 'site_charset' => __( 'Site character set', 'shortcoder' ), |
| 372 | 'wp_version' => __( 'WordPress version', 'shortcoder' ), |
| 373 | 'stylesheet_url' => __( 'Active theme\'s stylesheet URL', 'shortcoder' ), |
| 374 | 'stylesheet_directory' => __( 'Active theme\'s directory', 'shortcoder' ), |
| 375 | 'atom_url' => __( 'Atom feed URL', 'shortcoder' ), |
| 376 | 'rss_url' => __( 'RSS 2.0 feed URL', 'shortcoder' ) |
| 377 | ) |
| 378 | ), |
| 379 | 'date_info' => array( |
| 380 | 'name' => __( 'Date parameters', 'shortcoder' ), |
| 381 | 'icon' => 'calendar-alt', |
| 382 | 'params' => array( |
| 383 | 'day' => __( 'Day', 'shortcoder' ), |
| 384 | 'day_lz' => __( 'Day - leading zeros', 'shortcoder' ), |
| 385 | 'day_ws' => __( 'Day - words - short form', 'shortcoder' ), |
| 386 | 'day_wf' => __( 'Day - words - full form', 'shortcoder' ), |
| 387 | 'month' => __( 'Month', 'shortcoder' ), |
| 388 | 'month_lz' => __( 'Month - leading zeros', 'shortcoder' ), |
| 389 | 'month_ws' => __( 'Month - words - short form', 'shortcoder' ), |
| 390 | 'month_wf' => __( 'Month - words - full form', 'shortcoder' ), |
| 391 | 'year' => __( 'Year', 'shortcoder' ), |
| 392 | 'year_2d' => __( 'Year - 2 digit', 'shortcoder' ), |
| 393 | ) |
| 394 | ), |
| 395 | 'sc_cnt' => array( |
| 396 | 'name' => __( 'Shortcode enclosed content', 'shortcoder' ), |
| 397 | 'icon' => 'text', |
| 398 | 'params' => array( |
| 399 | 'enclosed_content' => __( 'Shortcode enclosed content', 'shortcoder' ) |
| 400 | ) |
| 401 | ) |
| 402 | )); |
| 403 | |
| 404 | } |
| 405 | |
| 406 | public static function set_defaults( $a, $b ){ |
| 407 | |
| 408 | $a = (array) $a; |
| 409 | $b = (array) $b; |
| 410 | $result = $b; |
| 411 | |
| 412 | foreach ( $a as $k => &$v ) { |
| 413 | if ( is_array( $v ) && isset( $result[ $k ] ) ) { |
| 414 | $result[ $k ] = self::set_defaults( $v, $result[ $k ] ); |
| 415 | } else { |
| 416 | $result[ $k ] = $v; |
| 417 | } |
| 418 | } |
| 419 | return $result; |
| 420 | } |
| 421 | |
| 422 | } |
| 423 | |
| 424 | Shortcoder::init(); |
| 425 | |
| 426 | ?> |