-
Notifications
You must be signed in to change notification settings - Fork 2
/
class.view.php
158 lines (131 loc) · 4.41 KB
/
class.view.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
<?php
namespace WPS;
class View {
public $post_id,
$field_prefix,
$img_width,
$img_height,
$render_args,
$helper,
$cmb_prefix = CMB2_PREFIX,
$field_defaults = [
'multi' => false,
'multi_val' => null,
'is_single' => true,
'placeholder' => false
];
private $_initialised_post_id;
public function __construct($post_id = null) {
$this->post_id = $post_id;
$this->_initialised_post_id = $post_id;
$this->helper = new ViewHelper(ViewHelpersLoader::init());
}
public function set_image_size($width, $height) {
$this->img_width = $width;
$this->img_height = $height;
}
public function field($field_name, array $field_args = null) {
$field_args = array_merge(
$this->field_defaults,
(!empty($field_args) ? $field_args : [])
);
$field_value = $this->empty_field_check(
$this->field_prefix . $field_name,
$field_args['multi'],
$field_args['multi_val'],
$field_args['is_single']
);
if(strpos($field_name, 'image') !== false && empty($field_value) &&
$field_args['placeholder'] === true) {
if(!empty($field_args['image'])) {
return $this->get_placeholder(
$field_args['image']['w'],
$field_args['image']['h']
);
} else {
return $this->get_placeholder($this->img_width, $this->img_height);
}
} else {
return $field_value;
}
}
public function get_featured_image($size = 'full') {
if(has_post_thumbnail($this->post_id)) {
$featured_image = wp_get_attachment_image_src(
get_post_thumbnail_id($this->post_id),
$size
);
return $featured_image[0];
}
if($this->field_defaults['placeholder']) {
return $this->generate_placeholder_from($size);
}
return false;
}
public function render($template_name, array $render_args = null) {
$this->render_args = $render_args;
$template_file = strpos($template_name, '-tpl.php') === false ?
$template_name . '-tpl.php' :
$template_name;
$render_path = trailingslashit(WPS_VIEWS_DIR) . $template_file;
if(file_exists($render_path)) {
include($render_path);
$this->restore_previous_post_id();
} else {
trigger_error("No template found for: $template_file ($template_name)");
}
}
public function template_exists($template) {
return !empty(locate_template($template)) ?
true :
false;
}
public function format_content($content) {
return apply_filters('the_content', $content);
}
public function set_new_post_id(int $new_post_id) {
$this->post_id = $new_post_id;
}
public function restore_previous_post_id() {
$this->post_id = $this->_initialised_post_id;
}
protected function get_cmb2_field($field, $is_single = true) {
return get_post_meta($this->post_id, $field, $is_single);
}
protected function empty_field_check($metabox_field, $multi = null, $multi_val = null, $is_single = true) {
if($multi === true) {
return !empty($this->get_cmb2_field(
$this->cmb_prefix . $metabox_field,
$is_single
)) ?
($this->get_cmb2_field(
$this->cmb_prefix . $metabox_field,
$is_single
) == $multi_val ? true : false) :
false;
} else {
return !empty($this->get_cmb2_field(
$this->cmb_prefix . $metabox_field,
$is_single
)) ?
$this->get_cmb2_field(
$this->cmb_prefix . $metabox_field,
$is_single
) :
'';
}
}
protected function get_placeholder($width, $height) {
return 'http://placehold.it/' . $width . 'x' . $height;
}
protected function get_post_object($post_id = null) {
return get_post(!empty($post_id) ? $post_id : $this->post_id);
}
protected function generate_placeholder_from($size) {
global $_wp_additional_image_sizes;
$placeholder_size = $size == 'full' ? 'post-thumbnail' : $size;
$width = $_wp_additional_image_sizes[$placeholder_size]['width'];
$height = $_wp_additional_image_sizes[$placeholder_size]['height'];
return $this->get_placeholder($width, $height);
}
}