| 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 |
/** |
| 23 |
* Constructor |
| 24 |
*/ |
| 25 |
protected function __construct() { |
| 26 |
$this->init_hooks(); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Initialize hooks when class init |
| 31 |
*/ |
| 32 |
protected function init_hooks() { |
| 33 |
// Register Menu |
| 34 |
add_action( 'admin_menu', [ $this, 'add_yaymail_menu' ], YAYMAIL_MENU_PRIORITY ); |
| 35 |
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ], 30 ); |
| 36 |
|
| 37 |
add_filter( 'plugin_action_links_' . YAYMAIL_PLUGIN_BASENAME, [ $this, 'plugin_action_links' ] ); |
| 38 |
add_filter( 'plugin_row_meta', [ $this, 'add_support_and_docs_links' ], 10, 2 ); |
| 39 |
add_filter( 'mce_external_plugins', [ $this, 'register_wp_editor_plugins_script' ] ); |
| 40 |
|
| 41 |
// Add Column YayMail Customizer on Setting email of WooCommerce |
| 42 |
add_filter( 'woocommerce_email_setting_columns', [ $this, 'woocommerce_email_setting_columns' ] ); |
| 43 |
add_action( 'woocommerce_email_setting_column_yaymail_customizer', [ $this, 'woocommerce_email_setting_column_yaymail_customizer' ] ); |
| 44 |
|
| 45 |
// Fix conflict plugins styles in Settings page |
| 46 |
add_action( 'admin_enqueue_scripts', [ $this, 'fix_conflict_plugins_styles' ], PHP_INT_MAX ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Register the YayMAil sub menu to WordPress YayCommerce menu. |
| 51 |
*/ |
| 52 |
public function add_yaymail_menu() { |
| 53 |
$menu_args = [ |
| 54 |
'parent_slug' => 'yaycommerce', |
| 55 |
'page_title' => __( 'Email Builder Settings', 'yaymail' ), |
| 56 |
'menu_title' => __( 'YayMail', 'yaymail' ), |
| 57 |
'capability' => 'manage_woocommerce', |
| 58 |
'menu_slug' => YAYMAIL_PREFIX . '-settings', |
| 59 |
'function' => [ $this, 'render_yaymail_page' ], |
| 60 |
'position' => 0, |
| 61 |
]; |
| 62 |
$this->yaymail_hook_surfix = add_submenu_page( $menu_args['parent_slug'], $menu_args['page_title'], $menu_args['menu_title'], $menu_args['capability'], $menu_args['menu_slug'], $menu_args['function'], $menu_args['position'] ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Render the settings page |
| 67 |
*/ |
| 68 |
public function render_yaymail_page() { |
| 69 |
include_once YAYMAIL_PLUGIN_PATH . 'templates/pages/settings.php'; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Enqueue scripts using in settings page |
| 74 |
*/ |
| 75 |
public function register_wp_editor_plugins_script( $plugin_array ) { |
| 76 |
|
| 77 |
$plugin_array['advlist'] = YAYMAIL_PLUGIN_URL . 'assets/scripts/wp-editor-plugins/advlist/plugin.min.js'; |
| 78 |
$plugin_array['autolink'] = YAYMAIL_PLUGIN_URL . 'assets/scripts/wp-editor-plugins/autolink/plugin.min.js'; |
| 79 |
$plugin_array['searchreplace'] = YAYMAIL_PLUGIN_URL . 'assets/scripts/wp-editor-plugins/searchreplace/plugin.min.js'; |
| 80 |
$plugin_array['code'] = YAYMAIL_PLUGIN_URL . 'assets/scripts/wp-editor-plugins/code/plugin.min.js'; |
| 81 |
$plugin_array['visualblocks'] = YAYMAIL_PLUGIN_URL . 'assets/scripts/wp-editor-plugins/visualblocks/plugin.min.js'; |
| 82 |
$plugin_array['table'] = YAYMAIL_PLUGIN_URL . 'assets/scripts/wp-editor-plugins/table/plugin.min.js'; |
| 83 |
$plugin_array['insertdatetime'] = YAYMAIL_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 ], true ) && class_exists( 'WC_Emails' ) ) { |
| 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 |
|
| 107 |
$_wc_emails = wc()->mailer()->emails; |
| 108 |
|
| 109 |
// override template base for wc emails |
| 110 |
foreach ( $_wc_emails as $email ) { |
| 111 |
$reflector = new \ReflectionClass( $email ); |
| 112 |
$email->template_base = $reflector->getFileName(); |
| 113 |
unset( $reflector ); |
| 114 |
} |
| 115 |
|
| 116 |
$_wc_emails = array_map( |
| 117 |
function( $email ) { |
| 118 |
return (object) [ |
| 119 |
'id' => $email->id, |
| 120 |
'title' => $email->title, |
| 121 |
'enabled' => $email->enabled, |
| 122 |
'description' => $email->description, |
| 123 |
'template_base' => $email->template_base, |
| 124 |
'recipient' => $email->recipient, |
| 125 |
'content_type' => $email->get_content_type(), |
| 126 |
'setting_page_url' => Helpers::yaymail_get_url_email_setting_page( $email->id ), |
| 127 |
]; |
| 128 |
}, |
| 129 |
$_wc_emails |
| 130 |
); |
| 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 |
], |
| 178 |
'social_icons' => Localize::get_social_icons_data(), |
| 179 |
'revision_limit' => RevisionController::YAYMAIL_TEMPLATE_REVISION_LIMIT, |
| 180 |
'global_headers_footers' => Localize::get_global_headers_footers(), |
| 181 |
'section_templates' => SectionTemplateService::get_instance()->get_list_data(), |
| 182 |
'patterns' => PatternService::get_instance()->get_list_data(), |
| 183 |
], |
| 184 |
'colors' => [ |
| 185 |
'default_background_color' => YAYMAIL_COLOR_BACKGROUND_DEFAULT, |
| 186 |
'default_text_link_color' => YAYMAIL_COLOR_WC_DEFAULT, |
| 187 |
], |
| 188 |
'smtp' => [ |
| 189 |
'link_detail' => self_admin_url( 'plugin-install.php?tab=plugin-information&plugin=yaysmtp§ion=description&TB_iframe=true&width=600&height=800' ), |
| 190 |
'setting' => admin_url( 'admin.php?page=yaysmtp' ), |
| 191 |
'is_active' => Helpers::check_plugin_installed( 'yaysmtp/yay-smtp.php' ) || Helpers::check_plugin_installed( 'yaysmtp-pro/yay-smtp.php' ), |
| 192 |
], |
| 193 |
'reviewed' => boolval( get_option( 'yaymail_review' ) ), |
| 194 |
'ghf_tour' => get_option( 'yaymail_ghf_tour', 'initial' ), |
| 195 |
'test_email_address' => get_option( 'yaymail_default_email_test', wp_get_current_user()->user_email ), |
| 196 |
'site_title' => get_option( 'blogname' ), |
| 197 |
// TODO: legacy: use get_user_meta |
| 198 |
'wc_emails' => $_wc_emails, |
| 199 |
'is_critical_migration_required' => MigrationModel::get_instance()->check_if_critical_migration_required(), |
| 200 |
'supported_plugins' => SupportedPlugins::get_instance()->get_slug_name_supported_plugins(), |
| 201 |
'show_multi_select_notice' => get_option( 'yaymail_show_multi_select_notice', 'yes' ), |
| 202 |
'viewed_new_elements' => ! empty( get_option( 'yaymail_viewed_new_elements', [] ) ) ? get_option( 'yaymail_viewed_new_elements' ) : [], |
| 203 |
], |
| 204 |
apply_filters( 'yaymail_additional_localized_variables', [] ) |
| 205 |
) |
| 206 |
); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Add link to YayMail settings page & Go Pro in Plugin row |
| 211 |
*/ |
| 212 |
public function plugin_action_links( $links ) { |
| 213 |
$action_links = [ |
| 214 |
'settings' => '<a href="' . admin_url( 'admin.php?page=yaymail-settings' ) . '" aria-label="' . esc_attr__( 'View WooCommerce Email Builder', 'yaymail' ) . '">' . esc_html__( 'Start Customizing', 'yaymail' ) . '</a>', |
| 215 |
]; |
| 216 |
$links[] = '<a target="_blank" href="https://yaycommerce.com/yaymail-woocommerce-email-customizer/" style="color: #43B854; font-weight: bold">' . __( 'Go Pro', 'yaymail' ) . '</a>'; |
| 217 |
|
| 218 |
return array_merge( $action_links, $links ); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Add extra plugin meta in Plugin row |
| 223 |
*/ |
| 224 |
public function add_support_and_docs_links( $plugin_meta, $plugin_file ) { |
| 225 |
if ( YAYMAIL_PLUGIN_BASENAME === $plugin_file ) { |
| 226 |
$plugin_meta[] = '<a target="_blank" href="https://docs.yaycommerce.com/yaymail/getting-started/introduction">' . esc_html__( 'Docs', 'yaymail' ) . '</a>'; |
| 227 |
$plugin_meta[] = '<a target="_blank" href="https://yaycommerce.com/support/">' . esc_html__( 'Support', 'yaymail' ) . '</a>'; |
| 228 |
} |
| 229 |
return $plugin_meta; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Add new column to action column |
| 234 |
*/ |
| 235 |
public function woocommerce_email_setting_columns( $array ) { |
| 236 |
if ( isset( $array['actions'] ) ) { |
| 237 |
unset( $array['actions'] ); |
| 238 |
return array_merge( |
| 239 |
$array, |
| 240 |
[ |
| 241 |
'yaymail_customizer' => '', |
| 242 |
'actions' => '', |
| 243 |
] |
| 244 |
); |
| 245 |
} |
| 246 |
return $array; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Add link to setting column |
| 251 |
*/ |
| 252 |
public function woocommerce_email_setting_column_yaymail_customizer( $email ) { |
| 253 |
$email_id = $email->id; |
| 254 |
if ( 'yith-coupon-email-system' === $email->id ) { |
| 255 |
if ( class_exists( 'YayMailYITHWooCouponEmailSystem\templateDefault\DefaultCouponEmailSystem' ) ) { |
| 256 |
$email_id = 'YWCES_register'; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
echo '<td class="wc-email-settings-table-template"> |
| 261 |
<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>'; |
| 262 |
} |
| 263 |
|
| 264 |
public function fix_conflict_plugins_styles() { |
| 265 |
if ( ! function_exists( 'get_current_screen' ) ) { |
| 266 |
return; |
| 267 |
} |
| 268 |
$screen = get_current_screen(); |
| 269 |
if ( $this->yaymail_hook_surfix === $screen->id ) { |
| 270 |
wp_dequeue_style( 'real-media-library-lite-rml' ); |
| 271 |
wp_dequeue_script( 'real-media-library-lite-rml' ); |
| 272 |
wp_dequeue_style( 'real-media-library-rml' ); |
| 273 |
wp_dequeue_script( 'real-media-library-rml' ); |
| 274 |
wp_dequeue_style( 'real-category-library-admin' ); |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|