Skip to content

Commit

Permalink
import
Browse files Browse the repository at this point in the history
وارد کردن نسخه اولیه
  • Loading branch information
amin30000 committed Mar 5, 2023
0 parents commit 25fc85d
Show file tree
Hide file tree
Showing 551 changed files with 151,387 additions and 0 deletions.
457 changes: 457 additions & 0 deletions ajax-wrapper/AjaxWrapper.php

Large diffs are not rendered by default.

74 changes: 74 additions & 0 deletions ajax-wrapper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# AJAX Action Wrapper

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

### Example
Define action:
```php
$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:
```javascript
AjawV1.getAction('ws_do_something').post(
{
'foo': '...'
},
function(response) {
console.log(response);
}
);
```

### Features
- Automate common, boring stuff.
- [x] Automatically pass the `admin-ajax.php` URL and nonce to JS.
- [x] Define required parameters.
```php
$builder->requiredParam('foo', 'int')
```
- [x] Define optional parameters with default values.
```php
$builder->optionalParam('meaningOfLife', 42, 'int')
```
- [x] Automatically remove "magic quotes" that WordPress adds to `$_GET`, `$_POST` and `$_REQUEST`.
- [x] Encode return values as JSON.
- Security should be the default.
- [x] Generate and verify nonces. Nonce verification is on by default, but can be disabled.
```php
$builder->withoutNonce()
```
- [x] Check capabilities.
```php
$builder->requiredCap('manage_options');
```
- [x] Verify that all required parameters are set.
- [x] Validate parameter values.
```php
$builder->optionalParam('things', 1, 'int', function($value) {
if ($value > 10) {
return new WP_Error(
'excessive_things',
'Too many things!',
400 //HTTP status code.
);
}
})
```
- [x] Set the required HTTP method.
```php
$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.
- [x] 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+.
15 changes: 15 additions & 0 deletions ajax-wrapper/ajax-action-wrapper.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Basic type definitions for the Ajaw AJAX wrapper library 1.0

declare namespace AjawV1 {
interface RequestParams { [name: string]: any }
interface SuccessCallback { (data, textStatus: string, jqXHR): void }
interface ErrorCallback { (data, textStatus: string, jqXHR, errorThrown): void }

class AjawAjaxAction {
get(params?: RequestParams, success?: SuccessCallback, error?: ErrorCallback): void;
post(params?: RequestParams, success?: SuccessCallback, error?: ErrorCallback): void;
request(params?: RequestParams, success?: SuccessCallback, error?: ErrorCallback, method?: string): void;
}

function getAction(action: string): AjawAjaxAction;
}
139 changes: 139 additions & 0 deletions ajax-wrapper/ajax-action-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
var AjawV1 = window.AjawV1 || {};

