-
Notifications
You must be signed in to change notification settings - Fork 0
/
gwentdb.php
281 lines (245 loc) · 7.49 KB
/
gwentdb.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/*
Plugin Name: GwentDB
Plugin URI: https://www.gwentdb.hu
Description: This plugin allows user to insert Gwent Cards into their posts.
Version: 1.0.0
Author: Lóna Lore
Author URI: https://lonalore.hu
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
/**
* Registers our gwentdb_plugin_settings to the admin_init action hook.
*/
add_action('admin_init', 'gwentdb_plugin_settings');
/**
* Registers new administration links.
*
* Fires before the administration menu loads in the admin.
*/
add_action('admin_menu', 'gwentdb_create_admin_menu_link');
/**
* Enqueue new scripts in order to expose plugin settings for javascript.
*/
add_action('admin_enqueue_scripts', 'gwentdb_enqueue_admin_scripts');
add_action('wp_enqueue_scripts', 'gwentdb_enqueue_scripts');
/**
* Enqueue new styles.
*/
add_action('admin_enqueue_scripts', 'gwentdb_enqueue_admin_styles');
add_action('wp_enqueue_scripts', 'gwentdb_enqueue_styles');
/**
* Registers new TinyMCE buttons.
*
* The mce_buttons group of filters can be used by developers to add or remove
* buttons/features from the WordPress's TinyMCE toolbar(s). This filter is
* passed an array of button ids which determine which buttons are displayed or
* removed.
*/
add_filter('mce_buttons', 'gwentdb_mce_register_buttons');
/**
* Registers additional javascript files for TinyMCE.
*
* The mce_external_plugins filter is used to load external TinyMCE plugins.
* This filter may be useful for loading any of the TinyMCE core plugins, many
* of which are not included by default in WordPress, as well as any custom
* TinyMCE plugins you may want to create.
*/
add_filter('mce_external_plugins', 'gwentdb_mce_register_javascript');
/**
* Registers additional stylesheet files for TinyMCE.
*
* The mce_css filter provides a method to add custom stylesheets to the TinyMCE
* editor window.
*
* The file can be a .php file, allowing dynamic generation of CSS rules for the
* content editor.
*/
add_filter('mce_css', 'gwentdb_mce_register_stylesheet');
/**
* Callback function to enqueue new scripts for admin pages.
*/
function gwentdb_enqueue_admin_scripts() {
wp_register_script('gwentdb', plugins_url('/js/gwentdb.cards.js', __FILE__), array('jquery'));
$options = gwentdb_get_javascript_settings();
wp_localize_script('gwentdb', 'GwentConfig', $options);
wp_enqueue_script('gwentdb');
}
/**
* Callback function to enqueue new scripts for frontend pages.
*/
function gwentdb_enqueue_scripts() {
wp_register_script('gwentdb', plugins_url('/js/gwentdb.cards.js', __FILE__), array('jquery'));
$options = gwentdb_get_javascript_settings();
wp_localize_script('gwentdb', 'GwentConfig', $options);
wp_enqueue_script('gwentdb');
}
/**
* Helper function to build settings array will be exposed for javascript.
*
* @return array
*/
function gwentdb_get_javascript_settings() {
$locale = get_locale();
$locale_iso = substr($locale, 0, 2);
if (!in_array($locale_iso, array('en', 'hu'))) {
$locale_iso = 'en';
}
$options = array(
'apiKey' => get_option('gwentdb_api_key'),
'language' => $locale_iso,
'translate' => array(
'Version' => __('Version'),
'Latest' => __('Latest'),
'Any' => __('Any'),
'Category' => __('Category'),
'Faction' => __('Faction'),
'Type' => __('Type'),
'Position' => __('Position'),
'No results...' => __('No results...'),
),
);
return $options;
}
/**
* Callback function to enqueue new styles for admin pages.
*/
function gwentdb_enqueue_admin_styles() {
wp_register_style('gwentdb', plugins_url('/css/tinymce.plugin.css', __FILE__));
wp_register_style('gwentdb', plugins_url('/css/gwentdb.cards.css', __FILE__));
wp_enqueue_style('gwentdb');
}
/**
* Callback function to enqueue new styles for frontend pages.
*/
function gwentdb_enqueue_styles() {
wp_register_style('gwentdb', plugins_url('/css/gwentdb.cards.css', __FILE__));
wp_enqueue_style('gwentdb');
}
/**
* Callback function to register new TinyMCE buttons.
*
* @param array $buttons
* Array contains buttons already added to TinyMCE.
*
* @return mixed
*/
function gwentdb_mce_register_buttons($buttons) {
array_push($buttons, 'separator', 'gwentdb');
return $buttons;
}
/**
* Callback function to register additional javascript files for TinyMCE.
*
* @param array $plugin_array
* Array contains plugins.
*
* @return array
*/
function gwentdb_mce_register_javascript($plugin_array) {
$plugin_array['gwentdb'] = plugins_url('/js/tinymce.plugin.js', __FILE__);
return $plugin_array;
}
/**
* Callback function to register additional stylesheet files for TinyMCE.
*
* @param string $mce_css
* $mce_css is a comma separated list of stylesheet URIs.
*
* @return string
*/
function gwentdb_mce_register_stylesheet($mce_css) {
$files = explode(',', $mce_css);
$files[] = plugins_url('/css/tinymce.plugin.css', __FILE__);
return implode(',', $files);
}
/**
* Callback function to initialize plugin settings.
*/
function gwentdb_plugin_settings() {
// Register our settings.
register_setting('gwentdb-plugin-settings-group', 'gwentdb_api_key');
}
/**
* Admin callback function to provide admin page links.
*/
function gwentdb_create_admin_menu_link() {
// Create new top-level menu.
add_menu_page(__('Gwent settings'), __('Gwent settings'), 'manage_options', 'gwentdb', 'gwentdb_plugin_settings_page');
}
/**
* Page callback function to provide a page for plugin settings.
*/
function gwentdb_plugin_settings_page() {
// Check user capabilities.
if (!current_user_can('manage_options')) {
return;
}
gwentdb_get_template('plugin-settings-page.php');
}
/**
* Get template.
*
* Search for the template and include the file.
*
* @param string $template_name
* Template to load.
* @param array $args
* Args passed for the template file.
* @param string $template_path
* Path to templates.
* @param string $default_path
* Default path to template files.
*/
function gwentdb_get_template($template_name, $args = array(), $template_path = '', $default_path = '') {
if (is_array($args) && isset($args)) {
extract($args);
}
$template_file = gwentdb_locate_template($template_name, $template_path, $default_path);
if (!file_exists($template_file)) {
// @todo error message?
return;
}
include $template_file;
}
/**
* Locate template.
*
* Locate the called template.
* Search Order:
* 1. /themes/theme/gwentdb/$template_name
* 2. /themes/theme/$template_name
* 3. /plugins/gwentdb/templates/$template_name.
*
* @param string $template_name
* Template to load.
* @param string $template_path
* Path to templates.
* @param string $default_path
* Default path to template files.
*
* @return string
* Path to the template file.
*/
function gwentdb_locate_template($template_name, $template_path = '', $default_path = '') {
// Set variable to search in gwentdb folder of theme.
if (!$template_path) {
$template_path = 'gwentdb/';
}
// Set default plugin templates path.
if (!$default_path) {
// Path to the template folder.
$default_path = plugin_dir_path(__FILE__) . 'templates/';
}
// Search template file in theme folder.
$template = locate_template(array(
$template_path . $template_name,
$template_name
));
// Get plugins template file.
if (!$template) {
$template = $default_path . $template_name;
}
return apply_filters('gwentdb_locate_template', $template, $template_name, $template_path, $default_path);
}