1
|
|
|
<?php |
2
|
|
|
namespace testContent; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class to build test data for custom post types. |
6
|
|
|
* |
7
|
|
|
* @package WordPress |
8
|
|
|
* @subpackage Evans |
9
|
|
|
* @author Old Town Media |
10
|
|
|
*/ |
11
|
|
|
class AdminPage{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* plugin |
15
|
|
|
* Access to plugin definitions. |
16
|
|
|
* |
17
|
|
|
* @var Plugin |
18
|
|
|
* @access private |
19
|
|
|
*/ |
20
|
|
|
private $plugin; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* definitions |
24
|
|
|
* Easy way to access all of our defined paths & info. |
25
|
|
|
* |
26
|
|
|
* @var object |
27
|
|
|
* @access private |
28
|
|
|
*/ |
29
|
|
|
private $definitions; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* connected |
33
|
|
|
* Whether or not we're successfully connected to the Internet. |
34
|
|
|
* |
35
|
|
|
* @var boolean |
36
|
|
|
* @access private |
37
|
|
|
*/ |
38
|
|
|
private $connected; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Hooks function. |
43
|
|
|
* |
44
|
|
|
* This function is used to avoid loading any unnecessary functions/code. |
45
|
|
|
* |
46
|
|
|
* @see admin_menu, wp_ajax actions |
47
|
|
|
*/ |
48
|
|
|
public function hooks(){ |
49
|
|
|
|
50
|
|
|
$connection = new ConnectionTest; |
51
|
|
|
$this->definitions = $this->plugin->get_definitions(); |
52
|
|
|
$this->connected = $connection->test(); |
53
|
|
|
|
54
|
|
|
add_action( 'admin_menu' , array( $this, 'add_menu_item' ) ); |
55
|
|
|
add_action( 'wp_ajax_handle_test_data', array( $this, 'handle_test_data_callback' ) ); |
56
|
|
|
add_filter( 'plugin_action_links_' . $this->definitions->basename , array( $this, 'add_settings_link' ) ); |
57
|
|
|
add_action( 'admin_notices', array( $this, 'internet_connected_admin_notice' ) ); |
58
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set a reference to the main plugin instance. |
64
|
|
|
* |
65
|
|
|
* @param Plugin $plugin Main plugin instance. |
66
|
|
|
*/ |
67
|
|
|
public function set_plugin( $plugin ) { |
68
|
|
|
|
69
|
|
|
$this->plugin = $plugin; |
70
|
|
|
return $this; |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Add the admin-side menu item for creating & deleting test data. |
77
|
|
|
* |
78
|
|
|
* @see add_submenu_page |
79
|
|
|
*/ |
80
|
|
|
public function add_menu_item() { |
81
|
|
|
|
82
|
|
|
$page = add_submenu_page( |
83
|
|
|
'tools.php', |
84
|
|
|
__( 'Create Test Content', 'otm-test-content' ), |
85
|
|
|
__( 'Test Content', 'otm-test-content' ), |
86
|
|
|
'manage_options', |
87
|
|
|
'create-test-data', |
88
|
|
|
array( $this, 'admin_page' ) |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
add_action( 'admin_print_styles-' . $page, array( $this, 'load_scripts' ) ); |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Add 'build test content' link to plugin list table. |
97
|
|
|
* |
98
|
|
|
* @param array $links Existing links |
99
|
|
|
* @return array Modified links |
100
|
|
|
*/ |
101
|
|
|
public function add_settings_link( $links ) { |
102
|
|
|
|
103
|
|
|
$settings_link = '<a href="tools.php?page=create-test-data">' . __( 'Build Test Content', 'otm-test-content' ) . '</a>'; |
104
|
|
|
array_push( $links, $settings_link ); |
105
|
|
|
return $links; |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Admin notice to notify a user that images won't work on test dat. |
112
|
|
|
* |
113
|
|
|
* Adds an admin notice that first checks if a user is connected to the |
114
|
|
|
* Internet, and the test fails displays a notice informing the user that |
115
|
|
|
* images will not pull into test data. |
116
|
|
|
*/ |
117
|
|
|
public function internet_connected_admin_notice(){ |
118
|
|
|
|
119
|
|
|
// Get the current admin screen & verify that we're on the right one |
120
|
|
|
// before continuing. |
121
|
|
|
$screen = get_current_screen(); |
122
|
|
|
|
123
|
|
|
if ( $screen->base != 'tools_page_create-test-data' ){ |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// Check the response |
128
|
|
|
if ( $this->connected ) { |
129
|
|
|
// We got a response so early return |
130
|
|
|
return; |
131
|
|
|
} else { |
132
|
|
|
// We didn't get a reponse so print the notice out |
133
|
|
|
echo '<div class="notice notice-error">'; |
134
|
|
|
echo '<p>'.__( 'WordPress could not connect to Splashbase and therefore images will not pull into metaboxes/thumbnails. Turn Airplane Mode off or reconnect to the Internet to get images when creating test data.', 'otm-test-content' ).'</p>'; |
135
|
|
|
echo '</div>'; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Load our script in the admin section and serve in data. |
143
|
|
|
*/ |
144
|
|
|
public function load_scripts(){ |
145
|
|
|
|
146
|
|
|
wp_enqueue_script( 'test-content-js', plugins_url( 'assets/admin.js' , dirname( __FILE__ ) ) ); |
147
|
|
|
wp_enqueue_style( 'test-content-css', plugins_url( 'assets/admin.css' , dirname( __FILE__ ) ) ); |
148
|
|
|
|
149
|
|
|
$data = array( |
150
|
|
|
'nonce' => wp_create_nonce( 'handle-test-data' ), |
151
|
|
|
'createdStr' => __( 'Created', 'otm-test-content' ), |
152
|
|
|
'deletedStr' => __( 'Deleting', 'otm-test-content' ), |
153
|
|
|
'creatingStr' => __( 'Creating', 'otm-test-content' ), |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
wp_localize_script( 'test-content-js', 'test_content', $data ); |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Ajax callback function for triggering the creation & deletion of test data. |
163
|
|
|
* |
164
|
|
|
* @see wp_ajax filter, $this->add_menu_item, $this->creation_routing |
165
|
|
|
*/ |
166
|
|
|
public function handle_test_data_callback() { |
167
|
|
|
|
168
|
|
|
$action = $_REQUEST['todo']; |
169
|
|
|
$nonce = $_REQUEST['nonce']; |
170
|
|
|
|
171
|
|
|
// Verify that we have a proper logged in user and it's the right person |
172
|
|
|
if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'handle-test-data' ) ){ |
173
|
|
|
return; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if ( $action == 'delete' ){ |
177
|
|
|
|
178
|
|
|
$this->deletion_routing( $_REQUEST ); |
179
|
|
|
|
180
|
|
|
} elseif ( $action == 'create' ){ |
181
|
|
|
|
182
|
|
|
$this->creation_routing( $_REQUEST ); |
183
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
die(); |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Choose which type of creation needs to be accomplished and route through |
193
|
|
|
* the correct class. |
194
|
|
|
*/ |
195
|
|
|
private function creation_routing( $data ){ |
196
|
|
|
|
197
|
|
|
if ( $data['type'] == 'post' ){ |
198
|
|
|
|
199
|
|
|
$create_content = new CreatePost; |
200
|
|
|
$create_content->create_post_type_content( $data['slug'], $data['connection'], true, 1 ); |
201
|
|
|
|
202
|
|
|
} elseif( $data['type'] == 'term' ){ |
203
|
|
|
|
204
|
|
|
$create_content = new CreateTerm; |
205
|
|
|
$create_content->create_terms( $data['slug'], true, 1 ); |
206
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Choose which type of deletion needs to be accomplished and route through |
214
|
|
|
* the correct method of Delete. |
215
|
|
|
*/ |
216
|
|
|
private function deletion_routing( $data ){ |
217
|
|
|
|
218
|
|
|
$delete_content = new Delete; |
219
|
|
|
|
220
|
|
|
if ( $data['type'] == 'post' ){ |
221
|
|
|
|
222
|
|
|
$delete_content->delete_posts( $data['slug'], true ); |
223
|
|
|
|
224
|
|
|
} elseif ( $data['type'] == 'term' ){ |
225
|
|
|
|
226
|
|
|
$delete_content->delete_terms( $data['slug'], true ); |
227
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Print out our admin page to control test data. |
235
|
|
|
*/ |
236
|
|
|
public function admin_page(){ |
237
|
|
|
|
238
|
|
|
$html = ""; |
239
|
|
|
|
240
|
|
|
$html .= '<div class="wrap" id="options_editor">' . "\n"; |
241
|
|
|
|
242
|
|
|
$html .= '<h2>' . __( 'Create Test Data' , 'otm-test-content' ) . '</h2>' . "\n"; |
243
|
|
|
|
244
|
|
|
$html .= "<div>"; |
245
|
|
|
|
246
|
|
|
$html .= "<div class='test-data-cpt'>"; |
247
|
|
|
$html .= "<h3>"; |
248
|
|
|
$html .= "<span class='label'>".__( 'Quantity', 'otm-test-content' )."</span>"; |
249
|
|
|
$html .= "<input type='number' value='0' id='quantity-adjustment'> <small><i>".__( 'Set to 0 to keep random', 'otm-test-content' )."</i></small>"; |
250
|
|
|
$html .= "</h3>"; |
251
|
|
|
$html .= "</div>"; |
252
|
|
|
|
253
|
|
|
$html .= "<input type='hidden' id='connection-status' value='".$this->connected."'>"; |
254
|
|
|
|
255
|
|
|
// Loop through every post type available on the site |
256
|
|
|
$post_types = get_post_types( array( 'public' => true ), 'objects' ); |
257
|
|
|
|
258
|
|
|
foreach ( $post_types as $post_type ) { |
259
|
|
|
|
260
|
|
|
$skipped_cpts = array( |
261
|
|
|
'attachment' |
262
|
|
|
); |
263
|
|
|
|
264
|
|
|
// Skip banned cpts |
265
|
|
|
if ( in_array( $post_type->name, $skipped_cpts ) ){ |
266
|
|
|
continue; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$html .= "<div class='test-data-cpt'>"; |
270
|
|
|
|
271
|
|
|
// Create row for the post/page/cpt |
272
|
|
|
$html .= "<h3>"; |
273
|
|
|
|
274
|
|
|
$html .= "<span class='label'>" . $post_type->labels->name . "</span>"; |
275
|
|
|
$html .= " <a href='javascript:void(0);' data-type='post' data-slug='".$post_type->name."' data-todo='create' class='button-primary handle-test-data' /><span class='dashicons dashicons-plus'></span> ".__( 'Create Test Data', 'otm-test-content' )."</a>"; |
276
|
|
|
$html .= " <a href='javascript:void(0);' data-type='post' data-slug='".$post_type->name."' data-todo='delete' class='button-primary handle-test-data' /><span class='dashicons dashicons-trash'></span> ".__( 'Delete Test Data', 'otm-test-content' )."</a>"; |
277
|
|
|
|
278
|
|
|
$html .= "</h3>"; |
279
|
|
|
|
280
|
|
|
// Create row for each taxonomy associated with the post/page/cpt |
281
|
|
|
$taxonomies = get_object_taxonomies( $post_type->name ); |
282
|
|
|
|
283
|
|
|
if ( !empty( $taxonomies ) ){ |
284
|
|
|
|
285
|
|
|
foreach( $taxonomies as $tax ){ |
286
|
|
|
|
287
|
|
|
$html .= "<h3 class='term-box'>"; |
288
|
|
|
|
289
|
|
|
$skipped_taxonomies = array( |
290
|
|
|
'post_format', // We shouldn't be making random post format classes |
291
|
|
|
'product_shipping_class' // These aren't used visually and are therefore skipped |
292
|
|
|
); |
293
|
|
|
|
294
|
|
|
// Skip banned taxonomies |
295
|
|
|
if ( in_array( $tax, $skipped_taxonomies ) ){ |
296
|
|
|
continue; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
$taxonomy = get_taxonomy( $tax ); |
300
|
|
|
|
301
|
|
|
$html .= "<span class='label'>".$taxonomy->labels->name."</span>"; |
302
|
|
|
|
303
|
|
|
$html .= " <a href='javascript:void(0);' data-type='term' data-slug='".$tax."' data-todo='create' class='button-primary handle-test-data' /><span class='dashicons dashicons-category'></span> ".__( 'Create', 'otm-test-content' )." ".$taxonomy->labels->name."</a>"; |
304
|
|
|
|
305
|
|
|
$html .= " <a href='javascript:void(0);' data-type='term' data-slug='".$tax."' data-todo='delete' class='button-primary handle-test-data' /><span class='dashicons dashicons-trash'></span> ".__( 'Delete', 'otm-test-content' )." ".$taxonomy->labels->name."</a>"; |
306
|
|
|
|
307
|
|
|
$html .= "</h3>"; |
308
|
|
|
|
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
$html .= "</div>"; |
313
|
|
|
|
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
$html .= "<pre class='test-data-status-box' id='status-updates'></pre>"; |
317
|
|
|
|
318
|
|
|
$html .= "</div>"; |
319
|
|
|
|
320
|
|
|
echo $html; |
321
|
|
|
|
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
} |
325
|
|
|
|