Code Monkey home page Code Monkey logo

ajax-wrapper's Introduction

AJAX Action Wrapper

This helper library makes it easier to handle AJAX requests in WordPress plugins. Mainly for personal use.

Example

Define action:

$exampleAction = ajaw_v1_CreateAction('ws_do_something')
	->handler(array($this, 'myAjaxCallback'))
	->requiredCap('manage_options')
	->method('post')
	->requiredParam('foo')
	->optionalParam('bar', 'default value')
	->register();

Call from JavaScript:

AjawV1.getAction('ws_do_something').post(
	{
		'foo': '...'
	},
	function(response) {
		console.log(response);
	}
);

Features

  • Automate common, boring stuff.
    • Automatically pass the admin-ajax.php URL and nonce to JS.
    • Define required parameters.
       $builder->requiredParam('foo', 'int')
    • Define optional parameters with default values.
       $builder->optionalParam('meaningOfLife', 42, 'int')
    • Automatically remove "magic quotes" that WordPress adds to $_GET, $_POST and $_REQUEST.
    • Encode return values as JSON.
  • Security should be the default.
    • Generate and verify nonces. Nonce verification is on by default, but can be disabled.
       $builder->withoutNonce()
    • Check capabilities.
       $builder->requiredCap('manage_options');
    • Verify that all required parameters are set.
    • Validate parameter values.
       $builder->optionalParam('things', 1, 'int', function($value) {
       	if ($value > 10) {
       		return new WP_Error(
       			'excessive_things',
       			'Too many things!',
       			400 //HTTP status code.
       		);
       	}
       })
    • Set the required HTTP method.
       $builder->method('post')
  • Resilience.
    • Lenient response parsing to work around bugs in other plugins. For example, deal with extraneous whitespace and PHP notices in AJAX responses.
    • Multiple versions of the library can coexist on the same site.

Why not use the REST API instead?

Backwards compatibility. In theory, this library should be compatible with WP 4.1+.

ajax-wrapper's People

Contributors

yahniselsts avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.