Use doctrine in Shortcodes Helper

[MAILPOET-2993]
This commit is contained in:
Pavel Dohnal
2020-10-29 09:34:02 +01:00
committed by Veljko V
parent d7c14753fe
commit 4134e6e882
4 changed files with 67 additions and 52 deletions

View File

@ -36,6 +36,9 @@ class NewsletterEditor {
/** @var ServicesChecker */ /** @var ServicesChecker */
private $servicesChecker; private $servicesChecker;
/** @var ShortcodesHelper */
private $shortcodesHelper;
public function __construct( public function __construct(
PageRenderer $pageRenderer, PageRenderer $pageRenderer,
SettingsController $settings, SettingsController $settings,
@ -43,6 +46,7 @@ class NewsletterEditor {
WooCommerceHelper $woocommerceHelper, WooCommerceHelper $woocommerceHelper,
WPFunctions $wp, WPFunctions $wp,
TransactionalEmails $wcTransactionalEmails, TransactionalEmails $wcTransactionalEmails,
ShortcodesHelper $shortcodesHelper,
ServicesChecker $servicesChecker ServicesChecker $servicesChecker
) { ) {
$this->pageRenderer = $pageRenderer; $this->pageRenderer = $pageRenderer;
@ -52,6 +56,7 @@ class NewsletterEditor {
$this->wp = $wp; $this->wp = $wp;
$this->wcTransactionalEmails = $wcTransactionalEmails; $this->wcTransactionalEmails = $wcTransactionalEmails;
$this->servicesChecker = $servicesChecker; $this->servicesChecker = $servicesChecker;
$this->shortcodesHelper = $shortcodesHelper;
} }
public function render() { public function render() {
@ -84,7 +89,7 @@ class NewsletterEditor {
} }
$data = [ $data = [
'shortcodes' => ShortcodesHelper::getShortcodes(), 'shortcodes' => $this->shortcodesHelper->getShortcodes(),
'settings' => $this->settings->getAll(), 'settings' => $this->settings->getAll(),
'editor_tutorial_seen' => $this->userFlags->get('editor_tutorial_seen'), 'editor_tutorial_seen' => $this->userFlags->get('editor_tutorial_seen'),
'current_wp_user' => array_merge($subscriberData, $this->wp->wpGetCurrentUser()->to_array()), 'current_wp_user' => array_merge($subscriberData, $this->wp->wpGetCurrentUser()->to_array()),

View File

@ -307,6 +307,7 @@ class ContainerConfigurator implements IContainerConfigurator {
$container->autowire(\MailPoet\Newsletter\Renderer\Preprocessor::class)->setPublic(true); $container->autowire(\MailPoet\Newsletter\Renderer\Preprocessor::class)->setPublic(true);
$container->autowire(\MailPoet\Newsletter\Renderer\Renderer::class)->setPublic(true); $container->autowire(\MailPoet\Newsletter\Renderer\Renderer::class)->setPublic(true);
$container->autowire(\MailPoet\Newsletter\Segment\NewsletterSegmentRepository::class)->setPublic(true); $container->autowire(\MailPoet\Newsletter\Segment\NewsletterSegmentRepository::class)->setPublic(true);
$container->autowire(\MailPoet\Newsletter\Shortcodes\ShortcodesHelper::class)->setPublic(true);
$container->autowire(\MailPoet\Newsletter\Statistics\NewsletterStatisticsRepository::class)->setPublic(true); $container->autowire(\MailPoet\Newsletter\Statistics\NewsletterStatisticsRepository::class)->setPublic(true);
$container->autowire(\MailPoet\Newsletter\Scheduler\WelcomeScheduler::class)->setPublic(true); $container->autowire(\MailPoet\Newsletter\Scheduler\WelcomeScheduler::class)->setPublic(true);
$container->autowire(\MailPoet\Newsletter\Scheduler\PostNotificationScheduler::class)->setPublic(true); $container->autowire(\MailPoet\Newsletter\Scheduler\PostNotificationScheduler::class)->setPublic(true);

View File

@ -2,110 +2,116 @@
namespace MailPoet\Newsletter\Shortcodes; namespace MailPoet\Newsletter\Shortcodes;
use MailPoet\Models\CustomField; use MailPoet\CustomFields\CustomFieldsRepository;
use MailPoet\Models\NewsletterLink; use MailPoet\Models\NewsletterLink;
use MailPoet\WP\Functions as WPFunctions;
class ShortcodesHelper { class ShortcodesHelper {
public static function getShortcodes() { /** @var CustomFieldsRepository */
private $customFieldsRepository;
public function __construct(CustomFieldsRepository $customFieldsRepository) {
$this->customFieldsRepository = $customFieldsRepository;
}
public function getShortcodes(): array {
$shortcodes = [ $shortcodes = [
WPFunctions::get()->__('Subscriber', 'mailpoet') => [ __('Subscriber', 'mailpoet') => [
[ [
'text' => WPFunctions::get()->__('First Name', 'mailpoet'), 'text' => __('First Name', 'mailpoet'),
'shortcode' => '[subscriber:firstname | default:reader]', 'shortcode' => '[subscriber:firstname | default:reader]',
], ],
[ [
'text' => WPFunctions::get()->__('Last Name', 'mailpoet'), 'text' => __('Last Name', 'mailpoet'),
'shortcode' => '[subscriber:lastname | default:reader]', 'shortcode' => '[subscriber:lastname | default:reader]',
], ],
[ [
'text' => WPFunctions::get()->__('Email Address', 'mailpoet'), 'text' => __('Email Address', 'mailpoet'),
'shortcode' => '[subscriber:email]', 'shortcode' => '[subscriber:email]',
], ],
[ [
'text' => WPFunctions::get()->__('WordPress User Display Name', 'mailpoet'), 'text' => __('WordPress User Display Name', 'mailpoet'),
'shortcode' => '[subscriber:displayname | default:member]', 'shortcode' => '[subscriber:displayname | default:member]',
], ],
[ [
'text' => WPFunctions::get()->__('Total Number of Subscribers', 'mailpoet'), 'text' => __('Total Number of Subscribers', 'mailpoet'),
'shortcode' => '[subscriber:count]', 'shortcode' => '[subscriber:count]',
], ],
], ],
WPFunctions::get()->__('Newsletter', 'mailpoet') => [ __('Newsletter', 'mailpoet') => [
[ [
'text' => WPFunctions::get()->__('Newsletter Subject', 'mailpoet'), 'text' => __('Newsletter Subject', 'mailpoet'),
'shortcode' => '[newsletter:subject]', 'shortcode' => '[newsletter:subject]',
], ],
], ],
WPFunctions::get()->__('Post Notifications', 'mailpoet') => [ __('Post Notifications', 'mailpoet') => [
[ [
'text' => WPFunctions::get()->__('Total Number of Posts or Pages', 'mailpoet'), 'text' => __('Total Number of Posts or Pages', 'mailpoet'),
'shortcode' => '[newsletter:total]', 'shortcode' => '[newsletter:total]',
], ],
[ [
'text' => WPFunctions::get()->__('Most Recent Post Title', 'mailpoet'), 'text' => __('Most Recent Post Title', 'mailpoet'),
'shortcode' => '[newsletter:post_title]', 'shortcode' => '[newsletter:post_title]',
], ],
[ [
'text' => WPFunctions::get()->__('Issue Number', 'mailpoet'), 'text' => __('Issue Number', 'mailpoet'),
'shortcode' => '[newsletter:number]', 'shortcode' => '[newsletter:number]',
], ],
], ],
WPFunctions::get()->__('Date', 'mailpoet') => [ __('Date', 'mailpoet') => [
[ [
'text' => WPFunctions::get()->__('Current day of the month number', 'mailpoet'), 'text' => __('Current day of the month number', 'mailpoet'),
'shortcode' => '[date:d]', 'shortcode' => '[date:d]',
], ],
[ [
'text' => WPFunctions::get()->__('Current day of the month in ordinal form, i.e. 2nd, 3rd, 4th, etc.', 'mailpoet'), 'text' => __('Current day of the month in ordinal form, i.e. 2nd, 3rd, 4th, etc.', 'mailpoet'),
'shortcode' => '[date:dordinal]', 'shortcode' => '[date:dordinal]',
], ],
[ [
'text' => WPFunctions::get()->__('Full name of current day', 'mailpoet'), 'text' => __('Full name of current day', 'mailpoet'),
'shortcode' => '[date:dtext]', 'shortcode' => '[date:dtext]',
], ],
[ [
'text' => WPFunctions::get()->__('Current month number', 'mailpoet'), 'text' => __('Current month number', 'mailpoet'),
'shortcode' => '[date:m]', 'shortcode' => '[date:m]',
], ],
[ [
'text' => WPFunctions::get()->__('Full name of current month', 'mailpoet'), 'text' => __('Full name of current month', 'mailpoet'),
'shortcode' => '[date:mtext]', 'shortcode' => '[date:mtext]',
], ],
[ [
'text' => WPFunctions::get()->__('Year', 'mailpoet'), 'text' => __('Year', 'mailpoet'),
'shortcode' => '[date:y]', 'shortcode' => '[date:y]',
], ],
], ],
WPFunctions::get()->__('Links', 'mailpoet') => [ __('Links', 'mailpoet') => [
[ [
'text' => WPFunctions::get()->__('Unsubscribe link', 'mailpoet'), 'text' => __('Unsubscribe link', 'mailpoet'),
'shortcode' => sprintf( 'shortcode' => sprintf(
'<a target="_blank" href="%s">%s</a>', '<a target="_blank" href="%s">%s</a>',
NewsletterLink::UNSUBSCRIBE_LINK_SHORT_CODE, NewsletterLink::UNSUBSCRIBE_LINK_SHORT_CODE,
WPFunctions::get()->__('Unsubscribe', 'mailpoet') __('Unsubscribe', 'mailpoet')
), ),
], ],
[ [
'text' => WPFunctions::get()->__('Edit subscription page link', 'mailpoet'), 'text' => __('Edit subscription page link', 'mailpoet'),
'shortcode' => sprintf( 'shortcode' => sprintf(
'<a target="_blank" href="%s">%s</a>', '<a target="_blank" href="%s">%s</a>',
'[link:subscription_manage_url]', '[link:subscription_manage_url]',
WPFunctions::get()->__('Manage subscription', 'mailpoet') __('Manage subscription', 'mailpoet')
), ),
], ],
[ [
'text' => WPFunctions::get()->__('View in browser link', 'mailpoet'), 'text' => __('View in browser link', 'mailpoet'),
'shortcode' => sprintf( 'shortcode' => sprintf(
'<a target="_blank" href="%s">%s</a>', '<a target="_blank" href="%s">%s</a>',
'[link:newsletter_view_in_browser_url]', '[link:newsletter_view_in_browser_url]',
WPFunctions::get()->__('View in your browser', 'mailpoet') __('View in your browser', 'mailpoet')
), ),
], ],
], ],
]; ];
$customFields = self::getCustomFields(); $customFields = $this->getCustomFields();
if ($customFields) { if (count($customFields) > 0) {
$shortcodes[__('Subscriber', 'mailpoet')] = array_merge( $shortcodes[__('Subscriber', 'mailpoet')] = array_merge(
$shortcodes[__('Subscriber', 'mailpoet')], $shortcodes[__('Subscriber', 'mailpoet')],
$customFields $customFields
@ -114,13 +120,12 @@ class ShortcodesHelper {
return $shortcodes; return $shortcodes;
} }
public static function getCustomFields() { public function getCustomFields(): array {
$customFields = CustomField::findMany(); $customFields = $this->customFieldsRepository->findAll();
if (!$customFields) return false;
return array_map(function($customField) { return array_map(function($customField) {
return [ return [
'text' => $customField->name, 'text' => $customField->getName(),
'shortcode' => '[subscriber:cf_' . $customField->id . ']', 'shortcode' => '[subscriber:cf_' . $customField->getId() . ']',
]; ];
}, $customFields); }, $customFields);
} }

View File

@ -2,13 +2,20 @@
namespace MailPoet\Test\Newsletter; namespace MailPoet\Test\Newsletter;
use MailPoet\Models\CustomField; use MailPoet\Entities\CustomFieldEntity;
use MailPoet\Newsletter\Shortcodes\ShortcodesHelper; use MailPoet\Newsletter\Shortcodes\ShortcodesHelper;
use MailPoetVendor\Idiorm\ORM;
class ShortcodesHelperTest extends \MailPoetTest { class ShortcodesHelperTest extends \MailPoetTest {
/** @var ShortcodesHelper */
private $shortcodesHelper;
public function _before() {
$this->truncateEntity(CustomFieldEntity::class);
$this->shortcodesHelper = $this->diContainer->get(ShortcodesHelper::class);
}
public function testGetsShortcodes() { public function testGetsShortcodes() {
$shortcodes = ShortcodesHelper::getShortcodes(); $shortcodes = $this->shortcodesHelper->getShortcodes();
expect(array_keys($shortcodes))->equals( expect(array_keys($shortcodes))->equals(
[ [
'Subscriber', 'Subscriber',
@ -21,21 +28,18 @@ class ShortcodesHelperTest extends \MailPoetTest {
} }
public function testItGetsCustomShortShortcodes() { public function testItGetsCustomShortShortcodes() {
$shortcodes = ShortcodesHelper::getShortcodes(); $shortcodes = $this->shortcodesHelper->getShortcodes();
expect(count($shortcodes['Subscriber']))->equals(5); expect(count($shortcodes['Subscriber']))->equals(5);
$customField = CustomField::create(); $customField = new CustomFieldEntity();
$customField->name = 'name'; $customField->setName('name');
$customField->type = 'type'; $customField->setType('type');
$customField->save(); $this->entityManager->persist($customField);
$shortcodes = ShortcodesHelper::getShortcodes(); $this->entityManager->flush();
$shortcodes = $this->shortcodesHelper->getShortcodes();
expect(count($shortcodes['Subscriber']))->equals(6); expect(count($shortcodes['Subscriber']))->equals(6);
$customSubscriberShortcode = end($shortcodes['Subscriber']); $customSubscriberShortcode = end($shortcodes['Subscriber']);
expect($customSubscriberShortcode['text'])->equals($customField->name); expect($customSubscriberShortcode['text'])->equals($customField->getName());
expect($customSubscriberShortcode['shortcode']) expect($customSubscriberShortcode['shortcode'])
->equals('[subscriber:cf_' . $customField->id . ']'); ->equals('[subscriber:cf_' . $customField->getId() . ']');
}
public function _after() {
ORM::raw_execute('TRUNCATE ' . CustomField::$_table);
} }
} }