codebjorn / mjolnir
WordPress Utility Framework
Installs: 19
Dependents: 2
Suggesters: 0
Security: 0
Stars: 39
Watchers: 3
Forks: 0
Open Issues: 0
pkg:composer/codebjorn/mjolnir
Requires
- php: >=7.1
- eftec/bladeone: ^3.52
- league/container: ^3.3
Requires (Dev)
- phpunit/phpunit: ^7.0
README
Mjölnir, WordPress Utility Framework
Mjölnir is small utility framework that gives utilities and possibilities to create WordPress plugins & themes using best of PHP. This package is compliant with PSR-1, PSR-2, PSR-4 and PSR-11.
If you think this approach is not working, please open an issue and let's discuss :)
Already made implementations
To see already made implementations please check official boilerplates:
Pre-Requirements
Before we proceed further:
-
I suggest you to read documentation of PHP League Container.
Mjölnir it uses as to provide Dependency Injection in your application. -
Also, if you will use default template engine, check documentation of BladeOne
Requirements
Requirements for this framework are:
- PHP 7.1+
- Composer
Installation
You can install framework via composer:
composer require codebjorn/mjolnir
Copy stubs from folder stubs to your folder, rename app folder if is need it and update {{namespace}} with
application namespace
In a nutshell
Base of framework is dependency injection container, that stores some utility classes for: configuration, render, exception handling, hooks, blocks and all other services that you add and resolve, after all of that you add your service to hook.
As example, you have class that add some WP functionality PostTypes.php, you resolve all dependencies
in ServiceProvider and after apply it to a hook
Utilities
To explain how utilities works, let's take a look to each namespace that has utilities
Admin
Admin namespace contains utilities for add new features on admin side:
Option.phpis a wrapper of functions related to Option Api, examples:
\Mjolnir\Admin\Option::get('someOption'); \Mjolnir\Admin\Option::update('someOption', 'someContent');
Page.phpis a wrapper for all functions related to creating a page in admin, examples:
\Mjolnir\Admin\Page::dashboard('Page', 'Page', 'read', 'page'); \Mjolnir\Admin\Page::management('Page', 'Page', 'read', 'page');
Content
Content namespace contains utilities for creating different content solutions that WordPress has:
PostType.phpallows you easy to create different custom post types, example:
$books = \Mjolnir\Content\PostType::make('Books') ->supports(['title', 'editor']) ->public(true) ->showInRest(true); $books->register(); //Call register to exit creating of arguments
Taxonomy.phpallows you easy to create different taxonomies, example:
$countries = \Mjolnir\Content\Taxonomy::make('Countries') ->postTypes(['books']) ->public(true) ->showInRest(true); $countries->register(); //Call register to exit creating of arguments
Shortcode.phpis a wrapper for functions related to Shortcode Api, example:
\Mjolnir\Content\Shortcode::add('hello', [$this, 'hello']); \Mjolnir\Content\Shortcode::do('[hello]');
Database
Database namespace contains utilities for database interaction:
Query.phpis a wrapper for WP Query arguments
$metaQuery = new \Mjolnir\Database\Parameter\Meta([ new \Mjolnir\Database\Parameter\MetaArgument('key', 'value', 'NUMERIC', '='), new \Mjolnir\Database\Parameter\MetaArgument('key', 'value', 'DATE', '>'), ]); $tax = new \Mjolnir\Database\Parameter\Tax(null, [ new \Mjolnir\Database\Parameter\TaxArgument('taxonomy', 'field', 'term1'), new \Mjolnir\Database\Parameter\TaxArgument('taxonomy', 'field', 'term2'), ]); $posts = new \Mjolnir\Database\Query(); $posts->postType('posts') ->meta($metaQuery) ->tax($tax) ->pagination(5); $posts->make(); // return new WP_Query $posts->get(); // returns Collection $posts->getRaw(); //return Array
Routing
Routing namespace contains utilities related to routing system of WordPress such as Api:
Api.phpis a wrapper of all functions related to REST API, example:
\Mjolnir\Routing\Api::make('/namespace', '/users') ->get([$this, 'getUsers'], '_return_true') ->post([$this, 'postUsers'], [$this, 'isAdmin']);
Support
Support namespace contains utilities that will help you work with data:
Arr.phpis utility class that allows you to manipulate with array, examples:
$first = \Mjolnir\Support\Arr::first($array); $key = \Mjolnir\Support\Arr::get($array, 'key');
Collection.phpis class for working with arrays of data, examples:
$collection = \Mjolnir\Support\Collection::make($array); $filtered = $collection->where('key', 'value'); $reversed = $filtered->reverse();
Is.phpis wrapper for functions to determine type of variable, examples:
\Mjolnir\Support\Is::file($value); \Mjolnir\Support\Is::str($value); \Mjolnir\Support\Is::int($value);
Str.phpis class to manipulate with string, examples:
$string = \Mjolnir\Support\Str::make('some string'); $reversed = $string->flip(); $contains = $string->has('some');
Utils
Utils namespace contains utilities for working with plugins and themes:
Enqueue.phpis wrapper of functions related to enqueue, example:
\Mjolnir\Utils\Enqueue::style('theme-style', 'folder/style.css', [], '1.0.0', 'all'); \Mjolnir\Utils\Enqueue::script('theme-script', 'folder/script.css', [], '1.0.0', true); \Mjolnir\Utils\Enqueue::all();
Post.phpis wrapper of WP_Post that gives better API to get post, example:
$currentPost = \Mjolnir\Utils\Post::current(); $post = \Mjolnir\Utils\Post::get(1); $postId = $post->getId(); $postSlug = $post->getSlug();
Theme.phpis wrapper of functions related to theme, example:
\Mjolnir\Utils\Theme::support('feature'); \Mjolnir\Utils\Theme::textDomain('domain', 'path'); \Mjolnir\Utils\Theme::mod()->set('item', 'value'); \Mjolnir\Utils\Theme::mod()->get('item');
Facades
Facades are API that allows you to create a class that will get resolved class from container, check stubs/app/Facades
to check default facades:
Action.phpgive access to action hook, example:
{{Namespace}}\Facades\Action::do('hook');
{{Namespace}}\Facades\Action::add('hook', [$this, 'function']);
{{Namespace}}\Facades\Action::group('hook')
->add([$this, 'function'])
->add(SomeClass::class)
->add(function () {
echo "something todo";
});
Filter.phpgive access to filter hook, example:
{{Namespace}}\Facades\Filter::apply('filter');
{{Namespace}}\Facades\Filter::add('filter', [$this, 'function']);
{{Namespace}}\Facades\Filter::group('filter')
->add([$this, 'function'])
->add(SomeClass::class)
->add(function () {
echo "something todo";
});
Block.phpgive access to class related to block, example:
{{Namespace}}\Facades\Block::add('namespace', 'name');
{{Namespace}}\Facades\Block::exists('namespace/name');
{{Namespace}}\Facades\Block::group('namespace')
->add('name')
->add('name');
Config.phpgive access to config files, example:
{{Namespace}}\Facades\Config::get('configFile.index.anotherIndex');
{{Namespace}}\Facades\Config::get('app.view.folder');
View.phpgive access to view file for render, example:
{{Namespace}}\Facades\View::render('books.single');
{{Namespace}}\Facades\View::render('books/single.blade.php');
Testing
//TODO
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email quotesun@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.