reaktivstudios / gppro-export-css

Adds a button to the Design Palette Pro settings tab to export a raw CSS file
0 stars 0 forks source link

svn diff #7

Open NicktheGeek opened 4 years ago

NicktheGeek commented 4 years ago
svn diff trunk/gppro-export-css.php
Index: trunk/gppro-export-css.php
===================================================================
--- trunk/gppro-export-css.php  (revision 2200664)
+++ trunk/gppro-export-css.php  (working copy)
@@ -1,124 +1,132 @@
 <?php
-/**
- * Plugin Name: Genesis Design Palette Pro - Export CSS
- * Plugin URI: https://genesisdesignpro.com/
- * Description: Adds a button to export raw CSS file
- * Author: Reaktiv Studios
- * Version: 1.1
- * Requires at least: 3.7
- * Author URI: https://genesisdesignpro.com
- *
- * @package gppro-export-css
- */
+/*
+Plugin Name: Genesis Design Palette Pro - Export CSS
+Plugin URI: https://genesisdesignpro.com/
+Description: Adds a button to export raw CSS file
+Author: Reaktiv Studios
+Version: 1.0.2
+Requires at least: 3.7
+Author URI: http://andrewnorcross.com
+*/
+/*  Copyright 2014 Andrew Norcross

-/**
- * Copyright 2018 Reaktiv Studios, Inc.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License (GPL v2) only.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- */
+       This program is free software; you can redistribute it and/or modify
+       it under the terms of the GNU General Public License as published by
+       the Free Software Foundation; version 2 of the License (GPL v2) only.

