| 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 |
* @param WP_Post|int $post |
| 36 |
*/ |
| 37 |
public function __construct( $post ) { |
| 38 |
|
| 39 |
// fetch post if it hasn't been fetched yet |
| 40 |
if( ! $post instanceof WP_Post ) { |
| 41 |
$post = get_post( $post ); |
| 42 |
} |
| 43 |
|
| 44 |
// store ID in property for quick access |
| 45 |
$this->ID = $post->ID; |
| 46 |
|
| 47 |
// store title in property |
| 48 |
$this->title = $post->post_title; |
| 49 |
|
| 50 |
// store content in property |
| 51 |
$this->content = $post->post_content; |
| 52 |
|
| 53 |
// is this box enabled? |
| 54 |
$this->enabled = ( $post->post_status === 'publish' ); |
| 55 |
|
| 56 |
// load and store options in property |
| 57 |
$this->options = $this->load_options(); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get the options for this box. |
| 62 |
** |
| 63 |
* @return array Array of box options |
| 64 |
*/ |
| 65 |
protected function load_options() { |
| 66 |
|
| 67 |
static $defaults = array( |
| 68 |
'css' => array( |
| 69 |
'background_color' => '', |
| 70 |
'color' => '', |
| 71 |
'width' => '', |
| 72 |
'border_color' => '', |
| 73 |
'border_width' => '', |
| 74 |
'border_style' => '', |
| 75 |
'position' => 'bottom-right', |
| 76 |
), |
| 77 |
'rules' => array( |
| 78 |
0 => array('condition' => '', 'value' => '') |
| 79 |
), |
| 80 |
'rules_comparision' => 'any', |
| 81 |
'cookie' => 0, |
| 82 |
'trigger' => 'percentage', |
| 83 |
'trigger_percentage' => 65, |
| 84 |
'trigger_element' => '', |
| 85 |
'trigger_time_on_site' => 0, |
| 86 |
'trigger_time_on_page' => 0, |
| 87 |
'animation' => 'fade', |
| 88 |
'auto_hide' => 0, |
| 89 |
'hide_on_screen_size' => '', |
| 90 |
'closable' => true, |
| 91 |
); |
| 92 |
$box = $this; |
| 93 |
|
| 94 |
$options = get_post_meta( $this->ID, 'boxzilla_options', true ); |
| 95 |
$options = is_array( $options ) ? $options : array(); |
| 96 |
|
| 97 |
// merge options with default options |
| 98 |
$options = array_replace_recursive( $defaults, $options ); |
| 99 |
|
| 100 |
// allow others to filter the final array of options |
| 101 |
/** |
| 102 |
* Filter the options for a given box |
| 103 |
* |
| 104 |
* @param array $options |
| 105 |
* @param Box $box |
| 106 |
*/ |
| 107 |
$options = apply_filters( 'boxzilla_box_options', $options, $box ); |
| 108 |
|
| 109 |
return $options; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @return bool |
| 114 |
*/ |
| 115 |
public function is_enabled() { |
| 116 |
return $this->enabled; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Get the options for this box |
| 121 |
* |
| 122 |
* @return array |
| 123 |
*/ |
| 124 |
public function get_options() { |
| 125 |
return $this->options; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Get the close / hide icon for this box |
| 130 |
* |
| 131 |
* @return string |
| 132 |
*/ |
| 133 |
public function get_close_icon() { |
| 134 |
|
| 135 |
$box = $this; |
| 136 |
$html = '×'; |
| 137 |
|
| 138 |
/** |
| 139 |
* Filters the HTML for the close icon. |
| 140 |
* |
| 141 |
* @param string $html |
| 142 |
* @param Box $box |
| 143 |
*/ |
| 144 |
$close_icon = (string) apply_filters( 'boxzilla_box_close_icon', $html, $box ); |
| 145 |
|
| 146 |
return $close_icon; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get the content of this box |
| 151 |
* |
| 152 |
* @return mixed|void |
| 153 |
*/ |
| 154 |
public function get_content() { |
| 155 |
$content = $this->content; |
| 156 |
$box = $this; |
| 157 |
|
| 158 |
/** |
| 159 |
* Filters the HTML for the box content |
| 160 |
* |
| 161 |
* @param string $content |
| 162 |
* @param Box $box |
| 163 |
*/ |
| 164 |
$content = apply_filters( 'boxzilla_box_content', $content, $box ); |
| 165 |
return $content; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Get the minimum allowed screen size for this box |
| 170 |
* |
| 171 |
* @return int |
| 172 |
*/ |
| 173 |
public function get_minimum_screen_size() { |
| 174 |
|
| 175 |
if( $this->options['hide_on_screen_size'] > 0 ) { |
| 176 |
$minimum_screen_size = absint( $this->options['hide_on_screen_size'] ); |
| 177 |
} else { |
| 178 |
$minimum_screen_size = 0; |
| 179 |
} |
| 180 |
|
| 181 |
return $minimum_screen_size; |
| 182 |
} |
| 183 |
|
| 184 |
public function get_client_options() { |
| 185 |
$box = $this; |
| 186 |
|
| 187 |
$trigger = false; |
| 188 |
if( $box->options['trigger'] ) { |
| 189 |
|
| 190 |
$trigger = array( 'method' => $this->options['trigger'] ); |
| 191 |
|
| 192 |
if( isset( $this->options[ 'trigger_' . $this->options['trigger'] ] ) ) { |
| 193 |
$trigger['value'] = $this->options[ 'trigger_' . $this->options['trigger'] ]; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
$client_options = array( |
| 198 |
'id' => $box->ID, |
| 199 |
'icon' => $box->get_close_icon(), |
| 200 |
'content' => $box->get_content(), |
| 201 |
'css' => array_filter( $box->options['css'] ), |
| 202 |
'trigger' => $trigger, |
| 203 |
'animation' => $box->options['animation'], |
| 204 |
'cookieTime' => absint( $box->options['cookie'] ), |
| 205 |
'rehide' => (bool) $box->options['auto_hide'], |
| 206 |
'position' => $box->options['css']['position'], |
| 207 |
'minimumScreenWidth' => $box->get_minimum_screen_size(), |
| 208 |
'closable' => $box->options['closable'], |
| 209 |
); |
| 210 |
|
| 211 |
/** |
| 212 |
* Filter the final options for the JS Boxzilla client. |
| 213 |
* |
| 214 |
* @param array $client_options |
| 215 |
* @param Box $box |
| 216 |
*/ |
| 217 |
$client_options = apply_filters( 'boxzilla_box_client_options', $client_options, $box ); |
| 218 |
|
| 219 |
return $client_options; |
| 220 |
} |
| 221 |
|
| 222 |
} |