diff --git a/lib/AdminPages/Pages/NewsletterEditor.php b/lib/AdminPages/Pages/NewsletterEditor.php index 87e416ca00..0fe22977df 100644 --- a/lib/AdminPages/Pages/NewsletterEditor.php +++ b/lib/AdminPages/Pages/NewsletterEditor.php @@ -36,6 +36,9 @@ class NewsletterEditor { /** @var ServicesChecker */ private $servicesChecker; + /** @var ShortcodesHelper */ + private $shortcodesHelper; + public function __construct( PageRenderer $pageRenderer, SettingsController $settings, @@ -43,6 +46,7 @@ class NewsletterEditor { WooCommerceHelper $woocommerceHelper, WPFunctions $wp, TransactionalEmails $wcTransactionalEmails, + ShortcodesHelper $shortcodesHelper, ServicesChecker $servicesChecker ) { $this->pageRenderer = $pageRenderer; @@ -52,6 +56,7 @@ class NewsletterEditor { $this->wp = $wp; $this->wcTransactionalEmails = $wcTransactionalEmails; $this->servicesChecker = $servicesChecker; + $this->shortcodesHelper = $shortcodesHelper; } public function render() { @@ -84,7 +89,7 @@ class NewsletterEditor { } $data = [ - 'shortcodes' => ShortcodesHelper::getShortcodes(), + 'shortcodes' => $this->shortcodesHelper->getShortcodes(), 'settings' => $this->settings->getAll(), 'editor_tutorial_seen' => $this->userFlags->get('editor_tutorial_seen'), 'current_wp_user' => array_merge($subscriberData, $this->wp->wpGetCurrentUser()->to_array()), diff --git a/lib/DI/ContainerConfigurator.php b/lib/DI/ContainerConfigurator.php index 8b6b075caa..a35eada77b 100644 --- a/lib/DI/ContainerConfigurator.php +++ b/lib/DI/ContainerConfigurator.php @@ -307,6 +307,7 @@ class ContainerConfigurator implements IContainerConfigurator { $container->autowire(\MailPoet\Newsletter\Renderer\Preprocessor::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\Shortcodes\ShortcodesHelper::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\PostNotificationScheduler::class)->setPublic(true); diff --git a/lib/Newsletter/Shortcodes/ShortcodesHelper.php b/lib/Newsletter/Shortcodes/ShortcodesHelper.php index fbf792f76c..a8ec24e62b 100644 --- a/lib/Newsletter/Shortcodes/ShortcodesHelper.php +++ b/lib/Newsletter/Shortcodes/ShortcodesHelper.php @@ -2,110 +2,116 @@ namespace MailPoet\Newsletter\Shortcodes; -use MailPoet\Models\CustomField; +use MailPoet\CustomFields\CustomFieldsRepository; use MailPoet\Models\NewsletterLink; -use MailPoet\WP\Functions as WPFunctions; class ShortcodesHelper { - public static function getShortcodes() { + /** @var CustomFieldsRepository */ + private $customFieldsRepository; + + public function __construct(CustomFieldsRepository $customFieldsRepository) { + $this->customFieldsRepository = $customFieldsRepository; + } + + public function getShortcodes(): array { $shortcodes = [ - WPFunctions::get()->__('Subscriber', 'mailpoet') => [ + __('Subscriber', 'mailpoet') => [ [ - 'text' => WPFunctions::get()->__('First Name', 'mailpoet'), + 'text' => __('First Name', 'mailpoet'), 'shortcode' => '[subscriber:firstname | default:reader]', ], [ - 'text' => WPFunctions::get()->__('Last Name', 'mailpoet'), + 'text' => __('Last Name', 'mailpoet'), 'shortcode' => '[subscriber:lastname | default:reader]', ], [ - 'text' => WPFunctions::get()->__('Email Address', 'mailpoet'), + 'text' => __('Email Address', 'mailpoet'), 'shortcode' => '[subscriber:email]', ], [ - 'text' => WPFunctions::get()->__('WordPress User Display Name', 'mailpoet'), + 'text' => __('WordPress User Display Name', 'mailpoet'), 'shortcode' => '[subscriber:displayname | default:member]', ], [ - 'text' => WPFunctions::get()->__('Total Number of Subscribers', 'mailpoet'), + 'text' => __('Total Number of Subscribers', 'mailpoet'), 'shortcode' => '[subscriber:count]', ], ], - WPFunctions::get()->__('Newsletter', 'mailpoet') => [ + __('Newsletter', 'mailpoet') => [ [ - 'text' => WPFunctions::get()->__('Newsletter Subject', 'mailpoet'), + 'text' => __('Newsletter Subject', 'mailpoet'), '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]', ], [ - 'text' => WPFunctions::get()->__('Most Recent Post Title', 'mailpoet'), + 'text' => __('Most Recent Post Title', 'mailpoet'), 'shortcode' => '[newsletter:post_title]', ], [ - 'text' => WPFunctions::get()->__('Issue Number', 'mailpoet'), + 'text' => __('Issue Number', 'mailpoet'), '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]', ], [ - '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]', ], [ - 'text' => WPFunctions::get()->__('Full name of current day', 'mailpoet'), + 'text' => __('Full name of current day', 'mailpoet'), 'shortcode' => '[date:dtext]', ], [ - 'text' => WPFunctions::get()->__('Current month number', 'mailpoet'), + 'text' => __('Current month number', 'mailpoet'), 'shortcode' => '[date:m]', ], [ - 'text' => WPFunctions::get()->__('Full name of current month', 'mailpoet'), + 'text' => __('Full name of current month', 'mailpoet'), 'shortcode' => '[date:mtext]', ], [ - 'text' => WPFunctions::get()->__('Year', 'mailpoet'), + 'text' => __('Year', 'mailpoet'), 'shortcode' => '[date:y]', ], ], - WPFunctions::get()->__('Links', 'mailpoet') => [ + __('Links', 'mailpoet') => [ [ - 'text' => WPFunctions::get()->__('Unsubscribe link', 'mailpoet'), + 'text' => __('Unsubscribe link', 'mailpoet'), 'shortcode' => sprintf( '%s', 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( '%s', '[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( '%s', '[link:newsletter_view_in_browser_url]', - WPFunctions::get()->__('View in your browser', 'mailpoet') + __('View in your browser', 'mailpoet') ), ], ], ]; - $customFields = self::getCustomFields(); - if ($customFields) { + $customFields = $this->getCustomFields(); + if (count($customFields) > 0) { $shortcodes[__('Subscriber', 'mailpoet')] = array_merge( $shortcodes[__('Subscriber', 'mailpoet')], $customFields @@ -114,13 +120,12 @@ class ShortcodesHelper { return $shortcodes; } - public static function getCustomFields() { - $customFields = CustomField::findMany(); - if (!$customFields) return false; + public function getCustomFields(): array { + $customFields = $this->customFieldsRepository->findAll(); return array_map(function($customField) { return [ - 'text' => $customField->name, - 'shortcode' => '[subscriber:cf_' . $customField->id . ']', + 'text' => $customField->getName(), + 'shortcode' => '[subscriber:cf_' . $customField->getId() . ']', ]; }, $customFields); } diff --git a/tests/integration/Newsletter/ShortcodesHelperTest.php b/tests/integration/Newsletter/ShortcodesHelperTest.php index 8de6a99eac..12da1021bc 100644 --- a/tests/integration/Newsletter/ShortcodesHelperTest.php +++ b/tests/integration/Newsletter/ShortcodesHelperTest.php @@ -2,13 +2,20 @@ namespace MailPoet\Test\Newsletter; -use MailPoet\Models\CustomField; +use MailPoet\Entities\CustomFieldEntity; use MailPoet\Newsletter\Shortcodes\ShortcodesHelper; -use MailPoetVendor\Idiorm\ORM; 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() { - $shortcodes = ShortcodesHelper::getShortcodes(); + $shortcodes = $this->shortcodesHelper->getShortcodes(); expect(array_keys($shortcodes))->equals( [ 'Subscriber', @@ -21,21 +28,18 @@ class ShortcodesHelperTest extends \MailPoetTest { } public function testItGetsCustomShortShortcodes() { - $shortcodes = ShortcodesHelper::getShortcodes(); + $shortcodes = $this->shortcodesHelper->getShortcodes(); expect(count($shortcodes['Subscriber']))->equals(5); - $customField = CustomField::create(); - $customField->name = 'name'; - $customField->type = 'type'; - $customField->save(); - $shortcodes = ShortcodesHelper::getShortcodes(); + $customField = new CustomFieldEntity(); + $customField->setName('name'); + $customField->setType('type'); + $this->entityManager->persist($customField); + $this->entityManager->flush(); + $shortcodes = $this->shortcodesHelper->getShortcodes(); expect(count($shortcodes['Subscriber']))->equals(6); $customSubscriberShortcode = end($shortcodes['Subscriber']); - expect($customSubscriberShortcode['text'])->equals($customField->name); + expect($customSubscriberShortcode['text'])->equals($customField->getName()); expect($customSubscriberShortcode['shortcode']) - ->equals('[subscriber:cf_' . $customField->id . ']'); - } - - public function _after() { - ORM::raw_execute('TRUNCATE ' . CustomField::$_table); + ->equals('[subscriber:cf_' . $customField->getId() . ']'); } }