wwwurl_len = $pos + 1; $this->wwwurl = substr($wpurl, 0, $this->wwwurl_len); $this->abs_wpurl = substr($wpurl, $this->wwwurl_len); $this->wwwpath = str_replace('\\', '/', ABSPATH); $this->wwwpath = substr($this->wwwpath, 0, strlen($this->wwwpath) - strlen($this->abs_wpurl)); $this->abs_resources_path = $this->abs_wpurl . 'wp-content/wp-resources-back/'; $this->resources_path = $this->wwwpath . $this->abs_resources_path; $this->resources_url = $this->wwwurl . $this->abs_resources_path; $this->check_version(); } function optimize_style_script_url($src) { //echo "src=$src
"; if(substr($src, 0, $this->wwwurl_len) != $this->wwwurl) { return $src; } $abs_src = substr($this->ignore_query_string($src), $this->wwwurl_len); $source_path = $this->wwwpath . $abs_src; //echo "source_path=$source_path
"; if(!file_exists($source_path)) return $src; $mtime = filemtime($source_path); if(FALSE === $mtime) return $src; $mtime_path = $this->resources_path . $mtime . '/'; $to_path = $mtime_path . $abs_src; //echo "to_path = $to_path
"; if(!file_exists($to_path)) { if(substr($to_path, strlen($to_path) - 4) === '.css') { if(!$this->csssprit_copy($source_path, $abs_src, $mtime_path)) { //echo "false csssprite_copy
"; return $src; } } if(!$this->file_copy($source_path, $to_path)) return $src; } //$src = str_replace(get_home_url(null, '/'), 'http://static.litefeel.com/', $src); //echo "false file_copy
"; return $this->resources_url . $mtime . '/' . $abs_src; } function csssprit_copy(&$source_path, &$abs_source_path, &$to_dir) { $content = file_get_contents($source_path); $result = preg_match_all('/url\([\'"]?([^\)]+[\'"]?)\)/', $content, $out, PREG_PATTERN_ORDER); if($result === FALSE) return FALSE; if($result === 0) return TRUE; //print_r($out[1]); $arr = $out[1]; $abs_source_dir = dirname($abs_source_path) . '/'; foreach ($arr as $file) { if(strpos($file, 'http://') === 0 || strpos($file, 'https://') === 0 || strpos($file, '/') === 0) { continue; } $file = $this->ignore_query_string($file); $abs_from = $this->absolute_path($abs_source_dir . $file); if(!$abs_from) continue; $from = $this->wwwpath . $abs_from; $to = $to_dir . $abs_from; //echo "copy css sprite img :$from -> $to
"; if(!$this->file_copy($from, $to)) return FALSE; } return TRUE; } function file_copy(&$from, &$to) { if(!file_exists($from)) return TRUE; //echo "copy file $from
"; $to_dir = dirname($to) ; if(!file_exists($to_dir)) { if(!mkdir($to_dir, 0755, true)) return FALSE; } return copy($from, $to); } function img_copy(&$from, &$to) { if(!file_exists($from)) return TRUE; $to_dir = dirname($to) ; if(!file_exists($to_dir)) { if(!mkdir($to_dir, 0755, true)) return FALSE; } // 测试文件是否是同一个,通常修改一个图片文件后文件大小会改变. // 不能检测文件修改日期,因为mtime目录里的文件修改日期一定不等于文件修改日期 if(file_exists($to) && filesize($from) == filesize($to)) { return TRUE; } return copy($from, $to); } function absolute_path($path) { $list = explode('/', $path); $len = count($list); for($i = 0; $i < $len; $i++) { $del = 0; if($list[$i] === '' || $list[$i] === '.') $del = 1; else if($list[$i] === '..') $del = 2; if($del != 0) { if($i < $del - 1) return false; $i -= $del; $len -= $del; array_splice($list, $i + 1, $del); } } $path = implode('/', $list); return $path; } function ignore_query_string($src) { $pos = strpos($src, '?'); if($pos !== FALSE) { $src = substr($src, 0, $pos); } return $src; } function check_version() { if(!file_exists($this->resources_path)) { mkdir($this->resources_path, 0755, TRUE); } $file = $this->resources_path . $this->version; if(!file_exists($file)) { $this->del_tree($this->resources_path, TRUE); file_put_contents($file, $this->version); } } function del_tree($dir, $ignore_this = FALSE) { $files = array_diff(scandir($dir), array('.','..')); foreach ($files as $file) { $tmp = $dir . '/' . $file; (is_dir($tmp)) ? $this->del_tree($tmp) : unlink($tmp); } if($ignore_this) return TRUE; return rmdir($dir); } } $wp_resources_URL_optimization = new WP_Resources_URL_Optimization(); } ?>