tenweb-speed-optimizer
/
vendor
/
10web-utils
/
tenweb-image-optimizer
/
src
/
TenWebIO
/
Attachments.php
tenweb-speed-optimizer
/
vendor
/
10web-utils
/
tenweb-image-optimizer
/
src
/
TenWebIO
Last commit date
Exceptions
4 years ago
Queue
2 years ago
Views
2 years ago
Api.php
2 years ago
Attachments.php
2 years ago
Backup.php
2 years ago
CLI.php
3 years ago
Compress.php
2 years ago
CompressDataService.php
3 years ago
CompressService.php
3 years ago
Config.php
2 years ago
Init.php
2 years ago
LastCompress.php
3 years ago
Logs.php
2 years ago
PreInit.php
3 years ago
Rest.php
2 years ago
Settings.php
3 years ago
Utils.php
1 year ago
Attachments.php
291 lines
| 1 | <?php |
| 2 | |
| 3 | namespace TenWebIO; |
| 4 | |
| 5 | class Attachments |
| 6 | { |
| 7 | private $allowed_extensions = array('jpeg', 'jpg', 'png'); |
| 8 | private $excluded_ids = array(); |
| 9 | private $excluded_thumb_ids = array(); |
| 10 | private $excluded_other_paths = array(); |
| 11 | private $other_directories = array(); |
| 12 | private $filtered_ids = array(); |
| 13 | private $first_in_queue = array(); |
| 14 | private $limit_query = 0; |
| 15 | private $allowed_image_size = 8388608; // 8mb todo make this with config |
| 16 | |
| 17 | public function setExcludedIds($ids = array()) |
| 18 | { |
| 19 | $this->excluded_ids = array_map('intval', $ids); |
| 20 | } |
| 21 | |
| 22 | public function setExcludedThumbIds($ids = array()) |
| 23 | { |
| 24 | $this->excluded_thumb_ids = $ids; |
| 25 | } |
| 26 | |
| 27 | public function setExcludedOtherPaths($paths = array()) |
| 28 | { |
| 29 | $this->excluded_other_paths = $paths; |
| 30 | } |
| 31 | |
| 32 | public function setOtherDirectories($dirs = array()) |
| 33 | { |
| 34 | $this->other_directories = $dirs; |
| 35 | } |
| 36 | |
| 37 | public function setFilteredIds($ids = array()) |
| 38 | { |
| 39 | $this->filtered_ids = array_map('intval', $ids);; |
| 40 | } |
| 41 | |
| 42 | public function setFirstInQueue($first_in_queue = array()) |
| 43 | { |
| 44 | $this->first_in_queue = $first_in_queue; |
| 45 | } |
| 46 | |
| 47 | public function setLimitQuery($limit_query = 0) |
| 48 | { |
| 49 | $this->limit_query = $limit_query; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * |
| 54 | * @param string[] $order |
| 55 | * |
| 56 | * @return array |
| 57 | */ |
| 58 | public function getData($order = array('attachments_other', 'attachments_full', 'attachments_meta', 'attachments_first_in_queue')) |
| 59 | { |
| 60 | $attachments = $this->getDataSeparate(); |
| 61 | $return_data = array(); |
| 62 | foreach ($order as $item) { |
| 63 | $return_data = array_merge($return_data, $attachments[$item]); |
| 64 | } |
| 65 | |
| 66 | return $return_data; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @return array |
| 71 | */ |
| 72 | public function getDataSeparate() |
| 73 | { |
| 74 | $attachments = $this->getAttachmentsParsedData(); |
| 75 | $other_attachments = $this->getOtherAttachmentsData(); |
| 76 | $total_size = $attachments['total_size'] + $this->getPhotoGalleryTotalSize(); |
| 77 | |
| 78 | return [ |
| 79 | 'attachments_full' => $attachments['full_sizes'], |
| 80 | 'attachments_meta' => $attachments['meta_sizes'], |
| 81 | 'attachments_other' => $other_attachments, |
| 82 | 'total_size' => $total_size, |
| 83 | 'attachments_first_in_queue' => $attachments['first_in_queue_sizes'], |
| 84 | ]; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * |
| 89 | * @return array |
| 90 | */ |
| 91 | public function getAttachmentsParsedData() |
| 92 | { |
| 93 | $config = new Config(); |
| 94 | $attachments = Utils::getGenerator($this->getAttachmentsData()); |
| 95 | $first_in_queue_sizes = array(); |
| 96 | $full_sizes = array(); |
| 97 | $meta_sizes = array(); |
| 98 | $files = array(); |
| 99 | $uploads = wp_get_upload_dir(); |
| 100 | $total_size = 0; |
| 101 | |
| 102 | foreach ($attachments as $attachment) { |
| 103 | $meta_value = $attachment->meta_value ? unserialize($attachment->meta_value) : array(); |
| 104 | $guid = $attachment->guid; |
| 105 | $file_not_exist = false; |
| 106 | $image_file_size = 0; |
| 107 | if (!empty($meta_value['file'])) { |
| 108 | $guid = $uploads['baseurl'] . '/' . $meta_value['file']; |
| 109 | $file_not_exist = !file_exists($uploads['basedir'] . '/' . $meta_value['file']); |
| 110 | $file_is_readable = is_readable($uploads['basedir'] . '/' . $meta_value['file']); |
| 111 | if ($file_not_exist || !$file_is_readable) { |
| 112 | continue; |
| 113 | } |
| 114 | $image_file_size = filesize($uploads['basedir'] . '/' . $meta_value['file']); |
| 115 | |
| 116 | } |
| 117 | if (!$file_not_exist && $image_file_size <= $this->allowed_image_size) { |
| 118 | $extension = strtolower(pathinfo($guid, PATHINFO_EXTENSION)); |
| 119 | if (!in_array($extension, array('jpg', 'jpeg', 'png'))) { |
| 120 | continue; |
| 121 | } |
| 122 | if (in_array($guid, $files) === false) { |
| 123 | $image = array( |
| 124 | 'guid' => $guid, |
| 125 | 'ID' => $attachment->ID, |
| 126 | 'size' => 'full', |
| 127 | ); |
| 128 | $total_size += !empty($meta_value['filesize']) ? $meta_value['filesize'] : 0; |
| 129 | if (!empty($this->first_in_queue) && in_array($guid, $this->first_in_queue)) { |
| 130 | $first_in_queue_sizes[] = $image; |
| 131 | } else { |
| 132 | $full_sizes[] = $image; |
| 133 | } |
| 134 | $files[] = $guid; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | $files = array(); |
| 139 | if (!empty($meta_value['sizes']) && !$config->getCompressOnlyFullSize()) { |
| 140 | $data = Utils::getAttachmentData($meta_value['file']); |
| 141 | foreach ($meta_value['sizes'] as $size_name => $size_data) { |
| 142 | $guid = $uploads['baseurl'] . '/' . $data['destination'] . '/' . $size_data['file']; |
| 143 | $file_path = $uploads['basedir'] . '/' . $data['destination'] . '/' . $size_data['file']; |
| 144 | if (in_array($attachment->ID . '_' . $size_name, $this->excluded_thumb_ids)) { |
| 145 | continue; |
| 146 | } |
| 147 | if (!file_exists($file_path) || !is_readable($file_path)) { |
| 148 | continue; |
| 149 | } |
| 150 | $image_file_size = filesize($file_path); |
| 151 | if (file_exists($file_path) && in_array($guid, $files) === false && $image_file_size <= $this->allowed_image_size) { |
| 152 | $image = array( |
| 153 | 'guid' => $guid, |
| 154 | 'ID' => $attachment->ID, |
| 155 | 'size' => $size_name, |
| 156 | ); |
| 157 | $total_size += (!empty($size_data['filesize']) ? $size_data['filesize'] : 0); |
| 158 | if (!empty($this->first_in_queue) && in_array($guid, $this->first_in_queue)) { |
| 159 | $first_in_queue_sizes[] = $image; |
| 160 | } else { |
| 161 | $meta_sizes[] = $image; |
| 162 | } |
| 163 | $files[] = $guid; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return array('full_sizes' => $full_sizes, 'meta_sizes' => $meta_sizes, 'first_in_queue_sizes' => $first_in_queue_sizes, 'total_size' => $total_size); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @return array |
| 174 | */ |
| 175 | public function getOtherAttachmentsData() |
| 176 | { |
| 177 | if (empty($this->other_directories)) { |
| 178 | return array(); |
| 179 | } |
| 180 | $other_attachments = array(); |
| 181 | $result = array(); |
| 182 | foreach ($this->other_directories as $dir) { |
| 183 | if (is_dir(ABSPATH . $dir)) { |
| 184 | $other_attachments = array_merge($other_attachments, Utils::getFilesFromDir(ABSPATH . $dir)); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | foreach ($other_attachments as $other_attachment) { |
| 189 | $attachment_data = Utils::getAttachmentData($other_attachment); |
| 190 | if (in_array($attachment_data['absolute_url'], $this->excluded_other_paths) || |
| 191 | !in_array($attachment_data['extension'], $this->allowed_extensions)) { |
| 192 | continue; |
| 193 | } |
| 194 | $result[] = array( |
| 195 | 'guid' => $attachment_data['absolute_url'], |
| 196 | 'ID' => 0, |
| 197 | 'size' => 'full_other' |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | return $result; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * @param $image_urls |
| 206 | * @param string $size |
| 207 | * |
| 208 | * @return array|array[] |
| 209 | */ |
| 210 | public function getCustomAttachmentsData($image_urls, $size = 'full_custom') |
| 211 | { |
| 212 | if (empty($image_urls)) { |
| 213 | return array(); |
| 214 | } |
| 215 | |
| 216 | return array_map(function ($value) { |
| 217 | return array('guid' => $value, 'ID' => 0, 'size' => 'full_custom'); |
| 218 | }, $image_urls); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @param $id |
| 223 | * @param null $meta |
| 224 | * |
| 225 | * @return array |
| 226 | */ |
| 227 | public static function getPostMeta($id, $meta = null) |
| 228 | { |
| 229 | if ($meta === null) { |
| 230 | $meta = get_post_meta($id); |
| 231 | } |
| 232 | $files = array(); |
| 233 | if ($meta) { |
| 234 | if (!empty($meta['sizes'])) { |
| 235 | foreach ($meta['sizes'] as $meta_size) { |
| 236 | $files[$meta_size['file']] = !empty($meta_size['filesize']) ? $meta_size['filesize'] : 0; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return $files; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * @return mixed |
| 246 | */ |
| 247 | private function getAttachmentsData() |
| 248 | { |
| 249 | global $wpdb; |
| 250 | $prepare_data = array(); |
| 251 | $where_ids = ''; |
| 252 | $where_exclude_ids = ''; |
| 253 | $limit_by = ''; |
| 254 | if ($this->filtered_ids) { |
| 255 | $where_ids = " AND ID IN (" . implode(', ', array_fill(0, count($this->filtered_ids), '%d')) . ")"; |
| 256 | $prepare_data = array_merge($prepare_data, $this->filtered_ids); |
| 257 | } |
| 258 | if ($this->excluded_ids) { |
| 259 | $where_exclude_ids = " AND ID NOT IN (" . implode(', ', array_fill(0, count($this->excluded_ids), '%d')) . ")"; |
| 260 | $prepare_data = array_merge($prepare_data, $this->excluded_ids); |
| 261 | } |
| 262 | if ($this->limit_query) { |
| 263 | $limit_by = " LIMIT 0,%d"; |
| 264 | $prepare_data[] = $this->limit_query; |
| 265 | } |
| 266 | |
| 267 | return $wpdb->get_results($wpdb->prepare("SELECT ID, guid, 'full' as size, meta_value FROM " . $wpdb->prefix . "posts |
| 268 | left join " . $wpdb->prefix . "postmeta on " . $wpdb->prefix . "posts.ID=" . $wpdb->prefix . "postmeta.post_id and meta_key='_wp_attachment_metadata' |
| 269 | WHERE post_type='attachment' and (post_mime_type LIKE '%jpeg%' or post_mime_type LIKE '%jpg%' or post_mime_type LIKE '%png%') " |
| 270 | . $where_ids . " " . $where_exclude_ids . $limit_by, |
| 271 | $prepare_data |
| 272 | ), OBJECT_K); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @return int |
| 277 | */ |
| 278 | private function getPhotoGalleryTotalSize() |
| 279 | { |
| 280 | if (file_exists(WP_PLUGIN_DIR . '/photo-gallery/framework/WDWLibrary.php')) { |
| 281 | include_once WP_PLUGIN_DIR . '/photo-gallery/framework/WDWLibrary.php'; |
| 282 | if (function_exists('\WDWLibrary::get_images_total_size')) { |
| 283 | return \WDWLibrary::get_images_total_size(); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 |