1
|
|
|
<?php |
2
|
|
|
namespace testContent; |
3
|
|
|
use testContent\Views\Users as Users; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class to handle deletion of test data for the plugin. |
7
|
|
|
* |
8
|
|
|
* @package WordPress |
9
|
|
|
* @subpackage Evans |
10
|
|
|
* @author Old Town Media |
11
|
|
|
*/ |
12
|
|
|
class Delete{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Delete all test content created ever. |
16
|
|
|
* |
17
|
|
|
* @access private |
18
|
|
|
*/ |
19
|
|
|
public function delete_all_test_data(){ |
20
|
|
|
$return = ''; |
21
|
|
|
|
22
|
|
|
if ( ! $this->user_can_delete() ){ |
23
|
|
|
return; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$types = apply_filters( 'tc-types', array() ); |
27
|
|
|
|
28
|
|
|
if ( !empty( $types ) ){ |
29
|
|
|
|
30
|
|
|
foreach ( $types as $type ){ |
31
|
|
|
|
32
|
|
|
$class = 'testContent\Types\\' . ucwords( $type ); |
33
|
|
|
$object = new $class(); |
34
|
|
|
|
35
|
|
|
$return .= $object->delete_all(); |
36
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $return; |
42
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Delete test data terms. |
47
|
|
|
* |
48
|
|
|
* This function will search for all terms of a particular taxonomy ($slug) |
49
|
|
|
* and delete them all using a particular term_meta flag that we set when creating |
50
|
|
|
* the posts. Validates the user first. |
51
|
|
|
* |
52
|
|
|
* @see WP_Query, wp_delete_post |
53
|
|
|
* |
54
|
|
|
* @param string $data Information about the type. |
55
|
|
|
*/ |
56
|
|
|
public function delete_objects( $data ){ |
57
|
|
|
|
58
|
|
|
// Make sure that the current user is logged in & has full permissions. |
59
|
|
|
if ( !$this->user_can_delete() ){ |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ( empty( $data ) ){ |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$type = 'testContent\Types\\' . ucwords( $data['type'] ); |
68
|
|
|
$slug = $data['slug']; |
69
|
|
|
|
70
|
|
|
$object = new $type(); |
71
|
|
|
|
72
|
|
|
return $object->delete( $slug ); |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Run some checks to make sure that our user is allowed to delete data. |
79
|
|
|
* |
80
|
|
|
* @see is_user_logged_in, current_user_can |
81
|
|
|
*/ |
82
|
|
|
public function user_can_delete(){ |
83
|
|
|
|
84
|
|
|
// User must be logged in |
85
|
|
|
if ( !is_user_logged_in() ){ |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// User must have editor priveledges, at a minimum |
90
|
|
|
if ( !current_user_can( 'delete_others_posts' ) ){ |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// We passed all the checks, hooray! |
95
|
|
|
return true; |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
|