1
|
|
|
<?php |
2
|
|
|
namespace DummyPress; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Handling Ajax return and data |
6
|
|
|
* |
7
|
|
|
* @package WordPress |
8
|
|
|
* @subpackage Evans |
9
|
|
|
* @author Mike Selander |
10
|
|
|
*/ |
11
|
|
|
class Ajax { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* reporting |
15
|
|
|
* Reporting class instance. |
16
|
|
|
* |
17
|
|
|
* @var object |
18
|
|
|
* @access private |
19
|
|
|
*/ |
20
|
|
|
private $reporting; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* plugin |
24
|
|
|
* Plugin class instance. |
25
|
|
|
* |
26
|
|
|
* @var object |
27
|
|
|
*/ |
28
|
|
|
private $plugin; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* action |
32
|
|
|
* Name of the action we want to use in our AJAX calls |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $action; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Instantiate any WP hooks that need to be fired. |
41
|
|
|
*/ |
42
|
|
|
public function hooks() { |
43
|
|
|
|
44
|
|
|
$this->reporting = new Reporting; |
45
|
|
|
$this->action = 'handle_test_data'; |
46
|
|
|
|
47
|
|
|
add_action( "wp_ajax_{$this->action}", array( $this, 'handle_ajax' ) ); |
48
|
|
|
add_filter( 'option_active_plugins', array( $this, 'ajax_exclude_plugins' ) ); |
49
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Set a reference to the main plugin instance. |
55
|
|
|
* |
56
|
|
|
* @param $plugin Plugin instance. |
57
|
|
|
* @return Ajax instance |
58
|
|
|
*/ |
59
|
|
|
public function set_plugin( $plugin ) { |
60
|
|
|
|
61
|
|
|
$this->plugin = $plugin; |
62
|
|
|
return $this; |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Turn outside plugins off during our AJAX calls to speed everything up. |
69
|
|
|
* |
70
|
|
|
* Having a lot of plugins running slows down an AJAX request, this function |
71
|
|
|
* turns all other plugins off temporarliy while the AJAX requests is running. |
72
|
|
|
* |
73
|
|
|
* https://deliciousbrains.com/excluding-wordpress-plugins-loading-specific-ajax-requests/ |
74
|
|
|
* |
75
|
|
|
* @param array $plugins All active plugins. |
76
|
|
|
* @return array Whitelisted plugins. |
77
|
|
|
*/ |
78
|
|
|
public function ajax_exclude_plugins( $plugins ) { |
|
|
|
|
79
|
|
|
|
80
|
|
|
if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX || ! isset( $_POST['action'] ) || false === strpos( $_POST['action'], $this->action ) ) { |
81
|
|
|
return $plugins; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
foreach( $plugins as $key => $plugin ) { |
85
|
|
|
|
86
|
|
|
if ( false !== strpos( $plugin, $this->plugin->definitions->slug ) ) { |
87
|
|
|
continue; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
unset( $plugins[$key] ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $plugins; |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Ajax callback function for triggering the creation & deletion of test data. |
100
|
|
|
* |
101
|
|
|
* @see wp_ajax filter, $this->add_menu_item, $this->creation_routing |
102
|
|
|
*/ |
103
|
|
|
public function handle_ajax() { |
|
|
|
|
104
|
|
|
|
105
|
|
|
$action = $_REQUEST['todo']; |
106
|
|
|
$nonce = $_REQUEST['nonce']; |
107
|
|
|
|
108
|
|
|
// Verify that we have a proper logged in user and it's the right person |
109
|
|
|
if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'handle-test-data' ) ) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ( $action == 'delete' ) { |
114
|
|
|
|
115
|
|
|
$this->deletion_routing( $_REQUEST ); |
116
|
|
|
|
117
|
|
|
} elseif ( $action == 'create' ) { |
118
|
|
|
|
119
|
|
|
$this->creation_routing( $_REQUEST ); |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
die(); |
|
|
|
|
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Choose which type of creation needs to be accomplished and route through |
130
|
|
|
* the correct class. |
131
|
|
|
*/ |
132
|
|
|
private function creation_routing( $data ) { |
133
|
|
|
|
134
|
|
|
$type = 'DummyPress\Types\\' . ucwords( $data['type'] ); |
135
|
|
|
$object = new $type(); |
136
|
|
|
$return = $object->create_objects( $data['slug'], $data['connection'], true, 1 ); |
137
|
|
|
|
138
|
|
|
$clean = $this->reporting->create_report( $return ); |
139
|
|
|
|
140
|
|
|
echo $clean; |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Choose which type of deletion needs to be accomplished and route through |
147
|
|
|
* the correct method of Delete. |
148
|
|
|
*/ |
149
|
|
|
private function deletion_routing( $data ) { |
150
|
|
|
|
151
|
|
|
$delete_content = new Delete; |
152
|
|
|
|
153
|
|
|
if ( $data['type'] == 'all' ) { |
154
|
|
|
|
155
|
|
|
$return = $delete_content->delete_all_test_data(); |
156
|
|
|
|
157
|
|
|
} else { |
158
|
|
|
|
159
|
|
|
$return = $delete_content->delete_objects( $data ); |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$clean = $this->reporting->create_report( $return ); |
164
|
|
|
|
165
|
|
|
echo $clean; |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: