问题描述

为了避免与其他插件冲突,应该使用唯一前缀的所有全局函数,操作和插件前缀,例如:

function xyz_function_name() { ... }

问题是,如何验证 xyz 确实是独一无二的?例如,Yoast SEO 使用 wpseo_,我可以想象其他 SEO 插件也可以轻松使用。搜索可用的 WordPress 插件潜在的冲突的最佳方式是什么?还是在那里?

最佳解决方案

您可以使用 Mark Jaquith 的 WordPres Plugin Directory Slurper shell 脚本从 WordPress.org repo 下载所有插件的最新版本。一旦插件被下载,你可以 grep 你想检查的插件/钩子前缀,例如:

grep -r --include=*.php 'wpseo_' ./

将 WordPres Plugin Directory Slurper 软件包解压缩到文档根目录。默认目录名为 WordPress-Plugin-Directory-Slurper,它包含:

  /plugins/
  /readmes/
  /zips/
  LICENSE
  README.markdown
  update

WordPress-Plugin-Directory-Slurper 目录中执行 php update 运行 bash 脚本。拉链插件将下载到/zips 并提取到/plugins 。整个售后服务在 15GB 左右,首次下载需要几个小时。

update 脚本的内容:

#!/usr/bin/php
<?php
$args = $argv;
$cmd = array_shift( $args );

$type = 'all';
if ( !empty( $args[0] ) ) {
    $type = $args[0];
}

switch ( $type ) {
    case 'readme':
        $directory = 'readmes';
        $download = 'readmes/%s.readme';
        $url = 'http://plugins.svn.wordpress.org/%s/trunk/readme.txt';
        break;
    case 'all':
        $directory = 'plugins';
        $download = 'zips/%s.zip';
        $url = 'http://downloads.wordpress.org/plugin/%s.latest-stable.zip?nostats=1';
        break;
    default:
        echo $cmd . ": invalid commandrn";
        echo 'Usage: php ' . $cmd . " [command]rnrn";
        echo "Available commands:rn";
        echo "  all - Downloads full plugin zipsrn";
        echo "  readme - Downloads plugin readmes onlyrn";
        die();
}

echo "Determining most recent SVN revision...rn";
try {
    $changelog = @file_get_contents( 'http://plugins.trac.wordpress.org/log/?format=changelog&stop_rev=HEAD' );
    if ( !$changelog )
        throw new Exception( 'Could not fetch the SVN changelog' );
    preg_match( '#[([0-9]+)]#', $changelog, $matches );
    if ( !$matches[1] )
        throw new Exception( 'Could not determine most recent revision.' );
} catch ( Exception $e ) {
    die( $e->getMessage() . "rn" );
}
$svn_last_revision = (int) $matches[1];
echo "Most recent SVN revision: " . $svn_last_revision . "rn";
if ( file_exists( $directory . '/.last-revision' ) ) {
    $last_revision = (int) file_get_contents( $directory . '/.last-revision' );
    echo "Last synced revision: " . $last_revision . "rn";
} else {
    $last_revision = false;
    echo "You have not yet performed a successful sync. Settle in. This will take a while.rn";
}

$start_time = time();

if ( $last_revision != $svn_last_revision ) {
    if ( $last_revision ) {
        $changelog_url = sprintf( 'http://plugins.trac.wordpress.org/log/?verbose=on&mode=follow_copy&format=changelog&rev=%d&limit=%d', $svn_last_revision, $svn_last_revision - $last_revision );
        $changes = file_get_contents( $changelog_url );
        preg_match_all( '#^' . "t" . '** ([^/A-Z ]+)[ /].* ((added|modified|deleted|moved|copied))' . "n" . '#m', $changes, $matches );
        $plugins = array_unique( $matches[1] );
    } else {
        $plugins = file_get_contents( 'http://svn.wp-plugins.org/' );
        preg_match_all( '#<li><a href="http://([/%5D+)/">([^/]+)/</a></li>#', $plugins, $matches );
        $plugins = $matches[1];
    }

    foreach ( $plugins as $plugin ) {
        $plugin = urldecode( $plugin );
        echo "Updating " . $plugin;

        $output = null; $return = null;
        exec( 'wget -q -np -O ' . escapeshellarg( sprintf($download, $plugin) ) . ' ' . escapeshellarg( sprintf($url, $plugin) ) . ' > /dev/null', $output, $return );

        if ( $return === 0 && file_exists( sprintf($download, $plugin) ) ) {
            if ($type === 'all') {
                if ( file_exists( 'plugins/' . $plugin ) )
                    exec( 'rm -rf ' . escapeshellarg( 'plugins/' . $plugin ) );

                exec( 'unzip -o -d plugins ' . escapeshellarg( 'zips/' . $plugin . '.zip' ) );
                exec( 'rm -rf ' . escapeshellarg( 'zips/' . $plugin . '.zip' ) );
            }
        } else {
            echo '... download failed.';
        }
        echo "rn";
    }

    if ( file_put_contents( $directory . '/.last-revision', $svn_last_revision ) )
        echo "[CLEANUP] Updated $directory/.last-revision to " . $svn_last_revision . "rn";
    else
        echo "[ERROR] Could not update $directory/.last-revision to " . $svn_last_revision . "rn";
}

$end_time = time();
$minutes = ( $end_time - $start_time ) / 60;
$seconds = ( $end_time - $start_time ) % 60;

echo "[SUCCESS] Done updating plugins!rn";
echo "It took " . number_format($minutes) . " minute" . ( $minutes == 1 ? '' : 's' ) . " and " . $seconds . " second" . ( $seconds == 1 ? '' : 's' ) . " to update ". count($plugins)  ." plugin" . ( count($plugins) == 1 ? '' : 's') . "rn";
echo "[DONE]rn";

如果您想下载所有最新的批准主题,还有一个脚本:Aaron Jorbin 的 WordPress Theme Directory Slurper

这些 shell 脚本是为 Unix 系统而设计的。如果您使用的是 Windows,则可以使用 cygwin 运行 Plugin /Theme Directory Slurper 脚本。

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。