Plugin Directory

Changeset 2658195


Ignore:
Timestamp:
01/15/2022 09:19:07 PM (4 years ago)
Author:
hexydec
Message:

Updated assets::buildCss() and assets::buildJavascript() to check if the file exists so that if a plugin is removed, the assets build without issue.
Added CLI command "torque rebuild".
Updated readme.txt.

Location:
torque/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • torque/trunk/assets.php

    r2615365 r2658195  
    234234        $css = '';
    235235        foreach ($files AS $item) {
    236             if (($file = \file_get_contents($item)) !== false) {
     236            if (\file_exists($item) && ($file = \file_get_contents($item)) !== false) {
    237237                $css .= \preg_replace_callback('/url\\([\'"]?+([^\\)"\':]++)[\'"]?\\)/i', function (array $match) use ($item) {
    238238                    \chdir(\dirname($item));
     
    282282        // minify each file
    283283        foreach ($files AS $item) {
    284             if (($file = \file_get_contents($item)) !== false) {
     284            if (\file_exists($item) && ($file = \file_get_contents($item)) !== false) {
    285285                $js .= ($js ? "\n\n" : '').$file;
    286286            }
     
    312312        return false;
    313313    }
     314
     315    /**
     316     * Rebuilds the configured combined assets
     317     *
     318     * @return bool Whether the assets were regenerated
     319     */
     320    public static function rebuildAssets() : bool {
     321        if (($options = \get_option(packages::SLUG)) !== false) {
     322            $success = true;
     323            $dir = \dirname(\dirname(\dirname(__DIR__))).'/'; // can't use get_home_path() here
     324
     325            // rebuild CSS
     326            if (!empty($options['combinestyle']) && \is_array($options['combinestyle'])) {
     327                $files = [];
     328                foreach ($options['combinestyle'] AS $item) {
     329                    $files[] = $dir.$item;
     330                }
     331                $target = __DIR__.'/build/'.\md5(\implode(',', $options['combinestyle'])).'.css';
     332                if (!self::buildCss($files, $target, $options['minifystyle'] ? ($options['style'] ?? []) : null)) {
     333                    $success = false;
     334                }
     335            }
     336
     337            // rebuild javascript
     338            if (!empty($options['combinescript']) && \is_array($options['combinescript'])) {
     339                $files = [];
     340                foreach ($options['combinescript'] AS $item) {
     341                    $files[] = $dir.$item;
     342                }
     343                $target =  __DIR__.'/build/'.\md5(\implode(',', $options['combinescript'])).'.js';
     344                if (!self::buildJavascript($files, $target, $options['minifyscript'] ? ($options['script'] ?? []) : null)) {
     345                    $success = false;
     346                }
     347            }
     348            return $success;
     349        }
     350        return false;
     351    }
    314352}
  • torque/trunk/readme.txt

    r2639345 r2658195  
    137137== Changelog ==
    138138
     139= Version 0.5.8 =
     140
     141* Added hook to rebuild the assets when a plugin is updated
     142* Added CLI command "torque rebuild"
     143
    139144= Version 0.5.7 =
    140145
  • torque/trunk/torque.php

    r2639355 r2658195  
    4949// uninstall
    5050\register_uninstall_hook(__FILE__, '\\hexydec\\torque\\install::uninstall');
     51
     52// rebuild files when a plugin is updated
     53\add_action('upgrader_process_complete', function () {
     54    \hexydec\torque\assets::rebuildAssets();
     55});
     56
     57// add rebuild command
     58if (\class_exists('WP_CLI')) {
     59    \WP_CLI::add_command('torque rebuild', '\\hexydec\\torque\\packages::rebuildAssets', [
     60        'shortdesc' => 'Rebuild the configured combined CSS and Javascript files'
     61    ]);
     62}
Note: See TracChangeset for help on using the changeset viewer.