callback()   F
last analyzed

Complexity

Conditions 15
Paths 323

Size

Total Lines 80

Duplication

Lines 4
Ratio 5 %

Importance

Changes 0
Metric Value
cc 15
nc 323
nop 3
dl 4
loc 80
rs 3.273
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
3
new WPCOM_JSON_API_Update_Media_v1_1_Endpoint(
4
	array(
5
		'description'          => 'Edit basic information about a media item.',
6
		'group'                => 'media',
7
		'stat'                 => 'media:1:POST',
8
		'min_version'          => '1.1',
9
		'max_version'          => '1.1',
10
		'method'               => 'POST',
11
		'path'                 => '/sites/%s/media/%d',
12
		'path_labels'          => array(
13
			'$site'     => '(int|string) Site ID or domain',
14
			'$media_ID' => '(int) The ID of the media item',
15
		),
16
17
		'request_format'       => array(
18
			'parent_id'     => '(int) ID of the post this media is attached to',
19
			'title'         => '(string) The file name.',
20
			'caption'       => '(string) File caption.',
21
			'description'   => '(HTML) Description of the file.',
22
			'alt'           => '(string) Alternative text for image files.',
23
			'rating'        => '(string) Video only. Video rating.',
24
			'display_embed' => '(string) Video only. Whether to share or not the video.',
25
			'artist'        => '(string) Audio Only. Artist metadata for the audio track.',
26
			'album'         => '(string) Audio Only. Album metadata for the audio track.',
27
		),
28
29
		'response_format'      => array(
30
			'ID'                         => '(int) The ID of the media item',
31
			'date'                       => '(ISO 8601 datetime) The date the media was uploaded',
32
			'post_ID'                    => '(int) ID of the post this media is attached to',
33
			'author_ID'                  => '(int) ID of the user who uploaded the media',
34
			'URL'                        => '(string) URL to the file',
35
			'guid'                       => '(string) Unique identifier',
36
			'file'                       => '(string) File name',
37
			'extension'                  => '(string) File extension',
38
			'mime_type'                  => '(string) File mime type',
39
			'title'                      => '(string) File name',
40
			'caption'                    => '(string) User provided caption of the file',
41
			'description'                => '(string) Description of the file',
42
			'alt'                        => '(string)  Alternative text for image files.',
43
			'thumbnails'                 => '(object) Media item thumbnail URL options',
44
			'height'                     => '(int) (Image & video only) Height of the media item',
45
			'width'                      => '(int) (Image & video only) Width of the media item',
46
			'length'                     => '(int) (Video & audio only) Duration of the media item, in seconds',
47
			'exif'                       => '(array) (Image & audio only) Exif (meta) information about the media item',
48
			'rating'                     => '(string) (Video only) VideoPress rating of the video',
49
			'display_embed'              => '(string) Video only. Whether to share or not the video.',
50
			'videopress_guid'            => '(string) (Video only) VideoPress GUID of the video when uploaded on a blog with VideoPress',
51
			'videopress_processing_done' => '(bool) (Video only) If the video is uploaded on a blog with VideoPress, this will return the status of processing on the video.',
52
		),
53
54
		'example_request'      => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/media/446',
55
		'example_request_data' => array(
56
			'headers' => array(
57
				'authorization' => 'Bearer YOUR_API_TOKEN',
58
			),
59
			'body'    => array(
60
				'title' => 'Updated Title',
61
			),
62
		),
63
	)
64
);
65
66
class WPCOM_JSON_API_Update_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint {
67
	function callback( $path = '', $blog_id = 0, $media_id = 0 ) {
68
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
69
		if ( is_wp_error( $blog_id ) ) {
70
			return $blog_id;
71
		}
72
73
		if ( ! current_user_can( 'upload_files', $media_id ) ) {
74
			return new WP_Error( 'unauthorized', 'User cannot view media', 403 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
75
		}
76
77
		$item = $this->get_media_item_v1_1( $media_id );
78
79
		if ( is_wp_error( $item ) ) {
80
			return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'unknown_media'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
81
		}
82
83
		$input  = $this->input( true );
84
		$insert = array();
85
86
		if ( isset( $input['title'] ) ) {
87
			$insert['post_title'] = $input['title'];
88
		}
89
90
		if ( isset( $input['caption'] ) ) {
91
			$insert['post_excerpt'] = $input['caption'];
92
		}
93
94
		if ( isset( $input['description'] ) ) {
95
			$insert['post_content'] = $input['description'];
96
		}
97
98
		if ( isset( $input['parent_id'] ) ) {
99
			$insert['post_parent'] = $input['parent_id'];
100
		}
101
102 View Code Duplication
		if ( isset( $input['alt'] ) ) {
103
			$alt = wp_strip_all_tags( $input['alt'], true );
104
			update_post_meta( $media_id, '_wp_attachment_image_alt', $alt );
105
		}
106
107
		// audio only artist/album info.
108
		if ( 0 === strpos( $item->mime_type, 'audio/' ) ) {
109
			$changed = false;
110
			$id3data = wp_get_attachment_metadata( $media_id );
111
112
			if ( ! is_array( $id3data ) ) {
113
				$changed = true;
114
				$id3data = array();
115
			}
116
117
			$id3_keys = array(
118
				'artist' => __( 'Artist', 'jetpack' ),
119
				'album'  => __( 'Album', 'jetpack' ),
120
			);
121
122
			foreach ( $id3_keys as $key => $label ) {
123
				if ( isset( $input[ $key ] ) ) {
124
					$changed         = true;
125
					$id3data[ $key ] = wp_strip_all_tags( $input[ $key ], true );
126
				}
127
			}
128
129
			if ( $changed ) {
130
				wp_update_attachment_metadata( $media_id, $id3data );
131
			}
132
		}
133
134
		// Pass the item to the handle_video_meta() that checks if it's a VideoPress item and saves it.
135
		$result = $this->handle_video_meta( $media_id, $input, $item );
136
137
		if ( is_wp_error( $result ) ) {
138
			return $result;
139
		}
140
141
		$insert['ID'] = $media_id;
142
		wp_update_post( (object) $insert );
143
144
		$item = $this->get_media_item_v1_1( $media_id );
145
		return $item;
146
	}
147
148
	/**
149
	 * Persist the VideoPress metadata if the given item argument is a VideoPress item.
150
	 *
151
	 * @param string   $media_id The ID of the video.
152
	 * @param array    $input    The request input.
153
	 * @param stdClass $item     The response item.
154
	 *
155
	 * @return bool|WP_Error
156
	 */
157
	public function handle_video_meta( $media_id, $input, $item ) {
158
		if ( ! class_exists( \Videopress_Attachment_Metadata::class ) ) {
159
			return false;
160
		}
161
162
		if ( ! \Videopress_Attachment_Metadata::is_videopress_media( $item ) ) {
163
			return false;
164
		}
165
166
		return \Videopress_Attachment_Metadata::persist_metadata(
167
			$media_id,
168
			$item->videopress_guid,
169
			$input['title'],
170
			$input['caption'],
171
			$input['description'],
172
			$input['rating'],
173
			$input['display_embed']
174
		);
175
	}
176
}
177