' . __('Settings', 'cache-images') . ''; $scaning_link = '' . __('Scanning', 'cache-images') . ''; $links = array_merge( array($settings_link, $scaning_link), $links); // before other links } return $links; } add_filter( 'plugin_action_links', 'cache_images_filter_plugin_actions', 10, 2 ); /** * Search a multiple array * @link http://www.php.net/manual/en/function.in-array.php#20594 */ function in_multi_array($needle, $haystack) { $in_multi_array = false; if(in_array($needle, $haystack)) { $in_multi_array = true; } else { for($i = 0; $i < sizeof($haystack); $i++) { if(is_array($haystack[$i])) { if(in_multi_array($needle, $haystack[$i])) { $in_multi_array = true; break; } } } } return $in_multi_array; } /** * Add Cache Images page under Tools */ function mm_ci_add_pages() { add_management_page( __( 'Cache Remote Images', 'cache-images' ), __( 'Cache Remote Images', 'cache-images' ), 8, __FILE__, 'mm_ci_manage_page' ); } function mm_ci_manage_page() { global $wpdb; ?>

  1. (Scan will search only for images that are hotlinked (ie. used like in example <img src="http://example.com/picture.jpg" />), while button Scan (including linked) will search for images that are only linked from this site (ie. ised like in example <a href="http://example.com/picture.jpg">example</a>). Use second button with caution!', 'cache-images' ); ?>)

read more about WP Smush.it)", "cache-images" ), "http://dialect.ca/code/wp-smushit/" );?>
Install WP Smush.it", "cache-images" ), esc_url(admin_url( 'plugin-install.php?tab=plugin-information&plugin=wp-smushit&TB_iframe=true&width=600&height=550' )));?>

()

