11

To avoid collisions with other plugins, one should prefix all global functions, actions and plugins with a unique prefix, e.g.:

function xyz_function_name() { ... }

The question is, how do I verify that xyz is indeed unique? For instance, Yoast SEO uses wpseo_ which I can imagine other SEO plugin could easily use as well. What's the best way to search the available WordPress plugins for potential collisions? Or is there?

  • 4
    Prefixes are a thing of the past. Nowadays we use namespaces, and you can nest those as deep as you need. – fuxia Oct 14 '16 at 19:32
  • I'll update the question to include actions and filters which are global and cannot be prefixed using classes. – Borek Bernard Oct 14 '16 at 20:10
  • with that update, this is a very good question – prosti Oct 14 '16 at 20:53
  • 1
    I up voted this because I think the answer is going to be hard. But I really don't think that this is neccesary since there is potentially an infinite number of combinations of prefixes and function names. I think the real solution is to be more detailed in the function name. Also, and maybe overkill, but a postfix could be added. – stims Oct 14 '16 at 21:28
5

You can use the WordPres Plugin Directory Slurper shell script by Mark Jaquith to download the the most recent version of all plugins from the WordPress.org repo. Once the plugins have been downloaded, you can grep for the plugin/hook prefix you want to check, e.g.:

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

Unzip the WordPres Plugin Directory Slurper package to to your document root. The default directory name is WordPress-Plugin-Directory-Slurper and it contains:

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

Run the bash script by executing php update from within the WordPress-Plugin-Directory-Slurper directory. Zipped plugins will be downloaded to /zips and extracted to /plugins. The entire repo is somewhere around 15GB and will take several hours to download the first time.

The contents of the update script:

#!/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";

If you'd like to download all of the most recent approved themes, there's a script for that too: WordPress Theme Directory Slurper by Aaron Jorbin.

These shell scripts are designed for a Unix system. If you're using Windows, you can run the Plugin/Theme Directory Slurper scripts using cygwin.

0
  1. don't be generic, use some variation of your name.
  2. No one that installs new plugins uses PHP 5.2 anymore (oct 2016), just use PHP namespace, and make it something long but relevant like the plugin name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.