PluginProbe
JCH Optimize / trunk
JCH Optimize vtrunk
6.0.2 6.0.1 trunk 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.10.0 2.2.0 2.2.1 All 58 releases
jch-optimize / src / View / OptimizeImageHtml.php

OptimizeImageHtml.php in JCH Optimize trunk, at src/View/OptimizeImageHtml.php

99 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * JCH Optimize - Performs several front-end optimizations for fast downloads
5 *
6 * @package jchoptimize/wordpress-platform
7 * @author Samuel Marshall <samuel@jch-optimize.net>
8 * @copyright Copyright (c) 2025 Samuel Marshall / JCH Optimize
9 * @license GNU/GPLv3, or later. See LICENSE file
10 *
11 * If LICENSE file missing, see <http://www.gnu.org/licenses/>.
12 */
13
14 namespace JchOptimize\WordPress\View;
15
16 use _JchOptimizeVendor\V91\Joomla\Registry\Registry;
17 use _JchOptimizeVendor\V91\Joomla\Renderer\RendererInterface;
18 use JchOptimize\Core\Mvc\View;
19
20 use function __;
21 use function json_encode;
22 use function wp_add_inline_script;
23 use function wp_create_nonce;
24 use function wp_enqueue_script;
25 use function wp_enqueue_style;
26 use function wp_register_script;
27 use function wp_register_style;
28
29 use const JCH_PLUGIN_URL;
30
31 class OptimizeImageHtml extends View
32 {
33 public function __construct(private Registry $params, RendererInterface $renderer)
34 {
35 parent::__construct($renderer);
36 }
37
38 public function loadResources(): void
39 {
40 wp_register_style('jch-file-tree', JCH_PLUGIN_URL . 'media/filetree/jquery.filetree.css', [], JCH_VERSION);
41
42 wp_register_script(
43 'jch-file-tree',
44 JCH_PLUGIN_URL . 'media/filetree/jquery.filetree.js',
45 ['jquery', 'jch-platform-wordpress'],
46 JCH_VERSION,
47 true
48 );
49
50 $filetree_nonce = wp_create_nonce('jch_optimize_filetree');
51 $js = <<<JS
52 const jch_filetree_url_nonce = '{$filetree_nonce}';
53 JS;
54
55 wp_add_inline_script('jch-platform-wordpress', $js, 'before');
56
57 if (JCH_PRO) {
58 wp_register_script_module(
59 'jch-optimize-image',
60 JCH_PLUGIN_URL . 'media/core/js/optimize-image.js',
61 [],
62 JCH_VERSION
63 );
64 wp_enqueue_script_module('jch-optimize-image');
65 }
66
67 wp_enqueue_style('jch-file-tree');
68 wp_enqueue_script('jch-file-tree');
69
70 $params = json_encode([
71 'auth' => [
72 'dlid' => $this->params->get('pro_downloadid', ''),
73 'secret' => '11e603aa',
74 ],
75 'resize_mode' => $this->params->get('pro_api_resize_mode', '1') ? 'auto' : 'manual',
76 'webp' => (bool)$this->params->get('pro_next_gen_images', '1'),
77 'avif' => (bool)$this->params->get('gen_avif_images', '1'),
78 'lossy' => (bool)$this->params->get('lossy', '1'),
79 'save_metadata' => (bool)$this->params->get('save_metadata', '0'),
80 'quality' => $this->params->get('quality', '85'),
81 'cropgravity' => $this->params->get('cropgravity', []),
82 'responsive' => (bool)$this->params->get('pro_gen_responsive_images', '1')
83 ]);
84
85
86 $message = __('Please open a directory to optimize images.', 'jch-optimize');
87 $noProID = __('Please enter your Download ID on the Configurations tab.', 'jch-optimize');
88
89 $script = <<<JS
90 window.jchOptimizeImageData ={
91 message : '{$message}',
92 noproid: '{$noProID}',
93 params: JSON.parse('{$params}')
94 }
95 JS;
96 wp_add_inline_script('jch-platform-wordpress', $script, 'before');
97 }
98 }
99