Completed
Push — master ( c6a753...e758eb )
by Mike
02:16
created

Delete::delete_terms()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 58
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 6
eloc 28
c 2
b 0
f 2
nc 6
nop 2
dl 0
loc 58
rs 8.7274

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 );
0 ignored issues
show
Bug introduced by
The method delete_posts() does not seem to exist on object<testContent\Delete>.

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.

Loading history...
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 );
0 ignored issues
show
Bug introduced by
The method delete_terms() does not seem to exist on object<testContent\Delete>.

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.

Loading history...
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.
0 ignored issues
show
Bug introduced by
There is no parameter named $slug. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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