邮编下载ZIP更改Wordpress主题的文件夹名称 [英] Zip Download ZIP Changes Folder Name of Wordpress Theme

查看:174
本文介绍了邮编下载ZIP更改Wordpress主题的文件夹名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图解决这个问题,但似乎无法找到任何东西。



当你从github上下载ZIP时,它并不会给你所希望的 theme_name.zip ,而是 theme_name-master.zip ,当普通用户安装它时,乱七八糟的文件夹名称会抛出子主题。



如何可以这是可以解决的,所以zip下载不会改变任何东西?


该脚本会在没有 -master 的情况下重命名该文件夹,然后重新压缩该项目,然后将其作为下载文件发送给用户。



因此,类似于(需要PHP5> = 5.2.0, cURL < a>, ZipArchive ,safe_mode& open_basedir off strong>):

F从GitHub购买它; p

 <?php 
//示例用法
new GitDL('https://github.com/lcherone/GitDL');

/ **
* GitHub Project / Repository Downloader代理。
*该课程将处理下载,删除主文件夹前缀,
*重新包装并代理项目作为下载。
*
* @author Lawrence Cherone
* @version 0.2
* /
class GitDL {
//工程文件工作目录 - 自动创建
const PWD =./project_files/;

/ **
*类构造。
*
* @param string $ url
* /
函数__construct($ url = null){
//检查构造参数
if(! $ url)die('Class Error:Missing construct argument:$ url');

//修正尾部斜线(如果有的话)
$ url = rtrim($ url,'/');

//赋予类属性
$ this-> project = basename($ url);
$ this-> project_url = $ url。'/ archive / master.zip';
$ this-> tmp_file = md5($ url)。'。zip';

// make project working folder
if(!file_exists(self :: PWD)){
mkdir(self :: PWD.md5($ url),0775,true );
}

//从GitHub获取项目zip
尝试{
$ this-> get_project();
} catch(Exception $ e){
die($ e-> getMessage());
}

//从git
$解压缩项目zip(this ::> extract(self :: PWD。$ this-> tmp_file,self :: PWD.md5 $ URL));

//通过重命名
rename(self :: PWD.md5($ url)。'/'。$ this->项目。 。自:: PWD.md5($网址) '/' $这个 - >项目);

// rezip项目文件
$ this-> zipcreate(self :: PWD.md5($ url),self :: PWD.'new _'。$ this-> tmp_file );

//将zip发送给用户
header('Content-Description:File Transfer');
header('Content-Type:application / zip');
header('Content-Disposition:attachment; filename =''。$ this-> project。'。zip'');
header('Content-Transfer-Encoding:binary');
header('Expires:0');
header('Cache-Control:must-revalidate,post-check = 0,pre-check = 0');
header('Pragma:public');
header('Content-Length:'.sprintf(%u,filesize(self :: PWD.'new _'。$ this-> tmp_file)));
readfile(self :: PWD.'new _'。$ this-> tmp_file);

//清理
$ this-> destroy_dir(self :: PWD.md5($ url));
unlink(self :: PWD。$ this-> tmp_file);
unlink(self :: PWD.'new _'。$ this-> tmp_file);
}

/ **
* cURL GitHub项目下载器。
*不支持开放的基本目录/安全模式,因为有一个GitHub重定向到那里CDN
* a HEAD预检查完成检查项目是否存在,
*项目zip直接写入到文件。
* /
函数get_project(){
//检查卷曲安装
if(!function_exists('curl_init')){
抛出新异常('cURL Error :你必须安装cURL才能使用这个类。');
}
//检查不支持的设置
if(ini_get('open_basedir')!=''|| ini_get('safe_mode')=='On'){
抛出新的异常('cURL Error:safe_mode或open_basedir已启用,类不支持');
}

// HEAD请求 - 验证项目是否存在
$ ch = curl_init();
curl_setopt_array($ ch,array(
CURLOPT_URL => $ this-> project_url,
CURLOPT_TIMEOUT => 5,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_FAILONERROR => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_NOBODY => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
));如果(curl_exec($ ch)!== false){
$ fp = fopen(self :: PWD。$ this-> tmp_file ,'a + b');
if(flock($ fp,LOCK_EX | LOCK_NB)){
//清空可能的内容
ftruncate($ fp,0);
倒带($ fp);

// HTTP GET请求 - 直接写入文件
$ ch = curl_init();
curl_setopt_array($ ch,array(
CURLOPT_URL => $ this-> project_url,
CURLOPT_TIMEOUT => 5,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_FAILONERROR => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FILE => $ fp,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
));

转移失败
if(curl_exec($ ch)=== false){
ftruncate($ fp,0);
抛出新的异常('cURL Error:transfer failed。');
}
fflush($ fp);
flock($ fp,LOCK_UN);
curl_close($ ch);
}
fclose($ fp);
} else {
curl_close($ ch);
抛出新的异常('Error:'.htmlentities($ this-> project)'项目在GitHub上找不到');
}
}

/ **
*从提取/固定项目中创建zip。
*
* @uses ZipArchive
* @uses RecursiveIteratorIterator
* @param string $ source
* @param string $ destination
@return bool
* /
函数zipcreate($ source,$ destination){
if(!extension_loaded('zip')||!file_exists($ source)){
return false;
}
$ zip = new ZipArchive();
if(!$ zip-> open($ destination,ZIPARCHIVE :: CREATE)){
return false;
}
$ source = str_replace('\\','/',realpath($ source));
if(is_dir($ source)=== true){
$ files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($ source),RecursiveIteratorIterator :: SELF_FIRST);
foreach($ files as $ file){
$ file = str_replace('\\','/',realpath($ file));
if(is_dir($ file)=== true){
$ zip-> addEmptyDir(str_replace($ source。'/','',$ file。'/'));
} else if(is_file($ file)=== true){
$ zip-> addFromString(str_replace($ source。'/','',$ file),file_get_contents($ file ));
}
}
}
return $ zip-> close();
}

