Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Internal/EmailEdi...
File: EmailApiController.php
<?php
[0] Fix | Delete
[1] Fix | Delete
declare( strict_types = 1 );
[2] Fix | Delete
[3] Fix | Delete
namespace Automattic\WooCommerce\Internal\EmailEditor;
[4] Fix | Delete
[5] Fix | Delete
use Automattic\WooCommerce\EmailEditor\Validator\Builder;
[6] Fix | Delete
use Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails\WCTransactionalEmailPostsManager;
[7] Fix | Delete
use WC_Email;
[8] Fix | Delete
use WP_Error;
[9] Fix | Delete
[10] Fix | Delete
defined( 'ABSPATH' ) || exit;
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* API Controller for managing WooCommerce email templates via extending the post type API.
[14] Fix | Delete
*
[15] Fix | Delete
* @internal
[16] Fix | Delete
*/
[17] Fix | Delete
class EmailApiController {
[18] Fix | Delete
/**
[19] Fix | Delete
* A list of WooCommerce emails.
[20] Fix | Delete
*
[21] Fix | Delete
* @var \WC_Email[]
[22] Fix | Delete
*/
[23] Fix | Delete
private array $emails;
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* The WooCommerce transactional email post manager.
[27] Fix | Delete
*
[28] Fix | Delete
* @var WCTransactionalEmailPostsManager|null
[29] Fix | Delete
*/
[30] Fix | Delete
private ?WCTransactionalEmailPostsManager $post_manager;
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* Initialize the controller.
[34] Fix | Delete
*
[35] Fix | Delete
* @internal
[36] Fix | Delete
*/
[37] Fix | Delete
final public function init(): void {
[38] Fix | Delete
$this->emails = WC()->mailer()->get_emails();
[39] Fix | Delete
$this->post_manager = WCTransactionalEmailPostsManager::get_instance();
[40] Fix | Delete
}
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* Returns the data from wp_options table for the given post.
[44] Fix | Delete
*
[45] Fix | Delete
* @param array $post_data - Post data.
[46] Fix | Delete
* @return array - The email data.
[47] Fix | Delete
*/
[48] Fix | Delete
public function get_email_data( $post_data ): array {
[49] Fix | Delete
$email_type = $this->post_manager->get_email_type_from_post_id( $post_data['id'] );
[50] Fix | Delete
$email = $this->get_email_by_type( $email_type ?? '' );
[51] Fix | Delete
[52] Fix | Delete
// When the email type is not found, it means that the email type is not supported.
[53] Fix | Delete
if ( ! $email ) {
[54] Fix | Delete
return array(
[55] Fix | Delete
'subject' => null,
[56] Fix | Delete
'subject_full' => null,
[57] Fix | Delete
'subject_partial' => null,
[58] Fix | Delete
'preheader' => null,
[59] Fix | Delete
'default_subject' => null,
[60] Fix | Delete
'email_type' => null,
[61] Fix | Delete
'recipient' => null,
[62] Fix | Delete
'cc' => null,
[63] Fix | Delete
'bcc' => null,
[64] Fix | Delete
);
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
$form_fields = $email->get_form_fields();
[68] Fix | Delete
$enabled = $email->get_option( 'enabled' );
[69] Fix | Delete
return array(
[70] Fix | Delete
'enabled' => is_null( $enabled ) ? $email->is_enabled() : 'yes' === $enabled,
[71] Fix | Delete
'is_manual' => $email->is_manual(),
[72] Fix | Delete
'subject' => $email->get_option( 'subject' ),
[73] Fix | Delete
'subject_full' => $email->get_option( 'subject_full' ), // For customer_refunded_order email type because it has two different subjects.
[74] Fix | Delete
'subject_partial' => $email->get_option( 'subject_partial' ),
[75] Fix | Delete
'preheader' => $email->get_option( 'preheader' ),
[76] Fix | Delete
'default_subject' => $email->get_default_subject(),
[77] Fix | Delete
'email_type' => $email_type,
[78] Fix | Delete
// Recipient is possible to set only for the specific type of emails. When the field `recipient` is set in the form fields, it means that the email type has a recipient field.
[79] Fix | Delete
'recipient' => array_key_exists( 'recipient', $form_fields ) ? $email->get_option( 'recipient', get_option( 'admin_email' ) ) : null,
[80] Fix | Delete
'cc' => $email->get_option( 'cc' ),
[81] Fix | Delete
'bcc' => $email->get_option( 'bcc' ),
[82] Fix | Delete
);
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
/**
[86] Fix | Delete
* Update WooCommerce specific option data by post name.
[87] Fix | Delete
*
[88] Fix | Delete
* @param array $data - Data that are stored in the wp_options table.
[89] Fix | Delete
* @param \WP_Post $post - WP_Post object.
[90] Fix | Delete
* @return \WP_Error|null Returns WP_Error if email validation fails, null otherwise.
[91] Fix | Delete
*/
[92] Fix | Delete
public function save_email_data( array $data, \WP_Post $post ): ?\WP_Error {
[93] Fix | Delete
$error = $this->validate_email_data( $data );
[94] Fix | Delete
if ( is_wp_error( $error ) ) {
[95] Fix | Delete
return new \WP_Error( 'invalid_email_data', implode( ' ', $error->get_error_messages() ), array( 'status' => 400 ) );
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
if ( ! array_key_exists( 'subject', $data ) && ! array_key_exists( 'preheader', $data ) ) {
[99] Fix | Delete
return null;
[100] Fix | Delete
}
[101] Fix | Delete
$email_type = $this->post_manager->get_email_type_from_post_id( $post->ID );
[102] Fix | Delete
$email = $this->get_email_by_type( $email_type ?? '' );
[103] Fix | Delete
[104] Fix | Delete
if ( ! $email ) {
[105] Fix | Delete
return null; // not saving of type wc_email. Allow process to continue.
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
// Handle customer_refunded_order email type because it has two different subjects.
[109] Fix | Delete
if ( 'customer_refunded_order' === $email_type ) {
[110] Fix | Delete
if ( array_key_exists( 'subject_full', $data ) ) {
[111] Fix | Delete
$email->update_option( 'subject_full', $data['subject_full'] );
[112] Fix | Delete
}
[113] Fix | Delete
if ( array_key_exists( 'subject_partial', $data ) ) {
[114] Fix | Delete
$email->update_option( 'subject_partial', $data['subject_partial'] );
[115] Fix | Delete
}
[116] Fix | Delete
} elseif ( array_key_exists( 'subject', $data ) ) {
[117] Fix | Delete
$email->update_option( 'subject', $data['subject'] );
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
if ( array_key_exists( 'preheader', $data ) ) {
[121] Fix | Delete
$email->update_option( 'preheader', $data['preheader'] );
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
if ( array_key_exists( 'enabled', $data ) ) {
[125] Fix | Delete
$email->update_option( 'enabled', $data['enabled'] ? 'yes' : 'no' );
[126] Fix | Delete
}
[127] Fix | Delete
if ( array_key_exists( 'recipient', $data ) ) {
[128] Fix | Delete
$email->update_option( 'recipient', $data['recipient'] );
[129] Fix | Delete
}
[130] Fix | Delete
if ( array_key_exists( 'cc', $data ) ) {
[131] Fix | Delete
$email->update_option( 'cc', $data['cc'] );
[132] Fix | Delete
}
[133] Fix | Delete
if ( array_key_exists( 'bcc', $data ) ) {
[134] Fix | Delete
$email->update_option( 'bcc', $data['bcc'] );
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
return null;
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
/**
[141] Fix | Delete
* Validate the email data.
[142] Fix | Delete
*
[143] Fix | Delete
* @param array $data - The email data.
[144] Fix | Delete
* @return \WP_Error|null Returns WP_Error if email validation fails, null otherwise.
[145] Fix | Delete
*/
[146] Fix | Delete
private function validate_email_data( array $data ) {
[147] Fix | Delete
$error = new \WP_Error();
[148] Fix | Delete
[149] Fix | Delete
// Validate 'recipient' email(s) field.
[150] Fix | Delete
$invalid_recipients = $this->filter_invalid_email_addresses( $data['recipient'] ?? '' );
[151] Fix | Delete
if ( ! empty( $invalid_recipients ) ) {
[152] Fix | Delete
$error_message = sprintf(
[153] Fix | Delete
// translators: %s will be replaced by comma-separated email addresses. For example, "invalidemail1@example.com,invalidemail2@example.com".
[154] Fix | Delete
__( 'One or more Recipient email addresses are invalid: ā€œ%sā€. Please enter valid email addresses separated by commas.', 'woocommerce' ),
[155] Fix | Delete
implode( ',', $invalid_recipients )
[156] Fix | Delete
);
[157] Fix | Delete
$error->add( 'invalid_recipient_email_address', $error_message );
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
// Validate 'cc' email(s) field.
[161] Fix | Delete
$invalid_cc = $this->filter_invalid_email_addresses( $data['cc'] ?? '' );
[162] Fix | Delete
if ( ! empty( $invalid_cc ) ) {
[163] Fix | Delete
$error_message = sprintf(
[164] Fix | Delete
// translators: %s will be replaced by comma-separated email addresses. For example, "invalidemail1@example.com,invalidemail2@example.com".
[165] Fix | Delete
__( 'One or more CC email addresses are invalid: ā€œ%sā€. Please enter valid email addresses separated by commas.', 'woocommerce' ),
[166] Fix | Delete
implode( ',', $invalid_cc )
[167] Fix | Delete
);
[168] Fix | Delete
$error->add( 'invalid_cc_email_address', $error_message );
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
// Validate 'bcc' email(s) field.
[172] Fix | Delete
$invalid_bcc = $this->filter_invalid_email_addresses( $data['bcc'] ?? '' );
[173] Fix | Delete
if ( ! empty( $invalid_bcc ) ) {
[174] Fix | Delete
$error_message = sprintf(
[175] Fix | Delete
// translators: %s will be replaced by comma-separated email addresses. For example, "invalidemail1@example.com,invalidemail2@example.com".
[176] Fix | Delete
__( 'One or more BCC email addresses are invalid: ā€œ%sā€. Please enter valid email addresses separated by commas.', 'woocommerce' ),
[177] Fix | Delete
implode( ',', $invalid_bcc )
[178] Fix | Delete
);
[179] Fix | Delete
$error->add( 'invalid_bcc_email_address', $error_message );
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
if ( $error->has_errors() ) {
[183] Fix | Delete
return $error;
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
return null;
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
/**
[190] Fix | Delete
* Filter in invalid email addresses from a comma-separated string.
[191] Fix | Delete
*
[192] Fix | Delete
* @param string $comma_separated_email_addresses - A comma-separated string of email addresses.
[193] Fix | Delete
* @return array - An array of invalid email addresses.
[194] Fix | Delete
*/
[195] Fix | Delete
private function filter_invalid_email_addresses( $comma_separated_email_addresses ) {
[196] Fix | Delete
$invalid_email_addresses = array();
[197] Fix | Delete
[198] Fix | Delete
if ( empty( trim( $comma_separated_email_addresses ) ) ) {
[199] Fix | Delete
return $invalid_email_addresses;
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
foreach ( explode( ',', $comma_separated_email_addresses ) as $email_address ) {
[203] Fix | Delete
if ( ! filter_var( trim( $email_address ), FILTER_VALIDATE_EMAIL ) ) {
[204] Fix | Delete
$invalid_email_addresses[] = trim( $email_address );
[205] Fix | Delete
}
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
return $invalid_email_addresses;
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
/**
[212] Fix | Delete
* Get the schema for the WooCommerce email post data.
[213] Fix | Delete
*
[214] Fix | Delete
* @return array
[215] Fix | Delete
*/
[216] Fix | Delete
public function get_email_data_schema(): array {
[217] Fix | Delete
return Builder::object(
[218] Fix | Delete
array(
[219] Fix | Delete
'subject' => Builder::string()->nullable(),
[220] Fix | Delete
'subject_full' => Builder::string()->nullable(), // For customer_refunded_order email type because it has two different subjects.
[221] Fix | Delete
'subject_partial' => Builder::string()->nullable(),
[222] Fix | Delete
'preheader' => Builder::string()->nullable(),
[223] Fix | Delete
'default_subject' => Builder::string()->nullable(),
[224] Fix | Delete
'email_type' => Builder::string()->nullable(),
[225] Fix | Delete
'recipient' => Builder::string()->nullable(),
[226] Fix | Delete
'cc' => Builder::string()->nullable(),
[227] Fix | Delete
'bcc' => Builder::string()->nullable(),
[228] Fix | Delete
)
[229] Fix | Delete
)->to_array();
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
/**
[233] Fix | Delete
* Get the email object by ID.
[234] Fix | Delete
*
[235] Fix | Delete
* @param string $id - The email ID.
[236] Fix | Delete
* @return \WC_Email|null - The email object or null if not found.
[237] Fix | Delete
*/
[238] Fix | Delete
private function get_email_by_type( ?string $id ): ?WC_Email {
[239] Fix | Delete
foreach ( $this->emails as $email ) {
[240] Fix | Delete
if ( $email->id === $id ) {
[241] Fix | Delete
return $email;
[242] Fix | Delete
}
[243] Fix | Delete
}
[244] Fix | Delete
return null;
[245] Fix | Delete
}
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function