Plugin Directory

Changeset 2606364


Ignore:
Timestamp:
09/28/2021 07:54:50 PM (4 years ago)
Author:
mottodesignstudio
Message:

Preparing for 1.1.2 release

Location:
blaze-css/trunk
Files:
1808 added
13 edited

Legend:

Unmodified
Added
Removed
  • blaze-css/trunk/README.txt

    r2584992 r2606364  
    33Donate link: https://motto.ca
    44Tags: purgecss, purifycss, uncss, tailwind, css optimization, pagespeed, lighthouse
    5 Requires at least: 4.1
    6 Tested up to: 5.7
    7 Stable tag: 1.0.1
     5Requires at least: 5.2
     6Tested up to: 5.8
     7Stable tag: 1.1.2
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    3333== Changelog ==
    3434
     35= 1.1.2 =
     36* Added settings menu.
     37* Fixed PHP notices.
     38* Vendor directory not being deployed properly. 
     39
    3540= 1.0.1 =
    3641* Fixed some PHP notices.
  • blaze-css/trunk/blaze-css.php

    r2584989 r2606364  
    1010 *
    1111 * @link              https://motto.ca
    12  * @since             1.0.0
     12 * @since             1.1.2
    1313 * @package           Blaze_CSS
    1414 *
     
    1717 * Plugin URI:        https://github.com/wpmotto/wp-blaze-css
    1818 * Description:       WordPress plugin to render your CSS as fast as possible. Crush your lighthouse scores by simply activating the plugin.
    19  * Version:           1.0.1
     19 * Version:           1.1.2
    2020 * Author:            Motto
    2121 * Author URI:        https://motto.ca
     
    4747\register_deactivation_hook( __FILE__, '\Motto\BlazeCss\Deactivator::deactivate' );
    4848
     49if (!function_exists('write_log')) {
     50    function write_log($log) {
     51        if (true === WP_DEBUG) {
     52            if (is_array($log) || is_object($log)) {
     53                error_log(print_r($log, true));
     54            } else {
     55                error_log($log);
     56            }
     57        }
     58    }
     59}
     60
     61
    4962/**
    5063 * Begins execution of the plugin.
  • blaze-css/trunk/dist/scripts/blaze-admin.js

    r2499500 r2606364  
    11(function( $ ) {
    22    'use strict';
     3   
     4    $( window ).load(function() {
    35
    4     /**
    5      * All of the code for your Dashboard-specific JavaScript source
    6      * should reside in this file.
    7      *
    8      * Note that this assume you're going to use jQuery, so it prepares
    9      * the $ function reference to be used within the scope of this
    10      * function.
    11      *
    12      * From here, you're able to define handlers for when the DOM is
    13      * ready:
    14      *
    15      * $(function() {
    16      *
    17      * });
    18      *
    19      * Or when the window is loaded:
    20      *
    21      * $( window ).load(function() {
    22      *
    23      * });
    24      *
    25      * ...and so on.
    26      *
    27      * Remember that ideally, we should not attach any more than a single DOM-ready or window-load handler
    28      * for any particular page. Though other scripts in WordPress core, other plugins, and other themes may
    29      * be doing this, we should try to minimize doing that in our own work.
    30      */
     6        $( "#blaze_btn_generate_csv" ).click(function() {
     7
     8            fetch(blaze_ajax_object.ajax_url, {
     9                method: 'POST',
     10                credentials: 'same-origin',
     11                headers: new Headers({'Content-Type': 'application/x-www-form-urlencoded'}),
     12                body: new URLSearchParams({
     13                    action: 'blaze_generate_csv',
     14                    _ajax_nonce: blaze_ajax_object.ajax_nonce
     15                })
     16            })
     17            .then(response => {
     18                console.log('response', response);
     19                // return response.json();
     20            })
     21            // .then(data => {
     22            //  console.log('Success:', data);
     23            // })
     24            // .catch(error => {
     25            //  console.error('Error:', error);
     26            // });
     27
     28        });
     29
     30    });
     31     
    3132
    3233})( jQuery );
  • blaze-css/trunk/dist/scripts/blaze.js

    r2499500 r2606364  
    2222            _ajax_nonce: blaze_ajax_object.ajax_nonce,
    2323            url: window.location.href,
     24            width: window.innerWidth,
     25            height: window.innerHeight,
    2426            log: JSON.stringify(log),
    2527        })
  • blaze-css/trunk/lib/Admin.php

    r2499500 r2606364  
    55 *
    66 * @link       https://github.com/wpmotto/wp-blaze-css
    7  * @since      1.0.0
     7 * @since      1.1.2
    88 *
    99 * @package    BlazeCss
     
    1212
    1313namespace Motto\BlazeCss;
     14
     15use Motto\BlazeCss\Common\File;
    1416
    1517/**
     
    3436    private $plugin;
    3537
     38    private $settings;
     39
    3640    /**
    3741     * Initialize the class and set its properties.
     
    4347    public function __construct( Plugin $plugin ) {
    4448        $this->plugin = $plugin;
     49        $this->settings = new Settings( $plugin );
     50        $this->settings->add_page('settings', 'Blaze Settings');
    4551    }
    4652
     
    6672        \wp_enqueue_style(
    6773            $this->plugin->get_plugin_name(),
    68             \plugin_dir_url( dirname( __FILE__ ) ) . 'dist/styles/plugin-name-admin.css',
     74            \plugin_dir_url( dirname( __FILE__ ) ) . 'dist/styles/blaze-admin.css',
    6975            array(),
    7076            $this->plugin->get_version(),
    7177            'all' );
    72 
    7378    }
    7479
     
    9499        \wp_enqueue_script(
    95100            $this->plugin->get_plugin_name(),
    96             \plugin_dir_url( dirname( __FILE__ ) ) . 'dist/scripts/plugin-name-admin.js',
     101            \plugin_dir_url( dirname( __FILE__ ) ) . 'dist/scripts/blaze-admin.js',
    97102            array( 'jquery' ),
    98103            $this->plugin->get_version(),
    99104            false );
    100105
    101     }
    102 
     106        \wp_localize_script(
     107            $this->plugin->get_plugin_name(),
     108            $this->plugin->get_plugin_name() . '_ajax_object', [
     109            'ajax_url'   => admin_url( 'admin-ajax.php' ),
     110            'ajax_nonce' => wp_create_nonce(
     111                $this->plugin->get_ajax_nonce_name()
     112            )
     113        ]);
     114
     115    }
     116
     117    public function add_settings_page() {
     118        add_options_page(
     119            __( 'Blaze CSS Settings', 'blazecss' ),
     120            __( 'Blaze', 'blazecss' ),
     121            'manage_options',
     122            $this->settings->get_page_name(),
     123            [ $this->settings, 'renderPage' ]
     124        );
     125    }
     126
     127    private function getSettingsConfig() {
     128        return [
     129            'general_settings' => [
     130                'label' => 'General',
     131                'fields' => [
     132                    [
     133                        'name' => 'logging',
     134                        'label' => 'Activate Logging',
     135                    ],
     136                    [
     137                        'name' => 'log_for_all',
     138                        'label' => 'Log for Logged in Users',
     139                    ],
     140                    [
     141                        'name' => 'clean_data',
     142                        'label' => 'Cleanup',
     143                    ],
     144                ],
     145            ],
     146            'generate_csv_settings' => [
     147                'label' => 'Generation results CSV Options',
     148                'fields' => [
     149                    [
     150                        'name' => 'gcsv_auto',
     151                        'label' => 'Generate Automaticaly',
     152                    ],
     153                    [
     154                        'name' => 'gcsv_path_file',
     155                        'label' => 'Path CSV File',
     156                    ],
     157                ],
     158            ]
     159        ];
     160    }
     161
     162    public function init_settings() {
     163        $this->add_generate_csv_after_form();
     164
     165        add_option(
     166            $this->settings->get_options_name(), [
     167                'logging' => 0,
     168                'log_for_all' => 0,
     169                'clean_data' => 0,
     170                'gcsv_auto' => 0,
     171                'gcsv_path_file' => 'uploads/blaze.csv'
     172            ]
     173        );
     174
     175        register_setting(
     176            $this->settings->get_options_name(),
     177            $this->settings->get_options_name(),
     178            [ $this, 'validate' ]
     179        );
     180
     181        $this->settings->registerSettings( $this->getSettingsConfig() );
     182    }
     183
     184    public function generate_csv()
     185    {
     186        // check nonce
     187        \check_ajax_referer(
     188            $this->plugin->get_ajax_nonce_name(),
     189            '_ajax_nonce'
     190        );
     191
     192        $file = new File($this->plugin);
     193        $file->write();
     194        die();
     195    }
     196
     197    private function add_generate_csv_after_form()
     198    {
     199        add_action('after_blaze-settings_page', function() {
     200            if( $this->settings->get_option('gcsv_auto') == 1 )
     201                return;
     202        ?>
     203        <div>
     204            <h3>Manually Generate CSS .CSV File</h3>
     205            <p>Click on the following button to generate the .csv file with the results.</p>
     206            <button id="<?php echo $this->settings->field_id_from_name('btn_generate_csv') ?>">
     207                Generate CSV
     208            </button>
     209        </div> 
     210        <?php       
     211        });
     212    }
     213
     214    public function validate( $input ) {
     215        $input['logging'] = boolval($input['logging'] ?? false);
     216        $input['clean_data'] = boolval($input['clean_data'] ?? false);
     217        $input['gcsv_auto'] = boolval($input['gcsv_auto'] ?? false);
     218        $input['gcsv_path_file'] = $this->sanitizeCsvPath($input['gcsv_path_file']);
     219        return $input;
     220    }
     221
     222    private function sanitizeCsvPath( $input )
     223    {
     224        $path = trim(strval($input ?? ''));
     225        $dir = dirname(WP_CONTENT_DIR . "/$path");
     226        if( $dir && file_exists($dir) ) {
     227            return $path;
     228        } else {
     229            add_settings_error(
     230                $this->settings->field_name_from_name('gcsv_path_file'), 'gcsv_path_file',
     231                "This path does not exist."
     232            );
     233            return $this->settings->get_option('gcsv_path_file');
     234        }
     235    }
    103236}
  • blaze-css/trunk/lib/Common/File.php

    r2567479 r2606364  
    77class File {
    88
    9     public static function write()
    10     {
    11         $path = wp_get_upload_dir()['basedir'] . '/blaze.csv';
    12         $elements = (new Element)->select('DISTINCT el_class')->where('el_class IS NOT NULL')->get();
    13         $classes = implode("\n", array_column($elements, 'el_class'));
     9    protected $plugin;
     10
     11    public function __construct( \Motto\BlazeCss\Plugin $plugin ) {
     12        $this->plugin = $plugin;
     13    }
     14
     15    public function write()
     16    {   
     17        $csv_file_path = $this->plugin->settings->get_option('gcsv_path_file');
     18        $path = WP_CONTENT_DIR . $csv_file_path;
     19       
     20        $elements = (new Element)->select('DISTINCT el_class')
     21                                ->where('el_class IS NOT NULL')
     22                                ->get();
     23
     24        $classes = implode(
     25            "\n", array_column($elements, 'el_class')
     26        );
     27       
    1428        file_put_contents($path, $classes);
    1529    }
  • blaze-css/trunk/lib/Common/Logger.php

    r2584992 r2606364  
    1111    protected $plugin;
    1212    protected $url;
     13    protected $viewport;
    1314    protected $pageQuery;
    14     protected $log;
     15    protected $log = null;
    1516
    1617    public function __construct( Array $postData, Plugin $plugin )
     
    1819        $this->plugin = $plugin;
    1920        $this->url = (object) parse_url($postData['url']);
     21        $this->viewport = [
     22            'width' => $postData['width'],
     23            'height' => $postData['height'],
     24        ];
    2025        if( !isset($this->url->query) )
    2126            $this->url->query = null;
     
    4954        if( $this->cacheMiss() ) {
    5055            $this->create();
     56
     57            /**
     58             * Log elements for each user-agent
     59             */
    5160            Element::fromLogger( $this );
    52             File::write();
     61
     62            // change for event driven do_action
     63            if( (bool) $this->plugin->settings->get_option('gcsv_auto') ) {
     64                $file = new File($this->plugin);
     65                $file->write();
     66            }           
    5367        }
    5468    }
     
    5973            $this->log = $found;
    6074
    61         return true;
     75        return is_null($this->log);
    6276    }
    6377
     
    6781            host = %s
    6882            AND path = %s
    69         ", [$this->url->host, $this->url->path])->get();
     83            AND query = %s
     84        ", [
     85            $this->url->host,
     86            $this->url->path,
     87            $this->url->query,
     88        ])->get();
    7089       
    7190        if( !empty($logs) ) {
     
    7392
    7493            $this->log->update([
    75                 'logged_at' => time(),
     94                'logged_at' => date('Y-m-d H:i:s'),
    7695                'hash' => $this->hash(),
    7796            ]);
    7897        } else {
     98            $current_theme = wp_get_theme();
     99
    79100            $this->log = new Log;
    80101            $this->log->insert([
     
    83104                'query' => $this->url->query,
    84105                'hash' => $this->hash(),
     106                'theme' => $current_theme->get( 'Name' ),
     107                'width' => $this->viewport['width'],
     108                'height' => $this->viewport['height'],
    85109            ]);
    86110        }
  • blaze-css/trunk/lib/Common/Model.php

    r2584992 r2606364  
    7878    {
    7979        $sql = <<<SQL
    80         SELECT {$this->query['select']}
    81         FROM {$this->getTable()}           
    82         WHERE {$this->query['where']}
    83         SQL;
     80SELECT {$this->query['select']}
     81FROM {$this->getTable()}           
     82WHERE {$this->query['where']}
     83SQL;
    8484
    8585        return $this->db->get_results(
     
    9191    {
    9292        $sql = <<<SQL
    93         SELECT * FROM {$this->getTable()}
    94         WHERE $prop = %s
    95         SQL;
     93SELECT * FROM {$this->getTable()}
     94WHERE $prop = %s
     95SQL;
    9696
    9797        $row = $this->db->get_row(
  • blaze-css/trunk/lib/Common/Schema.php

    r2499500 r2606364  
    1010
    1111    protected $tables = [
     12        'elements',
    1213        'logs',
    13         'elements'
    1414    ];
    1515    protected $db;
     
    3333    {
    3434        return <<<SQL
    35             id mediumint(9) NOT NULL AUTO_INCREMENT,
    36             logged_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    37             `host` varchar(55) NOT NULL,
    38             `path` varchar(55) NOT NULL,
    39             `query` varchar(55),
    40             hash CHAR(32) NOT NULL,
    41             PRIMARY KEY (id),
    42             UNIQUE KEY(`host`, `path`, hash)
     35        `id` mediumint(9) NOT NULL AUTO_INCREMENT,
     36        `logged_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
     37        `host` varchar(55) NOT NULL,
     38        `path` varchar(55) NOT NULL,
     39        `query` varchar(55),
     40        `hash` CHAR(32) NOT NULL,
     41        `theme` varchar(55) NOT NULL,
     42        `width` mediumint(9) UNSIGNED NOT NULL,
     43        `height` mediumint(9) UNSIGNED NOT NULL,
     44        PRIMARY KEY  (id),
     45        UNIQUE KEY (`host`, `path`, `hash`)
    4346        SQL;
    4447    }
     
    4750    {
    4851        return <<<SQL
    49             id mediumint(9) NOT NULL AUTO_INCREMENT,
    50             log_id mediumint(9) NOT NULL,
    51             el_tag tinytext NOT NULL,
    52             el_id VARCHAR(255),
    53             el_class VARCHAR(510),
    54             height FLOAT,
    55             `left` FLOAT,
    56             `right` FLOAT,
    57             `top` FLOAT,
    58             width FLOAT,
    59             x FLOAT,
    60             y FLOAT,
    61             PRIMARY KEY  (id)
     52        `id` mediumint(9) NOT NULL AUTO_INCREMENT,
     53        `log_id` mediumint(9) NOT NULL,
     54        `el_tag` tinytext NOT NULL,
     55        `el_id` VARCHAR(255),
     56        `el_class` VARCHAR(510),
     57        `user_agent` VARCHAR(255),
     58        `height` FLOAT,
     59        `left` FLOAT,
     60        `right` FLOAT,
     61        `top` FLOAT,
     62        `width` FLOAT,
     63        `x` FLOAT,
     64        `y` FLOAT,
     65        PRIMARY KEY  (`id`)
    6266        SQL;
    6367    }
     
    6569    public function init()
    6670    {
    67         $sql = [];
     71        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    6872        foreach( $this->tables as $table ) {
    6973            $schema = $this->{$table . '_schema'}();
    70             $sql[] = "CREATE TABLE {$this->getTableName($table)} (
     74            $sql = "CREATE TABLE IF NOT EXISTS {$this->getTableName($table)} (
    7175                $schema
    72             ) {$this->db->get_charset_collate()};";
     76            ) {$this->db->get_charset_collate()}";
     77            dbDelta( $sql );
    7378        }
    74         require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    75         dbDelta( $sql );
     79
    7680        add_option( $this->version_key, $this->version );
    7781    }
     
    106110       
    107111        //     update_option( "jal_db_version", $jal_db_version );
    108         // }       
     112        // }
    109113    }
    110114}
  • blaze-css/trunk/lib/Deactivator.php

    r2499500 r2606364  
    3636     */
    3737    public static function deactivate() {
    38         $schema = new Schema((new Plugin));
    39         $schema->destroy();
     38        $plugin = new Plugin();
     39        if( $plugin->settings->get_option('clean_data') == 1 ) {
     40            $schema = new Schema($plugin);
     41            $schema->destroy();
     42            $plugin->settings->destroy();
     43        }
    4044    }
    4145
  • blaze-css/trunk/lib/Frontend.php

    r2567479 r2606364  
    5555     */
    5656    public function enqueue_scripts() {
    57         if( is_user_logged_in() )
    58             return;
    59 
    6057        \wp_enqueue_script(
    6158            $this->plugin->get_plugin_name(),
  • blaze-css/trunk/lib/Models/Element.php

    r2499500 r2606364  
    1616    public static function fromLogger( Logger $logger )
    1717    {
     18        /**
     19         * TODO: only add if body width and height are different
     20         */
    1821        (new self)->delete(['log_id' => $logger->getLogId()]);
    1922        foreach( $logger->getPageQuery() as $data )
  • blaze-css/trunk/lib/Plugin.php

    r2584989 r2606364  
    88 *
    99 * @link       https://github.com/wpmotto/wp-blaze-css
    10  * @since      1.0.0
     10 * @since      1.1.2
    1111 *
    1212 * @package    BlazeCss
     
    2525 * version of the plugin.
    2626 *
    27  * @since      1.0.0
     27 * @since      1.1.2
    2828 * @package    BlazeCss
    2929 * @subpackage BlazeCss/includes
     
    3636     * the plugin.
    3737     *
    38      * @since    1.0.0
     38     * @since    1.1.2
    3939     * @access   protected
    4040     * @var      Motto\BlazeCss\Loader    $loader    Maintains and registers all hooks for the plugin.
     
    4242    protected $loader;
    4343
     44    public $settings;
     45
    4446    protected $db;
    4547
     
    6062     * @var      string    $version    The current version of the plugin.
    6163     */
    62     protected $version = '1.0.0';
     64    protected $version = '1.1.2';
    6365
    6466    /**
     
    7274    public function __construct() {
    7375        $this->loader = new Loader();
     76        $this->settings = new Settings($this);
     77    }
     78
     79    public function get_root_path()
     80    {
     81        return plugin_dir_path( dirname( __FILE__ ) );     
    7482    }
    7583
     
    105113        // $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
    106114
     115        $this->loader->add_action( 'admin_menu', $plugin_admin, 'add_settings_page' );
     116        $this->loader->add_action( 'admin_init', $plugin_admin, 'init_settings' );
     117
     118        if( (bool) $this->settings->get_option('gcsv_auto') )
     119            $this->loader->add_action( 'wp_ajax_blaze_generate_csv', $plugin_admin, 'generate_csv' );
    107120    }
    108121
     
    118131        $plugin_frontend = new Frontend( $this );
    119132
    120         $this->loader->add_action( 'wp_enqueue_scripts', $plugin_frontend, 'enqueue_scripts' );
    121 
    122         $this->loader->add_action( 'get_header', $plugin_frontend, 'debug' );
     133        if( $this->should_log() ) {
     134            $this->loader->add_action(
     135                'wp_enqueue_scripts', $plugin_frontend, 'enqueue_scripts'
     136            );
     137            $this->loader->add_action(
     138                'get_header', $plugin_frontend, 'debug'
     139            );
     140        }
    123141
    124142        /**
     
    129147
    130148        // to logged in users
    131         // $this->loader->add_action( 'wp_ajax_blaze_ajax', $plugin_frontend, 'save_page_elements' );
     149        $this->loader->add_action( 'wp_ajax_blaze_ajax', $plugin_frontend, 'save_page_elements' );
    132150       
    133151        // to not logged in users or users without permissions
    134         $this->loader->add_action( 'wp_ajax_nopriv_blaze_ajax', $plugin_frontend, 'save_page_elements' );
     152        $this->loader->add_action(
     153            'wp_ajax_nopriv_blaze_ajax', $plugin_frontend, 'save_page_elements'
     154        );
     155    }
     156
     157    private function should_log()
     158    {
     159        $logging = (bool) $this->settings->get_option('logging');
     160        $log_for_all = (bool) $this->settings->get_option('log_for_all');
     161        if( !$logging )
     162            return false;
     163
     164        if( !$log_for_all && is_user_logged_in() )
     165            return false;
     166
     167        return true;
    135168    }
    136169
Note: See TracChangeset for help on using the changeset viewer.