/ **
*提取Zip文件
*
* @uses ZipArchive
* @param string $ source
* @param string $ destination
* @return bool
* /
函数解压缩($ source,$ destination){
$ zip = new ZipArchive;
if($ zip-> open($ source)=== TRUE){
$ zip-> extractTo($ destination);
$ zip-> close();
返回true;
} else {
return false;


$ b $ **
递归目录移除器/删除器
*
* @uses RecursiveIteratorIterator
* @参数字符串$ dir
* @return bool
* /
函数destroy_dir($ dir){
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($ dir,FilesystemIterator :: SKIP_DOTS), RecursiveIteratorIterator :: CHILD_FIRST)作为$ path){
$ path-> isFile()? unlink($ path-> getPathname()):rmdir($ path-> getPathname());
}
return rmdir($ dir);
}

}
?>


I've been trying to solve this but can't seem to find anything on it.

When you "Download ZIP" from github, it doesn't give you the "theme_name.zip" that you'd hope for, but rather "theme_name-master.zip", which when average users install this the messed up folder name throws off child themes.

How can this be remedied, so that the zip download does not change anything?

解决方案

For simplicity, You could host a script somewhere that downloads the script, renames the folder without the -master and then rezip's the project, then send it to the user as a download.

So something like (Requires PHP5 >= 5.2.0, cURL, ZipArchive, safe_mode & open_basedir off):

Fork it from GitHub ;p

<?php
//Example Usage
new GitDL('https://github.com/lcherone/GitDL');

/**
 * GitHub Project/Repository Downloader proxy.
 * This class will handle downloading, removing master folder prefix, 
 * repacking and proxying back the project as a download.
 * 
 * @author Lawrence Cherone
 * @version 0.2
 */
class GitDL{
    // project files working directory - automatically created
    const PWD = "./project_files/";

    /**
     * Class construct.
     *
     * @param string $url
     */
    function __construct($url=null){
        // check construct argument
        if(!$url) die('Class Error: Missing construct argument: $url');

        // fix trailing slash if any
        $url = rtrim($url, '/');

        // assign class properties
        $this->project     = basename($url);
        $this->project_url = $url.'/archive/master.zip';
        $this->tmp_file    = md5($url).'.zip';

        // make project working folder
        if(!file_exists(self::PWD)){
            mkdir(self::PWD.md5($url), 0775, true);
        }

        // get project zip from GitHub
        try{
            $this->get_project();
        }catch(Exception $e){
            die($e->getMessage());
        }

        // extract project zip from git
        $this->extract(self::PWD.$this->tmp_file, self::PWD.md5($url));

        // remove the master part, by renaming
        rename(self::PWD.md5($url).'/'.$this->project.'-master', self::PWD.md5($url).'/'.$this->project);

        // rezip project files
        $this->zipcreate(self::PWD.md5($url), self::PWD.'new_'.$this->tmp_file);

        // send zip to user
        header('Content-Description: File Transfer');
        header('Content-Type: application/zip');
        header('Content-Disposition: attachment; filename="'.$this->project.'.zip"');
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: '.sprintf("%u", filesize(self::PWD.'new_'.$this->tmp_file)));
        readfile(self::PWD.'new_'.$this->tmp_file);

        // cleanup
        $this->destroy_dir(self::PWD.md5($url));
        unlink(self::PWD.$this->tmp_file);
        unlink(self::PWD.'new_'.$this->tmp_file);
    }

