From 2e88d7cce0a0184635f43b9a7dba8062fa2034cb Mon Sep 17 00:00:00 2001 From: Jonathan Labreuille Date: Tue, 2 Aug 2016 17:08:43 +0200 Subject: [PATCH] Added API/Endpoint abstract class - (re)Added Endpoints folder to both API and Router - fixed syntax in namespaces - xhr.responseJSON is returned to the fail() - fixed Router endpoints (view in browser, cron,...) --- assets/js/src/ajax.js | 25 ++++++------ lib/API/API.php | 40 +++++-------------- lib/API/Endpoint.php | 25 ++++++++++++ .../AutomatedLatestContent.php | 3 +- lib/API/{ => Endpoints}/Cron.php | 3 +- lib/API/{ => Endpoints}/CustomFields.php | 3 +- lib/API/{ => Endpoints}/Forms.php | 3 +- lib/API/{ => Endpoints}/ImportExport.php | 3 +- lib/API/{ => Endpoints}/Mailer.php | 3 +- .../{ => Endpoints}/NewsletterTemplates.php | 3 +- lib/API/{ => Endpoints}/Newsletters.php | 3 +- lib/API/{ => Endpoints}/Segments.php | 3 +- lib/API/{ => Endpoints}/SendingQueue.php | 3 +- lib/API/{ => Endpoints}/Settings.php | 7 ++-- lib/API/{ => Endpoints}/Setup.php | 4 +- lib/API/{ => Endpoints}/Subscribers.php | 3 +- ...APIErrorResponse.php => ErrorResponse.php} | 2 +- lib/API/{APIResponse.php => Response.php} | 2 +- ...uccessResponse.php => SuccessResponse.php} | 2 +- lib/Config/Populator.php | 7 ---- lib/Cron/CronHelper.php | 2 +- lib/Models/Setting.php | 3 ++ lib/Newsletter/Links/Links.php | 2 +- lib/Newsletter/Url.php | 2 +- lib/Router/{ => Endpoints}/Queue.php | 2 +- lib/Router/{ => Endpoints}/Subscription.php | 2 +- lib/Router/{ => Endpoints}/Track.php | 2 +- lib/Router/{ => Endpoints}/ViewInBrowser.php | 2 +- lib/Router/Front.php | 8 ++-- lib/Subscription/Pages.php | 1 - lib/Subscription/Url.php | 2 +- tests/unit/API/CustomFieldsTest.php | 2 +- tests/unit/API/FormsTest.php | 2 +- tests/unit/API/NewsletterTemplatesTest.php | 2 +- tests/unit/API/NewslettersTest.php | 2 +- tests/unit/API/SegmentsTest.php | 2 +- tests/unit/API/SettingsTest.php | 14 +++---- tests/unit/API/SetupTest.php | 2 +- tests/unit/API/SubscribersTest.php | 2 +- views/settings.html | 7 ++-- 40 files changed, 99 insertions(+), 111 deletions(-) create mode 100644 lib/API/Endpoint.php rename lib/API/{ => Endpoints}/AutomatedLatestContent.php (97%) rename lib/API/{ => Endpoints}/Cron.php (95%) rename lib/API/{ => Endpoints}/CustomFields.php (97%) rename lib/API/{ => Endpoints}/Forms.php (99%) rename lib/API/{ => Endpoints}/ImportExport.php (97%) rename lib/API/{ => Endpoints}/Mailer.php (94%) rename lib/API/{ => Endpoints}/NewsletterTemplates.php (96%) rename lib/API/{ => Endpoints}/Newsletters.php (99%) rename lib/API/{ => Endpoints}/Segments.php (98%) rename lib/API/{ => Endpoints}/SendingQueue.php (99%) rename lib/API/{ => Endpoints}/Settings.php (71%) rename lib/API/{ => Endpoints}/Setup.php (91%) rename lib/API/{ => Endpoints}/Subscribers.php (99%) rename lib/API/{APIErrorResponse.php => ErrorResponse.php} (93%) rename lib/API/{APIResponse.php => Response.php} (96%) rename lib/API/{APISuccessResponse.php => SuccessResponse.php} (89%) rename lib/Router/{ => Endpoints}/Queue.php (85%) rename lib/Router/{ => Endpoints}/Subscription.php (93%) rename lib/Router/{ => Endpoints}/Track.php (91%) rename lib/Router/{ => Endpoints}/ViewInBrowser.php (88%) diff --git a/assets/js/src/ajax.js b/assets/js/src/ajax.js index 36b111126b..0bf8f9c882 100644 --- a/assets/js/src/ajax.js +++ b/assets/js/src/ajax.js @@ -16,9 +16,6 @@ define('ajax', ['mailpoet', 'jquery', 'underscore'], function(MailPoet, jQuery, post: function(options) { return this.request('post', options); }, - delete: function(options) { - return this.request('delete', options); - }, init: function(options) { // merge options this.options = jQuery.extend({}, this.defaults, options); @@ -48,7 +45,7 @@ define('ajax', ['mailpoet', 'jquery', 'underscore'], function(MailPoet, jQuery, // set request params var params = this.getParams(); - var jqXHR; + var deferred = jQuery.Deferred(); // remove null values from the data object if (_.isObject(params.data)) { @@ -59,25 +56,29 @@ define('ajax', ['mailpoet', 'jquery', 'underscore'], function(MailPoet, jQuery, // make ajax request depending on method if(method === 'get') { - jqXHR = jQuery.get( + jQuery.get( this.options.url, params, - this.options.onSuccess, + null, 'json' ); } else { - jqXHR = jQuery.ajax({ - url: this.options.url, - type : 'post', - data: params, - dataType: 'json' + jQuery.post( + this.options.url, + params, + null, + 'json' + ).then(function(data) { + deferred.resolve(); + }, function(xhr) { + deferred.reject(xhr.responseJSON); }); } // clear options this.options = {}; - return jqXHR; + return deferred; } }; }); diff --git a/lib/API/API.php b/lib/API/API.php index 987a074679..b935e622b0 100644 --- a/lib/API/API.php +++ b/lib/API/API.php @@ -36,19 +36,19 @@ class API { function setupAdmin() { if($this->checkToken() === false) { - $this->errorResponse( + (new ErrorResponse( array('unauthorized' => __('This request is not authorized.')), array(), - APIResponse::STATUS_UNAUTHORIZED - )->send(); + Response::STATUS_UNAUTHORIZED + ))->send(); } if($this->checkPermissions() === false) { - $this->errorResponse( + (new ErrorResponse( array('forbidden' => __('You do not have the required permissions.')), array(), - APIResponse::STATUS_FORBIDDEN - )->send(); + Response::STATUS_FORBIDDEN + ))->send(); } $this->processRoute(); @@ -56,9 +56,9 @@ class API { function setupPublic() { if($this->checkToken() === false) { - $response = new APIErrorResponse(array( + $response = new ErrorResponse(array( 'unauthorized' => __('This request is not authorized.') - ), APIResponse::STATUS_UNAUTHORIZED); + ), Response::STATUS_UNAUTHORIZED); $response->send(); } @@ -67,7 +67,7 @@ class API { function processRoute() { $class = ucfirst($_POST['endpoint']); - $endpoint = __NAMESPACE__ . "\\" . $class; + $endpoint = __NAMESPACE__ . "\\Endpoints\\" . $class; $method = $_POST['method']; $doing_ajax = (bool)(defined('DOING_AJAX') && DOING_AJAX); @@ -101,9 +101,7 @@ class API { wp_send_json($response); } } catch(\Exception $e) { - $this->errorResponse(array( - $e->getMessage() - ))->send(); + (new ErrorResponse(array($e->getMessage())))->send(); } } @@ -125,22 +123,4 @@ class API { wp_verify_nonce($_POST['token'], 'mailpoet_token') ); } - - function successResponse( - $data = array(), $meta = array(), $status = APIResponse::STATUS_OK - ) { - - return new APISuccessResponse($data, $meta, $status); - } - - function errorResponse( - $errors = array(), $meta = array(), $status = APIResponse::STATUS_NOT_FOUND - ) { - - return new APIErrorResponse($errors, $meta, $status); - } - - function badRequest($errors = array(), $meta = array()) { - return new APIErrorResponse($errors, $meta, APIResponse::STATUS_BAD_REQUEST); - } } \ No newline at end of file diff --git a/lib/API/Endpoint.php b/lib/API/Endpoint.php new file mode 100644 index 0000000000..f41ca3d9b2 --- /dev/null +++ b/lib/API/Endpoint.php @@ -0,0 +1,25 @@ + $value) { Setting::setValue($name, $value); } - return $this->successResponse(); + return $this->successResponse(Setting::getAll()); } } } diff --git a/lib/API/Setup.php b/lib/API/Endpoints/Setup.php similarity index 91% rename from lib/API/Setup.php rename to lib/API/Endpoints/Setup.php index cd2ea31985..b7a4929f77 100644 --- a/lib/API/Setup.php +++ b/lib/API/Endpoints/Setup.php @@ -1,7 +1,5 @@ 'WordPress' - )); - } - // default sender info based on current user $sender = array( 'name' => $current_user->display_name, diff --git a/lib/Cron/CronHelper.php b/lib/Cron/CronHelper.php index 72b1a4ea4f..d78d5b122c 100644 --- a/lib/Cron/CronHelper.php +++ b/lib/Cron/CronHelper.php @@ -2,7 +2,7 @@ namespace MailPoet\Cron; use MailPoet\Router\Front as FrontRouter; -use MailPoet\Router\Queue as QueueEndpoint; +use MailPoet\Router\Endpoints\Queue as QueueEndpoint; use MailPoet\Models\Setting; use MailPoet\Util\Security; diff --git a/lib/Models/Setting.php b/lib/Models/Setting.php index 9d0a94398d..e94f084149 100644 --- a/lib/Models/Setting.php +++ b/lib/Models/Setting.php @@ -38,6 +38,9 @@ class Setting extends Model { 'interval' => self::DEFAULT_SENDING_FREQUENCY_INTERVAL ) ), + 'task_scheduler' => array( + 'method' => 'WordPress' + ), 'signup_confirmation' => array( 'enabled' => true, 'subject' => sprintf(__('Confirm your subscription to %1$s'), get_option('blogname')), diff --git a/lib/Newsletter/Links/Links.php b/lib/Newsletter/Links/Links.php index f7066f9476..912f44aa8a 100644 --- a/lib/Newsletter/Links/Links.php +++ b/lib/Newsletter/Links/Links.php @@ -2,7 +2,7 @@ namespace MailPoet\Newsletter\Links; use MailPoet\Router\Front as FrontRouter; -use MailPoet\Router\Track as TrackEndpoint; +use MailPoet\Router\Endpoints\Track as TrackEndpoint; use MailPoet\Models\NewsletterLink; use MailPoet\Newsletter\Shortcodes\Shortcodes; use MailPoet\Util\Security; diff --git a/lib/Newsletter/Url.php b/lib/Newsletter/Url.php index 18e52fa34c..5601dba5fa 100644 --- a/lib/Newsletter/Url.php +++ b/lib/Newsletter/Url.php @@ -2,7 +2,7 @@ namespace MailPoet\Newsletter; use MailPoet\Router\Front as FrontRouter; -use MailPoet\Router\ViewInBrowser as ViewInBrowserEndpoint; +use MailPoet\Router\Endpoints\ViewInBrowser as ViewInBrowserEndpoint; use MailPoet\Models\Subscriber; class Url { diff --git a/lib/Router/Queue.php b/lib/Router/Endpoints/Queue.php similarity index 85% rename from lib/Router/Queue.php rename to lib/Router/Endpoints/Queue.php index 36740d5273..a4719af087 100644 --- a/lib/Router/Queue.php +++ b/lib/Router/Endpoints/Queue.php @@ -1,5 +1,5 @@ endpoint); + $class = __NAMESPACE__ . "\\Endpoints\\" . ucfirst($this->endpoint); + if(!$this->api_request) return; - if(!$this->endpoint || !class_exists($endpoint)) { + if(!$this->endpoint || !class_exists($class)) { self::terminateRequest(self::RESPONSE_ERROR, __('Invalid Router endpoint.')); } $this->callEndpoint( - $endpoint, + $class, $this->action, $this->data ); diff --git a/lib/Subscription/Pages.php b/lib/Subscription/Pages.php index 331bbb7504..20717f63c3 100644 --- a/lib/Subscription/Pages.php +++ b/lib/Subscription/Pages.php @@ -1,7 +1,6 @@ get(); - expect($response->status)->equals(APIResponse::STATUS_OK); + expect($response->status)->equals(Response::STATUS_OK); expect($response->data)->notEmpty(); expect($response->data['some']['setting']['key'])->true(); Setting::deleteMany(); $response = $router->get(); - expect($response->status)->equals(APIResponse::STATUS_OK); + expect($response->status)->equals(Response::STATUS_OK); expect($response->data)->equals(Setting::getDefaults()); } @@ -36,13 +36,13 @@ class SettingsTest extends MailPoetTest { $router = new Settings(); $response = $router->set(/* missing data */); - expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST); + expect($response->status)->equals(Response::STATUS_BAD_REQUEST); $response = $router->set($new_settings); - expect($response->status)->equals(APIResponse::STATUS_OK); + expect($response->status)->equals(Response::STATUS_OK); $response = $router->get(); - expect($response->status)->equals(APIResponse::STATUS_OK); + expect($response->status)->equals(Response::STATUS_OK); expect($response->data['some']['setting'])->hasntKey('key'); expect($response->data['some']['setting']['new_key'])->true(); expect($response->data['some']['new_setting'])->true(); diff --git a/tests/unit/API/SetupTest.php b/tests/unit/API/SetupTest.php index e79e05fed9..fe44d2f3d8 100644 --- a/tests/unit/API/SetupTest.php +++ b/tests/unit/API/SetupTest.php @@ -1,5 +1,5 @@ ", { scroll: true } ); MailPoet.Modal.loading(false); - }).error(function(xhr) { - var response = xhr.responseJSON; - if(response.errors !== undefined) { + }).fail(function(response) { + if (response.errors !== undefined) { MailPoet.Notice.error( response.errors.map(function(error) { return error.message; }), { scroll: true }