1 | <?php |
||
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() { |
||
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 ) { |
||
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 ) { |
||
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() { |
||
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 ) { |
||
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 ) { |
||
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: