Plugin Directory

Changeset 2562917


Ignore:
Timestamp:
07/12/2021 02:30:16 PM (5 years ago)
Author:
ajay930
Message:

Added create template and other minor changes

Location:
quick-child-theme-generator
Files:
21 added
7 edited

Legend:

Unmodified
Added
Removed
  • quick-child-theme-generator/trunk/includes/qcthg_function.php

    r2446397 r2562917  
    1717            add_action('admin_enqueue_scripts', array('QCTHG_Main', 'qcthgEnqueueAssets'));
    1818            add_action('admin_post_qcthg_create_theme', array('QCTHG_Main', 'qcthgCreateChildTheme'));
     19            add_action('admin_post_qcthg_create_template', array('QCTHG_Main', 'qcthgCreateBlankTemplate'));
    1920            if (isset($_COOKIE['qcthg_adminNotices']) && $_COOKIE['qcthg_adminNotices'] !== 0) {
    2021                ob_start();
    2122                add_action( 'admin_notices', array('QCTHG_Helper', 'adminNoticeError'));
    2223            }
     24            if (isset($_COOKIE['qcthg_adminNoticesSuccess']) && $_COOKIE['qcthg_adminNoticesSuccess'] !== 0) {
     25                ob_start();
     26                add_action( 'admin_notices', array('QCTHG_Helper', 'adminNoticeSuccess'));
     27            }
    2328        }
    2429
     
    2833        public function qcthgPluginMenu()
    2934        {
    30             add_theme_page(
     35            add_menu_page(
    3136                'Quick Child Theme Generator',
    3237                'Quick Child Theme',
     
    3439                'quick-child-theme-generator',
    3540                array('QCTHG_Main', 'qcthgMenuPage'),
    36                 0
     41                'dashicons-groups',
     42                60
    3743            );
     44
     45            add_submenu_page(
     46                'quick-child-theme-generator',
     47                'Create Template',
     48                'Template',
     49                'manage_options',
     50                'quick-blank-template-generator',
     51                array('QCTHG_Main', 'qcthgSubMenuTemplatePage'),
     52                1
     53            );
     54
    3855        }
    3956
     
    5572        public function qcthgEnqueueAssets($hook)
    5673        {
    57             if($hook === 'toplevel_page_quick-child-theme-generator' || $_GET['page'] === 'quick-child-theme-generator') {
     74            if($hook === 'toplevel_page_quick-child-theme-generator'
     75            || $hook === 'quick-child-theme_page_quick-blank-template-generator'
     76            || $_GET['page'] === 'quick-child-theme-generator'
     77            || $_GET['page'] === 'quick-blank-template-generator') {
    5878                wp_enqueue_script('jquery');
    5979                wp_enqueue_script('jquery-ui-core');
     
    7292        {
    7393            require_once(QCTHG_PATH.'/views/qcthg_menu.php');
     94        }
     95
     96        /**
     97         * Added sub menu plugin page
     98         */
     99        public function qcthgSubMenuTemplatePage()
     100        {
     101            require_once(QCTHG_PATH.'/views/qcthg_template.php');
    74102        }
    75103
     
    283311        }
    284312
     313        /**
     314         * Validate and create custom template
     315         */
     316        public function qcthgCreateBlankTemplate()
     317        {
     318            if(!empty($_POST)) {
     319                if(!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'qcthg_create_template')) {
     320                    wp_die('Ooops!!! you are not auththorized user.');
     321                } else {
     322                    if(empty(trim($_POST['tmp_name']))) {
     323                        $cookieValue = '<b>ERROR: </b> Missing template name';
     324                        QCTHG_Helper::qcthgSetCookies('', $cookieValue);
     325                        wp_redirect(wp_get_referer());
     326                        exit;
     327                    }
     328
     329                    $postedData = array();
     330                    $postedData['template_name'] = sanitize_text_field($_POST['tmp_name']);
     331                    $postedData['header_name'] = empty(trim($_POST['tmp_header_name'])) ? '' : sanitize_text_field($_POST['tmp_header_name']);
     332                    $postedData['footer_name'] = empty(trim($_POST['tmp_footer_name'])) ? '' : sanitize_text_field($_POST['tmp_footer_name']);
     333
     334                    QCTHG_Main::qcthgGenerateTemplateAssets($postedData);
     335
     336                    $cookieValue = '<b>SUCCESS: </b> Template successfully created';
     337                    QCTHG_Helper::qcthgSetCookies('qcthg_adminNoticesSuccess', $cookieValue);
     338                    wp_redirect(wp_get_referer());
     339                    exit;
     340                }
     341            }
     342        }
     343
     344        /**
     345         * Validate/Refined custom template data
     346         * @param  [array] $data
     347         */
     348        public function qcthgGenerateTemplateAssets($data)
     349        {
     350            $themeRoot = get_theme_root();
     351            $childThemeDir = get_stylesheet_directory();
     352            $templateName = QCTHG_Helper::qcthgSanitizeText($data['template_name']);
     353            $refTemplateName = QCTHG_Helper::qcthgRefSanitizeText($templateName);
     354            $refTemplateNameLowerCase = strtolower($refTemplateName);
     355            $getActiveTheme = wp_get_theme();
     356            $parentThemeDirName = $getActiveTheme["Template"];
     357            $parentThemeDirPath = $themeRoot.'/'.$parentThemeDirName;
     358            $scanPath = scandir($parentThemeDirPath, 1);
     359            $scanChildPath = scandir($childThemeDir, 1);
     360
     361            if(empty($templateName)) {
     362                $cookieValue = '<b>ERROR: </b>Special characters are not allowed in the template name! please try again.';
     363                QCTHG_Helper::qcthgSetCookies('', $cookieValue);
     364                wp_redirect(wp_get_referer());
     365                exit;
     366            }
     367
     368            $chkTemplate = QCTHG_Helper::qcthgChkTemplateName($refTemplateNameLowerCase);
     369
     370            if($chkTemplate) {
     371                $cookieValue = '<b>ERROR: </b>The template name <b>( template-'.$refTemplateNameLowerCase.'.php )</b> already exists in theme root directory!';
     372                QCTHG_Helper::qcthgSetCookies('', $cookieValue);
     373                wp_redirect(wp_get_referer());
     374                exit;
     375            }
     376
     377            if(!empty($data['header_name'])) {
     378                $headerFileNm = $data['header_name'].'.php';
     379                $findHeader = QCTHG_Helper::findHeaderFooter($headerFileNm, $scanChildPath);
     380
     381                if(!$findHeader) {
     382                    $cookieValue = '<b>ERROR: </b>Unable to find theme header! Please enter header name correctly';
     383                    QCTHG_Helper::qcthgSetCookies('', $cookieValue);
     384                    wp_redirect(wp_get_referer());
     385                    exit;
     386                }
     387
     388            } else {
     389                $headerFileNm = 'header.php';
     390                /* check default header exists in child/parent theme */
     391                if(!QCTHG_Helper::findHeaderFooter('header.php', $scanPath) && !QCTHG_Helper::findHeaderFooter('header.php', $scanChildPath)) {
     392                    $cookieValue = '<b>ERROR: </b>Unable to find theme header';
     393                    QCTHG_Helper::qcthgSetCookies('', $cookieValue);
     394                    wp_redirect(wp_get_referer());
     395                    exit;
     396                }
     397            }
     398
     399            if(!empty($data['footer_name'])) {
     400                $footerFileNm = $data['footer_name'].'.php';
     401                $findFooter = QCTHG_Helper::findHeaderFooter($footerFileNm, $scanChildPath);
     402
     403                if(!$findFooter) {
     404                    $cookieValue = '<b>ERROR: </b>Unable to find theme footer! Please enter footer name correctly';
     405                    QCTHG_Helper::qcthgSetCookies('', $cookieValue);
     406                    wp_redirect(wp_get_referer());
     407                    exit;
     408                }
     409
     410            } else {
     411                $footerFileNm = 'footer.php';
     412                /* check default footer exists in child/parent theme */
     413                if(!QCTHG_Helper::findHeaderFooter('footer.php', $scanChildPath) && !QCTHG_Helper::findHeaderFooter('footer.php', $scanPath)) {
     414                    $cookieValue = '<b>ERROR: </b>Unable to find theme footer';
     415                    QCTHG_Helper::qcthgSetCookies('', $cookieValue);
     416                    wp_redirect(wp_get_referer());
     417                    exit;
     418                }
     419            }
     420
     421            $result['template'] = QCTHG_Main::qcthgCreateBlankTemplatePhp($childThemeDir, $refTemplateNameLowerCase, $templateName, $headerFileNm, $footerFileNm);
     422            return $result;
     423        }
     424
     425        /**
     426         * Create blank template php file (with header/footer)
     427         * @param  [string] $childThemeDir
     428         * @param  [string] $refTemplateNameLowerCase
     429         * @param  [string] $templateName
     430         * @param  [string] $headerFileNm
     431         * @param  [string] $footerFileNm
     432         * @return [string]
     433         */
     434        public function qcthgCreateBlankTemplatePhp($childThemeDir, $refTemplateNameLowerCase, $templateName, $headerFileNm, $footerFileNm)
     435        {
     436            $headerFileNmEx = explode('.', $headerFileNm);
     437            $footerFileNmEx = explode('.', $footerFileNm);
     438            $headerNm = 'get_header();';
     439            $footerNm = 'get_footer();';
     440
     441            if($headerFileNmEx[0] != 'header') {
     442                $headerExplode = explode('-', $headerFileNmEx[0]);
     443                $hedrNm = '';
     444                foreach ($headerExplode as $k => $v) {
     445                    if($k > 0) { $hedrNm .= $v; }
     446                }
     447                $headerNm = 'get_header("'.$hedrNm.'");';
     448            }
     449
     450            if($footerFileNmEx[0] != 'footer') {
     451                $footerExplode = explode('-', $footerFileNmEx[0]);
     452                $fotrNm = '';
     453                foreach ($footerExplode as $k => $v) {
     454                    if($k > 0) { $fotrNm .= $v; }
     455                }
     456                $footerNm = 'get_footer("'.$fotrNm.'");';
     457            }
     458
     459            $phpContent = "<?php" . PHP_EOL;
     460            $phpContent .= '/*' . PHP_EOL;
     461            $phpContent .= 'Template Name: ' . $templateName . PHP_EOL;
     462            $phpContent .= '*/' . PHP_EOL;
     463            $phpContent .= "{$headerNm}" . PHP_EOL;
     464            $phpContent .= "?>" . PHP_EOL . PHP_EOL;
     465            $phpContent .= "<!-- Place your custom page code here -->" . PHP_EOL . PHP_EOL;
     466            $phpContent .= "<?php" . PHP_EOL;
     467            $phpContent .= "{$footerNm}" . PHP_EOL;
     468            $phpContent .= "?>";
     469
     470            if(file_put_contents($childThemeDir.'/template-'.$refTemplateNameLowerCase.'.php', $phpContent, LOCK_EX)) {
     471                return 'success';
     472            } else {
     473                $cookieValue = '<b>ERROR: </b>Permissions denied!' .PHP_EOL. 'Unable to create <b>template file</b>. Please set the write permissions for following path: ' .PHP_EOL. $childThemeDir;
     474                QCTHG_Helper::qcthgSetCookies('', $cookieValue);
     475                wp_redirect(wp_get_referer());
     476                exit;
     477            }
     478        }
     479
    285480    }
    286481}
  • quick-child-theme-generator/trunk/includes/qcthg_helper.php

    r2446397 r2562917  
    101101
    102102        /**
    103          * Set cookies for one hour
     103         * Set cookies for 60 seconds
    104104         * @param  $cookieName
    105105         * @param  $cookieValue
     
    110110                $cookieName = "qcthg_adminNotices";
    111111            }
    112             setcookie($cookieName, $cookieValue, time() + (3600), "/");
     112            setcookie($cookieName, $cookieValue, time() + (60), "/");
    113113        }
    114114
     
    123123            <?php
    124124            unset($_COOKIE['qcthg_adminNotices']);
    125             setcookie('qcthg_adminNotices', 0, time() - 3600, '/');
     125            setcookie('qcthg_adminNotices', 0, time() - 60, '/');
     126        }
     127
     128        /**
     129         * Display dynamic success notices (destroy current cookies)
     130         */
     131        public function adminNoticeSuccess() {
     132            ?>
     133            <div class="notice notice-success is-dismissible qcthgErrNotice">
     134                <p><?php _e(str_replace('\\\\', '\\', $_COOKIE['qcthg_adminNoticesSuccess']), 'quck-child-theme-generator'); ?></p>
     135            </div>
     136            <?php
     137            unset($_COOKIE['qcthg_adminNoticesSuccess']);
     138            setcookie('qcthg_adminNoticesSuccess', 0, time() - 60, '/');
    126139        }
    127140
     
    150163        }
    151164
     165        /**
     166         * Check template name exist in theme root directory or not
     167         * @param  $templateName
     168         * @return [boolean]
     169         */
     170        public function qcthgChkTemplateName($templateName)
     171        {
     172            $res = locate_template('template-'.$templateName.'.php');
     173
     174            if($res == false) {
     175                return false;
     176            }
     177            return true;
     178        }
     179
     180        /**
     181         * find the files in specfic path
     182         * @param  $fileNm
     183         * @param  $scanPath
     184         * @return [boolean]
     185         */
     186        public function findHeaderFooter($fileNm, $scanPath)
     187        {
     188            $find = false;
     189            if(in_array($fileNm, $scanPath)) {
     190                $find = true;
     191            }
     192            return $find;
     193        }
     194
    152195    }
    153196}
  • quick-child-theme-generator/trunk/quick-child-theme-generator.php

    r2446397 r2562917  
    33 * Plugin Name: Quick Child Theme Generator
    44 * Plugin URI: https://sharmajay.com/quick-child-theme-generator
    5  * Description: This plugin allows us to generate a quick and easy free child theme.
    6  * Version: 1.1.3
     5 * Description: This plugin allows us to generate a quick and easy free child theme and you can create custom templates as well.
     6 * Version: 2.0.0
    77 * Author: Ajay Kumar
    88 * Author URI: https://sharmajay.com
  • quick-child-theme-generator/trunk/readme.txt

    r2446397 r2562917  
    22Contributors: ajay930
    33Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=HDAU63WNAT4TL
    4 Tags: child theme, childtheme, design, theme, themes, quick theme, childthemes
     4Tags: child theme, childtheme, design, theme, template, themes, quick theme, childthemes
    55Requires at least: 4.7
    6 Tested up to: 5.6
    7 Stable tag: 1.1.3
     6Tested up to: 5.8
     7Stable tag: 2.0.0
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    1414
    1515Quick Child Theme Generator is a very easy, quick and user friendly plugin to create a child theme for any theme supported by WordPress CMS. It allows you to edit or add functionality to your template without having to overwrite any of the parent theme files and it will helpful for restrict losing data on WP update time. You do not need to deal with any code and configuration to create child themes. It will reduce your `time` and `effort` to create a child theme.
     16
     17This plugin is providing you a way to create basic custom templates pages, those will help you to add custom code in specific pages.
    1618
    1719== Installation ==
     
    42442. Select a parent theme from the drop-down list and fill the fields. Then click on the Create Now button.
    43453. Here is information about the plugin and contact.
     464. Enter any template name and fill other fields as per your requirements. Then click on the Create Template button.
    4447
    4548== Changelog ==
     49
     50= 2.0.0 =
     51* Added create custom template feature.
     52* Updated menu and added sub menu.
     53* Added updated instruction images.
    4654
    4755= 1.1.3 =
     
    6472== Upgrade Notice ==
    6573
     74= 2.0.0 =
     75Added create custom template feature.
     76
    6677= 1.1.3 =
    6778Remove plugin page menu from the main menu and added sub menu for it to the Appearance menu.
Note: See TracChangeset for help on using the changeset viewer.