__autoload in a plugin throws an error in WP – not able to locate translation_entry.class.php

Ok, I’m going to try to explain this as simple as possible.

I’m trying to modify a plugin called smart-lencioni-image-resizer v2.0. to be used in WP-Multisite.

Read More

I’m trying to display an image through SLIR using the following URL:

http://subsite.mainsite.no/wp-content/plugins/wp-filebrowser/slir/?w=90&h=90&c=1:1&i=http://bekkelaget.stiengenterprises.no/files/seniorsenter/DSC_3535.jpg

I’ve done this successfully in v.1.4.2. But v2.0 is using a different method for including files. The index.php file looks like this:

function __autoload($className)
{
  var_dump($className);  
    require_once strtolower($className) . '.class.php';
}

new SLIR();

Now, where ever I try to include WP header:

require_once($_SERVER['DOCUMENT_ROOT'].'/wp-blog-header.php');

I get the following error message:

*Warning: require_once(translation_entry.class.php) [function.require-once]: failed to open stream: No such file or directory in I:Developmentwampwwwmysitewp-contentpluginswp-filebrowserslirindex.php on line 43*

Line 43 in the index.php is:

require_once strtolower($className) . '.class.php';

For some reason the __autoload function makes WP look for files in the SLIR directory. This is of cours wrong 🙁

UPDATE
I found one solution on the web that kind of works:

function __autoload($className)
{
  if(file_exists(strtolower($className) . '.class.php')) {
    require_once strtolower($className) . '.class.php';
  }
}

This allows WP header file to be loaded. But for some reason it loads mainsite.no and drops executing the URL I added at the top.

Is there any other way I can solve this problem?

UPDATE2

There is an .htaccess file in the SLIR folder. The file looks like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule . index.php [L]
</IfModule>

# Prevent viewing of the error log file in its default location
<Files slir-error-log>
Order Deny,Allow
Deny from All
</Files>

Could it be that when I include the WP header file, it ignors the .htaccess file from SLIR?

Related posts

Leave a Reply

3 comments

  1. Denis was pointing to the right direction, but just looking for if the file exists is not the whole job.

    I do this by example, but you need to do this for every file that is unable to load:

    • The class that is unable to get auto-loaded, locate it. Class is Translation_Entry and it is located in /wp-includes/pomo/entry.php. That is some other library and not a core class.
    • You need to extend your autoloader to either deal with all cases -or- to ensure that wp-load.php got included before or while you register your autoloader.
    • Your autoloader is designed to load only classes bases on a specific scheme. And your autoloader is totally incompatible to any other autoloader. Instead, make use of spl_autoload_register() and register your base class autoloader. Then you can add another one upfront that is dealing with the other classes.

    I’ve written some sample code that might not work, it’s untested. You might not even register multiple autoloaders in the end but just one (that calls the different subroutines instead), so this is just a quick written mock-up:

    class MyAutoloader {
        public static function bootstrap() {
            spl_autoload_register(__CLASS__.'::loaderPomo');
            spl_autoload_register(__CLASS__.'::loaderWpClasses');
        }
        public static function getWpClassFile($name) {
            return sprintf('%s.class.php', strtolower($name));
        }
        public static function isWpClass($name) {
            $file = self::getWpClassFile($name);
            return (bool) file_exists($file);
        }
        public static function require($file) {
            require $file;
        }
        // standard wordpress .class.php file loader
        public static function loaderWpClasses($name) {
            if (self::isWpClass($name) {
                $file = self::getWpClassFile($name);
                self::require($file);
            }
        }
        // [...] write any code you need to handle other cases.
        // loader for pomo classes
        public static function loaderPomo($name) {
            if (self::isPomoClass($name)) {
                $file = self::getPomoFile($name);
                self::require($file);
            }
        }
    }
    
  2. I got the same error when writing my own plugin. I solved this issue with the following code.

    $strDirPath = dirname(__FILE__) . '\classes';
    $arrPHPfiles = array_map(create_function( '$a', 'return basename($a, ".php");' ), glob($strDirPath . '*.php'));
    spl_autoload_register(
        create_function('$class_name', '
            global $arrPHPfiles, $strDirPath;
            if (in_array(strtolower($class_name), $arrPHPfiles)) 
                include($strDirPath . strtolower($class_name) . ".php");' )
    );
    

    This checks necessary class files under a directory classes and use include() only if the parsing class name matches existing file names.