/** * Upload images and save new urls * @return bool */ public function save($post) { $excludePostTypes = self::getOption('exclude_post_types'); if (is_array($excludePostTypes) && in_array($post->post_type, $excludePostTypes)) { return false; } global $wpdb; $content = $post->post_content; $images = $this->findAllImageUrls($content); if ($images === null) { return false; } foreach ($images as $image) { $uploader = new ImageUploader($image['url'], $image['alt'], $post); if ($uploader->validate() && $uploader->save()) { $url = parse_url($uploader->url); $base_url = $uploader->getHostUrl() == null ? null : "http://{$uploader->getHostUrl()}"; $image_url = $base_url . $url['path']; $content = preg_replace('/' . preg_quote($image['url'], '/') . '/', $image_url, $content); $content = preg_replace('/alt=["\']' . preg_quote($image['alt'], '/') . '["\']/', "alt='{$uploader->getAlt()}'", $content); } } return $wpdb->update($wpdb->posts, array('post_content' => $content), array('ID' => $post->ID)) ? true : false; }