PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.2
YayMail – WooCommerce Email Customizer v4.4.2
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 3.4.2 3.4.3 All 55 releases
yaymail / src / Engine / Backend / SettingsPage.php

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

260 lines 12.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 (YaymailPluginAdapter); keep hook id for enqueue/screen checks.
36 if ( defined( 'YAYMAIL_PREFIX' ) ) {
37 $this->yaymail_hook_surfix = 'yaycommerce_page_' . YAYMAIL_PREFIX . '-settings';
38 }
39 if ( defined( 'YAYMAIL_WP_VERSION' ) ) {
40 $this->yay_wp_hook_surfix = 'yaycommerce_page_yay-wp-email-customizer-settings';
41 }
42 add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ], 30 );
43 add_filter( 'mce_external_plugins', [ $this, 'register_wp_editor_plugins_script' ] );
44
45 // Add Column YayMail Customizer on Setting email of WooCommerce (only when WC is active)
46 if ( yaymail_is_wc_installed() ) {
47 add_filter( 'woocommerce_email_setting_columns', [ $this, 'woocommerce_email_setting_columns' ] );
48 add_action( 'woocommerce_email_setting_column_yaymail_customizer', [ $this, 'woocommerce_email_setting_column_yaymail_customizer' ] );
49 }
50
51 // Fix conflict plugins styles in Settings page
52 add_action( 'admin_enqueue_scripts', [ $this, 'fix_conflict_plugins_styles' ], PHP_INT_MAX );
53 }
54
55 /**
56 * Enqueue scripts using in settings page
57 */
58 public function register_wp_editor_plugins_script( $plugin_array ) {
59
60 $plugin_array['advlist'] = YAYMAIL_PLUGIN_URL . 'assets/scripts/wp-editor-plugins/advlist/plugin.min.js';
61 $plugin_array['autolink'] = YAYMAIL_PLUGIN_URL . 'assets/scripts/wp-editor-plugins/autolink/plugin.min.js';
62 $plugin_array['searchreplace'] = YAYMAIL_PLUGIN_URL . 'assets/scripts/wp-editor-plugins/searchreplace/plugin.min.js';
63 $plugin_array['code'] = YAYMAIL_PLUGIN_URL . 'assets/scripts/wp-editor-plugins/code/plugin.min.js';
64 $plugin_array['visualblocks'] = YAYMAIL_PLUGIN_URL . 'assets/scripts/wp-editor-plugins/visualblocks/plugin.min.js';
65 $plugin_array['table'] = YAYMAIL_PLUGIN_URL . 'assets/scripts/wp-editor-plugins/table/plugin.min.js';
66 $plugin_array['insertdatetime'] = YAYMAIL_PLUGIN_URL . 'assets/scripts/wp-editor-plugins/insertdatetime/plugin.min.js';
67
68 return $plugin_array;
69 }
70
71 public function admin_enqueue_scripts( $hook_suffix ) {
72 if ( in_array( $hook_suffix, [ $this->yaymail_hook_surfix, $this->yay_wp_hook_surfix ], true ) ) {
73 do_action( 'yaymail_before_enqueue_settings_page_scripts' );
74 // Enqueue script here
75 YayMailViteApp::get_instance()->enqueue_entry( 'yaymail-main.tsx', [ 'react', 'react-dom', 'wp-i18n' ] );
76 add_action( 'yaymail_after_enqueue_scripts', [ $this, 'localize_js_vars' ] );
77
78 wp_enqueue_media();
79 wp_enqueue_editor();
80 wp_enqueue_script( 'accounting' );
81 do_action( 'yaymail_after_enqueue_settings_page_scripts' );
82 }
83 }
84
85 /**
86 * Register localize data
87 */
88 public function localize_js_vars() {
89 $screen = get_current_screen();
90 if ( $screen->id === $this->yaymail_hook_surfix ) {
91 $_wc_emails = wc()->mailer()->emails;
92
93 // override template base for wc emails
94 foreach ( $_wc_emails as $email ) {
95 $reflector = new \ReflectionClass( $email );
96 $email->template_base = $reflector->getFileName();
97 unset( $reflector );
98 }
99
100 $_wc_emails = array_map(
101 function( $email ) {
102 return (object) [
103 'id' => $email->id,
104 'title' => $email->title,
105 'enabled' => $email->enabled,
106 'description' => $email->description,
107 'template_base' => $email->template_base,
108 'recipient' => $email->recipient,
109 'content_type' => $email->get_content_type(),
110 'setting_page_url' => Helpers::yaymail_get_url_email_setting_page( $email->id ),
111 ];
112 },
113 $_wc_emails
114 );
115 } else {
116 $_wc_emails = [];
117 }//end if
118
119 $plugin_work = Helpers::get_plugin_work_info();
120
121 wp_localize_script(
122 'module/yaymail/yaymail-main.tsx',
123 'yaymailData',
124 array_merge(
125 [
126 'is_rtl' => is_rtl(),
127 'urls' => [
128 'vite_dynamic_base' => YAYMAIL_PLUGIN_URL . 'assets/dist/yaymail/',
129 'asset_url' => YAYMAIL_PLUGIN_URL . 'assets/images/',
130 'home_url' => home_url(),
131 'wc_placeholder_img_src' => function_exists( 'wc_placeholder_img_src' ) ? wc_placeholder_img_src() : '',
132 ],
133 'admin_ajax' => [
134 'url' => admin_url( 'admin-ajax.php' ),
135 'nonce' => wp_create_nonce( 'yaymail_frontend_nonce' ),
136 ],
137 'rest_path' => [
138 'root' => esc_url_raw( rest_url() ),
139 'base' => YAYMAIL_REST_NAMESPACE,
140 'nonce' => wp_create_nonce( 'wp_rest' ),
141 ],
142 'shared' => [
143 'util_functions' => [],
144 'stores' => [],
145 'core_components' => [],
146 'activated_addons' => Localize::get_activated_addons(),
147 ],
148 'list_orders' => Localize::get_list_orders(),
149 'i18n' => apply_filters(
150 'yaymail_translations',
151 []
152 ),
153 'builder' => [
154 'font_families' => [
155 '"Helvetica Neue",Helvetica,Roboto,Arial,sans-serif',
156 'Georgia, serif',
157 '"Times New Roman", Times, Serif',
158 'Arial, Helvetica, sans-serif',
159 '"Arial Black", Gadget, sans-serif',
160 '"Comic Sans MS", cursive, sans-serif',
161 'Tahoma, Geneva, sans-serif',
162 '"Trebuchet MS", Helvetica, sans-serif',
163 'Verdana, Geneva, sans-serif',
164 '"Courier New", Courier, monospace',
165 '"Lucida Console", Monaco, monospace',
166 '"Outfit", "DM Sans", sans-serif',
167 '"Kodchasan", system-ui, sans-serif',
168 '"Fraunces", serif',
169 ],
170 'social_icons' => Localize::get_social_icons_data(),
171 'revision_limit' => RevisionController::YAYMAIL_TEMPLATE_REVISION_LIMIT,
172 'global_headers_footers' => Localize::get_global_headers_footers(),
173 'section_templates' => SectionTemplateService::get_instance()->get_list_data(),
174 'patterns' => PatternService::get_instance()->get_list_data(),
175 ],
176 'colors' => [
177 'default_background_color' => YAYMAIL_COLOR_BACKGROUND_DEFAULT,
178 'default_text_link_color' => YAYMAIL_COLOR_WC_DEFAULT,
179 'default_content_background_color' => YAYMAIL_COLOR_CONTENT_BACKGROUND_DEFAULT,
180 'default_content_text_color' => YAYMAIL_COLOR_CONTENT_TEXT_DEFAULT,
181 'default_title_color' => YAYMAIL_COLOR_TITLE_DEFAULT,
182 ],
183 'smtp' => [
184 'link_detail' => self_admin_url( 'plugin-install.php?tab=plugin-information&plugin=yaysmtp&section=description&TB_iframe=true&width=600&height=800' ),
185 'setting' => admin_url( 'admin.php?page=yaysmtp' ),
186 'is_active' => Helpers::check_plugin_installed( 'yaysmtp/yay-smtp.php' ) || Helpers::check_plugin_installed( 'yaysmtp-pro/yay-smtp.php' ),
187 ],
188 'reviewed' => boolval( get_option( 'yaymail_review' ) ),
189 'ghf_tour' => get_option( 'yaymail_ghf_tour', 'initial' ),
190 'test_email_address' => get_option( 'yaymail_default_email_test', wp_get_current_user()->user_email ),
191 'site_title' => get_option( 'blogname' ),
192 // TODO: legacy: use get_user_meta
193 'wc_emails' => $_wc_emails,
194 'is_critical_migration_required' => MigrationModel::get_instance()->check_if_critical_migration_required(),
195 'supported_plugins' => SupportedPlugins::get_instance()->get_slug_name_supported_plugins(),
196 'show_multi_select_notice' => get_option( 'yaymail_show_multi_select_notice', 'yes' ),
197 'viewed_new_elements' => ! empty( get_option( 'yaymail_viewed_new_elements', [] ) ) ? get_option( 'yaymail_viewed_new_elements' ) : [],
198 'platform' => $screen->id === $this->yay_wp_hook_surfix ? 'email-builder' : 'yaymail',
199 'woocommerce_email_styles' => $this->get_scoped_woocommerce_email_styles(),
200 ],
201 apply_filters( 'yaymail_additional_localized_variables', [] )
202 )
203 );
204 }
205
206 private function get_scoped_woocommerce_email_styles() {
207 if ( ! function_exists( 'WC' ) ) {
208 return '';
209 }
210
211 $raw_styles = wp_unslash( wc_get_template_html( 'emails/email-styles.php' ) );
212 return Helpers::scope_css_block( $raw_styles, '.yaymail-customizer-template-section' );
213 }
214
215 /**
216 * Add new column to action column
217 */
218 public function woocommerce_email_setting_columns( $array ) {
219 if ( isset( $array['actions'] ) ) {
220 unset( $array['actions'] );
221 return array_merge(
222 $array,
223 [
224 'yaymail_customizer' => '',
225 'actions' => '',
226 ]
227 );
228 }
229 return $array;
230 }
231
232 /**
233 * Add link to setting column
234 */
235 public function woocommerce_email_setting_column_yaymail_customizer( $email ) {
236 $email_id = $email->id;
237 if ( 'yith-coupon-email-system' === $email->id ) {
238 if ( class_exists( 'YayMailYITHWooCouponEmailSystem\templateDefault\DefaultCouponEmailSystem' ) ) {
239 $email_id = 'YWCES_register';
240 }
241 }
242
243 echo '<td class="wc-email-settings-table-template">
244 <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>';
245 }
246
247 public function fix_conflict_plugins_styles() {
248 if ( ! function_exists( 'get_current_screen' ) ) {
249 return;
250 }
251 $screen = get_current_screen();
252 if ( in_array( $screen->id, [ $this->yaymail_hook_surfix, $this->yay_wp_hook_surfix ], true ) ) {
253 wp_dequeue_style( 'real-media-library-lite-rml' );
254 wp_dequeue_script( 'real-media-library-lite-rml' );
255 wp_dequeue_style( 'real-media-library-rml' );
256 wp_dequeue_script( 'real-media-library-rml' );
257 wp_dequeue_style( 'real-category-library-admin' );
258 }
259 }
260 }