GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

WP_Post_Helper   C
last analyzed

Complexity

Total Complexity 74

Size/Duplication

Total Lines 272
Duplicated Lines 6.62 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 18
loc 272
rs 5.5244
c 0
b 0
f 0
wmc 74
lcom 1
cbo 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A postid() 0 3 1
A attachment_id() 0 3 1
C init() 0 27 8
C set() 0 36 12
B insert() 9 17 5
B update() 9 17 6
D add_related_meta() 0 39 10
B add_tags() 0 14 6
B add_terms() 0 12 5
C add_media() 0 53 13
A add_meta() 0 5 3
A add_field() 0 5 3

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like WP_Post_Helper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use WP_Post_Helper, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 en : https://gist.github.com/4084471
4
 ja : https://gist.github.com/4078027
5
6
License:
7
 Released under the GPL license
8
  http://www.gnu.org/copyleft/gpl.html
9
10
  Copyright 2013 (email : [email protected])
11
12
    This program is free software; you can redistribute it and/or modify
13
    it under the terms of the GNU General Public License as published by
14
    the Free Software Foundation; either version 2 of the License, or
15
    (at your option) any later version.
16
17
    This program is distributed in the hope that it will be useful,
18
    but WITHOUT ANY WARRANTY; without even the implied warranty of
19
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
    GNU General Public License for more details.
21
22
    You should have received a copy of the GNU General Public License
23
    along with this program; if not, write to the Free Software
24
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
25
*/
26
27
if ( defined( 'ABSPATH' ) ) :
28
29
	require_once( ABSPATH . 'wp-admin/includes/image.php' );
30
	require_once( ABSPATH . 'wp-admin/includes/post.php' );
