Abilities.php
1 month ago
WooCommerceAutomationTemplates.php
1 month ago
WooCommerceMarketingStatus.php
1 month ago
index.php
1 month ago
WooCommerceAutomationTemplates.php
150 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Abilities; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use Automattic\WooCommerce\Abilities\AbilityDefinition; |
| 9 | use MailPoet\Automation\Engine\Data\AutomationTemplate; |
| 10 | use MailPoet\Automation\Engine\Registry; |
| 11 | use MailPoet\Config\AccessControl; |
| 12 | use MailPoet\DI\ContainerWrapper; |
| 13 | use MailPoet\WP\Functions as WPFunctions; |
| 14 | |
| 15 | if (!defined('ABSPATH')) exit; |
| 16 | |
| 17 | if (!interface_exists(AbilityDefinition::class)) { |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | class WooCommerceAutomationTemplates implements AbilityDefinition { |
| 22 | private const CATEGORIES = [ |
| 23 | 'abandoned-cart', |
| 24 | 'bookings', |
| 25 | 'purchase', |
| 26 | 'review', |
| 27 | 'subscriptions', |
| 28 | ]; |
| 29 | |
| 30 | public static function get_name(): string { // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps -- Required by WooCommerce's AbilityDefinition interface. |
| 31 | return 'mailpoet/list-woocommerce-automation-templates'; |
| 32 | } |
| 33 | |
| 34 | public static function get_registration_args(): array { // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps -- Required by WooCommerce's AbilityDefinition interface. |
| 35 | return [ |
| 36 | 'label' => __('List MailPoet WooCommerce automation templates', 'mailpoet'), |
| 37 | 'description' => __('List MailPoet automation templates for WooCommerce marketing workflows.', 'mailpoet'), |
| 38 | 'category' => 'woocommerce', |
| 39 | 'input_schema' => self::getInputSchema(), |
| 40 | 'output_schema' => self::getOutputSchema(), |
| 41 | 'execute_callback' => [self::class, 'execute'], |
| 42 | 'permission_callback' => [self::class, 'canReadTemplates'], |
| 43 | 'meta' => [ |
| 44 | 'show_in_rest' => true, |
| 45 | 'mcp' => [ |
| 46 | 'public' => true, |
| 47 | 'type' => 'tool', |
| 48 | ], |
| 49 | 'annotations' => [ |
| 50 | 'readonly' => true, |
| 51 | 'destructive' => false, |
| 52 | 'idempotent' => true, |
| 53 | ], |
| 54 | ], |
| 55 | ]; |
| 56 | } |
| 57 | |
| 58 | public static function execute($input = []): array { |
| 59 | $category = is_array($input) && isset($input['category']) && is_string($input['category']) ? $input['category'] : ''; |
| 60 | if ($category !== '' && !in_array($category, self::CATEGORIES, true)) { |
| 61 | return ['templates' => []]; |
| 62 | } |
| 63 | |
| 64 | $container = ContainerWrapper::getInstance(); |
| 65 | |
| 66 | /** @var Registry $registry */ |
| 67 | $registry = $container->get(Registry::class); |
| 68 | |
| 69 | $templates = $registry->getTemplates($category !== '' ? $category : null); |
| 70 | $templates = array_filter($templates, function (AutomationTemplate $template): bool { |
| 71 | return in_array($template->getCategory(), self::CATEGORIES, true); |
| 72 | }); |
| 73 | |
| 74 | return [ |
| 75 | 'templates' => array_values(array_map([self::class, 'formatTemplate'], $templates)), |
| 76 | ]; |
| 77 | } |
| 78 | |
| 79 | public static function canReadTemplates(): bool { |
| 80 | return WPFunctions::get()->currentUserCan(AccessControl::PERMISSION_MANAGE_AUTOMATIONS); |
| 81 | } |
| 82 | |
| 83 | private static function formatTemplate(AutomationTemplate $template): array { |
| 84 | return [ |
| 85 | 'slug' => $template->getSlug(), |
| 86 | 'name' => $template->getName(), |
| 87 | 'description' => $template->getDescription(), |
| 88 | 'category' => $template->getCategory(), |
| 89 | 'type' => $template->getType(), |
| 90 | 'required_capabilities' => $template->getRequiredCapabilities(), |
| 91 | 'is_recommended' => $template->isRecommended(), |
| 92 | ]; |
| 93 | } |
| 94 | |
| 95 | private static function getInputSchema(): array { |
| 96 | return [ |
| 97 | 'type' => 'object', |
| 98 | 'properties' => [ |
| 99 | 'category' => [ |
| 100 | 'type' => 'string', |
| 101 | 'enum' => self::CATEGORIES, |
| 102 | ], |
| 103 | ], |
| 104 | 'additionalProperties' => false, |
| 105 | ]; |
| 106 | } |
| 107 | |
| 108 | private static function getOutputSchema(): array { |
| 109 | return [ |
| 110 | 'type' => 'object', |
| 111 | 'properties' => [ |
| 112 | 'templates' => [ |
| 113 | 'type' => 'array', |
| 114 | 'items' => [ |
| 115 | 'type' => 'object', |
| 116 | 'properties' => [ |
| 117 | 'slug' => ['type' => 'string'], |
| 118 | 'name' => ['type' => 'string'], |
| 119 | 'description' => ['type' => 'string'], |
| 120 | 'category' => [ |
| 121 | 'type' => 'string', |
| 122 | 'enum' => self::CATEGORIES, |
| 123 | ], |
| 124 | 'type' => [ |
| 125 | 'type' => 'string', |
| 126 | 'enum' => [ |
| 127 | AutomationTemplate::TYPE_DEFAULT, |
| 128 | AutomationTemplate::TYPE_PREMIUM, |
| 129 | AutomationTemplate::TYPE_COMING_SOON, |
| 130 | ], |
| 131 | ], |
| 132 | 'required_capabilities' => [ |
| 133 | 'type' => 'object', |
| 134 | 'additionalProperties' => [ |
| 135 | 'type' => ['boolean', 'integer'], |
| 136 | ], |
| 137 | ], |
| 138 | 'is_recommended' => ['type' => 'boolean'], |
| 139 | ], |
| 140 | 'required' => ['slug', 'name', 'description', 'category', 'type', 'required_capabilities', 'is_recommended'], |
| 141 | 'additionalProperties' => false, |
| 142 | ], |
| 143 | ], |
| 144 | ], |
| 145 | 'required' => ['templates'], |
| 146 | 'additionalProperties' => false, |
| 147 | ]; |
| 148 | } |
| 149 | } |
| 150 |