    /**
     * cURL GitHub project downloader. 
     * No support for open base dir/safe mode as there is a GitHub redirect to there CDN
     * a HEAD pre-check is done to check project exists,
     * project zip is written directly to the file.
     */
    function get_project(){
        // check curl installed
        if(!function_exists('curl_init')){
            throw new Exception('cURL Error: You must have cURL installed to use this class.');
        }
        // check for unsupported settings
        if (ini_get('open_basedir') != '' || ini_get('safe_mode') == 'On'){
            throw new Exception('cURL Error: safe_mode or an open_basedir is enabled, class not supported.');
        }

        // HEAD request - To verify the project exists
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => $this->project_url,
            CURLOPT_TIMEOUT => 5,
            CURLOPT_CONNECTTIMEOUT => 5,
            CURLOPT_FAILONERROR => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_BINARYTRANSFER => true,
            CURLOPT_HEADER => false,
            CURLOPT_NOBODY => true,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_SSL_VERIFYPEER => false,
        ));

        // lets grab it
        if(curl_exec($ch) !== false){
            $fp = fopen(self::PWD.$this->tmp_file, 'a+b');
            if(flock($fp, LOCK_EX | LOCK_NB)){
                // empty possible contents
                ftruncate($fp, 0);
                rewind($fp);

                // HTTP GET request - write directly to the file
                $ch = curl_init();
                curl_setopt_array($ch, array(
                    CURLOPT_URL => $this->project_url,
                    CURLOPT_TIMEOUT => 5,
                    CURLOPT_CONNECTTIMEOUT => 5,
                    CURLOPT_FAILONERROR => true,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_BINARYTRANSFER => true,
                    CURLOPT_HEADER => false,
                    CURLOPT_FILE => $fp,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_SSL_VERIFYHOST => false,
                    CURLOPT_SSL_VERIFYPEER => false,
                ));

                // transfer failed
                if(curl_exec($ch) === false){
                    ftruncate($fp, 0);
                    throw new Exception('cURL Error: transfer failed.');
                }
                fflush($fp);
                flock($fp, LOCK_UN);
                curl_close($ch);
            }
            fclose($fp);
        }else{
            curl_close($ch);
            throw new Exception('Error: '.htmlentities($this->project).' project not found on GitHub');
        }
    }

    /**
     * Create zip from extracted/fixed project.
     *
     * @uses ZipArchive
     * @uses RecursiveIteratorIterator
     * @param string $source
     * @param string $destination
     * @return bool
     */
    function zipcreate($source, $destination) {
        if (!extension_loaded('zip') || !file_exists($source)) {
            return false;
        }
        $zip = new ZipArchive();
        if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
            return false;
        }
        $source = str_replace('\\', '/', realpath($source));
        if (is_dir($source) === true) {
            $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
            foreach ($files as $file) {
                $file = str_replace('\\', '/', realpath($file));
                if (is_dir($file) === true) {
                    $zip->addEmptyDir(str_replace($source.'/', '', $file.'/'));
                } else if (is_file($file) === true) {
                    $zip->addFromString(str_replace($source.'/', '', $file), file_get_contents($file));
                }
            }
        }
        return $zip->close();
    }

    /**
     * Extract Zip file
     *
     * @uses ZipArchive
     * @param string $source
     * @param string $destination
     * @return bool
     */
    function extract($source, $destination){
        $zip = new ZipArchive;
        if($zip->open($source) === TRUE) {
            $zip->extractTo($destination);
            $zip->close();
            return true;
        } else {
            return false;
        }
    }

    /**
     * Recursive directory remover/deleter
     *
     * @uses RecursiveIteratorIterator
     * @param string $dir
     * @return bool
     */
    function destroy_dir($dir) {
        foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $path) {
            $path->isFile() ? unlink($path->getPathname()) : rmdir($path->getPathname());
        }
        return rmdir($dir);
    }

}
?>

这篇关于邮编下载ZIP更改Wordpress主题的文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