| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail; |
| 4 |
|
| 5 |
use YayMail\Utils\SingletonTrait; |
| 6 |
use YayMail\Models\SettingModel; |
| 7 |
use YayMail\Models\TemplateModel; |
| 8 |
use YayMail\Models\RevisionModel; |
| 9 |
use YayMail\Migrations\MainMigration; |
| 10 |
use YayMail\Utils\Helpers; |
| 11 |
use YayMail\Migrations\AbstractMigration; |
| 12 |
|
| 13 |
/** |
| 14 |
* I18n Logic |
| 15 |
* |
| 16 |
* @method static Ajax get_instance() |
| 17 |
*/ |
| 18 |
class Ajax { |
| 19 |
use SingletonTrait; |
| 20 |
|
| 21 |
protected function __construct() { |
| 22 |
$this->init_hooks(); |
| 23 |
} |
| 24 |
|
| 25 |
protected function init_hooks() { |
| 26 |
add_action( 'wp_ajax_yaymail_preview_mail', [ $this, 'preview_mail' ] ); |
| 27 |
add_action( 'wp_ajax_yaymail_preview_mail_for_woo', [ $this, 'preview_mail_for_woo' ] ); |
| 28 |
add_action( 'wp_ajax_yaymail_send_test_mail', [ $this, 'send_test_mail' ] ); |
| 29 |
add_action( 'wp_ajax_yaymail_install_yaysmtp', [ $this, 'install_yaysmtp' ] ); |
| 30 |
add_action( 'wp_ajax_yaymail_get_custom_hook_html', [ $this, 'get_custom_hook_html' ] ); |
| 31 |
add_action( 'wp_ajax_yaymail_get_template_data_onload', [ $this, 'get_template_data_onload' ] ); |
| 32 |
add_action( 'wp_ajax_yaymail_export_templates', [ $this, 'export_templates' ] ); |
| 33 |
add_action( 'wp_ajax_yaymail_import_templates', [ $this, 'import_templates' ] ); |
| 34 |
add_action( 'wp_ajax_yaymail_review', [ $this, 'yaymail_review' ] ); |
| 35 |
add_action( 'wp_ajax_yaymail_change_ghf_tour', [ $this, 'change_ghf_tour' ] ); |
| 36 |
add_action( 'wp_ajax_yaymail_dismiss_multi_select_notice', [ $this, 'dismiss_multi_select_notice' ] ); |
| 37 |
add_action( 'wp_ajax_yaymail_export_state', [ $this, 'export_state' ] ); |
| 38 |
add_action( 'wp_ajax_yaymail_import_state', [ $this, 'import_state' ] ); |
| 39 |
add_action( 'wp_ajax_yaymail_dismiss_new_element_notification', [ $this, 'dismiss_new_element_notification' ] ); |
| 40 |
} |
| 41 |
|
| 42 |
public function import_state() { |
| 43 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 44 |
if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) { |
| 45 |
return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] ); |
| 46 |
} |
| 47 |
try { |
| 48 |
$import_file = isset( $_FILES['import_file'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_FILES['import_file'] ) ) : null; |
| 49 |
if ( ! $import_file ) { |
| 50 |
return wp_send_json_error( [ 'mess' => __( 'Can\'t find import file', 'yaymail' ) ] ); |
| 51 |
} |
| 52 |
|
| 53 |
if ( $import_file['type'] !== 'application/zip' ) { |
| 54 |
return wp_send_json_error( [ 'mess' => __( 'Invalid file type. Please upload a ZIP file.', 'yaymail' ) ] ); |
| 55 |
} |
| 56 |
|
| 57 |
$zip = new \ZipArchive(); |
| 58 |
if ( $zip->open( $import_file['tmp_name'] ) !== true ) { |
| 59 |
return wp_send_json_error( [ 'mess' => __( 'Cannot open ZIP file.', 'yaymail' ) ] ); |
| 60 |
} |
| 61 |
|
| 62 |
$state_data = null; |
| 63 |
for ( $i = 0; $i < $zip->numFiles; $i++ ) { |
| 64 |
$filename = $zip->getNameIndex( $i ); |
| 65 |
if ( $filename === 'yaymail_backup.json' ) { |
| 66 |
$state_data = $zip->getFromIndex( $i ); |
| 67 |
break; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
$zip->close(); |
| 72 |
|
| 73 |
if ( ! $state_data ) { |
| 74 |
return wp_send_json_error( [ 'mess' => __( 'Cannot find yaymail_backup.json in the ZIP file.', 'yaymail' ) ] ); |
| 75 |
} |
| 76 |
|
| 77 |
$imported_data = json_decode( $state_data ); |
| 78 |
if ( json_last_error() !== JSON_ERROR_NONE ) { |
| 79 |
return wp_send_json_error( [ 'mess' => __( 'Invalid JSON data in the state file.', 'yaymail' ) ] ); |
| 80 |
} |
| 81 |
|
| 82 |
if ( empty( $imported_data->posts ) || empty( $imported_data->postmeta ) || empty( $imported_data->options ) ) { |
| 83 |
return wp_send_json_error( [ 'mess' => __( 'Invalid state file structure.', 'yaymail' ) ] ); |
| 84 |
} |
| 85 |
|
| 86 |
$migration_model = \YayMail\Models\MigrationModel::get_instance(); |
| 87 |
|
| 88 |
$source_version = $imported_data->version; |
| 89 |
|
| 90 |
$imported_posts = array_values( |
| 91 |
array_filter( |
| 92 |
$imported_data->posts, |
| 93 |
function( $post ) { |
| 94 |
return isset( $post->post_type ) && 'yaymail_template' === $post->post_type; |
| 95 |
} |
| 96 |
) |
| 97 |
); |
| 98 |
|
| 99 |
$imported_postmeta = array_values( |
| 100 |
array_filter( |
| 101 |
$imported_data->postmeta, |
| 102 |
function( $postmeta ) use ( $imported_posts ) { |
| 103 |
if ( empty( $imported_posts ) ) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
if ( ! isset( $postmeta->post_id ) || ! isset( $postmeta->meta_key ) ) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
if ( strpos( (string) $postmeta->meta_key, 'yaymail' ) === false ) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
foreach ( $imported_posts as $post ) { |
| 113 |
if ( isset( $post->ID ) && (int) $post->ID === (int) $postmeta->post_id ) { |
| 114 |
return true; |
| 115 |
} |
| 116 |
} |
| 117 |
return false; |
| 118 |
} |
| 119 |
) |
| 120 |
); |
| 121 |
|
| 122 |
$imported_options = array_values( |
| 123 |
array_filter( |
| 124 |
$imported_data->options, |
| 125 |
function( $option ) { |
| 126 |
return isset( $option->option_name ) && strpos( (string) $option->option_name, 'yaymail' ) !== false; |
| 127 |
} |
| 128 |
) |
| 129 |
); |
| 130 |
|
| 131 |
$backup_data = [ |
| 132 |
'posts' => $imported_posts, |
| 133 |
'postmeta' => $imported_postmeta, |
| 134 |
'options' => $imported_options, |
| 135 |
'created_date' => $imported_data->created_date ?? current_datetime()->format( 'Y-m-d H:i:s' ), |
| 136 |
'name' => '_yaymail_import_backup_' . $source_version, |
| 137 |
'version' => $source_version, |
| 138 |
]; |
| 139 |
|
| 140 |
$migration_model->reset( $backup_data ); |
| 141 |
|
| 142 |
wp_send_json_success( |
| 143 |
[ |
| 144 |
'message' => __( 'Import state successfully', 'yaymail' ), |
| 145 |
] |
| 146 |
); |
| 147 |
} catch ( \Error $error ) { |
| 148 |
yaymail_get_logger( $error ); |
| 149 |
wp_send_json_error( [ 'mess' => __( 'Import failed: ', 'yaymail' ) . $error->getMessage() ] ); |
| 150 |
} catch ( \Exception $exception ) { |
| 151 |
yaymail_get_logger( $exception ); |
| 152 |
wp_send_json_error( [ 'mess' => __( 'Import failed: ', 'yaymail' ) . $exception->getMessage() ] ); |
| 153 |
}//end try |
| 154 |
} |
| 155 |
|
| 156 |
public function export_state() { |
| 157 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 158 |
if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) { |
| 159 |
return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] ); |
| 160 |
} |
| 161 |
|
| 162 |
try { |
| 163 |
global $wpdb; |
| 164 |
|
| 165 |
/** |
| 166 |
* Backup posts and postmeta |
| 167 |
*/ |
| 168 |
$query_posts = " |
| 169 |
SELECT * |
| 170 |
FROM {$wpdb->posts} |
| 171 |
WHERE post_type = 'yaymail_template' |
| 172 |
"; |
| 173 |
$yaymail_template_posts = $wpdb->get_results( $query_posts );// phpcs:ignore |
| 174 |
|
| 175 |
$query_postmeta = " |
| 176 |
SELECT * |
| 177 |
FROM {$wpdb->postmeta} |
| 178 |
WHERE meta_key LIKE '%yaymail%' |
| 179 |
"; |
| 180 |
$yaymail_template_postmeta = $wpdb->get_results( $query_postmeta );// phpcs:ignore |
| 181 |
|
| 182 |
$backup_data = [ |
| 183 |
'posts' => $yaymail_template_posts, |
| 184 |
'postmeta' => $yaymail_template_postmeta, |
| 185 |
]; |
| 186 |
/** ****************************** */ |
| 187 |
|
| 188 |
/** |
| 189 |
* Backup options |
| 190 |
*/ |
| 191 |
$query_options = " |
| 192 |
SELECT * |
| 193 |
FROM {$wpdb->options} |
| 194 |
WHERE option_name LIKE '%yaymail%' |
| 195 |
"; |
| 196 |
$yaymail_options = $wpdb->get_results( $query_options ); // phpcs:ignore |
| 197 |
$backup_data['options'] = $yaymail_options; |
| 198 |
|
| 199 |
$backup_data['created_date'] = current_datetime()->format( 'Y-m-d H:i:s' ); |
| 200 |
$backup_data['version'] = get_option( 'yaymail_version_backup' ); |
| 201 |
|
| 202 |
$backup_data = apply_filters( 'yaymail_backup_state_data', $backup_data ); |
| 203 |
|
| 204 |
wp_send_json_success( |
| 205 |
[ |
| 206 |
'message' => 'success', |
| 207 |
'data' => $backup_data, |
| 208 |
'file_name' => 'yaymail_export_backup_' . gmdate( 'm-d-Y' ), |
| 209 |
] |
| 210 |
); |
| 211 |
|
| 212 |
} catch ( \Error $error ) { |
| 213 |
yaymail_get_logger( $error ); |
| 214 |
} catch ( \Exception $exception ) { |
| 215 |
yaymail_get_logger( $exception ); |
| 216 |
}//end try |
| 217 |
} |
| 218 |
|
| 219 |
public function sanitize( $array ) { |
| 220 |
|
| 221 |
return wp_kses_post_deep( $array ); |
| 222 |
} |
| 223 |
|
| 224 |
public function process_plugin_installer( $slug ) { |
| 225 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 226 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 227 |
require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php'; |
| 228 |
require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php'; |
| 229 |
|
| 230 |
$api = plugins_api( |
| 231 |
'plugin_information', |
| 232 |
[ |
| 233 |
'slug' => $slug, |
| 234 |
'fields' => [ |
| 235 |
'short_description' => false, |
| 236 |
'sections' => false, |
| 237 |
'requires' => false, |
| 238 |
'rating' => false, |
| 239 |
'ratings' => false, |
| 240 |
'downloaded' => false, |
| 241 |
'last_updated' => false, |
| 242 |
'added' => false, |
| 243 |
'tags' => false, |
| 244 |
'compatibility' => false, |
| 245 |
'homepage' => false, |
| 246 |
'donate_link' => false, |
| 247 |
], |
| 248 |
] |
| 249 |
); |
| 250 |
|
| 251 |
$skin = new \WP_Ajax_Upgrader_Skin(); |
| 252 |
|
| 253 |
$plugin_upgrader = new \Plugin_Upgrader( $skin ); |
| 254 |
|
| 255 |
try { |
| 256 |
$result = $plugin_upgrader->install( $api->download_link ); |
| 257 |
|
| 258 |
if ( is_wp_error( $result ) ) { |
| 259 |
yaymail_get_logger( $result ); |
| 260 |
} |
| 261 |
|
| 262 |
return true; |
| 263 |
} catch ( \Exception $exception ) { |
| 264 |
yaymail_get_logger( $exception ); |
| 265 |
} |
| 266 |
|
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
public function install_yaysmtp() { |
| 271 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 272 |
if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) { |
| 273 |
return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] ); |
| 274 |
} |
| 275 |
|
| 276 |
if ( ! current_user_can( 'install_plugins' ) && ! current_user_can( 'activate_plugins' ) ) { |
| 277 |
return wp_send_json_error( [ 'mess' => __( 'You do not have permission to install plugins', 'yaymail' ) ] ); |
| 278 |
} |
| 279 |
|
| 280 |
try { |
| 281 |
$is_installed = $this->process_plugin_installer( 'yaysmtp' ); |
| 282 |
|
| 283 |
if ( false === $is_installed ) { |
| 284 |
wp_send_json_error( [ 'message' => $is_installed ] ); |
| 285 |
} |
| 286 |
|
| 287 |
$result = activate_plugin( 'yaysmtp/yay-smtp.php' ); |
| 288 |
|
| 289 |
if ( is_wp_error( $result ) ) { |
| 290 |
return wp_send_json_error( [ 'mess' => esc_html( $result->get_error_message() ) ] ); |
| 291 |
} |
| 292 |
|
| 293 |
wp_send_json_success( |
| 294 |
[ |
| 295 |
'installed' => null === $result, |
| 296 |
] |
| 297 |
); |
| 298 |
|
| 299 |
} catch ( \Error $error ) { |
| 300 |
yaymail_get_logger( $error ); |
| 301 |
} catch ( \Exception $exception ) { |
| 302 |
yaymail_get_logger( $exception ); |
| 303 |
}//end try |
| 304 |
} |
| 305 |
|
| 306 |
public function send_test_mail() { |
| 307 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 308 |
if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) { |
| 309 |
return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] ); |
| 310 |
} |
| 311 |
try { |
| 312 |
$template_name = isset( $_POST['template_name'] ) ? sanitize_text_field( wp_unslash( $_POST['template_name'] ) ) : ''; |
| 313 |
$order_id = isset( $_POST['order_id'] ) ? sanitize_text_field( wp_unslash( $_POST['order_id'] ) ) : 'sample_order'; |
| 314 |
$email = isset( $_POST['email'] ) ? sanitize_text_field( $_POST['email'] ) : ''; |
| 315 |
$is_wc_email = strpos( $template_name, 'wp-core-' ) !== 0; |
| 316 |
|
| 317 |
if ( empty( $template_name ) ) { |
| 318 |
return wp_send_json_error( [ 'mess' => __( 'Can\'t find template', 'yaymail' ) ] ); |
| 319 |
} |
| 320 |
|
| 321 |
if ( empty( $order_id ) && $is_wc_email ) { |
| 322 |
return wp_send_json_error( [ 'mess' => __( 'Can\'t find order', 'yaymail' ) ] ); |
| 323 |
} |
| 324 |
|
| 325 |
if ( empty( $email ) ) { |
| 326 |
return wp_send_json_error( [ 'mess' => __( 'Can\'t find email', 'yaymail' ) ] ); |
| 327 |
} |
| 328 |
|
| 329 |
$template = new YayMailTemplate( $template_name ); |
| 330 |
|
| 331 |
$render_data = []; |
| 332 |
|
| 333 |
if ( empty( $order_id ) || ( 'sample_order' === $order_id ) ) { |
| 334 |
$render_data['is_sample'] = true; |
| 335 |
} else { |
| 336 |
$render_data['order'] = wc_get_order( $order_id ); |
| 337 |
} |
| 338 |
|
| 339 |
$render_data['is_customized_preview'] = true; |
| 340 |
// check if email template on preview and send test mail |
| 341 |
|
| 342 |
update_option( 'yaymail_default_email_test', $email ); |
| 343 |
|
| 344 |
$html = $template->get_content( $render_data ); |
| 345 |
|
| 346 |
$headers = "Content-Type: text/html\r\n"; |
| 347 |
$subject = __( 'Email Test', 'yaymail' ); |
| 348 |
|
| 349 |
if ( $is_wc_email ) { |
| 350 |
$class_wc_email = \WC_Emails::instance(); |
| 351 |
$send_mail = $class_wc_email->send( $email, $subject, $html, $headers, [] ); |
| 352 |
} else { |
| 353 |
$send_mail = wp_mail( $email, $subject, $html, $headers, [] ); |
| 354 |
} |
| 355 |
if ( ! $send_mail ) { |
| 356 |
return wp_send_json_error( [ 'mess' => __( 'Can\'t send email', 'yaymail' ) ] ); |
| 357 |
} |
| 358 |
|
| 359 |
wp_send_json_success( |
| 360 |
[ |
| 361 |
'email' => $email, |
| 362 |
'send_mail_success' => $send_mail, |
| 363 |
] |
| 364 |
); |
| 365 |
} catch ( \Error $error ) { |
| 366 |
yaymail_get_logger( $error ); |
| 367 |
} catch ( \Exception $exception ) { |
| 368 |
yaymail_get_logger( $exception ); |
| 369 |
}//end try |
| 370 |
} |
| 371 |
|
| 372 |
public function preview_mail() { |
| 373 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 374 |
if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) { |
| 375 |
return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] ); |
| 376 |
} |
| 377 |
try { |
| 378 |
$order_id = ! empty( $_POST['order_id'] ) ? sanitize_text_field( wp_unslash( $_POST['order_id'] ) ) : 'sample_order'; |
| 379 |
$template_data = isset( $_POST['template_data'] ) ? $this->sanitize( wp_unslash( $_POST['template_data'] ) ) : []; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 380 |
$unsaved_settings = isset( $_POST['unsaved_settings'] ) ? $this->sanitize( wp_unslash( $_POST['unsaved_settings'] ) ) : []; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 381 |
|
| 382 |
if ( empty( $template_data ) ) { |
| 383 |
return wp_send_json_error( [ 'mess' => __( 'Can\'t find template', 'yaymail' ) ] ); |
| 384 |
} |
| 385 |
|
| 386 |
if ( empty( $order_id ) ) { |
| 387 |
return wp_send_json_error( [ 'mess' => __( 'Can\'t find order', 'yaymail' ) ] ); |
| 388 |
} |
| 389 |
|
| 390 |
$template = new YayMailTemplate( $template_data['name'] ); |
| 391 |
|
| 392 |
if ( ! empty( $unsaved_settings ) ) { |
| 393 |
global $yaymail_unsaved_settings; |
| 394 |
$yaymail_unsaved_settings = $unsaved_settings; |
| 395 |
} |
| 396 |
|
| 397 |
$template->set_background_color( $template_data['background_color'] ); |
| 398 |
$template->set_text_link_color( $template_data['text_link_color'] ); |
| 399 |
$template->set_global_header_settings( $template_data['global_header_settings'] ); |
| 400 |
$template->set_global_footer_settings( $template_data['global_footer_settings'] ); |
| 401 |
$template->set_elements( $template_data['elements'] ); |
| 402 |
|
| 403 |
$render_data = []; |
| 404 |
|
| 405 |
if ( empty( $order_id ) || ( 'sample_order' === $order_id ) ) { |
| 406 |
$render_data['is_sample'] = true; |
| 407 |
} else { |
| 408 |
$render_data['order'] = wc_get_order( $order_id ); |
| 409 |
} |
| 410 |
|
| 411 |
$render_data['is_customized_preview'] = true; |
| 412 |
// check if email template on preview and send test mail |
| 413 |
|
| 414 |
$html = $template->get_content( $render_data ); |
| 415 |
|
| 416 |
if ( strpos( $template->get_name(), 'wp-core-' ) !== 0 ) { |
| 417 |
// TODO: render with passing settings |
| 418 |
$current_email = null; |
| 419 |
$subject = 'Sample Subject'; |
| 420 |
$emails = wc()->mailer()->emails; |
| 421 |
foreach ( $emails as $email ) { |
| 422 |
if ( $email->id === $template_data['name'] ) { |
| 423 |
$current_email = $email; |
| 424 |
if ( method_exists( $current_email, 'set_object' ) ) { |
| 425 |
if ( ! empty( $render_data['order'] ) && is_a( $render_data['order'], '\WC_Order' ) ) { |
| 426 |
$current_email->set_object( $render_data['order'] ); |
| 427 |
} else { |
| 428 |
$current_email->set_object( Helpers::get_dummy_order() ); |
| 429 |
} |
| 430 |
} |
| 431 |
break; |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
if ( ! empty( $current_email ) ) { |
| 437 |
$subject = $current_email->get_subject(); |
| 438 |
} |
| 439 |
$email_address = wp_get_current_user()->user_email ?? 'sample@example.com'; |
| 440 |
|
| 441 |
wp_send_json_success( |
| 442 |
[ |
| 443 |
'html' => $html, |
| 444 |
'subject' => $subject, |
| 445 |
'email_address' => $email_address, |
| 446 |
] |
| 447 |
); |
| 448 |
} catch ( \Error $error ) { |
| 449 |
yaymail_get_logger( $error ); |
| 450 |
} catch ( \Exception $exception ) { |
| 451 |
yaymail_get_logger( $exception ); |
| 452 |
}//end try |
| 453 |
} |
| 454 |
|
| 455 |
public function preview_mail_for_woo() { |
| 456 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 457 |
if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) { |
| 458 |
return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] ); |
| 459 |
} |
| 460 |
try { |
| 461 |
$template_name = isset( $_POST['template_name'] ) ? sanitize_text_field( wp_unslash( $_POST['template_name'] ) ) : ''; |
| 462 |
$search_order_id = isset( $_POST['search_order_id'] ) ? sanitize_text_field( wp_unslash( $_POST['search_order_id'] ) ) : null; |
| 463 |
$email_address = isset( $_POST['email_address'] ) ? sanitize_text_field( wp_unslash( $_POST['email_address'] ) ) : ''; |
| 464 |
|
| 465 |
$email_preview_output = PreviewEmail\PreviewEmailWoo::email_preview_output( $search_order_id, $template_name, $email_address ); |
| 466 |
|
| 467 |
$email_preview_output = apply_filters( 'yaymail_preview_email', $email_preview_output, $search_order_id, $template_name, $email_address ); |
| 468 |
|
| 469 |
$send_mail = false; |
| 470 |
|
| 471 |
if ( ! empty( $email_address ) && ! empty( $email_preview_output['html'] ) ) { |
| 472 |
$headers = "Content-Type: text/html\r\n"; |
| 473 |
$class_wc_email = \WC_Emails::instance(); |
| 474 |
$subject = __( 'Email Preview', 'yaymail' ); |
| 475 |
$send_mail = $class_wc_email->send( $email_address, $subject, $email_preview_output['html'], $headers, [] ); |
| 476 |
if ( ! $send_mail ) { |
| 477 |
return wp_send_json_error( [ 'mess' => __( 'Can\'t send email', 'yaymail' ) ] ); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
wp_send_json_success( |
| 482 |
[ |
| 483 |
'html' => ! empty( $email_preview_output['html'] ) ? $email_preview_output['html'] : __( 'No email content found', 'yaymail' ), |
| 484 |
'subject' => ! empty( $email_preview_output['subject'] ) ? $email_preview_output['subject'] : __( 'No subject found', 'yaymail' ), |
| 485 |
'is_disabled_send_mail' => ! empty( $email_preview_output['is_disabled_send_mail'] ) ? $email_preview_output['is_disabled_send_mail'] : false, |
| 486 |
'send_mail_success' => $send_mail, |
| 487 |
] |
| 488 |
); |
| 489 |
} catch ( \Error $error ) { |
| 490 |
yaymail_get_logger( $error ); |
| 491 |
} catch ( \Exception $exception ) { |
| 492 |
yaymail_get_logger( $exception ); |
| 493 |
}//end try |
| 494 |
} |
| 495 |
|
| 496 |
public function export_templates() { |
| 497 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 498 |
if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) { |
| 499 |
return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] ); |
| 500 |
} |
| 501 |
try { |
| 502 |
$templates = isset( $_POST['templates'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['templates'] ) ) : []; |
| 503 |
// TODO: sanitize |
| 504 |
$default = [ |
| 505 |
'post_type' => 'yaymail_template', |
| 506 |
'post_status' => [ 'publish', 'pending', 'future' ], |
| 507 |
'posts_per_page' => '-1', |
| 508 |
'meta_query' => [ |
| 509 |
[ |
| 510 |
'key' => YayMailTemplate::META_KEYS['name'], |
| 511 |
'value' => $templates, |
| 512 |
'compare' => 'IN', |
| 513 |
], |
| 514 |
], |
| 515 |
]; |
| 516 |
$export_data = []; |
| 517 |
$query = new \WP_Query( $default ); |
| 518 |
if ( $query->have_posts() ) { |
| 519 |
$posts = $query->get_posts(); |
| 520 |
foreach ( $posts as $post ) { |
| 521 |
$template_name = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['name'], true ); |
| 522 |
$elements = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['elements'], true ); |
| 523 |
$language = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['language'], true ); |
| 524 |
$text_link_color = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['text_link_color'], true ); |
| 525 |
$background_color = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['background_color'], true ); |
| 526 |
$content_background_color = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['content_background_color'], true ); |
| 527 |
$content_text_color = get_post_meta( $post->ID, YayMailTemplate::META_KEYS['content_text_color'], true ); |
| 528 |
$file_name = "{$template_name}.json"; |
| 529 |
if ( empty( $language ) ) { |
| 530 |
$export_data[] = [ |
| 531 |
'file_name' => $file_name, |
| 532 |
'templates_data' => [ |
| 533 |
'template' => $template_name, |
| 534 |
'elements' => $elements, |
| 535 |
'language' => '', |
| 536 |
'text_link_color' => $text_link_color, |
| 537 |
'background_color' => $background_color, |
| 538 |
'content_background_color' => $content_background_color, |
| 539 |
'content_text_color' => $content_text_color, |
| 540 |
'title_color' => $title_color, |
| 541 |
], |
| 542 |
]; |
| 543 |
} |
| 544 |
}//end foreach |
| 545 |
}//end if |
| 546 |
wp_reset_postdata(); |
| 547 |
wp_send_json_success( |
| 548 |
[ |
| 549 |
'message' => __( 'Export successfully', 'yaymail' ), |
| 550 |
'data' => $export_data, |
| 551 |
'file_name' => 'yaymail_customizer_templates_' . gmdate( 'm-d-Y' ), |
| 552 |
] |
| 553 |
); |
| 554 |
} catch ( \Error $error ) { |
| 555 |
yaymail_get_logger( $error ); |
| 556 |
} catch ( \Exception $exception ) { |
| 557 |
yaymail_get_logger( $exception ); |
| 558 |
}//end try |
| 559 |
} |
| 560 |
|
| 561 |
public function import_templates() { |
| 562 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 563 |
if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) { |
| 564 |
return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] ); |
| 565 |
} |
| 566 |
try { |
| 567 |
if ( ! empty( $_FILES ) ) { |
| 568 |
$result = $this->process_import( $_FILES ); |
| 569 |
wp_send_json_success( |
| 570 |
[ |
| 571 |
'imported_data' => $result, |
| 572 |
] |
| 573 |
); |
| 574 |
} else { |
| 575 |
wp_send_json_error( [ 'message' => __( 'Can\'t find import files.', 'yaymail' ) ] ); |
| 576 |
} |
| 577 |
} catch ( \Error $error ) { |
| 578 |
yaymail_get_logger( $error ); |
| 579 |
} catch ( \Exception $exception ) { |
| 580 |
yaymail_get_logger( $exception ); |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
public function process_import( $files ) { |
| 585 |
global $wp_filesystem; |
| 586 |
if ( empty( $wp_filesystem ) ) { |
| 587 |
require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 588 |
WP_Filesystem(); |
| 589 |
} |
| 590 |
$imported_templates = []; |
| 591 |
$is_legacy = false; |
| 592 |
foreach ( $files as $file ) { |
| 593 |
if ( isset( $file['type'] ) ) { |
| 594 |
if ( 'application/json' === $file['type'] ) { |
| 595 |
if ( ! empty( $file['tmp_name'] ) ) { |
| 596 |
$file_tmp_name = sanitize_text_field( $file['tmp_name'] ); |
| 597 |
$file_content = $wp_filesystem->get_contents( $file_tmp_name ); |
| 598 |
$file_content = json_decode( $file_content, true ); |
| 599 |
if ( ! isset( $file_content['template'] ) ) { |
| 600 |
if ( isset( $file_content['yaymailTemplateExport'] ) ) { |
| 601 |
$is_legacy = true; |
| 602 |
$imported_templates = array_merge( $imported_templates, $this->process_import_legacy( $file_content ) ); |
| 603 |
} else { |
| 604 |
continue; |
| 605 |
} |
| 606 |
} else { |
| 607 |
$update_result = $this->processing_import_update_data( $file_content ); |
| 608 |
if ( ! empty( $update_result ) ) { |
| 609 |
$imported_templates[] = $update_result; |
| 610 |
} |
| 611 |
} |
| 612 |
}//end if |
| 613 |
}//end if |
| 614 |
}//end if |
| 615 |
}//end foreach |
| 616 |
if ( $is_legacy ) { |
| 617 |
MainMigration::get_instance()->migrate( true ); |
| 618 |
} |
| 619 |
return $imported_templates; |
| 620 |
} |
| 621 |
|
| 622 |
public function process_import_legacy( $file_content ) { |
| 623 |
$updated_templates = []; |
| 624 |
// Import templates |
| 625 |
foreach ( $file_content['yaymailTemplateExport'] as $template ) { |
| 626 |
$updated_result = $this->processing_import_update_data( $template, true ); |
| 627 |
if ( ! empty( $updated_result ) ) { |
| 628 |
$updated_templates[] = $updated_result; |
| 629 |
} |
| 630 |
}//end foreach |
| 631 |
// Import settings |
| 632 |
$import_settings = isset( $file_content['yaymail_settings'] ) ? $file_content['yaymail_settings'] : []; |
| 633 |
if ( ! empty( $import_settings ) ) { |
| 634 |
update_option( 'yaymail_settings', $import_settings ); |
| 635 |
} |
| 636 |
return $updated_templates; |
| 637 |
} |
| 638 |
|
| 639 |
public function processing_import_update_data( $data, $is_legacy = false ) { |
| 640 |
$template_name = $data['template'] ?? null; |
| 641 |
$elements = $data['elements'] ?? null; |
| 642 |
$text_link_color = $data['text_link_color'] ?? null; |
| 643 |
$background_color = $data['background_color'] ?? null; |
| 644 |
$title_color = $data['title_color'] ?? null; |
| 645 |
|
| 646 |
if ( empty( $template_name ) ) { |
| 647 |
return null; |
| 648 |
} |
| 649 |
|
| 650 |
$template = new YayMailTemplate( $template_name ); |
| 651 |
if ( ! $template->is_exists() ) { |
| 652 |
return null; |
| 653 |
} |
| 654 |
|
| 655 |
$template->set_elements( $elements ); |
| 656 |
$template->set_text_link_color( $text_link_color ); |
| 657 |
$template->set_background_color( $background_color ); |
| 658 |
$template->set_title_color( $title_color ); |
| 659 |
$template->set_content_background_color( $content_background_color ); |
| 660 |
$template->set_content_text_color( $content_text_color ); |
| 661 |
if ( $is_legacy ) { |
| 662 |
$template->set_status( 'inactive' ); |
| 663 |
} |
| 664 |
|
| 665 |
$template->save(); |
| 666 |
|
| 667 |
wp_reset_postdata(); |
| 668 |
|
| 669 |
return [ |
| 670 |
'template_name' => $template_name, |
| 671 |
]; |
| 672 |
} |
| 673 |
|
| 674 |
/** |
| 675 |
* Process a custom hook request and generate HTML content. |
| 676 |
* |
| 677 |
* This function handles a custom hook request, generates HTML content based on the provided data and attributes. |
| 678 |
* It is designed to be used as an AJAX callback. |
| 679 |
* |
| 680 |
* @example $_POST['data'] = |
| 681 |
* [ |
| 682 |
* 'template_data' => YayMail\YayMailTemplate, |
| 683 |
* 'order_id' => 'sample_order', |
| 684 |
* 'attributes' => [ |
| 685 |
* [ |
| 686 |
* 'name' => 'hook', |
| 687 |
* 'value' => 'your_hook' |
| 688 |
* ], |
| 689 |
* [ |
| 690 |
* 'name' => 'background_color', |
| 691 |
* 'value' => '#ffffff' |
| 692 |
* ] |
| 693 |
* ] |
| 694 |
* ] |
| 695 |
* |
| 696 |
* @return void This function sends a JSON response with HTML content or error messages. |
| 697 |
*/ |
| 698 |
public function get_custom_hook_html() { |
| 699 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 700 |
if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) { |
| 701 |
return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] ); |
| 702 |
} |
| 703 |
try { |
| 704 |
$attributes = isset( $_POST['data']['attributes'] ) ? $_POST['data']['attributes'] : []; // phpcs:ignore |
| 705 |
if ( empty( $attributes ) ) { |
| 706 |
return wp_send_json_error( [ 'mess' => __( 'Attributes empty', 'yaymail' ) ] ); |
| 707 |
} |
| 708 |
|
| 709 |
/** |
| 710 |
* Build data for shortcode |
| 711 |
*/ |
| 712 |
$template_model = \YayMail\Models\TemplateModel::get_instance(); |
| 713 |
$data = []; |
| 714 |
if ( ! empty( $_POST['data']['template_data'] ) ) { |
| 715 |
$data = \YayMail\Models\TemplateModel::get_shortcode_executor_data( sanitize_text_field( wp_unslash( $_POST['data']['template_data'] ) ), sanitize_text_field( wp_unslash( $_POST['data']['order_id'] ) ) ); |
| 716 |
|
| 717 |
$data['template']->set_props( sanitize_text_field( wp_unslash( $_POST['data']['template_data'] ) ) ); |
| 718 |
} |
| 719 |
|
| 720 |
$hook_shortcodes = \YayMail\Shortcodes\HookShortcodes::get_instance(); |
| 721 |
$html = $hook_shortcodes->yaymail_handle_custom_hook_shortcode( $data, $attributes ); |
| 722 |
wp_send_json_success( |
| 723 |
[ |
| 724 |
'html' => $html, |
| 725 |
] |
| 726 |
); |
| 727 |
} catch ( \Error $error ) { |
| 728 |
yaymail_get_logger( $error ); |
| 729 |
} catch ( \Exception $exception ) { |
| 730 |
yaymail_get_logger( $exception ); |
| 731 |
}//end try |
| 732 |
} |
| 733 |
|
| 734 |
/** |
| 735 |
* Get all needed data when load YayMail template to customizer. |
| 736 |
*/ |
| 737 |
public function get_template_data_onload() { |
| 738 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 739 |
if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) { |
| 740 |
return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] ); |
| 741 |
} |
| 742 |
try { |
| 743 |
$setting_model = SettingModel::get_instance(); |
| 744 |
$settings_data = $setting_model->find_all(); |
| 745 |
|
| 746 |
$template_name = isset( $_POST['data']['template_name'] ) ? sanitize_text_field( $_POST['data']['template_name'] ) : 'new_order'; |
| 747 |
$order_id = isset( $_POST['data']['order_id'] ) ? sanitize_text_field( $_POST['data']['order_id'] ) : 'sample_order'; |
| 748 |
|
| 749 |
$template_model = TemplateModel::get_instance(); |
| 750 |
|
| 751 |
$shortcodes_data = $template_model->get_shortcodes_by_template_name_and_order_id( $template_name, $order_id ); |
| 752 |
|
| 753 |
$templates_data = apply_filters( 'yaymail_get_all_templates', $template_model->find_all() ); |
| 754 |
|
| 755 |
$selected_template_data = $template_model->find_by_name( $template_name ); |
| 756 |
|
| 757 |
$elements_data = TemplateModel::get_elements_for_template( $template_name ); |
| 758 |
|
| 759 |
$revision_model = RevisionModel::get_instance(); |
| 760 |
$revisions_data = $revision_model->get_by_template( $template_name ); |
| 761 |
|
| 762 |
wp_send_json_success( |
| 763 |
[ |
| 764 |
'settings_data' => $settings_data, |
| 765 |
'templates_data' => $templates_data, |
| 766 |
'selected_template_data' => $selected_template_data, |
| 767 |
'elements_data' => $elements_data, |
| 768 |
'revisions_data' => $revisions_data, |
| 769 |
'shortcodes_data' => $shortcodes_data, |
| 770 |
] |
| 771 |
); |
| 772 |
} catch ( \Error $error ) { |
| 773 |
yaymail_get_logger( $error ); |
| 774 |
wp_send_json_error( [ 'mess' => $error->getMessage() ] ); |
| 775 |
} catch ( \Exception $exception ) { |
| 776 |
yaymail_get_logger( $exception ); |
| 777 |
wp_send_json_error( [ 'mess' => $exception->getMessage() ] ); |
| 778 |
} catch ( \Throwable $throwable ) { |
| 779 |
yaymail_get_logger( $throwable ); |
| 780 |
wp_send_json_error( [ 'mess' => $throwable->getMessage() ] ); |
| 781 |
}//end try |
| 782 |
} |
| 783 |
|
| 784 |
public function yaymail_review() { |
| 785 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 786 |
if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) { |
| 787 |
return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] ); |
| 788 |
} |
| 789 |
try { |
| 790 |
|
| 791 |
$yaymail_review = update_option( 'yaymail_review', true ); |
| 792 |
|
| 793 |
wp_send_json_success( |
| 794 |
[ |
| 795 |
'reviewed' => $yaymail_review, |
| 796 |
] |
| 797 |
); |
| 798 |
|
| 799 |
} catch ( \Error $error ) { |
| 800 |
yaymail_get_logger( $error ); |
| 801 |
} catch ( \Exception $exception ) { |
| 802 |
yaymail_get_logger( $exception ); |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
public function change_ghf_tour() { |
| 807 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 808 |
if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) { |
| 809 |
return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] ); |
| 810 |
} |
| 811 |
|
| 812 |
try { |
| 813 |
$next_move = isset( $_POST['next_move'] ) ? sanitize_text_field( wp_unslash( $_POST['next_move'] ) ) : 'initial'; |
| 814 |
$ghf_tour = update_option( 'yaymail_ghf_tour', $next_move ); |
| 815 |
|
| 816 |
wp_send_json_success( |
| 817 |
[ |
| 818 |
'ghf_tour' => $ghf_tour, |
| 819 |
] |
| 820 |
); |
| 821 |
} catch ( \Error $error ) { |
| 822 |
yaymail_get_logger( $error ); |
| 823 |
} catch ( \Exception $exception ) { |
| 824 |
yaymail_get_logger( $exception ); |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
public function dismiss_multi_select_notice() { |
| 829 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 830 |
if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) { |
| 831 |
return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] ); |
| 832 |
} |
| 833 |
|
| 834 |
try { |
| 835 |
update_option( 'yaymail_show_multi_select_notice', 'no' ); |
| 836 |
|
| 837 |
wp_send_json_success( |
| 838 |
[ |
| 839 |
'show_multi_select_notice' => 'no', |
| 840 |
] |
| 841 |
); |
| 842 |
} catch ( \Error $error ) { |
| 843 |
yaymail_get_logger( $error ); |
| 844 |
} catch ( \Exception $exception ) { |
| 845 |
yaymail_get_logger( $exception ); |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
public function dismiss_new_element_notification() { |
| 850 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 851 |
if ( ! wp_verify_nonce( $nonce, 'yaymail_frontend_nonce' ) ) { |
| 852 |
return wp_send_json_error( [ 'mess' => __( 'Verify nonce failed', 'yaymail' ) ] ); |
| 853 |
} |
| 854 |
|
| 855 |
$elements = isset( $_POST['elements'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['elements'] ) ) : []; |
| 856 |
|
| 857 |
$viewed_new_elements = get_option( 'yaymail_viewed_new_elements', [] ); |
| 858 |
$viewed_new_elements = array_unique( array_merge( $viewed_new_elements, $elements ) ); |
| 859 |
|
| 860 |
try { |
| 861 |
update_option( 'yaymail_viewed_new_elements', $viewed_new_elements ); |
| 862 |
|
| 863 |
wp_send_json_success( |
| 864 |
[ |
| 865 |
'viewed_new_elements' => $viewed_new_elements, |
| 866 |
] |
| 867 |
); |
| 868 |
} catch ( \Error $error ) { |
| 869 |
wp_send_json_error( [ 'mess' => $error->getMessage() ] ); |
| 870 |
} catch ( \Exception $exception ) { |
| 871 |
yaymail_get_logger( $exception ); |
| 872 |
} |
| 873 |
} |
| 874 |
} |
| 875 |
|