PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.5.5
EmailKit – Email Customizer for WooCommerce & WP v1.5.5
1.6.9 1.6.8 1.6.7 trunk 1.0.0 1.2.0 1.4.0 1.4.5 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6
emailkit / includes / Admin / Api / TemplateStatus.php

TemplateStatus.php in EmailKit – Email Customizer for WooCommerce & WP 1.5.5, at includes/Admin/Api/TemplateStatus.php

108 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace EmailKit\Admin\Api;
4
5 defined('ABSPATH') || exit;
6
7 class TemplateStatus {
8
9 public function __construct()
10 {
11 add_action('rest_api_init', function () {
12 register_rest_route('emailkit/v1', 'template-status', array(
13 'methods' => 'POST',
14 'callback' => [$this, 'changeStatus'],
15 'permission_callback' => '__return_true',
16 ));
17 });
18 }
19
20 public function changeStatus($request) {
21
22 if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) {
23 return [
24 'status' => 'fail',
25 'message' => [ __( 'Nonce mismatch.', 'emailkit' ) ]
26 ];
27 }
28
29 if (!is_user_logged_in() || !current_user_can('publish_posts')) {
30 return [
31 'status' => 'fail',
32 'message' => [ __( 'Permission denied.', 'emailkit' ) ]
33 ];
34 }
35
36
37 $post_id = absint($request->get_param('templateId'));
38
39 $old_status = sanitize_text_field(get_post_meta($post_id, 'emailkit_template_status', true));
40
41 if ( empty($post_id) ) {
42 return [
43 'status' => 'fail',
44 'message' => [ __( 'Invalid parameters.','emailkit' ) ]
45 ];
46 }
47
48
49 $new_status = $old_status == 'active' ? 'inactive' : 'active';
50 $template_type = get_post_meta($post_id, 'emailkit_template_type', true);
51
52
53 $data = array(
54 'ID' => $post_id,
55 'post_type' => 'emailkit',
56 'post_status' => 'publish',
57 'meta_input' => array(
58 'emailkit_template_status' => $new_status,
59 )
60 );
61
62
63 if('active' == $new_status){
64
65 $this->deactivateTemplateTypes($template_type);
66 }
67 // Update the post status in the database
68 wp_update_post($data);
69
70
71 return [
72 'status' => 'success',
73 'message' => [ __( 'Template status updated successfully.', 'emailkit' ) ],
74 'status_text' => ucfirst($new_status),
75 'template_type' => str_replace(' ', '-', strtolower($template_type))
76 ];
77 }
78
79 public function deactivateTemplateTypes($type)
80 {
81
82 $query = array(
83 'post_type' => 'emailkit',
84 'meta_query' => array(
85 array(
86 'key' => 'emailkit_template_type',
87 'value' => $type,
88 'compare' => '=',
89 ),
90 array(
91 'key' => 'emailkit_template_status',
92 'value' => 'active',
93 'compare' => '=',
94 ),
95 'relation' => 'AND',
96 'fields' => 'ids'
97 )
98 );
99
100 $data = new \WP_Query($query);
101 if (isset($data)) {
102 $postsIds = wp_list_pluck($data->posts, 'ID');
103 foreach ($postsIds as $id) {
104 update_post_meta($id, 'emailkit_template_status', 'inactive');
105 }
106 }
107 }
108 }