shortcoder
Last commit date
admin
9 years ago
includes
9 years ago
languages
9 years ago
readme.txt
9 years ago
shortcoder.php
9 years ago
shortcoder.php
225 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 exceuted in that place. You can create a shortcode for Youtube videos, adsense ads, buttons and more. |
| 6 | Author: Aakash Chakravarthy |
| 7 | Version: 4.0.1 |
| 8 | Author URI: https://www.aakashweb.com/ |
| 9 | */ |
| 10 | |
| 11 | define( 'SC_VERSION', '4.0.1'); |
| 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 . 'admin/sc-admin.php' ); |
| 47 | |
| 48 | } |
| 49 | |
| 50 | public static function execute_shortcode( $atts, $content ) { |
| 51 | |
| 52 | $shortcodes = self::list_all(); |
| 53 | |
| 54 | if( empty( $shortcodes ) ){ |
| 55 | return ''; |
| 56 | } |
| 57 | |
| 58 | // Get the Shortcode name |
| 59 | if(isset($atts[0])){ |
| 60 | $sc_name = str_replace(array('"', "'", ":"), '', $atts[0]); |
| 61 | unset($atts[0]); |
| 62 | }else{ |
| 63 | // Old version with "name" param support |
| 64 | if(array_key_exists("name", $atts)){ |
| 65 | $tVal = $atts['name']; |
| 66 | if(array_key_exists($tVal, $shortcodes)){ |
| 67 | $sc_name = $tVal; |
| 68 | unset($atts['name']); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if(!isset($sc_name)){ |
| 74 | return ''; |
| 75 | } |
| 76 | |
| 77 | // Check whether shortcoder can execute |
| 78 | if( self::check_conditions( $sc_name ) ){ |
| 79 | |
| 80 | $sc_content_final = ''; |
| 81 | |
| 82 | // If SC has parameters, then replace it |
| 83 | if( !empty( $atts ) ){ |
| 84 | |
| 85 | $keys = array(); |
| 86 | $values = array(); |
| 87 | $i = 0; |
| 88 | |
| 89 | // Seperate key and value from atts |
| 90 | foreach( $atts as $k => $v ){ |
| 91 | if( $k !== 0 ){ |
| 92 | $keys[$i] = "%%" . $k . "%%"; |
| 93 | $values[$i] = $v; |
| 94 | } |
| 95 | $i++; |
| 96 | } |
| 97 | |
| 98 | // Replace the params |
| 99 | $sc_content = $shortcodes[ $sc_name ][ 'content' ]; |
| 100 | $sc_content_rep1 = str_ireplace( $keys, $values, $sc_content ); |
| 101 | $sc_content_final = preg_replace( '/%%[^%\s]+%%/', '', $sc_content_rep1 ); |
| 102 | |
| 103 | } |
| 104 | else{ |
| 105 | |
| 106 | // If the SC has no params, then replace the %%vars%% |
| 107 | $sc_content = $shortcodes[ $sc_name ][ 'content' ]; |
| 108 | $sc_content_final = preg_replace( '/%%[^%\s]+%%/', '', $sc_content ); |
| 109 | |
| 110 | } |
| 111 | |
| 112 | $sc_content_final = self::replace_wp_params( $sc_content_final ); |
| 113 | return '<!-- Start shortcoder -->' . do_shortcode( $sc_content_final ) . '<!-- End shortcoder v' . SC_VERSION . '-->'; |
| 114 | |
| 115 | }else{ |
| 116 | return '<!-- Shortcoder conditions not met -->'; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | public static function check_conditions( $name ){ |
| 121 | |
| 122 | $shortcodes = self::list_all(); |
| 123 | |
| 124 | if( array_key_exists( $name, $shortcodes ) ){ |
| 125 | |
| 126 | $sc = wp_parse_args( $shortcodes[ $name ], self::defaults() ); |
| 127 | |
| 128 | $devices = $sc[ 'devices' ]; |
| 129 | if( $devices == 'mobile_only' && !wp_is_mobile() ){ |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | if( $devices == 'desktop_only' && wp_is_mobile() ){ |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | if( $sc[ 'disabled' ] == 0 ){ |
| 138 | if( current_user_can( 'level_10' ) && $sc[ 'hide_admin' ] == 1 ){ |
| 139 | return false; |
| 140 | }else{ |
| 141 | return true; |
| 142 | } |
| 143 | }else{ |
| 144 | return false; |
| 145 | } |
| 146 | }else{ |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | } |
| 151 | |
| 152 | public static function replace_wp_params( $content ){ |
| 153 | |
| 154 | $params = self::wp_params_list(); |
| 155 | $metadata = Shortcoder_Metadata::metadata(); |
| 156 | $to_replace = array(); |
| 157 | |
| 158 | foreach( $params as $id => $name ){ |
| 159 | if( array_key_exists( $id, $metadata ) ){ |
| 160 | $placeholder = '$$' . $id . '$$'; |
| 161 | $to_replace[ $placeholder ] = $metadata[ $id ]; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | $content = strtr( $content, $to_replace ); |
| 166 | |
| 167 | return $content; |
| 168 | |
| 169 | } |
| 170 | |
| 171 | public static function wp_params_list(){ |
| 172 | return apply_filters( 'sc_mod_wp_params', array( |
| 173 | 'url' => 'URL of the post/location', |
| 174 | 'title' => 'Title of the post/location', |
| 175 | 'short_url' => 'Short URL of the post/location', |
| 176 | |
| 177 | 'post_id' => 'Post ID', |
| 178 | 'post_image' => 'Post featured image URL', |
| 179 | 'post_excerpt' => 'Post excerpt', |
| 180 | 'post_author' => 'Post author', |
| 181 | 'post_date' => 'Post date', |
| 182 | 'post_comments_count' => 'Post comments count' |
| 183 | )); |
| 184 | } |
| 185 | |
| 186 | public static function on_activate(){ |
| 187 | |
| 188 | $shortcodes = self::list_all(); |
| 189 | $sc_flags = get_option( 'shortcoder_flags' ); |
| 190 | |
| 191 | // Move the flag version fix to sc_flags option |
| 192 | if( isset( $shortcodes[ '_version_fix' ] ) ){ |
| 193 | unset( $shortcodes['_version_fix'] ); |
| 194 | update_option( 'shortcoder_data', $shortcodes ); |
| 195 | } |
| 196 | |
| 197 | $sc_flags[ 'version' ] = SC_VERSION; |
| 198 | update_option( 'shortcoder_flags', $sc_flags ); |
| 199 | |
| 200 | } |
| 201 | |
| 202 | public static function defaults(){ |
| 203 | return array( |
| 204 | 'content' => '', |
| 205 | 'disabled' => 0, |
| 206 | 'hide_admin' => 0, |
| 207 | 'devices' => 'all' |
| 208 | ); |
| 209 | } |
| 210 | |
| 211 | public static function wp_44_workaround( $content ){ |
| 212 | return str_replace( '[sc:', '[sc name=', $content ); |
| 213 | } |
| 214 | |
| 215 | public static function load_text_domain(){ |
| 216 | |
| 217 | load_plugin_textdomain( 'shortcoder', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' ); |
| 218 | |
| 219 | } |
| 220 | |
| 221 | } |
| 222 | |
| 223 | Shortcoder::init(); |
| 224 | |
| 225 | ?> |