-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-image.php
141 lines (119 loc) · 3.95 KB
/
class-image.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* Image module class
*
* @package Hogan
*/
declare( strict_types = 1 );
namespace Dekode\Hogan;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if ( ! class_exists( '\\Dekode\\Hogan\\Image' ) && class_exists( '\\Dekode\\Hogan\\Module' ) ) {
/**
* Image module class.
*
* @extends Modules base class.
*/
class Image extends Module {
/**
* Image
*
* @var array|null $image
*/
public $image = null;
/**
* Module constructor.
*/
public function __construct() {
$this->label = __( 'Image', 'hogan-image' );
$this->template = __DIR__ . '/assets/template.php';
parent::__construct();
}
/**
* Field definitions for module.
*
* @return array $fields Fields for this module
*/
public function get_fields() : array {
$choices = apply_filters( 'hogan/module/image/image_size/choices', [
'thumbnail' => _x( 'Small', 'Image Size', 'hogan-image' ),
'medium' => _x( 'Medium', 'Image Size', 'hogan-image' ),
'large' => _x( 'Large', 'Image Size', 'hogan-image' ),
] );
$constraints_defaults = [
'min_width' => '',
'min_height' => '',
'max_width' => '',
'max_height' => '',
'min_size' => '',
'max_size' => '',
'mime_types' => '',
];
// Merge $args from filter with $defaults.
$constraints_args = wp_parse_args( apply_filters( 'hogan/module/image/image_size/constraints', [] ), $constraints_defaults );
$fields = [];
array_push( $fields,
[
'type' => 'button_group',
'key' => $this->field_key . '_image_size',
'name' => 'image_size',
'label' => __( 'Image size', 'hogan-image' ),
'value' => is_array( $choices ) && ! empty( $choices ) ? reset( $choices ) : null,
'instructions' => __( 'Choose Image Size', 'hogan-image' ),
'choices' => $choices,
'layout' => 'horizontal',
'return_format' => 'value',
],
[
'type' => 'image',
'key' => $this->field_key . '_image_id',
'name' => 'image_id',
'label' => __( 'Add Image', 'hogan-image' ),
'required' => 1,
'return_format' => 'id',
'preview_size' => apply_filters( 'hogan/module/image/image_size/preview_size', 'medium' ),
'library' => apply_filters( 'hogan/module/image/image_size/library', 'all' ),
'min_width' => $constraints_args['min_width'],
'min_height' => $constraints_args['min_height'],
'max_width' => $constraints_args['max_width'],
'max_height' => $constraints_args['max_height'],
'min_size' => $constraints_args['min_size'],
'max_size' => $constraints_args['max_size'],
'mime_types' => $constraints_args['mime_types'],
]
);
hogan_append_caption_field( $fields, $this );
return $fields;
}
/**
* Map raw fields from acf to object variable.
*
* @param array $raw_content Content values.
* @param int $counter Module location in page layout.
* @return void
*/
public function load_args_from_layout_content( array $raw_content, int $counter = 0 ) {
if ( ! empty( $raw_content['image_id'] ) ) {
$image = wp_parse_args( apply_filters( 'hogan/module/image/image/args', [] ), [
'size' => $raw_content['image_size'],
'icon' => false,
'attr' => apply_filters_deprecated( 'hogan/module/image/attachment/attr', [ [] ], '1.1.0', 'hogan/module/image/image/args' ),
] );
$image['id'] = $raw_content['image_id'];
$this->image = $image;
// Add image size classname to wrapper.
$this->outer_wrapper_classnames = [ 'hogan-image-size-' . $image['size'] ];
}
parent::load_args_from_layout_content( $raw_content, $counter );
}
/**
* Validate module content before template is loaded.
*
* @return bool Whether validation of the module is successful / filled with content.
*/
public function validate_args() : bool {
return ! empty( $this->image );
}
}
}