31
32
	class WP_Post_Helper {
33
		public $post;
34
35
		private $postid = false;
36
		private $post_id = false;
37
		private $attachment_id = array();
38
39
		private $tags   = array();
40
		private $medias = array();
41
		private $metas  = array();
42
		private $fields = array();
43
		private $media_count = 0;
44
		private $terms  = array();
45
46
		public function __construct($args = array()) {
47
			$this->init( $args );
48
		}
49
50
		// Get PostID
51
		public function postid() {
52
			return $this->postid;
53
		}
54
55
		// Get Attachment ID
56
		public function attachment_id() {
57
			return $this->attachment_id;
58
		}
59
60
		// Init Post Data
61
		public function init($args = array()) {
62
			if ( is_object( $args ) ) {
63
				$args = (array) $args; }
64
			$this->attachment_id = array();
65
			$this->tags   = array();
66
			$this->medias = array();
67
			$this->metas  = array();
68
			$this->fields = array();
69
			$this->media_count = 0;
70
71
			if ( is_numeric( $args ) ) {
72
				$post = get_post( intval( $args ) );
73
				if ( $post && isset( $post->ID ) && ! is_wp_error( $post ) ) {
74
					$this->post_id = $post->ID;
75
					$this->post = $post;
76
					return true;
77
				} else {
78
					return false;
79
				}
80
			} else {
81
				$this->post = get_default_post_to_edit();
82
				$this->post->post_category = null;
83
				if ( is_array( $args ) && count( $args ) > 0 ) {
84
					return $this->set( $args );
85
				} else { 				return true; }
86
			}
87
		}
88
89
		// Set Post Data
90
		public function set($args) {
91
			if ( is_object( $args ) ) {
92
				$args = (array) $args; }
93
			if ( ! is_array( $args ) ) {
94
				return false; }
95
96
			if ( isset( $args['ID'] ) || isset( $args['post_id'] ) ) {
97
				$post_id = isset( $args['ID'] ) ? $args['ID'] : $args['post_id'];
98
				$post = get_post( $post_id, 'ARRAY_A' );
99
				if ( isset( $post['ID'] ) ) {
100
					$this->post_id  = $post_id;
101
					$this->post->ID = $post_id;
102
					unset( $post['ID'] );
103
					$this->set( $post );
104
				}
105
				unset( $post );
106
			}
107
108
			$post = $this->post;
109
			foreach ( $post as $key => &$val ) {
110
				if ( 'ID' !== $key && isset( $args[ $key ] ) ) {
111
					$val = $args[ $key ];
112
				}
113
			}
114
			$this->post = $post;
115
116
			if ( isset( $args['post_tags'] ) ) {
117
				$this->add_tags(
118
					is_array( $args['post_tags'] )
119
					? $args['post_tags']
120
					: explode( ',', $args['post_tags'] )
121
				);
122
			}
123
124
			return true;
125
		}
126
127
		// Add Post
128
		public function insert() {
129
			if ( ! isset( $this->post ) ) {
130
				return false; }
131
132
			$this->postid   = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $postid was declared of type boolean, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
133
			$this->post->ID = 0;
134
			$postid = wp_insert_post( $this->post );
135 View Code Duplication
			if ( $postid && ! is_wp_error( $postid ) ) {
136
				$this->postid   = $postid;
137
				$this->post->ID = $postid;
138
				return $this->add_related_meta( $postid ) ? $postid : false;
139
			} else {
140
				$this->postid   = $postid;
141
				$this->post->ID = 0;
142
				return false;
143
			}
144
		}
145
146
		// Update Post
147
		public function update() {
148
			if ( ! isset( $this->post ) ) {
149
				return false; }
150
151
			$postid = $this->postid
152
			? wp_update_post( $this->post )
153
			: wp_insert_post( $this->post );
154 View Code Duplication
			if ( $postid && ! is_wp_error( $postid ) ) {
155
				$this->postid   = $postid;
156
				$this->post->ID = $postid;
157
				return $this->add_related_meta( $postid ) ? $postid : false;
158
			} else {
159
				$this->postid   = false;
160
				$this->post->ID = 0;
161
				return false;
162
			}
163
		}
164
165
		private function add_related_meta($postid) {
166
			if ( ! $postid || is_wp_error( $postid ) ) {
167
				return false; }
168
169
			$this->postid   = $postid;
170
171
			// add Tags
172
			if ( count( $this->tags ) > 0 ) {
173
				$this->add_tags( $this->tags ); }
174
			$this->tags = array();
175
176
			// add medias
177
			foreach ( $this->medias as $key => $val ) {
178
				$this->add_media( $key, $val[0], $val[1], $val[2], $val[3] );
179
			}
180
			$this->medias = array();
181
182
			// add terms
183
			foreach ( $this->terms as $taxonomy => $terms ) {
184
				$this->add_terms( $taxonomy, $terms );
185
			}
186
			$this->terms = array();
187
188
			// add Custom Fields
189
			foreach ( $this->metas as $key => $val ) {
190
				if ( is_array( $val ) ) {
191
					$this->add_meta( $key, $val[0], isset( $val[1] ) ? $val[1] : true );
192
				} else { 				$this->add_meta( $key, $val ); }
193
			}
194
			$this->metas = array();
195
196
			// add ACF Fields
197
			foreach ( $this->fields as $key => $val ) {
198
				$this->add_field( $key, $val );
199
			}
200
			$this->fields = array();
201
202
			return true;
203
		}
204
205
		// Add Tag
206
		public function add_tags($tags = array()) {
207
			$tags = is_array( $tags ) ? $tags : explode( ',', $tags );
208
			foreach ( $tags as $tag ) {
209
				if ( ! empty( $tag ) && ! array_search( $tag, $this->tags ) ) {
210
					$this->tags[] = $tag; }
211
			}
212
			unset( $tags );
213
214
			if ( $this->postid ) {
215
				$tags = implode( ',', $this->tags );
216
				$this->tags = array();
217
				return wp_add_post_tags( $this->postid, $tags );
218
			}
219
		}
220
221
		// add terms
222
		public function add_terms($taxonomy, $terms) {
223
			if ( ! $this->postid ) {
224
				if ( ! isset( $this->terms[ $taxonomy ] ) ) {
225
					$this->terms[ $taxonomy ] = array(); }
226
				foreach ( (array) $terms as $term ) {
227
					if ( array_search( $term, $this->terms[ $taxonomy ] ) === false ) {
228
						$this->terms[ $taxonomy ][] = $term; }
229
				}
230
			} else {
231
				return wp_set_object_terms( $this->postid, $terms, $taxonomy );
232
			}
233
		}
234
235
		// Add Media
236
		public function add_media($filename, $title = null, $content = null, $excerpt = null, $thumbnail = false) {
237
			if ( ! $this->postid ) {
238
				$this->medias[ $filename ] = array(
239
				$title,
240
				$content,
241
				$excerpt,
242
				$thumbnail,
243
				);
244
				return;
245
			}
246
247
			if ( $filename && file_exists( $filename ) ) {
248
				$mime_type = '';
249
				$wp_filetype = wp_check_filetype( basename( $filename ), null );
250
				if ( isset( $wp_filetype['type'] ) && $wp_filetype['type'] ) {
251
					$mime_type = $wp_filetype['type']; }
252
				unset( $wp_filetype );
253
254
				$title = isset( $title ) ? $title : preg_replace( '/\.[^.]+$/', '', basename( $filename ) );
255
				$content = isset( $content ) ? $content : $title;
256
				$excerpt = isset( $excerpt ) ? $excerpt : $content;
257
				$attachment = array(
258
				'post_mime_type' => $mime_type,
259
				'post_parent'    => $this->postid,
260
				'post_author'    => $this->post->post_author,
261
				'post_title'     => $title,
262
				'post_content'   => $content,
263
				'post_excerpt'   => $excerpt,
264
				'post_status'    => 'inherit',
265
				'menu_order'     => $this->media_count + 1,
266
				);
267
				if ( isset( $this->post->post_name ) && $this->post->post_name ) {
268
					$attachment['post_name'] = $this->post->post_name; }
269
				$attachment_id = wp_insert_attachment( $attachment, $filename, $this->postid );
270
				unset( $attachment );
271
272
				if ( ! is_wp_error( $attachment_id ) ) {
273
					$this->media_count++;
274
					$this->attachment_id[] = $attachment_id;
275
					$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
276
					wp_update_attachment_metadata( $attachment_id,  $attachment_data );
277
					unset( $attachment_data );
278
					if ( $thumbnail ) {
279
						set_post_thumbnail( $this->postid, $attachment_id ); }
280
281
					return $attachment_id;
282
				} else {
283
					return false;
284
				}
285
			} else {
286
				return false;
287
			}
288
		}
289
290
		// Add Custom Field
291
		public function add_meta($metakey, $val, $unique = true) {
292
			if ( ! $this->postid ) {
293
				$this->metas[ $metakey ] = array( $val, $unique );
294
			} else { 			return $val ? add_post_meta( $this->postid, $metakey, $val, $unique ) : false; }
295
		}
296
297
		// Add Advanced Custom Field
298
		public function add_field($field_key, $val) {
299
			if ( ! $this->postid ) {
300
				$this->fields[ $field_key ] = $val;
301
			} else { 			return $val ? update_field( $field_key, $val, $this->postid ) : false; }
302
		}
303
	}
