Skip to content

Commit

Permalink
Add Project
Browse files Browse the repository at this point in the history
  • Loading branch information
airani committed May 24, 2013
0 parents commit 9884b87
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 0 deletions.
Binary file added banner-772x250.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added banner-772x250.psd
Binary file not shown.
201 changes: 201 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
<?php
/*
Plugin Name: Auto Upload Images
Plugin URI: http://p30design.net/1391/08/wp-auto-upload-images.html
Description: Automatically upload external images of a post to wordpress upload directory
Version: 1.4.1
Author: Ali Irani
Author URI: http://p30design.net
License: GPLv2 or later
*/

class wp_auto_upload {

function __construct() {
add_action('save_post', array($this, 'auto_upload'));
}

/**
* Automatically upload external images of a post to wordpress upload directory
*
* @param $post_id
*/
public function auto_upload( $post_id ) {

if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return;

if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
return;

if ( false !== wp_is_post_revision($post_id) )
return;

global $wpdb;

$content = $wpdb->get_var( "SELECT post_content FROM wp_posts WHERE ID='$post_id' LIMIT 1" );

$images_url = $this->wp_get_images_url($content);

if($images_url) {
foreach ($images_url as $image_url) {
if(!$this->wp_is_myurl($image_url) && $new_image_url = $this->wp_save_image($image_url, $post_id)) {
$new_images_url[] = $new_image_url;
unset($new_image_url);
} else {
$new_images_url[] = $image_url;
}
}

$total = count($new_images_url);

for ($i = 0; $i <= $total-1; $i++) {
$new_images_url[$i] = parse_url($new_images_url[$i]);
$content = preg_replace('/'. preg_quote($images_url[$i], '/') .'/', $new_images_url[$i]['path'], $content);
}

remove_action( 'save_post', array($this, 'auto_upload') );
wp_update_post( array('ID' => $post_id, 'post_content' => $content) );
add_action( 'save_post', array($this, 'auto_upload') );
}
}

/**
* Detect url of images which exists in content
*
* @param $content
* @return array of urls or false
*/
public function wp_get_images_url( $content ) {
preg_match_all('/<img[^>]*src=("|\')([^(\?|#|"|\')]*)(\?|#)?[^("|\')]*("|\')[^>]*\/>/', $content, $urls, PREG_SET_ORDER);

if(is_array($urls)) {
foreach ($urls as $url)
$images_url[] = $url[2];
}

if (is_array($images_url)) {
$images_url = array_unique($images_url);
rsort($images_url);
}

return isset($images_url) ? $images_url : false;
}

/**
* Check url is internal or external
*
* @param $url
* @return true or false
*/
public function wp_is_myurl( $url ) {
$url = $this->wp_get_base_url($url);
$myurl = $this->wp_get_base_url(get_bloginfo('url'));

switch ($url) {
case NULL:
case $myurl:
return true;
break;

default:
return false;
break;
}
}

/**
* Give a $url and return Base of a $url
*
* @param $url
* @return base of $url without wwww
*/
public function wp_get_base_url( $url ) {
$url = parse_url($url, PHP_URL_HOST); // Give base URL
$temp = preg_split('/^(www(2|3)?\.)/i', $url, -1, PREG_SPLIT_NO_EMPTY); // Delete www from URL

return $temp[0];
}

/**
* Save image on wp_upload_dir
* Add image to Media Library and attach to post
*
* @param $url
* @param $post_id
* @return new $url or false
*/
public function wp_save_image($url, $post_id = 0) {
$image_name = basename($url);

$upload_dir = wp_upload_dir(date('Y/m'));
$path = $upload_dir['path'] . '/' . $image_name;
$new_image_url = $upload_dir['url'] . '/' . rawurlencode($image_name);
$file_exists = true;
$i = 0;

while ( $file_exists ) {
if ( file_exists($path) ) {
if ( $this->wp_get_exfilesize($url) == filesize($path) ) {
return $new_image_url;
} else {
$i++;
$path = $upload_dir['path'] . '/' . $i . '_' . $image_name;
$new_image_url = $upload_dir['url'] . '/' . $i . '_' . $image_name;
}
} else {
$file_exists = false;
}
}

if(function_exists('curl_init')) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$data = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $data);

$wp_filetype = wp_check_filetype($new_image_url);
$attachment = array(
'guid' => $new_image_url,
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($new_image_url)),
'post_content' => '',
'post_status' => 'inherit'
);
wp_insert_attachment($attachment, $path, $post_id);

return $new_image_url;
} else {
return false;
}
}

/**
* return size of external file
*
* @param $file
* @return $size
*/
public function wp_get_exfilesize( $file ) {
$ch = curl_init($file);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$data = curl_exec($ch);
curl_close($ch);

if (preg_match('/Content-Length: (\d+)/', $data, $matches))
return $contentLength = (int)$matches[1];
else
return false;
}

}

$wp_auto_upload = new wp_auto_upload();
62 changes: 62 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
=== Auto Upload Images ===
Contributors: airani
Donate link: http://p30design.net/about/contact
Tags: upload, auto, image, automaticlly, images, admin, post
Requires at least: 2.7
Tested up to: 3.5.1
Stable tag: trunk
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

== Description ==

When you want to save post, this plugin search for images url which exists in post and automatically upload external images to wordpress upload directory and replace external link with your link.
Automatically add image to Media Library and attach to post.

<a href="http://p30design.net/1391/08/wp-auto-upload-images.html" title="plugin page" target="_blank">Official Plugin Page</a>

== Installation ==

Upload the "Auto Upload Images" plugin to your blog, Activate it.

1, 2, 3: You're done!

== Frequently Asked Questions ==

= Are there special settings for the plugin? =

No, Just Activate it :)

== Changelog ==

= 1.4.1 =

* Fixed Bug: Fixed tiny bug ;) Thanks to Ali for reporting bug

= 1.4 =

* New Feature: Work With Multi Address Sites
* Fixed Bug: Work with Persian & Arabic URLs
* Fixed Bug: Replace URL for images already been uploaded
* Implementation with object-oriented

= 1.3 =

* Fixed some bugs

= 1.2 =

* Fixed Bug: Save one revision post
* Fixed Bug: Fix pattern of urls
* Fixed Bug: Save file with same name
* Fixed Bug: More images with same urls in post
* Fixed Bug: Work with ssl urls

= 1.1 =

* Add image to Media Library and attach to post
* Fix a bug

= 1.0 =

* It's first version.

0 comments on commit 9884b87

Please sign in to comment.