0

I am currently trying to detect when an administrator uploads (and installs) a theme or plugin.

The code I wrote is very fragile because it depends on the chance that the plugin/theme's name is the same as the uploaded zip file (which is rarely the case).

function EventFileUploaded($attachmentID){
    if(isset($_REQUEST['action'])){
        $file = get_attached_file($attachmentID);
        switch($_REQUEST['action']){
            case 'upload-theme':
                HandleThemeUpload($file);
                break;
            case 'upload-plugin':
                HandlePluginUpload($file);
                break;
        }
    }
}
add_action('delete_attachment', 'EventFileUploaded');

function HandleThemeUpload($fileName){
    $themes = wp_get_themes();
    // TODO somehow find which theme in $themes was installed from $fileName
}

function HandlePluginUpload($fileName){
    $plugins = get_plugins();
    // TODO somehow find which plugin in $plugins was installed from $fileName
}

I basically need to fill up the two TODOs marked above. This is a sample of the code I was using:

$fileName = explode('.', basename($fileName));
foreach(get_plugins() as $pluginFile => $plugin){
    if(strtolower(str_replace(' ', '-', $plugin['Name'])) == $fileName){
        $newPlugin = $plugin;
        break;
    }
}
// use $newPlugin if set

Of course the code above fails in some situations.

PS: I wasn't sure what tags to use, feel free to suggest better ones.

1 Answer 1

1

I think your best bet here is to hook into the flow at the time of uploading and processing the uploaded file.

There is a filter (line 327, file.php):

apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ), 'upload' );

You could create a filter within which you do nothing else but create a global variables to hold the current values for:

wp_get_themes();
wp_get_plugins();

then, within your existing functions

HandleThemeUpload() { }
HandlePluginUpload() { }

you get the themes and plugins again and then compare these results with your previous global results using array_diff(): e.g.

$NewPlugin = array_diff( $LatestPlugins, $OldGlobalPlugins );

Then you have the new plugins / themes.

Hope that helps.

2
  • Hmm, I was thinking about doing something on those lines. Didn't know the exact hooks to use though. Thanks!
    – Christian
    May 1, 2014 at 21:00
  • No worries... you'll really want to have the WordPress source code handy to dig through. It's a maze, but you'll eventually get the hang of it :)
    – Paul G.
    May 2, 2014 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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