Plugin Directory

Changeset 2711273


Ignore:
Timestamp:
04/18/2022 10:21:54 PM (4 years ago)
Author:
hexydec
Message:

Fixed issue where the plugin said it was only compatible with PHP 8.0+, whereas it still supports 7.4.
Updated dependencies.
Updated version to 0.6.1.

Location:
torque/trunk
Files:
2 added
10 edited

Legend:

Unmodified
Added
Removed
  • torque/trunk/packages/cssdoc/cssdoc.php

    r2693063 r2711273  
    457457
    458458    /**
    459      * Compile the property to a string
    460      *
    461      * @param array $options An array of compilation options
     459     * Compile the document to a string
     460     *
     461     * @param array $options An array indicating output options, this is merged with cssdoc::$output
    462462     * @return void
    463463     */
     
    468468
    469469    /**
    470      * Compile the document as an HTML string and save it to the specified location
    471      *
    472      * @param array $options An array indicating output options, this is merged with htmldoc::$output
    473      * @return string The compiled HTML
     470     * Compile the document and save it to the specified location
     471     *
     472     * @param string|null $file The file location to save the document to, or null to just return the compiled code
     473     * @param array $options An array indicating output options, this is merged with cssdoc::$output
     474     * @return string|bool The compiled CSS, or false if the file could not be saved
    474475     */
    475476    public function save(string $file = null, array $options = []) {
    476477        $css = $this->compile($options);
    477478
     479        // save file
     480        if ($file && \file_put_contents($file, $css) === false) {
     481            \trigger_error('File could not be written', E_USER_WARNING);
     482            return false;
     483        }
     484
    478485        // send back as string
    479         if (!$file) {
    480             return $css;
    481 
    482         // save file
    483         } elseif (\file_put_contents($file, $css) === false) {
    484             \trigger_error('File could not be written', E_USER_WARNING);
    485         } else {
    486             return true;
    487         }
    488         return false;
     486        return $css;
    489487    }
    490488
  • torque/trunk/packages/htmldoc/autoload.php

    r2592945 r2711273  
    1111        'hexydec\\html\\custom' => __DIR__.'/tokens/custom.php',
    1212        'hexydec\\html\\tag' => __DIR__.'/tokens/tag.php',
    13         'hexydec\\html\\text' => __DIR__.'/tokens/text.php'
     13        'hexydec\\html\\text' => __DIR__.'/tokens/text.php',
     14        'hexydec\\html\\selector' => __DIR__.'/helpers/selector.php'
    1415    ];
    1516    if (isset($classes[$class])) {
  • torque/trunk/packages/htmldoc/htmldoc.php

    r2693063 r2711273  
    44use \hexydec\tokens\tokenise;
    55
     6/**
     7 * @property array $config
     8 * @property-read int $length
     9 */
    610class htmldoc extends config implements \ArrayAccess, \Iterator {
    711
     
    8993     */
    9094    public function offsetSet($i, $value) : void {
    91         if (\is_null($i)) {
    92             $this->children[] = $value;
    93         } else {
    94             $this->children[$i] = $value;
    95         }
     95        $this->children[$i] = $value;
    9696    }
    9797
     
    119119     *
    120120     * @param string|integer $i The key to be accessed, can be a string or integer
    121      * @return mixed The requested value or null if the key doesn't exist
    122      */
    123     public function offsetGet(mixed $i) : mixed { // return reference so you can set it like an array
    124         return $this->children[$i] ?? null;
     121     * @return htmldoc|null The child node at the requested position or null if there is no child at the requested position
     122     */
     123    public function offsetGet(mixed $i) : ?htmldoc { // return reference so you can set it like an array
     124        if (isset($this->children[$i])) {
     125            $obj = new htmldoc($this->config);
     126            $obj->collection([$this->children[$i]]);
     127            return $obj;
     128        }
     129        return null;
    125130    }
    126131
     
    128133     * Retrieve the document node in the current position
    129134     *
    130      * @return tag|text|comment|doctype The child node at the current pointer position
    131      */
    132     public function current() : mixed {
    133         return $this->children[$this->pointer] ?? null;
     135     * @return htmldoc|null The child node at the current pointer position or null if there are no children
     136     */
     137    public function current() : ?htmldoc {
     138        if (isset($this->children[$this->pointer])) {
     139            $obj = new htmldoc($this->config);
     140            $obj->collection([$this->children[$this->pointer]]);
     141            return $obj;
     142        }
     143        return null;
    134144    }
    135145
     
    198208            $charset = null;
    199209            $meta = \stream_get_meta_data($handle);
    200             if (!empty($meta['wrapper_data'])) {
    201                 foreach ($meta['wrapper_data'] AS $item) {
    202                     if (\mb_stripos($item, 'Content-Type:') === 0 && ($value = \mb_stristr($item, 'charset=')) !== false) {
    203                         $charset = \mb_substr($value, 8);
    204                         break;
    205                     }
     210            foreach ($meta['wrapper_data'] ?? [] AS $item) {
     211                if (\mb_stripos($item, 'Content-Type:') === 0 && ($value = \mb_stristr($item, 'charset=')) !== false) {
     212                    $charset = \mb_substr($value, 8);
     213                    break;
    206214                }
    207215            }
     
    252260     */
    253261    protected function getCharsetFromHtml(string $html) : ?string {
    254         if (\preg_match('/<meta[^>]+charset[^>]+>/i', $html, $match)) {
    255             $obj = new htmldoc($this->config);
    256             if ($obj->load($match[0], \mb_internal_encoding())) {
    257 
    258                 // <meta charset="xxx" />
    259                 if (($charset = $obj->attr('charset')) !== null && $this->isEncodingValid($charset)) {
     262        $obj = new htmldoc($this->config);
     263        $pat = '/<meta[^>]+charset[^>]+>/i';
     264        if (\preg_match($pat, $html, $match) && $obj->load($match[0], \mb_internal_encoding())) {
     265
     266            // <meta charset="xxx" />
     267            if (($charset = $obj->attr('charset')) !== null && $this->isEncodingValid($charset)) {
     268                return $charset;
     269
     270            // <meta http-equiv="Content-Type" content="text/html; charset=xxx" />
     271            } elseif (($value = $obj->eq(0)->attr('content')) !== null && ($charset = \mb_stristr($value, 'charset=')) !== false) {
     272                $charset = \mb_substr($charset, 8);
     273                if ($this->isEncodingValid($charset)) {
    260274                    return $charset;
    261 
    262                 // <meta http-equiv="Content-Type" content="text/html; charset=xxx" />
    263                 } elseif (($value = $obj->eq(0)->attr('content')) !== null && ($charset = \mb_stristr($value, 'charset=')) !== false) {
    264                     $charset = \mb_substr($charset, 8);
    265                     if ($this->isEncodingValid($charset)) {
    266                         return $charset;
    267                     }
    268275                }
    269276            }
     
    309316
    310317    /**
    311      * Parses a CSS selector string
    312      *
    313      * @param string $selector The CSS selector string to parse
    314      * @return array|bool An array of selector components
    315      */
    316     protected function parseSelector(tokenise $tokens) {
    317         if (($token = $tokens->next()) !== null) {
    318             $selectors = $parts = [];
    319             $join = null;
    320             do {
    321                 switch ($token['type']) {
    322                     case 'id':
    323                         $parts[] = [
    324                             'id' => \mb_substr($token['value'], 1),
    325                             'join' => $join
    326                         ];
    327                         $join = null;
    328                         break;
    329 
    330                     case 'class':
    331                         $parts[] = [
    332                             'class' => \mb_substr($token['value'], 1),
    333                             'join' => $join
    334                         ];
    335                         $join = null;
    336                         break;
    337 
    338                     case 'string':
    339                         $parts[] = [
    340                             'tag' => $token['value'],
    341                             'join' => $join
    342                         ];
    343                         $join = null;
    344                         break;
    345 
    346                     case 'squareopen':
    347                         $item = ['join' => $join, 'sensitive' => true];
    348                         while (($token = $tokens->next()) !== null) {
    349                             if ($token['type'] === 'squareclose') {
    350                                 break;
    351                             } elseif (\in_array($token['type'], ['string', 'quotes'], true)) {
    352                                 if ($token['type'] === 'quotes') {
    353                                     $token['value'] = \stripslashes(\mb_substr($token['value'], 1, -1));
    354                                 }
    355                                 if (!isset($item['attribute'])) {
    356                                     $item['attribute'] = $token['value'];
    357                                 } elseif (!isset($item['value'])) {
    358                                     $item['value'] = $token['value'];
    359                                 } elseif ($token['value'] === 'i') {
    360                                     $item['sensitive'] = false;
    361                                 }
    362                             } elseif ($token['type'] === 'comparison') {
    363                                 $item['comparison'] = $token['value'];
    364                             }
    365                         }
    366                         $parts[] = $item;
    367                         $join = null;
    368                         break;
    369 
    370                     case 'pseudo':
    371                         $sub = null;
    372                         if (($bracket = $tokens->next()) !== null && $bracket['type'] === 'bracketopen') {
    373                             $sub = $this->parseSelector($tokens);
    374                         } elseif ($bracket) {
    375                             $tokens->prev();
    376                         }
    377                         $parts[] = [
    378                             'pseudo' => \mb_substr($token['value'], 1),
    379                             'sub' => $sub,
    380                             'join' => $join
    381                         ];
    382                         $join = null;
    383                         break;
    384 
    385                     case 'join':
    386                         $join = \trim($token['value']);
    387                         break;
    388 
    389                     case 'whitespace':
    390                         if ($parts) {
    391                             $join = ' ';
    392                         }
    393                         break;
    394 
    395                     case 'comma':
    396                         $selectors[] = $parts;
    397                         $parts = [];
    398                         break;
    399 
    400                     case 'bracketclose':
    401                         $selectors[] = $parts;
    402                         $parts = [];
    403                         break;
    404                 }
    405             } while (($token = $tokens->next()) !== null);
    406             if ($parts) {
    407                 $selectors[] = $parts;
    408             }
    409             return $selectors;
    410         }
    411         return false;
    412     }
    413 
    414     /**
    415318     * Caches the input values and records the number of occurences
    416319     *
     
    476379     */
    477380    public function find(string $selector) : htmldoc {
    478         $tokens = new tokenise(self::$selectors, \trim($selector));
     381        $obj = new selector();
    479382
    480383        // parse selector and find tags
    481384        $found = [];
    482         if (($parsed = $this->parseSelector($tokens)) !== false) {
     385        if (($tokens = $obj->get($selector)) !== false) {
    483386            foreach ($this->children AS $item) {
    484387                if (\get_class($item) === 'hexydec\\html\\tag') {
    485                     foreach ($parsed AS $value) {
     388                    foreach ($tokens AS $value) {
    486389                        if (($items = $item->find($value)) !== false) {
    487390                            $found = \array_merge($found, $items);
     
    629532
    630533        // sort classes by occurence, then by string
    631         if (\is_array($minify['attributes'])) {
    632 
    633             // sort attribute values by most frequent
    634             if ($minify['attributes']['sort'] && !empty($this->cache['attr'])) {
    635                 \arsort($this->cache['attr'], SORT_NUMERIC);
    636                 \arsort($this->cache['attrvalues'], SORT_NUMERIC);
    637                 $attr = [];
    638                 foreach ($this->cache['attrvalues'] AS $item => $occurences) {
    639                     if ($occurences > 5) {
    640                         $item = \mb_strstr($item, '=', true);
    641                         if (!\in_array($item, $attr, true)) {
    642                             $attr[] = $item;
    643                         }
    644                     } else {
    645                         break;
    646                     }
    647                 }
    648                 $minify['attributes']['sort'] = \array_unique(\array_merge($attr, \array_keys($this->cache['attr'])));
    649             }
     534        if (!empty($minify['attributes']['sort']) && !empty($this->cache['attr'])) {
     535            $minify['attributes']['sort'] = $this->sortAttributes($this->cache['attr'], $this->cache['attrvalues']);
    650536        }
    651537
     
    654540            $item->minify($minify);
    655541        }
     542    }
     543
     544    /**
     545     * Sort attributes in frequency order
     546     *
     547     * @param array $attr An array of attribute keys
     548     * @param array $values An array of attribute values
     549     * @return array An array of attributes ordered by frequency
     550     */
     551    protected function sortAttributes(array $attr, array $values) : array {
     552        \arsort($attr, SORT_NUMERIC);
     553        \arsort($values, SORT_NUMERIC);
     554        $items = [];
     555        foreach ($values AS $item => $occurences) {
     556            if ($occurences > 5) {
     557                $item = \mb_strstr($item, '=', true);
     558                if (!\in_array($item, $items, true)) {
     559                    $items[] = $item;
     560                }
     561            } else {
     562                break;
     563            }
     564        }
     565        return \array_unique(\array_merge($items, \array_keys($attr)));
    656566    }
    657567
     
    767677     * Compile the document as an HTML string and save it to the specified location
    768678     *
     679     * @param string|null $file The file location to save the document to, or null to just return the compiled code
    769680     * @param array $options An array indicating output options, this is merged with htmldoc::$output
    770      * @return string The compiled HTML
     681     * @return string|bool The compiled HTML, or false if the file could not be saved
    771682     */
    772683    public function save(string $file = null, array $options = []) {
     
    784695
    785696            // convert to target charset
    786             $html = \mb_convert_encoding($html, $options['charset']);
    787         }
    788 
    789         // send back as string
    790         if (!$file) {
    791             return $html;
     697            $html = (string) \mb_convert_encoding($html, $options['charset']);
     698        }
    792699
    793700        // save file
    794         } elseif (\file_put_contents($file, $html) === false) {
     701        if ($file && \file_put_contents($file, $html) === false) {
    795702            \trigger_error('File could not be written', E_USER_WARNING);
    796         } else {
    797             return true;
    798         }
    799         return false;
     703            return false;
     704        }
     705        return $html;
    800706    }
    801707
     
    814720            $str .= \chr($i);
    815721        }
    816         $str = \mb_convert_encoding($str, \mb_internal_encoding(), $charset);
     722        $str = (string) \mb_convert_encoding($str, \mb_internal_encoding(), $charset);
    817723
    818724        // build html entities conversion map
     
    826732
    827733        // convert entities
    828         $html = \mb_convert_encoding($html, 'HTML-ENTITIES');
     734        $html = (string) \mb_convert_encoding($html, 'HTML-ENTITIES');
    829735        return \str_replace(\array_values($replace), \array_keys($replace), $html);
    830736    }
  • torque/trunk/packages/htmldoc/tokens/comment.php

    r2693063 r2711273  
    4040     */
    4141    public function minify(array $minify) : void {
    42         if (!empty($minify['comments']['remove']) && (empty($minify['comments']['ie']) || (\mb_strpos($this->content, '[if ') !== 0 && $this->content !== '<![endif]'))) {
    43             $this->content = null;
     42        if (!empty($minify['comments']['remove']) && $this->content) {
     43            if (empty($minify['comments']['ie']) || (\mb_strpos($this->content, '[if ') !== 0 && $this->content !== '<![endif]')) {
     44                $this->content = null;
     45            }
    4446        }
    4547    }
  • torque/trunk/packages/htmldoc/tokens/custom.php

    r2693063 r2711273  
    2828     */
    2929    public function __construct(htmldoc $root, string $tagName = null) {
    30         $this->tagName = $tagName = \mb_strtolower($tagName);
     30        $this->tagName = $tagName = \mb_strtolower($tagName ?? '');
    3131        $this->config = $root->config['custom'][$tagName];
    3232    }
  • torque/trunk/packages/htmldoc/tokens/tag.php

    r2693063 r2711273  
    44use \hexydec\tokens\tokenise;
    55
     6/**
     7 * @property tag|null $parent
     8 * @property htmldoc $root
     9 * @property array $config
     10 * @property tag|null $parent
     11 * @property array $parenttags
     12 * @property string|null $tagName
     13 * @property array $attributes
     14 * @property string|null $singleton
     15 * @property array $children
     16 */
    617class tag implements token {
    718
     
    1728
    1829    /**
    19      * @var tag The parent tag object
     30     * @var tag|null The parent tag object
    2031     */
    2132    protected ?tag $parent = null;
     
    2435     * @var array Cache for the list of parent tags
    2536     */
    26     protected ?array $parenttags = null;
     37    protected array $parenttags = [];
    2738
    2839    /**
     
    3748
    3849    /**
    39      * @var string If the tag is a singleton, this defines the closing string
     50     * @var string|null If the tag is a singleton, this defines the closing string
    4051     */
    4152    protected ?string $singleton = null;
     
    194205     */
    195206    protected function getParentTagNames() : array {
    196         if (!$this->parenttags) {
     207        if (empty($this->parenttags)) {
    197208            $this->parenttags = $this->parent ? $this->parent->getParentTagNames() : [];
    198             if ($this->tagName) {
     209            if ($this->tagName !== null) {
    199210                $this->parenttags[] = \mb_strtolower($this->tagName);
    200211            }
     
    312323        // insert the nodes
    313324        if ($index !== null) {
    314             $this->chidren = \array_splice($this->children, $index, 0, $clones);
     325            \array_splice($this->children, $index, 0, $clones);
    315326        }
    316327    }
     
    336347     */
    337348    protected function getIndex() : ?int {
    338         foreach ($this->parent->children() AS $key => $item) {
    339             if ($item === $this) {
    340                 return $key;
     349        if ($this->parent) {
     350            foreach ($this->parent->children() AS $key => $item) {
     351                if ($item === $this) {
     352                    return $key;
     353                }
    341354            }
    342355        }
     
    351364     */
    352365    public function before(array $nodes) : void {
    353         if (($index = $this->getIndex()) !== null) {
     366        if ($this->parent !== null && ($index = $this->getIndex()) !== null) {
    354367            $this->parent->append($nodes, $index);
    355368        }
     
    363376     */
    364377    public function after(array $nodes) : void {
    365         if (($index = $this->getIndex()) !== null) {
     378        if ($this->parent !== null && ($index = $this->getIndex()) !== null) {
    366379            $this->parent->append($nodes, $index + 1);
    367380        }
     
    395408        $config = $this->config;
    396409        $attr = $config['attributes'];
    397         if ($minify['lowercase']) {
     410        if ($minify['lowercase'] && $this->tagName) {
    398411            $this->tagName = \mb_strtolower($this->tagName);
    399412        }
     
    405418        $tag = $this->tagName;
    406419        $attributes = $this->attributes;
     420        $host = null;
    407421        foreach ($attributes AS $key => $value) {
    408422
     
    434448                // remove host for own domain
    435449                if ($minify['urls']['host'] && isset($_SERVER['HTTP_HOST'])) {
    436                     $host ??= ['//'.$_SERVER['HTTP_HOST'], $scheme.$_SERVER['HTTP_HOST']];
     450                    $host = $host ?? ['//'.$_SERVER['HTTP_HOST'], $scheme.$_SERVER['HTTP_HOST']];
    437451                    foreach ($host AS $item) {
    438452
     
    542556
    543557        // work out whether to omit the closing tag
    544         if ($minify['close'] && \in_array($tag, $config['elements']['closeoptional']) && ($this->parent->tagName === null || !\in_array($this->parent->tagName, $config['elements']['inline'], true))) {
     558        if ($minify['close'] && $this->parent !== null && \in_array($tag, $config['elements']['closeoptional']) && ($this->parent->tagName === null || !\in_array($this->parent->tagName, $config['elements']['inline'], true))) {
    545559            $children = $this->parent->toArray();
    546560            $next = false;
     
    571585
    572586            // if last tag, remove closing tag
    573             if (!$children || $next) {
     587            if (empty($children) || $next) {
    574588                $this->close = false;
    575589            }
     
    731745                    // match first-child
    732746                    case 'first-child':
    733                         $children = $this->parent->children();
     747                        $children = $this->parent !== null ? $this->parent->children() : [];
    734748                        if (!isset($children[0]) || $this !== $children[0]) {
    735749                            $match = false;
     
    740754                    // match last child
    741755                    case 'last-child':
    742                         $children = $this->parent->children();
     756                        $children = $this->parent !== null ? $this->parent->children() : [];
    743757                        if (($last = \end($children)) === false || $this !== $last) {
    744758                            $match = false;
     
    779793     * @param string $key The key of the attribute whos value you wish to retrieve or update
    780794     * @param string $value The value of the attribute to update
    781      * @return string The value of the attrbute or NULL if the attribute does not exist
     795     * @return string|null The value of the attrbute or NULL if the attribute does not exist
    782796     */
    783797    public function attr(string $key, ?string $value = null) : ?string {
     
    788802
    789803        // get the value
    790         } elseif (\array_key_exists($key, $this->attributes)) {
    791             return $this->attributes[$key] === null ? true : $this->attributes[$key];
     804        } else {
     805            return $this->attributes[$key] ?? null;
    792806        }
    793807        return null;
  • torque/trunk/packages/htmldoc/tokens/text.php

    r2693063 r2711273  
    1212
    1313    /**
    14      * @var tag The parent tag object
     14     * @var tag|null The parent tag object
    1515     */
    16     protected tag $parent;
     16    protected ?tag $parent = null;
    1717
    1818    /**
     
    2727     * @param tag $parent The parent tag object
    2828     */
    29     public function __construct(htmldoc $root, tag $parent = null) {
     29    public function __construct(htmldoc $root, ?tag $parent = null) {
    3030        $this->root = $root;
    3131        $this->parent = $parent;
  • torque/trunk/packages/jslite/jslite.php

    r2693063 r2711273  
    185185
    186186    /**
    187      * Compile the document as an HTML string
    188      *
    189      * @return string The compiled Javascript
    190      */
    191     public function compile() : string {
     187     * Compile the document to a string
     188     *
     189     * @param array $options An array indicating output options
     190     * @return void
     191     */
     192    public function compile(array $options = []) : string {
    192193        $js = '';
    193194        foreach ($this->expressions AS $item) {
    194             $js .= $item->compile();
     195            $js .= $item->compile($options);
    195196        }
    196197        return $js;
    197198    }
     199
     200    /**
     201     * Compile the document and save it to the specified location
     202     *
     203     * @param string|null $file The file location to save the document to, or null to just return the compiled code
     204     * @param array $options An array indicating output options
     205     * @return string|bool The compiled Javascript, or false if the file could not be saved
     206     */
     207    public function save(string $file = null, array $options = []) {
     208        $js = $this->compile($options);
     209
     210        // save file
     211        if ($file && \file_put_contents($file, $js) === false) {
     212            \trigger_error('File could not be written', E_USER_WARNING);
     213            return false;
     214        }
     215
     216        // send back as string
     217        return $js;
     218    }
    198219}
  • torque/trunk/readme.txt

    r2693063 r2711273  
    44Requires at least: 5.9
    55Tested up to: 5.9
    6 Requires PHP: 8.0
    7 Stable tag: 0.6.0
     6Requires PHP: 7.4
     7Stable tag: 0.6.1
    88License: GPLv3
    99License URI: https://www.gnu.org/licenses/gpl-3.0.en.html
     
    137137== Changelog ==
    138138
     139= Version 0.6.1 =
     140
     141* Fixed issue where the plugin said it was only compatible with PHP 8.0+, whereas it still supports 7.4
     142* Updated dependencies
     143
    139144= Version 0.6.0 =
    140145
  • torque/trunk/torque.php

    r2693063 r2711273  
    1111Description:    Make your Wordpress website noticably faster by optimising how it is delivered. Analyse your website's performance and security, minify and combine your assets, and configure an array of performance and security settings quickly and easily with this comprehensive plugin. Achieves the best compression of any minification plugin.
    1212Version:        0.6.0
    13 Requires PHP:   8.0
     13Requires PHP:   7.4
    1414Author:         Hexydec
    1515Author URI:     https://github.com/hexydec/
Note: See TracChangeset for help on using the changeset viewer.