• H2GD Part 38: WordPress 3.9(.1) and plugin_basename()

    It’s nearly a month since WordPress 3.9 was officially released. I finally upgraded my local version. Then things went wrong. Turns out I’d not tested my new oik-batch plugin on WordPress 3.9 after all.

    Background

    In order to report that my plugins are compatible with versions from 3.7 to 3.9.1, I’ve been

    • keeping my local development at 3.8.1
    • testing new plugin versions in alternative local environments, both single and multisite with 3.9.1 and 4.0-alpha
    • and crossing my fingers that the code still works on 3.7(.n)

    Upgrade local installation to WP 3.9.1

    I upgraded my primary development environment earlier today. But this has revealed a new problem. I’ve been caught out by a change to the plugin_basename() function.

    In version 3.8.3 this function was

    function plugin_basename($file) {
    $file = str_replace('\\','/',$file); // sanitize for Win32 installs
    $file = preg_replace('|/+|','/', $file); // remove any duplicate slash
    $plugin_dir = str_replace('\\','/',WP_PLUGIN_DIR); // sanitize for Win32 installs
    $plugin_dir = preg_replace('|/+|','/', $plugin_dir); // remove any duplicate slash
    $mu_plugin_dir = str_replace('\\','/',WPMU_PLUGIN_DIR); // sanitize for Win32 installs
    $mu_plugin_dir = preg_replace('|/+|','/', $mu_plugin_dir); // remove any duplicate slash
    $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir
    $file = trim($file, '/');
    return $file;
    }

    Pretty ugly but it suited my needs.

    The function has been changed to:

    function plugin_basename( $file ) {
    global $wp_plugin_paths;
    foreach ( $wp_plugin_paths as $dir => $realdir ) {
    if ( strpos( $file, $realdir ) === 0 ) {
    $file = $dir . substr( $file, strlen( $realdir ) );
    }
    }
    $file = wp_normalize_path( $file );
    $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
    $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );

    $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir
    $file = trim($file, '/');
    return $file;
    }

    This function is now producing the following message

    Warning: Invalid argument supplied for foreach() in C:\apache\htdocs\wordpress\wp-includes\plugin.php on line 622
    

    Explanation

    The function was changed to improve the handling of symlinked files.

    But it’s not working in my oik-batch plugin since  I haven’t included wp-settings.php

    So the global $wp_plugin_paths is not set.

    Solution

    Fortunately, I have a fix; oik-batch will have to simulate wp-settings.php

    function oik_batch_simulate_wp_settings() {
    $GLOBALS['wp_plugin_paths'] = array();
    }

    , , ,

    Published:

    Last updated:

    August 18, 2014

Categories

Tide times from tidetimes.co.uk

Tide Times & Heights for Northney on
Saturday, 16 October 2021

Tide times from tidetimes.org.uk

Tide Times & Heights for Northney on
16th October 2021
01:57 Low Tide ( 1.42m )
09:49 High Tide ( 3.97m )
14:29 Low Tide ( 1.48m )
22:17 High Tide ( 3.92m )