PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.5
YayMail – WooCommerce Email Customizer v4.4.5
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.5, at src/Engine/Backend/SettingsPage.php

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