1
|
|
|
<?php |
2
|
|
|
namespace testContent; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class to handle deletion of test data for the plugin. |
6
|
|
|
* |
7
|
|
|
* @package WordPress |
8
|
|
|
* @subpackage Evans |
9
|
|
|
* @author Old Town Media |
10
|
|
|
*/ |
11
|
|
|
class Delete{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Delete all test content created ever. |
15
|
|
|
* |
16
|
|
|
* @access private |
17
|
|
|
*/ |
18
|
|
|
public function delete_all_test_data( $echo = false ){ |
19
|
|
|
|
20
|
|
|
if ( !$this->user_can_delete() ){ |
21
|
|
|
return; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
// Loop through all post types and remove any test data |
25
|
|
|
$post_types = get_post_types( array( 'public' => true ), 'objects' ); |
26
|
|
|
foreach ( $post_types as $post_type ) : |
27
|
|
|
|
28
|
|
|
$this->delete_posts( $post_type->name, $echo ); |
|
|
|
|
29
|
|
|
|
30
|
|
|
endforeach; |
31
|
|
|
|
32
|
|
|
// Loop through all taxonomies and remove any data |
33
|
|
|
$taxonomies = get_taxonomies(); |
34
|
|
|
foreach ( $taxonomies as $tax ) : |
35
|
|
|
|
36
|
|
|
$this->delete_terms( $tax, $echo ); |
|
|
|
|
37
|
|
|
|
38
|
|
|
endforeach; |
39
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Delete test data terms. |
44
|
|
|
* |
45
|
|
|
* This function will search for all terms of a particular taxonomy ($slug) |
46
|
|
|
* and delete them all using a particular term_meta flag that we set when creating |
47
|
|
|
* the posts. Validates the user first. |
48
|
|
|
* |
49
|
|
|
* @see WP_Query, wp_delete_post |
50
|
|
|
* |
51
|
|
|
* @param string $slug a custom post type ID. |
|
|
|
|
52
|
|
|
* @param boolean $echo Whether or not to echo the result |
53
|
|
|
*/ |
54
|
|
|
public function delete_objects( $echo = false, $data ){ |
55
|
|
|
|
56
|
|
|
// Make sure that the current user is logged in & has full permissions. |
57
|
|
|
if ( !$this->user_can_delete() ){ |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ( empty( $data ) ){ |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$type = 'testContent\Types\\' . ucwords( $data['type'] ); |
66
|
|
|
$slug = $data['slug']; |
67
|
|
|
|
68
|
|
|
$object = new $type(); |
69
|
|
|
$object->delete( $slug, $echo ); |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Run some checks to make sure that our user is allowed to delete data. |
76
|
|
|
* |
77
|
|
|
* @see is_user_logged_in, current_user_can |
78
|
|
|
*/ |
79
|
|
|
public function user_can_delete(){ |
80
|
|
|
|
81
|
|
|
// User must be logged in |
82
|
|
|
if ( !is_user_logged_in() ){ |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// User must have editor priveledges, at a minimum |
87
|
|
|
if ( !current_user_can( 'delete_others_posts' ) ){ |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// We passed all the checks, hooray! |
92
|
|
|
return true; |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.