Completed
Push — master ( 4713b5...48986a )
by David
05:28 queued 15s
created

Wordlift_Admin_Status_Page::admin_menu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
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() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
57
58
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
76
77
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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