PluginProbe
Child Theme Wizard / 1.1
Child Theme Wizard v1.1
trunk 1.0 1.1 1.2 1.3 1.4 1.5
child-theme-wizard / child-theme-wizard.php

child-theme-wizard.php in Child Theme Wizard 1.1, at child-theme-wizard.php

380 lines 13.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Child Theme Wizard
4 * Plugin URI: http://wpguru.co.uk
5 * Description: Creates a child theme from any theme you have installed
6 * Version: 1.1
7 * Author: Jay Versluis
8 * Author URI: http://wpguru.co.uk
9 * License: GPL2
10 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
11 */
12
13 /* Copyright 2013 Jay Versluis (email support@wpguru.co.uk)
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>Seletct which theme you want to use as a base, fill in the details and click &quot;Create Child Theme&quot;.</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>
196 <ul>
197 <li>DONE: List all available themes as drop down</li>
198 <li>DONE: let user enter values</li>
199 <li>DONE: submit button </li>
200 <li>DONE: sanitize text fields</li>
201 <li>DONE: function that creates the child theme directory and file</li>
202 <li>DONE: write contents to file</li>
203 <li>create screenshot</li>
204 <li>DONE: move to Tools and correct main function name</li>
205 <li>update footer links</li>
206 <li>DONE: create Git reop</li>
207 <li>DONE: filter out child themes
208
209 </li>
210 </ul>
211 </div>
212 -->
213 </div> <!-- end of main wrap -->
214
215 <?php // display the footer ?>
216 <p><a href="http://wpguru.co.uk" target="_blank"><img src="<?php
217 echo plugins_url('images/guru-header-2013.png', __FILE__); ?>" width="300"></a> </p>
218
219 <p><a href="http://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="http://wphosting.tv" target="_blank">WP Hosting</a></p>
220
221 <p><span><!-- Social Buttons -->
222
223 <!-- Google+ -->
224 <div class="g-follow" data-annotation="bubble" data-height="20" data-href="//plus.google.com/116464794189222694062" data-rel="author"></div>
225
226 <!-- Place this tag after the last widget tag. -->
227 <script type="text/javascript">
228 (function() {
229 var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
230 po.src = 'https://apis.google.com/js/platform.js';
231 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
232 })();
233 </script>
234
235 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
236
237 <!-- Twitter -->
238 <a href="https://twitter.com/versluis" class="twitter-follow-button" data-show-count="true">Follow @versluis</a>
239 <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>
240
241 <!-- Facebook -->
242 <iframe src="//www.facebook.com/plugins/like.php?href=https%3A%2F%2Fwww.facebook.com%2Fpages%2FThe-WP-Guru%2F162188713810370&amp;width&amp;layout=button_count&amp;action=like&amp;show_faces=true&amp;share=true&amp;height=21&amp;appId=186277158097599" scrolling="no" frameborder="0" style="border:none; overflow:hidden; height:21px;" allowTransparency="true"></iframe>
243
244 </span></p>
245
246 <?php
247 } // end of main function
248
249 /*
250 * this function creates the new directory and file
251 */
252
253 function ctw_create_theme($childtheme) {
254
255 // add messages to this variable
256 $status = array();
257 $status['alert'] = 0;
258
259 // create the directory
260 $directory = get_theme_root() . '/' . sanitize_file_name($childtheme['title']);
261 // echo '<p>The child theme directory will be created at ' . $directory;
262 if (!mkdir($directory)) {
263 $status['message'] = "<p>Could not create directory. Does it exist already?</p>";
264 $status['alert'] = -1;
265 return $status;
266 } else {
267 $status['message'] = "<p>Directory created successfully.</p>";
268 }
269
270 // create style.css in our directory
271 $filename = $directory . '/' . 'style.css';
272 $handle = fopen($filename, 'w');
273
274 // add meta data
275 $data = "/* \n Theme Name: " . $childtheme['title'];
276 $data = $data . "\n Theme URI: " . $childtheme['url'];
277 $data = $data . "\n Description: " . $childtheme['description'];
278 $data = $data . "\n Author: " . $childtheme['author'];
279 $data = $data . "\n Author URI: " . $childtheme['author-url'];
280 $data = $data . "\n Template: " . $childtheme['parent'];
281 $data = $data . "\n Version: " . $childtheme['version'];
282
283 // insert GPL License Terms
284 if ($childtheme['include-gpl'] == 'on') {
285 $data = $data . "\n License: GNU General Public License v2 or later";
286 $data = $data . "\n License URI: http://www.gnu.org/licenses/gpl-2.0.html";
287 }
288
289 // adding the call to the parent style sheet in CSS is no longer best practice
290 // $data = $data . "\n*/\n\n" . '@import url("../' . $childtheme['parent'] . '/style.css");';
291 $data = $data . "\n\n /* == Add your own styles below this line ==";
292 $data = $data . "\n--------------------------------------------*/\n\n";
293
294 // write data and close the file
295 if (fwrite($handle, $data) == false) {
296 $status['message'] = $status['message'] . "<p>There was a problem writing data to style.css.</p>";
297 $status['alert'] = -1;
298 return $status;
299 } else {
300 $status['message'] = $status['message'] . "<p>Writing data to style.css...</p>";
301 }
302 fclose($handle);
303
304 // create functions.php
305 $filename = $directory . '/' . 'functions.php';
306 $handle = fopen($filename, 'w');
307
308 // add some meta data
309 $data = "<?php /*\n\n This file is part of a child theme called " . $childtheme['title'] . ".";
310 $data = $data . "\n Functions in this file will be loaded before the parent theme's functions.";
311 $data = $data . "\n For more information, please read https://codex.wordpress.org/Child_Themes.";
312 $data = $data . "\n\n*/";
313
314 // add call to enqueue parent style sheet via functions.php
315 $data = $data . "\n\n// this code loads the parent's stylesheet (leave it in place unless you know what you're doing)";
316 $data = $data . "\n\nfunction theme_enqueue_styles() {";
317 $data = $data . "\n wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css');";
318 $data = $data . "\n wp_enqueue_style('child-style', get_stylesheet_directory_uri() . '/style.css', array(". '$parent_style' ."));\n}";
319 $data = $data . "\nadd_action('wp_enqueue_scripts', 'theme_enqueue_styles');";
320
321 $data = $data . "\n\n/* Add your own functions below this line.";
322 $data = $data . "\n ======================================== */ \n\n";
323
324 // write data and close the file
325 if (fwrite($handle, $data) == false) {
326 $status['message'] = $status['message'] . "<p>There was a problem writing data to functions.php.</p>";
327 $status['alert'] = -1;
328 return $status;
329 } else {
330 $status['message'] = $status['message'] . "<p>Writing data to functions.php...</p>";
331 }
332 fclose($handle);
333
334 // create a nice screenshot
335 $thumbnail_status = ctw_make_thumbnail($directory);
336 $status['message'] = $status['message'] . $thumbnail_status;
337
338 return $status;
339 }
340
341 // return an array only of parent themes
342 function ctw_giveme_parents() {
343
344 // first we'll grab all installed themes
345 $allThemes = wp_get_themes();
346 $parentThemes = array();
347
348 // next we'll add all parents and return the result
349 foreach ($allThemes as $theme) {
350
351 if ($theme->parent() == false) {
352 $parentThemes[] = $theme;
353 }
354 }
355
356 return $parentThemes;
357 }
358
359 // create a new thumbnail
360 function ctw_make_thumbnail($childpath) {
361
362 // for now we'll just copy an existing image
363 $thumbnail = plugins_url('images/screenshot.png', __FILE__);
364 $destination = $childpath . '/screenshot.png';
365 if (!copy($thumbnail, $destination)) {
366 return "<p>Could not copy thumbnail image :-(</p>";
367 } else {
368 return "<p>Copied thumbnail file over - looking good!</p>";
369 }
370 }
371
372 // quick tests go here
373 function ctw_testing($childtheme) {
374
375 $directory = get_theme_root() . '/' . sanitize_file_name($childtheme['title']);
376 ctw_make_thumbnail($directory);
377 }
378
379 ?>
380