cba85 / wp-icarus-framework
Wordpress Icarus plugin framework boilerplate
1.0.2
2021-04-26 15:41 UTC
Requires (Dev)
- phpunit/phpunit: ^8.3
This package is auto-updated.
Last update: 2025-03-27 00:47:30 UTC
README
Wordpress Icarus plugin framework.
Install
Via Composer
$ composer require cba85/wp-icarus-framework
Usage
Bootstrap
use Icarus\Plugin; require __DIR__ . '/../vendor/autoload.php'; new Plugin();
Facades
You can use components as objects:
use Icarus\Assets\Script; $script = new Script(__DIR__ . '/js'); $script->add('style1-name', 'style.css', [], false, 'all') ->add('style2-name', 'style2.css', [], false, 'all') ->save();
You can also use some components as facades:
Hook::register('activation', __FILE__, function () { return new HookController; });
Components
Config
use Icarus\Config\Config; $config = new Config; // Bind a configuration file $config->bind(['test' => require __DIR__ . '/config/test.php']); // Get a key from a configuration file $config->get('test')['key']; // Value
Config file example:
return [ 'name' => 'WP Icarus plugin', 'view' => __DIR__ . '/../resources/views/', 'styles' => __DIR__ . '/../public/css/', 'scripts' => __DIR__ . '/../public/js/', ];
View
Your view files name must end with .view.php
(e.g. filename.view.php
);
use Icarus\View\View; $view = new View; $view->setPath(__DIR__ . '/views/'); $view->render('filename'); $view->render('filename', ['key' => 'value']); // With data
Style
use Icarus\Assets\Style; (new Style)->setPath(Config::get('plugin')['styles']) ->add('style1-name', 'style.css', [], false, 'all') ->add('style2-name', 'style2.css', [], false, 'all') ->save('wp_enqueue_style');
Script
use Icarus\Assets\Script; (new Script)->setPath(Config::get('plugin')['scripts']) ->add('scripts', 'scripts.js', [], false, true) ->add('admin', 'admin.js', [], false, true) ->save('wp_enqueue_script');
Menu
Facade
Menu::create(function () { Menu::addPage('page', 'menu', 'capability', 'slug', function () { return (new \Icarus\Tests\Mocks\AdminController)->index(); }); Menu::addSubPage('parent', 'page', 'menu', 'capability', 'slug', function () { return (new \Icarus\Tests\Mocks\AdminController)->index(); }); });
Session
Facade
Session::set('success', "Success test"); echo Session::has('success'); // True echo Session::get('success'); // Success test Session::remove('success'); Session::all(); Session::flush();
Notice
Facade
// Create a notice message Notice::setKey("icarus-framework")->create('success', "Success test"); // Display a notice message Notice::display();
Hook
Facade
Hook::register('activation', __FILE__, function () { new ActivationHook; })->register('deactivation', __FILE__, function () { new DeactivationHook; });
Admin
Facade
Admin::action('process_test', function () { return $this->save(); });
Models
Table
<?php namespace Icarus\Plugin\Models; use Icarus\Models\Tables\Table; class Post extends Table { public function table() { global $wpdb; return $wpdb->prefix . 'posts'; } public function key() { return 'ID'; } public function fields() { return []; } }
Option
<?php namespace Icarus\Plugin\Models; use Icarus\Models\Tables\Option; class WoocommerceOption extends Option { public function prefix() { return 'woocommerce_'; } public function fields() { return [ 'status', ]; } }
Meta
<?php namespace Icarus\Plugin\Models; use Icarus\Models\Tables\Meta; class ShippingMeta extends Meta { public function prefix() { return '_shipping_'; } public function fields() { return [ 'first_name', 'last_name', 'company', 'address_1', 'address_2', 'city', 'postcode', 'country', ]; } }
Testing
$ ./vendor/bin/phpunit
Or
$ composer run-script test