-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-doublee.php
224 lines (186 loc) · 7.09 KB
/
class-doublee.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
<?php
/**
* The plugin bootstrap file
*
* This file is read by WordPress to generate the plugin information in the plugin
* admin area. This file also includes all the dependencies used by the plugin,
* registers the activation and deactivation functions, and defines a function
* that starts the plugin.
*
* Development of this plugin was started using the WordPress Plugin Boilerplate Generator https://wppb.me/
*/
// If this file is called directly, abort.
if (!defined('WPINC')) {
die;
}
/**
* Current plugin version.
* Rename this for your plugin and update it as you release new versions.
*/
const DOUBLEE_VERSION = '2.1.0';
/**
* Path of plugin root folder
*/
define('DOUBLEE_PLUGIN_PATH', plugin_dir_path(__FILE__));
/**
* The core plugin class
*
* @since 1.0.0
* @package Doublee
* @subpackage Doublee/includes
* @author Leesa Ward
*/
class Doublee {
/**
* The current version of the plugin.
*
* @since 1.0.0
* @access protected
* @var string $version The current version of the plugin.
*/
protected string $version;
/**
* Variables to store instances of our custom classes
* This is one option of how to create an instance of a class, in this case using the same instance in multiple places.
* Instances can also be created as-needed within functions below,
* so it is not mandatory to do this for every custom class we create;
* we can make an informed decision each time about the best way/place to use a class.
* Considerations include:
* - Do we need to use it multiple times or just once?
* - Does it make sense to use the same instance, or should we have a new instance/object each time?
* - Does one method "break" something?
* - Is one method more efficient than another?
* - Which way might be clearer and easier to understand? Are there any downsides to the "easier" way?
*/
private static Doublee_Users $user_functions;
/**
* Set up the core functionality of the plugin in the constructor
* by loading the modular classes of functionality.
*
* @since 2.0.0
*/
public function __construct() {
$this->version = DOUBLEE_VERSION;
// Call the function that initialises our classes
// and sets up some values that can be used throughout this file
$this->load_classes();
}
/**
* Load the required dependencies for this plugin.
* Each time we create a class file, we need to add it and initialise it here.
*
* @return void
* @since 2.0.0
* @access private
*/
private function load_classes(): void {
require_once DOUBLEE_PLUGIN_PATH . '/includes/class-users.php';
self::$user_functions = new Doublee_Users();
require_once DOUBLEE_PLUGIN_PATH . '/includes/class-admin-notices.php';
new Doublee_Admin_Notices();
require_once DOUBLEE_PLUGIN_PATH . '/includes/class-admin-ui.php';
new Doublee_Admin_UI();
require_once DOUBLEE_PLUGIN_PATH . '/includes/class-seo.php';
new Doublee_SEO();
if (class_exists('WooCommerce')) {
require_once DOUBLEE_PLUGIN_PATH . '/includes/class-woocommerce.php';
new Doublee_WooCommerce();
}
}
/**
* Run functions on plugin activation.
* Things we only want to run once - when the plugin is activated
* (as opposed to every time the admin initialises, for example)
* @return void
*/
public static function activate(): void {
self::$user_functions->create_roles();
self::$user_functions->reassign_users_roles();
}
/**
* Run functions on plugin deactivation.
* NOTE: This can be a destructive operation!
* Basically anything done by the plugin should be reversed or adjusted to work with built-in WordPress functionality
* if the plugin is deactivated. However, it is important to note that often developers/administrators will
* deactivate a plugin temporarily to troubleshoot something and then reactivate it, so we should not do a full cleanup
* (such as deleting data) by default.
*
* Consider carefully whether deactivation or uninstallation is the better place to remove/undo something.
*
* @return void
*/
public static function deactivate(): void {
self::$user_functions->delete_roles();
}
/**
* Run functions on plugin uninstallation
* NOTE: This is for VERY destructive operations!
* There are some things that it is best practice to do on uninstallation,
* for example custom database tables created by the plugin (if we had any)
* should be deleted when the plugin is uninstalled from the site.
* Think of this as "not using it anymore" levels of cleanup.
*
* Consider carefully whether deactivation or uninstallation is the better place to remove/undo something.
*
* @return void
*/
public static function uninstall(): void {
self::$user_functions->revert_users_roles(true);
}
/**
* Function to retrieve the version number of the plugin.
* @wp-hook
*
* @return string The version number of the plugin.
* @since 1.0.0
*/
public function get_version(): string {
return $this->version;
}
/**
* Function to retrieve the name of the plugin for use in the admin
* (e.g., labelling stuff)
*
* @return string The name of the plugin
* @since 1.0.0
*/
public static function get_name(): string {
$plugin_data = get_plugin_data(DOUBLEE_PLUGIN_PATH . 'doublee.php');
return $plugin_data['Name'];
}
/**
* Shared utility function to conditionally change the ACF JSON save location
* - to be used for field groups relevant to CPTs, taxonomies, etc. introduced by this plugin
* - when called, must be wrapped in a relevant conditional to identify the group to save to the plugin
* @return void
*/
public static function override_acf_json_save_location(): void {
// remove this filter so it will not affect other groups
remove_filter('acf/settings/save_json', 'override_acf_json_save_location', 400);
add_filter('acf/settings/save_json', function ($path) {
// remove this filter so it will not affect other groups
remove_filter('acf/settings/save_json', 'override_acf_json_save_location', 400);
// override save path in this case
return DOUBLEE_PLUGIN_PATH . 'assets/acf-json';
}, 9999);
}
/**
* Utility function to get lists of filenames where ACF JSON files are stored in the plugin and theme
* @return array
*/
public static function get_acf_json_filenames(): array {
$in_events_plugin = array();
$in_plugin = scandir(DOUBLEE_PLUGIN_PATH . 'assets/acf-json/');
$in_parent_theme = scandir(get_template_directory() . '/acf-json/');
$in_theme = scandir(get_stylesheet_directory() . '/acf-json/');
if (class_exists('Doublee_Events') && defined('DOUBLEE_EVENTS_PLUGIN_PATH')) {
$in_events_plugin = scandir(DOUBLEE_EVENTS_PLUGIN_PATH . 'assets/acf-json/');
}
return array(
'plugin' => array_values(array_filter($in_plugin, fn($item) => str_contains($item, '.json'))),
'parent_theme' => array_values(array_filter($in_parent_theme, fn($item) => str_contains($item, '.json'))),
'theme' => array_values(array_filter($in_theme, fn($item) => str_contains($item, '.json'))),
'events_plugin' => class_exists('Doublee_Events') ? array_values(array_filter($in_events_plugin, fn($item) => str_contains($item, '.json'))) : array()
);
}
}