PageRenderTime 83ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/one-click-child-theme/one-click-child-theme.php

https://gitlab.com/eita/encontre-consumo-responsavel
PHP | 686 lines | 450 code | 25 blank | 211 comment | 30 complexity | 0ef8befb1406140309034384df494845 MD5 | raw file
  1. <?php
  2. /*
  3. **************************************************************************
  4. Plugin Name: One-Click Child Theme
  5. Plugin URI: http://terrychay.com/wordpress-plugins/one-click-child-theme
  6. Version: 1.6
  7. Description: Easily child theme any theme from wp-admin wp-admin without going into shell or using FTP.
  8. Author: tychay
  9. Author URI: http://terrychay.com/
  10. License: GPLv2 or later
  11. License URI: https://www.gnu.org/licenses/gpl-2.0.html
  12. Text Domain: one-click-child-theme
  13. Domain Path: /languages
  14. **************************************************************************/
  15. /* Copyright 2011-2015 terry chay (email : tychay@php.net)
  16. This program is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License, version 2, as
  18. published by the Free Software Foundation.
  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. You should have received a copy of the GNU General Public License
  24. along with this program; if not, write to the Free Software
  25. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. /*
  28. * Performance: One-Click Child Theme is only active in admin page
  29. */
  30. if (!is_admin()) { return; }
  31. /**
  32. * Load textdomain
  33. */
  34. function _load_textdomain() {
  35. load_plugin_textdomain( 'one-click-child-theme', false, basename(dirname(__FILE__)) . '/languages' );
  36. }
  37. add_action( 'init', '_load_textdomain' );
  38. /**
  39. * The namespace for the One-Click Child Theme Plugin
  40. */
  41. class OneClickChildTheme {
  42. /**
  43. * @const string Used for id generation and language text domain.
  44. */
  45. const _SLUG = 'one-click-child-theme';
  46. /**
  47. * Used for loading in files
  48. * @var string
  49. */
  50. private $_pluginDir = '';
  51. /**
  52. * This plugin's theme page
  53. */
  54. private $_themePageUrl = '';
  55. /**
  56. * Theme page name (menu slug)
  57. * @var string
  58. */
  59. private $_menuId = '';
  60. /**
  61. * action for Create Child form
  62. */
  63. private $_createChildFormId = '';
  64. /**
  65. * action for Repair Child form
  66. */
  67. private $_repairChildFormId = '';
  68. /**
  69. * action for Copy Template form
  70. */
  71. private $_copyTemplateFormId = '';
  72. /**
  73. * action for screenshot generation
  74. */
  75. private $_mshotSiteFormId = '';
  76. public function __construct() {
  77. $this->_pluginDir = dirname(__FILE__);
  78. $this->_menuId = self::_SLUG . '-page';
  79. $this->_themePageUrl = admin_url('themes.php?page='.$this->_menuId);
  80. $this->_createChildFormId = self::_SLUG.'-create-child';
  81. $this->_repairChildFormId = self::_SLUG.'-repair-child';
  82. $this->_copyTemplateFormId = self::_SLUG.'-copy-template';
  83. $this->_mshotSiteFormId = self::_SLUG.'-mshot-site';
  84. // it has to be buried like this or you get an error:
  85. // "You do not have sufficient permissions to access this page"
  86. add_action( 'admin_menu', array($this,'createAdminMenu') );
  87. // form handling code
  88. add_action( 'admin_post_'.$this->_createChildFormId, array($this,'processCreateForm') );
  89. add_action( 'admin_post_'.$this->_repairChildFormId, array($this,'processRepairChildForm') );
  90. add_action( 'admin_post_'.$this->_copyTemplateFormId, array($this,'processCopyTemplateForm') );
  91. add_action( 'admin_post_'.$this->_mshotSiteFormId, array($this,'processMShotSiteForm') );
  92. // TODO: I could also use the $pagenow global, but is it still there?
  93. if ( basename($_SERVER['PHP_SELF']) == 'themes.php' && !empty($_REQUEST['occt_error']) ) {
  94. add_action( 'admin_notices', array($this,'showErrorNotice'));
  95. }
  96. }
  97. /**
  98. * Handle error and update notices for this theme
  99. *
  100. * There are now four types of notices: success (green), warning (orange), error (red),
  101. * and info (blue).
  102. *
  103. * Put here because there is a redirect between all forms and error notifications and
  104. * add_settings_error() only covers options API errors.
  105. */
  106. public function showErrorNotice()
  107. {
  108. switch ($_GET['occt_error']) {
  109. case 'child_created': //SUCCESS: child theme created
  110. $type = 'updated'; //fade?
  111. $msg = sprintf(
  112. __('Theme switched! <a href="%s">Click here to edit the child stylesheet</a>.', self::_SLUG),
  113. add_query_arg(
  114. urlencode_deep(array(
  115. 'file' => 'style.css',
  116. 'theme' => get_stylesheet(),
  117. )),
  118. admin_url('theme-editor.php')
  119. )
  120. );
  121. break;
  122. case 'create_failed': //ERROR: create file failed (probably due to permissions)
  123. $type = 'error';
  124. $msg = sprintf(
  125. __('Failed to create file: %s', self::_SLUG),
  126. esc_html($_GET['filename'])
  127. );
  128. break;
  129. case 'edit_failed': //ERROR: edit file failed (probably do to permissions)
  130. $type = 'error';
  131. $msg = sprintf(
  132. __('Failed to edit file: %s', self::_SLUG),
  133. esc_html($_GET['filename'])
  134. );
  135. break;
  136. case 'repair_success': //SUCCESS: repaired child theme
  137. $type = 'updated fade';
  138. $msg = __('Repaired child theme.', self::_SLUG);
  139. break;
  140. case 'no_template': //ERROR: template file not specified
  141. $type = 'error';
  142. $msg = __('No template file specified.', self::_SLUG);
  143. case 'missing_template': //ERROR: parent theme doesn't have template
  144. $type = 'error';
  145. $msg = sprintf(
  146. __('Template file %s does not exist in parent theme!', self::_SLUG),
  147. esc_html($_GET['filename'])
  148. );
  149. break;
  150. case 'already_template': //ERROR: child theme already has template
  151. $type = 'error';
  152. $msg = sprintf(
  153. __('Template file %s already exists in child theme!', self::_SLUG),
  154. esc_html($_GET['filename'])
  155. );
  156. break;
  157. case 'copy_failed': //ERROR: couldn't duplicate file for some reason
  158. $type = 'error';
  159. $msg = sprintf(
  160. __('Failed to duplicate file %s!', self::_SLUG),
  161. esc_html($_GET['filename'])
  162. );
  163. break;
  164. case 'copy_success': //SUCCESS: template file created
  165. $type = 'updated'; //fade?
  166. $msg = sprintf(
  167. __('<a href="%s">File %s created!</a>', self::_SLUG),
  168. add_query_arg(
  169. urlencode_deep(array(
  170. 'file' => $_GET['filename'],
  171. 'theme' => get_stylesheet(),
  172. )),
  173. admin_url('theme-editor.php')
  174. ),
  175. esc_html($_GET['filename'])
  176. );
  177. break;
  178. case 'delete_failed': //ERROR: couldn't delete file for some reason
  179. $type = 'error';
  180. $msg = sprintf(
  181. __('Failed to delete file %s!', self::_SLUG),
  182. esc_html($_GET['filename'])
  183. );
  184. break;
  185. case 'mshot_404': //ERROR: couldn't find mshot
  186. $type = 'error';
  187. $msg = sprintf(
  188. __('404 File not found at %s!', self::_SLUG),
  189. esc_html($_GET['url'])
  190. );
  191. break;
  192. case 'mshot_mime_wrong': //ERROR: couldn't find mshot
  193. $type = 'error';
  194. $msg = sprintf(
  195. __('Unrecognized mimetype at %s!', self::_SLUG),
  196. esc_html($_GET['url'])
  197. );
  198. break;
  199. case 'mshot_nocreate': //ERROR: couldn't find mshot
  200. $type = 'error';
  201. $msg = sprintf(
  202. __('Failed to create file %s!', self::_SLUG),
  203. esc_html($_GET['filename'])
  204. );
  205. break;
  206. case 'mshot_success': //SUCCESS: screenshot generated
  207. $type = 'updated fade'; //fade?
  208. $msg = __('Successfully changed screenshot.', self::_SLUG);
  209. break;
  210. default: //ERROR: it is a generic error message
  211. $type = 'error';
  212. $msg = esc_html($_GET['occt_error']);
  213. }
  214. printf(
  215. '<div class="%s"><p>%s</p></div>',
  216. $type,
  217. $msg
  218. );
  219. }
  220. /**
  221. * Adds an admin menu for One Click Child Theme in Appearances
  222. */
  223. public function createAdminMenu() {
  224. add_theme_page(
  225. __('Make a Child Theme', self::_SLUG), //page title
  226. __('Child Theme', self::_SLUG), //menu title
  227. 'install_themes', //capability needed to view
  228. $this->_menuId, //menu slug (and page query url)
  229. array( $this, 'showThemePage' ) //callback function
  230. );
  231. }
  232. //
  233. // SHOW THEME PAGE
  234. //
  235. /**
  236. * Show the theme page which has a form allowing you to child theme
  237. * currently selected theme.
  238. *
  239. */
  240. public function showThemePage()
  241. {
  242. // Form is processed in the admin_post_* hooks
  243. // Handle case where current theme is already a child
  244. if ( is_child_theme() ) {
  245. $this->_showFormAlreadyChild( $this->_child_theme_needs_repair() );
  246. return;
  247. }
  248. // Default behavior: We are not a child theme, but interested in creating one.
  249. // Grab default values from a form fail
  250. $theme_name = ( !empty($_GET['theme_name']) ) ? $_GET['theme_name'] : '';
  251. $description = ( !empty($_GET['description']) ) ? $_GET['description'] : '';
  252. if ( !empty($_GET['author_name']) ) {
  253. $author = $_GET['author_name'];
  254. } else {
  255. global $current_user;
  256. get_currentuserinfo();
  257. $author = $current_user->display_name;
  258. }
  259. // render default behaivor
  260. require $this->_pluginDir.'/templates/create_child_form.php';
  261. }
  262. /**
  263. * Show the "is child already" template.
  264. * @param boolean $child_needs_repair whether or not child theme needs repair
  265. * @todo handle grandchildren
  266. */
  267. private function _showFormAlreadyChild($child_needs_repair) {
  268. // set template parameters
  269. $current_theme = wp_get_theme();
  270. $child_theme_screenshot_url = ( $screenshot_filename = $this->_scanForScreenshot( get_stylesheet_directory() ) )
  271. ? get_stylesheet_directory_uri().'/'.$screenshot_filename
  272. : '';
  273. $mshot_url = $this->_mshotUrl();
  274. // Search for template files.
  275. // Note: since there can be files like {mimetype}.php, we must assume
  276. // that any root level .php files in the template directory are
  277. // templates.
  278. $template_files = glob ( get_template_directory().'/*.php' );
  279. foreach ( $template_files as $index=>$file ) {
  280. $template_files[$index] = basename( $file );
  281. }
  282. // Filter out any files in child already created
  283. $child_theme_dir = get_stylesheet_directory();
  284. foreach ( $template_files as $index=>$filename ) {
  285. if ( file_exists($child_theme_dir.'/'.$filename) ) {
  286. unset($template_files[$index]);
  287. }
  288. }
  289. require $this->_pluginDir.'/templates/is_child_already.php';
  290. }
  291. //
  292. // FORM HANDLING
  293. //
  294. /**
  295. * Handle the create child form.
  296. */
  297. public function processCreateForm() {
  298. check_admin_referer( $this->_createChildFormId . '-verify' );
  299. $theme_name = $_POST['theme_name'];
  300. $description = ( empty($_POST['description']) )
  301. ? ''
  302. : $_POST['description'];
  303. $author_name = ( empty($_POST['author_name']) )
  304. ? ''
  305. : $_POST['author_name'];
  306. $result = $this->_make_child_theme( $theme_name, $description, $author_name );
  307. if ( is_wp_error( $result ) ) {
  308. // should show create child form again
  309. $this->_redirect(
  310. $this->_themePageUrl,
  311. $result->get_error_message(),
  312. array(
  313. 'theme_name' => $theme_name,
  314. 'description' => $description,
  315. 'author_name' => $author_name,
  316. )
  317. );
  318. return;
  319. } else {
  320. switch_theme( $result['parent_template'], $result['new_theme'] );
  321. // Redirect to themes page on success
  322. $this->_redirect(
  323. admin_url('themes.php'),
  324. 'child_created'
  325. );
  326. }
  327. }
  328. /**
  329. * Handle the repair_child_form form.
  330. */
  331. public function processRepairChildForm()
  332. {
  333. check_admin_referer( $this->_repairChildFormId . '-verify' );
  334. $child_theme_dir = get_stylesheet_directory();
  335. $functions_file = $child_theme_dir.'/functions.php';
  336. $style_file = $child_theme_dir.'/style.css';
  337. // create functions.php if it doesn't exist yet
  338. if ( !file_exists($functions_file) ) {
  339. if ( !touch($functions_file) ) {
  340. // fixing is hopeless if we can't create the file :-(
  341. $this->_redirect(
  342. $this->_themePageUrl,
  343. 'create_failed',
  344. array( 'filename' => $functions_file )
  345. );
  346. return;
  347. }
  348. }
  349. // read in style.css
  350. $style_text = file_get_contents( $style_file );
  351. // prune out old rules
  352. $style_text = preg_replace(
  353. '!@import\s+url\(\s?["\']\.\./.*/style.css["\']\s?\);!ims',
  354. '',
  355. $style_text
  356. );
  357. $style_text = preg_replace(
  358. '!@import\s+url\(\s?["\']'.get_template_directory_uri().'/style.css["\']\s?\);!ims',
  359. '',
  360. $style_text
  361. );
  362. if ( file_put_contents( $style_file, $style_text) === false ) {
  363. $this->_redirect(
  364. $this->_themePageUrl,
  365. 'edit_failed',
  366. array( 'filename' => $style_file )
  367. );
  368. return;
  369. }
  370. // modify functions.php to prepend new rules
  371. $functions_text = file_get_contents( $this->_pluginDir.'/templates/functions.php' );
  372. // ^^^ above file has no final carriage return and ending comment so it should
  373. // "smash" the starting '<?php' string in any existing functions.php.
  374. $functions_text .= file_get_contents( $functions_file );
  375. if ( file_put_contents( $functions_file, $functions_text ) === false ) {
  376. $this->_redirect(
  377. $this->_themePageUrl,
  378. 'edit_failed',
  379. array( 'filename' => $functions_file )
  380. );
  381. return;
  382. }
  383. $this->_redirect(
  384. $this->_themePageUrl,
  385. 'repair_success'
  386. );
  387. }
  388. /**
  389. * Handle the Copy Template form.
  390. */
  391. public function processCopyTemplateForm() {
  392. check_admin_referer( $this->_copyTemplateFormId . '-verify' );
  393. $filename = ( empty($_POST['filename']) )
  394. ? ''
  395. : $_POST['filename'];
  396. if ( !$filename ) {
  397. $this->_redirect(
  398. $this->_themePageUrl,
  399. 'no_template'
  400. );
  401. return;
  402. }
  403. $child_theme_dir = get_stylesheet_directory();
  404. $template_dir = get_template_directory();
  405. var_dump('bar');
  406. if ( !file_exists($template_dir.'/'.$filename) ) {
  407. $this->_redirect(
  408. $this->_themePageUrl,
  409. 'missing_template',
  410. array( 'filename' => $filename )
  411. );
  412. return;
  413. }
  414. if ( file_exists($child_theme_dir.'/'.$filename) ) {
  415. $this->_redirect(
  416. $this->_themePageUrl,
  417. 'already_template',
  418. array( 'filename' => $filename )
  419. );
  420. return;
  421. }
  422. if ( !copy( $template_dir.'/'.$filename, $child_theme_dir.'/'.$filename ) ) {
  423. $this->_redirect(
  424. $this->_themePageUrl,
  425. 'copy_failed',
  426. array( 'filename' => $filename )
  427. );
  428. }
  429. $this->_redirect(
  430. $this->_themePageUrl,
  431. 'copy_success',
  432. array( 'filename' => $filename )
  433. );
  434. }
  435. /**
  436. * Handle the mshot Screenshot form
  437. */
  438. public function processMShotSiteForm()
  439. {
  440. check_admin_referer( $this->_mshotSiteFormId . '-verify' );
  441. // delete existing screenshot if it exists
  442. $child_theme_dir = get_stylesheet_directory();
  443. if ( $screenshot_filename = $this->_scanForScreenshot($child_theme_dir) ) {
  444. $screenshot_path = $child_theme_dir.'/'.$screenshot_filename;
  445. if ( !unlink($screenshot_path) ) {
  446. // most likely a directory problem Fail with an error
  447. $this->_redirect(
  448. $this->_themePageUrl,
  449. 'delete_failed',
  450. array( 'filename' => $screenshot_path )
  451. );
  452. return;
  453. }
  454. }
  455. $mshot_url = $this->_mshotUrl();
  456. // Get the mshot
  457. $response = wp_remote_get($mshot_url);
  458. if ( $response['code'] == 404 ) {
  459. // The 404 image is gorgeous nowadays, but (if wp.com correctly handled error
  460. // codes for image generation) we'd not let them use it as a theme screenshot.
  461. $this->_redirect(
  462. $this->_themePageUrl,
  463. 'mshot_404',
  464. array( 'url' => $mshot_url )
  465. );
  466. }
  467. // Should be 'image/jpeg', but let's hedge our bets
  468. switch ($response['headers']['content-type']) {
  469. case 'image/jpeg':
  470. $screenshot_filename = 'screenshot.jpg';
  471. break;
  472. case 'image/png':
  473. $screenshot_filename = 'screenshot.png';
  474. break;
  475. case 'image/gif':
  476. $screenshot_filename = 'screenshot.gif';
  477. break;
  478. default:
  479. $this->_redirect(
  480. $this->_themePageUrl,
  481. 'mshot_mime_wrong',
  482. array( 'url' => $mshot_url )
  483. );
  484. return;
  485. }
  486. $screenshot_path = $child_theme_dir.'/'.$screenshot_filename;
  487. if ( file_put_contents($screenshot_path, $response['body']) === false ) {
  488. $this->_redirect(
  489. $this->_themePageUrl,
  490. 'mshot_nocreate',
  491. array( 'filename' => $screenshot_path )
  492. );
  493. return;
  494. }
  495. $this->_redirect(
  496. $this->_themePageUrl,
  497. 'mshot_success'
  498. );
  499. }
  500. //
  501. // PRIVATE METHOD
  502. //
  503. /**
  504. * Does the work to make a child theme based on the current theme.
  505. *
  506. * This currently supports the following files:
  507. *
  508. * 1. style.css: Follows the rules outlined in {@link http://codex.wordpress.org/Child_Themes the Codex}
  509. * 2. functions.php: Followed the updated rules outlined in the Codex. Note
  510. * that since WordPress ?.? functions.php hierarchy is automatically
  511. * included.
  512. * 3. rtl.css: right to left language support, if not avaialble in parent, it
  513. * uses TwentyFifteen's rtl
  514. * 4. screenshot.png: screenshot if available in the parent
  515. *
  516. * @author terry chay <tychay@autoamttic.com>
  517. * @author Chris Robinson <http://contempographicdesign.com/> (for screenshot support).
  518. * @return array|WP_Error If successful, it returns a hash contianing
  519. * - new_theme: (directory) name of new theme
  520. * - parent_template: (directory) name of parent template
  521. * - parent_theme: (directory) name of parent theme
  522. * - new_theme_path: full path to the directory cotnaining the new theme
  523. * - new_theme_title: the name of the new theme
  524. */
  525. private function _make_child_theme( $new_theme_title, $new_theme_description, $new_theme_author ) {
  526. $parent_theme_title = get_current_theme();
  527. $parent_theme_template = get_template(); //Doesn't play nice with the grandkids
  528. $parent_theme_name = get_stylesheet();
  529. $parent_theme_dir = get_stylesheet_directory();
  530. // Turn a theme name into a directory name
  531. $new_theme_name = sanitize_title( $new_theme_title );
  532. $theme_root = get_theme_root();
  533. // Validate theme name
  534. $new_theme_path = $theme_root.'/'.$new_theme_name;
  535. if ( file_exists( $new_theme_path ) ) {
  536. return new WP_Error( 'exists', __( 'Theme directory already exists!', self::_SLUG ) );
  537. }
  538. mkdir( $new_theme_path );
  539. // Make style.css
  540. ob_start();
  541. require $this->_pluginDir.'/templates/child-theme-css.php';
  542. $css = ob_get_clean();
  543. file_put_contents( $new_theme_path.'/style.css', $css );
  544. // "Generate" functions.php
  545. copy( $this->_pluginDir.'/templates/functions.php', $new_theme_path.'/functions.php' );
  546. // RTL support
  547. $rtl_theme = ( file_exists( $parent_theme_dir.'/rtl.css' ) )
  548. ? $parent_theme_name
  549. : 'twentyfifteen'; //use the latest default theme rtl file
  550. ob_start();
  551. require $this->_pluginDir.'/templates/rtl-css.php';
  552. $css = ob_get_clean();
  553. file_put_contents( $new_theme_path.'/rtl.css', $css );
  554. // Copy screenshot
  555. if ( $screenshot_filename = $this->_scanForScreenshot( $parent_theme_dir ) ) {
  556. copy(
  557. $parent_theme_dir.'/'.$screenshot_filename,
  558. $new_theme_path.'/'.$screenshot_filename
  559. );
  560. } // removed grandfather screenshot check (use mshot instead, rly)
  561. // Make child theme an allowed theme (network enable theme)
  562. $allowed_themes = get_site_option( 'allowedthemes' );
  563. $allowed_themes[ $new_theme_name ] = true;
  564. update_site_option( 'allowedthemes', $allowed_themes );
  565. return array(
  566. 'parent_template' => $parent_theme_template,
  567. 'parent_theme' => $parent_theme_name,
  568. 'new_theme' => $new_theme_name,
  569. 'new_theme_path' => $new_theme_path,
  570. 'new_theme_title' => $new_theme_title,
  571. );
  572. }
  573. //
  574. // PRIVATE UTILITY FUNCTIONS
  575. //
  576. /**
  577. * Detect if child theme needs repair.
  578. *
  579. * A child theme needs repair if it is missing a functions.php or the
  580. * style.css still has a rule that points to the parent.
  581. */
  582. private function _child_theme_needs_repair()
  583. {
  584. $child_theme_dir = get_stylesheet_directory();
  585. if ( !file_exists($child_theme_dir.'/functions.php') ) {
  586. return true;
  587. }
  588. $style_text = file_get_contents( $child_theme_dir.'/style.css' );
  589. // look for relative match (dificult to extract parent theme directory
  590. // so I'll assume any in this path is parent theme)
  591. if ( preg_match(
  592. '!@import\s+url\(\s?["\']\.\./.*/style.css["\']\s?\);!ims',
  593. $style_text
  594. ) ) {
  595. return true;
  596. }
  597. // look for absolute match
  598. if ( preg_match(
  599. '!@import\s+url\(\s?["\']'.get_template_directory_uri().'/style.css["\']\s?\);!ims',
  600. $style_text
  601. ) ) {
  602. return true;
  603. }
  604. return false;
  605. }
  606. /**
  607. * Handle error redirects (for admin_notices generated by plugin)
  608. *
  609. * Note add_query_arg() is written like shit. Here are it's problems:
  610. *
  611. * 1. doesn't take advantage of built-in parse_url()
  612. * 2. uses urlencode_deep() instead of an array_merge and built-in http_build_query()
  613. * 3. doesn't urlencode() if $arg[0] is an array.
  614. *
  615. * The 3rd one is extremely non-intuitive, but fixing it, would break backward
  616. * compatibility due to double-escaping. I'm hacking around that. :-(
  617. *
  618. * @param string $url The (base) url to redirect to, usually admin_url()
  619. * @param string $error the error code to use
  620. * @param string $args other arguments to add to the query string
  621. * @return null
  622. */
  623. private function _redirect($url, $error, $args = array()) {
  624. $args['occt_error'] = $error;
  625. $args = urlencode_deep($args);
  626. wp_redirect( add_query_arg( $args, $url ) );
  627. }
  628. /**
  629. * Searches directory for a theme screenshot
  630. *
  631. * @param string $directory directory to search (a theme directory)
  632. * @return string|false 'screenshot.png' (or whatever) or false if there is no screenshot
  633. */
  634. private function _scanForScreenshot($directory)
  635. {
  636. $screenshots = glob( $directory.'/screenshot.{png,jpg,jpeg,gif}', GLOB_BRACE );
  637. return (empty($screenshots))
  638. ? false
  639. : basename($screenshots[0]);
  640. }
  641. /**
  642. * Generate mshot of wordpress homepage.
  643. *
  644. * Recommende image dimensions from https://codex.wordpress.org/Theme_Development#Screenshot
  645. * @todo probably won't work correctly in multisite installs
  646. * @todo remove debugging code
  647. */
  648. private function _mshotUrl()
  649. {
  650. $scheme = (is_ssl()) ? 'https' : 'http';
  651. return $scheme . '://s.wordpress.com/mshots/v1/'. urlencode(get_site_url()) . '?w=880&h=660';
  652. }
  653. }
  654. new OneClickChildTheme();
  655. // Start this plugin
  656. //add_action( 'admin_init', array('OneClickChildTheme','init'), 12 );