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