| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail; |
| 4 |
|
| 5 |
use YayMail\Helper\Helper; |
| 6 |
use YayMail\Helper\LogHelper; |
| 7 |
use YayMail\MailBuilder\Shortcodes; |
| 8 |
use YayMail\Page\Source\CustomPostType; |
| 9 |
use YayMail\Page\Source\DefaultElement; |
| 10 |
use YayMail\Templates\Templates; |
| 11 |
|
| 12 |
defined( 'ABSPATH' ) || exit; |
| 13 |
|
| 14 |
class Ajax { |
| 15 |
|
| 16 |
protected static $instance = null; |
| 17 |
|
| 18 |
public static function getInstance() { |
| 19 |
if ( null == self::$instance ) { |
| 20 |
self::$instance = new self(); |
| 21 |
self::$instance->doHooks(); |
| 22 |
} |
| 23 |
|
| 24 |
return self::$instance; |
| 25 |
} |
| 26 |
private function doHooks() { |
| 27 |
add_action( 'wp_ajax_yaymail_send_mail', array( $this, 'sendTestMail' ) ); |
| 28 |
add_action( 'wp_ajax_yaymail_parse_template', array( new Shortcodes(), 'templateParser' ) ); |
| 29 |
|
| 30 |
add_action( 'wp_ajax_yaymail_save_mail', array( $this, 'saveTemplate' ) ); |
| 31 |
add_action( 'wp_ajax_yaymail_copy_mail', array( $this, 'copyTemplate' ) ); |
| 32 |
add_action( 'wp_ajax_yaymail_reset_template', array( $this, 'resetTemplate' ) ); |
| 33 |
add_action( 'wp_ajax_yaymail_export_all_template', array( $this, 'exportAllTemplate' ) ); |
| 34 |
add_action( 'wp_ajax_yaymail_import_template', array( $this, 'importAllTemplate' ) ); |
| 35 |
add_action( 'wp_ajax_yaymail_enable_disable_template', array( $this, 'enableDisableTempalte' ) ); |
| 36 |
add_action( 'wp_ajax_yaymail_general_setting', array( $this, 'generalSettings' ) ); |
| 37 |
} |
| 38 |
|
| 39 |
private function __construct() {} |
| 40 |
// public function showMailError($wp_error) |
| 41 |
// { |
| 42 |
// wp_send_json_error(array('mess' => $wp_error)); |
| 43 |
// } |
| 44 |
public function exportAllTemplate() { |
| 45 |
try { |
| 46 |
// 1. check nonce |
| 47 |
Helper::checkNonce(); |
| 48 |
// 2. download |
| 49 |
$template_export = CustomPostType::getTemplateExport(); |
| 50 |
$fileName = 'yaymail_all-customize-email-templates_' . gmdate( 'm-d-Y' ) . '.json'; |
| 51 |
header( 'Content-Type: application/json' ); |
| 52 |
header( 'Content-Disposition: attachment; filename="' . $fileName . '";' ); |
| 53 |
$response_object = array( |
| 54 |
'result' => $template_export, |
| 55 |
'fileName' => $fileName, |
| 56 |
'mess' => __( 'Export successfully.', 'yaymail' ), |
| 57 |
); |
| 58 |
wp_send_json_success( $response_object ); |
| 59 |
} catch ( \Exception $ex ) { |
| 60 |
LogHelper::getMessageException( $ex, true ); |
| 61 |
} catch ( \Error $ex ) { |
| 62 |
LogHelper::getMessageException( $ex, true ); |
| 63 |
} |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
// html output of mail must to map with html output in single-mail-template.php |
| 68 |
public function sendTestMail() { |
| 69 |
try { |
| 70 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; |
| 71 |
if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) { |
| 72 |
wp_send_json_error( array( 'mess' => 'Nonce is invalid' ) ); |
| 73 |
} else { |
| 74 |
if ( isset( $_POST['order_id'] ) && isset( $_POST['template'] ) && isset( $_POST['email_address'] ) ) { |
| 75 |
// Helper::checkNonce(); |
| 76 |
$template = sanitize_text_field( $_POST['template'] ); |
| 77 |
$email_address = sanitize_email( $_POST['email_address'] ); |
| 78 |
// check email |
| 79 |
if ( ! is_email( $email_address ) ) { |
| 80 |
wp_send_json_error( array( 'mess' => __( 'Invalid email format!', 'yaymail' ) ) ); |
| 81 |
} |
| 82 |
if ( CustomPostType::postIDByTemplate( $template ) ) { |
| 83 |
update_user_meta( get_current_user_id(), 'yaymail_default_email_test', $email_address ); |
| 84 |
$customShortcode = new Shortcodes( $template, sanitize_text_field( $_POST['order_id'] ) ); |
| 85 |
if ( sanitize_text_field( $_POST['order_id'] ) !== 'sampleOrder' ) { |
| 86 |
$order_id = intval( sanitize_text_field( $_POST['order_id'] ) ); |
| 87 |
} |
| 88 |
$postID = CustomPostType::postIDByTemplate( $template ); |
| 89 |
$order_tn = 'mail-sent'; |
| 90 |
$customShortcode->setOrderId( $order_id, true ); |
| 91 |
$customShortcode->shortCodesOrderDefined(); |
| 92 |
$html = do_shortcode( get_post_meta( $postID, '_yaymail_html', true ) ); |
| 93 |
$html = html_entity_decode( $html, ENT_QUOTES, 'UTF-8' ); |
| 94 |
$headers = "Content-Type: text/html\r\n"; |
| 95 |
$sendMail = \WC_Emails::instance(); |
| 96 |
$subjectEmail = $this->getSubjectEmail( $sendMail, $template ); |
| 97 |
// $email_admin = get_bloginfo('admin_email'); |
| 98 |
if ( ! empty( $email_address ) ) { |
| 99 |
$sendMailSucc = $sendMail->send( $email_address, $subjectEmail, $html, $headers, array() ); |
| 100 |
wp_send_json_success( |
| 101 |
array( |
| 102 |
'sendMailSucc' => $sendMailSucc, |
| 103 |
'mess' => __( |
| 104 |
'Email has been sent.', |
| 105 |
'yaymail' |
| 106 |
), |
| 107 |
) |
| 108 |
); |
| 109 |
} |
| 110 |
} else { |
| 111 |
wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) ); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
wp_send_json_error( array( 'mess' => __( 'Error send mail!', 'yaymail' ) ) ); |
| 116 |
} catch ( \Exception $ex ) { |
| 117 |
LogHelper::getMessageException( $ex, true ); |
| 118 |
} catch ( \Error $ex ) { |
| 119 |
LogHelper::getMessageException( $ex, true ); |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
public function getSubjectEmail( $wc_emails, $template ) { |
| 125 |
foreach ( $wc_emails->emails as $email => $item ) { |
| 126 |
if ( $item->id == $template ) { |
| 127 |
if ( 'customer_invoice' == $template ) { |
| 128 |
$subject = Helper::getCustomerInvoiceSubject( $wc_emails->emails[ $email ] ); |
| 129 |
} else { |
| 130 |
$subject = $wc_emails->emails[ $email ]->get_subject(); |
| 131 |
} |
| 132 |
|
| 133 |
return $subject; |
| 134 |
} |
| 135 |
} |
| 136 |
return null; |
| 137 |
} |
| 138 |
|
| 139 |
public function saveTemplate() { |
| 140 |
try { |
| 141 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; |
| 142 |
if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) { |
| 143 |
wp_send_json_error( array( 'mess' => 'Nonce is invalid' ) ); |
| 144 |
} else { |
| 145 |
if ( isset( $_POST['template'] ) && isset( $_POST['htmlContent'] ) ) { |
| 146 |
// Helper::checkNonce(); |
| 147 |
$emailBackgroundColor = isset( $_POST['emailBackgroundColor'] ) ? sanitize_text_field( $_POST['emailBackgroundColor'] ) : 'rgb(236, 236, 236)'; |
| 148 |
$emailTextLinkColor = isset( $_POST['emailTextLinkColor'] ) ? sanitize_text_field( $_POST['emailTextLinkColor'] ) : '#96588a'; |
| 149 |
$titleShipping = isset( $_POST['titleShipping'] ) ? sanitize_text_field( $_POST['titleShipping'] ) : 'Shipping Address'; |
| 150 |
$titleBilling = isset( $_POST['titleBilling'] ) ? sanitize_text_field( $_POST['titleBilling'] ) : 'Billing Address'; |
| 151 |
$orderTitle = isset( $_POST['orderTitle'] ) ? filter_var_array( $_POST['orderTitle'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 152 |
$template = sanitize_text_field( $_POST['template'] ); |
| 153 |
$htmlContent = filter_var( $_POST['htmlContent'], FILTER_SANITIZE_SPECIAL_CHARS, FILTER_FLAG_STRIP_LOW ); |
| 154 |
$emailContents = isset( $_POST['emailContents'] ) ? filter_var_array( $_POST['emailContents'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 155 |
foreach ( $emailContents as $key => $value ) { |
| 156 |
if ( 'TwoColumns' == $value['type'] || 'ThreeColumns' == $value['type'] || 'FourColumns' == $value['type'] ) { |
| 157 |
if ( ! array_key_exists( 'column1', $emailContents[ $key ]['settingRow'] ) ) { |
| 158 |
$emailContents[ $key ]['settingRow']['column1'] = array(); |
| 159 |
} |
| 160 |
if ( ! array_key_exists( 'column2', $emailContents[ $key ]['settingRow'] ) ) { |
| 161 |
$emailContents[ $key ]['settingRow']['column2'] = array(); |
| 162 |
} |
| 163 |
if ( ( 'ThreeColumns' == $value['type'] || 'FourColumns' == $value['type'] ) && ! array_key_exists( 'column3', $emailContents[ $key ]['settingRow'] ) ) { |
| 164 |
$emailContents[ $key ]['settingRow']['column3'] = array(); |
| 165 |
} |
| 166 |
if ( 'FourColumns' == $value['type'] && ! array_key_exists( 'column4', $emailContents[ $key ]['settingRow'] ) ) { |
| 167 |
$emailContents[ $key ]['settingRow']['column4'] = array(); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
if ( CustomPostType::postIDByTemplate( $template ) ) { |
| 172 |
$postID = CustomPostType::postIDByTemplate( $template ); |
| 173 |
// write Log use to check error |
| 174 |
// LogHelper::writeLogContent($htmlContent, 'html'); |
| 175 |
// LogHelper::writeLogContent($emailContents, 'text'); |
| 176 |
|
| 177 |
update_post_meta( $postID, '_yaymail_html', $htmlContent ); |
| 178 |
update_post_meta( $postID, '_yaymail_elements', $emailContents ); |
| 179 |
update_post_meta( $postID, '_email_backgroundColor_settings', $emailBackgroundColor ); |
| 180 |
update_post_meta( $postID, '_yaymail_email_textLinkColor_settings', $emailTextLinkColor ); |
| 181 |
update_post_meta( $postID, '_email_title_shipping', $titleShipping ); |
| 182 |
update_post_meta( $postID, '_email_title_billing', $titleBilling ); |
| 183 |
update_post_meta( $postID, '_yaymail_email_order_item_title', $orderTitle ); |
| 184 |
wp_send_json_success( array( 'mess' => __( 'Email has been saved.', 'yaymail' ) ) ); |
| 185 |
} else { |
| 186 |
wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) ); |
| 187 |
} |
| 188 |
} |
| 189 |
wp_send_json_error( array( 'mess' => __( 'Error save data.', 'yaymail' ) ) ); |
| 190 |
} |
| 191 |
} catch ( \Exception $ex ) { |
| 192 |
LogHelper::getMessageException( $ex, true ); |
| 193 |
} catch ( \Error $ex ) { |
| 194 |
LogHelper::getMessageException( $ex, true ); |
| 195 |
} |
| 196 |
|
| 197 |
} |
| 198 |
public function copyTemplate() { |
| 199 |
try { |
| 200 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; |
| 201 |
if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) { |
| 202 |
wp_send_json_error( array( 'mess' => 'Nonce is invalid' ) ); |
| 203 |
} else { |
| 204 |
if ( isset( $_POST['copy_to'] ) && isset( $_POST['copy_from'] ) ) { |
| 205 |
Helper::checkNonce(); |
| 206 |
$copyTo = sanitize_text_field( $_POST['copy_to'] ); |
| 207 |
$copyFrom = sanitize_text_field( $_POST['copy_from'] ); |
| 208 |
if ( CustomPostType::postIDByTemplate( $copyFrom ) ) { |
| 209 |
$postID = CustomPostType::postIDByTemplate( $copyFrom ); |
| 210 |
$htmlFrom = get_post_meta( $postID, '_yaymail_html', true ); |
| 211 |
$emailContentsFrom = get_post_meta( $postID, '_yaymail_elements', true ); |
| 212 |
$emailBackgroundColor = get_post_meta( $postID, '_email_backgroundColor_settings', true ) ? get_post_meta( $postID, '_email_backgroundColor_settings', true ) : 'rgb(236, 236, 236)'; |
| 213 |
$emailTextLinkColor = get_post_meta( $postID, '_yaymail_email_textLinkColor_settings', true ) ? get_post_meta( $postID, '_yaymail_email_textLinkColor_settings', true ) : '#96588a'; |
| 214 |
$titleShipping = isset( $_POST['titleShipping'] ) ? sanitize_text_field( $_POST['titleShipping'] ) : 'Shipping Address'; |
| 215 |
$titleBilling = isset( $_POST['titleBilling'] ) ? sanitize_text_field( $_POST['titleBilling'] ) : 'Billing Address'; |
| 216 |
$orderTitle = isset( $_POST['orderTitle'] ) ? filter_var_array( $_POST['orderTitle'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 217 |
if ( CustomPostType::postIDByTemplate( $copyTo ) ) { |
| 218 |
$idTo = CustomPostType::postIDByTemplate( $copyTo ); |
| 219 |
update_post_meta( $idTo, '_yaymail_html', $htmlFrom ); |
| 220 |
update_post_meta( $idTo, '_yaymail_elements', $emailContentsFrom ); |
| 221 |
update_post_meta( $idTo, '_email_backgroundColor_settings', $emailBackgroundColor ); |
| 222 |
update_post_meta( $idTo, '_yaymail_email_textLinkColor_settings', $emailTextLinkColor ); |
| 223 |
update_post_meta( $idTo, '_email_title_shipping', $titleShipping ); |
| 224 |
update_post_meta( $idTo, '_email_title_billing', $titleBilling ); |
| 225 |
update_post_meta( $idTo, '_yaymail_email_order_item_title', $orderTitle ); |
| 226 |
wp_send_json_success( |
| 227 |
array( |
| 228 |
'mess' => __( 'Copied Template successfully.', 'yaymail' ), |
| 229 |
'data' => $emailContentsFrom, |
| 230 |
) |
| 231 |
); |
| 232 |
} else { |
| 233 |
wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) ); |
| 234 |
} |
| 235 |
} else { |
| 236 |
wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) ); |
| 237 |
} |
| 238 |
} |
| 239 |
wp_send_json_error( array( 'mess' => __( 'Error save data.', 'yaymail' ) ) ); |
| 240 |
} |
| 241 |
} catch ( \Exception $ex ) { |
| 242 |
LogHelper::getMessageException( $ex, true ); |
| 243 |
} catch ( \Error $ex ) { |
| 244 |
LogHelper::getMessageException( $ex, true ); |
| 245 |
} |
| 246 |
|
| 247 |
} |
| 248 |
|
| 249 |
public function resetTemplate() { |
| 250 |
try { |
| 251 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; |
| 252 |
if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) { |
| 253 |
wp_send_json_error( array( 'mess' => 'Nonce is invalid' ) ); |
| 254 |
} else { |
| 255 |
if ( isset( $_POST['template'] ) ) { |
| 256 |
// Helper::checkNonce(); |
| 257 |
$reset = sanitize_text_field( $_POST['template'] ); |
| 258 |
$templateEmail = \YayMail\Templates\Templates::getInstance(); |
| 259 |
$templates = $templateEmail::getList(); |
| 260 |
$orderTitle = array( |
| 261 |
'order_title' => '', |
| 262 |
'product_title' => 'Product', |
| 263 |
'quantity_title' => 'Quantity', |
| 264 |
'price_title' => 'Price', |
| 265 |
'subtoltal_title' => 'Subtotal:', |
| 266 |
'payment_method_title' => 'Payment method:', |
| 267 |
'total_title' => 'Total:', |
| 268 |
); |
| 269 |
if ( 'all' == $reset ) { |
| 270 |
foreach ( $templates as $key => $template ) { |
| 271 |
if ( CustomPostType::postIDByTemplate( $key ) ) { |
| 272 |
$postID = CustomPostType::postIDByTemplate( $key ); |
| 273 |
update_post_meta( $postID, '_yaymail_html', $template['html'] ); |
| 274 |
update_post_meta( $postID, '_yaymail_elements', json_decode( $template['elements'], true ) ); |
| 275 |
update_post_meta( $postID, '_email_backgroundColor_settings', 'rgb(236, 236, 236)' ); |
| 276 |
update_post_meta( $postID, '_yaymail_email_textLinkColor_settings', '#96588a' ); |
| 277 |
update_post_meta( $postID, '_email_title_shipping', __( 'Shipping Address', 'yaymail' ) ); |
| 278 |
update_post_meta( $postID, '_email_title_billing', __( 'Billing Address', 'yaymail' ) ); |
| 279 |
update_post_meta( $postID, '_yaymail_email_order_item_title', $orderTitle ); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
if ( get_option( 'yaymail_settings' ) ) { |
| 284 |
$yaymail_settings = get_option( 'yaymail_settings' ); |
| 285 |
$yaymail_settings['container_width'] = '605px'; |
| 286 |
update_option( 'yaymail_settings', $yaymail_settings ); |
| 287 |
} |
| 288 |
|
| 289 |
wp_send_json_success( array( 'mess' => __( 'Template reset successfully.', 'yaymail' ) ) ); |
| 290 |
} else { |
| 291 |
if ( CustomPostType::postIDByTemplate( $reset ) && isset( $templates[ $reset ] ) ) { |
| 292 |
$postID = CustomPostType::postIDByTemplate( $reset ); |
| 293 |
update_post_meta( $postID, '_yaymail_html', $templates[ $reset ]['html'] ); |
| 294 |
update_post_meta( $postID, '_yaymail_elements', json_decode( $templates[ $reset ]['elements'], true ) ); |
| 295 |
update_post_meta( $postID, '_email_backgroundColor_settings', 'rgb(236, 236, 236)' ); |
| 296 |
update_post_meta( $postID, '_yaymail_email_textLinkColor_settings', '#96588a' ); |
| 297 |
update_post_meta( $postID, '_email_title_shipping', __( 'Shipping Address', 'yaymail' ) ); |
| 298 |
update_post_meta( $postID, '_email_title_billing', __( 'Billing Address', 'yaymail' ) ); |
| 299 |
update_post_meta( $postID, '_yaymail_email_order_item_title', $orderTitle ); |
| 300 |
wp_send_json_success( array( 'mess' => __( 'Template reset successfully.', 'yaymail' ) ) ); |
| 301 |
} else { |
| 302 |
wp_send_json_error( array( 'mess' => __( 'Template not Exists!.', 'yaymail' ) ) ); |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
wp_send_json_error( array( 'mess' => __( 'Error Reset Template!', 'yaymail' ) ) ); |
| 307 |
} |
| 308 |
} catch ( \Exception $ex ) { |
| 309 |
LogHelper::getMessageException( $ex, true ); |
| 310 |
} catch ( \Error $ex ) { |
| 311 |
LogHelper::getMessageException( $ex, true ); |
| 312 |
} |
| 313 |
|
| 314 |
} |
| 315 |
|
| 316 |
public function importAllTemplate() { |
| 317 |
try { |
| 318 |
Helper::checkNonce(); |
| 319 |
if ( isset( $_FILES['file']['type'] ) ) { |
| 320 |
if ( 'application/json' == $_FILES['file']['type'] ) { |
| 321 |
if ( ! empty( $_FILES['file']['tmp_name'] ) ) { |
| 322 |
$fileJson = sanitize_text_field( $_FILES['file']['tmp_name'] ); |
| 323 |
$data = file_get_contents( $fileJson ); |
| 324 |
$data = json_decode( $data, true ); |
| 325 |
$dataImports = $data['yaymailTemplateExport']; |
| 326 |
|
| 327 |
$versionOld = $data['yaymail_version']; |
| 328 |
$versionCurrent = YAYMAIL_VERSION; |
| 329 |
|
| 330 |
/* |
| 331 |
check key in settingRow whether or not it exists. |
| 332 |
note: case when add setting row for element. |
| 333 |
*/ |
| 334 |
if ( $versionOld != $versionCurrent ) { |
| 335 |
$element = new DefaultElement(); |
| 336 |
$defaultDataElements = $element->defaultDataElement; |
| 337 |
|
| 338 |
foreach ( $defaultDataElements as $defaultelement ) { |
| 339 |
|
| 340 |
foreach ( $dataImports as $keyTemplate => $templateImport ) { |
| 341 |
foreach ( $templateImport['_yaymail_elements'] as $keyElem => $elemImport ) { |
| 342 |
if ( $defaultelement['type'] == $elemImport['type'] ) { |
| 343 |
/* |
| 344 |
@@@ add key default for element |
| 345 |
*/ |
| 346 |
$keyEleDefaus = array(); |
| 347 |
$keyEleDefaus = array_diff_key( $defaultelement, $elemImport ); |
| 348 |
if ( count( $keyEleDefaus ) > 0 ) { |
| 349 |
$dataImports[ $keyTemplate ]['_yaymail_elements'][ $keyElem ] = array_merge( $elemImport, $keyEleDefaus ); |
| 350 |
} |
| 351 |
|
| 352 |
/* |
| 353 |
add key default for setting row |
| 354 |
note: when add a field in setting row |
| 355 |
*/ |
| 356 |
$propSettings = array(); |
| 357 |
$propSettings = array_diff_key( $defaultelement['settingRow'], $elemImport['settingRow'] ); |
| 358 |
$lenPropSettings = count( $propSettings ); |
| 359 |
if ( $lenPropSettings > 0 ) { |
| 360 |
$result = array(); |
| 361 |
$result = array_merge( $elemImport['settingRow'], $propSettings ); |
| 362 |
$dataImports[ $keyTemplate ]['_yaymail_elements'][ $keyElem ]['settingRow'] = $result; |
| 363 |
} |
| 364 |
|
| 365 |
/* |
| 366 |
remove Key not needed for setting row |
| 367 |
note: when deleting a field in setting row |
| 368 |
*/ |
| 369 |
// $propSetNotNeeds = array(); |
| 370 |
// $propSetNotNeeds = array_diff_key($elemImport['settingRow'], $defaultelement['settingRow']); |
| 371 |
// $lenpropSetNotNeeds = count($propSetNotNeeds); |
| 372 |
// if ($lenpropSetNotNeeds > 0) { |
| 373 |
// foreach ($propSetNotNeeds as $keyNotNeed => $propSetNotNeed) { |
| 374 |
// unset($dataImports[$keyTemplate]['_yaymail_elements'][$keyElem]['settingRow'][$keyNotNeed]); |
| 375 |
// } |
| 376 |
// } |
| 377 |
|
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
$flag = false; |
| 384 |
if ( count( $dataImports ) > 0 ) { |
| 385 |
foreach ( $dataImports as $key => $value ) { |
| 386 |
if ( isset( $value['_yaymail_html'] ) && isset( $value['_yaymail_elements'] ) ) { |
| 387 |
$template = $value['_yaymail_template']; |
| 388 |
if ( CustomPostType::postIDByTemplate( $template ) ) { |
| 389 |
$pID = CustomPostType::postIDByTemplate( $template ); |
| 390 |
update_post_meta( $pID, '_yaymail_html', $value['_yaymail_html'] ); |
| 391 |
update_post_meta( $pID, '_yaymail_elements', $value['_yaymail_elements'] ); |
| 392 |
} else { |
| 393 |
$array = array( |
| 394 |
'mess' => '', |
| 395 |
'post_date' => current_time( 'Y-m-d H:i:s' ), |
| 396 |
'post_type' => 'yaymail_template', |
| 397 |
'post_status' => 'publish', |
| 398 |
'_yaymail_template' => $template, |
| 399 |
'_yaymail_html' => $value['_yaymail_html'], |
| 400 |
'_yaymail_elements' => $value['_yaymail_elements'], |
| 401 |
|
| 402 |
); |
| 403 |
$insert = CustomPostType::insert( $array ); |
| 404 |
} |
| 405 |
$flag = true; |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
if ( ! $flag ) { |
| 410 |
wp_send_json_error( array( 'mess' => __( 'Import Failed.', 'yaymail' ) ) ); |
| 411 |
} |
| 412 |
wp_send_json_success( array( 'mess' => __( 'Imported successfully.', 'yaymail' ) ) ); |
| 413 |
} else { |
| 414 |
wp_send_json_error( array( 'mess' => __( 'File not found.', 'yaymail' ) ) ); |
| 415 |
} |
| 416 |
} else { |
| 417 |
wp_send_json_error( array( 'mess' => __( 'File not correct format.', 'yaymail' ) ) ); |
| 418 |
} |
| 419 |
} |
| 420 |
wp_send_json_error( array( 'mess' => __( 'Please upload 1 file to import.', 'yaymail' ) ) ); |
| 421 |
} catch ( \Exception $ex ) { |
| 422 |
LogHelper::getMessageException( $ex, true ); |
| 423 |
} catch ( \Error $ex ) { |
| 424 |
LogHelper::getMessageException( $ex, true ); |
| 425 |
} |
| 426 |
|
| 427 |
} |
| 428 |
public function enableDisableTempalte() { |
| 429 |
try { |
| 430 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; |
| 431 |
if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) { |
| 432 |
wp_send_json_error( array( 'mess' => 'Nonce is invalid' ) ); |
| 433 |
} else { |
| 434 |
if ( isset( $_POST['settings'] ) ) { |
| 435 |
// Helper::checkNonce(); |
| 436 |
$settingDefault = CustomPostType::templateEnableDisable(); |
| 437 |
$listTemplates = ! empty( $settingDefault ) ? array_keys( $settingDefault ) : array(); |
| 438 |
$settingCurrent = wp_unslash( $_POST['settings'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 439 |
|
| 440 |
isset( $settingCurrent['yaymail_direction'] ) ? update_option( 'yaymail_direction', $settingCurrent['yaymail_direction'] ) : update_option( 'yaymail_direction', 'ltr' ); |
| 441 |
if ( ! empty( $listTemplates ) ) { |
| 442 |
foreach ( $settingCurrent as $key => $value ) { |
| 443 |
if ( in_array( $key, $listTemplates ) ) { |
| 444 |
update_post_meta( $settingDefault[ $key ]['post_id'], '_yaymail_status', $value ); |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
wp_send_json_success( array( 'mess' => __( 'Settings saved.', 'yaymail' ) ) ); |
| 449 |
} |
| 450 |
wp_send_json_error( array( 'mess' => __( 'Settings Failed!.', 'yaymail' ) ) ); |
| 451 |
} |
| 452 |
} catch ( \Exception $ex ) { |
| 453 |
LogHelper::getMessageException( $ex, true ); |
| 454 |
} catch ( \Error $ex ) { |
| 455 |
LogHelper::getMessageException( $ex, true ); |
| 456 |
} |
| 457 |
|
| 458 |
} |
| 459 |
|
| 460 |
public function generalSettings() { |
| 461 |
try { |
| 462 |
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; |
| 463 |
if ( ! wp_verify_nonce( $nonce, 'email-nonce' ) ) { |
| 464 |
wp_send_json_error( array( 'mess' => 'Nonce is invalid' ) ); |
| 465 |
} else { |
| 466 |
if ( isset( $_POST['settings'] ) ) { |
| 467 |
// Helper::checkNonce(); |
| 468 |
update_option( 'yaymail_settings', wp_unslash( $_POST['settings'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 469 |
wp_send_json_success( array( 'mess' => __( 'Settings saved.', 'yaymail' ) ) ); |
| 470 |
} |
| 471 |
wp_send_json_error( array( 'mess' => __( 'Settings Failed!.', 'yaymail' ) ) ); |
| 472 |
} |
| 473 |
} catch ( \Exception $ex ) { |
| 474 |
LogHelper::getMessageException( $ex, true ); |
| 475 |
} catch ( \Error $ex ) { |
| 476 |
LogHelper::getMessageException( $ex, true ); |
| 477 |
} |
| 478 |
|
| 479 |
} |
| 480 |
|
| 481 |
} |
| 482 |
|