|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Pages: Admin Status Page. |
|
4
|
|
|
* |
|
5
|
|
|
* A page which reports WordLift's status, currently checking for duplicated entities. |
|
6
|
|
|
* This class is WIP and useful to delete entities that have been created out of |
|
7
|
|
|
* revisions. |
|
8
|
|
|
* |
|
9
|
|
|
* @since 3.9.8 |
|
10
|
|
|
* |
|
11
|
|
|
* @package Wordlift |
|
12
|
|
|
* @subpackage Wordlift/admin |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Define the {@link Wordlift_Admin_Status_Page} class. |
|
17
|
|
|
* |
|
18
|
|
|
* @since 3.6.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class Wordlift_Admin_Status_Page { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Hook to 'admin_menu' to add the 'Status Report' page. |
|
24
|
|
|
* |
|
25
|
|
|
* @since 3.9.8 |
|
26
|
|
|
*/ |
|
27
|
|
View Code Duplication |
public function admin_menu() { |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
// Add a callback to our 'page' function. |
|
30
|
|
|
add_submenu_page( |
|
31
|
|
|
'wl_admin_menu', |
|
32
|
|
|
_x( 'Status Report', 'Page title', 'wordlift' ), |
|
33
|
|
|
_x( 'Status Report', 'Menu title', 'wordlift' ), |
|
34
|
|
|
'manage_options', |
|
35
|
|
|
'wl_status_report', |
|
36
|
|
|
array( $this, 'page', ) |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The admin menu callback to render the page. |
|
43
|
|
|
* |
|
44
|
|
|
* @since 3.9.8 |
|
45
|
|
|
*/ |
|
46
|
|
|
public function page() { |
|
47
|
|
|
|
|
48
|
|
|
$branches = $this->delete_entity_branches(); |
|
49
|
|
|
$revisions = $this->delete_entity_revisions(); |
|
50
|
|
|
|
|
51
|
|
|
// Include the partial. |
|
52
|
|
|
include( 'partials/wordlift-admin-status-page.php' ); |
|
53
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
View Code Duplication |
private function delete_entity_branches() { |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
global $wpdb; |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
$results = $wpdb->get_results( |
|
61
|
|
|
"SELECT DISTINCT p.id" . |
|
62
|
|
|
" FROM $wpdb->posts p" . |
|
63
|
|
|
// Get the post revisions. |
|
64
|
|
|
" WHERE p.post_parent > 0" . |
|
65
|
|
|
" AND p.post_type = 'entity'" |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
foreach ( $results as $result ) { |
|
69
|
|
|
wp_delete_post( $result->id, true ); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return sizeof( $results ); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
View Code Duplication |
private function delete_entity_revisions() { |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
global $wpdb; |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
$results = $wpdb->get_results( |
|
80
|
|
|
"SELECT DISTINCT r.id" . |
|
81
|
|
|
" FROM $wpdb->posts p, $wpdb->posts r" . |
|
82
|
|
|
" WHERE r.post_type = 'revision'" . |
|
83
|
|
|
" AND p.post_type = 'entity'" . |
|
84
|
|
|
" AND p.id = r.post_parent" |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
foreach ( $results as $result ) { |
|
88
|
|
|
wp_delete_post_revision( $result->id ); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return sizeof( $results ); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.