Logger
11 months ago
Patterns
11 months ago
PersonalizationTags
2 days ago
Renderer
2 days ago
Templates
4 months ago
class-assets-manager.php
5 months ago
class-dependency-check.php
11 months ago
class-email-api-controller.php
6 months ago
class-email-editor.php
2 days ago
class-email-styles-schema.php
11 months ago
class-personalizer.php
2 days ago
class-send-preview-email.php
5 months ago
class-settings-controller.php
3 months ago
class-site-style-sync-controller.php
4 months ago
class-theme-controller.php
3 months ago
class-user-theme.php
11 months ago
content-editor.css
2 weeks ago
content-shared.css
11 months ago
index.php
11 months ago
theme.json
3 months ago
class-email-api-controller.php
94 lines
| 1 | <?php |
| 2 | declare(strict_types = 1); |
| 3 | namespace Automattic\WooCommerce\EmailEditor\Engine; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use Automattic\WooCommerce\EmailEditor\Engine\PersonalizationTags\Personalization_Tag; |
| 6 | use Automattic\WooCommerce\EmailEditor\Engine\PersonalizationTags\Personalization_Tags_Registry; |
| 7 | use Automattic\WooCommerce\EmailEditor\Validator\Builder; |
| 8 | use WP_Post; |
| 9 | use WP_REST_Request; |
| 10 | use WP_REST_Response; |
| 11 | class Email_Api_Controller { |
| 12 | private Personalization_Tags_Registry $personalization_tags_registry; |
| 13 | public function __construct( Personalization_Tags_Registry $personalization_tags_registry ) { |
| 14 | $this->personalization_tags_registry = $personalization_tags_registry; |
| 15 | } |
| 16 | public function get_email_data(): array { |
| 17 | // Here comes code getting Email specific data that will be passed on 'email_data' attribute. |
| 18 | return array(); |
| 19 | } |
| 20 | public function save_email_data( array $data, WP_Post $email_post ): void { |
| 21 | // Here comes code saving of Email specific data that will be passed on 'email_data' attribute. |
| 22 | } |
| 23 | public function send_preview_email_data( WP_REST_Request $request ): WP_REST_Response { |
| 24 | $data = $request->get_params(); |
| 25 | try { |
| 26 | $result = apply_filters( 'woocommerce_email_editor_send_preview_email', $data ); |
| 27 | return new WP_REST_Response( |
| 28 | array( |
| 29 | 'success' => (bool) $result, |
| 30 | 'result' => $result, |
| 31 | ), |
| 32 | $result ? 200 : 400 |
| 33 | ); |
| 34 | } catch ( \Exception $exception ) { |
| 35 | return new WP_REST_Response( array( 'error' => $exception->getMessage() ), 400 ); |
| 36 | } |
| 37 | } |
| 38 | public function get_personalization_tags(): WP_REST_Response { |
| 39 | $tags = $this->personalization_tags_registry->get_all(); |
| 40 | return new WP_REST_Response( |
| 41 | array( |
| 42 | 'success' => true, |
| 43 | 'result' => array_values( |
| 44 | array_map( |
| 45 | function ( Personalization_Tag $tag ) { |
| 46 | return array( |
| 47 | 'name' => $tag->get_name(), |
| 48 | 'token' => $tag->get_token(), |
| 49 | 'category' => $tag->get_category(), |
| 50 | 'attributes' => $tag->get_attributes(), |
| 51 | 'valueToInsert' => $tag->get_value_to_insert(), |
| 52 | 'postTypes' => $tag->get_post_types(), |
| 53 | ); |
| 54 | }, |
| 55 | $tags |
| 56 | ), |
| 57 | ), |
| 58 | ), |
| 59 | 200 |
| 60 | ); |
| 61 | } |
| 62 | public function get_personalization_tags_collection( WP_REST_Request $request ): WP_REST_Response { |
| 63 | $post_id = $request->get_param( 'post_id' ); |
| 64 | // Allow extensions to extend or modify tags based on post context. |
| 65 | // This fires before getting tags so extensions can register additional tags. |
| 66 | $post_id = is_numeric( $post_id ) ? (int) $post_id : 0; |
| 67 | if ( $post_id > 0 ) { |
| 68 | do_action( 'woocommerce_email_editor_personalization_tags_for_post', $post_id ); |
| 69 | } |
| 70 | $tags = $this->personalization_tags_registry->get_all(); |
| 71 | return new WP_REST_Response( |
| 72 | array_values( |
| 73 | array_map( |
| 74 | function ( Personalization_Tag $tag ) { |
| 75 | return array( |
| 76 | 'name' => $tag->get_name(), |
| 77 | 'token' => $tag->get_token(), |
| 78 | 'category' => $tag->get_category(), |
| 79 | 'attributes' => $tag->get_attributes(), |
| 80 | 'valueToInsert' => $tag->get_value_to_insert(), |
| 81 | 'postTypes' => $tag->get_post_types(), |
| 82 | ); |
| 83 | }, |
| 84 | $tags |
| 85 | ) |
| 86 | ), |
| 87 | 200 |
| 88 | ); |
| 89 | } |
| 90 | public function get_email_data_schema(): array { |
| 91 | return Builder::object()->to_array(); |
| 92 | } |
| 93 | } |
| 94 |