| 1 |
<?php |
| 2 |
|
| 3 |
namespace FL\Assistant\Services; |
| 4 |
|
| 5 |
use FL\Assistant\Services\ThemeService; |
| 6 |
use FL\Assistant\Helpers\CustomizerOptionHelper; |
| 7 |
|
| 8 |
class CustomizerService { |
| 9 |
|
| 10 |
/** |
| 11 |
* @return array |
| 12 |
*/ |
| 13 |
public function export_settings() { |
| 14 |
$wp_customize = $this->init_customizer(); |
| 15 |
$theme_service = new ThemeService(); |
| 16 |
$theme = $theme_service->get_current_theme_data(); |
| 17 |
|
| 18 |
$data = array( |
| 19 |
'mods' => array(), |
| 20 |
'options' => array(), |
| 21 |
'css' => '', |
| 22 |
'theme' => $theme, |
| 23 |
); |
| 24 |
|
| 25 |
$ignore = array( |
| 26 |
'blogname', |
| 27 |
'blogdescription', |
| 28 |
'show_on_front', |
| 29 |
'page_on_front', |
| 30 |
'page_for_posts', |
| 31 |
'nav_menu_locations', |
| 32 |
'nav_menus_created_posts', |
| 33 |
'sidebars_widgets', |
| 34 |
'site_icon', |
| 35 |
'custom_css_post_id', |
| 36 |
); |
| 37 |
|
| 38 |
$mods = get_theme_mods(); |
| 39 |
$settings = $wp_customize->settings(); |
| 40 |
|
| 41 |
foreach ( $mods as $key => $value ) { |
| 42 |
if ( in_array( $key, $ignore ) ) { |
| 43 |
continue; |
| 44 |
} |
| 45 |
$data['mods'][ $key ] = $value; |
| 46 |
} |
| 47 |
|
| 48 |
foreach ( $settings as $key => $setting ) { |
| 49 |
if ( 'option' === $setting->type ) { |
| 50 |
if ( 0 === strpos( $key, 'widget_' ) || 0 === strpos( $key, 'sidebars_' ) ) { |
| 51 |
continue; |
| 52 |
} elseif ( in_array( $key, $ignore ) ) { |
| 53 |
continue; |
| 54 |
} |
| 55 |
$data['options'][ $key ] = $setting->value(); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if ( function_exists( 'wp_get_custom_css_post' ) ) { |
| 60 |
$data['css'] = wp_get_custom_css(); |
| 61 |
} |
| 62 |
|
| 63 |
return $data; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @param array $data |
| 68 |
* @return bool|WP_Error |
| 69 |
*/ |
| 70 |
public function can_import_settings( $data ) { |
| 71 |
$theme = $data['theme']; |
| 72 |
$slug = $theme['slug']; |
| 73 |
$parentSlug = $theme['parent'] ? $theme['parent']['slug'] : null; |
| 74 |
$stylesheet = get_stylesheet(); |
| 75 |
$template = get_template(); |
| 76 |
|
| 77 |
if ( $slug !== $stylesheet && $slug !== $template && $parentSlug !== $stylesheet ) { |
| 78 |
return new \WP_Error( 'import', __( 'Import failed! These settings are not for the current theme.' ) ); |
| 79 |
} |
| 80 |
|
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @param array $data |
| 86 |
* @return bool|WP_Error |
| 87 |
*/ |
| 88 |
public function import_settings( $data ) { |
| 89 |
$wp_customize = $this->init_customizer(); |
| 90 |
$can_import = $this->can_import_settings( $data ); |
| 91 |
|
| 92 |
if ( is_wp_error( $can_import ) ) { |
| 93 |
return $can_import; |
| 94 |
} |
| 95 |
|
| 96 |
foreach ( $data['options'] as $option_key => $option_value ) { |
| 97 |
$option = new CustomizerOptionHelper( $wp_customize, $option_key, array( |
| 98 |
'default' => '', |
| 99 |
'type' => 'option', |
| 100 |
'capability' => 'edit_theme_options' |
| 101 |
) ); |
| 102 |
$option->import( $option_value ); |
| 103 |
} |
| 104 |
|
| 105 |
if( function_exists( 'wp_update_custom_css_post' ) && $data['css'] ) { |
| 106 |
wp_update_custom_css_post( $data['css'] ); |
| 107 |
} |
| 108 |
|
| 109 |
do_action( 'customize_save', $wp_customize ); |
| 110 |
|
| 111 |
foreach ( $data['mods'] as $key => $val ) { |
| 112 |
do_action( 'customize_save_' . $key, $wp_customize ); |
| 113 |
set_theme_mod( $key, $val ); |
| 114 |
} |
| 115 |
|
| 116 |
do_action( 'customize_save_after', $wp_customize ); |
| 117 |
|
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Dynamically initialize the customizer. Inspired by the core |
| 123 |
* function _wp_customize_publish_changeset. |
| 124 |
* |
| 125 |
* @return object |
| 126 |
*/ |
| 127 |
protected function init_customizer() { |
| 128 |
global $wp_customize; |
| 129 |
|
| 130 |
if ( ! class_exists( 'WP_Customize_Manager' ) ) { |
| 131 |
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; |
| 132 |
} |
| 133 |
if ( ! $wp_customize ) { |
| 134 |
$wp_customize = new \WP_Customize_Manager(); |
| 135 |
} |
| 136 |
if ( ! did_action( 'customize_register' ) ) { |
| 137 |
remove_action( 'customize_register', array( $wp_customize, 'register_controls' ) ); |
| 138 |
$wp_customize->register_controls(); |
| 139 |
do_action( 'customize_register', $wp_customize ); |
| 140 |
} |
| 141 |
|
| 142 |
return $wp_customize; |
| 143 |
} |
| 144 |
} |
| 145 |
|