Changeset 1711334
- Timestamp:
- 08/10/2017 07:50:27 AM (9 years ago)
- Location:
- snippy/trunk
- Files:
-
- 1 added
- 7 edited
-
admin/css/style.css (modified) (3 diffs)
-
admin/js/script.js (modified) (3 diffs)
-
includes/bits.php (modified) (7 diffs)
-
includes/placeholders.php (added)
-
includes/shortcodes.php (modified) (1 diff)
-
includes/utils.php (modified) (3 diffs)
-
readme.txt (modified) (3 diffs)
-
snippy.php (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
snippy/trunk/admin/css/style.css
r1700307 r1711334 36 36 } 37 37 38 .snippy--bit-resource-remote::before { 39 content: '\f176'; 40 font-family: dashicons; 41 margin-right:.25em; 42 position:relative; 43 top:.1875em; 44 } 38 45 39 46 … … 51 58 * Snippy type toggle 52 59 */ 53 .snippy-- bit-type-toggle ul {54 margin:0; 55 } 56 57 .snippy-- bit-type-toggle li {60 .snippy--type-toggle ul { 61 margin:0; 62 } 63 64 .snippy--type-toggle li { 58 65 display:inline-block; 59 66 margin-right:1em; … … 61 68 } 62 69 63 .snippy-- bit-type-toggle legend {70 .snippy--type-toggle legend { 64 71 display:none; 65 72 } -
snippy/trunk/admin/js/script.js
r1700307 r1711334 1 1 var Snippy = (function(undefined){ 2 3 function showLocalField() { 4 5 showNodes(document.querySelectorAll('.snippy--resource-type-local')); 6 hideNodes(document.querySelectorAll('.snippy--resource-type-remote')); 7 8 } 9 10 function showRemoteField() { 11 12 showNodes(document.querySelectorAll('.snippy--resource-type-remote')); 13 hideNodes(document.querySelectorAll('.snippy--resource-type-local')); 14 15 16 } 2 17 3 18 function showFileField(isText) { … … 24 39 var exports = { 25 40 Bits:{ 41 showLocalField:showLocalField, 42 showRemoteField:showRemoteField, 26 43 showTextField:showTextField, 27 44 showFileField:showFileField, … … 59 76 function updatePlaceholders(value) { 60 77 var placeholders = document.querySelector('.snippy--bit-placeholders'); 61 var matches = value.match(/({{[a-z ]+(?::.+?){0,1}}})/gi) || [];78 var matches = value.match(/({{[a-z_]+(?::.+?){0,1}}})/gi) || []; 62 79 placeholders.innerHTML = matches.map(function(str) { 63 var tag = str.match(/^{{([a-z ]+)/i)[0].replace(/^{{/,'');80 var tag = str.match(/^{{([a-z_]+)/i)[0].replace(/^{{/,''); 64 81 var value = ''; 65 82 if (str.indexOf(':')!==-1 && tag !== 'content') { -
snippy/trunk/includes/bits.php
r1700307 r1711334 41 41 $label = ''; 42 42 if ($bitType === 'resource') { 43 $label = '<span class="snippy--bit-type-resource ">' . $item['value']. '</span>';43 $label = '<span class="snippy--bit-type-resource ' . (Utils::is_remote($item['value']) ? 'snippy--bit-resource-remote' : '') . '">' . Utils::to_filename($item['value']) . '</span>'; 44 44 } 45 45 … … 110 110 111 111 // previous value 112 $item_previous_value = $_REQUEST['previous_value'];112 //$item_previous_value = $_REQUEST['previous_value']; 113 113 114 114 // if a file is posted … … 116 116 117 117 // uploading new file or overwriting file 118 if (isset($_FILES['resource']) && $_FILES['resource']['size'] > 0) { 119 120 $uploaded_file = $_FILES['resource']; 121 $uploaded_filename = $uploaded_file['name']; 122 $uploaded_file_extension = strtolower(pathinfo($uploaded_filename, PATHINFO_EXTENSION)); 123 124 $allowed_extensions = ['css', 'js']; 125 126 if (!in_array($uploaded_file_extension, $allowed_extensions)) { 127 $item_valid = \__('There was an error while uploading the resource', 'snippy'); 128 } 129 else { 130 $move_result = \wp_handle_upload( $uploaded_file, array( 'test_form' => false ) ); 131 132 if ( $move_result && ! isset( $move_result['error'] ) ) { 133 134 $path = str_replace( \wp_upload_dir()['basedir'], '', $move_result['file'] ); 135 136 // set type to the extension of the uploaded file 137 $item['type'] = $uploaded_file_extension === 'js' ? 'script' : 'stylesheet'; 138 $item['value'] = $path; 139 140 } else { 118 if ($_REQUEST['resource-location'] === 'local') { 119 120 // if sent a file 121 if (isset($_FILES['resource']) && 122 $_FILES['resource']['size'] > 0) { 123 124 $uploaded_file = $_FILES['resource']; 125 $uploaded_filename = $uploaded_file['name']; 126 $uploaded_file_extension = strtolower(pathinfo($uploaded_filename, PATHINFO_EXTENSION)); 127 128 $allowed_extensions = ['css', 'js']; 129 130 if (!in_array($uploaded_file_extension, $allowed_extensions)) { 141 131 $item_valid = \__('There was an error while uploading the resource', 'snippy'); 142 132 } 143 } 144 145 } 146 // updating existing file meta data 147 else { 148 149 // type can remain the same 150 // value can remain the same 151 // only name could be changed 152 133 else { 134 $move_result = \wp_handle_upload( $uploaded_file, array( 'test_form' => false ) ); 135 136 if ( $move_result && ! isset( $move_result['error'] ) ) { 137 138 $path = str_replace( \wp_upload_dir()['basedir'], '', $move_result['file'] ); 139 140 // set type to the extension of the uploaded file 141 $item['type'] = $uploaded_file_extension === 'js' ? 'script' : 'stylesheet'; 142 $item['value'] = $path; 143 144 } else { 145 $item_valid = \__('There was an error while uploading the resource', 'snippy'); 146 } 147 } 148 } 149 // updating existing file meta data 150 else { 151 152 // type can remain the same 153 // value can remain the same 154 // only name could be changed 155 156 $item['type'] = strtolower(pathinfo($item['value'], PATHINFO_EXTENSION)) === 'js' ? 'script' : 'stylesheet'; 157 158 } 159 160 } 161 else if ($_REQUEST['resource-location'] === 'remote') { 162 $item['value'] = $_REQUEST['resource-remote']; 153 163 $item['type'] = strtolower(pathinfo($item['value'], PATHINFO_EXTENSION)) === 'js' ? 'script' : 'stylesheet'; 154 155 164 } 156 165 … … 192 201 else { 193 202 203 // get current value 204 $bit = Data::get_entry('bits', $item['id']); 205 194 206 // remove previous file if set 195 if ($ item_previous_value!== $item['value']) {196 Data::delete_ file($item_previous_value);207 if ($bit['value'] !== $item['value']) { 208 Data::delete_bit_resource($item['id']); 197 209 } 198 210 … … 298 310 </th> 299 311 <td> 300 <fieldset class="snippy-- bit-type-toggle">312 <fieldset class="snippy--type-toggle snippy--bit-type-toggle"> 301 313 <legend><?php \_e('Type', 'snippy')?></legend> 302 314 <ul> … … 362 374 <pre><code>[person]John Doe[/person]</code></pre> 363 375 </li> 376 <li> 377 <p><?php \_e('The following list of placeholders can be used to access dynamic page data.', 'snippy')?></p> 378 <pre><code>{{page_id}}</code></pre> 379 <pre><code>{{page_absolute_url}}</code></pre> 380 <pre><code>{{page_relative_url}}</code></pre> 381 <pre><code>{{unique_id}}</code></pre> 382 <pre><code>{{date_today}}</code></pre> 383 <pre><code>{{date_tomorrow}}</code></pre> 384 </li> 364 385 </ol> 365 386 … … 372 393 </th> 373 394 <td> 374 <?php if (!$isText) {?> 395 <?php 396 $is_remote_resource = Utils::is_remote($item['value']); 397 ?> 398 <fieldset class="snippy--type-toggle snippy--resource-typ-toggle"> 399 <legend><?php \_e('Resource location', 'snippy')?></legend> 400 <ul> 401 <li><label><input type="radio" name="resource-location" value="local" <?php echo $is_remote_resource ? '' : 'checked' ?>> Local</label></li> 402 <li><label><input type="radio" name="resource-location" value="remote" <?php echo $is_remote_resource ? 'checked' :'' ?>> Remote</label></li> 403 </ul> 404 <script> 405 (function(){ 406 407 document.querySelector('.snippy--resource-typ-toggle').addEventListener('change', function(e) { 408 if (e.target.value === 'local') { 409 Snippy.Bits.showLocalField(); 410 } 411 else { 412 Snippy.Bits.showRemoteField(); 413 } 414 }); 415 416 }()); 417 </script> 418 </fieldset> 419 420 <div class="snippy--resource-type-local" <?php echo $is_remote_resource ? 'style="display:none"' : '' ?>> 421 <?php if (!$isText && !$is_remote_resource) {?> 375 422 "<span class="snippy--bit-resource-original"><?php echo $item['value']; ?></span>" 376 423 <p> 377 424 Upload a new file: <input id="resource" name="resource" type="file"> 378 425 </p> 379 <?php } else { ?>426 <?php } else { ?> 380 427 <input id="resource" name="resource" type="file" style="width: 95%"> 381 <?php } ?> 428 <?php } ?> 429 </div> 430 431 <div class="snippy--resource-type-remote" <?php echo $is_remote_resource ? '' : 'style="display:none"' ?>> 432 <input id="resource-remote" name="resource-remote" type="text" value="<?php echo $is_remote_resource ? $item['value'] : '' ?>" style="width: 95%"> 433 </div> 382 434 </td> 383 435 </tr> -
snippy/trunk/includes/shortcodes.php
r1700307 r1711334 208 208 209 209 if (Utils::get_bit_type($bit) === 'resource') { 210 echo '<span class="snippy--bit-type-resource ">' . $bit['value']. '</span>';210 echo '<span class="snippy--bit-type-resource ' . (Utils::is_remote($bit['value']) ? 'snippy--bit-resource-remote' : '') . '">' . Utils::to_filename($bit['value']) . '</span>'; 211 211 } 212 212 ?> -
snippy/trunk/includes/utils.php
r1700307 r1711334 22 22 } 23 23 24 static public function is_remote($value) { 25 return preg_match('/^https{0,1}:\/\//', $value); 26 } 27 28 static public function to_filename($value) { 29 preg_match('/([^\/]*)$/', $value, $matches); 30 return $matches[0]; 31 } 32 24 33 static public function replace_placeholders($placeholders, $html) { 25 34 foreach ($placeholders as $placeholder) { 26 $html = preg_replace('/{{'. $placeholder['name'] .'(?::.+){0,1}}}/i', $placeholder['value'], $html); 35 $replacement = !empty($placeholder['value']) ? $placeholder['value'] : Placeholders::get_placeholder_value($placeholder['name']); 36 $html = preg_replace('/{{'. $placeholder['name'] .'(?::.+){0,1}}}/i', $replacement, $html); 27 37 } 28 38 return $html; … … 62 72 static public function get_placeholders_from_bit($bit) { 63 73 64 preg_match_all('/({{[a-z ]+(?::.+?){0,1}}})/i', html_entity_decode($bit['value']), $placeholders);74 preg_match_all('/({{[a-z_]+(?::.+?){0,1}}})/i', html_entity_decode($bit['value']), $placeholders); 65 75 66 76 $formatted_placeholders = array(); … … 70 80 foreach ($placeholders[0] as $placeholder) { 71 81 72 preg_match('/{{([a-z ]+)/i', $placeholder, $nameMatch);82 preg_match('/{{([a-z_]+)/i', $placeholder, $nameMatch); 73 83 74 84 $name = preg_replace('/^{{/', '', $nameMatch[0]); -
snippy/trunk/readme.txt
r1711322 r1711334 2 2 Contributors: pqina 3 3 Donate link: https://codecanyon.net/user/pqina/portfolio?ref=pqina 4 Tags: shortcode, short code, custom, build, create, edit, javascript, jquery, plugin4 Tags: shortcode, short code, build, create, javascript 5 5 Requires at least: 4.5 6 Tested up to: 4.8 7 Stable tag: 1. 0.06 Tested up to: 4.8.1 7 Stable tag: 1.1.0 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 81 81 82 82 83 = Which default placeholders can I use = 84 85 The following list of placeholders have a special function: 86 87 * `{{content}}` is always replaced with the content wrapped by your shortcode. 88 * `{{date_today}}` is replaced with an ISO8601 representation of today's date. 89 * `{{date_tomorrow}}` is replaced with an ISO8601 representation of today's date. 90 * `{{unique_id}}` is replaced with a uniquely generated id. 91 * `{{page_id}}` is replaced with the current page id. 92 * `{{page_relative_url}}` relative url to the current page. 93 * `{{page_absolute_url}}` absolute url to the current page (includes the domain). 94 95 83 96 == Screenshots == 84 97 … … 87 100 3. Adding the shortcode to the page and setting a YouTube video id. 88 101 89 == Upgrade Notice ==90 91 Test92 93 102 94 103 == Changelog == 95 104 105 = 1.1.0 = 106 107 * Add "local" or "remote" resource bit which makes possible the option to include CDN resources. 108 * Add a starter set of dynamic placeholders 109 * Only admin can now edit Snippy shortcodes 110 111 96 112 = 1.0 = 113 97 114 * Initial release 115 116 117 == Upgrade Notice == -
snippy/trunk/snippy.php
r1700307 r1711334 4 4 * Plugin URI: https://pqina.nl/snippy 5 5 * Description: Snippy, create your own super flexible shortcodes 6 * Version: 1. 0.06 * Version: 1.1.0 7 7 * Author: PQINA 8 8 * Author URI: https://pqina.nl … … 47 47 require_once('includes/db.php'); 48 48 require_once('includes/utils.php'); 49 require_once('includes/placeholders.php'); 49 50 50 51 … … 61 62 class Snippy { 62 63 63 // Snippy version and Snippy Database version64 public static $version = '1. 0.0';64 // Snippy version 65 public static $version = '1.1.0'; 65 66 66 67 private static $_instance = null; … … 83 84 \add_action( 'plugins_loaded', array($this, 'update' ) ); 84 85 85 \add_action( 'admin_menu', array( $this, 'admin_menu') );86 87 86 \add_action( 'init', array( $this, 'init') ); 88 87 … … 95 94 public function init() 96 95 { 96 97 if (\current_user_can('administrator')) { 98 \add_action( 'admin_menu', array( $this, 'admin_menu') ); 99 } 97 100 98 101 \load_plugin_textdomain('snippy', false, dirname(\plugin_basename(__FILE__))); … … 205 208 public function register_admin_scripts() { 206 209 207 wp_enqueue_style( 'snippy-admin-styles',plugin_dir_url( __FILE__ ) . 'admin/css/style.css', array(), Snippy::$version );208 wp_enqueue_script( 'snippy-admin-scripts',plugin_dir_url( __FILE__ ) . 'admin/js/script.js', array(), Snippy::$version, true );210 \wp_enqueue_style( 'snippy-admin-styles', \plugin_dir_url( __FILE__ ) . 'admin/css/style.css', array(), Snippy::$version ); 211 \wp_enqueue_script( 'snippy-admin-scripts', \plugin_dir_url( __FILE__ ) . 'admin/js/script.js', array(), Snippy::$version, true ); 209 212 210 213 } … … 212 215 public function register_scripts() { 213 216 214 $upload_url = wp_upload_dir()['baseurl'];217 $upload_url = \wp_upload_dir()['baseurl']; 215 218 $bits = Data::get_entries_all('bits'); 216 219 foreach($bits as $bit) { 220 221 $url = Utils::is_remote($bit['value']) ? $bit['value'] : $upload_url . $bit['value']; 222 $name = $bit['name']; 223 217 224 if ($bit['type'] === 'script') { 218 wp_register_script( $bit['name'], $upload_url . $bit['value'], array(), false, true );225 \wp_register_script( $name, $url, array(), false, true ); 219 226 } 220 227 else if ($bit['type'] === 'stylesheet') { 221 wp_register_style( $bit['name'], $upload_url . $bit['value']);228 \wp_register_style( $name, $url ); 222 229 } 223 230 } … … 230 237 231 238 foreach ($shortcode_entries as $shortcode_entry) { 232 add_shortcode($shortcode_entry['name'], array( $this , 'handle_shortcode'));239 \add_shortcode($shortcode_entry['name'], array( $this , 'handle_shortcode')); 233 240 } 234 241 } … … 253 260 // if is script, enqueue 254 261 if ($bit_type === 'script') { 255 wp_enqueue_script( $bit_name );262 \wp_enqueue_script( $bit_name ); 256 263 } 257 264 258 265 // if is stylesheet, enqueue 259 266 else if ($bit_type === 'stylesheet') { 260 wp_enqueue_style( $bit_name );267 \wp_enqueue_style( $bit_name ); 261 268 } 262 269 … … 294 301 } 295 302 303 // replace placeholders 296 304 $html = Utils::replace_placeholders($placeholders_merged, $html); 297 305
Note: See TracChangeset
for help on using the changeset viewer.