如何检查插件前缀的唯一性?


11

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

function xyz_function_name() { ... }

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


4
前缀已成为过去。如今,我们使用名称空间,您可以根据需要嵌套它们。
fuxia

我将更新问题以包括全局的且不能使用类作为前缀的操作和过滤器。
Borek Bernard

有了这个更新,这是一个很好的问题
prosti

1
我对此投了赞成票,因为我认为答案将很难。但是我真的不认为这是必须的,因为前缀和函数名称的组合可能有无数个。我认为真正的解决方案是在函数名称中更详细地说明。此外,也许矫kill过正,但可以添加一个后缀。
2016年

Answers:


5

您可以使用Mark Jaquith 的WordPres插件目录Slurper shell脚本从WordPress.org存储库中下载所有插件的最新版本。插件下载完成后,您可以grep输入要检查的插件/挂钩前缀,例如:

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

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

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

通过php updateWordPress-Plugin-Directory-Slurper目录中执行来运行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 command\r\n";
        echo 'Usage: php ' . $cmd . " [command]\r\n\r\n";
        echo "Available commands:\r\n";
        echo "  all - Downloads full plugin zips\r\n";
        echo "  readme - Downloads plugin readmes only\r\n";
        die();
}

echo "Determining most recent SVN revision...\r\n";
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() . "\r\n" );
}
$svn_last_revision = (int) $matches[1];
echo "Most recent SVN revision: " . $svn_last_revision . "\r\n";
if ( file_exists( $directory . '/.last-revision' ) ) {
    $last_revision = (int) file_get_contents( $directory . '/.last-revision' );
    echo "Last synced revision: " . $last_revision . "\r\n";
} else {
    $last_revision = false;
    echo "You have not yet performed a successful sync. Settle in. This will take a while.\r\n";
}

$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="([^/]+)/">([^/]+)/</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 "\r\n";
    }

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

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

echo "[SUCCESS] Done updating plugins!\r\n";
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') . "\r\n";
echo "[DONE]\r\n";

如果您想下载所有最近批准的主题,那么也有一个脚本:Aaron Jorbin撰写的WordPress主题目录 Slurper。

这些shell脚本是为Unix系统设计的。如果使用Windows,则可以使用cygwin运行“插件/主题目录Slurper”脚本。


0
  1. 不要通用,请使用您的名字的一些变体。
  2. 没有一个安装新插件的人再使用PHP 5.2(2016年10月),仅使用PHP名称空间,并将其命名为冗长但相关的名称,例如插件名称。
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.