├── dist └── restjquery.min.js └── src └── restjquery.js /dist/restjquery.min.js: -------------------------------------------------------------------------------- 1 | /*! WordPress REST API jQuery v1.0.0 - 2018-09-22 16:35 */var response=null,restjQuery=function(a){"use strict";function b(a){return h?(console.log("WP REST API jQuery Authenticating Request..."),$.ajax({async:!1,url:a.site_url+"/wp-json/"+a.namespace+a.endpoint,method:a.formMethod,cache:a.cache,crossDomain:!0,crossOrigin:!0,data:a.postData,beforeSend:function(b){d(a,b)},complete:function(b){jQuery("body").trigger("restjquery_complete_"+a.formMethod+"_"+a.endpoint,[b]),response=b.responseJSON},error:function(b){jQuery("body").trigger("restjquery_error_"+a.formMethod+"_"+a.endpoint,[b]),response=b},dataType:a.dataType})):$.ajax({async:!1,url:a.site_url+"/wp-json/"+a.namespace+a.endpoint,method:a.formMethod,cache:a.cache,contentType:"application/json",crossDomain:!0,crossOrigin:!0,data:a.postData,complete:function(b){jQuery("body").trigger("restjquery_complete_"+a.formMethod+"_"+a.endpoint,[b]),response=b.responseJSON},error:function(b){jQuery("body").trigger("restjquery_error_"+a.formMethod+"_"+a.endpoint,[b]),response=b},dataType:a.dataType}),response}function c(a){return $.ajax({async:!0,url:a.site_url+"/wp-json/"+a.namespace+"media",method:"POST",data:a.postData,cache:!1,contentType:!1,processData:!1,headers:{"Content-Disposition":"attachment;filename="+a.filename},beforeSend:function(b){d(a,b)},complete:function(a){response=a,console.log(response),"200"==a.status?console.log("Upload Done"):console.error("Upload failed!")},error:function(a){response=a}}),response}function d(a,b){null!==a.nonce&&b.setRequestHeader("X-WP-Nonce",a.nonce);var c=a.authorization.authorized_method;return"basic"!=c&&"consumer"!=c||b.setRequestHeader("Authorization","Basic "+btoa(a.authorization.username+":"+a.authorization.password)),b}var e=window.location.hostname,f=window.location.protocol,g=$.extend({site_url:f+"//"+e,authorization:{authorized_method:null,username:null,password:null,token:{access_token:null,consumer_key:null,consumer_secret:null}},nonce:null,namespace:"wp/v2/",endpoint:"posts",postData:"{}",filename:"",formMethod:"GET",dataType:"json",cache:!0},a);if(""==g.site_url&&"file:"==f)return console.error("WP REST API jQuery Error: Script can not run as a file."),!1;if(null!==g.authorization.authorized_method)return console.error("WP REST API jQuery Error: Authorization method was not specified."),!1;if(""!==g.authorization.username&&""==g.authorization.password)return console.error("WP REST API jQuery Error: Password for authorization is missing!"),!1;if(""==g.namespace)return console.error("WP REST API jQuery Error: Namespace is unknown!"),!1;if(""==g.endpoint)return console.error("WP REST API jQuery Error: Endpoint was not defined!"),!1;if(""==g.formMethod)return console.error("WP REST API jQuery Error: Ajax method was not set!"),!1;if("GET"!==g.formMethod&&"{}"==g.postData)return console.error("WP REST API jQuery Error: Post data is empty!"),!1;"jsonp"==g.dataType&&(g.cache=!1);var h=!1;null===g.nonce&&null===g.authorization.authorized_method||(h=!0),console.log("Requested Endpoint: "+g.site_url+"/wp-json/"+g.namespace+g.endpoint);var i=!0;return"media"==g.endpoint&&"POST"==g.formMethod&&(i=!1),response=i?b(g):c(g)}; -------------------------------------------------------------------------------- /src/restjquery.js: -------------------------------------------------------------------------------- 1 | ;( function ( $, window, document, undefined ) { 2 | 'use strict'; 3 | 4 | // If there's no jQuery, RESTjQuery can't work. 5 | if (!$) { 6 | return; 7 | } 8 | 9 | var rj_scripts = {}; 10 | 11 | $.fn.restjQuery = function() { 12 | var $this = $( this ); 13 | 14 | var version = "1.0.0"; // Version 15 | var hostName = window.location.hostname; // Returns current host name only. 16 | var protocol = window.location.protocol; // Returns the protocol used. i.e file: or http: or https: 17 | 18 | // Default settings. 19 | var defaults = { 20 | 21 | }; 22 | 23 | rj_scripts = { 24 | } 25 | } 26 | 27 | // Initialize RESTjQuery. 28 | rj_scripts.init(); 29 | }); 30 | 31 | var response = null; 32 | 33 | var restjQuery = function( options ) { 34 | 'use strict'; 35 | 36 | var version = "1.0"; 37 | 38 | var hostName = window.location.hostname; // Returns current host name only. 39 | 40 | var protocol = window.location.protocol; // Returns the protocol used. i.e file: or http: or https: 41 | 42 | // These are the default settings. 43 | var settings = $.extend({ 44 | site_url: protocol + "//" + hostName, // Default is the current host name. Only set if connecting with another site. 45 | authorization: { 46 | authorized_method: null, 47 | username: null, 48 | password: null, 49 | token: { 50 | access_token: null, 51 | consumer_key: null, 52 | consumer_secret: null 53 | } 54 | }, 55 | nonce: null, // Must be set so logged in users can access authorized requests. 56 | namespace: 'wp/v2/', // Default is wp/v2/ for WordPress. For WooCommerce, set it to wc/v1/ 57 | endpoint: 'posts', // Default: Posts 58 | postData: '{}', // Default: Empty JSON 59 | filename: '', // Default: Empty - Used to specify the filename that the media file will be called. 60 | formMethod: 'GET', // Default: GET. Can use POST for posting data. 61 | dataType: 'json', // Default: json - For cross-domain support, set as jsonp 62 | cache: true, // Default: true - If dataType is set as jsonp then this will automatically set to false. 63 | }, options ); 64 | 65 | if ( settings.site_url == '' && protocol == 'file:' ) { 66 | console.error('WP REST API jQuery Error: Script can not run as a file.'); 67 | return false; 68 | } 69 | 70 | if ( settings.authorization.authorized_method !== null ) { 71 | console.error('WP REST API jQuery Error: Authorization method was not specified.'); 72 | return false; 73 | } 74 | 75 | // Checks if a password was entered if username is not empty. 76 | if ( settings.authorization.username !== '' && settings.authorization.password == '' ) { 77 | console.error('WP REST API jQuery Error: Password for authorization is missing!'); 78 | return false; 79 | } 80 | 81 | // Checks that the namespace is identified. 82 | if ( settings.namespace == '' ) { 83 | console.error('WP REST API jQuery Error: Namespace is unknown!'); 84 | return false; 85 | } 86 | 87 | // Checks that the endpoint is defined. 88 | if ( settings.endpoint == '' ) { 89 | console.error('WP REST API jQuery Error: Endpoint was not defined!'); 90 | return false; 91 | } 92 | 93 | // Checks that the AJAX method is not empty. 94 | if ( settings.formMethod == '' ) { 95 | console.error('WP REST API jQuery Error: Ajax method was not set!'); 96 | return false; 97 | } 98 | 99 | // Checks if we are posting data and post data is not empty. 100 | if ( settings.formMethod !== 'GET' && settings.postData == '{}' ) { 101 | console.error('WP REST API jQuery Error: Post data is empty!'); 102 | return false; 103 | } 104 | 105 | // Set cache to false if dataType is set to jsonp. 106 | if ( settings.dataType == 'jsonp' ) { 107 | settings.cache = false; 108 | } 109 | 110 | // Check if the request requires authentication. False by default. 111 | var auth_headers = false; 112 | if ( settings.nonce !== null || settings.authorization.authorized_method !== null ) { 113 | auth_headers = true; 114 | } 115 | 116 | console.log('Requested Endpoint: ' + settings.site_url + "/wp-json/" + settings.namespace + settings.endpoint ); 117 | 118 | var standard_request = true; 119 | 120 | // Checks that we are posting data for uploading media files. 121 | if ( settings.endpoint == 'media' && settings.formMethod == 'POST' ) { 122 | standard_request = false; 123 | } 124 | 125 | if ( standard_request ) { 126 | response = restRequest( settings ); 127 | } 128 | else { 129 | response = restUploadMedia( settings ); 130 | } 131 | 132 | return response; 133 | 134 | /** 135 | * This runs the REST API request. Passes the settings variable. 136 | */ 137 | function restRequest( settings ) { 138 | 139 | // If authorization is requested then we set the appropriate headers. 140 | if ( auth_headers ) { 141 | 142 | console.log('WP REST API jQuery Authenticating Request...'); 143 | 144 | $.ajax({ 145 | async: false, 146 | url: settings.site_url + "/wp-json/" + settings.namespace + settings.endpoint, 147 | method: settings.formMethod, 148 | cache: settings.cache, 149 | crossDomain: true, 150 | crossOrigin: true, 151 | data: settings.postData, 152 | beforeSend: function ( xhr ) { 153 | setHeaders( settings, xhr ); 154 | }, 155 | complete: function( newData ) { 156 | jQuery('body').trigger('restjquery_complete_' + settings.formMethod + '_' + settings.endpoint, [newData]); 157 | 158 | response = newData.responseJSON; 159 | }, 160 | error: function( error ) { 161 | jQuery('body').trigger('restjquery_error_' + settings.formMethod + '_' + settings.endpoint, [error]); 162 | 163 | response = error; 164 | }, 165 | dataType: settings.dataType 166 | }); 167 | 168 | } 169 | else { 170 | 171 | $.ajax({ 172 | async: false, 173 | url: settings.site_url + "/wp-json/" + settings.namespace + settings.endpoint, 174 | method: settings.formMethod, 175 | cache: settings.cache, 176 | contentType: "application/json", 177 | crossDomain: true, 178 | crossOrigin: true, 179 | data: settings.postData, 180 | complete: function( newData ) { 181 | jQuery('body').trigger('restjquery_complete_' + settings.formMethod + '_' + settings.endpoint, [newData]); 182 | 183 | response = newData.responseJSON; 184 | }, 185 | error: function( error ) { 186 | jQuery('body').trigger('restjquery_error_' + settings.formMethod + '_' + settings.endpoint, [error]); 187 | 188 | response = error; 189 | }, 190 | dataType: settings.dataType 191 | }); 192 | 193 | } 194 | 195 | return response; 196 | } 197 | 198 | function restUploadMedia( settings ) { 199 | 200 | $.ajax({ 201 | async: true, 202 | url: settings.site_url + "/wp-json/" + settings.namespace + "media", 203 | method: 'POST', 204 | data: settings.postData, 205 | cache: false, 206 | contentType: false, 207 | processData: false, 208 | headers: { 'Content-Disposition': 'attachment;filename=' + settings.filename }, 209 | beforeSend: function ( xhr ) { 210 | setHeaders( settings, xhr ); 211 | }, 212 | complete: function( newData ) { 213 | response = newData; 214 | console.log(response); 215 | 216 | if ( newData.status == '200' ) { 217 | console.log('Upload Done'); 218 | } 219 | else { 220 | console.error('Upload failed!'); 221 | } 222 | }, 223 | error: function( error ) { 224 | response = error; 225 | }, 226 | }); 227 | 228 | return response; 229 | } 230 | 231 | /** 232 | * Sets the required request headers. 233 | */ 234 | function setHeaders( settings, xhr ) { 235 | if ( settings.nonce !== null ) { 236 | xhr.setRequestHeader( 'X-WP-Nonce', settings.nonce ); 237 | } 238 | 239 | var authorized_method = settings.authorization.authorized_method; 240 | 241 | if ( authorized_method == 'basic' || authorized_method == 'consumer' ) { 242 | xhr.setRequestHeader( 'Authorization', 'Basic ' + btoa( settings.authorization.username + ':' + settings.authorization.password ) ); 243 | } 244 | 245 | return xhr; 246 | } 247 | 248 | }; 249 | --------------------------------------------------------------------------------