npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wping

v1.0.0

Published

WordPress nonce refresh utility

Downloads

11

Readme

wping

wping is a zero-dependency WordPress nonce refresh utility weighing in at less than 600 bytes. It works both in the browser and in Node. In typical usage, it's plug-and-play, detecting and updating the nonce value of the wpApiSettings window global assigned when using either the Backbone client or wp-api-request scripts.

Getting Started

Enqueue the script in your theme or plugin, localizing with wpApiSettings if you're not already using wp-api or wp-api-request client scripts.

function myplugin_enqueue_scripts() {
	wp_enqueue_script( 'wping', 'https://unpkg.com/wping/dist/wping.min.js' );
	wp_localize_script( 'wping', 'wpApiSettings', array(
		'apiRoot' => esc_url_raw( rest_url() ),
		'nonce' => wp_create_nonce( 'wp_rest' ),
	) );
}
add_action( 'wp_enqueue_scripts', 'myplugin_enqueue_scripts' );

Note: The above example references wping by CDN. You may instead want to download the file to your theme or plugin directory and update the URL reference accordingly.

When using in a Node.js context, the default export is the wping function:

import wping from 'wping';

Usage

Using the Getting Started theme code, you're all set! There's nothing else to do. The wpApiSettings.nonce value will be updated automatically.

In all other cases, you can configure your own behavior to occur when the nonce is refreshed, or change the settings for refreshing the nonce.

wping( callback: Function, options: Object )

Example:

var nonce = window._initialNonce;
wping( function( error, nextNonce ) {
	if ( ! error ) {
		nonce = nextNonce;
	}
}, {
	nonce: nonce,
	delay: 5000,
	apiRoot: 'https://example.com/path/to/rest-api'
} );

The callback is a Node-style callback function, receiving an error as the first argument if the refresh was unsuccessful, and the next nonce value as the second argument.

Options:

  • nonce (string): Initial nonce to use for refresh.
  • delay (number): Time to elapse before refreshing nonce. Defaults to 12 hours, to occur in second tick of default nonce lifetime.
  • apiRoot (string): Root URL of the WordPress REST API.

License

Copyright 2018 Andrew Duthie

Released under the MIT License.