304
305
	function remote_get_file($url = null, $file_dir = '', $headers = array()) {
306
		if ( ! $url ) {
307
			return false; }
308
309
		if ( empty( $file_dir ) ) {
310
			 $upload_dir = wp_upload_dir();
311
			 $file_dir = isset( $upload_dir['path'] ) ? $upload_dir['path'] : '';
312
		}
313
		$file_dir = trailingslashit( $file_dir );
314
315
		// make directory
316
		if ( ! file_exists( $file_dir ) ) {
317
			$dirs = explode( '/', $file_dir );
318
			$subdir = '/';
319
			foreach ( $dirs as $dir ) {
320
				if ( ! empty( $dir ) ) {
321
					$subdir .= $dir . '/';
322
					if ( ! file_exists( $subdir ) ) {
323
						mkdir( $subdir );
324
					}
325
				}
326
			}
327
		}
328
329
		// remote get!
330
		$photo = $file_dir . basename( $url );
331
332
		if ( ! file_exists( $photo ) ) {
333
			$response = wp_remote_get( $url, $headers );
334
			if ( ! is_wp_error( $response ) && 200 === $response['response']['code'] ) {
335
				$photo_data = $response['body'];
336
				file_put_contents( $photo, $photo_data );
337
				unset( $photo_data );
338
			} else {
339
				$photo = false;
340
			}
341
			unset( $response );
342
		}
343
		return file_exists( $photo ) ? $photo : false;
344
	}
345
346
endif;
347