Commit d544e089 authored by Michael Iseard's avatar Michael Iseard
Browse files

Move rest routes to separate service

parent ac37a661
......@@ -11,6 +11,7 @@ use Kudos\Service\ActivatorService;
use Kudos\Service\AdminNotice;
use Kudos\Service\LoggerService;
use Kudos\Service\MapperService;
use Kudos\Service\RestRouteService;
use Kudos\Service\TwigService;
/**
......@@ -210,8 +211,8 @@ class Admin {
'kudos',
[
'version' => KUDOS_VERSION,
'checkApiUrl' => rest_url( 'kudos/v1' . '/check-api' ),
'sendTestUrl' => rest_url( 'kudos/v1' . '/email/test' ),
'checkApiUrl' => rest_url( RestRouteService::NAMESPACE . RestRouteService::PAYMENT_TEST ),
'sendTestUrl' => rest_url( RestRouteService::NAMESPACE . RestRouteService::EMAIL_TEST ),
'ajaxurl' => admin_url( 'admin-ajax.php' ),
]
);
......
......@@ -10,6 +10,7 @@ use Kudos\Helpers\Settings;
use Kudos\Helpers\Utils;
use Kudos\Service\MapperService;
use Kudos\Service\PaymentService;
use Kudos\Service\RestRouteService;
/**
* The public-facing functionality of the plugin.
......@@ -142,7 +143,7 @@ class Front {
'kudos',
[
'_wpnonce' => wp_create_nonce( 'wp_rest' ),
'createPaymentUrl' => rest_url( PaymentService::REST_NAMESPACE . PaymentService::PAYMENT_ROUTE ),
'createPaymentUrl' => rest_url( RestRouteService::NAMESPACE . RestRouteService::PAYMENT_CREATE ),
]
);
wp_set_script_translations( $handle, 'kudos-donations', KUDOS_PLUGIN_DIR . '/languages' );
......
......@@ -9,8 +9,8 @@ use Kudos\Entity\TransactionEntity;
use Kudos\Front\Front;
use Kudos\Service\ActivatorService;
use Kudos\Service\I18nService;
use Kudos\Service\MailerService;
use Kudos\Service\PaymentService;
use Kudos\Service\RestRouteService;
/**
* The file that defines the core plugin class
......@@ -157,8 +157,7 @@ class KudosDonations {
*/
private function define_rest_hooks() {
add_action( 'rest_api_init', [ new PaymentService(), 'register_rest_routes' ] );
add_action( 'rest_api_init', [ new MailerService(), 'register_rest_routes' ] );
add_action( 'rest_api_init', [ new RestRouteService(), 'register_all' ] );
}
......
......@@ -9,7 +9,6 @@ use Kudos\Helpers\Utils;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use WP_REST_Request;
use WP_REST_Server;
class MailerService extends AbstractService {
......@@ -34,27 +33,6 @@ class MailerService extends AbstractService {
}
/**
* Register the mailer rest routes.
*/
public function register_rest_routes() {
register_rest_route( 'kudos/v1',
'email/test',
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'send_test' ],
'args' => [
'email' => [
'required' => true,
],
],
'permission_callback' => function () {
return current_user_can( 'manage_options' );
},
] );
}
/**
* Initializes the mailer by modifying default config if setting
* is enabled.
......
......@@ -13,30 +13,9 @@ use Mollie\Api\Resources\Payment;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;
class PaymentService extends AbstractService {
/**
* Namespace used for registering the routes
*/
const REST_NAMESPACE = 'kudos/v1';
/**
* The route used for payment webhook
*/
const WEBHOOK_ROUTE = '/payment/webhook';
/**
* New payment route
*/
const PAYMENT_ROUTE = '/payment/create';
/**
* Rest route used for checking if api key is valid
*/
const TEST_API = '/check-api';
/**
* @var AbstractVendor
*/
......@@ -62,50 +41,6 @@ class PaymentService extends AbstractService {
}
/**
* Register the vendor's rest routes
*
* @since 2.3.0
*/
public function register_rest_routes() {
$routes = [
self::PAYMENT_ROUTE => [
'methods' => 'POST',
'callback' => [ $this, 'submit_payment' ],
'permission_callback' => '__return_true',
],
self::WEBHOOK_ROUTE => [
'methods' => 'POST',
'callback' => [ $this, 'handle_webhook' ],
'args' => [
'id' => [
'required' => true,
],
],
'permission_callback' => '__return_true',
],
self::TEST_API => [
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'check_api_keys' ],
'args' => [
'apiMode' => [
'required' => true,
],
],
'permission_callback' => function () {
return current_user_can( 'manage_options' );
},
],
];
foreach ( $routes as $key => $route ) {
register_rest_route( self::REST_NAMESPACE, $key, $route );
}
}
/**
* Processes the transaction. Used by action scheduler.
*
......@@ -312,7 +247,7 @@ class PaymentService extends AbstractService {
'value' => $value,
],
'redirectUrl' => $redirect_url,
'webhookUrl' => $_ENV['WEBHOOK_URL'] ?? rest_url( self::REST_NAMESPACE . self::WEBHOOK_ROUTE ),
'webhookUrl' => $_ENV['WEBHOOK_URL'] ?? rest_url( RestRouteService::NAMESPACE . RestRouteService::PAYMENT_WEBHOOK ),
'sequenceType' => $sequence_type,
'description' => sprintf(
/* translators: %s: The order id */
......
<?php
namespace Kudos\Service;
use WP_REST_Server;
class RestRouteService {
/**
* Namespace used for registering the routes
*/
const NAMESPACE = 'kudos/v1';
/**
* The route used for payment webhook
*/
const PAYMENT_WEBHOOK = '/payment/webhook';
/**
* New payment route
*/
const PAYMENT_CREATE = '/payment/create';
/**
* Rest route used for checking if api key is valid
*/
const PAYMENT_TEST = '/payment/test-api';
/**
* Route used to send a test email
*/
const EMAIL_TEST = '/email/test';
/**
* @var array[]
*/
private $routes;
/**
* RestRoutesService constructor.
*/
public function __construct() {
$this->payment_service();
$this->mailer_service();
}
/**
* Payment service routes
*
* @since 2.3.4
*/
private function payment_service() {
$payment = new PaymentService();
$this->routes[] = [
self::PAYMENT_CREATE => [
'methods' => 'POST',
'callback' => [ $payment, 'submit_payment' ],
'permission_callback' => '__return_true',
],
self::PAYMENT_WEBHOOK => [
'methods' => 'POST',
'callback' => [ $payment, 'handle_webhook' ],
'args' => [
'id' => [
'required' => true,
],
],
'permission_callback' => '__return_true',
],
self::PAYMENT_TEST => [
'methods' => WP_REST_Server::READABLE,
'callback' => [ $payment, 'check_api_keys' ],
'args' => [
'apiMode' => [
'required' => true,
],
],
'permission_callback' => function () {
return current_user_can( 'manage_options' );
},
],
];
}
/**
* Mail service routes
*
* @since 2.3.4
*/
private function mailer_service() {
$mailer = new MailerService();
$this->routes[] = [
self::EMAIL_TEST => [
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $mailer, 'send_test' ],
'args' => [
'email' => [
'required' => true,
],
],
'permission_callback' => function () {
return current_user_can( 'manage_options' );
},
],
];
}
/**
* Called to register all the routes defined in this service
*
* @since 2.3.4
*/
public function register_all() {
foreach ( $this->routes as $service ) {
foreach ( $service as $key => $route ) {
register_rest_route( self::NAMESPACE, $key, $route );
}
}
}
}
\ No newline at end of file
......@@ -7,7 +7,7 @@ use Kudos\Entity\TransactionEntity;
use Kudos\Helpers\Settings;
use Kudos\Helpers\Utils;
use Kudos\Service\MapperService;
use Kudos\Service\PaymentService;
use Kudos\Service\RestRouteService;
use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\MollieApiClient;
use Mollie\Api\Resources\Customer;
......@@ -278,7 +278,7 @@ class MollieVendor extends AbstractVendor {
'value' => $value,
'currency' => $currency,
],
'webhookUrl' => $_ENV['WEBHOOK_URL'] ?? rest_url( PaymentService::REST_NAMESPACE . PaymentService::WEBHOOK_ROUTE ),
'webhookUrl' => $_ENV['WEBHOOK_URL'] ?? rest_url( RestRouteService::NAMESPACE . RestRouteService::PAYMENT_WEBHOOK ),
'mandateId' => $mandate_id,
'interval' => $interval,
'startDate' => $start_date,
......
......@@ -102,7 +102,7 @@ e.g:
= 2.3.4 =
* Add 'Business name' to address fields
* Fix campaign table warning
* Fix campaign table warnings
= 2.3.3 =
* Fix address fields not showing
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment