-
Notifications
You must be signed in to change notification settings - Fork 0
/
Charm.php
154 lines (135 loc) · 3.32 KB
/
Charm.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace Charm;
use Charm\Cron\Cron;
/**
* Class Charm
*
* @author Ryan Sechrest
* @package Charm
*/
class Charm
{
/************************************************************************************/
// Properties
/**
* Configuration
*
* @var array
*/
private array $config = [];
/************************************************************************************/
// Default constructor and load method
/**
* Charm constructor
*
* @param array $config
*/
public function __construct(array $config)
{
$this->config = [
'cron_viewer' => true,
];
$this->load($config);
}
/**
* Load instance with config
*
* @param array $config
*/
public function load(array $config): void
{
foreach ($config as $key => $value) {
if (!isset($this->config[$key])) {
continue;
}
$this->config[$key] = $value;
}
}
/************************************************************************************/
// Instantiation methods
/**
* Initialize Charm
*
* @param array $config
*/
public static function init(array $config)
{
$charm = new static($config);
$charm->register();
}
/************************************************************************************/
// Action methods
/**
* Register everything
*/
public function register(): void
{
$this->register_tools();
}
/**
* Register tools
*/
public function register_tools(): void
{
$tools = [
'cron_viewer'
];
foreach ($tools as $tool) {
if (!$this->is_enabled($tool)) {
continue;
}
$method = 'register_' . $tool;
if (!method_exists($this, $method)) {
continue;
}
$this->$method();
}
}
/*----------------------------------------------------------------------------------*/
/**
* Register cron viewer
*/
public function register_cron_viewer(): void
{
add_action('admin_menu', [$this, 'add_cron_viewer_page']);
}
/**
* Add cron viewer page
*/
public function add_cron_viewer_page(): void
{
add_management_page(
'Cron Viewer',
'Cron Viewer',
'manage_options',
'charm-cron-viewer',
[$this, 'render_cron_viewer_page']
);
}
/**
* Render cron viewer page
*/
public function render_cron_viewer_page(): void
{
$output = '<div class="wrap">';
$output .= '<h1>' . esc_html(get_admin_page_title()) . ' <small>by <a href="https://wpcharm.com/" target="_blank">Charm</a></small></h1>';
$output .= Cron::to_html();
$output .= '</div>';
echo $output;
}
/************************************************************************************/
// Check methods
/**
* Check if feature is enabled
*
* @param string $key
* @return bool
*/
public function is_enabled(string $key): bool
{
if (!isset($this->config[$key])) {
return false;
}
return $this->config[$key];
}
}