wpml-tf-backend-bulk-actions.php
3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
use WPML\API\Sanitize;
/**
* Class WPML_TF_Backend_Bulk_Actions
*
* @author OnTheGoSystems
*/
class WPML_TF_Backend_Bulk_Actions {
/** @var WPML_TF_Data_Object_Storage $feedback_storage */
private $feedback_storage;
/** @var WPML_WP_API $wp_api */
private $wp_api;
/** @var WPML_TF_Backend_Notices $backend_notices */
private $backend_notices;
/**
* WPML_TF_Feedback_List_Bulk_Action_Hooks constructor.
*
* @param WPML_TF_Data_Object_Storage $feedback_storage
* @param WPML_WP_API $wp_api
* @param WPML_TF_Backend_Notices $backend_notices
*/
public function __construct(
WPML_TF_Data_Object_Storage $feedback_storage,
WPML_WP_API $wp_api,
WPML_TF_Backend_Notices $backend_notices
) {
$this->feedback_storage = $feedback_storage;
$this->wp_api = $wp_api;
$this->backend_notices = $backend_notices;
}
/**
* Method bulk_action_callback
*/
public function process() {
if ( $this->is_valid_request() && current_user_can( 'manage_options' ) ) {
$feedback_ids = array_map( 'intval', $_GET['feedback_ids'] );
$bulk_action = Sanitize::stringProp( 'bulk_action', $_GET );
$updated_feedback_ids = $feedback_ids;
switch ( $bulk_action ) {
case 'trash':
$this->delete( $feedback_ids );
break;
case 'untrash':
$this->untrash( $feedback_ids );
break;
case 'delete':
$this->delete( $feedback_ids, true );
break;
default:
$updated_feedback_ids = $this->change_status( $feedback_ids, $bulk_action );
break;
}
$this->backend_notices->add_bulk_updated_notice( $updated_feedback_ids, $bulk_action );
$this->redirect();
} else {
$this->backend_notices->remove_bulk_updated_notice_after_display();
}
}
private function change_status( array $feedback_ids, $new_status ) {
$updated_feedback_ids = array();
foreach ( $feedback_ids as $feedback_id ) {
$feedback = $this->feedback_storage->get( $feedback_id );
if ( $feedback ) {
/** @var WPML_TF_Feedback $feedback */
$feedback->set_status( $new_status );
$updated_feedback_ids[] = $this->feedback_storage->persist( $feedback );
}
}
return $updated_feedback_ids;
}
private function delete( array $feedback_ids, $force_delete = false ) {
foreach ( $feedback_ids as $feedback_id ) {
$this->feedback_storage->delete( $feedback_id, $force_delete );
}
}
private function untrash( array $feedback_ids ) {
foreach ( $feedback_ids as $feedback_id ) {
$this->feedback_storage->untrash( $feedback_id );
}
}
/**
* @return bool
*/
private function is_valid_request() {
$is_valid = false;
if ( isset( $_GET['bulk_action'], $_GET['bulk_action2'] ) ) {
if ( '-1' === $_GET['bulk_action'] ) {
$_GET['bulk_action'] = $_GET['bulk_action2'];
}
if ( isset( $_GET['nonce'], $_GET['feedback_ids'] ) ) {
$is_valid = $this->is_valid_action( $_GET['bulk_action'] )
&& wp_verify_nonce( $_GET['nonce'], WPML_TF_Backend_Hooks::PAGE_HOOK );
}
}
return $is_valid;
}
private function is_valid_action( $action ) {
return in_array( $action, array( 'pending', 'fixed', 'trash', 'untrash', 'delete' ), true );
}
/**
* Redirect after processing the bulk action
*/
private function redirect() {
$args_to_remove = array( 'feedback_ids', 'bulk_action', 'bulk_action2' );
$url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
$url = remove_query_arg( $args_to_remove, $url );
$this->wp_api->wp_safe_redirect( $url );
}
}