-
Notifications
You must be signed in to change notification settings - Fork 2
/
famethemes-demo-importer.php
576 lines (484 loc) · 18.4 KB
/
famethemes-demo-importer.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
<?php
/*
Plugin Name: FameTheme Demo Importer
Plugin URI: https://github.com/FameThemes/famethemes-demo-importer
Description: Demo data import tool for FameThemes's themes.
Author: FameThemes
Author URI: http://www.famethemes.com/
Version: 1.1.7
Text Domain: demo-contents
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
define('DEMO_CONTENT_URL', trailingslashit(plugins_url('', __FILE__)));
define('DEMO_CONTENT_PATH', trailingslashit(plugin_dir_path(__FILE__)));
class Demo_Contents
{
public $dir;
public $url;
private static $git_repo = 'FameThemes/famethemes-xml-demos';
public $dashboard;
public $progress;
static $_instance;
static function get_instance()
{
if (is_null(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
function __construct()
{
require_once DEMO_CONTENT_PATH . 'inc/functions.php';
require_once DEMO_CONTENT_PATH . 'inc/class-tgm-plugin-activation.php';
require_once DEMO_CONTENT_PATH . 'inc/theme-supports.php';
require_once DEMO_CONTENT_PATH . 'inc/class-dashboard.php';
require_once DEMO_CONTENT_PATH . 'inc/class-progress.php';
$this->dashboard = new Demo_Content_Dashboard();
$this->progress = new Demo_Contents_Progress();
if (is_admin()) {
add_action('init', array($this, 'export'));
}
}
static function get_github_repo()
{
return apply_filters('demo_contents_github_repo', self::$git_repo);
}
static function php_support()
{
return version_compare(PHP_VERSION, '5.6.20', '>=');
}
/**
* Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
*
* @since 2.6.0
*
* @param array $file_array Array similar to a `$_FILES` upload array.
* @param int $post_id The post ID the media is associated with.
* @param string $desc Optional. Description of the side-loaded file. Default null.
* @param array $post_data Optional. Post data to override. Default empty array.
* @return int|object The ID of the attachment or a WP_Error on failure.
*/
static function media_handle_sideload($file_array, $post_id, $desc = null, $post_data = array(), $save_attachment = true)
{
$overrides = array(
'test_form' => false,
'test_type' => false
);
$time = current_time('mysql');
if ($post = get_post($post_id)) {
if (substr($post->post_date, 0, 4) > 0)
$time = $post->post_date;
}
$file = wp_handle_sideload($file_array, $overrides, $time);
if (isset($file['error']))
return new WP_Error('upload_error', $file['error']);
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$title = preg_replace('/\.[^.]+$/', '', basename($file));
$content = '';
if ($save_attachment) {
if (isset($desc)) {
$title = $desc;
}
// Construct the attachment array.
$attachment = array_merge(array(
'post_mime_type' => $type,
'guid' => $url,
'post_parent' => $post_id,
'post_title' => $title,
'post_content' => $content,
), $post_data);
// This should never be set as it would then overwrite an existing attachment.
unset($attachment['ID']);
// Save the attachment metadata
$id = wp_insert_attachment($attachment, $file, $post_id);
return $id;
} else {
return $file;
}
}
/**
* Download image form url
*
* @return bool
*/
static function download_file($url, $name = '', $save_attachment = true)
{
if (!$url || empty($url)) {
return false;
}
// These files need to be included as dependencies when on the front end.
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
$file_array = array();
// Download file to temp location.
$file_array['tmp_name'] = download_url($url);
// If error storing temporarily, return the error.
if (empty($file_array['tmp_name']) || is_wp_error($file_array['tmp_name'])) {
return false;
}
if ($name) {
$file_array['name'] = $name;
} else {
$file_array['name'] = basename($url);
}
// Do the validation and storage stuff.
$file_path_or_id = self::media_handle_sideload($file_array, 0, null, array(), $save_attachment);
// If error storing permanently, unlink.
if (is_wp_error($file_path_or_id)) {
@wp_delete_file($file_array['tmp_name']);
return false;
}
return $file_path_or_id;
}
/**
* Available widgets
*
* Gather site's widgets into array with ID base, name, etc.
* Used by export and import functions.
*
* @since 0.4
* @global array $wp_registered_widget_updates
* @return array Widget information
*/
static function get_available_widgets()
{
global $wp_registered_widget_controls;
$widget_controls = $wp_registered_widget_controls;
$available_widgets = array();
foreach ($widget_controls as $widget) {
if (!empty($widget['id_base']) && !isset($available_widgets[$widget['id_base']])) { // no dupes
$available_widgets[$widget['id_base']]['id_base'] = $widget['id_base'];
$available_widgets[$widget['id_base']]['name'] = $widget['name'];
}
}
return $available_widgets;
}
static function get_update_keys()
{
$key = 'demo_contents_customizer_keys';
$theme_slug = get_option('stylesheet');
$data = get_option($key);
if (!is_array($data)) {
$data = array();
}
if (isset($data[$theme_slug])) {
return $data[$theme_slug];
}
$r = wp_remote_post(
admin_url('customize.php'),
array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'cookies' => array(
SECURE_AUTH_COOKIE => $_COOKIE[SECURE_AUTH_COOKIE],
AUTH_COOKIE => $_COOKIE[AUTH_COOKIE],
LOGGED_IN_COOKIE => $_COOKIE[LOGGED_IN_COOKIE],
)
)
);
if (is_wp_error($r)) {
return false;
} else {
global $wpdb;
$row = $wpdb->get_row($wpdb->prepare("SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $key));
$notoptions = wp_cache_get('notoptions', 'options');
// Has to be get_row instead of get_var because of funkiness with 0, false, null values
if (is_object($row)) {
$value = $row->option_value;
$data = apply_filters('option_' . $key, maybe_unserialize($value), $key);
wp_cache_add($key, $value, 'options');
} else { // option does not exist, so we must cache its non-existence
if (!is_array($notoptions)) {
$notoptions = array();
}
$notoptions[$key] = true;
wp_cache_set('notoptions', $notoptions, 'options');
/** This filter is documented in wp-includes/option.php */
$data = apply_filters('default_option_' . $key, '', $key);
}
if (!is_array($data)) {
$data = array();
}
if (isset($data[$theme_slug])) {
return $data[$theme_slug];
}
}
return false;
}
/**
* Generate Widgets export data
*
* @since 0.1
* @return string Export file contents
*/
static function generate_widgets_export_data()
{
// Get all available widgets site supports
$available_widgets = self::get_available_widgets();
// Get all widget instances for each widget
$widget_instances = array();
foreach ($available_widgets as $widget_data) {
// Get all instances for this ID base
$instances = get_option('widget_' . $widget_data['id_base']);
// Have instances
if (!empty($instances)) {
// Loop instances
foreach ($instances as $instance_id => $instance_data) {
// Key is ID (not _multiwidget)
if (is_numeric($instance_id)) {
$unique_instance_id = $widget_data['id_base'] . '-' . $instance_id;
$widget_instances[$unique_instance_id] = $instance_data;
}
}
}
}
// Gather sidebars with their widget instances
$sidebars_widgets = get_option('sidebars_widgets'); // get sidebars and their unique widgets IDs
$sidebars_widget_instances = array();
foreach ($sidebars_widgets as $sidebar_id => $widget_ids) {
// Skip inactive widgets
if ('wp_inactive_widgets' == $sidebar_id) {
continue;
}
// Skip if no data or not an array (array_version)
if (!is_array($widget_ids) || empty($widget_ids)) {
continue;
}
// Loop widget IDs for this sidebar
foreach ($widget_ids as $widget_id) {
// Is there an instance for this widget ID?
if (isset($widget_instances[$widget_id])) {
// Add to array
$sidebars_widget_instances[$sidebar_id][$widget_id] = $widget_instances[$widget_id];
}
}
}
// Filter pre-encoded data
$data = apply_filters('ft_demo_export_widgets_data', $sidebars_widget_instances);
// Encode the data for file contents
return $data;
}
static function get_widgets_config_fields($config)
{
$_config = array();
foreach ($config as $k => $f) {
switch ($f['type']) {
case 'list_cat':
$_config[$f['name']] = array(
'type' => 'term',
'tax' => 'category'
);
break;
case 'group':
foreach ($f['fields'] as $_k => $_f) {
if ($_f['type'] == 'source') {
if (isset($_f['source']['post_type'])) {
$_config[$_f['name']] = 'post';
} else {
$_config[$_f['name']] = array(
'type' => 'term',
'tax' => $_f['source']['tax']
);
}
} else {
$_config[$_f['name']] = $_f['type'];
}
}
$_config[$f['name']] = 'repeater';
break;
default:
if ($f['type'] == 'source') {
if (isset($f['source']['post_type'])) {
$_config[$f['name']] = 'post';
} else {
$_config[$f['name']] = array(
'type' => 'term',
'tax' => $f['source']['tax']
);
}
} else {
$_config[$f['name']] = $f['type'];
}
break;
}
}
return $_config;
}
static function get_widgets_config()
{
global $wp_registered_widget_controls;
$widget_instances = array();
foreach ($wp_registered_widget_controls as $widget_id => $widget) {
$base_id = isset($widget['id_base']) ? $widget['id_base'] : null;
if (!empty($base_id) && !isset($widget_instances[$base_id])) {
$widget_instances[$base_id] = '';
}
}
global $wp_widget_factory;
foreach ($wp_widget_factory->widgets as $class_name => $object) {
$config = array();
if (method_exists($object, 'config')) {
$config = $object->config();
}
// get_configs
if (method_exists($object, 'get_configs')) {
$config = $object->get_configs();
}
$widget_instances[$object->id_base] = self::get_widgets_config_fields($config);
}
return $widget_instances;
}
static function generate_config()
{
$nav_menu_locations = get_theme_mod('nav_menu_locations');
// Just update the customizer keys
$regen_keys = self::get_update_keys();
$config = array(
'home_url' => home_url('/'),
'menus' => $nav_menu_locations,
'pages' => array(
'page_on_front' => get_option('page_on_front'),
'page_for_posts' => get_option('page_for_posts'),
),
'options' => array(
'show_on_front' => get_option('show_on_front')
),
'theme_mods' => get_theme_mods(),
'widgets' => self::generate_widgets_export_data(),
'widgets_config' => self::get_widgets_config(),
'customizer_keys' => $regen_keys
);
$config = apply_filters('demo_contents_generate_config', $config);
return wp_json_encode($config, JSON_NUMERIC_CHECK);
}
function export()
{
if (!isset($_REQUEST['demo_contents_export'])) {
return;
}
if (!current_user_can('export')) {
return;
}
ob_start();
ob_end_clean();
ob_flush();
/**
* Filters the export filename.
*
* @since 4.4.0
*
* @param string $wp_filename The name of the file for download.
* @param string $sitename The site name.
* @param string $date Today's date, formatted.
*/
$filename = 'config.json';
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename=' . $filename);
header('Content-Type: application/xml; charset=' . get_option('blog_charset'), true);
echo Demo_Contents::generate_config();
die();
}
/**
* Check if an item exists out there in the "ether".
*
* @param string $url - preferably a fully qualified URL
* @return boolean - true if it is out there somewhere
*/
static function url_exists($url)
{
if (($url == '') || ($url == null)) {
return false;
}
if (strpos($url, home_url()) !== false) {
$args = array(
'method' => 'GET',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'cookies' => array(
SECURE_AUTH_COOKIE => isset($_COOKIE[SECURE_AUTH_COOKIE]) ? $_COOKIE[SECURE_AUTH_COOKIE] : null,
AUTH_COOKIE => isset($_COOKIE[AUTH_COOKIE]) ? $_COOKIE[AUTH_COOKIE] : null,
LOGGED_IN_COOKIE => isset($_COOKIE[LOGGED_IN_COOKIE]) ? $_COOKIE[LOGGED_IN_COOKIE] : null,
)
);
} else {
$args = array();
}
$response = wp_remote_get($url, $args);
$code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body($response);
$body = wp_unslash($body);
if (strpos($body, 'id="error-page"') !== false) {
return false;
}
$accepted_status_codes = array(200, 301);
if (!is_wp_error($response) && in_array(wp_remote_retrieve_response_code($response), $accepted_status_codes)) {
return true;
}
return false;
}
}
if (is_admin()) {
function demo_contents__init()
{
new Demo_Contents();
}
add_action('plugins_loaded', 'demo_contents__init');
}
/**
* Redirect to import page
*
* @param $plugin
* @param bool|false $network_wide
*/
function demo_contents_importer_plugin_activate($plugin, $network_wide = false)
{
if (!$network_wide && $plugin == plugin_basename(__FILE__)) {
$template_slug = get_option('template');
$url = add_query_arg(
array(
'page' => 'ft_' . $template_slug,
'tab' => 'demo-data-importer',
),
admin_url('themes.php')
);
// Check url exists
if (Demo_Contents::url_exists($url)) {
wp_redirect($url);
die();
}
$url = add_query_arg(
array(
'page' => $template_slug,
'tab' => 'demo-data-importer',
),
admin_url('themes.php')
);
if (Demo_Contents::url_exists($url)) {
wp_redirect($url);
die();
}
}
}
add_action('activated_plugin', 'demo_contents_importer_plugin_activate', 90, 2);
// Support Upload XML file
add_filter('upload_mimes', 'demo_contents_custom_upload_xml');
function demo_contents_custom_upload_xml($mimes)
{
if (current_user_can('upload_files')) {
$mimes = array_merge($mimes, array(
'xml' => 'application/xml',
'json' => 'application/json'
));
}
return $mimes;
}