Wrap calls in DateTime and Emoji classes
This commit is contained in:
@@ -41,11 +41,14 @@ class Newsletter extends Model {
|
|||||||
// automatic newsletters status
|
// automatic newsletters status
|
||||||
const STATUS_ACTIVE = 'active';
|
const STATUS_ACTIVE = 'active';
|
||||||
|
|
||||||
|
private $emoji;
|
||||||
|
|
||||||
function __construct() {
|
function __construct() {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->addValidations('type', array(
|
$this->addValidations('type', array(
|
||||||
'required' => __('Please specify a type.', 'mailpoet')
|
'required' => __('Please specify a type.', 'mailpoet')
|
||||||
));
|
));
|
||||||
|
$this->emoji = new Emoji();
|
||||||
}
|
}
|
||||||
|
|
||||||
function queue() {
|
function queue() {
|
||||||
@@ -105,7 +108,7 @@ class Newsletter extends Model {
|
|||||||
}
|
}
|
||||||
$this->set(
|
$this->set(
|
||||||
'body',
|
'body',
|
||||||
Emoji::encodeForUTF8Column(self::$_table, 'body', $this->body)
|
$this->emoji->encodeForUTF8Column(self::$_table, 'body', $this->body)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -24,12 +24,15 @@ class SendingQueue extends Model {
|
|||||||
const PRIORITY_MEDIUM = 5;
|
const PRIORITY_MEDIUM = 5;
|
||||||
const PRIORITY_LOW = 10;
|
const PRIORITY_LOW = 10;
|
||||||
|
|
||||||
|
private $emoji;
|
||||||
|
|
||||||
function __construct() {
|
function __construct() {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
$this->addValidations('newsletter_rendered_body', array(
|
$this->addValidations('newsletter_rendered_body', array(
|
||||||
'validRenderedNewsletterBody' => __('Rendered newsletter body is invalid!', 'mailpoet')
|
'validRenderedNewsletterBody' => __('Rendered newsletter body is invalid!', 'mailpoet')
|
||||||
));
|
));
|
||||||
|
$this->emoji = new Emoji();
|
||||||
}
|
}
|
||||||
|
|
||||||
function task() {
|
function task() {
|
||||||
@@ -107,7 +110,7 @@ class SendingQueue extends Model {
|
|||||||
function encodeEmojisInBody($newsletter_rendered_body) {
|
function encodeEmojisInBody($newsletter_rendered_body) {
|
||||||
if(is_array($newsletter_rendered_body)) {
|
if(is_array($newsletter_rendered_body)) {
|
||||||
foreach($newsletter_rendered_body as $key => $value) {
|
foreach($newsletter_rendered_body as $key => $value) {
|
||||||
$newsletter_rendered_body[$key] = Emoji::encodeForUTF8Column(
|
$newsletter_rendered_body[$key] = $this->emoji->encodeForUTF8Column(
|
||||||
self::$_table,
|
self::$_table,
|
||||||
'newsletter_rendered_body',
|
'newsletter_rendered_body',
|
||||||
$value
|
$value
|
||||||
@@ -120,7 +123,7 @@ class SendingQueue extends Model {
|
|||||||
function decodeEmojisInBody($newsletter_rendered_body) {
|
function decodeEmojisInBody($newsletter_rendered_body) {
|
||||||
if(is_array($newsletter_rendered_body)) {
|
if(is_array($newsletter_rendered_body)) {
|
||||||
foreach($newsletter_rendered_body as $key => $value) {
|
foreach($newsletter_rendered_body as $key => $value) {
|
||||||
$newsletter_rendered_body[$key] = Emoji::decodeEntities($value);
|
$newsletter_rendered_body[$key] = $this->emoji->decodeEntities($value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $newsletter_rendered_body;
|
return $newsletter_rendered_body;
|
||||||
|
@@ -12,18 +12,21 @@ class DateTime {
|
|||||||
|
|
||||||
private $wp;
|
private $wp;
|
||||||
|
|
||||||
function __construct() {
|
function __construct(WPFunctions $wp = null) {
|
||||||
$this->wp = new WPFunctions();
|
if($wp === null) {
|
||||||
|
$wp = new WPFunctions();
|
||||||
|
}
|
||||||
|
$this->wp = $wp;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTimeFormat() {
|
function getTimeFormat() {
|
||||||
$time_format = get_option('time_format');
|
$time_format = $this->wp->getOption('time_format');
|
||||||
if (empty($time_format)) $time_format = self::DEFAULT_TIME_FORMAT;
|
if (empty($time_format)) $time_format = self::DEFAULT_TIME_FORMAT;
|
||||||
return $time_format;
|
return $time_format;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDateFormat() {
|
function getDateFormat() {
|
||||||
$date_format = get_option('date_format');
|
$date_format = $this->wp->getOption('date_format');
|
||||||
if (empty($date_format)) $date_format = self::DEFAULT_DATE_FORMAT;
|
if (empty($date_format)) $date_format = self::DEFAULT_DATE_FORMAT;
|
||||||
return $date_format;
|
return $date_format;
|
||||||
}
|
}
|
||||||
|
@@ -1,17 +1,27 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\WP;
|
namespace MailPoet\WP;
|
||||||
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
class Emoji {
|
class Emoji {
|
||||||
static function encodeForUTF8Column($table, $field, $value) {
|
private $wp;
|
||||||
|
|
||||||
|
function __construct(WPFunctions $wp = null) {
|
||||||
|
if($wp === null) {
|
||||||
|
$wp = new WPFunctions();
|
||||||
|
}
|
||||||
|
$this->wp = $wp;
|
||||||
|
}
|
||||||
|
|
||||||
|
function encodeForUTF8Column($table, $field, $value) {
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
$charset = $wpdb->get_col_charset($table, $field);
|
$charset = $wpdb->get_col_charset($table, $field);
|
||||||
if($charset === 'utf8') {
|
if($charset === 'utf8') {
|
||||||
$value = wp_encode_emoji($value);
|
$value = $this->wp->wpEncodeEmoji($value);
|
||||||
}
|
}
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function decodeEntities($content) {
|
function decodeEntities($content) {
|
||||||
// Based on wp_staticize_emoji()
|
// Based on wp_staticize_emoji()
|
||||||
|
|
||||||
// Loosely match the Emoji Unicode range.
|
// Loosely match the Emoji Unicode range.
|
||||||
|
@@ -34,6 +34,13 @@ class Functions {
|
|||||||
|
|
||||||
function isUserLoggedIn() {
|
function isUserLoggedIn() {
|
||||||
return call_user_func_array('is_user_logged_in', func_get_args());
|
return call_user_func_array('is_user_logged_in', func_get_args());
|
||||||
|
|
||||||
|
function getOption() {
|
||||||
|
return call_user_func_array('get_option', func_get_args());
|
||||||
|
}
|
||||||
|
|
||||||
|
function wpEncodeEmoji() {
|
||||||
|
return call_user_func_array('wp_encode_emoji', func_get_args());
|
||||||
}
|
}
|
||||||
|
|
||||||
function getImageInfo($id) {
|
function getImageInfo($id) {
|
||||||
|
@@ -1,62 +1,73 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Test\WP;
|
namespace MailPoet\Test\WP;
|
||||||
|
|
||||||
use Helper\WordPress as WordPressHelper;
|
use Codeception\Stub;
|
||||||
use MailPoet\WP\DateTime;
|
use MailPoet\WP\DateTime as WPDateTime;
|
||||||
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
class DateTimeTest extends \MailPoetTest {
|
class DateTimeTest extends \MailPoetTest {
|
||||||
function _before() {
|
|
||||||
$this->date_time = new DateTime();
|
|
||||||
}
|
|
||||||
|
|
||||||
function testGetTimeFormat() {
|
function testGetTimeFormat() {
|
||||||
WordPressHelper::interceptFunction('get_option', function($key) {
|
$date_time = new WPDateTime(Stub::make(new WPFunctions(), [
|
||||||
return 'H:i';
|
'getOption' => function($key) {
|
||||||
});
|
return 'H:i';
|
||||||
expect($this->date_time->getTimeFormat())->equals('H:i');
|
}
|
||||||
|
]));
|
||||||
|
expect($date_time->getTimeFormat())->equals('H:i');
|
||||||
|
|
||||||
WordPressHelper::interceptFunction('get_option', function($key) {
|
$date_time = new WPDateTime(Stub::make(new WPFunctions(), [
|
||||||
return '';
|
'getOption' => function($key) {
|
||||||
});
|
return '';
|
||||||
expect($this->date_time->getTimeFormat())->equals('H:i:s');
|
}
|
||||||
|
]));
|
||||||
|
expect($date_time->getTimeFormat())->equals('H:i:s');
|
||||||
}
|
}
|
||||||
|
|
||||||
function testGetDateFormat() {
|
function testGetDateFormat() {
|
||||||
WordPressHelper::interceptFunction('get_option', function($key) {
|
$date_time = new WPDateTime(Stub::make(new WPFunctions(), [
|
||||||
return 'm-d';
|
'getOption' => function($key) {
|
||||||
});
|
return 'm-d';
|
||||||
expect($this->date_time->getDateFormat())->equals('m-d');
|
}
|
||||||
|
]));
|
||||||
|
expect($date_time->getDateFormat())->equals('m-d');
|
||||||
|
|
||||||
WordPressHelper::interceptFunction('get_option', function($key) {
|
$date_time = new WPDateTime(Stub::make(new WPFunctions(), [
|
||||||
return '';
|
'getOption' => function($key) {
|
||||||
});
|
return '';
|
||||||
expect($this->date_time->getDateFormat())->equals('Y-m-d');
|
}
|
||||||
|
]));
|
||||||
|
expect($date_time->getDateFormat())->equals('Y-m-d');
|
||||||
}
|
}
|
||||||
|
|
||||||
function testGetCurrentDate() {
|
function testGetCurrentDate() {
|
||||||
expect($this->date_time->getCurrentDate("Y-m"))->equals(date("Y-m"));
|
$date_time = new WPDateTime();
|
||||||
|
expect($date_time->getCurrentDate("Y-m"))->equals(date("Y-m"));
|
||||||
}
|
}
|
||||||
|
|
||||||
function testGetCurrentTime() {
|
function testGetCurrentTime() {
|
||||||
expect($this->date_time->getCurrentTime("i:s"))->regExp('/\d\d:\d\d/');
|
$date_time = new WPDateTime();
|
||||||
|
expect($date_time->getCurrentTime("i:s"))->regExp('/\d\d:\d\d/');
|
||||||
}
|
}
|
||||||
|
|
||||||
function testFormatTime() {
|
function testFormatTime() {
|
||||||
|
$date_time = new WPDateTime();
|
||||||
$timestamp = 1234567;
|
$timestamp = 1234567;
|
||||||
$format = "H:i:s";
|
$format = "H:i:s";
|
||||||
expect($this->date_time->formatTime($timestamp))->equals(date($this->date_time->getTimeFormat(), $timestamp));
|
expect($date_time->formatTime($timestamp))->equals(date($date_time->getTimeFormat(), $timestamp));
|
||||||
expect($this->date_time->formatTime($timestamp, $format))->equals(date($format, $timestamp));
|
expect($date_time->formatTime($timestamp, $format))->equals(date($format, $timestamp));
|
||||||
}
|
}
|
||||||
|
|
||||||
function testFormatDate() {
|
function testFormatDate() {
|
||||||
|
$date_time = new WPDateTime();
|
||||||
$timestamp = 1234567;
|
$timestamp = 1234567;
|
||||||
$format = "Y-m-d";
|
$format = "Y-m-d";
|
||||||
expect($this->date_time->formatDate($timestamp))->equals(date($this->date_time->getDateFormat(), $timestamp));
|
expect($date_time->formatDate($timestamp))->equals(date($date_time->getDateFormat(), $timestamp));
|
||||||
expect($this->date_time->formatDate($timestamp, $format))->equals(date($format, $timestamp));
|
expect($date_time->formatDate($timestamp, $format))->equals(date($format, $timestamp));
|
||||||
}
|
}
|
||||||
|
|
||||||
function testTimeInterval() {
|
function testTimeInterval() {
|
||||||
$one_hour_interval = array_keys($this->date_time->getTimeInterval(
|
$date_time = new WPDateTime();
|
||||||
|
$one_hour_interval = array_keys($date_time->getTimeInterval(
|
||||||
'00:00:00',
|
'00:00:00',
|
||||||
'+1 hour',
|
'+1 hour',
|
||||||
$total_steps = 5
|
$total_steps = 5
|
||||||
@@ -65,7 +76,7 @@ class DateTimeTest extends \MailPoetTest {
|
|||||||
'00:00:00', '01:00:00', '02:00:00', '03:00:00', '04:00:00');
|
'00:00:00', '01:00:00', '02:00:00', '03:00:00', '04:00:00');
|
||||||
expect($one_hour_interval)->equals($one_hour_expected);
|
expect($one_hour_interval)->equals($one_hour_expected);
|
||||||
|
|
||||||
$quarter_hour_interval = array_keys($this->date_time->getTimeInterval(
|
$quarter_hour_interval = array_keys($date_time->getTimeInterval(
|
||||||
'00:00:00',
|
'00:00:00',
|
||||||
'+15 minute',
|
'+15 minute',
|
||||||
$total_steps = 5
|
$total_steps = 5
|
||||||
@@ -75,7 +86,7 @@ class DateTimeTest extends \MailPoetTest {
|
|||||||
);
|
);
|
||||||
expect($quarter_hour_interval)->equals($quarter_hour_expected);
|
expect($quarter_hour_interval)->equals($quarter_hour_expected);
|
||||||
|
|
||||||
$offset_start_time_interval = array_keys($this->date_time->getTimeInterval(
|
$offset_start_time_interval = array_keys($date_time->getTimeInterval(
|
||||||
'03:00:00',
|
'03:00:00',
|
||||||
'+1 hour',
|
'+1 hour',
|
||||||
$total_steps = 5
|
$total_steps = 5
|
||||||
@@ -85,8 +96,4 @@ class DateTimeTest extends \MailPoetTest {
|
|||||||
);
|
);
|
||||||
expect($offset_start_time_interval)->equals($offset_start_time_expected);
|
expect($offset_start_time_interval)->equals($offset_start_time_expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
function _afterStep() {
|
|
||||||
WordPressHelper::releaseAllFunctions();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -10,13 +10,14 @@ class EmojiTest extends \MailPoetTest {
|
|||||||
$this->data_decoded = "Emojis: 😃😵💪, not emojis: .Ž";
|
$this->data_decoded = "Emojis: 😃😵💪, not emojis: .Ž";
|
||||||
|
|
||||||
$this->column = 'dummycol';
|
$this->column = 'dummycol';
|
||||||
|
$this->emoji = new Emoji();
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanEncodeForUTF8Column() {
|
function testItCanEncodeForUTF8Column() {
|
||||||
$table = Env::$db_prefix . 'dummytable_utf8';
|
$table = Env::$db_prefix . 'dummytable_utf8';
|
||||||
$this->createTable($table, 'utf8');
|
$this->createTable($table, 'utf8');
|
||||||
|
|
||||||
$result = Emoji::encodeForUTF8Column($table, $this->column, $this->data_decoded);
|
$result = $this->emoji->encodeForUTF8Column($table, $this->column, $this->data_decoded);
|
||||||
expect($result)->equals($this->data_encoded);
|
expect($result)->equals($this->data_encoded);
|
||||||
|
|
||||||
$this->dropTable($table);
|
$this->dropTable($table);
|
||||||
@@ -26,14 +27,14 @@ class EmojiTest extends \MailPoetTest {
|
|||||||
$table = Env::$db_prefix . 'dummytable_utf8mb4';
|
$table = Env::$db_prefix . 'dummytable_utf8mb4';
|
||||||
$this->createTable($table, 'utf8mb4');
|
$this->createTable($table, 'utf8mb4');
|
||||||
|
|
||||||
$result = Emoji::encodeForUTF8Column($table, $this->column, $this->data_decoded);
|
$result = $this->emoji->encodeForUTF8Column($table, $this->column, $this->data_decoded);
|
||||||
expect($result)->equals($this->data_decoded);
|
expect($result)->equals($this->data_decoded);
|
||||||
|
|
||||||
$this->dropTable($table);
|
$this->dropTable($table);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanDecodeEntities() {
|
function testItCanDecodeEntities() {
|
||||||
$result = Emoji::decodeEntities($this->data_encoded);
|
$result = $this->emoji->decodeEntities($this->data_encoded);
|
||||||
expect($result)->equals($this->data_decoded);
|
expect($result)->equals($this->data_decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user