EmailImprovements.php
266 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Helper class to gradually enable email improvements to existing merchants. |
| 4 | * |
| 5 | * @since 9.9.0 |
| 6 | */ |
| 7 | |
| 8 | declare( strict_types=1 ); |
| 9 | |
| 10 | namespace Automattic\WooCommerce\Internal\Admin\EmailImprovements; |
| 11 | |
| 12 | use Automattic\WooCommerce\Utilities\FeaturesUtil; |
| 13 | use WC_Tracker; |
| 14 | |
| 15 | defined( 'ABSPATH' ) || exit; |
| 16 | |
| 17 | /** |
| 18 | * EmailImprovements Class. |
| 19 | */ |
| 20 | class EmailImprovements { |
| 21 | |
| 22 | /** |
| 23 | * Non-exhaustive list of email customizers. |
| 24 | * |
| 25 | * @var string[] |
| 26 | */ |
| 27 | private const EMAIL_CUSTOMIZERS = array( |
| 28 | 'aco-email-customizer-and-designer-for-woocommerce.php', |
| 29 | 'decorator.php', |
| 30 | 'email-customizer-for-woocommerce.php', |
| 31 | 'email-customizer-pro.php', |
| 32 | 'kadence-woocommerce-email-designer.php', |
| 33 | 'mailpoet.php', |
| 34 | 'wp-html-mail.php', |
| 35 | 'yaymail.php', |
| 36 | ); |
| 37 | |
| 38 | private const EMAIL_TEMPLATE_PARTS = array( |
| 39 | 'email-addresses.php', |
| 40 | 'email-customer-details.php', |
| 41 | 'email-downloads.php', |
| 42 | 'email-footer.php', |
| 43 | 'email-header.php', |
| 44 | 'email-mobile-messaging.php', |
| 45 | 'email-order-details.php', |
| 46 | 'email-order-items.php', |
| 47 | 'email-styles.php', |
| 48 | ); |
| 49 | |
| 50 | /** |
| 51 | * Hook into WordPress. |
| 52 | */ |
| 53 | public function __construct() { |
| 54 | add_action( 'admin_init', array( __CLASS__, 'add_email_improvements_modal_to_url' ) ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Check if any core emails are being overridden by a template override. |
| 59 | * |
| 60 | * @return bool True if core emails are being overridden, false otherwise. |
| 61 | */ |
| 62 | public static function has_email_templates_overridden() { |
| 63 | $all_template_overrides = WC_Tracker::get_all_template_overrides(); |
| 64 | $core_email_overrides = self::get_core_email_overrides( $all_template_overrides ); |
| 65 | return count( $core_email_overrides ) > 0; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Check if any of the email customizers is enabled. |
| 70 | * |
| 71 | * @return bool True if any of the email customizers is enabled, false otherwise. |
| 72 | */ |
| 73 | public static function is_email_customizer_enabled() { |
| 74 | $all_plugins = WC_Tracker::get_all_plugins(); |
| 75 | $active_plugins = $all_plugins['active_plugins']; |
| 76 | $plugin_slugs = array_map( |
| 77 | function ( $plugin_path ) { |
| 78 | $parts = explode( '/', $plugin_path ); |
| 79 | return end( $parts ); |
| 80 | }, |
| 81 | array_keys( $active_plugins ) |
| 82 | ); |
| 83 | return count( array_intersect( self::EMAIL_CUSTOMIZERS, $plugin_slugs ) ) > 0; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Check if email improvements are enabled for existing stores. |
| 88 | * |
| 89 | * @return bool True if email improvements are enabled for existing stores, false otherwise. |
| 90 | */ |
| 91 | public static function is_email_improvements_enabled_for_existing_stores() { |
| 92 | $is_feature_enabled = FeaturesUtil::feature_is_enabled( 'email_improvements' ); |
| 93 | $is_enabled_for_existing_stores = 'yes' === get_option( 'woocommerce_email_improvements_existing_store_enabled' ); |
| 94 | return $is_feature_enabled && $is_enabled_for_existing_stores; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Check if email improvements should be enabled for existing stores. |
| 99 | * - The feature is not already enabled. |
| 100 | * - The feature was not manually disabled. |
| 101 | * - The email templates are not overridden. |
| 102 | * - The email customizer is not enabled. |
| 103 | * |
| 104 | * @return bool True if email improvements should be enabled for existing stores, false otherwise. |
| 105 | */ |
| 106 | public static function should_enable_email_improvements_for_existing_stores() { |
| 107 | if ( FeaturesUtil::feature_is_enabled( 'email_improvements' ) ) { |
| 108 | return false; |
| 109 | } |
| 110 | $manually_disabled_before = get_option( 'woocommerce_email_improvements_last_disabled_at' ); |
| 111 | if ( $manually_disabled_before ) { |
| 112 | return false; |
| 113 | } |
| 114 | if ( self::has_email_templates_overridden() ) { |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | if ( self::is_email_customizer_enabled() ) { |
| 119 | return false; |
| 120 | } |
| 121 | // Temporarily paused roll-out to gather more feedback. |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Check if we should notice the merchant about email improvements. |
| 127 | * |
| 128 | * @return bool True if we should notice the merchant about email improvements, false otherwise. |
| 129 | */ |
| 130 | public static function should_notify_merchant_about_email_improvements() { |
| 131 | return ! FeaturesUtil::feature_is_enabled( 'email_improvements' ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Add email improvements modal parameter to the URL when loading the WooCommerce Home page. |
| 136 | * |
| 137 | * @return void |
| 138 | */ |
| 139 | public static function add_email_improvements_modal_to_url() { |
| 140 | // Check if we're on the WooCommerce Home page. |
| 141 | if ( ! isset( $_GET['page'] ) || 'wc-admin' !== $_GET['page'] || isset( $_GET['path'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | $dismissed_modal = get_option( 'woocommerce_admin_dismissed_email_improvements_modal' ); |
| 146 | if ( 'yes' !== $dismissed_modal && self::is_email_improvements_enabled_for_existing_stores() ) { |
| 147 | update_option( 'woocommerce_admin_dismissed_email_improvements_modal', 'yes' ); |
| 148 | wp_safe_redirect( add_query_arg( 'emailImprovementsModal', 'enabled' ) ); |
| 149 | exit; |
| 150 | } |
| 151 | |
| 152 | $dismissed_modal = get_option( 'woocommerce_admin_dismissed_try_email_improvements_modal' ); |
| 153 | if ( 'yes' !== $dismissed_modal && self::should_notify_merchant_about_email_improvements() ) { |
| 154 | update_option( 'woocommerce_admin_dismissed_try_email_improvements_modal', 'yes' ); |
| 155 | wp_safe_redirect( add_query_arg( 'emailImprovementsModal', 'try' ) ); |
| 156 | exit; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Get all core emails. |
| 162 | * |
| 163 | * @return array Core emails. |
| 164 | */ |
| 165 | public static function get_core_emails() { |
| 166 | return array_filter( |
| 167 | self::get_emails(), |
| 168 | function ( $email ) { |
| 169 | return strpos( get_class( $email ), 'WC_Email_' ) === 0 && is_string( $email->template_html ); |
| 170 | } |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Get all core email template overrides. |
| 176 | * |
| 177 | * @param array $template_overrides All template overrides. |
| 178 | * @return array Core email template overrides. |
| 179 | */ |
| 180 | public static function get_core_email_overrides( $template_overrides ) { |
| 181 | $core_emails = self::get_core_emails(); |
| 182 | $core_email_templates = array_map( |
| 183 | function ( $email ) { |
| 184 | return basename( $email->template_html ); |
| 185 | }, |
| 186 | $core_emails |
| 187 | ); |
| 188 | $all_email_templates = array_merge( $core_email_templates, self::EMAIL_TEMPLATE_PARTS ); |
| 189 | return array_intersect( $all_email_templates, $template_overrides ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Get all enabled email IDs. |
| 194 | * |
| 195 | * @return array Enabled email IDs. |
| 196 | */ |
| 197 | public static function get_enabled_emails() { |
| 198 | $enabled_emails = array_filter( |
| 199 | self::get_emails(), |
| 200 | function ( $email ) { |
| 201 | return $email->is_enabled() && ! $email->is_manual(); |
| 202 | } |
| 203 | ); |
| 204 | return array_values( array_map( fn( $email ) => get_class( $email ), $enabled_emails ) ); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Get all disabled email IDs. |
| 209 | * |
| 210 | * @return array Enabled email IDs. |
| 211 | */ |
| 212 | public static function get_disabled_emails() { |
| 213 | $disabled_emails = array_filter( |
| 214 | self::get_emails(), |
| 215 | function ( $email ) { |
| 216 | return ! $email->is_enabled() && ! $email->is_manual(); |
| 217 | } |
| 218 | ); |
| 219 | return array_values( array_map( fn( $email ) => get_class( $email ), $disabled_emails ) ); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Get all enabled or manual emails with Cc or Bcc. |
| 224 | * |
| 225 | * @return array Enabled or manual emails with Cc or Bcc. |
| 226 | */ |
| 227 | public static function get_enabled_or_manual_emails_with_cc_or_bcc() { |
| 228 | $enabled_or_manual_emails = array_filter( |
| 229 | self::get_emails(), |
| 230 | function ( $email ) { |
| 231 | return $email->is_enabled() || $email->is_manual(); |
| 232 | } |
| 233 | ); |
| 234 | |
| 235 | $email_ids_with_cc = array(); |
| 236 | $email_ids_with_bcc = array(); |
| 237 | |
| 238 | foreach ( $enabled_or_manual_emails as $email ) { |
| 239 | if ( $email->get_cc_recipient() ) { |
| 240 | $email_ids_with_cc[] = get_class( $email ); |
| 241 | } |
| 242 | if ( $email->get_bcc_recipient() ) { |
| 243 | $email_ids_with_bcc[] = get_class( $email ); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | return array( |
| 248 | 'ccs' => $email_ids_with_cc, |
| 249 | 'bccs' => $email_ids_with_bcc, |
| 250 | ); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * A helper method to filter out non-WC_Email objects. |
| 255 | * |
| 256 | * @return \WC_Email[] All WC_Email objects. |
| 257 | */ |
| 258 | private static function get_emails() { |
| 259 | $emails = WC()->mailer()->get_emails(); |
| 260 | return array_filter( |
| 261 | $emails, |
| 262 | fn( $email ) => is_object( $email ) && $email instanceof \WC_Email |
| 263 | ); |
| 264 | } |
| 265 | } |
| 266 |