| @@ -1,112 +1,112 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace FL\Assistant\Clients\Cloud; | |
| 4 | - | |
| 5 | -class CloudLibraries { | |
| 6 | - | |
| 7 | - /** | |
| 8 | - * @var CloudClient | |
| 9 | - */ | |
| 10 | - protected $client; | |
| 11 | - | |
| 12 | - /** | |
| 13 | - * @param CloudClient $client | |
| 14 | - * @return void | |
| 15 | - */ | |
| 16 | - public function __construct( CloudClient $client ) { | |
| 17 | - $this->client = $client; | |
| 18 | - } | |
| 19 | - | |
| 20 | - /** | |
| 21 | - * @return object | |
| 22 | - */ | |
| 23 | - public function create_item( $library_id, $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; | |
| 28 | - } | |
| 29 | - | |
| 30 | - /** | |
| 31 | - * @return object | |
| 32 | - */ | |
| 33 | - public function update_item( $item_id, $data ) { | |
| 34 | - $data['_method'] = 'PUT'; | |
| 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; | |
| 39 | - } | |
| 40 | - | |
| 41 | - /** | |
| 42 | - * @return object | |
| 43 | - */ | |
| 44 | - public function get_item( $item_id ) { | |
| 45 | - return $this->client->get( "/library-items/$item_id?include_post_data=1" ); | |
| 46 | - } | |
| 47 | - | |
| 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 | - /** | |
| 64 | - * @return array | |
| 65 | - */ | |
| 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 ); | |
| 94 | - } | |
| 95 | - } | |
| 96 | - } | |
| 97 | - } | |
| 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; | |
| 111 | - } | |
| 112 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace FL\Assistant\Clients\Cloud; | |
| 4 | + | |
| 5 | +class CloudLibraries { | |
| 6 | + | |
| 7 | + /** | |
| 8 | + * @var CloudClient | |
| 9 | + */ | |
| 10 | + protected $client; | |
| 11 | + | |
| 12 | + /** | |
| 13 | + * @param CloudClient $client | |
| 14 | + * @return void | |
| 15 | + */ | |
| 16 | + public function __construct( CloudClient $client ) { | |
| 17 | + $this->client = $client; | |
| 18 | + } | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * @return object | |
| 22 | + */ | |
| 23 | + public function create_item( $library_id, $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; | |
| 28 | + } | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * @return object | |
| 32 | + */ | |
| 33 | + public function update_item( $item_id, $data ) { | |
| 34 | + $data['_method'] = 'PUT'; | |
| 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; | |
| 39 | + } | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * @return object | |
| 43 | + */ | |
| 44 | + public function get_item( $item_id ) { | |
| 45 | + return $this->client->get( "/library-items/$item_id?include_post_data=1" ); | |
| 46 | + } | |
| 47 | + | |
| 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 | + /** | |
| 64 | + * @return array | |
| 65 | + */ | |
| 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 ); | |
| 94 | + } | |
| 95 | + } | |
| 96 | + } | |
| 97 | + } | |
| 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; | |
| 111 | + } | |
| 112 | +} | |