PluginProbe
FakerPress / 0.8.0
FakerPress v0.8.0
0.9.2 trunk 0.1.0 0.1.1 0.1.2 0.1.3 0.5.2 0.5.3 0.6.1 0.6.2 0.6.3 0.6.4 0.6.5 0.6.6 0.7.0 0.7.1 0.7.2 0.8.0 0.9.0 0.9.1
fakerpress / src / FakerPress / Module / Attachment.php

Attachment.php in FakerPress 0.8.0, at src/FakerPress/Module/Attachment.php

201 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace FakerPress\Module;
3 use FakerPress\Provider\Image\Placeholder;
4 use FakerPress\Provider\Image\LoremPicsum;
5 use WP_Error;
6 use FakerPress;
7 use FakerPress\ThirdParty\Faker;
8
9 class Attachment extends Abstract_Module {
10
11 /**
12 * Holds the key for the meta value of the original URL from where
13 * a given attachment was downloaded from.
14 *
15 * @since 0.5.0
16 *
17 * @var string
18 */
19 public static $meta_key_original_url = '_fakerpress_orginal_url';
20
21 protected $dependencies = [
22 FakerPress\ThirdParty\Faker\Provider\Lorem::class,
23 FakerPress\ThirdParty\Faker\Provider\DateTime::class,
24 FakerPress\Provider\HTML::class,
25 FakerPress\Provider\Image\Placeholder::class,
26 FakerPress\Provider\Image\LoremPicsum::class,
27 ];
28
29 protected $provider_class = FakerPress\Provider\WP_Attachment::class;
30
31 public static function get_slug(): string {
32 return 'attachments';
33 }
34
35 public function hook():void {
36 }
37
38 /**
39 * To use the Attachment Module the current user must have at least the `upload_files` permission.
40 *
41 * @since 0.6.0
42 *
43 * @return string
44 */
45 public static function get_permission_required(): string {
46 return 'upload_files';
47 }
48
49 /**
50 * @inheritDoc
51 */
52 public static function fetch( array $args = [] ): array {
53 // TODO: Implement fetch() method.
54 }
55
56 /**
57 * @inheritDoc
58 */
59 public static function delete( $item ) {
60 // TODO: Implement delete() method.
61 }
62
63 /**
64 * Handle the downloads of Attachments given a URL and Post Parent ID, which will default to 0.
65 * Currently only support images.
66 *
67 * @since 0.5.0
68 *
69 * @param string $url Which URL we are using to download.
70 * @param integer $post_parent_id Which post this will be attached to.
71 *
72 * @return int|WP_Error Attachment ID or WP_Error.
73 */
74 protected function handle_download( $url, $post_parent_id = 0 ) {
75 /**
76 * Allows filtering of the attachment download_url timeout, which is here just to
77 * prevent fakerpress timing out.
78 *
79 * @since 0.5.0
80 *
81 * @param int $timeout Download timeout.
82 * @param string $url Which url we are downloading it for.
83 * @param int $post_parent_id Which post this will be attached to.
84 */
85 $timeout = apply_filters( 'fakerpress.module.attachment.download_url_timeout', 10, $url, $post_parent_id );
86
87 // Download temp file
88 $temporary_file = download_url( $url, $timeout );
89
90 // Check for download errors if there are error unlink the temp file name
91 if ( is_wp_error( $temporary_file ) ) {
92 return $temporary_file;
93 }
94
95 $mime_type = wp_get_image_mime( $temporary_file );
96 if ( ! $mime_type ) {
97 return new WP_Error( 'invalid-image-mimetype', __( 'Invalid image MimeType', 'fakerpress' ) );
98 }
99
100 $allowed_mime_types = get_allowed_mime_types();
101
102 $extension = array_search( $mime_type, $allowed_mime_types );
103 if ( $extension ) {
104 $extension = explode( '|', $extension );
105 }
106
107 if ( ! $extension ) {
108 return new WP_Error( 'invalid-image-mimetype', __( 'Invalid image MimeType', 'fakerpress' ) );
109 }
110
111 // Build file name with Extension.
112 $filename = implode( '.', [ $this->get_faker()->uuid(), reset( $extension ) ] );
113
114 $file = [
115 'name' => $filename,
116 'tmp_name' => $temporary_file,
117 ];
118
119 // uploads as an attachment to WP
120 $attachment_id = media_handle_sideload( $file, $post_parent_id );
121
122 // download_url requires deleting the file
123 if ( file_exists( $temporary_file ) ) {
124 unlink( $temporary_file );
125 }
126
127 /**
128 * We don't want to pass something to $id
129 * if there were upload errors.
130 * So this checks for errors
131 */
132 if ( is_wp_error( $attachment_id ) ) {
133 if ( file_exists( $temporary_file ) ) {
134 unlink( $temporary_file );
135 }
136 return $attachment_id;
137 }
138
139 // Return Attachment ID
140 return $attachment_id;
141 }
142
143 /**
144 * @inheritDoc
145 */
146 public function filter_save_response( $response, array $data, Abstract_Module $module ) {
147 if ( empty( $data['attachment_url'] ) ) {
148 return false;
149 }
150
151 $attachment_id = $this->handle_download( $data['attachment_url'] );
152
153 // Flag the Object as FakerPress
154 update_post_meta( $attachment_id, static::get_flag(), 1 );
155
156 // Add the Original URL to the meta of the attachment
157 update_post_meta( $attachment_id, static::$meta_key_original_url, $data['attachment_url'] );
158
159 return $attachment_id;
160 }
161
162 /**
163 * Gets an Array with all the providers based on a given Type
164 *
165 * @param string $type Which type of provider you are looking for
166 *
167 * @return array With ID, Text and Type
168 */
169 public static function get_providers( string $type = 'image' ): array {
170 $providers = [
171 [
172 'id' => Placeholder::ID,
173 'text' => esc_attr__( 'Placehold.co', 'fakerpress' ),
174 'type' => 'image',
175 ],
176 [
177 'id' => LoremPicsum::ID,
178 'text' => esc_attr__( 'Lorem Picsum', 'fakerpress' ),
179 'type' => 'image',
180 ],
181 ];
182
183 return $providers;
184
185 }
186
187 /**
188 * @since 0.6.4
189 *
190 * @throws \Exception
191 *
192 * @param $request
193 * @param $qty
194 *
195 * @return array|string
196 */
197 public function parse_request( $qty, $request = [] ) {
198
199 }
200 }
201