PluginProbe
Child Theme Wizard / 1.0
Child Theme Wizard v1.0
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.0, at child-theme-wizard.php

370 lines 13.0 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.0
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 $data = $data . "\n*/\n\n" . '@import url("../' . $childtheme['parent'] . '/style.css");';
290 $data = $data . "\n\n /* == Add your own styles below this line ==";
291 $data = $data . "\n--------------------------------------------*/\n\n";
292
293 // write data and close the file
294 if (fwrite($handle, $data) == false) {
295 $status['message'] = $status['message'] . "<p>There was a problem writing data to style.css.</p>";
296 $status['alert'] = -1;
297 return $status;
298 } else {
299 $status['message'] = $status['message'] . "<p>Writing data to style.css...</p>";
300 }
301 fclose($handle);
302
303 // create functions.php
304 $filename = $directory . '/' . 'functions.php';
305 $handle = fopen($filename, 'w');
306
307 // add some meta data
308 $data = "<?php /*\n\n This file is part of a child theme called " . $childtheme['title'] . ".";
309 $data = $data . "\n Functions in this file will be loaded before the parent theme's functions.";
310 $data = $data . "\n For more information, please read https://codex.wordpress.org/Child_Themes.";
311 $data = $data . "\n\n Add your own functions below this line.";
312 $data = $data . "\n ========================================== */ \n\n";
313
314 // write data and close the file
315 if (fwrite($handle, $data) == false) {
316 $status['message'] = $status['message'] . "<p>There was a problem writing data to functions.php.</p>";
317 $status['alert'] = -1;
318 return $status;
319 } else {
320 $status['message'] = $status['message'] . "<p>Writing data to functions.php...</p>";
321 }
322 fclose($handle);
323
324 // create a nice screenshot
325 $thumbnail_status = ctw_make_thumbnail($directory);
326 $status['message'] = $status['message'] . $thumbnail_status;
327
328 return $status;
329 }
330
331 // return an array only of parent themes
332 function ctw_giveme_parents() {
333
334 // first we'll grab all installed themes
335 $allThemes = wp_get_themes();
336 $parentThemes = array();
337
338 // next we'll add all parents and return the result
339 foreach ($allThemes as $theme) {
340
341 if ($theme->parent() == false) {
342 $parentThemes[] = $theme;
343 }
344 }
345
346 return $parentThemes;
347 }
348
349 // create a new thumbnail
350 function ctw_make_thumbnail($childpath) {
351
352 // for now we'll just copy an existing image
353 $thumbnail = plugins_url('images/screenshot.png', __FILE__);
354 $destination = $childpath . '/screenshot.png';
355 if (!copy($thumbnail, $destination)) {
356 return "<p>Could not copy thumbnail image :-(</p>";
357 } else {
358 return "<p>Copied thumbnail file over - looking good!</p>";
359 }
360 }
361
362 // quick tests go here
363 function ctw_testing($childtheme) {
364
365 $directory = get_theme_root() . '/' . sanitize_file_name($childtheme['title']);
366 ctw_make_thumbnail($directory);
367 }
368
369 ?>
370