-if ( ! defined( 'GPXCS_BASE' ) ) {
-       define( 'GPXCS_BASE', plugin_basename( __FILE__ ) );
+       This program is distributed in the hope that it will be useful,
+       but WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+       GNU General Public License for more details.
+
+       You should have received a copy of the GNU General Public License
+       along with this program; if not, write to the Free Software
+       Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+if( ! defined( 'GPXCS_BASE' ) ) {
+       define( 'GPXCS_BASE', plugin_basename(__FILE__) );
 }

-if ( ! defined( 'GPXCS_DIR' ) ) {
+if( ! defined( 'GPXCS_DIR' ) ) {
        define( 'GPXCS_DIR', dirname( __FILE__ ) );
 }

-if ( ! defined( 'GPXCS_VER' ) ) {
-       define( 'GPXCS_VER', '1.1' );
+if( ! defined( 'GPXCS_VER' ) ) {
+       define( 'GPXCS_VER', '1.0.2' );
 }

-/**
- * Class GP_Pro_Export_CSS.
- */
-class GP_Pro_Export_CSS {

+class GP_Pro_Export_CSS
+{
+
        /**
-        * Static property to hold our singleton instance.
-        *
+        * Static property to hold our singleton instance
         * @var GP_Pro_Export_CSS
         */
-       public static $instance = false;
+       static $instance = false;

        /**
-        * This is our constructor.
+        * This is our constructor
+        *
+        * @return GP_Pro_Export_CSS
         */
        private function __construct() {

-               // General backend.
-               add_action( 'plugins_loaded', array( $this, 'textdomain' ) );
-               add_action( 'admin_init', array( $this, 'export_css_file' ) );
-               add_action( 'admin_notices', array( $this, 'gppro_active_check' ), 10 );
-               add_action( 'admin_notices', array( $this, 'export_css_notices' ) );
-               add_action( 'admin_head', array( $this, 'export_css_style' ) );
+               // general backend
+               add_action              (       'plugins_loaded',                                           array(   $this,  'textdomain'                            )                       );
+               add_action              (       'admin_init',                                               array(   $this,  'export_css_file'                       )                       );
+               add_action              (       'admin_notices',                                            array(   $this,  'gppro_active_check'            ),      10              );
+               add_action              (       'admin_notices',                                            array(   $this,  'export_css_notices'            )                       );
+               add_action              (       'admin_head',                                               array(   $this,  'export_css_style'                      )                       );

-               // GP Pro specific.
-               add_filter( 'dpp_settings', array( $this, 'export_css_section' ), 15, 2 );
+               // GP Pro specific
+               add_filter              (       'gppro_section_inline_build_settings',          array(  $this,       'export_css_section'            ),      15,     2       );
        }

        /**
-        * If an instance exists, this returns it.  If not, it creates one and returns it.
+        * If an instance exists, this returns it.  If not, it creates one and
+        * retuns it.
         *
         * @return GP_Pro_Export_CSS
         */
-       public static function get_instance() {
-               if ( ! self::$instance ) {
-                       self::$instance = new self();
+       public static function getInstance() {
+
+               if ( !self::$instance ) {
+                       self::$instance = new self;
                }
                return self::$instance;
        }

        /**
-        * Load textdomain.
+        * load textdomain
+        *
+        * @return
         */
        public function textdomain() {
+
                load_plugin_textdomain( 'gppro-export-css', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
+
        }

        /**
-        * Check for GP Pro being active.
+        * check for GP Pro being active
+        *
+        * @return GP_Pro_Export_CSS
         */
        public function gppro_active_check() {
-               // Get the current screen.
+               // get the current screen
                $screen = get_current_screen();
-               // Bail if not on the plugins page.
-               if ( is_object( $screen ) && 'plugins.php' !== $screen->parent_file ) {
+               // bail if not on the plugins page
+               if ( is_object( $screen ) && $screen->parent_file !== 'plugins.php' ) {
                        return;
                }
-               // Run the active check.
-               $coreactive = class_exists( 'Genesis_Palette_Pro' ) ? Genesis_Palette_Pro::check_active() : false;
-               // Active. Bail.
+               // run the active check
+               $coreactive     = class_exists( 'Genesis_Palette_Pro' ) ? Genesis_Palette_Pro::check_active() : false;
+               // active. bail
                if ( $coreactive ) {
                        return;
                }
-               // Not active. Show message.
-               echo '<div id="message" class="error fade below-h2"><p><strong>' . esc_html__( 'This plugin requires Genesis Design Palette Pro to function and cannot be activated.', 'gppro-export-css' ) . '</strong></p></div>';
-               // Hide activation method.
+               // not active. show message
+               echo '<div id="message" class="error fade below-h2"><p><strong>'.__( sprintf( 'This plugin requires Genesis Design Palette Pro to function and cannot be activated.' ), 'gppro-export-css' ).'</strong></p></div>';
+               // hide activation method
                unset( $_GET['activate'] );
-               // Deactivate the plugin.
+               // deactivate the plugin
                deactivate_plugins( plugin_basename( __FILE__ ) );
+               // and finish
+               return;
        }

        /**
-        * Add CSS to the admin head.
+        * add CSS to the admin head
+        *
+        * @return CSS
         */
        public function export_css_style() {
-               // Fetch current screen.
-               $screen = get_current_screen();
-               // Display our admin UI CSS if on DPP page.
-               if ( is_object( $screen ) && 'genesis_page_genesis-palette-pro' === $screen->base ) {
+               // fetch current screen
+               $screen = get_current_screen();
+               // display our admin UI CSS if on DPP page
+               if ( is_object( $screen ) && $screen->base == 'genesis_page_genesis-palette-pro' ) {
                        echo '<style media="all" type="text/css">';
                        echo 'a.gppro-css-export-view{display:inline-block;margin-left:5px;text-decoration:none;}';
                        echo '</style>';
@@ -126,98 +134,107 @@
        }

        /**
-        * Display messages if export failure.
+        * display messages if export failure
+        *
+        * @return mixed
         */
        public function export_css_notices() {

-               // First check to make sure we're on our settings.
-               if ( ! isset( $_GET['page'] ) || isset( $_GET['page'] ) && 'genesis-palette-pro' !== $_GET['page'] ) { // WPCS: csrf ok.
+               // first check to make sure we're on our settings
+               if ( ! isset( $_REQUEST['page'] ) || isset( $_REQUEST['page'] ) && $_REQUEST['page'] !== 'genesis-palette-pro' )
                        return;
-               }

-               // Check our CSS export action.
-               if ( ! isset( $_GET['export-css'] ) ) { // WPCS: csrf ok.
+               // check our CSS export action
+               if ( ! isset( $_REQUEST['export-css'] ) )
                        return;
-               }

-               // Check for non failure.
-               if ( isset( $_GET['export-css'] ) && 'failure' !== $_GET['export-css'] ) { // WPCS: csrf ok.
+               // check for non failure
+               if ( isset( $_REQUEST['export-css'] ) && $_REQUEST['export-css'] != 'failure' )
                        return;
-               }

-               // Check for failure.
-               if ( isset( $_GET['export-css'] ) && 'failure' === $_GET['export-css'] ) { // WPCS: csrf ok.
+               // check for failure
+               if ( isset( $_REQUEST['export-css'] ) && $_REQUEST['export-css'] == 'failure' ) {

-                       // Set a default message.
-                       $message = __( 'There was an error with your export. Please try again later.', 'gppro-export-css' );
+                       // set a default message
+                       $message        = __( 'There was an error with your export. Please try again later.', 'gppro-export-css' );

-                       // No parent class present.
-                       if ( 'noclass' === $_GET['reason'] ) { // WPCS: csrf ok.
-                               $message = __( 'The main Genesis Design Palette Pro files are not present.', 'gppro-export-css' );
+                       // no parent class present
+                       if ( $_REQUEST['reason'] == 'noclass' ) {
+                               $message        = __( 'The main Genesis Design Palette Pro files are not present.', 'gppro-export-css' );
                        }

-                       // No data stored.
-                       if ( 'nodata' === $_GET['reason'] ) { // WPCS: csrf ok.
-                               $message = __( 'No settings data has been saved. Please save your settings and try again.', 'gppro-export-css' );
+                       // no data stored
+                       if ( $_REQUEST['reason'] == 'nodata' ) {
+                               $message        = __( 'No settings data has been saved. Please save your settings and try again.', 'gppro-export-css' );
                        }

-                       // No CSS file present.
-                       if ( 'nofile' === $_GET['reason'] ) { // WPCS: csrf ok.
-                               $message = __( 'No CSS file exists to export. Please save your settings and try again.', 'gppro-export-css' );
+                       // no CSS file present
+                       if ( $_REQUEST['reason'] == 'nofile' ) {
+                               $message        = __( 'No CSS file exists to export. Please save your settings and try again.', 'gppro-export-css' );
                        }

-                       // Return the message.
+
+                       // return the message
                        echo '<div id="message" class="error">';
-                       echo '<p>' . esc_attr( $message ) . '</p>';
+                       echo '<p>'.esc_attr( $message ).'</p>';
                        echo '</div>';
+
+                       return;
+
                }
+
+               return;
+
        }

        /**
-        * Export our CSS file.
+        * export our CSS file
         *
         * @return mixed
         */
        public function export_css_file() {
-               // Check nonce.
-               if ( empty( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'gppro_css_export_nonce' ) ) {
+
+               // check page and query string
+               if ( ! isset( $_REQUEST['gppro-css-export'] ) || isset( $_REQUEST['gppro-css-export'] ) && $_REQUEST['gppro-css-export'] != 'go' )
                        return;
-               }

-               // Check page and query string.
-               if ( ! isset( $_GET['gppro-css-export'] ) || isset( $_GET['gppro-css-export'] ) && 'go' !== $_GET['gppro-css-export'] ) {
+               // check nonce
+               $nonce = $_REQUEST['_wpnonce'];
+               if ( ! wp_verify_nonce( $nonce, 'gppro_css_export_nonce' ) ) {
                        return;
                }

-               // Get current settings.
-               $current = get_option( 'gppro-settings' );
+               // get current settings
+               $current        = get_option( 'gppro-settings' );

-               // If settings empty, bail.
+               // if settings empty, bail
                if ( empty( $current ) ) {
-                       $failure = menu_page_url( 'genesis-palette-pro', 0 ) . '&section=build_settings&export-css=failure&reason=nodata';
+                       $failure        = menu_page_url( 'genesis-palette-pro', 0 ).'&section=build_settings&export-css=failure&reason=nodata';
                        wp_safe_redirect( $failure );

                        return;
                }

-               // Check for class.
+               // check for class
                if ( ! class_exists( 'Genesis_Palette_Pro' ) ) {
-                       $failure = menu_page_url( 'genesis-palette-pro', 0 ) . '&section=build_settings&export-css=failure&reason=noclass';
+                       $failure        = menu_page_url( 'genesis-palette-pro', 0 ).'&section=build_settings&export-css=failure&reason=noclass';
                        wp_safe_redirect( $failure );

                        return;
                }

-               $output = get_theme_mod( 'dpp_styles' );
+               // get CSS file
+               $file   = Genesis_Palette_Pro::filebase();
+               if ( ! file_exists( $file['dir'] ) ) {
+                       $failure        = menu_page_url( 'genesis-palette-pro', 0 ).'&section=build_settings&export-css=failure&reason=nofile';
+                       wp_safe_redirect( $failure );

-               if ( empty( $output ) ) {
-                       $failure = menu_page_url( 'genesis-palette-pro', 0 ) . '&section=build_settings&export-css=failure&reason=nofile';
-                       wp_safe_redirect( esc_url( $failure ) );
-
                        return;
                }

-               // Prepare and send the export file to the browser.
+               $output = file_get_contents( $file['dir'] );
+
+               //* Prepare and send the export file to the browser
                header( 'Content-Description: File Transfer' );
                header( 'Cache-Control: public, must-revalidate' );
                header( 'Pragma: hack' );
@@ -224,111 +241,112 @@
                header( 'Content-type: text/css; charset=utf-8' );
                header( 'Content-Disposition: attachment; filename="gppro-custom.css"' );
                header( 'Content-Length: ' . mb_strlen( $output ) );
-               echo $this->escape_css( $output ); // WPCS: xss ok.
+               echo $output;
                exit();
+
        }

+
        /**
-        * Take the CSS data stored in the settings row and escape it for proper output.
+        * add new option for exporting CSS
         *
-        * @param  string $data the sanitized CSS data stored.
-        *
-        * @return string $data the escaped and encoded CSS data to output.
+        * @return string $items
         */
-       public function escape_css( $data = '' ) {
+       public function export_css_section( $items, $class ) {

-               // Convert single quotes to double quotes.
-               $data = str_replace( '\'', '"', $data );
+               // add section header for export
+               $items['section-break-css-export']      = array(
+                       'break' => array(
+                               'type'  => 'full',
+                               'title' => __( 'Export Raw CSS', 'gppro-export-css' ),
+                               'text'  => __( 'Download a stand-alone CSS file', 'gppro-export-css' ),
+                       ),
+               );

-               // Escape it.
-               $data = esc_attr( $data );
+               // add button for export
+               $items['css-export-area-setup'] = array(
+                       'title'         => '',
+                       'data'          => array(
+                               'css-export-field'      => array(
+                                       'label'         => __( 'Download CSS file', 'gppro-export-css' ),
+                                       'button'        => __( 'Export CSS', 'gppro-export-css' ),
+                                       'input'         => 'custom',
+                                       'callback'      => array( $this, 'export_css_input' )
+                               ),
+                       ),
+               );

-               // Now decode it.
-               $data = html_entity_decode( $data );
+               return $items;

-               // And return it, filtered.
-               return apply_filters( 'gppro_export_css_escaped', $data );
        }

-
        /**
-        * Add new option for exporting CSS.
+        * create input field for CSS export
         *
-        * @param  array $settings The DPP settings array.
-        * @return array
+        * @return
         */
-       public function export_css_section( $settings ) {
-               $settings['gppro-export-css'] = array(
-                       'label'    => __( 'Export Raw CSS', 'gppro-export-css' ),
-                       'section'  => 'utilities',
-                       'callback' => array( $this, 'export_css_input' ),
-               );
+       static function export_css_input( $field, $item ) {

-               return $settings;
-       }
+               // bail if items missing
+               if ( ! $field || ! $item ) {
+                       return;
+               }

-       /**
-        * Create input field for CSS export.
-        */
-       public function export_css_input() {
-
-               // First check for the data.
-               $saved = get_option( 'gppro-settings' );
-               // Display message without saved options.
+               // first check for the data
+               $saved  = get_option( 'gppro-settings' );
+               // display message without saved options
                if ( empty( $saved ) ) {
-                       $text = __( 'No data has been saved. Please save your settings before attempting to export.', 'gppro-export-css' );
-                       echo '<div class="gppro-input gppro-description-input"><p class="description">' . esc_attr( $text ) . '</p></div>';
+                       $text   = __( 'No data has been saved. Please save your settings before attempting to export.', 'gppro-export-css' );
+                       return '<div class="gppro-input gppro-description-input"><p class="description">' . esc_attr( $text ) . '</p></div>';
                }

-               // Get my values.
-               $id     = 'gppro-export-css';
-               $name   = 'gppro-export-css';
-               $button = __( 'Export File', 'gppro-export-css' );
+               // get my values
+               $id                     = GP_Pro_Helper::get_field_id( $field );
+               $name           = GP_Pro_Helper::get_field_name( $field );
+               $button         = ! empty( $item['button'] ) ? esc_attr( $item['button'] ) : __( 'Export File', 'gppro-export-css' );

-               // Get CSS file for link.
-               $file_key = get_theme_mod( 'dpp_file_key' );
+               // get CSS file for link
+               $file   = Genesis_Palette_Pro::filebase();

-               // Create export URL with nonce.
-               $expnonce = wp_create_nonce( 'gppro_css_export_nonce' );
+               // create export URL with nonce
+               $expnonce       = wp_create_nonce( 'gppro_css_export_nonce' );

-               // Set the empty.
-               $input = '';
-               // Begin markup.
-               $input .= '<div class="gppro-input gppro-css-export-input gppro-setting-input">';
+               // set the empty
+               $input  = '';
+               // begin markup
+               $input  .= '<div class="gppro-input gppro-css-export-input gppro-setting-input">';
+                       // handle label with optional CSS file link
+                       $input  .= '<div class="gppro-input-item gppro-input-wrap"><p class="description">';
+                               $input  .= esc_attr( $item['label'] );
+                               // handle browser link
+                               if ( file_exists( $file['dir'] ) && ! empty( $file['url'] ) ) {
+                                       $input  .= '<a class="gppro-css-export-view" href="' . esc_url( $file['url'] ) . '" title="' . __( 'View in browser', 'gppro-export-css' ) . '" target="_blank">';
+                                       $input  .= '<i class="dashicons dashicons-admin-site"></i>';
+                                       $input  .= '</a>';
+                               }

-               // Handle browser link.
-               if ( ! empty( $file_key ) ) {
-                       // Handle label with optional CSS file link.
-                       $input .= '<div class="gppro-input-item gppro-input-wrap"><p class="description">';
+                       $input  .= '</p></div>';

-                       $input .= esc_html__( 'View CSS file', 'gppro-export-css' );
-                       $url    = sprintf( '%1$sdpp-custom-styles-%2$s', trailingslashit( get_site_url() ), $file_key );
-                       $input .= '<a class="gppro-css-export-view" href="' . esc_url( $url ) . '" title="' . __( 'View in browser', 'gppro-export-css' ) . '" target="_blank">';
-                       $input .= '<i class="dashicons dashicons-admin-site"></i>';
-                       $input .= '</a>';
+                       // display button
+                       $input  .= '<div class="gppro-input-item gppro-input-label choice-label">';
+                               $input  .= '<span class="gppro-settings-button">';

-                       $input .= '</p></div>';
-               }
+                               $input  .= '<a name="' . esc_attr( $name ) . '" id="' . sanitize_html_class( $id ) . '" href="' . menu_page_url( 'genesis-palette-pro', 0 ) . '&gppro-css-export=go&_wpnonce=' . $expnonce . '" class="button-primary button-small ' . esc_attr( $field ) . '">' . $button . '</a>';

-                       // Display button.
-                       $input     .= '<div class="gppro-input-item gppro-input-label choice-label">';
-                               $input .= '<span class="gppro-settings-button">';
+                               $input  .= '</span>';
+                       $input .= '</div>';

-                               $input .= '<a name="' . esc_attr( $name ) . '" id="' . sanitize_html_class( $id ) . '" href="' . menu_page_url( 'genesis-palette-pro', 0 ) . '&gppro-css-export=go&_wpnonce=' . $expnonce . '" class="button-primary button-small">' . $button . '</a>';
+               // close markup
+               $input  .= '</div>';

-                               $input .= '</span>';
-                       $input     .= '</div>';
+               // send it back
+               return $input;

-               // Close markup.
-               $input .= '</div>';
-
-               // Send it back.
-               echo $input; // WPCS: xss ok.
-
        }

-       // End class.
+/// end class
 }

-// Instantiate our class.
-$gp_pro_export_css = GP_Pro_Export_CSS::get_instance();
+// Instantiate our class
+$GP_Pro_Export_CSS = GP_Pro_Export_CSS::getInstance();
+
NicktheGeek commented 4 years ago

Looks like we need to configure phpcs for this and resolve some issues with white space etc. I also see that the version in the plugin file and constant are not aligned.

@admills do you know if this has been tested lately? Seems like it has so the code should be g2g once the white space and other wpcs issues are corrected.