Title: Support External Increment
Last modified: September 6, 2017

---

# Support External Increment

 *  [chrisburge](https://wordpress.org/support/users/chrisburge/)
 * (@chrisburge)
 * [8 years, 8 months ago](https://wordpress.org/support/topic/support-external-increment/)
 * **Problem/Motivation**
    Currently, it is not possible to increase the increment
   value outside of the UI. When deploying theme code (e.g. CSS/JS), it would be
   useful to be able to send a POST to REST endpoint, so that the increment can 
   be increased in an automated fashion.
 * **Proposed resolution**
    Add a REST endpoint to accept HTTP POSTs, which will
   increase the increment value. The admin will need to set a key, which will be
   appended to the REST URL as such for authentication:
 * `https://example.com/wp-json/wpvqsm/v1/increment?key=3472c269d46a697d9cab765716c5f6bb`
 * An ‘externalKey’ value is added to the options setting value. This new setting
   is exposed to the admin on the plugin’s configuration page. If no key value is
   set when the REST URL receives a POST, then it will fail. If no value is set 
   when the admin visits the config page, then a random MD5 hash will be populated
   in the field; however, it is still necessary that the admin save the config.
 * **Remaining tasks**
    1. Plugin contributor review
 * **User interface changes**
    An ‘External Key’ field is added to the plugin’s 
   configuration page.

Viewing 1 replies (of 1 total)

 *  Thread Starter [chrisburge](https://wordpress.org/support/users/chrisburge/)
 * (@chrisburge)
 * [8 years, 8 months ago](https://wordpress.org/support/topic/support-external-increment/#post-9473552)
 * Patch is below:
 *     ```
       From ff56242192f8f824adcddfac08d18cdcb3319af7 Mon Sep 17 00:00:00 2001
       From: Chris Burge
       Date: Wed, 6 Sep 2017 12:43:16 -0500
       Subject: [PATCH] Support external increment
   
       ---
        tmpl/menu.php                           | 24 +++++++++++++++++
        wp-version-in-query-string-modifier.php | 48 ++++++++++++++++++++++++++++++++-
        2 files changed, 71 insertions(+), 1 deletion(-)
   
       diff --git a/tmpl/menu.php b/tmpl/menu.php
       index 0c553c8..d089839 100644
       --- a/tmpl/menu.php
       +++ b/tmpl/menu.php
       @@ -16,6 +16,10 @@
                    $check_i = ' checked="checked"';
                    $disable_increment = false;
            }
       +    if (!isset($options['externalKey'])) {
       +        $options['externalKey'] = md5(uniqid(rand(), true));
       +    }
       +
        ?>
        <div class='wrap'>
            <h2><?php echo WPVQSM_LONG_NAME; ?></h2>
       @@ -34,6 +38,10 @@
                            <th><label><input type='radio' <?php echo $check_i; ?> value='i' name='selection'>Cache Buster</label></th>
                            <td><code><?php echo $site_url; ?>/wp-includes/js/jquery/jquery.js?ver=<span id='inc_index'><?php echo $ndx; ?></span></code> <input type='button' value='Increment' id='increment' class='button-primary'></td>
                        </tr>
       +                <tr class="external-key">
       +                    <th></th>
       +                    <td><label for="external-key">External Key: </label><input type="text" name="externalKey" size="32" value="<?php print $options['externalKey']; ?>"/><br /><br />The cache buster can be externally incremented using the URL below:<br /><br /><code><?php print get_site_url() . 'wp-json/wpvqsm/v1/increment?key=<span class="external-key-inline">' . $options['externalKey'] . '</span>'; ?></code></td>
       +                </tr>
                        <tr>
                            <th><label><input type='radio' <?php echo $check_r; ?> value='r' name='selection'> Remove Version</label></th>
                            <td><code><?php echo $site_url; ?>/wp-includes/js/jquery/jquery.js</code></td>
       @@ -87,5 +95,21 @@
                    }
        		});
            });
       +    var selection = $('input:radio[name=selection]');
       +    selection.change(function () {
       +        var value = this.value;
       +        if (value != "i") {
       +            $('.external-key').hide();
       +        }
       +        else {
       +            $('.external-key').show();
       +        }
       +    });
       +    var externalKey = $('input:text[name=externalKey]');
       +    externalKey.change(function () {
       +        var value = this.value;
       +        console.log(value);
       +        $('.external-key-inline').text(value);
       +    });
        })(jQuery);
        </script>
       \ No newline at end of file
       diff --git a/wp-version-in-query-string-modifier.php b/wp-version-in-query-string-modifier.php
       index 537d17f..1eb5b88 100644
       --- a/wp-version-in-query-string-modifier.php
       +++ b/wp-version-in-query-string-modifier.php
       @@ -134,6 +134,7 @@ function wpvqsm_update_callback() {
            $options = wpvqsm_get_options();
            $options['selection'] = $_POST['selection'];
            $options['addTime'] = $_POST['addTime'];
       +    $options['externalKey'] = $_POST['externalKey'];
            wpvqsm_update_options( $options );
            wp_safe_redirect( 'options-general.php?settings-updated=true&page=' . WPVQSM );
        }
       @@ -162,6 +163,49 @@ function wpvqsm_ajax_callback() {
            die( 1 );
        }
   
       +/**
       + * Callback function for REST API.
       + *
       + * @since 1.x
       + *
       + */
       +function wpvqsm_external_increment_callback(WP_REST_Request $data) {
       +
       +    // Get options.
       +    $options = wpvqsm_get_options();
       +
       +    if ($options['selection'] != 'i') {
       +        return rest_ensure_response( array("enabled"=>false));
       +    }
       +
       +    // Get key from query string and validate.
       +    $query_string = $data->get_query_params();
       +    $key = $query_string['key'];
       +    if (is_null($options['externalKey']) || $key != $options['externalKey']) {
       +        return rest_ensure_response( array("authorized"=>false));
       +    }
       +
       +    // Update Increment value.
       +    ++$options['increment'];
       +    wpvqsm_update_options( $options );
       +
       +    // rest_ensure_response() wraps the data we want to return into a WP_REST_Response, and ensures it will be properly returned.
       +    return rest_ensure_response( array("authorized"=>true, "increment"=>$options['increment']) );
       +}
       +
       +/**
       + * Register REST route for endpoint.
       + *
       + * @since 1.x
       + *
       + */
       +function wpvqsm_external_increment() {
       +    register_rest_route( 'wpvqsm/v1', '/increment', array(
       +       'methods'  => 'GET',
       +       'callback' => 'wpvqsm_external_increment_callback',
       +   ) );
       +}
       +
        /**
         * plugin activation process
         *
       @@ -224,4 +268,6 @@ if ( is_admin() ){
        }
   
        add_filter( 'script_loader_src', WPVQSM. 'modify_version' );
       -add_filter( 'style_loader_src', WPVQSM. 'modify_version' );   
       \ No newline at end of file
       +add_filter( 'style_loader_src', WPVQSM. 'modify_version' );
       +
       +add_action( 'rest_api_init', WPVQSM . 'external_increment' );
       \ No newline at end of file
       -- 
       2.14.1.windows.1
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Support External Increment’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/wp-version-in-query-string-modifier.
   svg)
 * [WP Version in Query String Modifier](https://wordpress.org/plugins/wp-version-in-query-string-modifier/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-version-in-query-string-modifier/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-version-in-query-string-modifier/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-version-in-query-string-modifier/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-version-in-query-string-modifier/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-version-in-query-string-modifier/reviews/)

## Tags

 * [cache busting](https://wordpress.org/support/topic-tag/cache-busting/)

 * 1 reply
 * 1 participant
 * Last reply from: [chrisburge](https://wordpress.org/support/users/chrisburge/)
 * Last activity: [8 years, 8 months ago](https://wordpress.org/support/topic/support-external-increment/#post-9473552)
 * Status: not a support question