PluginProbe
Boxzilla – WordPress Popup Builder / 3.1.11
Boxzilla – WordPress Popup Builder v3.1.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
boxzilla / src / class-box.php

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

246 lines 5.1 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 * @var int
11 */
12 public $ID;
13
14 /**
15 * @var array
16 */
17 public $options = array();
18
19 /**
20 * @var string
21 */
22 public $title = '';
23
24 /**
25 * @var string
26 */
27 protected $content = '';
28
29 /**
30 * @var bool
31 */
32 public $enabled = false;
33
34 /**
35 * @var WP_Post
36 */
37 private $post;
38
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 = get_post( $post );
47 }
48
49 // store ref to post
50 $this->post = $post;
51
52 // store ID in property for quick access
53 $this->ID = $post->ID;
54
55 // store title in property
56 $this->title = $post->post_title;
57
58 // store content in property
59 $this->content = $post->post_content;
60
61 // is this box enabled?
62 $this->enabled = ( $post->post_status === 'publish' );
63
64 // load and store options in property
65 $this->options = $this->load_options();
66 }
67
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(
104 'condition' => 'larger',
105 'value' => 0,
106 ),
107 'closable' => true,
108 );
109 $box = $this;
110
111 $options = get_post_meta( $this->ID, 'boxzilla_options', true );
112 $options = is_array( $options ) ? $options : array();
113
114 // merge options with default options
115 $options = array_replace_recursive( $defaults, $options );
116
117 // allow others to filter the final array of options
118 /**
119 * Filter the options for a given box
120 *
121 * @param array $options
122 * @param Box $box
123 */
124 $options = apply_filters( 'boxzilla_box_options', $options, $box );
125
126 return $options;
127 }
128
129 /**
130 * @return bool
131 */
132 public function is_enabled() {
133 return $this->enabled;
134 }
135
136 /**
137 * Get the options for this box
138 *
139 * @return array
140 */
141 public function get_options() {
142 return $this->options;
143 }
144
145 /**
146 * Get the close / hide icon for this box
147 *
148 * @return string
149 */
150 public function get_close_icon() {
151
152 $box = $this;
153 $html = '&times;';
154
155 /**
156 * Filters the HTML for the close icon.
157 *
158 * @param string $html
159 * @param Box $box
160 */
161 $close_icon = (string) apply_filters( 'boxzilla_box_close_icon', $html, $box );
162
163 return $close_icon;
164 }
165
166 /**
167 * Get the content of this box
168 *
169 * @return string
170 */
171 public function get_content() {
172 $content = $this->content;
173 $box = $this;
174
175 /**
176 * Filters the HTML for the box content
177 *
178 * @param string $content
179 * @param Box $box
180 */
181 $content = apply_filters( 'boxzilla_box_content', $content, $box );
182 return $content;
183 }
184
185 /**
186 * Get options object for JS script.
187 *
188 * @return array
189 */
190 public function get_client_options() {
191 $box = $this;
192
193 $trigger = false;
194 if( $box->options['trigger'] ) {
195 $trigger = array(
196 'method' => $this->options['trigger']
197 );
198
199 if( isset( $this->options[ 'trigger_' . $this->options['trigger'] ] ) ) {
200 $trigger['value'] = $this->options[ 'trigger_' . $this->options['trigger'] ];
201 }
202 }
203
204 // build screenWidthCondition object (or null)
205 $screen_width_condition = null;
206 if( $box->options['screen_size_condition']['value'] > 0 ) {
207 $screen_width_condition = array(
208 'condition' => $box->options['screen_size_condition']['condition'],
209 'value' => intval( $box->options['screen_size_condition']['value'] ),
210 );
211 }
212
213 $client_options = array(
214 'id' => $box->ID,
215 'icon' => $box->get_close_icon(),
216 'content' => $box->get_content(),
217 'css' => array_filter( $box->options['css'] ),
218 'trigger' => $trigger,
219 'animation' => $box->options['animation'],
220 'cookie' => array(
221 'triggered' => absint( $box->options['cookie']['triggered'] ),
222 'dismissed' => absint( $box->options['cookie']['dismissed'] ),
223 ),
224 'rehide' => (bool) $box->options['auto_hide'],
225 'position' => $box->options['css']['position'],
226 'screenWidthCondition' => $screen_width_condition,
227 'closable' => $box->options['closable'],
228 'post' => array(
229 'id' => $this->post->ID,
230 'title' => $this->post->post_title,
231 'slug' => $this->post->post_name,
232 ),
233 );
234
235 /**
236 * Filter the final options for the JS Boxzilla client.
237 *
238 * @param array $client_options
239 * @param Box $box
240 */
241 $client_options = apply_filters( 'boxzilla_box_client_options', $client_options, $box );
242
243 return $client_options;
244 }
245
246 }