-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
122 lines (108 loc) · 3.39 KB
/
plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* Plugin Name: Example Plugin
* Plugin URI: https://www.veronalabs.com
* Plugin Prefix: EXAMPLE_PLUGIN
* Description: Example WordPress Plugin Based on Rabbit Framework!
* Author: VeronaLabs
* Author URI: https://veronalabs.com
* Text Domain: example-plugin
* Domain Path: /languages
* Version: 1.0
*/
use Rabbit\Application;
use Rabbit\Redirects\RedirectServiceProvider;
use Rabbit\Database\DatabaseServiceProvider;
use Rabbit\Logger\LoggerServiceProvider;
use Rabbit\Plugin;
use Rabbit\Redirects\AdminNotice;
use Rabbit\Templates\TemplatesServiceProvider;
use Rabbit\Utils\Singleton;
use ExamplePlugin\BooksInfoServiceProvider;
use ExamplePlugin\Admin\AdminServiceProvider;
use ExamplePlugin\BooksInfo;
if (file_exists(dirname(__FILE__) . '/vendor/autoload.php')) {
require dirname(__FILE__) . '/vendor/autoload.php';
}
/**
* Class ExamplePluginInit
* @package ExamplePluginInit
*/
class ExamplePluginInit extends Singleton
{
/**
* @var \League\Container\Container
*/
private $application;
/**
* ExamplePluginInit constructor.
*/
public function __construct()
{
$this->application = Application::get()->loadPlugin(__DIR__, __FILE__, 'config');
$this->init();
}
public function init()
{
try {
/**
* Load service providers
*/
$this->application->addServiceProvider(RedirectServiceProvider::class);
$this->application->addServiceProvider(DatabaseServiceProvider::class);
$this->application->addServiceProvider(TemplatesServiceProvider::class);
$this->application->addServiceProvider(LoggerServiceProvider::class);
$this->application->addServiceProvider(BooksInfoServiceProvider::class);
$this->application->addServiceProvider(AdminServiceProvider::class);
/**
* Activation hooks
*/
$this->application->onActivation(function () {
BooksInfo::activate();
});
/**
* Deactivation hooks
*/
$this->application->onDeactivation(function () {
BooksInfo::deactivate();
});
$this->application->boot(function (Plugin $plugin) {
$plugin->loadPluginTextDomain();
// Load template
$this->application->template('plugin-template.php', ['foo' => 'bar']);
});
} catch (Exception $e) {
/**
* Print the exception message to admin notice area
*/
add_action('admin_notices', function () use ($e) {
AdminNotice::permanent(['type' => 'error', 'message' => $e->getMessage()]);
});
/**
* Log the exception to file
*/
add_action('init', function () use ($e) {
if ($this->application->has('logger')) {
$this->application->get('logger')->warning($e->getMessage());
}
});
}
}
/**
* @return \League\Container\Container
*/
public function getApplication()
{
return $this->application;
}
}
/**
* Returns the main instance of ExamplePluginInit.
*
* @return ExamplePluginInit
*/
function examplePlugin()
{
return ExamplePluginInit::get();
}
examplePlugin();