-
Notifications
You must be signed in to change notification settings - Fork 10
/
widget-importer-exporter.php
244 lines (210 loc) · 5.68 KB
/
widget-importer-exporter.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
<?php
/**
* Plugin Name: Widget Importer & Exporter
* Plugin URI: https://churchthemes.coqm/plugins/widget-importer-exporter/
* Description: Imports and exports widgets.
* Version: 1.6.1
* Author: ChurchThemes.com
* Author URI: https://churchthemes.com
* License: GPLv2 or later
* Text Domain: widget-importer-exporter
* Domain Path: /languages
*
* @package Widget_Importer_Exporter
* @copyright Copyright (c) 2013 - 2023, ChurchThemes.com, LLC
* @link https://churchthemes.com/plugins/widget-importer-exporter/
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0-or-later
*
* TODO: Move from procedural to OOP and require same minimum PHP as WordPress.
*/
defined('ABSPATH') || exit; // No direct access.
/**
* Main class
*
* @since 0.1
*/
class Widget_Importer_Exporter
{
/**
* Plugin data from get_plugins()
*
* @since 0.1
* @var object
*/
public $plugin_data;
/**
* Includes to load
*
* @since 0.1
* @var array
*/
public $includes;
/**
* Constructor
*
* Add actions for methods that define constants, load translation and load includes.
*
* @since 0.1
* @access public
*/
public function __construct()
{
// Set plugin data.
add_action('plugins_loaded', array(&$this, 'setPluginData'), 1);
// Define constants.
add_action('plugins_loaded', array(&$this, 'defineConstants'), 1);
// Load language file.
add_action('plugins_loaded', array(&$this, 'loadTextdomain'), 1);
// Set includes.
add_action('plugins_loaded', array(&$this, 'setIncludes'), 1);
// Load includes.
add_action('plugins_loaded', array(&$this, 'loadIncludes'), 1);
}
/**
* Set plugin data
*
* This data is used by constants.
*
* @since 0.1
* @access public
*/
public function setPluginData()
{
// Load plugin.php if get_plugins() not available.
if (! function_exists('get_plugins')) {
// @codingStandardsIgnoreLine
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
// Get path to plugin's directory.
$plugin_dir = plugin_basename(dirname(__FILE__));
// Get plugin data.
$plugin_data = current(get_plugins('/' . $plugin_dir));
// Set plugin data.
$this->plugin_data = apply_filters('wie_plugin_data', $plugin_data);
}
/**
* Define constants
*
* @since 0.1
* @access public
*/
public function defineConstants()
{
// Plugin version.
define('WIE_VERSION', $this->plugin_data['Version']);
// Plugin's main file path.
define('WIE_FILE', __FILE__);
// Plugin's directory.
define('WIE_DIR', dirname(plugin_basename(WIE_FILE)));
// Plugin's directory path.
define('WIE_PATH', untrailingslashit(plugin_dir_path(WIE_FILE)));
// Plugin's directory URL.
define('WIE_URL', untrailingslashit(plugin_dir_url(WIE_FILE)));
// Includes directory.
define('WIE_INC_DIR', 'includes');
// Stylesheets directory.
define('WIE_CSS_DIR', 'css');
// JavaScript directory.
define('WIE_JS_DIR', 'js');
// Image directory.
define('WIE_IMG_DIR', 'img');
// Languages directory.
define('WIE_LANG_DIR', 'languages');
}
/**
* Load language file
*
* This will load the MO file for the current locale.
* The translation file must be named widget-importer-exporter-$locale.mo.
*
* First it will check to see if the MO file exists in wp-content/languages/plugins.
* If not, then the 'languages' directory inside the plugin will be used.
* It is ideal to keep translation files outside of the plugin to avoid loss during updates.\
*
* @since 0.1
* @access public
*/
public function loadTextdomain()
{
// Text-domain.
$domain = 'widget-importer-exporter';
// WordPress core locale filter.
$locale = apply_filters('plugin_locale', get_locale(), $domain);
// WordPress 3.6 and earlier don't auto-load from wp-content/languages, so check and load manually
// http://core.trac.wordpress.org/changeset/22346.
$external_mofile = WP_LANG_DIR . '/plugins/' . $domain . '-' . $locale . '.mo';
// External translation exists.
if (get_bloginfo('version') <= 3.6 && file_exists($external_mofile)) {
load_textdomain($domain, $external_mofile);
} else {
// Load normally. Either using WordPress 3.7+ or older version with external translation.
$languages_dir = WIE_DIR . '/' . trailingslashit(WIE_LANG_DIR); // ensure trailing slash.
load_plugin_textdomain($domain, false, $languages_dir);
}
}
/**
* Set includes
*
* @since 0.1
* @access public
*/
public function setIncludes()
{
$this->includes = apply_filters('wie_includes', array(
// Admin only.
'admin' => array(
WIE_INC_DIR . '/admin.php',
WIE_INC_DIR . '/export.php',
WIE_INC_DIR . '/import.php',
WIE_INC_DIR . '/mime-types.php',
WIE_INC_DIR . '/page.php',
WIE_INC_DIR . '/widgets.php',
),
));
}
/**
* Load includes
*
* Include files based on whether or not condition is met.
*
* @since 0.1
* @access public
*/
public function loadIncludes()
{
// Get includes.
$includes = $this->includes;
// Loop conditions.
foreach ($includes as $condition => $files) {
$do_includes = false;
// Check condition.
// Change this to for statement so can use new lines without warning from wpcs - more readable.
switch ($condition) {
// Admin Only.
case 'admin':
if (is_admin()) {
$do_includes = true;
}
break;
// Frontend Only.
case 'frontend':
if (! is_admin()) {
$do_includes = true;
}
break;
// Admin or Frontend (always).
default:
$do_includes = true;
break;
}
// Loop files if condition met.
if ($do_includes) {
foreach ($files as $file) {
require_once trailingslashit(WIE_PATH) . $file;
}
}
}
}
}
// Instantiate the main class.
new Widget_Importer_Exporter();