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

Term   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 167
Duplicated Lines 5.39 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 167
rs 10
wmc 13
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
B create_objects() 9 24 5
B create_test_object() 0 34 2
B delete() 0 60 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace testContent\Types;
3
use testContent\Abstracts as Abs;
4
use testContent\TestContent as TestContent;
5
use testContent\Delete as Delete;
6
7
8
/**
9
 * Class to build test data for terms.
10
 *
11
 * @package    WordPress
12
 * @subpackage Evans
13
 * @author     Old Town Media
14
 */
15
class Term extends Abs\Type{
16
17
	protected $type = 'term';
18
19
	/**
20
	 * Create test data posts.
21
	 *
22
	 * This is where the magic begins. We accept a cpt id (slug) and potntially
23
	 * a number of posts to create. We then fetch the supports & metaboxes
24
	 * for that cpt and feed them into a function to create each post individually.
25
	 *
26
	 * @access private
27
	 *
28
	 * @see $this->get_cpt_supports, $this->get_metaboxes, $this->create_test_object
29
	 *
30
	 * @param string $slug a custom post type ID.
31
	 * @param boolean $echo Whether or not to echo. Optional.
32
	 * @param int $num Optional. Number of posts to create.
33
	 */
34
	public function create_objects( $slug, $connection, $echo = false, $num = '' ){
0 ignored issues
show
Unused Code introduced by
The parameter $connection is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
36
		// If we're missing a custom post type id - don't do anything
37
		if ( empty( $slug ) ){
38
			return;
39
		}
40
41
		// If we forgot to put in a quantity, make one for us
42
		if ( empty( $num ) ){
43
			$num = rand( 5, 30 );
44
		}
45
46
		// Create test terms
47 View Code Duplication
		for( $i = 0; $i < $num; $i++ ){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
48
49
			$return = $this->create_test_object( $slug );
50
51
			if ( $echo === true ){
52
				echo \json_encode( $return );
53
			}
54
55
		}
56
57
	}
58
59
60
	/**
61
	 * Creates the individual test data object.
62
	 *
63
	 * Create individual posts for testing with. Gathers basic information such
64
	 * as title, content, thumbnail, etc. and inserts them with the post. Also
65
	 * adds metaboxes if applicable .
66
	 *
67
	 * @access private
68
	 *
69
	 * @see TestContent, wp_insert_post, add_post_meta, update_post_meta, $this->random_metabox_content
70
	 *
71
	 * @param string $slug a custom post type ID.
72
	 */
73
	private function create_test_object( $slug ){
74
75
		// Get a random title
76
		$title = apply_filters( "tc_{$slug}_term_title", TestContent::title() );
77
78
		$return = wp_insert_term(
79
			$title,
80
			$slug,
81
			apply_filters( "tc_{$slug}_term_arguments", array(
82
					'description'=> TestContent::title(),
83
					'slug' => sanitize_title( $title ),
84
				)
85
			)
86
		);
87
88
		// Then, set a test content flag on the new post for later deletion
89
		add_term_meta( $return['term_id'], 'evans_test_content', '__test__', true );
90
91
		// Check if we have errors and return them or created message
92
		if ( is_wp_error( $return ) ){
93
			error_log( $return->get_error_message() );
94
			return $return;
95
		} else {
96
			return array(
97
				'type'		=> 'created',
98
				'object'	=> 'term',
99
				'tid'		=> $return['term_id'],
100
				'taxonomy'	=> $slug,
101
				'link_edit'	=> admin_url( '/edit-tags.php?action=edit&taxonomy='.$slug.'&tag_ID='.$return['term_id'] ),
102
				'link_view'	=> get_term_link( $return['term_id'] )
103
			);
104
		}
105
106
	}
107
108
	/**
109
	 * Delete test data terms.
110
	 *
111
	 * This function will search for all terms of a particular taxonomy ($slug)
112
	 * and delete them all using a particular term_meta flag that we set when creating
113
	 * the posts. Validates the user first.
114
	 *
115
	 * @see WP_Query, wp_delete_post
116
	 *
117
	 * @param string $slug a custom post type ID.
118
	 * @param boolean $echo Whether or not to echo the result
119
	 */
120
	public function delete( $slug, $echo = false ){
121
122
		$delete =  new Delete;
123
124
		// Make sure that the current user is logged in & has full permissions.
125
		if ( !$delete->user_can_delete() ){
126
			return;
127
		}
128
129
		// Check that $cptslg has a string.
130
		if ( empty( $slug ) ){
131
			return;
132
		}
133
134
		// Query for our terms
135
		$args = array(
136
		    'hide_empty' => false,
137
		    'meta_query' => array(
138
		        array(
139
		           'key'       => 'evans_test_content',
140
		           'value'     => '__test__',
141
		           'compare'   => '='
142
		        )
143
		    )
144
		);
145
146
		$terms = get_terms( $slug, $args );
147
148
		if ( !empty( $terms ) ){
149
150
			$events = array();
151
152
			foreach ( $terms as $term ){
153
154
				if ( $echo === true ){
155
					$events[] = array(
156
						'type'		=> 'deleted',
157
						'pid'		=> $term->term_id,
158
						'post_type'	=> $slug,
159
						'link'		=> ''
160
					);
161
				}
162
163
				// Delete our term
164
				wp_delete_term( $term->term_id, $slug );
165
166
			}
167
168
			$taxonomy = get_taxonomy( $slug );
169
170
			$events[] = array(
171
				'type'		=> 'general',
172
				'message'	=> __( 'Deleted', 'otm-test-content' ) . ' ' . $taxonomy->labels->name
173
			);
174
175
			echo \json_encode( $events );
176
177
		}
178
179
	}
180
181
}
182