-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathuse-child-theme.php
More file actions
183 lines (140 loc) · 5.57 KB
/
Copy pathuse-child-theme.php
File metadata and controls
183 lines (140 loc) · 5.57 KB
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
<?php
/*
* Use Child Theme
* A drop-in to make it easy to use WordPress child themes
* @version 0.4
*/
defined( 'ABSPATH' ) or exit;
if ( ! class_exists( 'Use_Child_Theme' ) && is_admin() ) {
class Use_Child_Theme
{
public $theme;
public $child_slug;
function __construct() {
add_action( 'admin_init', array( $this, 'admin_init' ) );
}
function admin_init() {
// Exit if unauthorized
if ( ! current_user_can( 'switch_themes' ) ) {
return;
}
// Exit if dismissed
if ( false !== get_transient( 'uct_dismiss_notice' ) ) {
return;
}
$this->theme = wp_get_theme();
// Exit if child theme
if ( false !== $this->theme->parent() ) {
return;
}
// Exit if no direct access
if ( 'direct' != get_filesystem_method() ) {
return;
}
add_action( 'wp_ajax_uct_activate', array( $this, 'activate_child_theme' ) );
add_action( 'wp_ajax_uct_dismiss', array( $this, 'dismiss_notice' ) );
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
}
function admin_notices() {
// Show only on specific admin page(s) (default: Appearance > Editor)
$admin_notices_screen_id = apply_filters( 'uct_admin_notices_screen_id', array( 'theme-editor' ) );
$screen = get_current_screen();
if ( ! isset( $screen->id ) || ! in_array( $screen->id, (array) $admin_notices_screen_id ) ) {
return;
}
?>
<script>
(function($) {
$(function() {
$(document).on('click', '.uct-activate', function() {
$.post(ajaxurl, { action: 'uct_activate' }, function(response) {
$('.uct-notice p').html(response);
});
});
$(document).on('click', '.uct-notice .notice-dismiss', function() {
$.post(ajaxurl, { action: 'uct_dismiss' });
});
});
})(jQuery);
</script>
<div class="notice notice-error uct-notice is-dismissible">
<p>
<?php printf( esc_html__( 'Please use a %s child theme to make changes', 'use-child-theme' ), $this->theme->get( 'Name' ) ); ?>
<a class="uct-activate" href="javascript:;"><?php esc_html_e( 'Activate now', 'use-child-theme' ); ?></a>
</p>
</div>
<?php
}
function dismiss_notice() {
set_transient( 'uct_dismiss_notice', 'yes', apply_filters( 'uct_dismiss_timeout', 86400 ) );
exit;
}
function has_child_theme() {
$themes = wp_get_themes();
$folder_name = $this->theme->get_stylesheet();
$this->child_slug = $folder_name . '-child';
foreach ( $themes as $theme ) {
if ( $folder_name == $theme->get( 'Template' ) ) {
$this->child_slug = $theme->get_stylesheet();
return true;
}
}
return false;
}
function activate_child_theme() {
$parent_slug = $this->theme->get_stylesheet();
// Create child theme
if ( ! $this->has_child_theme() ) {
$this->create_child_theme();
}
switch_theme( $this->child_slug );
// Copy customizer settings, widgets, etc.
$settings = get_option( 'theme_mods_' . $this->child_slug );
if ( false === $settings ) {
$parent_settings = get_option( 'theme_mods_' . $parent_slug );
update_option( 'theme_mods_' . $this->child_slug, $parent_settings );
}
wp_die( esc_html__( 'All done!', 'use-child-theme' ) );
}
function create_child_theme() {
$parent_dir = $this->theme->get_stylesheet_directory();
$child_dir = $parent_dir . '-child';
if ( wp_mkdir_p( $child_dir ) ) {
$creds = request_filesystem_credentials( admin_url() );
WP_Filesystem( $creds ); // we already have direct access
global $wp_filesystem;
$wp_filesystem->put_contents( $child_dir . '/style.css', $this->style_css() );
$wp_filesystem->put_contents( $child_dir . '/functions.php', $this->functions_php() );
if ( false !== ( $img = $this->theme->get_screenshot( 'relative' ) ) ) {
$wp_filesystem->copy( "$parent_dir/$img", "$child_dir/$img" );
}
}
else {
wp_die( esc_html__( 'Error: theme folder not writable', 'use-child-theme' ) );
}
}
function style_css() {
$name = $this->theme->get( 'Name' ) . ' Child';
$uri = $this->theme->get( 'ThemeURI' );
$parent = $this->theme->get_stylesheet();
$output = "/*
Theme Name: {$name}
Theme URI: {$uri}
Template: {$parent}
Version: 1.0
*/
";
return apply_filters( 'uct_style_css', $output );
}
function functions_php() {
$output = "<?php
function child_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
add_action( 'wp_enqueue_scripts', 'child_enqueue_styles' );
";
return apply_filters( 'uct_functions_php', $output );
}
}
new Use_Child_Theme();
}