{"id":14954950,"url":"https://github.com/devuri/plugin-interface","last_synced_at":"2026-03-01T15:03:32.538Z","repository":{"id":229240888,"uuid":"776210394","full_name":"devuri/plugin-interface","owner":"devuri","description":"A simple PHP interface designed to provide a consistent structure for WordPress plugins.","archived":false,"fork":false,"pushed_at":"2025-04-24T06:15:24.000Z","size":86,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-14T00:32:26.238Z","etag":null,"topics":["wordpress","wordpress-development","wordpress-plugin"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devuri.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-03-22T22:50:52.000Z","updated_at":"2025-04-24T06:15:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"c679c006-3b09-448d-86dd-c5f779872b6a","html_url":"https://github.com/devuri/plugin-interface","commit_stats":null,"previous_names":["devuri/plugin-interface"],"tags_count":3,"template":true,"template_full_name":"devuri/dot-access","purl":"pkg:github/devuri/plugin-interface","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devuri%2Fplugin-interface","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devuri%2Fplugin-interface/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devuri%2Fplugin-interface/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devuri%2Fplugin-interface/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devuri","download_url":"https://codeload.github.com/devuri/plugin-interface/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devuri%2Fplugin-interface/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29973170,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-01T14:44:57.896Z","status":"ssl_error","status_checked_at":"2026-03-01T14:43:27.662Z","response_time":124,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["wordpress","wordpress-development","wordpress-plugin"],"created_at":"2024-09-24T13:10:17.067Z","updated_at":"2026-03-01T15:03:32.503Z","avatar_url":"https://github.com/devuri.png","language":"PHP","readme":"# WordPress Plugin Interface\n\nA simple PHP interface designed to provide a consistent structure for WordPress plugins. It defines a set of methods that any implementing class must adhere to, ensuring uniformity and ease of use across different plugins.\n\n## Installation\n\nSimply include the `PluginInterface.php` file in your project, or install it via Composer:\n\n```bash\ncomposer require devuri/plugin-interface\n```\n\n## Usage\n\n### Implementing the Plugin Interface\n\nTo create a plugin using the Plugin Interface, follow these steps:\n\n1. **Implement the PluginInterface**: Create a class that implements the `PluginInterface`. This class will define the behavior of your plugin.\n\n2. **Define Class Properties**: Define class properties to hold the plugin directory path and URL. These properties will be used as initialization parameters.\n\n3. **Implement the init() Method**: Implement the `init()` method to initialize your plugin. Use the class properties to set the plugin directory path and URL.\n\n4. **Implement the hooks() Method**: Implement the `hooks()` method to register any WordPress hooks (actions and filters) necessary for your plugin.\n\n### Using the Plugin Interface\n\nTo use a plugin that implements the Plugin Interface, follow these steps:\n\n1. **Initialize the Plugin**: Call the `init()` method of the plugin class, passing the plugin directory path and URL as parameters.\n\n2. **Use the Plugin**: Once initialized, you can use the plugin as needed. Any hooks registered by the plugin will be automatically executed by WordPress.\n\n## Example\n\nHere's an example implementation of a plugin using the Plugin Interface:\n\n```php\n\u003c?php\n\nuse Urisoft\\PluginInterface;\n\nclass MyPlugin implements PluginInterface\n{\n    public static $plugin_dir_path;\n    public static $plugin_dir_url;\n\n    public static function init(string $plugin_dir_path = '', string $plugin_dir_url = ''): object\n    {\n        static $instance = [];\n\n        $called_class = static::class;\n\n        if (!isset($instance[$called_class])) {\n            $instance[$called_class] = new $called_class();\n        }\n\n        self::$plugin_dir_path = $plugin_dir_path;\n        self::$plugin_dir_url = $plugin_dir_url;\n\n        return $instance[$called_class];\n    }\n\n    public function hooks(): void\n    {\n        // Register hooks here using WordPress's hook registration functions\n        // For example:\n        // add_action('init', [$this, 'my_init_function']);\n        // add_filter('the_content', [$this, 'my_content_filter']);\n    }\n}\n```\n\n\u003e Or you can also use the base abstract Implementation, which will include `init()` and the required properties `$plugin_dir_path` and `$plugin_dir_url`\n```php\n\u003c?php\n\nuse Urisoft\\AbstractPlugin;\n\nclass MyPlugin extends AbstractPlugin\n{\n    public function hooks(): void\n    {\n        // Register hooks here using WordPress's hook registration functions\n        // For example:\n        // add_action('init', [$this, 'my_init_function']);\n        // add_filter('the_content', [$this, 'my_content_filter']);\n    }\n}\n```\n\nBelow is an example of how you can instantiate the plugin class, set the plugin path and  url:\n\n```php\n\u003c?php\n\n// Get the plugin directory path\n$plugin_dir_path = plugin_dir_path( __DIR__ );\n\n// Define the plugin URL\n$plugin_dir_url = plugin_dir_url(__FILE__);\n\n// Initialize the plugin\n$my_plugin = MyPlugin::init($plugin_dir_path, $plugin_dir_url);\n\n// Optionally, call the hooks() method to register hooks\n$my_plugin-\u003ehooks();\n```\n\nExplanation:\n\n1. **Include Plugin Interface and Class**: The `PluginInterface` is included to ensure the class adheres to the interface.\n\n2. **Get Plugin Directory Path**: The `wp_plugin_dir_path()` function is used to retrieve the directory path of the plugin file (`__FILE__`). This ensures that the plugin directory path is always accurate.\n\n3. **Define Plugin URL**: The `plugin_dir_url()` function is used to construct the URL of the plugin directory. This URL can be used to enqueue scripts, styles, or create links within the plugin.\n\n4. **Initialize the Plugin**: The `init()` method of the `MyPlugin` class is called, passing the plugin directory path and URL as parameters. This initializes an instance of the plugin is in the application.\n\n5. **Call hooks() Method**: Optionally, the `hooks()` method of the plugin class can be called to register any WordPress hooks necessary for the plugin's functionality.\n\nBenefits:\n\n- **Consistency**: By using `wp_plugin_dir_path()` and `plugin_dir_url()`, you ensure that the plugin directory path and URL are always accurate and consistent.\n\n- **Security**: Using `wp_plugin_dir_path()` ensures that the directory path is properly sanitized and secure, reducing the risk of directory traversal attacks.\n\n- **Flexibility**: The plugin directory path and URL can be easily retrieved and used throughout the plugin code, allowing for dynamic file and URL generation without hardcoding paths or URLs.\n\n- **Compatibility**: The use of WordPress functions (`wp_plugin_dir_path()` and `plugin_dir_url()`) ensures compatibility with future WordPress updates and changes to the file structure.\n\n\u003e using `wp_plugin_dir_path()` and `plugin_dir_url()` to set the plugin path and URL provides a robust approach to plugin development.\n\n\n## TraitInstalled\n\nThe `TraitInstalled` on `AbstractPlugin` provides utility methods to check if a WordPress plugin is installed and active. It helps manage plugin dependencies efficiently.\n\n- **Check if a plugin is installed**\n- **Check if a plugin is active**\n- **Retrieve all installed plugins**\n\n### 1. `MyPlugin::is_installed($plugin_file)`\n\nChecks if a plugin is installed.\n\n- **Parameter**:  \n  `$plugin_file` (string) - Path to the plugin file, e.g., `'example-plugin/example-plugin.php'`.\n\n- **Returns**:  \n  `bool` - `true` if installed, `false` otherwise.\n\n- **Example**:\n    ```php\n    if (MyPlugin::is_installed('example-plugin/example-plugin.php')) {\n        echo 'Plugin is installed.';\n    }\n    ```\n\n### 2. `MyPlugin::is_active($plugin_file)`\n\nChecks if a plugin is active.\n\n- **Parameter**:  \n  `$plugin_file` (string) - Path to the plugin file, e.g., `'example-plugin/example-plugin.php'`.\n\n- **Returns**:  \n  `bool` - `true` if active, `false` otherwise.\n\n- **Example**:\n    ```php\n    if (MyPlugin::is_active('example-plugin/example-plugin.php')) {\n        echo 'Plugin is active.';\n    }\n    ```\n\n### 3. `MyPlugin::get_installed_plugins()`\n\nRetrieves all installed plugins.\n\n- **Returns**:  \n  `array` - An array of installed plugins.\n\n- **Example**:\n    ```php\n    $installed_plugins = MyPlugin::get_installed_plugins();\n    print_r($installed_plugins);\n    ```\n\n### Example Usage\n\nCheck if a plugin is both installed and active:\n\n```php\nfunction is_example_plugin_ready() {\n    $plugin_file = 'example-plugin/example-plugin.php';\n    return MyPlugin::is_installed($plugin_file) \u0026\u0026 MyPlugin::is_active($plugin_file);\n}\n\nif (!is_example_plugin_ready()) {\n    // Show admin notice or handle plugin dependency\n}\n```\n\n## Benefits of the Interface\n\n- **Interface Segregation Principle (ISP)**: The interface follows the ISP by defining only the essential methods (`init()` and `hooks()`), ensuring that implementing classes aren't burdened with unnecessary dependencies.\n\n- **Separation of Concerns**: The interface separates initialization (`init()`) from hook registration (`hooks()`), promoting clearer code organization and making the plugin's behavior easier to understand and maintain.\n\n- **Clean Constructor**: By keeping the constructor clean, the interface adheres to the Single Responsibility Principle (SRP), ensuring that the class is only responsible for instantiation, which supports better code maintainability.\n\n- **Support for Standalone Unit Tests**: The clean constructor enables easy standalone unit testing. With minimal initialization logic, the class can be instantiated in isolation for focused testing, improving testability.\n\n- **Flexibility in Implementation**: The interface's minimal method requirements allow implementing classes the flexibility to achieve desired functionality, accommodating various plugin architectures and implementation strategies while ensuring consistent usage.\n\n- **Promotes Dependency Injection**: The `init()` method acts as a form of dependency injection, enabling clients to provide external dependencies (such as plugin directory path and URL) to the implementing class. This fosters decoupling and enhances flexibility and reusability.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevuri%2Fplugin-interface","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevuri%2Fplugin-interface","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevuri%2Fplugin-interface/lists"}