-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippet-loader.php
229 lines (184 loc) · 5.24 KB
/
snippet-loader.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* Plugin Name: Snippet Loader
* Description: Loads snippets.
* Version: 0.2.1
* Plugin URI: https://timbr.dev
* Author: Tim Brugman
* Author URI: https://timbr.dev
* Text Domain: snippet-loader
*/
if ( !defined( 'ABSPATH' ) )
exit;
define( 'SNPLDR_FILE_PATH', __FILE__ );
define( 'SNPLDR_FILE', basename( __FILE__ ) );
define( 'SNPLDR_DIR', basename( __DIR__ ) );
define( 'SNPLDR_SNIPPETS_PATH', __DIR__.DIRECTORY_SEPARATOR.'snippets'.DIRECTORY_SEPARATOR );
if ( !class_exists( 'SnippetLoader' ) )
{
class SnippetLoader
{
private $snippets_meta = [];
private $snippets_paths;
/**
* Constructor.
*/
public function __construct()
{
$this->snippets_paths = $this->set_snippet_paths();
}
/**
* Helpers.
*/
private function textdomain()
{
return 'snippet-loader';
}
private function admin_url( $args = [] )
{
$args['page'] = 'snippet-loader';
return admin_url( 'options-general.php?'.http_build_query( $args ) );
}
/**
* Setters.
*/
private function set_snippet_paths()
{
$snippets = glob( SNPLDR_SNIPPETS_PATH.'*.php' );
foreach ( $snippets as $k => $path )
if ( basename( $path ) == 'index.php' )
unset( $snippets[ $k ] );
return $snippets;
}
public function add_snippet_meta( $file, $meta )
{
$this->snippets_meta[ $file ] = $meta;
}
/**
* Getters.
*/
public function get_snippet_paths()
{
return $this->snippets_paths;
}
/**
* Page Helpers.
*/
private function page_header()
{
?>
<style>
.snippet-loader-wrapper {}
.snippet-loader-wrapper table { margin-top: 16px; width: auto; }
.snippet-loader-wrapper .dashicons-yes { color: green; }
.snippet-loader-wrapper .dashicons-no { color: red; }
.snippet-loader-wrapper td:nth-child(2) { white-space: nowrap; }
</style>
<div class="wrap snippet-loader-wrapper">
<?php
}
private function page_footer()
{
?>
</div><!-- wrap -->
<?php
}
private function build_table_data()
{
$table_data = [];
foreach ( $this->snippets_paths as $snippet_file )
{
$output_active = '<span class="dashicons dashicons-no" title="Inactive"></span>';
if ( $this->snippets_meta[ $snippet_file ]['active'] ?? false )
$output_active = '<span class="dashicons dashicons-yes" title="Active"></span>';
$table_data[] = [
$output_active,
$this->snippets_meta[ $snippet_file ]['name'] ?? '',
$this->snippets_meta[ $snippet_file ]['desc'] ?? '',
];
}
return $table_data;
}
private function display_snippets_table()
{
$table_data = $this->build_table_data();
?>
<?php if ( !empty( $table_data ) ): ?>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<td></td>
<td>Name</td>
<td>Description</td>
</tr>
</thead>
<tbody>
<?php foreach ( $table_data as $row ): ?>
<tr>
<?php foreach ( $row as $cell ): ?>
<td><?=$cell;?></td>
<?php endforeach; // $row ?>
</tr>
<?php endforeach; // $table_data ?>
</tbody>
</table>
<?php else: // $table_data is empty ?>
<p>No snippets found.</p>
<?php endif; // $table_data ?>
<?php
}
/**
* Pages.
*/
public function page_controller()
{
$this->page_main();
}
private function page_main()
{
$this->page_header();
echo '<h1>Snippets</h1>';
$this->display_snippets_table();
$this->page_footer();
}
/**
* Hooks.
*/
public function hook_register_settings_page()
{
add_options_page(
'Snippets', // page title
'Snippets', // menu title
'manage_options', // capability
'snippet-loader', // menu slug
[ $this, 'page_controller' ], // function
null // position
);
}
public function hook_register_settings_link( $links )
{
$links['settings'] = '<a href="'.$this->admin_url().'">Settings</a>';
return $links;
}
/**
* Register Hooks.
*/
public function register_hooks()
{
// register settings page
add_action( 'admin_menu', [ $this, 'hook_register_settings_page' ] );
// register settings link
add_filter( 'plugin_action_links_'.SNPLDR_DIR.'/'.SNPLDR_FILE, [ $this, 'hook_register_settings_link' ] );
}
}
/**
* Instantiate.
*/
$snippet_loader = new SnippetLoader();
$snippet_loader->register_hooks();
/**
* Load snippets.
*/
foreach ( $snippet_loader->get_snippet_paths() as $snippet )
include $snippet;
}