| 1 |
<?php |
| 2 |
|
| 3 |
namespace EmailKit\Admin\Api; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
|
| 7 |
class UpdateData |
| 8 |
{ |
| 9 |
|
| 10 |
public $prefix = ''; |
| 11 |
public $param = ''; |
| 12 |
public $request = null; |
| 13 |
|
| 14 |
|
| 15 |
public function __construct() |
| 16 |
{ |
| 17 |
|
| 18 |
add_action('rest_api_init', function () { |
| 19 |
register_rest_route('emailkit/v1/update-data', '(?P<post_id>\d+)', array( |
| 20 |
'methods' => \WP_REST_Server::ALLMETHODS, |
| 21 |
'callback' => [$this, 'update_action'], |
| 22 |
'permission_callback' => '__return_true', |
| 23 |
)); |
| 24 |
}); |
| 25 |
} |
| 26 |
|
| 27 |
public function update_action($request) |
| 28 |
{ |
| 29 |
|
| 30 |
if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) { |
| 31 |
return [ |
| 32 |
'status' => 'fail', |
| 33 |
'message' => [ esc_html__('Nonce mismatch.', 'emailkit') ] |
| 34 |
]; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
if (!is_user_logged_in() || !current_user_can('publish_posts')) { |
| 39 |
return [ |
| 40 |
'status' => 'fail', |
| 41 |
'message' => [ esc_html__('Access denied.', 'emailkit') ] |
| 42 |
]; |
| 43 |
} |
| 44 |
|
| 45 |
$body = $request->get_body(); |
| 46 |
$req = json_decode($body); |
| 47 |
|
| 48 |
$post_id = (int) $request->get_param('post_id'); |
| 49 |
if (!$post_id || !get_post($post_id) || get_post_type($post_id) !== 'emailkit') { |
| 50 |
return [ |
| 51 |
'status' => 'fail', |
| 52 |
'message' => ['Invalid post ID.'] |
| 53 |
]; |
| 54 |
} |
| 55 |
|
| 56 |
$data = array( |
| 57 |
'ID' => $post_id, |
| 58 |
'meta_input' => array( |
| 59 |
'emailkit_template_content_html' => $req->html, |
| 60 |
'emailkit_template_content_object' => $req->object, |
| 61 |
'emailkit_template_status' => trim($request->get_param('emailkit_template_status') ?? 'inactive'), |
| 62 |
'emailkit_email_subject' => $request->get_param('emailkit_email_subject'), |
| 63 |
'emailkit_email_preheader' => $request->get_param('emailkit_email_preheader'), |
| 64 |
|
| 65 |
), |
| 66 |
); |
| 67 |
|
| 68 |
if('active' == trim($request->get_param('emailkit_template_status'))){ |
| 69 |
|
| 70 |
$this->deactivateTemplateTypes(get_post_meta($post_id,'emailkit_template_type', true)); |
| 71 |
} |
| 72 |
|
| 73 |
$post_id = wp_update_post($data); |
| 74 |
|
| 75 |
if (is_wp_error($post_id)) { |
| 76 |
return [ |
| 77 |
"status" => "failed", |
| 78 |
"message" => [ |
| 79 |
esc_html__( 'Post updated successfully.', 'emailkit' ), |
| 80 |
], |
| 81 |
]; |
| 82 |
} else { |
| 83 |
return [ |
| 84 |
|
| 85 |
"status" => "success", |
| 86 |
"data" => [ |
| 87 |
"templateId" => $post_id, |
| 88 |
], |
| 89 |
"message" => [ |
| 90 |
esc_html__( 'Post updated successfully.', 'emailkit' ), |
| 91 |
], |
| 92 |
]; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
public function deactivateTemplateTypes($type) |
| 97 |
{ |
| 98 |
|
| 99 |
$query = array( |
| 100 |
'post_type' => 'emailkit', |
| 101 |
'meta_query' => array( |
| 102 |
array( |
| 103 |
'key' => 'emailkit_template_type', |
| 104 |
'value' => $type, |
| 105 |
'compare' => '=', |
| 106 |
), |
| 107 |
array( |
| 108 |
'key' => 'emailkit_template_status', |
| 109 |
'value' => 'active', |
| 110 |
'compare' => '=', |
| 111 |
), |
| 112 |
'relation' => 'AND', |
| 113 |
'fields' => 'ids' |
| 114 |
) |
| 115 |
); |
| 116 |
|
| 117 |
$data = new \WP_Query($query); |
| 118 |
if (isset($data)) { |
| 119 |
$postsIds = wp_list_pluck($data->posts, 'ID'); |
| 120 |
foreach ($postsIds as $id) { |
| 121 |
update_post_meta($id, 'emailkit_template_status', 'inactive'); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
} |