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