Plugin Directory

Changeset 1711334


Ignore:
Timestamp:
08/10/2017 07:50:27 AM (9 years ago)
Author:
pqina
Message:

add remote resource option
only admin can now edit shortcodes
add more placeholders

Location:
snippy/trunk
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • snippy/trunk/admin/css/style.css

    r1700307 r1711334  
    3636}
    3737
     38.snippy--bit-resource-remote::before {
     39    content: '\f176';
     40    font-family: dashicons;
     41    margin-right:.25em;
     42    position:relative;
     43    top:.1875em;
     44}
    3845
    3946
     
    5158 * Snippy type toggle
    5259 */
    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 {
    5865    display:inline-block;
    5966    margin-right:1em;
     
    6168}
    6269
    63 .snippy--bit-type-toggle legend {
     70.snippy--type-toggle legend {
    6471    display:none;
    6572}
  • snippy/trunk/admin/js/script.js

    r1700307 r1711334  
    11var 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    }
    217
    318    function showFileField(isText) {
     
    2439    var exports = {
    2540        Bits:{
     41            showLocalField:showLocalField,
     42            showRemoteField:showRemoteField,
    2643            showTextField:showTextField,
    2744            showFileField:showFileField,
     
    5976    function updatePlaceholders(value) {
    6077        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) || [];
    6279        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(/^{{/,'');
    6481            var value = '';
    6582            if (str.indexOf(':')!==-1 && tag !== 'content') {
  • snippy/trunk/includes/bits.php

    r1700307 r1711334  
    4141                        $label = '';
    4242                        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>';
    4444                        }
    4545
     
    110110
    111111            // previous value
    112             $item_previous_value = $_REQUEST['previous_value'];
     112            //$item_previous_value = $_REQUEST['previous_value'];
    113113
    114114            // if a file is posted
     
    116116
    117117                // 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)) {
    141131                            $item_valid = \__('There was an error while uploading the resource', 'snippy');
    142132                        }
    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'];
    153163                    $item['type'] = strtolower(pathinfo($item['value'], PATHINFO_EXTENSION)) === 'js' ? 'script' : 'stylesheet';
    154 
    155164                }
    156165
     
    192201                else {
    193202
     203                    // get current value
     204                    $bit = Data::get_entry('bits', $item['id']);
     205
    194206                    // 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']);
    197209                    }
    198210
     
    298310                </th>
    299311                <td>
    300                     <fieldset class="snippy--bit-type-toggle">
     312                    <fieldset class="snippy--type-toggle snippy--bit-type-toggle">
    301313                        <legend><?php \_e('Type', 'snippy')?></legend>
    302314                        <ul>
     
    362374                                <pre><code>[person]John Doe[/person]</code></pre>
    363375                            </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>
    364385                        </ol>
    365386
     
    372393                </th>
    373394                <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) {?>
    375422                        "<span class="snippy--bit-resource-original"><?php echo $item['value']; ?></span>"
    376423                        <p>
    377424                            Upload a new file: <input id="resource" name="resource" type="file">
    378425                        </p>
    379                     <?php } else { ?>
     426                        <?php } else { ?>
    380427                        <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>
    382434                </td>
    383435            </tr>
  • snippy/trunk/includes/shortcodes.php

    r1700307 r1711334  
    208208
    209209                                        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>';
    211211                                        }
    212212                                        ?>
  • snippy/trunk/includes/utils.php

    r1700307 r1711334  
    2222    }
    2323
     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
    2433    static public function replace_placeholders($placeholders, $html) {
    2534        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);
    2737        }
    2838        return $html;
     
    6272    static public function get_placeholders_from_bit($bit) {
    6373
    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);
    6575
    6676        $formatted_placeholders = array();
     
    7080            foreach ($placeholders[0] as $placeholder) {
    7181
    72                 preg_match('/{{([a-z]+)/i', $placeholder, $nameMatch);
     82                preg_match('/{{([a-z_]+)/i', $placeholder, $nameMatch);
    7383
    7484                $name = preg_replace('/^{{/', '', $nameMatch[0]);
  • snippy/trunk/readme.txt

    r1711322 r1711334  
    22Contributors: pqina
    33Donate link: https://codecanyon.net/user/pqina/portfolio?ref=pqina
    4 Tags: shortcode, short code, custom, build, create, edit, javascript, jquery, plugin
     4Tags: shortcode, short code, build, create, javascript
    55Requires at least: 4.5
    6 Tested up to: 4.8
    7 Stable tag: 1.0.0
     6Tested up to: 4.8.1
     7Stable tag: 1.1.0
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    8181
    8282
     83= Which default placeholders can I use =
     84
     85The 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
    8396== Screenshots ==
    8497
     
    871003. Adding the shortcode to the page and setting a YouTube video id.
    88101
    89 == Upgrade Notice ==
    90 
    91 Test
    92 
    93102
    94103== Changelog ==
    95104
     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
    96112= 1.0 =
     113
    97114* Initial release
     115
     116
     117== Upgrade Notice ==
  • snippy/trunk/snippy.php

    r1700307 r1711334  
    44* Plugin URI: https://pqina.nl/snippy
    55* Description: Snippy, create your own super flexible shortcodes
    6 * Version: 1.0.0
     6* Version: 1.1.0
    77* Author: PQINA
    88* Author URI: https://pqina.nl
     
    4747require_once('includes/db.php');
    4848require_once('includes/utils.php');
     49require_once('includes/placeholders.php');
    4950
    5051
     
    6162class Snippy {
    6263
    63     // Snippy version and Snippy Database version
    64     public static $version = '1.0.0';
     64    // Snippy version
     65    public static $version = '1.1.0';
    6566
    6667    private static $_instance = null;
     
    8384        \add_action( 'plugins_loaded', array($this, 'update' ) );
    8485
    85         \add_action( 'admin_menu', array( $this, 'admin_menu') );
    86 
    8786        \add_action( 'init', array( $this, 'init') );
    8887
     
    9594    public function init()
    9695    {
     96
     97        if (\current_user_can('administrator')) {
     98            \add_action( 'admin_menu', array( $this, 'admin_menu') );
     99        }
    97100
    98101        \load_plugin_textdomain('snippy', false, dirname(\plugin_basename(__FILE__)));
     
    205208    public function register_admin_scripts() {
    206209
    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 );
    209212
    210213    }
     
    212215    public function register_scripts() {
    213216
    214         $upload_url = wp_upload_dir()['baseurl'];
     217        $upload_url = \wp_upload_dir()['baseurl'];
    215218        $bits = Data::get_entries_all('bits');
    216219        foreach($bits as $bit) {
     220
     221            $url = Utils::is_remote($bit['value']) ? $bit['value'] : $upload_url . $bit['value'];
     222            $name = $bit['name'];
     223
    217224            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 );
    219226            }
    220227            else if ($bit['type'] === 'stylesheet') {
    221                 wp_register_style( $bit['name'], $upload_url . $bit['value'] );
     228                \wp_register_style( $name, $url );
    222229            }
    223230        }
     
    230237
    231238        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'));
    233240        }
    234241    }
     
    253260            // if is script, enqueue
    254261            if ($bit_type === 'script') {
    255                 wp_enqueue_script( $bit_name );
     262                \wp_enqueue_script( $bit_name );
    256263            }
    257264
    258265            // if is stylesheet, enqueue
    259266            else if ($bit_type === 'stylesheet') {
    260                 wp_enqueue_style( $bit_name );
     267                \wp_enqueue_style( $bit_name );
    261268            }
    262269
     
    294301                }
    295302
     303                // replace placeholders
    296304                $html = Utils::replace_placeholders($placeholders_merged, $html);
    297305
Note: See TracChangeset for help on using the changeset viewer.