PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.0
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.0
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 / classes / Context / WP.php

WP.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.0, at classes/Context/WP.php

135 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Imagify\Context;
3
4 use Imagify\Traits\InstanceGetterTrait;
5
6 /**
7 * Context class used for the WP media library.
8 *
9 * @since 1.9
10 * @author Grégory Viguier
11 */
12 final class WP extends AbstractContext {
13 use InstanceGetterTrait;
14
15 /**
16 * Context "short name".
17 *
18 * @var string
19 * @since 1.9
20 * @author Grégory Viguier
21 */
22 protected $context = 'wp';
23
24 /**
25 * Images max width for this context. This is used when resizing.
26 *
27 * @var int
28 * @since 1.9.8
29 * @author Grégory Viguier
30 */
31 protected $resizing_threshold = 0;
32
33 /**
34 * Get the thumbnail sizes for this context, except the full size.
35 *
36 * @since 1.9
37 * @author Grégory Viguier
38 *
39 * @return array {
40 * Data for the currently registered thumbnail sizes.
41 * Size names are used as array keys.
42 *
43 * @type int $width The image width.
44 * @type int $height The image height.
45 * @type bool $crop True to crop, false to resize.
46 * @type string $name The size name.
47 * }
48 */
49 public function get_thumbnail_sizes() {
50 $this->thumbnail_sizes = get_imagify_thumbnail_sizes();
51
52 return $this->thumbnail_sizes;
53 }
54
55 /**
56 * Get images max width for this context. This is used when resizing.
57 * 0 means to not resize.
58 *
59 * @since 1.9.8
60 * @author Grégory Viguier
61 *
62 * @return int
63 */
64 public function get_resizing_threshold() {
65 if ( get_imagify_option( 'resize_larger' ) ) {
66 $this->resizing_threshold = max( 0, get_imagify_option( 'resize_larger_w' ) );
67 }
68
69 return $this->resizing_threshold;
70 }
71
72 /**
73 * Tell if the optimization process is allowed to backup in this context.
74 *
75 * @since 1.9
76 * @author Grégory Viguier
77 *
78 * @return bool
79 */
80 public function can_backup() {
81 $this->can_backup = get_imagify_option( 'backup' );
82
83 return $this->can_backup;
84 }
85
86 /**
87 * Get user capacity to operate Imagify in this context.
88 *
89 * @since 1.9
90 * @author Grégory Viguier
91 *
92 * @param string $describer Capacity describer. Possible values are like 'manage', 'bulk-optimize', 'manual-optimize', 'auto-optimize'.
93 * @return string
94 */
95 public function get_capacity( $describer ) {
96 static $edit_attachment_cap;
97
98 switch ( $describer ) {
99 case 'manage':
100 $capacity = imagify_is_active_for_network() ? 'manage_network_options' : 'manage_options';
101 break;
102
103 case 'bulk-optimize':
104 $capacity = 'manage_options';
105 break;
106
107 case 'optimize':
108 case 'restore':
109 // This is a generic capacity: don't use it unless you have no other choices!
110 if ( ! isset( $edit_attachment_cap ) ) {
111 $edit_attachment_cap = get_post_type_object( 'attachment' );
112 $edit_attachment_cap = $edit_attachment_cap ? $edit_attachment_cap->cap->edit_posts : 'edit_posts';
113 }
114
115 $capacity = $edit_attachment_cap;
116 break;
117
118 case 'manual-optimize':
119 case 'manual-restore':
120 // Must be used with an Attachment ID.
121 $capacity = 'edit_post';
122 break;
123
124 case 'auto-optimize':
125 $capacity = 'upload_files';
126 break;
127
128 default:
129 $capacity = $describer;
130 }
131
132 return $this->filter_capacity( $capacity, $describer );
133 }
134 }
135