PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.7
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.7
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / classes / class-imagify-views.php

class-imagify-views.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.7, at inc/classes/class-imagify-views.php

469 lines 14.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' );
3
4 /**
5 * Class that handles templates and menus.
6 *
7 * @since 1.7
8 * @author Grégory Viguier
9 */
10 class Imagify_Views {
11
12 /**
13 * Class version.
14 *
15 * @var string
16 * @since 1.7
17 */
18 const VERSION = '1.0';
19
20 /**
21 * Slug used for the settings page URL.
22 *
23 * @var string
24 * @since 1.7
25 * @access protected
26 */
27 protected $slug_settings;
28
29 /**
30 * Slug used for the bulk optimization page URL.
31 *
32 * @var string
33 * @since 1.7
34 * @access protected
35 */
36 protected $slug_bulk;
37
38 /**
39 * Slug used for the "custom folders" page URL.
40 *
41 * @var string
42 * @since 1.7
43 * @access protected
44 */
45 protected $slug_files;
46
47 /**
48 * Stores the "custom folders" files list instance.
49 *
50 * @var object Imagify_Files_List_Table
51 * @since 1.7
52 * @access protected
53 */
54 protected $list_table;
55
56 /**
57 * The single instance of the class.
58 *
59 * @var object
60 * @since 1.7
61 * @access protected
62 */
63 protected static $_instance;
64
65
66 /** ----------------------------------------------------------------------------------------- */
67 /** INSTANCE/INIT =========================================================================== */
68 /** ----------------------------------------------------------------------------------------- */
69
70 /**
71 * The constructor.
72 *
73 * @since 1.7
74 * @author Grégory Viguier
75 * @access protected
76 */
77 protected function __construct() {
78 $this->slug_settings = IMAGIFY_SLUG;
79 $this->slug_bulk = IMAGIFY_SLUG . '-bulk-optimization';
80 $this->slug_files = IMAGIFY_SLUG . '-files';
81 }
82
83 /**
84 * Get the main Instance.
85 *
86 * @since 1.7
87 * @author Grégory Viguier
88 * @access public
89 *
90 * @return object Main instance.
91 */
92 public static function get_instance() {
93 if ( ! isset( self::$_instance ) ) {
94 self::$_instance = new self();
95 }
96
97 return self::$_instance;
98 }
99
100 /**
101 * Launch the hooks.
102 *
103 * @since 1.7
104 * @author Grégory Viguier
105 * @access public
106 */
107 public function init() {
108 // Menu items.
109 add_action( 'admin_menu', array( $this, 'add_site_menus' ) );
110
111 if ( imagify_is_active_for_network() ) {
112 add_action( 'network_admin_menu', array( $this, 'add_network_menus' ) );
113 }
114
115 // Action links in plugins list.
116 $basename = plugin_basename( IMAGIFY_FILE );
117 add_filter( 'plugin_action_links_' . $basename, array( $this, 'plugin_action_links' ) );
118 add_filter( 'network_admin_plugin_action_links_' . $basename, array( $this, 'plugin_action_links' ) );
119
120 // Save the "per page" option value from the files list screen.
121 add_filter( 'set-screen-option', array( 'Imagify_Files_List_Table', 'save_screen_options' ), 10, 3 );
122 }
123
124
125 /** ----------------------------------------------------------------------------------------- */
126 /** MENU ITEMS ============================================================================== */
127 /** ----------------------------------------------------------------------------------------- */
128
129 /**
130 * Add sub-menus for all sites.
131 *
132 * @since 1.7
133 * @author Grégory Viguier
134 * @access public
135 */
136 public function add_site_menus() {
137 // Sub-menu item: bulk optimization.
138 add_media_page( __( 'Bulk Optimization', 'imagify' ), __( 'Bulk Optimization', 'imagify' ), imagify_get_capacity( 'bulk-optimize' ), $this->get_bulk_page_slug(), array( $this, 'display_bulk_page' ) );
139
140 if ( imagify_is_active_for_network() ) {
141 return;
142 }
143
144 /**
145 * Plugin is not network activated.
146 */
147 if ( imagify_can_optimize_custom_folders() ) {
148 // Sub-menu item: custom folders list.
149 $screen_id = add_media_page( __( 'Other Media optimized by Imagify', 'imagify' ), __( 'Other Media', 'imagify' ), imagify_get_capacity( 'optimize-file' ), $this->get_files_page_slug(), array( $this, 'display_files_list' ) );
150
151 if ( $screen_id ) {
152 // Load the data for this page.
153 add_action( 'load-' . $screen_id, array( $this, 'load_files_list' ) );
154 }
155 }
156
157 // Sub-menu item: settings.
158 add_options_page( 'Imagify', 'Imagify', imagify_get_capacity(), $this->get_settings_page_slug(), array( $this, 'display_settings_page' ) );
159 }
160
161 /**
162 * Add menu and sub-menus in the network admin when Imagify is network-activated.
163 *
164 * @since 1.7
165 * @author Grégory Viguier
166 * @access public
167 */
168 public function add_network_menus() {
169 global $submenu;
170
171 if ( ! imagify_can_optimize_custom_folders() ) {
172 // Main item: settings (edge case).
173 add_menu_page( 'Imagify', 'Imagify', imagify_get_capacity(), $this->get_settings_page_slug(), array( $this, 'display_settings_page' ) );
174 return;
175 }
176
177 // Main item: bulk optimization (custom folders).
178 add_menu_page( __( 'Bulk Optimization', 'imagify' ), 'Imagify', imagify_get_capacity( 'optimize-file' ), $this->get_bulk_page_slug(), array( $this, 'display_bulk_page' ) );
179
180 // Sub-menu item: custom folders list.
181 $screen_id = add_submenu_page( $this->get_bulk_page_slug(), __( 'Other Media optimized by Imagify', 'imagify' ), __( 'Other Media', 'imagify' ), imagify_get_capacity( 'optimize-file' ), $this->get_files_page_slug(), array( $this, 'display_files_list' ) );
182
183 // Sub-menu item: settings.
184 add_submenu_page( $this->get_bulk_page_slug(), 'Imagify', __( 'Settings', 'imagify' ), imagify_get_capacity(), $this->get_settings_page_slug(), array( $this, 'display_settings_page' ) );
185
186 // Change the sub-menu label.
187 if ( ! empty( $submenu[ $this->get_bulk_page_slug() ] ) ) {
188 $submenu[ $this->get_bulk_page_slug() ][0][0] = __( 'Bulk Optimization', 'imagify' ); // WPCS: override ok.
189 }
190
191 if ( $screen_id ) {
192 // On the "Other Media optimized by Imagify" page, load the data.
193 add_action( 'load-' . $screen_id, array( $this, 'load_files_list' ) );
194 }
195 }
196
197
198 /** ----------------------------------------------------------------------------------------- */
199 /** PLUGIN ACTION LINKS ===================================================================== */
200 /** ----------------------------------------------------------------------------------------- */
201
202 /**
203 * Add links to the plugin row in the plugins list.
204 *
205 * @since 1.7
206 * @author Grégory Viguier
207 * @access public
208 *
209 * @param array $actions An array of action links.
210 * @return array
211 */
212 public function plugin_action_links( $actions ) {
213 array_unshift( $actions, sprintf( '<a href="%s" target="_blank">%s</a>', esc_url( imagify_get_external_url( 'documentation' ) ), __( 'Documentation', 'imagify' ) ) );
214 array_unshift( $actions, sprintf( '<a href="%s">%s</a>', esc_url( get_imagify_admin_url( 'bulk-optimization' ) ), __( 'Bulk Optimization', 'imagify' ) ) );
215 array_unshift( $actions, sprintf( '<a href="%s">%s</a>', esc_url( get_imagify_admin_url() ), __( 'Settings', 'imagify' ) ) );
216 return $actions;
217 }
218
219
220 /** ----------------------------------------------------------------------------------------- */
221 /** MAIN PAGE TEMPLATES ===================================================================== */
222 /** ----------------------------------------------------------------------------------------- */
223
224 /**
225 * The main settings page.
226 *
227 * @since 1.7
228 * @author Grégory Viguier
229 * @access public
230 */
231 public function display_settings_page() {
232 $this->print_template( 'page-settings' );
233 }
234
235 /**
236 * The bulk optimization page.
237 *
238 * @since 1.7
239 * @author Grégory Viguier
240 * @access public
241 */
242 public function display_bulk_page() {
243 $data = array(
244 // Global chart.
245 'total_attachments' => 0,
246 'unoptimized_attachments' => 0,
247 'optimized_attachments' => 0,
248 'errors_attachments' => 0,
249 // Stats block.
250 'already_optimized_attachments' => 0,
251 'original_size' => 0,
252 'optimized_size' => 0,
253 'optimized_percent' => 0,
254 // Limits.
255 'unoptimized_attachment_limit' => 0,
256 // What to optimize.
257 'icon' => 'images-alt2',
258 'title' => __( 'Optimize your images', 'imagify' ),
259 'groups' => array(),
260 );
261
262 if ( imagify_is_screen( 'bulk' ) ) {
263 if ( ! is_network_admin() ) {
264 /**
265 * Library: in each site.
266 */
267 $total_saving_data = imagify_count_saving_data();
268
269 // Global chart.
270 $data['total_attachments'] += imagify_count_attachments();
271 $data['unoptimized_attachments'] += imagify_count_unoptimized_attachments();
272 $data['optimized_attachments'] += imagify_count_optimized_attachments();
273 $data['errors_attachments'] += imagify_count_error_attachments();
274 // Stats block.
275 $data['already_optimized_attachments'] += $total_saving_data['count'];
276 $data['original_size'] += $total_saving_data['original_size'];
277 $data['optimized_size'] += $total_saving_data['optimized_size'];
278 // Limits.
279 $data['unoptimized_attachment_limit'] += imagify_get_unoptimized_attachment_limit();
280 // Group.
281 $data['groups']['library'] = array(
282 /**
283 * The group_id corresponds to the file names like 'part-bulk-optimization-results-row-{$group_id}'.
284 * It is also used in get_imagify_localize_script_translations() and imagify_get_folder_type_data().
285 */
286 'group_id' => 'library',
287 'context' => 'wp',
288 'title' => __( 'Media Library', 'imagify' ),
289 /* translators: 1 is the opening of a link, 2 is the closing of this link. */
290 'footer' => sprintf( __( 'You can also re-optimize your images from your %1$sMedia Library%2$s screen.', 'imagify' ), '<a href="' . esc_url( admin_url( 'upload.php' ) ) . '">', '</a>' ),
291 );
292 }
293
294 if ( imagify_can_optimize_custom_folders() && ( imagify_is_active_for_network() && is_network_admin() || ! imagify_is_active_for_network() ) ) {
295 /**
296 * Custom folders: in network admin only if network activated, in each site otherwise.
297 */
298 // Global chart.
299 $data['total_attachments'] += Imagify_Files_Stats::count_all_files();
300 $data['unoptimized_attachments'] += Imagify_Files_Stats::count_no_status_files();
301 $data['optimized_attachments'] += Imagify_Files_Stats::count_optimized_files();
302 $data['errors_attachments'] += Imagify_Files_Stats::count_error_files();
303 // Stats block.
304 $data['already_optimized_attachments'] += Imagify_Files_Stats::count_success_files();
305 $data['original_size'] += Imagify_Files_Stats::get_original_size();
306 $data['optimized_size'] += Imagify_Files_Stats::get_optimized_size();
307
308 if ( ! Imagify_Folders_DB::get_instance()->has_items() ) {
309 // New Feature!
310 $data['no-custom-folders'] = true;
311 } elseif ( Imagify_Folders_DB::get_instance()->has_active_folders() ) {
312 // Group.
313 $data['groups']['custom-folders'] = array(
314 'group_id' => 'custom-folders',
315 'context' => 'File',
316 'title' => __( 'Custom folders', 'imagify' ),
317 /* translators: 1 is the opening of a link, 2 is the closing of this link. */
318 'footer' => sprintf( __( 'You can re-optimize your images more finely directly in the %1$simages management%2$s.', 'imagify' ), '<a href="' . esc_url( get_imagify_admin_url( 'files-list' ) ) . '">', '</a>' ),
319 );
320 }
321 }
322 }
323
324 /**
325 * Filter the data to use on the bulk optimization page.
326 *
327 * @since 1.7
328 * @author Grégory Viguier
329 *
330 * @param array $data The data to use.
331 */
332 $data = apply_filters( 'imagify_bulk_page_data', $data );
333
334 /**
335 * Percentages.
336 */
337 if ( $data['total_attachments'] && $data['optimized_attachments'] ) {
338 $data['optimized_attachments_percent'] = round( 100 * $data['optimized_attachments'] / $data['total_attachments'] );
339 } else {
340 $data['optimized_attachments_percent'] = 0;
341 }
342
343 if ( $data['original_size'] && $data['optimized_size'] ) {
344 $data['optimized_percent'] = ceil( 100 - ( 100 * $data['optimized_size'] / $data['original_size'] ) );
345 } else {
346 $data['optimized_percent'] = 0;
347 }
348
349 $this->print_template( 'page-bulk', $data );
350 }
351
352 /**
353 * The page displaying the "custom folders" files.
354 *
355 * @since 1.7
356 * @author Grégory Viguier
357 * @access public
358 */
359 public function display_files_list() {
360 $this->print_template( 'page-files-list' );
361 }
362
363 /**
364 * Initiate the "custom folders" list table data.
365 *
366 * @since 1.7
367 * @author Grégory Viguier
368 * @access public
369 */
370 public function load_files_list() {
371 // Instantiate the list.
372 $this->list_table = new Imagify_Files_List_Table( array(
373 'screen' => 'imagify-files',
374 ) );
375
376 // Query the Items.
377 $this->list_table->prepare_items();
378 }
379
380
381 /** ----------------------------------------------------------------------------------------- */
382 /** GETTERS ================================================================================= */
383 /** ----------------------------------------------------------------------------------------- */
384
385 /**
386 * Get the settings page slug.
387 *
388 * @since 1.7
389 * @author Grégory Viguier
390 * @access public
391 *
392 * @return string
393 */
394 public function get_settings_page_slug() {
395 return $this->slug_settings;
396 }
397
398 /**
399 * Get the bulk optimization page slug.
400 *
401 * @since 1.7
402 * @author Grégory Viguier
403 * @access public
404 *
405 * @return string
406 */
407 public function get_bulk_page_slug() {
408 return $this->slug_bulk;
409 }
410
411 /**
412 * Get the "custom folders" files page slug.
413 *
414 * @since 1.7
415 * @author Grégory Viguier
416 * @access public
417 *
418 * @return string
419 */
420 public function get_files_page_slug() {
421 return $this->slug_files;
422 }
423
424
425 /** ----------------------------------------------------------------------------------------- */
426 /** GENERIC TEMPLATE TOOLS ================================================================== */
427 /** ----------------------------------------------------------------------------------------- */
428
429 /**
430 * Get a template contents.
431 *
432 * @since 1.7
433 * @author Grégory Viguier
434 * @access public
435 *
436 * @param string $template The template name.
437 * @param mixed $data Some data to pass to the template.
438 * @return string|bool The page contents. False if the template doesn't exist.
439 */
440 public function get_template( $template, $data = array() ) {
441 $path = str_replace( '_', '-', $template );
442 $path = IMAGIFY_PATH . 'views/' . $template . '.php';
443
444 if ( ! imagify_get_filesystem()->exists( $path ) ) {
445 return false;
446 }
447
448 ob_start();
449 include $path;
450 $contents = ob_get_clean();
451
452 return trim( (string) $contents );
453 }
454
455 /**
456 * Print a template.
457 *
458 * @since 1.7
459 * @author Grégory Viguier
460 * @access public
461 *
462 * @param string $template The template name.
463 * @param mixed $data Some data to pass to the template.
464 */
465 public function print_template( $template, $data = array() ) {
466 echo $this->get_template( $template, $data );
467 }
468 }
469