| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Child Theme Wizard |
| 4 |
* Plugin URI: https://wpguru.tv |
| 5 |
* Description: Creates a child theme from any theme you have installed |
| 6 |
* Version: 1.3 |
| 7 |
* Author: Jay Versluis |
| 8 |
* Author URI: https://wpguru.tv |
| 9 |
* License: GPL2 |
| 10 |
* License URI: http://www.gnu.org/licenses/gpl-2.0.html |
| 11 |
*/ |
| 12 |
|
| 13 |
/* Copyright 2013-2019 Jay Versluis (email support@wpguru.tv) |
| 14 |
|
| 15 |
This program is free software; you can redistribute it and/or modify |
| 16 |
it under the terms of the GNU General Public License, version 2, as |
| 17 |
published by the Free Software Foundation. |
| 18 |
|
| 19 |
This program is distributed in the hope that it will be useful, |
| 20 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
GNU General Public License for more details. |
| 23 |
|
| 24 |
You should have received a copy of the GNU General Public License |
| 25 |
along with this program; if not, write to the Free Software |
| 26 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 27 |
*/ |
| 28 |
|
| 29 |
// add submenu page under Tools |
| 30 |
function ctw_menu() { |
| 31 |
global $starter_plugin_admin_page; |
| 32 |
$starter_plugin_admin_page = add_submenu_page ('tools.php', __('Child Theme Wizard', 'ctw'), __('Child Theme Wizard', 'ctw'), 'manage_options', 'ChildThemeWizard', 'ctwMainFunction'); |
| 33 |
} |
| 34 |
add_action('admin_menu', 'ctw_menu'); |
| 35 |
|
| 36 |
|
| 37 |
/////////////////////////////////////////////// |
| 38 |
// main function that generates the admin page |
| 39 |
function ctwMainFunction () { |
| 40 |
|
| 41 |
// check that the user has the required capability |
| 42 |
if (!current_user_can('manage_options')) |
| 43 |
{ |
| 44 |
wp_die( __('You do not have sufficient privileges to access this page. Sorry!') ); |
| 45 |
} |
| 46 |
|
| 47 |
/////////////////////////////////////// |
| 48 |
// MAIN AMDIN CONTENT SECTION |
| 49 |
/////////////////////////////////////// |
| 50 |
|
| 51 |
// display heading with icon WP style |
| 52 |
?> |
| 53 |
<div class="wrap"> |
| 54 |
<div id="icon-index" class="icon32"><br></div> |
| 55 |
<h2>Child Theme Wizard</h2> |
| 56 |
|
| 57 |
<?php |
| 58 |
|
| 59 |
// check if the button has been pressed |
| 60 |
if( isset($_POST[ 'hiddenfield' ]) && $_POST[ 'hiddenfield' ] == 'Y' ) { |
| 61 |
|
| 62 |
// call function with sanitized values |
| 63 |
$newchildtheme = array( |
| 64 |
'parent' => $_POST['parent'], |
| 65 |
'title' => sanitize_text_field($_POST['title']), |
| 66 |
'description' => sanitize_text_field($_POST['description']), |
| 67 |
'url' => sanitize_text_field($_POST['url']), |
| 68 |
'author' => sanitize_text_field($_POST['author']), |
| 69 |
'author-url' => sanitize_text_field($_POST['author-url']), |
| 70 |
'version' => sanitize_text_field($_POST['version']), |
| 71 |
'include-gpl' => $_POST['include-gpl'], |
| 72 |
); |
| 73 |
|
| 74 |
// create child theme - let's see if this worked |
| 75 |
$status = ctw_create_theme($newchildtheme); |
| 76 |
// ctw_testing($newchildtheme); |
| 77 |
|
| 78 |
// Put a "settings updated" message on the screen |
| 79 |
if ($status['alert'] == -1) { |
| 80 |
// panic - there was an error |
| 81 |
?> |
| 82 |
<div class="error"> |
| 83 |
<strong><p>Yikes - something went wrong:</p> |
| 84 |
<?php echo $status['message']; ?> |
| 85 |
</strong> |
| 86 |
</div> |
| 87 |
<div><strong><p>Would you like to <a href="<?php get_admin_url('tools.php?page=ChildThemeWizard'); ?>">try again?</a></p></strong></div> |
| 88 |
|
| 89 |
<?php |
| 90 |
} else { |
| 91 |
// everything went fine |
| 92 |
?> |
| 93 |
<div class="updated"> |
| 94 |
<strong> |
| 95 |
<?php echo $status['message']; ?> |
| 96 |
<p> |
| 97 |
<?php _e('Your Child Theme was created successfully!', 'ctw' ); ?> |
| 98 |
</strong></p></div> |
| 99 |
<div> |
| 100 |
<p>Head over to <a href="<?php echo admin_url('themes.php'); ?>">Appearance - Themes</a> to activate it.</p> |
| 101 |
<p>Add your custom styles in <a href="<?php echo admin_url('theme-editor.php'); ?>">Appearance - Editor</a>.</p> |
| 102 |
</div> |
| 103 |
<?php |
| 104 |
} |
| 105 |
} else { |
| 106 |
|
| 107 |
// display the new child theme dialogue |
| 108 |
?> |
| 109 |
|
| 110 |
<p>This simple wizard will help you generate a new <a href="https://codex.wordpress.org/Child_Themes" target="_blank">Child Theme</a> with just one click. </p> |
| 111 |
<p>Select which theme you want to use as a base, fill in the details and click "Create Child Theme".</p> |
| 112 |
<hr> |
| 113 |
<form name="ctwform" method="post" action=""> |
| 114 |
<input type="hidden" name="hiddenfield" value="Y"> |
| 115 |
|
| 116 |
|
| 117 |
<?php |
| 118 |
|
| 119 |
// fields for author and title |
| 120 |
// we can pre-populate some data too |
| 121 |
$current_user = wp_get_current_user(); |
| 122 |
?> |
| 123 |
<table width="600" border="0"> |
| 124 |
<tr> |
| 125 |
<td>Parent Theme</td> |
| 126 |
<td><?php |
| 127 |
// grab a list of all parent themes and iterate through them |
| 128 |
// $themes = wp_get_themes(); |
| 129 |
$themes = ctw_giveme_parents(); |
| 130 |
$currentTheme = wp_get_theme(); |
| 131 |
echo '<select name="parent">'; |
| 132 |
foreach ($themes as $theme) { |
| 133 |
// if it's the current theme, make it selected |
| 134 |
if($currentTheme->get('Name') == $theme->get('Name')) { |
| 135 |
echo '<option value ="' . $theme->get_stylesheet() . '" selected>' . $theme->get('Name') . ' (currently active)</option>'; |
| 136 |
} else { |
| 137 |
echo '<option value ="' . $theme->get_stylesheet() . '">' . $theme->get('Name') . '</option>'; |
| 138 |
} |
| 139 |
} |
| 140 |
echo '</select>'; |
| 141 |
?></td> |
| 142 |
<td><em></em></td> |
| 143 |
</tr> |
| 144 |
<tr> |
| 145 |
<td>Title</td> |
| 146 |
<td><input type="text" name="title" value="" size="20"></td> |
| 147 |
<td><em>what's your new Child Theme called?</em></td> |
| 148 |
</tr> |
| 149 |
<tr> |
| 150 |
<td>Description</td> |
| 151 |
<td><input type="text" name="description" value="" size="20"></td> |
| 152 |
<td><em>a few notes about your Child Theme</em></td> |
| 153 |
</tr> |
| 154 |
<tr> |
| 155 |
<td>Child Theme URL</td> |
| 156 |
<td><input type="text" name="url" value="" size="20"></td> |
| 157 |
<td><em>does it have a website or release post?</em></td> |
| 158 |
</tr> |
| 159 |
<tr> |
| 160 |
<td>Author</td> |
| 161 |
<td><input type="text" name="author" value="<?php echo $current_user->display_name; ?>" size="20"></td> |
| 162 |
<td><em>that's you</em></td> |
| 163 |
</tr> |
| 164 |
<tr> |
| 165 |
<td>Author URL</td> |
| 166 |
<td><input type="text" name="author-url" value="<?php echo $current_user->user_url; ?>" size="20"></td> |
| 167 |
<td><em>that's your website</em></td> |
| 168 |
</tr> |
| 169 |
<tr> |
| 170 |
<td>Version</td> |
| 171 |
<td><input type="text" name="version" value="1.0" size="20"></td> |
| 172 |
<td><em>start with 1.0 and work your way up</em></td> |
| 173 |
</tr> |
| 174 |
<tr> |
| 175 |
<td>Include GPL License</td> |
| 176 |
<td> |
| 177 |
<select name="include-gpl"> |
| 178 |
<option value="on">Yes Please!</option> |
| 179 |
<option value="off">No Thanks</option> |
| 180 |
</select> |
| 181 |
</td> |
| 182 |
<td><em></em></td> |
| 183 |
</tr> |
| 184 |
</table> |
| 185 |
|
| 186 |
<p class="submit"> |
| 187 |
<input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Create Child Theme') ?>" /> |
| 188 |
</p> |
| 189 |
|
| 190 |
<?php // end of else |
| 191 |
} |
| 192 |
?> |
| 193 |
<br><hr><br> |
| 194 |
|
| 195 |
</div> <!-- end of main wrap --> |
| 196 |
|
| 197 |
<?php // display the footer ?> |
| 198 |
<p><a href="https://wpguru.co.uk" target="_blank"><img src="<?php |
| 199 |
echo plugins_url('images/guru-header-2013.png', __FILE__); ?>" width="300"></a> </p> |
| 200 |
|
| 201 |
<p><a href="https://wpguru.co.uk/2014/03/introducing-child-theme-wizard-for-wordpress/" target="_blank">Plugin by Jay Versluis</a> | <a href="https://github.com/versluis/Child-Theme-Wizard" target="_blank">Fork me or Contribute on GitHub</a> | <a href="https://patreon.com/versluis" target="_blank">Support me on Patreon</a></p> |
| 202 |
|
| 203 |
<p><span><!-- Social Buttons --> |
| 204 |
|
| 205 |
<!-- YouTube --> |
| 206 |
<script src="https://apis.google.com/js/platform.js"></script> |
| 207 |
<div class="g-ytsubscribe" data-channel="wphosting"></div> |
| 208 |
|
| 209 |
<!-- Place this tag after the last widget tag. --> |
| 210 |
<script type="text/javascript"> |
| 211 |
(function() { |
| 212 |
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; |
| 213 |
po.src = 'https://apis.google.com/js/platform.js'; |
| 214 |
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s); |
| 215 |
})(); |
| 216 |
</script> |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
<!-- Twitter --> |
| 221 |
<a href="https://twitter.com/versluis" class="twitter-follow-button" data-show-count="true">Follow @versluis</a> |
| 222 |
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script> |
| 223 |
|
| 224 |
<!-- Facebook --> |
| 225 |
<iframe src="//www.facebook.com/plugins/like.php?href=https%3A%2F%2Fwww.facebook.com%2Fpages%2FThe-WP-Guru%2F162188713810370&width&layout=button_count&action=like&show_faces=true&share=true&height=21&appId=186277158097599" scrolling="no" frameborder="0" style="border:none; overflow:hidden; height:21px;" allowTransparency="true"></iframe> |
| 226 |
|
| 227 |
</span></p> |
| 228 |
|
| 229 |
<?php |
| 230 |
} // end of main function |
| 231 |
|
| 232 |
/* |
| 233 |
* this function creates the new directory and file |
| 234 |
*/ |
| 235 |
|
| 236 |
function ctw_create_theme($childtheme) { |
| 237 |
|
| 238 |
// add messages to this variable |
| 239 |
$status = array(); |
| 240 |
$status['alert'] = 0; |
| 241 |
|
| 242 |
// create the directory |
| 243 |
$directory = get_theme_root() . '/' . sanitize_file_name($childtheme['title']); |
| 244 |
// echo '<p>The child theme directory will be created at ' . $directory; |
| 245 |
if (!mkdir($directory)) { |
| 246 |
$status['message'] = "<p>Could not create directory. Does it exist already?</p>"; |
| 247 |
$status['alert'] = -1; |
| 248 |
return $status; |
| 249 |
} else { |
| 250 |
$status['message'] = "<p>Directory created successfully.</p>"; |
| 251 |
} |
| 252 |
|
| 253 |
// create style.css in our directory |
| 254 |
$filename = $directory . '/' . 'style.css'; |
| 255 |
$handle = fopen($filename, 'w'); |
| 256 |
|
| 257 |
// add meta data |
| 258 |
$data = "/* \n Theme Name: " . $childtheme['title']; |
| 259 |
$data = $data . "\n Theme URI: " . $childtheme['url']; |
| 260 |
$data = $data . "\n Description: " . $childtheme['description']; |
| 261 |
$data = $data . "\n Author: " . $childtheme['author']; |
| 262 |
$data = $data . "\n Author URI: " . $childtheme['author-url']; |
| 263 |
$data = $data . "\n Template: " . $childtheme['parent']; |
| 264 |
$data = $data . "\n Version: " . $childtheme['version']; |
| 265 |
|
| 266 |
// insert GPL License Terms |
| 267 |
if ($childtheme['include-gpl'] == 'on') { |
| 268 |
$data = $data . "\n License: GNU General Public License v2 or later"; |
| 269 |
$data = $data . "\n License URI: http://www.gnu.org/licenses/gpl-2.0.html"; |
| 270 |
} |
| 271 |
|
| 272 |
// adding the call to the parent style sheet in CSS is no longer best practice |
| 273 |
// $data = $data . "\n*/\n\n" . '@import url("../' . $childtheme['parent'] . '/style.css");'; |
| 274 |
$data = $data . "\n\n /* == Add your own styles below this line =="; |
| 275 |
$data = $data . "\n--------------------------------------------*/\n\n"; |
| 276 |
|
| 277 |
// write data and close the file |
| 278 |
if (fwrite($handle, $data) == false) { |
| 279 |
$status['message'] = $status['message'] . "<p>There was a problem writing data to style.css.</p>"; |
| 280 |
$status['alert'] = -1; |
| 281 |
return $status; |
| 282 |
} else { |
| 283 |
$status['message'] = $status['message'] . "<p>Writing data to style.css...</p>"; |
| 284 |
} |
| 285 |
fclose($handle); |
| 286 |
|
| 287 |
// create functions.php |
| 288 |
$filename = $directory . '/' . 'functions.php'; |
| 289 |
$handle = fopen($filename, 'w'); |
| 290 |
|
| 291 |
// add some meta data |
| 292 |
$data = "<?php /*\n\n This file is part of a child theme called " . $childtheme['title'] . "."; |
| 293 |
$data = $data . "\n Functions in this file will be loaded before the parent theme's functions."; |
| 294 |
$data = $data . "\n For more information, please read https://developer.wordpress.org/themes/advanced-topics/child-themes/"; |
| 295 |
$data = $data . "\n\n*/"; |
| 296 |
|
| 297 |
// add call to enqueue parent style sheet via functions.php |
| 298 |
$data = $data . "\n\n// this code loads the parent's stylesheet (leave it in place unless you know what you're doing)"; |
| 299 |
$data = $data . "\n\nfunction your_theme_enqueue_styles() {"; |
| 300 |
$data = $data . "\n wp_enqueue_style( 'parent-style', \n"; |
| 301 |
$data = $data . " get_template_directory_uri() . '/style.css'); \n"; |
| 302 |
|
| 303 |
$data = $data . "\n wp_enqueue_style( 'child-style', \n"; |
| 304 |
$data = $data . " get_stylesheet_directory_uri() . '/style.css', \n"; |
| 305 |
$data = $data . " array(". '$parent_style' ."), \n"; |
| 306 |
$data = $data . " wp_get_theme()->get('Version') \n"; |
| 307 |
$data = $data . " );\n}"; |
| 308 |
$data = $data . "\n\nadd_action('wp_enqueue_scripts', 'your_theme_enqueue_styles');"; |
| 309 |
|
| 310 |
$data = $data . "\n\n/* Add your own functions below this line."; |
| 311 |
$data = $data . "\n ======================================== */ \n\n"; |
| 312 |
|
| 313 |
// write data and close the file |
| 314 |
if (fwrite($handle, $data) == false) { |
| 315 |
$status['message'] = $status['message'] . "<p>There was a problem writing data to functions.php.</p>"; |
| 316 |
$status['alert'] = -1; |
| 317 |
return $status; |
| 318 |
} else { |
| 319 |
$status['message'] = $status['message'] . "<p>Writing data to functions.php...</p>"; |
| 320 |
} |
| 321 |
fclose($handle); |
| 322 |
|
| 323 |
// create a nice screenshot |
| 324 |
$thumbnail_status = ctw_make_thumbnail($directory); |
| 325 |
$status['message'] = $status['message'] . $thumbnail_status; |
| 326 |
|
| 327 |
return $status; |
| 328 |
} |
| 329 |
|
| 330 |
// return an array only of parent themes |
| 331 |
function ctw_giveme_parents() { |
| 332 |
|
| 333 |
// first we'll grab all installed themes |
| 334 |
$allThemes = wp_get_themes(); |
| 335 |
$parentThemes = array(); |
| 336 |
|
| 337 |
// next we'll add all parents and return the result |
| 338 |
foreach ($allThemes as $theme) { |
| 339 |
|
| 340 |
if ($theme->parent() == false) { |
| 341 |
$parentThemes[] = $theme; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
return $parentThemes; |
| 346 |
} |
| 347 |
|
| 348 |
// create a new thumbnail |
| 349 |
function ctw_make_thumbnail($childpath) { |
| 350 |
|
| 351 |
// for now we'll just copy an existing image |
| 352 |
$thumbnail = plugins_url('images/screenshot.png', __FILE__); |
| 353 |
$destination = $childpath . '/screenshot.png'; |
| 354 |
if (!copy($thumbnail, $destination)) { |
| 355 |
return "<p>Could not copy thumbnail image :-(</p>"; |
| 356 |
} else { |
| 357 |
return "<p>Copied thumbnail file over - looking good!</p>"; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
// quick tests go here |
| 362 |
function ctw_testing($childtheme) { |
| 363 |
|
| 364 |
$directory = get_theme_root() . '/' . sanitize_file_name($childtheme['title']); |
| 365 |
ctw_make_thumbnail($directory); |
| 366 |
} |
| 367 |
|
| 368 |
?> |
| 369 |
|