Blocks
10 months ago
Coupons
1 month ago
Endpoints
2 months ago
Patterns
1 month ago
PersonalizationTags
3 days ago
ProductCollection
1 month ago
Templates
2 months ago
AutomationEmailContextProvider.php
1 month ago
AutomationEmailPreviewOrderProvider.php
1 month ago
BlockEmailContentDetector.php
1 month ago
Cli.php
2 weeks ago
DependencyNotice.php
10 months ago
EditorPageRenderer.php
1 week ago
EmailApiController.php
1 week ago
EmailEditor.php
2 months ago
EmailEditorPreviewEmail.php
9 months ago
Logger.php
10 months ago
MailPoetCssInliner.php
8 months ago
MailpoetCssInlinerFactory.php
1 year ago
PersonalizationTagManager.php
5 days ago
index.php
2 years ago
Cli.php
88 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\EmailEditor\Integrations\MailPoet; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\NewsletterEntity; |
| 9 | use MailPoet\Newsletter\NewsletterSaveController; |
| 10 | use MailPoet\WP\Functions as WPFunctions; |
| 11 | use WP_CLI; |
| 12 | |
| 13 | class Cli { |
| 14 | private const TEMPLATES = [ |
| 15 | [ |
| 16 | 'name' => 'testing-email-with-core-blocks', |
| 17 | 'subject' => 'Hey [subscriber:firstname | default:subscriber], we test new email editor!', |
| 18 | 'preheader' => 'This is a testing email containing core blocks with different configurations.', |
| 19 | ], |
| 20 | ]; |
| 21 | |
| 22 | private NewsletterSaveController $newsletterSaveController; |
| 23 | |
| 24 | private WPFunctions $wp; |
| 25 | |
| 26 | public function __construct( |
| 27 | NewsletterSaveController $newsletterSaveController, |
| 28 | WPFunctions $wp |
| 29 | ) { |
| 30 | $this->newsletterSaveController = $newsletterSaveController; |
| 31 | $this->wp = $wp; |
| 32 | } |
| 33 | |
| 34 | public function initialize(): void { |
| 35 | if (!class_exists(WP_CLI::class)) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | WP_CLI::add_command('mailpoet email-editor create-templates', [$this, 'createTemplates'], [ |
| 40 | 'shortdesc' => 'Create MailPoet email editor templates', |
| 41 | ]); |
| 42 | |
| 43 | // Deprecated colon-named alias kept for backwards compatibility. Remove in a future release (STOMAIL-8177). |
| 44 | $this->registerDeprecatedAlias('mailpoet:email-editor:create-templates', 'mailpoet email-editor create-templates', [$this, 'createTemplates']); |
| 45 | } |
| 46 | |
| 47 | private function registerDeprecatedAlias(string $oldName, string $newName, callable $handler): void { |
| 48 | WP_CLI::add_command($oldName, function () use ($oldName, $newName, $handler): void { |
| 49 | WP_CLI::warning(sprintf('`wp %s` is deprecated and will be removed in a future release. Use `wp %s` instead.', $oldName, $newName)); |
| 50 | $handler(); |
| 51 | }, [ |
| 52 | 'shortdesc' => sprintf('Deprecated. Use `wp %s` instead', $newName), |
| 53 | ]); |
| 54 | } |
| 55 | |
| 56 | public function createTemplates(): void { |
| 57 | WP_CLI::log("Starting creating MailPoet email editor templates."); |
| 58 | foreach (self::TEMPLATES as $template) { |
| 59 | $content = file_get_contents(__DIR__ . "/templates/{$template['name']}.html"); |
| 60 | $newsletter = $this->newsletterSaveController->save([ |
| 61 | 'subject' => $template['subject'], |
| 62 | 'preheader' => $template['preheader'], |
| 63 | 'type' => NewsletterEntity::TYPE_STANDARD, |
| 64 | 'new_editor' => true, |
| 65 | ]); |
| 66 | |
| 67 | $wpPost = $newsletter->getWpPost(); |
| 68 | if (!$wpPost) { |
| 69 | WP_CLI::error("Failed to create a post for the email template {$template['name']}."); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | $this->wp->wpUpdatePost([ |
| 74 | 'ID' => $wpPost->getId(), |
| 75 | 'post_title' => $this->getTemplateName($template['name']), |
| 76 | 'post_content' => $content, |
| 77 | ]); |
| 78 | WP_CLI::log("Created a new email template {$template['name']}."); |
| 79 | } |
| 80 | WP_CLI::log('Finished creating MailPoet email editor templates.'); |
| 81 | } |
| 82 | |
| 83 | private function getTemplateName(string $templateName): string { |
| 84 | $name = str_replace('-', ' ', $templateName); |
| 85 | return ucwords($name); |
| 86 | } |
| 87 | } |
| 88 |