-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-parallax-image.php
153 lines (133 loc) · 4.53 KB
/
class-parallax-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
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Parallax 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\\Parallax_Image' ) && class_exists( '\\Dekode\\Hogan\\Module' ) ) {
/**
* Parallax Image module class.
*
* @extends Modules base class.
*/
class Parallax_Image extends Module {
/**
* Image
*
* @var array|null $image
*/
public $image = null;
/**
* Parallax options
*
* @var array|null $options
*/
public $options = null;
/**
* Module constructor.
*/
public function __construct() {
$this->label = __( 'Parallax Image', 'hogan-parallax-image' );
$this->template = __DIR__ . '/assets/template.php';
parent::__construct();
}
/**
* Enqueue module assets
*/
public function enqueue_assets() {
$_debug = defined( 'SCRIPT_DEBUG' ) && true === SCRIPT_DEBUG;
$_version = $_debug ? time() : HOGAN_PARALLAX_IMAGE_VERSION;
wp_enqueue_script( 'hogan-parallax-image-plugin', plugins_url( '/assets/jquery.imageScroll.min.js', __FILE__ ), [ 'jquery' ], $_version, true );
wp_enqueue_script( 'hogan-parallax-image-effect', plugins_url( '/assets/parallax.js', __FILE__ ), [ 'hogan-parallax-image-plugin' ], $_version, true );
}
/**
* Field definitions for module.
*
* @return array $fields Fields for this module
*/
public function get_fields() : array {
$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/parallax_image/image_size/constraints', [] ), $constraints_defaults );
$fields = [
[
'type' => 'image',
'key' => $this->field_key . '_image_id',
'name' => 'image_id',
'label' => __( 'Add Image', 'hogan-parallax-image' ),
'required' => 1,
'return_format' => 'id',
'wrapper' => [
'width' => '50',
],
'preview_size' => apply_filters( 'hogan/module/parallax_image/image/preview_size', 'medium' ),
'library' => apply_filters( 'hogan/module/parallax_image/image/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'],
],
[
'type' => 'range',
'key' => $this->field_key . '_cover_ratio',
'name' => 'cover_ratio',
'label' => __( 'Cover Ratio', 'hogan-parallax-image' ),
'instructions' => __( 'Select how many percent of the screen height the image should cover', 'hogan-parallax-image' ),
'wrapper' => [
'width' => '50',
],
'default_value' => 50,
'min' => 30,
'max' => 100,
'step' => 10,
],
];
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 ) {
$image = wp_get_attachment_image_src( $raw_content['image_id'], apply_filters( 'hogan/module/parallax_image/image_size', 'full' ) );
if ( ! empty( $image ) ) {
$keys = [ 'url', 'width', 'height', 'is_intermediate' ];
$this->image = array_combine( $keys, array_values( $image ) );
$mobile_image = wp_get_attachment_image_src( $raw_content['image_id'], apply_filters( 'hogan/module/parallax_image/mobile_image_size', 'large' ) );
$this->image['mobile_url'] = ! empty( $mobile_image ) ? $mobile_image[0] : $this->image['url'];
$this->options = [
'cover_ratio' => $raw_content['cover_ratio'] / 100,
];
}
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 );
}
}
}