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 :)
To see already made implementations please check official boilerplates:
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 for this framework are:
- PHP 7.1+
- Composer
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
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
To explain how utilities works, let's take a look to each namespace that has utilities
Admin namespace contains utilities for add new features on admin side:
Option.php
is a wrapper of functions related to Option Api, examples:
\Mjolnir\Admin\Option::get('someOption');
\Mjolnir\Admin\Option::update('someOption', 'someContent');
Page.php
is 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 namespace contains utilities for creating different content solutions that WordPress has:
PostType.php
allows 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.php
allows 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.php
is a wrapper for functions related to Shortcode Api, example:
\Mjolnir\Content\Shortcode::add('hello', [$this, 'hello']);
\Mjolnir\Content\Shortcode::do('[hello]');
Database namespace contains utilities for database interaction:
Query.php
is 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 namespace contains utilities related to routing system of WordPress such as Api:
Api.php
is 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 namespace contains utilities that will help you work with data:
Arr.php
is utility class that allows you to manipulate with array, examples:
$first = \Mjolnir\Support\Arr::first($array);
$key = \Mjolnir\Support\Arr::get($array, 'key');
Collection.php
is class for working with arrays of data, examples:
$collection = \Mjolnir\Support\Collection::make($array);
$filtered = $collection->where('key', 'value');
$reversed = $filtered->reverse();
Is.php
is 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.php
is class to manipulate with string, examples:
$string = \Mjolnir\Support\Str::make('some string');
$reversed = $string->flip();
$contains = $string->has('some');
Utils namespace contains utilities for working with plugins and themes:
Enqueue.php
is 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.php
is 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.php
is 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 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.php
give 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.php
give 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.php
give 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.php
give access to config files, example:
{{Namespace}}\Facades\Config::get('configFile.index.anotherIndex');
{{Namespace}}\Facades\Config::get('app.view.folder');
View.php
give access to view file for render, example:
{{Namespace}}\Facades\View::render('books.single');
{{Namespace}}\Facades\View::render('books/single.blade.php');
//TODO
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.