get_results("SELECT DISTINCT ID FROM $wpdb->posts WHERE post_content LIKE ('%ID; $post = $wpdb->get_results("SELECT post_content FROM $wpdb->posts WHERE ID = '$postid'"); preg_match_all('||i', $post[0]->post_content, $matches); foreach ( $matches[1] as $url ) : if ( strstr( $url, get_option('siteurl') . '/' . get_option('upload_path') ) || !strstr( $url, $domain) || (($res) && in_multi_array($url, $res))) continue; // Already local // got tip from http://www.devcomments.com/Plus-signs-in-url-replaced-with-space-in-GET-at49448.htm $origurl = $url; $url = urlencode($url); $res[] = array('url' => $url, 'postid' => $postid, 'origurl' => $origurl); endforeach; } die( json_encode($res) ); } else if ($action == "getalllist") { $domain = $_POST["domain"]; $postid_list = $wpdb->get_results("SELECT DISTINCT ID FROM $wpdb->posts WHERE post_content LIKE ('%$domain%')"); foreach ( $postid_list as $v ) { $postid = $v->ID; $post = $wpdb->get_results("SELECT post_content FROM $wpdb->posts WHERE ID = '$postid'"); preg_match_all('#(http://[^\s]+(?=\.(jpe?g|png|gif)))#i', $post[0]->post_content, $matches); foreach ( $matches[1] as $url ) : if ( strstr( $post[0]->post_content, $url . '.jpg' ) ) { $url = $url . '.jpg'; } else if ( strstr( $post[0]->post_content, $url . '.jpeg' ) ) { $url = $url . '.jpeg'; } else if ( strstr( $post[0]->post_content, $url . '.png' ) ) { $url = $url . '.png'; } else if ( strstr( $post[0]->post_content, $url . '.gif' ) ) { $url = $url . '.gif'; } if ( strstr( $url, get_option('siteurl') . '/' . get_option('upload_path') ) || !strstr( $url, $domain) || (($res) && in_multi_array($url, $res))) continue; // Already local $origurl = $url; $url = urlencode($url); $res[] = array('url' => $url, 'postid' => $postid, 'origurl' => $origurl); endforeach; } die( json_encode($res) ); } else if ($action == "regen") { $url = $_POST["url"]; $postid = $_POST["postid"]; $url = cache_images_cache_image($url, $postid); die( $url ); } else if ($action == "getdomains") { $posts = $wpdb->get_results("SELECT post_content FROM $wpdb->posts WHERE post_content LIKE ('%post_content, $domains); endforeach; $local_domain = parse_url( get_option( 'siteurl' ) ); foreach ($domains as $domain => $num) : if ( strstr( $domain, $local_domain['host'] ) ) continue; // Already local $domain_md5 = md5( $domain ); $res[] = array('domain' => $domain, 'num' => $num, 'domainmd5' => $domain_md5); endforeach; die( json_encode($res) ); } else if ($action == "getalldomains") { $posts = $wpdb->get_results("SELECT post_content FROM $wpdb->posts WHERE post_content LIKE ('%post_content, $domains); endforeach; $local_domain = parse_url( get_option( 'siteurl' ) ); foreach ($domains as $domain => $num) : if ( strstr( $domain, $local_domain['host'] ) ) continue; // Already local $domain_md5 = md5( $domain ); $res[] = array('domain' => $domain, 'num' => $num, 'domainmd5' => $domain_md5); endforeach; if ( !$res ) die( __( "No posts with images were found.", "cache-images" ) ); die( json_encode($res) ); } } add_action('wp_ajax_cache_images', 'cache_images_ajax'); /** * Find hotlinked external images in provided content */ function cache_images_find_images($content, $domains) { preg_match_all('||i', $content, $matches); foreach ($matches[1] as $url) : $url = parse_url($url); $domains[$url['host']]++; endforeach; return $domains; } /** * Find all external images in provided content * Regex by BDuelz on StackOverflow * @link http://stackoverflow.com/questions/3371902/php-regex-get-image-from-url/3372785#3372785 */ function cache_images_find_all_images($content, $domains) { preg_match_all('#(http://[^\s]+(?=\.(jpe?g|png|gif)))#i', $content, $matches); foreach ($matches[1] as $url) : $url = parse_url($url); $domains[$url['host']]++; endforeach; return $domains; } /** * Cache image from provided URL * * Add image to media library and * replace all occurences of it */ function cache_images_cache_image($url, $postid) { global $wpdb; $orig_url = $url; //check if pic is on Blogger if (strpos($url, 'blogspot.com') || strpos($url, 'blogger.com') || strpos($url, 'ggpht.com') || strpos($url, 'googleusercontent.com') || strpos($url, 'gstatic.com')) { $response = wp_remote_request($url); if ( is_wp_error( $response ) ) return 'error1'; $my_body = wp_remote_retrieve_body($response); if (strpos($my_body, 'src')) { preg_match_all('||i', $my_body, $matches); foreach ( $matches[1] as $url ) : $spisak = $url; endforeach; $url = $spisak; } } set_time_limit( 300 ); require_once( ABSPATH . 'wp-admin/includes/media.php' ); require_once( ABSPATH . 'wp-admin/includes/file.php' ); $upload = media_sideload_image($url, $postid); if ( !is_wp_error($upload) ) { preg_match_all('||i', $upload, $locals); foreach ( $locals[1] as $newurl ) : $wpdb->query("UPDATE $wpdb->posts SET post_content = REPLACE(post_content, '$orig_url', '$newurl');"); endforeach; } return $url; } /** * Cache images on post's saving */ function cache_images_save_post($post_ID, $post) { global $wpdb; $cache_images_settings = get_option( 'cache_images_automatic_caching' ); if ( $cache_images_settings && $cache_images_settings == 1 ) { if ( wp_is_post_autosave($post_ID) || wp_is_post_revision($post_ID) ) return $post_ID; $domains = cache_images_find_images($post->post_content, $domains); if ( !$domains ) return $post_ID; $local_domain = parse_url( get_option( 'siteurl' ) ); foreach ($domains as $domain => $num) : if ( strstr( $domain, $local_domain['host'] ) ) continue; // Already local preg_match_all('||i', $post->post_content, $matches); foreach ( $matches[1] as $url ) : if ( strstr( $url, get_option('siteurl') . '/' . get_option('upload_path') ) || !strstr( $url, $domain) || (($res) && in_multi_array($url, $res))) continue; // Already local cache_images_cache_image($url, $post_ID); endforeach; endforeach; return $post_ID; } return $post_ID; } add_action( 'save_post', 'cache_images_save_post', 10, 2 ); /** * Add section & field on Media Settings page * @link http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/ * @link http://ottopress.com/2009/wordpress-settings-api-tutorial/ */ function cache_images_add_settings_field() { add_settings_section( 'cache_images_settings_section', __( 'Cache Images', 'cache-images' ), 'cache_images_section_callback', 'media' ); add_settings_field( 'cache_images_automatic_caching' , __( 'Automatic caching', 'cache-images' ) , 'cache_images_field_settings_form' , 'media' , 'cache_images_settings_section' ); register_setting( 'media', 'cache_images_automatic_caching' ); } function cache_images_section_callback() { } /** * Print field on Media Settings page * @link http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/ * @link http://ottopress.com/2009/wordpress-settings-api-tutorial/ */ function cache_images_field_settings_form() { ?>