8 || count(array_unique($ids)) !== count($ids)) { return new WP_Error('apeari_media', __('Wybierz maksymalnie osiem różnych zdjęć.', 'apeari'), [ 'status' => 422, ]); } $items = []; foreach ($ids as $index => $id) { if (!is_int($id) || !wp_attachment_is_image($id) || !current_user_can('read_post', $id)) { return new WP_Error( 'apeari_media', __('Zdjęcie nie jest dostępne w Twojej bibliotece.', 'apeari'), [ 'status' => 403, ], ); } $items['m' . $index] = [ 'id' => $id, 'url' => wp_get_attachment_url($id), 'alt' => (string) get_post_meta($id, '_wp_attachment_image_alt', true), 'title' => get_the_title($id), 'external' => false, ]; } return $items; } public static function existing_url(string $url, int $id = 0): bool { $id = $id ?: attachment_url_to_postid($url); if (!$id || !wp_attachment_is_image($id) || !current_user_can('read_post', $id)) { return false; } if ($url === wp_get_attachment_url($id)) { return true; } $meta = wp_get_attachment_metadata($id); $base = trailingslashit(dirname((string) wp_get_attachment_url($id))); foreach ($meta['sizes'] ?? [] as $size) { if ($url === $base . $size['file']) { return true; } } return false; } public static function vision(array $ids): array { return []; } public static function bind(array $operations, array $media) { foreach ($operations as &$op) { if (isset($op['mediaKey'])) { if (!isset($media[$op['mediaKey']])) { return new WP_Error( 'apeari_media', __('Wybierz istniejące zdjęcie w bibliotece.', 'apeari'), [ 'status' => 422, ], ); } $item = $media[$op['mediaKey']]; $op['attachments'] = [$item]; } if (isset($op['mediaKeys'])) { $op['attachments'] = []; foreach ($op['mediaKeys'] as $key) { if (!isset($media[$key])) { return new WP_Error('apeari_media', __('Wybierz zdjęcia w bibliotece.', 'apeari'), [ 'status' => 422, ]); } $item = $media[$key]; $op['attachments'][] = $item; } } } return $operations; } }