PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.3
YayMail – WooCommerce Email Customizer v4.4.3
4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 3.4.1 All 57 releases
yaymail / src / Engine / Backend / SettingsPage.php

SettingsPage.php in YayMail – WooCommerce Email Customizer 4.4.3, at src/Engine/Backend/SettingsPage.php

296 lines 15.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMail\Engine\Backend;
4
5 use YayMail\Controllers\RevisionController;
6 use YayMail\TemplatePatterns\PatternService;
7 use YayMail\Models\MigrationModel;
8 use YayMail\SupportedPlugins;
9 use YayMail\TemplatePatterns\SectionTemplateService;
10 use YayMail\Utils\SingletonTrait;
11 use YayMail\Utils\YayMailViteApp;
12 use YayMail\Utils\Localize;
13 use YayMail\Utils\Helpers;
14 /**
15 * YayMail Page
16 */
17 class SettingsPage {
18 use SingletonTrait;
19
20 private $yaymail_hook_surfix = null;
21
22 private $yay_wp_hook_surfix = null;
23
24 /**
25 * Constructor
26 */
27 protected function __construct() {
28 $this->init_hooks();
29 }
30
31 /**
32 * Initialize hooks when class init
33 */
34 protected function init_hooks() {
35 // Submenu is registered by YayCommerce AdminShell; keep each platform's
36 // settings-screen hook id (sourced from the registered platform) for the
37 // enqueue/screen checks below.
38 $yaymail_platform = \YayMail\Platform\PlatformRegistry::get( 'yaymail' );
39 if ( $yaymail_platform ) {
40 $this->yaymail_hook_surfix = $yaymail_platform->hook_suffix();
41 }
42 $wp_platform = \YayMail\Platform\PlatformRegistry::get( 'email-builder' );
43 if ( $wp_platform ) {
44 $this->yay_wp_hook_surfix = $wp_platform->hook_suffix();
45 }
46 add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ], 30 );
47 add_filter( 'mce_external_plugins', [ $this, 'register_wp_editor_plugins_script' ] );
48
49 // Add Column YayMail Customizer on Setting email of WooCommerce (only when WC is active)
50 if ( yaymail_is_wc_installed() ) {
51 add_filter( 'woocommerce_email_setting_columns', [ $this, 'woocommerce_email_setting_columns' ] );
52 add_action( 'woocommerce_email_setting_column_yaymail_customizer', [ $this, 'woocommerce_email_setting_column_yaymail_customizer' ] );
53 }
54
55 // Fix conflict plugins styles in Settings page
56 add_action( 'admin_enqueue_scripts', [ $this, 'fix_conflict_plugins_styles' ], PHP_INT_MAX );
57 }
58
59 /**
60 * Enqueue scripts using in settings page
61 */
62 public function register_wp_editor_plugins_script( $plugin_array ) {
63 $plugin_url = YAYMAIL_PLUGIN_URL;
64 $screen = get_current_screen();
65 $yaymail_platform = \YayMail\Platform\PlatformRegistry::get( 'yaymail' );
66 if ( ( ! $yaymail_platform || ! $screen || $screen->id !== $yaymail_platform->hook_suffix() ) && defined( 'YAYWP_PLUGIN_URL' ) ) {
67 $plugin_url = YAYWP_PLUGIN_URL;
68 }
69
70 $plugin_array['advlist'] = $plugin_url . 'assets/scripts/wp-editor-plugins/advlist/plugin.min.js';
71 $plugin_array['autolink'] = $plugin_url . 'assets/scripts/wp-editor-plugins/autolink/plugin.min.js';
72 $plugin_array['searchreplace'] = $plugin_url . 'assets/scripts/wp-editor-plugins/searchreplace/plugin.min.js';
73 $plugin_array['code'] = $plugin_url . 'assets/scripts/wp-editor-plugins/code/plugin.min.js';
74 $plugin_array['visualblocks'] = $plugin_url . 'assets/scripts/wp-editor-plugins/visualblocks/plugin.min.js';
75 $plugin_array['table'] = $plugin_url . 'assets/scripts/wp-editor-plugins/table/plugin.min.js';
76 $plugin_array['insertdatetime'] = $plugin_url . 'assets/scripts/wp-editor-plugins/insertdatetime/plugin.min.js';
77
78 return $plugin_array;
79 }
80
81 public function admin_enqueue_scripts( $hook_suffix ) {
82 if ( in_array( $hook_suffix, [ $this->yaymail_hook_surfix, $this->yay_wp_hook_surfix ], true ) ) {
83 do_action( 'yaymail_before_enqueue_settings_page_scripts' );
84 // Enqueue script here
85 YayMailViteApp::get_instance()->enqueue_entry( 'yaymail-main.tsx', [ 'react', 'react-dom', 'wp-i18n' ] );
86 add_action( 'yaymail_after_enqueue_scripts', [ $this, 'localize_js_vars' ] );
87
88 wp_enqueue_media();
89 wp_enqueue_editor();
90 wp_enqueue_script( 'accounting' );
91 do_action( 'yaymail_after_enqueue_settings_page_scripts' );
92 }
93 }
94
95 /**
96 * Register localize data
97 */
98 public function localize_js_vars() {
99 $screen = get_current_screen();
100 $current_platform = \YayMail\Platform\PlatformRegistry::from_screen( $screen );
101 if ( $screen->id === $this->yaymail_hook_surfix ) {
102 $_wc_emails = wc()->mailer()->emails;
103
104 // override template base for wc emails
105 foreach ( $_wc_emails as $email ) {
106 $reflector = new \ReflectionClass( $email );
107 $email->template_base = $reflector->getFileName();
108 unset( $reflector );
109 }
110
111 $_wc_emails = array_map(
112 function( $email ) {
113 return (object) [
114 'id' => $email->id,
115 'title' => $email->title,
116 'enabled' => $email->enabled,
117 'description' => $email->description,
118 'template_base' => $email->template_base,
119 'recipient' => $email->recipient,
120 'content_type' => $email->get_content_type(),
121 'setting_page_url' => Helpers::yaymail_get_url_email_setting_page( $email->id ),
122 ];
123 },
124 $_wc_emails
125 );
126 } else {
127 $_wc_emails = [];
128 }//end if
129
130 $plugin_work = Helpers::get_plugin_work_info();
131
132 wp_localize_script(
133 'module/yaymail/yaymail-main.tsx',
134 'yaymailData',
135 array_merge(
136 [
137 'is_rtl' => is_rtl(),
138 'urls' => [
139 'vite_dynamic_base' => YAYMAIL_PLUGIN_URL . 'assets/dist/yaymail/',
140 'asset_url' => YAYMAIL_PLUGIN_URL . 'assets/images/',
141 'home_url' => home_url(),
142 'wc_placeholder_img_src' => function_exists( 'wc_placeholder_img_src' ) ? wc_placeholder_img_src() : '',
143 ],
144 'admin_ajax' => [
145 'url' => admin_url( 'admin-ajax.php' ),
146 'nonce' => wp_create_nonce( 'yaymail_frontend_nonce' ),
147 ],
148 'rest_path' => [
149 'root' => esc_url_raw( rest_url() ),
150 'base' => YAYMAIL_REST_NAMESPACE,
151 'nonce' => wp_create_nonce( 'wp_rest' ),
152 ],
153 'shared' => [
154 'util_functions' => [],
155 'stores' => [],
156 'core_components' => [],
157 'activated_addons' => Localize::get_activated_addons(),
158 ],
159 'list_orders' => Localize::get_list_orders(),
160 'i18n' => apply_filters(
161 'yaymail_translations',
162 []
163 ),
164 'builder' => [
165 'font_families' => [
166 '"Helvetica Neue",Helvetica,Roboto,Arial,sans-serif',
167 'Georgia, serif',
168 '"Times New Roman", Times, Serif',
169 'Arial, Helvetica, sans-serif',
170 '"Arial Black", Gadget, sans-serif',
171 '"Comic Sans MS", cursive, sans-serif',
172 'Tahoma, Geneva, sans-serif',
173 '"Trebuchet MS", Helvetica, sans-serif',
174 'Verdana, Geneva, sans-serif',
175 '"Courier New", Courier, monospace',
176 '"Lucida Console", Monaco, monospace',
177 '"Outfit", "DM Sans", sans-serif',
178 '"Kodchasan", system-ui, sans-serif',
179 '"Fraunces", serif',
180 '"Rethink Sans","Helvetica Neue",Helvetica,Roboto,Arial,sans-serif',
181 ],
182 'social_icons' => Localize::get_social_icons_data(),
183 'revision_limit' => RevisionController::YAYMAIL_TEMPLATE_REVISION_LIMIT,
184 // Two compiled JS bundles still read global_headers_footers as a single
185 // legacy { global_header_elements, global_footer_elements } object rather
186 // than the per-language array Localize::get_global_headers_footers()
187 // returns: (1) the "email-builder" (WP core mail) bundle -- confirmed
188 // byte-identical to the free "yaymail" lite bundle, it never received the
189 // multi-language upgrade -- and (2) the "yaymail" lite bundle itself when
190 // YAYMAIL_LITE_LEGACY_GHF_SHAPE is set. The paid yaymail-pro /
191 // email-customizer-for-woocommerce variants already expect the array.
192 // Feeding the array shape to a legacy bundle leaves globalHeaderElements/
193 // globalFooterElements undefined and crashes the customizer with "Cannot
194 // read properties of undefined (reading 'find')"; feeding the legacy shape
195 // to an array-expecting bundle breaks it with "global_headers_footers.find
196 // is not a function". Keep both conditions -- don't collapse into version
197 // checks, they gate an unrelated concern (see YAYWP_HOSTS_CORE above).
198 'global_headers_footers' => ( $current_platform && (
199 'email-builder' === $current_platform->key()
200 || ( 'yaymail' === $current_platform->key() && defined( 'YAYMAIL_LITE_LEGACY_GHF_SHAPE' ) && YAYMAIL_LITE_LEGACY_GHF_SHAPE )
201 ) )
202 ? \YayMail\Models\TemplateModel::get_global_header_and_footer( '', $current_platform->ghf_option_key() )
203 : Localize::get_global_headers_footers(
204 $current_platform ? $current_platform->ghf_option_key() : 'yaymail_global_header_footer'
205 ),
206 'section_templates' => SectionTemplateService::get_instance()->get_list_data(),
207 'patterns' => PatternService::get_instance()->get_list_data(),
208 ],
209 'colors' => [
210 'default_background_color' => YAYMAIL_COLOR_BACKGROUND_DEFAULT,
211 'default_text_link_color' => ( $current_platform && 'email-builder' === $current_platform->key() ) ? YAYMAIL_COLOR_WP_DEFAULT : YAYMAIL_COLOR_WC_DEFAULT,
212 'default_content_background_color' => YAYMAIL_COLOR_CONTENT_BACKGROUND_DEFAULT,
213 'default_content_text_color' => YAYMAIL_COLOR_CONTENT_TEXT_DEFAULT,
214 'default_title_color' => ( $current_platform && 'email-builder' === $current_platform->key() ) ? YAYMAIL_COLOR_WP_DEFAULT : YAYMAIL_COLOR_TITLE_DEFAULT,
215 ],
216 'smtp' => [
217 'link_detail' => self_admin_url( 'plugin-install.php?tab=plugin-information&plugin=yaysmtp&section=description&TB_iframe=true&width=600&height=800' ),
218 'setting' => admin_url( 'admin.php?page=yaysmtp' ),
219 'is_active' => Helpers::check_plugin_installed( 'yaysmtp/yay-smtp.php' ) || Helpers::check_plugin_installed( 'yaysmtp-pro/yay-smtp.php' ),
220 ],
221 'translate_integrations' => Localize::get_translate_integrations(),
222 'reviewed' => boolval( get_option( 'yaymail_review' ) ),
223 'ghf_tour' => get_option( 'yaymail_ghf_tour', 'initial' ),
224 'test_email_address' => get_option( 'yaymail_default_email_test', wp_get_current_user()->user_email ),
225 'site_title' => get_option( 'blogname' ),
226 // TODO: legacy: use get_user_meta
227 'wc_emails' => $_wc_emails,
228 'is_critical_migration_required' => MigrationModel::get_instance()->check_if_critical_migration_required(),
229 'supported_plugins' => SupportedPlugins::get_instance()->get_slug_name_supported_plugins(),
230 'show_multi_select_notice' => get_option( 'yaymail_show_multi_select_notice', 'yes' ),
231 'viewed_new_elements' => ! empty( get_option( 'yaymail_viewed_new_elements', [] ) ) ? get_option( 'yaymail_viewed_new_elements' ) : [],
232 'ghf_disallowed_element_types' => yaymail_get_ghf_disallowed_element_types(),
233 'platform' => $current_platform ? $current_platform->key() : 'yaymail',
234 'woocommerce_email_styles' => $this->get_scoped_woocommerce_email_styles(),
235 ],
236 apply_filters( 'yaymail_additional_localized_variables', [] )
237 )
238 );
239 }
240
241 private function get_scoped_woocommerce_email_styles() {
242 if ( ! function_exists( 'WC' ) ) {
243 return '';
244 }
245
246 $raw_styles = wp_unslash( wc_get_template_html( 'emails/email-styles.php' ) );
247 return Helpers::scope_css_block( $raw_styles, '.yaymail-customizer-template-section' );
248 }
249
250 /**
251 * Add new column to action column
252 */
253 public function woocommerce_email_setting_columns( $array ) {
254 if ( isset( $array['actions'] ) ) {
255 unset( $array['actions'] );
256 return array_merge(
257 $array,
258 [
259 'yaymail_customizer' => '',
260 'actions' => '',
261 ]
262 );
263 }
264 return $array;
265 }
266
267 /**
268 * Add link to setting column
269 */
270 public function woocommerce_email_setting_column_yaymail_customizer( $email ) {
271 $email_id = $email->id;
272 if ( 'yith-coupon-email-system' === $email->id ) {
273 if ( class_exists( 'YayMailYITHWooCouponEmailSystem\templateDefault\DefaultCouponEmailSystem' ) ) {
274 $email_id = 'YWCES_register';
275 }
276 }
277
278 echo '<td class="wc-email-settings-table-template">
279 <a class="button alignright" target="_blank" href="' . esc_attr( admin_url( 'admin.php?page=yaymail-settings#/customizer' ) ) . '?template=' . esc_attr( $email_id ) . '">' . esc_html( __( 'Customize with YayMail', 'yaymail' ) ) . '</a></td>';
280 }
281
282 public function fix_conflict_plugins_styles() {
283 if ( ! function_exists( 'get_current_screen' ) ) {
284 return;
285 }
286 $screen = get_current_screen();
287 if ( in_array( $screen->id, [ $this->yaymail_hook_surfix, $this->yay_wp_hook_surfix ], true ) ) {
288 wp_dequeue_style( 'real-media-library-lite-rml' );
289 wp_dequeue_script( 'real-media-library-lite-rml' );
290 wp_dequeue_style( 'real-media-library-rml' );
291 wp_dequeue_script( 'real-media-library-rml' );
292 wp_dequeue_style( 'real-category-library-admin' );
293 }
294 }
295 }
296