forked from ErinMorelli/em-beer-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
em-beer-manager.php
384 lines (329 loc) · 12.7 KB
/
em-beer-manager.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
/**
* Plugin Name: EM Beer Manager
* Plugin URI: https://www.erinmorelli.com/projects/em-beer-manager
* Description: Manage and display your beers with WordPress. Integrates simply with Untappd and Untappd for Business. Great for everyone from home brewers to professional breweries!
* Version: 3.1.0
* Author: Erin Morelli
* Author URI: https://www.erinmorelli.com/
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: embm
* Domain Path: /languages
*/
/**
* Copyright (c) 2013-2017, Erin Morelli.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @package EMBM\Plugin
*/
// Define plugin file paths
define('EMBM_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('EMBM_PLUGIN_URL', plugin_dir_url(__FILE__));
if (!defined('PLUGINDIR')) {
define('PLUGINDIR', 'wp-content/plugins');
}
/**
* Loads EMBM plugin files
*
* @return void
*/
function EMBM_Plugin_load()
{
// Set current version
$embm_curr_version = '3.1.0';
// Define version key name
if (!defined('EMBM_VERSION_KEY')) {
define('EMBM_VERSION_KEY', 'embm_version');
}
// Create new version option
if (!defined('EMBM_VERSION_NUM')) {
define('EMBM_VERSION_NUM', $embm_curr_version);
// Store version in WP database
add_option(EMBM_VERSION_KEY, EMBM_VERSION_NUM);
}
// Update the version value
if (get_option(EMBM_VERSION_KEY) != $embm_curr_version) {
update_option(EMBM_VERSION_KEY, $embm_curr_version);
}
// Load core and output files
include_once EMBM_PLUGIN_DIR.'includes/embm-core.php';
include_once EMBM_PLUGIN_DIR.'includes/embm-output.php';
// Load admin files only in admin
if (is_admin()) {
include_once EMBM_PLUGIN_DIR.'includes/embm-admin.php';
}
// Iteratively load any widgets
foreach (scandir(EMBM_PLUGIN_DIR.'includes/widgets') as $filename) {
// Set widgets path
$path = EMBM_PLUGIN_DIR.'includes/widgets/' . $filename;
// If the PHP file exists, load it
if (is_file($path) && preg_match('/embm-widget-.*\.php$/', $filename)) {
include $path;
}
}
// Plugin localization
load_plugin_textdomain('embm', false, plugin_basename(dirname(__FILE__)).'/languages');
// Do any upgrades
if (!function_exists('EMBM_Upgrade_check')) {
include_once EMBM_PLUGIN_DIR.'includes/embm-upgrades.php';
EMBM_Upgrade_check();
}
}
// Initial plugin load
add_action('plugins_loaded', 'EMBM_Plugin_load', 10);
/**
* Plugin activation setup
*
* @return void
*/
function EMBM_Plugin_activate()
{
// Set default settings options
$defaults = array(
'embm_untappd_check' => '',
'embm_untappd_icons' => '2',
'embm_untappd_rating_format' => '3',
'embm_untappd_rating_color' => '#FFCC00',
'embm_untappd_rating_opacity' => '25',
'embm_css_url' => '',
'embm_group_slug' => 'group',
'embm_reviews_count_single' => '5'
);
// Get any existing options
$options = get_option('embm_options');
// If options exist, fill in any missing with defaults
if (is_array($options)) {
foreach ($defaults as $key => $value) {
if (!array_key_exists($key, $options)) {
$options[$key] = $value;
}
}
} else {
$options = $defaults;
}
// Save the updated options
update_option('embm_options', $options);
// Load core files
if (!function_exists('EMBM_Core_beer')) {
include_once EMBM_PLUGIN_DIR.'includes/embm-core.php';
}
// Load CPTs
EMBM_Core_beer();
EMBM_Core_styles();
EMBM_Core_group();
// Refresh permalinks
flush_rewrite_rules();
}
// Set activation hook
register_activation_hook(__FILE__, 'EMBM_Plugin_activate');
/**
* Plugin deactivation setup
*
* @return void
*/
function EMBM_Plugin_deactivate()
{
// Refresh permalinks
flush_rewrite_rules();
}
// Set deactivation hook
register_deactivation_hook(__FILE__, 'EMBM_Plugin_deactivate');
/**
* Plugin uninstallation setup
*
* @return void
*/
function EMBM_Plugin_uninstall()
{
// Get global post types variable
global $wp_post_types;
// Remove EMBM post type
if (isset($wp_post_types['embm_beer'])) {
unset($wp_post_types['embm_beer']);
}
// Set up EMBM post query
$args = array(
'post_type' =>'embm_beer',
'post_status' => array(
'publish',
'pending',
'draft',
'auto-draft',
'future',
'private',
'inherit',
'trash'
)
);
// Get all existing EMBM posts
$posts = get_posts($args);
// Iteratively remove existing EMBM posts
if (is_array($posts)) {
foreach ($posts as $post) {
wp_delete_post($post->ID, true);
}
}
// Set EMBM taxonomies
$tax = array('embm_group', 'embm_style');
// Get global WP taxonomies
global $wp_taxonomies;
// Remove all EMBM taxonomies
foreach ($tax as $taxonomy) {
// Set taxonomy
register_taxonomy($taxonomy);
// Find all terms for taxonomy
$terms = get_terms($taxonomy, array('hide_empty' => 0));
// Iteratively remove all terms for taxonomy
foreach ($terms as $term) {
wp_delete_term($term->term_id, $taxonomy);
}
// Remove taxonomy from WP
unset($wp_taxonomies[$taxonomy]);
}
// Remove EMBM widget CSS
wp_deregister_style('embm-widget');
wp_dequeue_style('embm-widget');
// Remove EMBM output CSS
wp_deregister_style('embm-output');
wp_dequeue_style('embm-output');
// Remove EMBM admin CSS
wp_deregister_style('embm-admin');
wp_dequeue_style('embm-admin');
// Retrieve custom CSS info
$get_style_option = get_option('embm_options');
$get_custom_css = $style_option['embm_css_url'];
// Remove custom CSS
wp_deregister_style('custom-embm-output');
wp_dequeue_style('custom-embm-output');
// Remove EMBM admin JS
wp_deregister_script('embm-admin-script');
wp_dequeue_script('embm-admin-script');
// Remove EMBM settings
delete_option(EMBM_VERSION_KEY);
delete_option('embm_options');
delete_option('embm_db_upgrade');
delete_option('embm_styles_loaded');
delete_option('widget_embm_beer_list_widget');
delete_option('widget_embm_recent_untappd_widget');
delete_option('embm_untappd_brewery_id');
delete_option('embm_untappd_token');
}
// Set uninstall hook
register_uninstall_hook(__FILE__, 'EMBM_Plugin_uninstall');
/**
* Add plugin listing action link
*
* @param array $links Existing action links
*
* @return array Updated action links
*/
function EMBM_Plugin_links($links)
{
// Define settings link HTML
$settings_link = '<a href="' . get_bloginfo('wpurl');
$settings_link .= '/wp-admin/admin.php?page=embm-settings">';
$settings_link .= __('Settings', 'embm') . '</a>';
// Add to to existing links array
return array_merge(array($settings_link), $links);
}
// Set plugin links filter
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'EMBM_Plugin_links');
/**
* Load plugin output CSS stylesheet
*
* @return void
*/
function EMBM_Plugin_styles()
{
// Get custom CSS URL from DB
$style_option = get_option('embm_options');
$has_custom_css = esc_url($style_option['embm_css_url']);
// If a file is defined, use it
if ($has_custom_css != '') {
// Remove existing output stylesheet
wp_deregister_style('embm-output');
wp_dequeue_style('embm-output');
// Add custom output stylesheet
wp_register_style('custom-embm-output', $has_custom_css);
wp_enqueue_style('custom-embm-output');
} else {
// Remove custom output stylesheet
wp_deregister_style('custom-embm-output');
wp_dequeue_style('custom-embm-output');
// Add default stylesheet
wp_register_style('embm-output', EMBM_PLUGIN_URL.'assets/css/output.css');
wp_enqueue_style('embm-output');
}
// Add widget stylesheet
wp_register_style('embm-widget', EMBM_PLUGIN_URL.'assets/css/widgets.css');
wp_enqueue_style('embm-widget');
// Add WP Dashicons
wp_enqueue_style('dashicons');
}
// Enqueue plugin styles
add_action('wp_enqueue_scripts', 'EMBM_Plugin_styles');
/**
* Returns array of default plugin contextual help
*
* @return array
*/
function EMBM_Plugin_help()
{
return array(
'untappd' => array(
'id' => 'embm-untappd-integration',
'title' => __('Untappd Integration', 'embm'),
'content' => '<p>'.
__('Checking the "Disable site-wide integration" option under the EM Beer Manager "Untappd settings", will completely disable all Untappd functionality, including the Recent Check-ins widget, ratings, check-ins, check-in buttons, and any Untappd-related Labs features.', 'embm').
'</p><p>'.
__('You can disable the Untappd check-in button for an individual beer by simply leaving the "Beer ID" setting empty. Beers that have an active check-in button will display a square Untappd icon next to their entry on the Beers admin page.', 'embm').
'</p><p>'.
__('You can display Untappd beer ratings and recent check-ins if you are logged in to Untappd. Ratings are shown in all beer views, including shortcodes. Check-ins are only displayed on single beer pages. This option can be disabled for specific beers. You can specify how many check-ins to show.', 'embm').
'</p></p>'.
__('Data from Untappd for ratings and check-ins is refreshed automatically, or can be refreshed manually. We do not recommend doing this often as Untappd places a limit on how many API calls can be made per hour.', 'embm').
'</p>'
),
'untappd_id' => array(
'id' => 'embm-untappd-beer-id',
'title' => __('Untappd Beer ID', 'embm'),
'content' => '<p>'.
__('Find your Untappd Beer ID by visiting your beer\'s official page. The URL will be formatted like this', 'embm').
':</p><p><code>https://untappd.com/b/the-alchemist-heady-topper/<strong>4691</strong></code></p><p>'.
__('The string of numbers at the end of the URL is your beer\'s ID.', 'embm').
'</p>'
),
'untappd_limit' => array (
'id' => 'embm-untappd-api-ratelimit',
'title' => __('API Rate-Limit', 'embm'),
'content' => '<p>'.
sprintf(
__('From the %s', 'embm').':',
sprintf(
'<a href="https://untappd.com/api/docs" target="_blank">%s</a>',
__('Untappd API documentation', 'embm')
)
).'</p><p><blockquote><em>"'.
__('All API applications are rate-limited to protect against abuse and keep the platform healthy. The default limit for API access is 100 calls per hour per key.', 'embm').'"</em></blockquote></p><p>'.
__('If you see this message, it means your authenticated API session has reached this limit and any actions that require an API call will be limited until your access is reset in the next hour.', 'embm').'</p><p>'.
__('In most cases you should still be able to use all of the Untappd features with cached data, but rare cases may display a rate-limit warning messages when no cached data is available.', 'embm').
'</p>'
),
'sidebar' => '<p><strong>' . __('For more information', 'embm') . ':</strong></p>' .
'<p><a href="https://www.erinmorelli.com/projects/em-beer-manager" target="_blank">' . __('Plugin Website', 'embm') . '</a></p>' .
'<p><a href="https://wordpress.org/support/plugin/em-beer-manager" target="_blank">' . __('Support Forums', 'embm') . '</a></p>'
);
}