8

我安装了 WordPress,并安装并激活了一些插件。但是我丢失了原始插件.zip文件。我想将一些插件安装到新的 WordPress 中。

不想迁移/移动/备份恢复整个当前的 WP 安装。我只想.zip为一些已安装的插件重新创建原始插件文件。我确实可以访问整个文件树以及数据库。有什么方法可以做到吗?

4

3 回答 3

8

是的,首先在插件操作链接中添加一个下载链接:

/**
 * Add action links to each plugin
 * @author brasofilo
 */
add_filter( 'plugin_action_links', function ( $plugin_meta, $plugin_file, $plugin_data, $status ) 
{
    $plugin_dir = dirname( $plugin_file );
    if( !empty( $plugin_dir ) && '.' !== $plugin_dir )
        $plugin_meta[] = sprintf( 
            "<a href='#' class='down-zip down-icon' data-file='%s' title='%s'></a>",
            $plugin_dir,
            'Download zip file for ' . $plugin_data['Name']
        );
    else
        $plugin_meta[] = "Root plugin, cannot zip";

    return $plugin_meta;
}, 10, 4 );

然后设置样式并附加 JS 动作:

/**
 * Style and actions for wp-admin/plugins.php
 * @author brasofilo
 */
add_action( 'admin_footer-plugins.php', function() {
    ?>
    <style>
    .down-icon:before { /* http://melchoyce.github.io/dashicons/ */
        content: "\f316";
        display: inline-block;
        -webkit-font-smoothing: antialiased;
        font: normal 20px/1 'dashicons';
        vertical-align: top;
    }
    </style>
    <script>
    root_wp = '<?php echo WP_PLUGIN_DIR; ?>' + '/';

    /**
     * Virtual $_POST
     * creates form, appends and submits
     *
     * @author https://stackoverflow.com/a/9815335/1287812
     */
    function b5f_submit_form_post( path, params, method ) 
    {
        $ = jQuery;
        method = method || "post"; // Set method to post by default, if not specified.

        var form = $(document.createElement( "form" ))
            .attr( {"method": method, "action": path} );

        $.each( params, function(key,value)
        {
            $.each( value instanceof Array? value : [value], function(i,val)
            {
                $(document.createElement("input"))
                    .attr({ "type": "hidden", "name": key, "value": val })
                    .appendTo( form );
            }); 
        }); 

        form.appendTo( document.body ).submit();
    }

    jQuery(document).ready(function($) 
    {   
        /**
         * Fire a plugin download
         */
        $("a.down-zip").click(function() 
        {  
            event.preventDefault();

            b5f_submit_form_post( '', { 
                action: 'zip_a_plugin',
                plugin_to_zip: root_wp + $(this).data('file'), 
                plugin_name: $(this).data('file')
            });
        });
    });            
    </script>
    <?php
});

捕获自定义$_POSTed 数据并将插件目录处理为 zip:

/**
 * Dispatch $_POST['action']=>'zip_a_plugin' custom action
 * @author brasofilo https://stackoverflow.com/a/23546276/1287812
 */ 
add_action('admin_action_zip_a_plugin', function() 
{
    if( empty( $_REQUEST['plugin_to_zip'] ) )
        return;

    zipFile( $_REQUEST['plugin_to_zip'], $_REQUEST['plugin_name'], false );
});

最后,使用Stack 上的压缩功能

/**
 * Makes zip from folder
 * @author https://stackoverflow.com/a/17585672/1287812
 */
function zipFile($source, $destination, $flag = '')
{
    if ( !extension_loaded('zip') ) {
        return false;
    }

    $zip = new ZipArchive();
    $tmp_file = tempnam(WP_CONTENT_DIR,'');
    if (!$zip->open($tmp_file, ZIPARCHIVE::CREATE)) {
        return false;
    }

    $source = str_replace('\\', '/', realpath($source));
    if($flag)
    {
        $flag = basename($source) . '/';
        //$zip->addEmptyDir(basename($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)
            {
                $src = str_replace($source . '/', '', $flag.$file . '/');
                if( WP_PLUGIN_DIR.'/' !== $src ) # Workaround, as it was creating a strange empty folder like /www_dev/dev.plugins/wp-content/plugins/
                    $zip->addEmptyDir( $src );
            }
            else if (is_file($file) === true)
            {
                $src = str_replace($source . '/', '', $flag.$file);
                $zip->addFromString( $src, file_get_contents($file));
            }
        }
    }
    else if (is_file($source) === true)
    {
        $zip->addFromString($flag.basename($source), file_get_contents($source));
    }

    $tt = $zip->close();
    if(file_exists($tmp_file))
    {
        // push to download the zip
        header('Content-type: application/zip');
        header('Content-Disposition: attachment; filename="'.$destination.'"');
        readfile($tmp_file);
        // remove zip file is exists in temp path
        exit();
    } 
    else {
        echo $tt;
        die();
    }
}
于 2014-06-11T15:52:38.263 回答
5

有趣的是,从现有插件创建 zip 文件实际上很简单。

只需创建一个包含插件文件夹的 zip 文件。执行此操作的 unix 命令是:

$ cd wp-content/plugins
$ zip -r my-plugin.zip my-plugin

然后,您可以下载生成的 my-plugin.zip 文件,然后通过上传到新站点(即:WP Admin -> Plugins -> Add New -> Upload)在 WordPress 插件安装中使用该文件。

显然,zip 文件不会包含任何数据库表/mod,但大多数插件会在安装时对此进行测试,并在安装时执行任何所需的数据库升级。不幸的是,如果不测试或检查插件源代码,或两者兼而有之,就无法知道这是否会成为问题。

于 2014-03-13T01:56:51.763 回答
1

最简单、无代码的方法是使用另一个插件,例如WordPress Downloader

于 2021-09-03T07:20:55.377 回答