AjawV1.AjaxAction = (function () {
"use strict";

function AjawAjaxAction(action, properties) {
this.action = action;
this.ajaxUrl = properties['ajaxUrl'];
this.nonce = properties['nonce'];
this.requiredMethod = (typeof properties['method'] !== 'undefined') ? properties['method'] : null;
}

/**
* Send a POST request.
*
* @param {Object} params
* @param {Function} success
* @param {Function} [error]
*/
AjawAjaxAction.prototype.post = function (params, success, error) {
return this.request(params, success, error, 'POST');
};

/**
* Send a GET request.
*
* @param {Object} params
* @param {Function} success
* @param {Function} [error]
*/
AjawAjaxAction.prototype.get = function(params, success, error) {
return this.request(params, success, error, 'GET');
};

/**
* Send an AJAX request using the specified HTTP method.
*
* @param {Object} params
* @param {Function} success
* @param {Function} [error]
* @param {String} [method]
*/
AjawAjaxAction.prototype.request = function(params, success, error, method) {
if (typeof params === 'function') {
//It looks like "params" was omitted and the first argument is actually the success callback.
//Shift all arguments left one step. The reverse order is due to argument binding shenanigans.
method = arguments[2];
error = arguments[1];
success = arguments[0];
params = {};
}

if (typeof params === 'undefined') {
params = {};
} else if (typeof params !== 'object') {
//While jQuery accepts request data in object and string form, this library only supports objects.
throw 'Data that\'s to be sent to the server must be an object, not ' + (typeof params);
}

if (typeof method === 'undefined') {
method = this.requiredMethod || 'POST';
}
if (this.requiredMethod && (method !== this.requiredMethod)) {
throw 'Wrong HTTP method. This action requires ' + this.requiredMethod;
}

//noinspection JSUnusedGlobalSymbols
return jQuery.ajax(
this.ajaxUrl,
{
method: method,
data: this.prepareRequestParams(params),
success: function(data, textStatus, jqXHR) {
if (success) {
success(data, textStatus, jqXHR);
}
},
error: function(jqXHR, textStatus, errorThrown) {
var data = jqXHR.responseText;
if (typeof jqXHR['responseJSON'] !== 'undefined') {
data = jqXHR['responseJSON'];
} else if (typeof jqXHR['responseXML'] !== 'undefined') {
data = jqXHR['responseXML'];
}

if (error) {
error(data, textStatus, jqXHR, errorThrown);
}
}
}
);
};

AjawAjaxAction.prototype.prepareRequestParams = function(params) {
if (params === null) {
params = {};
}

params['action'] = this.action;
if (this.nonce !== null) {
params['_ajax_nonce'] = this.nonce;
}
return params;
};

return AjawAjaxAction;
}());

AjawV1.actionRegistry = (function() {
var actions = {};

return {
/**
*
* @param {String} actionName
* @return {AjawAjaxAction}
*/
get: function(actionName) {
if (actions.hasOwnProperty(actionName)) {
return actions[actionName];
}
return null;
},

add: function(actionName, properties) {
actions[actionName] = new AjawV1.AjaxAction(actionName, properties);
}
}
})();

/**
* Get a registered action wrapper.
*
* @param {string} action
* @return {AjawAjaxAction|null}
*/
AjawV1.getAction = function(action) {
return this.actionRegistry.get(action);
};
88 changes: 88 additions & 0 deletions css/_boxes.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
$amePostboxBorderColor: #ccd0d4; //Was #e5e5e5 before WP 5.3.
$amePostboxShadow: 0 1px 1px rgba(0, 0, 0, 0.04);

@mixin ame-emulated-postbox($toggleWidth: 36px, $horizontalPadding: 12px) {
$borderColor: $amePostboxBorderColor;
$headerBackground: #fff;

position: relative;
box-shadow: $amePostboxShadow;
background: $headerBackground;

margin-bottom: 20px;

.ws-ame-postbox-header {
position: relative;
font-size: 14px;
margin: 0;
line-height: 1.4;

border: 1px solid $borderColor;

h3 {
padding: 10px $horizontalPadding;
margin: 0;
font-size: 1em;
line-height: 1;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
}

.ws-ame-postbox-toggle {
color: #72777c;
background: $headerBackground;

display: block;
font: normal 20px/1 dashicons;
text-align: center;
cursor: pointer;
border: none;

position: absolute;
top: 0;
right: 0;
bottom: 0;
width: $toggleWidth;
height: 100%;
padding: 0;

&:hover {
color: #23282d;
}

&:active, &:focus {
outline: none;
padding: 0;
}

&:before {
content: '\f142';
display: inline-block;
vertical-align: middle;
}

&:after {
display: inline-block;
content: "";
vertical-align: middle;
height: 100%;
}
}

.ws-ame-postbox-content {
border: 1px solid $borderColor;
border-top: none;

padding: $horizontalPadding;
}

&.ws-ame-closed-postbox .ws-ame-postbox-content {
display: none;
}

&.ws-ame-closed-postbox .ws-ame-postbox-toggle:before {
content: '\f140'; //downward triangle
}
}
Loading

0 comments on commit 25fc85d

Please sign in to comment.