PluginProbe
Boxzilla – WordPress Popup Builder / 3.2.14
Boxzilla – WordPress Popup Builder v3.2.14
3.4.11 3.4.10 3.4.9 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 trunk 3.0 3.0.1 3.0.2 3.0.3 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 All 72 releases
boxzilla / src / class-box.php

class-box.php in Boxzilla – WordPress Popup Builder 3.2.14, at src/class-box.php

270 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Boxzilla;
4
5 use WP_Post;
6
7 class Box
8 {
9
10 /**
11 * @var int
12 */
13 public $ID;
14
15 /**
16 * @var array
17 */
18 public $options = array();
19
20 /**
21 * @var string
22 */
23 public $title = '';
24
25 /**
26 * @var string
27 */
28 protected $content = '';
29
30 /**
31 * @var bool
32 */
33 public $enabled = false;
34
35 /**
36 * @var WP_Post
37 */
38 private $post;
39
40 /**
41 * @param WP_Post|int $post
42 */
43 public function __construct($post)
44 {
45
46 // fetch post if it hasn't been fetched yet
47 if (! $post instanceof WP_Post) {
48 $post = get_post($post);
49 }
50
51 // store ref to post
52 $this->post = $post;
53
54 // store ID in property for quick access
55 $this->ID = $post->ID;
56
57 // store title in property
58 $this->title = $post->post_title;
59
60 // store content in property
61 $this->content = $post->post_content;
62
63 // is this box enabled?
64 $this->enabled = ($post->post_status === 'publish');
65
66 // load and store options in property
67 $this->options = $this->load_options();
68 }
69
70 /**
71 * Get the options for this box.
72 **
73 * @return array Array of box options
74 */
75 protected function load_options()
76 {
77 $defaults = array(
78 'css' => array(
79 'background_color' => '',
80 'color' => '',
81 'width' => '',
82 'border_color' => '',
83 'border_width' => '',
84 'border_style' => '',
85 'position' => 'bottom-right',
86 ),
87 'rules' => array(
88 0 => array(
89 'condition' => '',
90 'value' => '',
91 ),
92 ),
93 'rules_comparision' => 'any',
94 'cookie' => array(
95 'triggered' => 0,
96 'dismissed' => 0,
97 ),
98 'trigger' => 'percentage',
99 'trigger_percentage' => 65,
100 'trigger_element' => '',
101 'trigger_time_on_site' => 0,
102 'trigger_time_on_page' => 0,
103 'animation' => 'fade',
104 'auto_hide' => 0,
105 'screen_size_condition' => array(
106 'condition' => 'larger',
107 'value' => 0,
108 ),
109 'closable' => 1,
110 'show_close_icon' => 1,
111 );
112 $box = $this;
113
114 $options = get_post_meta($this->ID, 'boxzilla_options', true);
115 $options = is_array($options) ? $options : array();
116
117
118 // merge options with default options
119 $options = array_replace_recursive($defaults, $options);
120
121 // allow others to filter the final array of options
122 /**
123 * Filter the options for a given box
124 *
125 * @param array $options
126 * @param Box $box
127 */
128 $options = apply_filters('boxzilla_box_options', $options, $box);
129
130 return $options;
131 }
132
133 /**
134 * @return bool
135 */
136 public function is_enabled()
137 {
138 return $this->enabled;
139 }
140
141 /**
142 * Get the options for this box
143 *
144 * @return array
145 */
146 public function get_options()
147 {
148 return $this->options;
149 }
150
151 /**
152 * Get the close / hide icon for this box
153 *
154 * @return string
155 */
156 public function get_close_icon()
157 {
158 if (! $this->options['show_close_icon']) {
159 return '';
160 }
161
162 $box = $this;
163 $html = '&times;';
164
165 /**
166 * Filters the HTML for the close icon.
167 *
168 * @param string $html
169 * @param Box $box
170 */
171 $close_icon = (string) apply_filters('boxzilla_box_close_icon', $html, $box);
172
173 return $close_icon;
174 }
175
176 /**
177 * Get the content of this box
178 *
179 * @return string
180 */
181 public function get_content()
182 {
183 $content = $this->content;
184 $box = $this;
185
186 // replace boxzilla specific shortcodes
187 $close_link = sprintf('<a href="javascript:Boxzilla.dismiss(%d);">', $this->ID);
188
189 $replacements = array(
190 '[boxzilla_close]' => $close_link, // accept underscore and dash here for consistency with other shortcode
191 '[boxzilla-close]' => $close_link,
192 '[/boxzilla_close]' => '</a>',
193 '[/boxzilla-close]' => '</a>',
194 );
195
196 $content = str_replace(array_keys($replacements), array_values($replacements), $content);
197
198 /**
199 * Filters the HTML for the box content
200 *
201 * @param string $content
202 * @param Box $box
203 */
204 $content = apply_filters('boxzilla_box_content', $content, $box);
205 return $content;
206 }
207
208 /**
209 * Get options object for JS script.
210 *
211 * @return array
212 */
213 public function get_client_options()
214 {
215 $box = $this;
216
217 $trigger = false;
218 if ($box->options['trigger']) {
219 $trigger = array(
220 'method' => $this->options['trigger']
221 );
222
223 if (isset($this->options[ 'trigger_' . $this->options['trigger'] ])) {
224 $trigger['value'] = $this->options[ 'trigger_' . $this->options['trigger'] ];
225 }
226 }
227
228 // build screenWidthCondition object (or null)
229 $screen_width_condition = null;
230 if ($box->options['screen_size_condition']['value'] > 0) {
231 $screen_width_condition = array(
232 'condition' => $box->options['screen_size_condition']['condition'],
233 'value' => intval($box->options['screen_size_condition']['value']),
234 );
235 }
236
237 $client_options = array(
238 'id' => $box->ID,
239 'icon' => $box->get_close_icon(),
240 'content' => '', // we grab this later from an HTML element
241 'css' => array_filter($box->options['css']),
242 'trigger' => $trigger,
243 'animation' => $box->options['animation'],
244 'cookie' => array(
245 'triggered' => absint($box->options['cookie']['triggered']),
246 'dismissed' => absint($box->options['cookie']['dismissed']),
247 ),
248 'rehide' => (bool) $box->options['auto_hide'],
249 'position' => $box->options['css']['position'],
250 'screenWidthCondition' => $screen_width_condition,
251 'closable' => !!$box->options['closable'],
252 'post' => array(
253 'id' => $this->post->ID,
254 'title' => $this->post->post_title,
255 'slug' => $this->post->post_name,
256 ),
257 );
258
259 /**
260 * Filter the final options for the JS Boxzilla client.
261 *
262 * @param array $client_options
263 * @param Box $box
264 */
265 $client_options = apply_filters('boxzilla_box_client_options', $client_options, $box);
266
267 return $client_options;
268 }
269 }
270