14

I've written several plugins using the structure :

/plugins/myplugin/myplugin.php /plugins/myplugin/class/class-myclass.php

So as to take advantage of OO and overall structuring my code

From within the class file there are times I need to get the URL of the base plugin... I have been using the following, but I'm sure there's a better way:

$this->plugin_location = substr(plugin_dir_url(__FILE__),0, strrpos(plugin_dir_url(__FILE__), "/",-2)) . "/";

Another idea I toyed with was having an additional singleton class that stores all of the settings for the plugin and adding an abstraction layer via the class files.

Any help is greatly appreciated.

19

In a subdirectory within your plugin directory you can use the following code:

$this->plugin_location = plugin_dir_url(dirname(__FILE__));
0
7

You should make all of your classes independent of their actual location, so you can move them around easily and maybe reuse them in other projects.

I would create a class that tells the other classes what path or URL to use, let it implement an interface so you can reuse the other classes maybe even in a theme or completely outside of WordPress.

Example for the interface:

interface DirectoryAddress
{
    /**
     * @return string Dir URL with trailing slash
     */
    public function url();

    /**
     * @return string Dir path with trailing slash
     */
    public function path();
}

The concrete implementation in your plugin could look like this:

class PluginDirectoryAddress implements DirectoryAddress
{
    private $path;

    private $url;

    public function __construct( $dirpath )
    {
        $this->url  = plugins_url( '/', $dirpath );
        $this->path = plugin_dir_path( $dirpath );
    }

    /**
     * @return string Dir URL with trailing slash
     */
    public function url() {
        return $this->url;
    }

    /**
     * @return string Dir path without trailing slash
     */
    public function path() {
        return $this->path;
    }
}

Now you create an instance of that class in your main plugin file:

$address = new PluginDirectoryAddress( __DIR__ );

And all the other classes have just a dependency on the interface in their constructor, like this:

public function __construct( DirectoryAddress $directory ) {}

They are accessing the URL and the path only from the passed instance now.

4
  • Then the class.Plugin_Controller.php would need to be required by the file in the higher directory, correct? – MikeiLL Feb 27 '17 at 16:09
  • @MikeiLL I have just refactored the code. Use an autoloader to include the class. :) – fuxia Feb 27 '17 at 16:38
  • Thank you. Need to educate myself: php-autoloader.malkusch.de/en. This plugin is due for a refactoring soon. And some unit tests. – MikeiLL Feb 27 '17 at 18:59
  • 1
    @MikeiLL Try this one. See the explanation on Code Review. – fuxia Feb 27 '17 at 19:32

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.