PluginProbe
Assistant – Every Day Productivity Apps / 1.4.7
Assistant – Every Day Productivity Apps v1.4.7
1.5.5 trunk 0.1.0 0.2.0 0.2.1 0.2.2 0.3.0 0.3.1 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.6.0 0.7.0 0.7.0.2 0.7.0.3 0.7.0.4 0.7.0.5 0.7.0.6 0.7.0.7 0.7.0.8 0.7.0.9 0.7.1 1.0 All 71 releases
← All changes | backend/src/Clients/Cloud/CloudLibraries.php +65 -19 0.7.0.4 → 1.4.7 View file →
@@ -20,10 +20,12 @@
20 20 /**
21 21 * @return object
22 22 */
23 23 public function create_item( $library_id, $data ) {
24 - $data = $this->normalize_media( $data );
25 - return $this->client->post( "/libraries/$library_id/library-items", $data );
24 + $remaining = $this->convert_media_to_chunks( $data );
25 + $result = $this->client->post( "/libraries/$library_id/library-items", $data );
26 + $this->upload_remaining_media_chunks( $result->id, $remaining );
27 + return $result;
26 28 }
27 29
28 30 /**
29 31 * @return object
@@ -28,11 +30,13 @@
28 30 /**
29 31 * @return object
30 32 */
31 33 public function update_item( $item_id, $data ) {
32 - $data = $this->normalize_media( $data );
33 34 $data['_method'] = 'PUT';
34 - return $this->client->post( "/library-items/$item_id", $data );
35 + $remaining = $this->convert_media_to_chunks( $data );
36 + $result = $this->client->post( "/library-items/$item_id", $data );
37 + $this->upload_remaining_media_chunks( $item_id, $remaining );
38 + return $result;
35 39 }
36 40
37 41 /**
38 42 * @return object
@@ -37,30 +41,72 @@
37 41 /**
38 42 * @return object
39 43 */
40 44 public function get_item( $item_id ) {
41 - return $this->client->get( "/library-items/$item_id" );
45 + return $this->client->get( "/library-items/$item_id?include_post_data=1" );
42 46 }
43 47
44 48 /**
49 + * @return void
50 + */
51 + public function upload_remaining_media_chunks( $item_id, $chunks ) {
52 + for ( $i = 0; $i < count( $chunks ); $i++ ) {
53 + $data = array_merge( $chunks[ $i ],
54 + [
55 + '_method' => 'PUT',
56 + 'preserve_media' => true,
57 + ]
58 + );
59 + $this->client->post( "/library-items/$item_id", $data );
60 + }
61 + }
62 +
63 + /**
45 64 * @return array
46 65 */
47 - protected function normalize_media( $data ) {
48 - if ( isset( $data['media'] ) ) {
49 - foreach ( $data['media'] as $key => $media ) {
50 - if ( ! $media ) {
51 - continue;
52 - } elseif ( is_array( $media ) ) {
53 - foreach ( $media as $i => $path ) {
54 - if ( $path && file_exists( $path ) ) {
55 - $data[ "media[$key][$i]" ] = curl_file_create( $path );
56 - }
66 + protected function convert_media_to_chunks( &$data ) {
67 + $max_uploads = ini_get( 'max_file_uploads' );
68 + $files = [];
69 +
70 + // Handle the post thumb - must come first.
71 + if ( isset( $data['media']['thumb'] ) ) {
72 + if ( file_exists( $data['media']['thumb'] ) ) {
73 + $files['media[thumb]'] = curl_file_create( $data['media']['thumb'] );
74 + } else {
75 + $files['media[thumb]'] = null;
76 + }
77 + }
78 +
79 + // Handle image file export.
80 + if ( isset( $data['media']['file'] ) ) {
81 + if ( file_exists( $data['media']['file'] ) ) {
82 + $files['media[file]'] = curl_file_create( $data['media']['file'] );
83 + }
84 + }
85 +
86 + // Handle post attachments.
87 + if ( isset( $data['media']['attachments'] ) ) {
88 + if ( empty( $data['media']['attachments'] ) ) {
89 + $files['media[attachments]'] = null;
90 + } else {
91 + foreach ( $data['media']['attachments'] as $i => $path ) {
92 + if ( $path && file_exists( $path ) ) {
93 + $files[ "media[attachments][$i]" ] = curl_file_create( $path );
57 94 }
58 - } elseif ( file_exists( $media ) ) {
59 - $data[ "media[$key]" ] = curl_file_create( $media );
60 95 }
61 96 }
62 - unset( $data['media'] );
63 97 }
64 - return $data;
98 +
99 + // Unset the original media array.
100 + unset( $data['media'] );
101 +
102 + // Convert media files to chunks. Send the first chunk with the data.
103 + $chunks = array_chunk( $files, $max_uploads, true );
104 +
105 + if ( isset( $chunks[0] ) ) {
106 + $data = array_merge( $data, $chunks[0] );
107 + array_shift( $chunks );
108 + }
109 +
110 + return $chunks;
65 111 }
66 112 }