/
class-model-thumbnail.php
202 lines (114 loc) · 4.8 KB
/
class-model-thumbnail.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
class CZR_thumbnail_model_class extends CZR_Model {
protected $post_id;
protected $media;
protected $image;
protected $lightbox_url;
protected $size;
protected $use_placeholder;
protected $use_attachment;
/**
* @override
*/
public function __construct( $model = array() ) {
/*
* the model->defaults by default are:
* 1) merged to the model array at instantiation time (see instance's method CZR_model.czr_fn_update )
* 2) they also can be merged (default behavior) to the passed args when updating a model while rendering its template
* 3) they can override the model properties when invoking instance's method CZR_model.czr_fn_reset_to_defaults
*/
$this->defaults = array(
'media' => null,
'image' => null,
'lighbox_url' => null,
'post_id' => null,
'size' => 'full',
'use_placeholder' => false,
'use_attachment' => true,
'visibility' => true,
'has_lightbox' => czr_fn_opt( 'tc_fancybox' ),
);
parent::__construct( $model );
}
public function czr_fn_setup( $args = array() ) {
$defaults = array (
'post_id' => null,
'size' => 'full',
'use_placeholder' => false,
'use_attachment' => true,
);
$args = wp_parse_args( $args, $defaults );
$args[ 'post_id' ] = $args[ 'post_id' ] ? $args[ 'post_id' ] : get_the_ID();
/* This will update the model object properties, merging the $model -> defaults too */
$this -> czr_fn_update( $args );
/* Set the media property */
$this -> czr_fn__set_raw_media();
/* Toggle visibility */
$this -> czr_fn_set_property( 'visibility', (bool) $this->czr_fn_get_raw_media() );
}
public function czr_fn_get_raw_media() {
return $this->media;
}
/*
* Fired just before the view is rendered
* @hook: pre_rendering_view_{$this -> id}, 9999
*/
/*
* Each time this model view is rendered setup the current thumbnail items
*/
function czr_fn_setup_late_properties() {
if ( is_null( $this->media ) ) {
$this -> czr_fn_setup( array(
'post_id' => $this->post_id,
'size' => $this->size,
'use_placeholder' => $this->use_placeholder,
'use_attachment' => $this->use_attachment,
'has_lightbox' => $this->has_lightbox,
) );
}
$this -> czr_fn__setup_the_thumbnail_item();
}
protected function czr_fn__set_raw_media() {
$this -> czr_fn_set_property( 'media', $this->czr_fn__get_post_thumbnail() );
}
protected function czr_fn__setup_the_thumbnail_item() {
$thumbnail_item = $this->czr_fn__get_the_thumbnail_item();
$this -> czr_fn_set_property( 'image', $thumbnail_item[ 'image' ] );
$this -> czr_fn_set_property( 'lightbox_url', $thumbnail_item[ 'lightbox_url' ] );
}
protected function czr_fn__get_the_thumbnail_item() {
$raw_media = $this -> media;
if ( empty( $raw_media ) )
return array();
$thumbnail_item = array(
'image' => $raw_media[ 'tc_thumb' ],
//lightbox
'lightbox_url' => $this->has_lightbox && array_key_exists( 'is_placeholder', $raw_media ) && $raw_media[ 'is_placeholder' ] ? '' : wp_get_attachment_url( $raw_media[ '_thumb_id' ] ), //full
);
return $thumbnail_item;
}
protected function czr_fn__get_post_thumbnail() {
$post_id = $this->post_id ? $this->post_id : get_the_ID();
//Get the Customizr thumbnail or the WordPress post thumbnail
if ( $this->use_attachment ) {
//model array
$post_thumbnail = czr_fn_get_thumbnail_model( array(
'requested_size' => $this->size,
'post_id' => $post_id,
'placeholder' => $this->use_placeholder
));
}
else {
//build array
$id = get_post_thumbnail_id( $post_id );
if ( $id ) {
$post_thumbnail[ '_thumb_id' ] = $id;
$post_thumbnail[ 'tc_thumb' ] = apply_filters( 'czr_thumb_html', get_the_post_thumbnail( $post_id, $this->size ) );
}
else {
$post_thumbnail = array();
}
}
return empty( $post_thumbnail ) ? false : $post_thumbnail;
}
}