Abstracts WP functions
This commit is contained in:
@ -7,6 +7,7 @@ use MailPoet\Router\Endpoints\CronDaemon as CronDaemonEndpoint;
|
|||||||
use MailPoet\Router\Router;
|
use MailPoet\Router\Router;
|
||||||
use MailPoet\Util\Security;
|
use MailPoet\Util\Security;
|
||||||
use MailPoet\WP\Hooks as WPHooks;
|
use MailPoet\WP\Hooks as WPHooks;
|
||||||
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
@ -54,7 +55,7 @@ class CronHelper {
|
|||||||
);
|
);
|
||||||
$result = self::queryCronUrl($url);
|
$result = self::queryCronUrl($url);
|
||||||
if (is_wp_error($result)) return $result->get_error_message();
|
if (is_wp_error($result)) return $result->get_error_message();
|
||||||
$response = wp_remote_retrieve_body($result);
|
$response = WPFunctions::wpRemoteRetrieveBody($result);
|
||||||
return (!$validate_response) ?
|
return (!$validate_response) ?
|
||||||
$response :
|
$response :
|
||||||
$response === Daemon::PING_SUCCESS_RESPONSE;
|
$response === Daemon::PING_SUCCESS_RESPONSE;
|
||||||
@ -67,7 +68,7 @@ class CronHelper {
|
|||||||
$data
|
$data
|
||||||
);
|
);
|
||||||
$result = self::queryCronUrl($url);
|
$result = self::queryCronUrl($url);
|
||||||
return wp_remote_retrieve_body($result);
|
return WPFunctions::wpRemoteRetrieveBody($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
static function queryCronUrl($url) {
|
static function queryCronUrl($url) {
|
||||||
@ -80,7 +81,7 @@ class CronHelper {
|
|||||||
'user-agent' => 'MailPoet Cron'
|
'user-agent' => 'MailPoet Cron'
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
return wp_remote_get($url, $args);
|
return WPFunctions::wpRemoteGet($url, $args);
|
||||||
}
|
}
|
||||||
|
|
||||||
static function getCronUrl($action, $data = false) {
|
static function getCronUrl($action, $data = false) {
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
namespace MailPoet\Mailer\Methods;
|
namespace MailPoet\Mailer\Methods;
|
||||||
|
|
||||||
use MailPoet\Mailer\Mailer;
|
use MailPoet\Mailer\Mailer;
|
||||||
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
@ -51,7 +52,7 @@ class AmazonSES {
|
|||||||
|
|
||||||
function send($newsletter, $subscriber, $extra_params = array()) {
|
function send($newsletter, $subscriber, $extra_params = array()) {
|
||||||
try {
|
try {
|
||||||
$result = wp_remote_post(
|
$result = WPFunctions::wpRemotePost(
|
||||||
$this->url,
|
$this->url,
|
||||||
$this->request($newsletter, $subscriber, $extra_params)
|
$this->request($newsletter, $subscriber, $extra_params)
|
||||||
);
|
);
|
||||||
@ -61,8 +62,8 @@ class AmazonSES {
|
|||||||
if(is_wp_error($result)) {
|
if(is_wp_error($result)) {
|
||||||
return Mailer::formatMailerConnectionErrorResult($result->get_error_message());
|
return Mailer::formatMailerConnectionErrorResult($result->get_error_message());
|
||||||
}
|
}
|
||||||
if(wp_remote_retrieve_response_code($result) !== 200) {
|
if(WPFunctions::wpRemoteRetrieveResponseCode($result) !== 200) {
|
||||||
$response = simplexml_load_string(wp_remote_retrieve_body($result));
|
$response = simplexml_load_string(WPFunctions::wpRemoteRetrieveBody($result));
|
||||||
$response = ($response) ?
|
$response = ($response) ?
|
||||||
$response->Error->Message->__toString() :
|
$response->Error->Message->__toString() :
|
||||||
sprintf(__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_AMAZONSES);
|
sprintf(__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_AMAZONSES);
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace MailPoet\Mailer\Methods;
|
namespace MailPoet\Mailer\Methods;
|
||||||
|
|
||||||
use MailPoet\Mailer\Mailer;
|
use MailPoet\Mailer\Mailer;
|
||||||
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
@ -18,14 +20,14 @@ class SendGrid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function send($newsletter, $subscriber, $extra_params = array()) {
|
function send($newsletter, $subscriber, $extra_params = array()) {
|
||||||
$result = wp_remote_post(
|
$result = WPFunctions::wpRemotePost(
|
||||||
$this->url,
|
$this->url,
|
||||||
$this->request($newsletter, $subscriber, $extra_params)
|
$this->request($newsletter, $subscriber, $extra_params)
|
||||||
);
|
);
|
||||||
if(is_wp_error($result)) {
|
if(is_wp_error($result)) {
|
||||||
return Mailer::formatMailerConnectionErrorResult($result->get_error_message());
|
return Mailer::formatMailerConnectionErrorResult($result->get_error_message());
|
||||||
}
|
}
|
||||||
if(wp_remote_retrieve_response_code($result) !== 200) {
|
if(WPFunctions::wpRemoteRetrieveResponseCode($result) !== 200) {
|
||||||
$response = json_decode($result['body'], true);
|
$response = json_decode($result['body'], true);
|
||||||
$response = (!empty($response['errors'][0])) ?
|
$response = (!empty($response['errors'][0])) ?
|
||||||
$response['errors'][0] :
|
$response['errors'][0] :
|
||||||
|
@ -5,6 +5,7 @@ namespace MailPoet\Services;
|
|||||||
use MailPoet\Mailer\Mailer;
|
use MailPoet\Mailer\Mailer;
|
||||||
use MailPoet\Models\Setting;
|
use MailPoet\Models\Setting;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
@ -55,8 +56,8 @@ class Bridge {
|
|||||||
'blocking' => true,
|
'blocking' => true,
|
||||||
'timeout' => 10
|
'timeout' => 10
|
||||||
);
|
);
|
||||||
$result = wp_remote_get(self::BRIDGE_URL, $params);
|
$result = WPFunctions::wpRemoteGet(self::BRIDGE_URL, $params);
|
||||||
return wp_remote_retrieve_response_code($result) === 200;
|
return WPFunctions::wpRemoteRetrieveResponseCode($result) === 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
function initApi($api_key) {
|
function initApi($api_key) {
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace MailPoet\Services\Bridge;
|
namespace MailPoet\Services\Bridge;
|
||||||
|
|
||||||
use MailPoet\WP\Hooks as WPHooks;
|
use MailPoet\WP\Hooks as WPHooks;
|
||||||
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
@ -34,10 +35,10 @@ class API {
|
|||||||
array('site' => home_url())
|
array('site' => home_url())
|
||||||
);
|
);
|
||||||
|
|
||||||
$code = wp_remote_retrieve_response_code($result);
|
$code = WPFunctions::wpRemoteRetrieveResponseCode($result);
|
||||||
switch($code) {
|
switch($code) {
|
||||||
case 200:
|
case 200:
|
||||||
$body = json_decode(wp_remote_retrieve_body($result), true);
|
$body = json_decode(WPFunctions::wpRemoteRetrieveBody($result), true);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$body = null;
|
$body = null;
|
||||||
@ -53,10 +54,10 @@ class API {
|
|||||||
array('site' => home_url())
|
array('site' => home_url())
|
||||||
);
|
);
|
||||||
|
|
||||||
$code = wp_remote_retrieve_response_code($result);
|
$code = WPFunctions::wpRemoteRetrieveResponseCode($result);
|
||||||
switch($code) {
|
switch($code) {
|
||||||
case 200:
|
case 200:
|
||||||
if($body = wp_remote_retrieve_body($result)) {
|
if($body = WPFunctions::wpRemoteRetrieveBody($result)) {
|
||||||
$body = json_decode($body, true);
|
$body = json_decode($body, true);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -80,11 +81,11 @@ class API {
|
|||||||
'message' => $result->get_error_message()
|
'message' => $result->get_error_message()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
$response_code = wp_remote_retrieve_response_code($result);
|
$response_code = WPFunctions::wpRemoteRetrieveResponseCode($result);
|
||||||
if($response_code !== 201) {
|
if($response_code !== 201) {
|
||||||
$response = (wp_remote_retrieve_body($result)) ?
|
$response = (WPFunctions::wpRemoteRetrieveBody($result)) ?
|
||||||
wp_remote_retrieve_body($result) :
|
WPFunctions::wpRemoteRetrieveBody($result) :
|
||||||
wp_remote_retrieve_response_message($result);
|
WPFunctions::wpRemoteRetrieveResponseMessage($result);
|
||||||
return array(
|
return array(
|
||||||
'status' => self::SENDING_STATUS_SEND_ERROR,
|
'status' => self::SENDING_STATUS_SEND_ERROR,
|
||||||
'message' => $response,
|
'message' => $response,
|
||||||
@ -99,8 +100,8 @@ class API {
|
|||||||
$this->url_bounces,
|
$this->url_bounces,
|
||||||
$emails
|
$emails
|
||||||
);
|
);
|
||||||
if(wp_remote_retrieve_response_code($result) === 200) {
|
if(WPFunctions::wpRemoteRetrieveResponseCode($result) === 200) {
|
||||||
return json_decode(wp_remote_retrieve_body($result), true);
|
return json_decode(WPFunctions::wpRemoteRetrieveBody($result), true);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -111,7 +112,7 @@ class API {
|
|||||||
array('subscriber_count' => (int)$count),
|
array('subscriber_count' => (int)$count),
|
||||||
'PUT'
|
'PUT'
|
||||||
);
|
);
|
||||||
return wp_remote_retrieve_response_code($result) === self::RESPONSE_CODE_STATS_SAVED;
|
return WPFunctions::wpRemoteRetrieveResponseCode($result) === self::RESPONSE_CODE_STATS_SAVED;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setKey($api_key) {
|
function setKey($api_key) {
|
||||||
@ -137,6 +138,6 @@ class API {
|
|||||||
),
|
),
|
||||||
'body' => json_encode($body)
|
'body' => json_encode($body)
|
||||||
);
|
);
|
||||||
return wp_remote_post($url, $params);
|
return WPFunctions::wpRemotePost($url, $params);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace MailPoet\Services\Release;
|
namespace MailPoet\Services\Release;
|
||||||
|
|
||||||
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class API {
|
class API {
|
||||||
@ -17,10 +20,10 @@ class API {
|
|||||||
$this->url_products . $plugin_name
|
$this->url_products . $plugin_name
|
||||||
);
|
);
|
||||||
|
|
||||||
$code = wp_remote_retrieve_response_code($result);
|
$code = WPFunctions::wpRemoteRetrieveResponseCode($result);
|
||||||
switch($code) {
|
switch($code) {
|
||||||
case 200:
|
case 200:
|
||||||
if($body = wp_remote_retrieve_body($result)) {
|
if($body = WPFunctions::wpRemoteRetrieveBody($result)) {
|
||||||
$body = json_decode($body);
|
$body = json_decode($body);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -47,6 +50,6 @@ class API {
|
|||||||
'timeout' => 10,
|
'timeout' => 10,
|
||||||
'httpversion' => '1.0'
|
'httpversion' => '1.0'
|
||||||
);
|
);
|
||||||
return wp_remote_get($url, $args);
|
return WPFunctions::wpRemoteGet($url, $args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
32
lib/WP/Functions.php
Normal file
32
lib/WP/Functions.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
namespace MailPoet\WP;
|
||||||
|
|
||||||
|
class Functions {
|
||||||
|
static function wpRemotePost() {
|
||||||
|
return self::callWithFallback('wp_remote_post', func_get_args());
|
||||||
|
}
|
||||||
|
|
||||||
|
static function wpRemoteGet() {
|
||||||
|
return self::callWithFallback('wp_remote_get', func_get_args());
|
||||||
|
}
|
||||||
|
|
||||||
|
static function wpRemoteRetrieveBody() {
|
||||||
|
return self::callWithFallback('wp_remote_retrieve_body', func_get_args());
|
||||||
|
}
|
||||||
|
|
||||||
|
static function wpRemoteRetrieveResponseCode() {
|
||||||
|
return self::callWithFallback('wp_remote_retrieve_response_code', func_get_args());
|
||||||
|
}
|
||||||
|
|
||||||
|
static function wpRemoteRetrieveResponseMessage() {
|
||||||
|
return self::callWithFallback('wp_remote_retrieve_response_message', func_get_args());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function callWithFallback($func, $args) {
|
||||||
|
$local_func = __NAMESPACE__ . '\\' . $func;
|
||||||
|
if(function_exists($local_func)) {
|
||||||
|
return call_user_func_array($local_func, $args);
|
||||||
|
}
|
||||||
|
return call_user_func_array($func, $args);
|
||||||
|
}
|
||||||
|
}
|
@ -147,7 +147,7 @@ class CronHelperTest extends \MailPoetTest {
|
|||||||
return $request_args;
|
return $request_args;
|
||||||
};
|
};
|
||||||
add_filter('mailpoet_cron_request_args', $filter);
|
add_filter('mailpoet_cron_request_args', $filter);
|
||||||
Mock::func('MailPoet\Cron', 'wp_remote_get', function($url, $args) {
|
Mock::func('MailPoet\WP', 'wp_remote_get', function($url, $args) {
|
||||||
return $args;
|
return $args;
|
||||||
});
|
});
|
||||||
expect(CronHelper::queryCronUrl('test'))->equals($request_args);
|
expect(CronHelper::queryCronUrl('test'))->equals($request_args);
|
||||||
|
@ -1,16 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace MailPoet\Test\Services;
|
namespace MailPoet\Test\Services;
|
||||||
|
|
||||||
use AspectMock\Test as Mock;
|
use AspectMock\Test as Mock;
|
||||||
use Codeception\Util\Stub;
|
use Codeception\Util\Stub;
|
||||||
use MailPoet\Services\Bridge;
|
|
||||||
use MailPoet\Services\Bridge\API;
|
|
||||||
use MailPoet\Mailer\Mailer;
|
use MailPoet\Mailer\Mailer;
|
||||||
use MailPoet\Models\Setting;
|
use MailPoet\Models\Setting;
|
||||||
|
use MailPoet\Services\Bridge;
|
||||||
|
use MailPoet\Services\Bridge\API;
|
||||||
|
use MailPoet\Services\Bridge\BridgeTestMockAPI as MockAPI;
|
||||||
|
use MailPoet\WP\Hooks as WPHooks;
|
||||||
|
|
||||||
require_once('BridgeTestMockAPI.php');
|
require_once('BridgeTestMockAPI.php');
|
||||||
use MailPoet\Services\Bridge\BridgeTestMockAPI as MockAPI;
|
|
||||||
use MailPoet\WP\Hooks;
|
|
||||||
|
|
||||||
class BridgeTest extends \MailPoetTest {
|
class BridgeTest extends \MailPoetTest {
|
||||||
function _before() {
|
function _before() {
|
||||||
@ -255,7 +256,7 @@ class BridgeTest extends \MailPoetTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function testItAllowChangingRequestTimeout() {
|
function testItAllowChangingRequestTimeout() {
|
||||||
$mock = Mock::func('MailPoet\Services\Bridge', 'wp_remote_post', true);
|
$mock = Mock::func('MailPoet\WP', 'wp_remote_post', true);
|
||||||
$api = new API('test_key');
|
$api = new API('test_key');
|
||||||
|
|
||||||
// test default request value
|
// test default request value
|
||||||
@ -267,10 +268,10 @@ class BridgeTest extends \MailPoetTest {
|
|||||||
$filter = function() use ($custom_request_value) {
|
$filter = function() use ($custom_request_value) {
|
||||||
return $custom_request_value;
|
return $custom_request_value;
|
||||||
};
|
};
|
||||||
Hooks::addFilter('mailpoet_bridge_api_request_timeout', $filter);
|
WPHooks::addFilter('mailpoet_bridge_api_request_timeout', $filter);
|
||||||
$api->sendMessages('test');
|
$api->sendMessages('test');
|
||||||
expect($mock->getCallsForMethod('wp_remote_post')[1][1]['timeout'])->equals($custom_request_value);
|
expect($mock->getCallsForMethod('wp_remote_post')[1][1]['timeout'])->equals($custom_request_value);
|
||||||
Hooks::removeFilter('mailpoet_bridge_api_request_timeout', $filter);
|
WPHooks::removeFilter('mailpoet_bridge_api_request_timeout', $filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function setMailPoetSendingMethod() {
|
private function setMailPoetSendingMethod() {
|
||||||
|
Reference in New Issue
Block a user