PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
← All changes | json-endpoints/class.wpcom-json-api-upload-media-endpoint.php +122 -0 16.2-beta → 16.3 View file →
@@ -1,0 +1,122 @@
1 +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 +/**
3 + * Upload media item API endpoint.
4 + *
5 + * Endpoint: /sites/%s/media/new
6 + */
7 +
8 +if ( ! defined( 'ABSPATH' ) ) {
9 + exit( 0 );
10 +}
11 +
12 +new WPCOM_JSON_API_Upload_Media_Endpoint(
13 + array(
14 + 'description' => 'Upload a new media item.',
15 + 'group' => 'media',
16 + 'stat' => 'media:new',
17 + 'method' => 'POST',
18 + 'path' => '/sites/%s/media/new',
19 + 'deprecated' => true,
20 + 'new_version' => '1.1',
21 + 'max_version' => '1',
22 + 'path_labels' => array(
23 + '$site' => '(int|string) Site ID or domain',
24 + ),
25 +
26 + 'request_format' => array(
27 + 'media' => '(media) An array of media to attach to the post. To upload media, the entire request should be multipart/form-data encoded. Accepts images (image/gif, image/jpeg, image/png) only at this time.<br /><br /><strong>Example</strong>:<br />' .
28 + "<code>curl \<br />--form 'media[]=@/path/to/file.jpg' \<br />-H 'Authorization: BEARER your-token' \<br />'https://public-api.wordpress.com/rest/v1/sites/123/media/new'</code>",
29 + 'media_urls' => '(array) An array of URLs to upload to the post.',
30 + ),
31 +
32 + 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/media/new/',
33 +
34 + 'response_format' => array(
35 + 'media' => '(array) Array of uploaded media',
36 + 'errors' => '(array) Array of error messages of uploading media failures',
37 + ),
38 + 'example_request_data' => array(
39 + 'headers' => array(
40 + 'authorization' => 'Bearer YOUR_API_TOKEN',
41 + ),
42 + 'body' => array(
43 + 'media_urls' => 'https://s.w.org/about/images/logos/codeispoetry-rgb.png',
44 + ),
45 + ),
46 + )
47 +);
48 +
49 +/**
50 + * Upload media item API class.
51 + *
52 + * @phan-constructor-used-for-side-effects
53 + */
54 +class WPCOM_JSON_API_Upload_Media_Endpoint extends WPCOM_JSON_API_Endpoint {
55 + /**
56 + * Upload media item API endpoint callback.
57 + *
58 + * @param string $path API path.
59 + * @param int $blog_id Blog ID.
60 + */
61 + public function callback( $path = '', $blog_id = 0 ) {
62 + $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
63 + if ( is_wp_error( $blog_id ) ) {
64 + return $blog_id;
65 + }
66 +
67 + if ( ! current_user_can( 'upload_files' ) ) {
68 + return new WP_Error( 'unauthorized', 'User cannot upload media.', 403 );
69 + }
70 +
71 + $input = $this->input( true );
72 +
73 + $has_media = isset( $input['media'] ) && $input['media'] ? count( $input['media'] ) : false;
74 + $has_media_urls = isset( $input['media_urls'] ) && $input['media_urls'] ? count( $input['media_urls'] ) : false;
75 +
76 + $errors = array();
77 + $files = array();
78 + $media_ids = array();
79 +
80 + if ( $has_media ) {
81 + $this->api->trap_wp_die( 'upload_error' );
82 + foreach ( $input['media'] as $index => $media_item ) {
83 + $_FILES['.api.media.item.'] = $media_item;
84 + // check for WP_Error if we ever actually need $media_id.
85 + $media_id = media_handle_upload( '.api.media.item.', 0 );
86 + if ( is_wp_error( $media_id ) ) {
87 + if ( is_countable( $input['media'] ) && 1 === count( $input['media'] ) && ! $has_media_urls ) {
88 + unset( $_FILES['.api.media.item.'] );
89 + return $media_id;
90 + }
91 + $errors[ $index ]['error'] = $media_id->get_error_code();
92 + $errors[ $index ]['message'] = $media_id->get_error_message();
93 + } else {
94 + $media_ids[ $index ] = $media_id;
95 + }
96 + $files[] = $media_item;
97 + }
98 + $this->api->trap_wp_die( null );
99 +
100 + unset( $_FILES['.api.media.item.'] );
101 + }
102 +
103 + if ( $has_media_urls ) {
104 + foreach ( $input['media_urls'] as $url ) {
105 + $id = $this->handle_media_sideload( $url );
106 + if ( ! empty( $id ) && is_int( $id ) ) {
107 + $media_ids[] = $id;
108 + }
109 + }
110 + }
111 +
112 + $results = array();
113 + foreach ( $media_ids as $media_id ) {
114 + $results[] = $this->get_media_item( $media_id );
115 + }
116 +
117 + return array(
118 + 'media' => $results,
119 + 'errors' => $errors,
120 + );
121 + }
122 +}