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
|
|
|
/** |
33
|
|
|
* Hooks function. |
34
|
|
|
* |
35
|
|
|
* This function is used to avoid loading any unnecessary functions/code. |
36
|
|
|
* |
37
|
|
|
* @see admin_menu, wp_ajax actions |
38
|
|
|
*/ |
39
|
|
|
public function hooks(){ |
40
|
|
|
|
41
|
|
|
$this->definitions = $this->plugin->get_definitions(); |
42
|
|
|
|
43
|
|
|
add_action( 'admin_menu' , array( $this, 'add_menu_item' ) ); |
44
|
|
|
add_action( 'wp_ajax_handle_test_data', array( $this, 'handle_test_data_callback' ) ); |
45
|
|
|
add_filter( 'plugin_action_links_' . $this->definitions->basename , array( $this, 'add_settings_link' ) ); |
46
|
|
|
add_action( 'admin_notices', array( $this, 'internet_connected_admin_notice' ) ); |
47
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set a reference to the main plugin instance. |
53
|
|
|
* |
54
|
|
|
* @param Plugin $plugin Main plugin instance. |
55
|
|
|
*/ |
56
|
|
|
public function set_plugin( $plugin ) { |
57
|
|
|
|
58
|
|
|
$this->plugin = $plugin; |
59
|
|
|
return $this; |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Add the admin-side menu item for creating & deleting test data. |
66
|
|
|
* |
67
|
|
|
* @see add_submenu_page |
68
|
|
|
*/ |
69
|
|
|
public function add_menu_item() { |
70
|
|
|
|
71
|
|
|
$page = add_submenu_page( |
72
|
|
|
'tools.php', |
73
|
|
|
__( 'Create Test Content', 'otm-test-content' ), |
74
|
|
|
__( 'Test Content', 'otm-test-content' ), |
75
|
|
|
'manage_options', |
76
|
|
|
'create-test-data', |
77
|
|
|
array( $this, 'admin_page' ) |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
add_action( 'admin_print_styles-' . $page, array( $this, 'load_scripts' ) ); |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Add 'build test content' link to plugin list table. |
86
|
|
|
* |
87
|
|
|
* @param array $links Existing links |
88
|
|
|
* @return array Modified links |
89
|
|
|
*/ |
90
|
|
|
public function add_settings_link( $links ) { |
91
|
|
|
|
92
|
|
|
$settings_link = '<a href="tools.php?page=create-test-data">' . __( 'Build Test Content', 'otm-test-content' ) . '</a>'; |
93
|
|
|
array_push( $links, $settings_link ); |
94
|
|
|
return $links; |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Admin notice to notify a user that images won't work on test dat. |
101
|
|
|
* |
102
|
|
|
* Adds an admin notice that first checks if a user is connected to the |
103
|
|
|
* Internet, and the test fails displays a notice informing the user that |
104
|
|
|
* images will not pull into test data. |
105
|
|
|
*/ |
106
|
|
|
public function internet_connected_admin_notice(){ |
107
|
|
|
|
108
|
|
|
// Get the current admin screen & verify that we're on the right one |
109
|
|
|
// before continuing. |
110
|
|
|
$screen = get_current_screen(); |
111
|
|
|
|
112
|
|
|
if ( $screen->base != 'tools_page_create-test-data' ){ |
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Check the response |
117
|
|
|
if ( $this->test_splashbase_api() ) { |
118
|
|
|
// We got a response so early return |
119
|
|
|
return; |
120
|
|
|
} else { |
121
|
|
|
// We didn't get a reponse so print the notice out |
122
|
|
|
echo '<div class="notice notice-error is-dismissible">'; |
123
|
|
|
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>'; |
124
|
|
|
echo '</div>'; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* A more intelligent check to see if we can connect to Splashbase or not. |
132
|
|
|
* |
133
|
|
|
* This function checks whether or not we can connect to the Internet, and |
134
|
|
|
* if we can, whether we can connect to Splashbase itself. This is used by |
135
|
|
|
* our admin notice function to check whether or not we should display a notice |
136
|
|
|
* to users warning them of issues with Splashbase. |
137
|
|
|
* |
138
|
|
|
* The purpose of this is to avoid useless bug-hunting when images don't work. |
139
|
|
|
* |
140
|
|
|
* @access private |
141
|
|
|
* |
142
|
|
|
* @see fsockopen, get_site_option, wp_remote_get |
143
|
|
|
* |
144
|
|
|
* @return boolean Status of connection to Splashbase. |
145
|
|
|
*/ |
146
|
|
|
private function test_splashbase_api(){ |
147
|
|
|
|
148
|
|
|
/* |
149
|
|
|
* Test #1 - Check for Airplane Mode plugin status |
150
|
|
|
*/ |
151
|
|
|
if ( class_exists( 'Airplane_Mode_Core' ) ){ |
152
|
|
|
// Is airplane mode active? |
153
|
|
|
$airplane_mode = get_site_option( 'airplane-mode' ); |
154
|
|
|
|
155
|
|
|
if ( $airplane_mode === 'on' ){ |
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/* |
161
|
|
|
* Test #2 - Check Internet connection in general |
162
|
|
|
*/ |
163
|
|
|
// Attempt to open a socket connection to Google |
164
|
|
|
$connected = @fsockopen( "www.google.com", 80 ); |
165
|
|
|
|
166
|
|
|
if ( !$connected ){ |
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// Close out our 1st test |
171
|
|
|
fclose( $connected ); |
172
|
|
|
|
173
|
|
|
/* |
174
|
|
|
* Test #3 - Check Splashbase itself |
175
|
|
|
*/ |
176
|
|
|
$test_url = 'http://www.splashbase.co/api/v1/images/'; |
177
|
|
|
$response = wp_remote_get( $test_url ); |
178
|
|
|
|
179
|
|
|
if ( !is_array( $response ) ){ |
180
|
|
|
return false; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// We've made it this far, looks like everything checks out OK! |
184
|
|
|
return true; |
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Load our script in the admin section and serve in data. |
191
|
|
|
*/ |
192
|
|
|
public function load_scripts(){ |
193
|
|
|
|
194
|
|
|
wp_enqueue_script( 'test-content-js', plugins_url( 'assets/admin.js' , dirname( __FILE__ ) ) ); |
195
|
|
|
wp_enqueue_style( 'test-content-css', plugins_url( 'assets/admin.css' , dirname( __FILE__ ) ) ); |
196
|
|
|
|
197
|
|
|
$data = array( |
198
|
|
|
'nonce' => wp_create_nonce( 'handle-test-data' ), |
199
|
|
|
'createdStr' => __( 'Created', 'otm-test-content' ), |
200
|
|
|
'deletedStr' => __( 'Deleting', 'otm-test-content' ), |
201
|
|
|
'creatingStr' => __( 'Creating', 'otm-test-content' ), |
202
|
|
|
); |
203
|
|
|
|
204
|
|
|
wp_localize_script( 'test-content-js', 'test_content', $data ); |
205
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Ajax callback function for triggering the creation & deletion of test data. |
211
|
|
|
* |
212
|
|
|
* @see wp_ajax filter, $this->add_menu_item, $this->creation_routing |
213
|
|
|
*/ |
214
|
|
|
public function handle_test_data_callback() { |
215
|
|
|
|
216
|
|
|
$action = $_REQUEST['todo']; |
217
|
|
|
$nonce = $_REQUEST['nonce']; |
218
|
|
|
|
219
|
|
|
// Verify that we have a proper logged in user and it's the right person |
220
|
|
|
if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'handle-test-data' ) ){ |
221
|
|
|
return; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
if ( $action == 'delete' ){ |
225
|
|
|
|
226
|
|
|
$this->deletion_routing( $_REQUEST ); |
227
|
|
|
|
228
|
|
|
} elseif ( $action == 'create' ){ |
229
|
|
|
|
230
|
|
|
$this->creation_routing( $_REQUEST ); |
231
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
die(); |
235
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Choose which type of creation needs to be accomplished and route through |
241
|
|
|
* the correct class. |
242
|
|
|
*/ |
243
|
|
|
private function creation_routing( $data ){ |
244
|
|
|
|
245
|
|
|
if ( $data['type'] == 'post' ){ |
246
|
|
|
|
247
|
|
|
$create_content = new CreatePost; |
248
|
|
|
$create_content->create_post_type_content( $data['slug'], true, 1 ); |
249
|
|
|
|
250
|
|
|
} elseif( $data['type'] == 'term' ){ |
251
|
|
|
|
252
|
|
|
$create_content = new CreateTerm; |
253
|
|
|
$create_content->create_terms( $data['slug'], true, 1 ); |
254
|
|
|
|
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Choose which type of deletion needs to be accomplished and route through |
262
|
|
|
* the correct method of Delete. |
263
|
|
|
*/ |
264
|
|
|
private function deletion_routing( $data ){ |
265
|
|
|
|
266
|
|
|
$delete_content = new Delete; |
267
|
|
|
|
268
|
|
|
if ( $data['type'] == 'post' ){ |
269
|
|
|
|
270
|
|
|
$delete_content->delete_posts( $data['slug'], true ); |
271
|
|
|
|
272
|
|
|
} elseif ( $data['type'] == 'term' ){ |
273
|
|
|
|
274
|
|
|
$delete_content->delete_terms( $data['slug'], true ); |
275
|
|
|
|
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Print out our admin page to control test data. |
283
|
|
|
*/ |
284
|
|
|
public function admin_page(){ |
285
|
|
|
|
286
|
|
|
$html = ""; |
287
|
|
|
|
288
|
|
|
$html .= '<div class="wrap" id="options_editor">' . "\n"; |
289
|
|
|
|
290
|
|
|
$html .= '<h2>' . __( 'Create Test Data' , 'otm-test-content' ) . '</h2>' . "\n"; |
291
|
|
|
|
292
|
|
|
// Loop through all other cpts |
293
|
|
|
$post_types = get_post_types( array( 'public' => true ), 'objects' ); |
294
|
|
|
|
295
|
|
|
$html .= "<div>"; |
296
|
|
|
|
297
|
|
|
$html .= "<div class='test-data-cpt'>"; |
298
|
|
|
$html .= "<h3>"; |
299
|
|
|
$html .= "<span class='label'>".__( 'Quantity', 'otm-test-content' )."</span>"; |
300
|
|
|
$html .= "<input type='number' value='0' id='quantity-adjustment'> <small><i>".__( 'Set to 0 to keep random', 'otm-test-content' )."</i></small>"; |
301
|
|
|
$html .= "</h3>"; |
302
|
|
|
$html .= "</div>"; |
303
|
|
|
|
304
|
|
|
// Loop through every post type available on the site |
305
|
|
|
foreach ( $post_types as $post_type ) { |
306
|
|
|
|
307
|
|
|
$skipped_cpts = array( |
308
|
|
|
'attachment' |
309
|
|
|
); |
310
|
|
|
|
311
|
|
|
// Skip banned cpts |
312
|
|
|
if ( in_array( $post_type->name, $skipped_cpts ) ){ |
313
|
|
|
continue; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
$html .= "<div class='test-data-cpt'>"; |
317
|
|
|
|
318
|
|
|
// Create row for the post/page/cpt |
319
|
|
|
$html .= "<h3>"; |
320
|
|
|
|
321
|
|
|
$html .= "<span class='label'>" . $post_type->labels->name . "</span>"; |
322
|
|
|
$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>"; |
323
|
|
|
$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>"; |
324
|
|
|
|
325
|
|
|
$html .= "</h3>"; |
326
|
|
|
|
327
|
|
|
// Create row for each taxonomy associated with the post/page/cpt |
328
|
|
|
$taxonomies = get_object_taxonomies( $post_type->name ); |
329
|
|
|
|
330
|
|
|
if ( !empty( $taxonomies ) ){ |
331
|
|
|
|
332
|
|
|
foreach( $taxonomies as $tax ){ |
333
|
|
|
|
334
|
|
|
$html .= "<h3 class='term-box'>"; |
335
|
|
|
|
336
|
|
|
$skipped_taxonomies = array( |
337
|
|
|
'post_format', // We shouldn't be making random post format classes |
338
|
|
|
'product_shipping_class' // These aren't used visually and are therefore skipped |
339
|
|
|
); |
340
|
|
|
|
341
|
|
|
// Skip banned taxonomies |
342
|
|
|
if ( in_array( $tax, $skipped_taxonomies ) ){ |
343
|
|
|
continue; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
$taxonomy = get_taxonomy( $tax ); |
347
|
|
|
|
348
|
|
|
$html .= "<span class='label'>".$taxonomy->labels->name."</span>"; |
349
|
|
|
|
350
|
|
|
$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>"; |
351
|
|
|
|
352
|
|
|
$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>"; |
353
|
|
|
|
354
|
|
|
$html .= "</h3>"; |
355
|
|
|
|
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
$html .= "</div>"; |
360
|
|
|
|
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
$html .= "<pre class='test-data-status-box' id='status-updates'></pre>"; |
364
|
|
|
|
365
|
|
|
$html .= "</div>"; |
366
|
|
|
|
367
|
|
|
echo $html; |
368
|
|
|
|
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
} |
372
|
|
|
|