| 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 |
$defaults = array( |
| 75 |
'css' => array( |
| 76 |
'background_color' => '', |
| 77 |
'color' => '', |
| 78 |
'width' => '', |
| 79 |
'border_color' => '', |
| 80 |
'border_width' => '', |
| 81 |
'border_style' => '', |
| 82 |
'position' => 'bottom-right', |
| 83 |
), |
| 84 |
'rules' => array( |
| 85 |
0 => array( |
| 86 |
'condition' => '', |
| 87 |
'value' => '', |
| 88 |
), |
| 89 |
), |
| 90 |
'rules_comparision' => 'any', |
| 91 |
'cookie' => array( |
| 92 |
'triggered' => 0, |
| 93 |
'dismissed' => 0, |
| 94 |
), |
| 95 |
'trigger' => 'percentage', |
| 96 |
'trigger_percentage' => 65, |
| 97 |
'trigger_element' => '', |
| 98 |
'trigger_time_on_site' => 0, |
| 99 |
'trigger_time_on_page' => 0, |
| 100 |
'animation' => 'fade', |
| 101 |
'auto_hide' => 0, |
| 102 |
'screen_size_condition' => array( |
| 103 |
'condition' => 'larger', |
| 104 |
'value' => 0, |
| 105 |
), |
| 106 |
'closable' => 1, |
| 107 |
'show_close_icon' => 1, |
| 108 |
); |
| 109 |
$box = $this; |
| 110 |
|
| 111 |
$options = get_post_meta( $this->ID, 'boxzilla_options', true ); |
| 112 |
$options = is_array( $options ) ? $options : array(); |
| 113 |
|
| 114 |
|
| 115 |
// merge options with default options |
| 116 |
$options = array_replace_recursive( $defaults, $options ); |
| 117 |
|
| 118 |
// allow others to filter the final array of options |
| 119 |
/** |
| 120 |
* Filter the options for a given box |
| 121 |
* |
| 122 |
* @param array $options |
| 123 |
* @param Box $box |
| 124 |
*/ |
| 125 |
$options = apply_filters( 'boxzilla_box_options', $options, $box ); |
| 126 |
|
| 127 |
return $options; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* @return bool |
| 132 |
*/ |
| 133 |
public function is_enabled() { |
| 134 |
return $this->enabled; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Get the options for this box |
| 139 |
* |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
public function get_options() { |
| 143 |
return $this->options; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Get the close / hide icon for this box |
| 148 |
* |
| 149 |
* @return string |
| 150 |
*/ |
| 151 |
public function get_close_icon() { |
| 152 |
|
| 153 |
if( ! $this->options['show_close_icon'] ) { |
| 154 |
return ''; |
| 155 |
} |
| 156 |
|
| 157 |
$box = $this; |
| 158 |
$html = '×'; |
| 159 |
|
| 160 |
/** |
| 161 |
* Filters the HTML for the close icon. |
| 162 |
* |
| 163 |
* @param string $html |
| 164 |
* @param Box $box |
| 165 |
*/ |
| 166 |
$close_icon = (string) apply_filters( 'boxzilla_box_close_icon', $html, $box ); |
| 167 |
|
| 168 |
return $close_icon; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Get the content of this box |
| 173 |
* |
| 174 |
* @return string |
| 175 |
*/ |
| 176 |
public function get_content() { |
| 177 |
$content = $this->content; |
| 178 |
$box = $this; |
| 179 |
|
| 180 |
// replace boxzilla specific shortcodes |
| 181 |
$close_link = sprintf('<a href="javascript:Boxzilla.dismiss(%d);">', $this->ID ); |
| 182 |
|
| 183 |
$replacements = array( |
| 184 |
'[boxzilla_close]' => $close_link, // accept underscore and dash here for consistency with other shortcode |
| 185 |
'[boxzilla-close]' => $close_link, |
| 186 |
'[/boxzilla_close]' => '</a>', |
| 187 |
'[/boxzilla-close]' => '</a>', |
| 188 |
); |
| 189 |
|
| 190 |
$content = str_replace( array_keys( $replacements ), array_values( $replacements ), $content ); |
| 191 |
|
| 192 |
/** |
| 193 |
* Filters the HTML for the box content |
| 194 |
* |
| 195 |
* @param string $content |
| 196 |
* @param Box $box |
| 197 |
*/ |
| 198 |
$content = apply_filters( 'boxzilla_box_content', $content, $box ); |
| 199 |
return $content; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Get options object for JS script. |
| 204 |
* |
| 205 |
* @return array |
| 206 |
*/ |
| 207 |
public function get_client_options() { |
| 208 |
$box = $this; |
| 209 |
|
| 210 |
$trigger = false; |
| 211 |
if( $box->options['trigger'] ) { |
| 212 |
$trigger = array( |
| 213 |
'method' => $this->options['trigger'] |
| 214 |
); |
| 215 |
|
| 216 |
if( isset( $this->options[ 'trigger_' . $this->options['trigger'] ] ) ) { |
| 217 |
$trigger['value'] = $this->options[ 'trigger_' . $this->options['trigger'] ]; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
// build screenWidthCondition object (or null) |
| 222 |
$screen_width_condition = null; |
| 223 |
if( $box->options['screen_size_condition']['value'] > 0 ) { |
| 224 |
$screen_width_condition = array( |
| 225 |
'condition' => $box->options['screen_size_condition']['condition'], |
| 226 |
'value' => intval( $box->options['screen_size_condition']['value'] ), |
| 227 |
); |
| 228 |
} |
| 229 |
|
| 230 |
$client_options = array( |
| 231 |
'id' => $box->ID, |
| 232 |
'icon' => $box->get_close_icon(), |
| 233 |
'content' => '', // we grab this later from an HTML element |
| 234 |
'css' => array_filter( $box->options['css'] ), |
| 235 |
'trigger' => $trigger, |
| 236 |
'animation' => $box->options['animation'], |
| 237 |
'cookie' => array( |
| 238 |
'triggered' => absint( $box->options['cookie']['triggered'] ), |
| 239 |
'dismissed' => absint( $box->options['cookie']['dismissed'] ), |
| 240 |
), |
| 241 |
'rehide' => (bool) $box->options['auto_hide'], |
| 242 |
'position' => $box->options['css']['position'], |
| 243 |
'screenWidthCondition' => $screen_width_condition, |
| 244 |
'closable' => !!$box->options['closable'], |
| 245 |
'post' => array( |
| 246 |
'id' => $this->post->ID, |
| 247 |
'title' => $this->post->post_title, |
| 248 |
'slug' => $this->post->post_name, |
| 249 |
), |
| 250 |
); |
| 251 |
|
| 252 |
/** |
| 253 |
* Filter the final options for the JS Boxzilla client. |
| 254 |
* |
| 255 |
* @param array $client_options |
| 256 |
* @param Box $box |
| 257 |
*/ |
| 258 |
$client_options = apply_filters( 'boxzilla_box_client_options', $client_options, $box ); |
| 259 |
|
| 260 |
return $client_options; |
| 261 |
} |
| 262 |
|
| 263 |
} |
| 264 |
|