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