| @@ -5,8 +5,9 @@ | ||
| 5 | 5 | use WP_Post; |
| 6 | 6 | |
| 7 | 7 | class Box { |
| 8 | 8 | |
| 9 | + | |
| 9 | 10 | /** |
| 10 | 11 | * @var int |
| 11 | 12 | */ |
| 12 | 13 | public $ID; |
| @@ -31,17 +32,25 @@ | ||
| 31 | 32 | */ |
| 32 | 33 | public $enabled = false; |
| 33 | 34 | |
| 34 | 35 | /** |
| 36 | + * @var WP_Post | |
| 37 | + */ | |
| 38 | + private $post; | |
| 39 | + | |
| 40 | + /** | |
| 35 | 41 | * @param WP_Post|int $post |
| 36 | 42 | */ |
| 37 | 43 | public function __construct( $post ) { |
| 38 | 44 | |
| 39 | 45 | // fetch post if it hasn't been fetched yet |
| 40 | - if( ! $post instanceof WP_Post ) { | |
| 46 | + if ( ! $post instanceof WP_Post ) { | |
| 41 | 47 | $post = get_post( $post ); |
| 42 | 48 | } |
| 43 | 49 | |
| 50 | + // store ref to post | |
| 51 | + $this->post = $post; | |
| 52 | + | |
| 44 | 53 | // store ID in property for quick access |
| 45 | 54 | $this->ID = $post->ID; |
| 46 | 55 | |
| 47 | 56 | // store title in property |
| @@ -50,9 +59,9 @@ | ||
| 50 | 59 | // store content in property |
| 51 | 60 | $this->content = $post->post_content; |
| 52 | 61 | |
| 53 | 62 | // is this box enabled? |
| 54 | - $this->enabled = ( $post->post_status === 'publish' ); | |
| 63 | + $this->enabled = $post->post_status === 'publish'; | |
| 55 | 64 | |
| 56 | 65 | // load and store options in property |
| 57 | 66 | $this->options = $this->load_options(); |
| 58 | 67 | } |
| @@ -62,35 +71,44 @@ | ||
| 62 | 71 | ** |
| 63 | 72 | * @return array Array of box options |
| 64 | 73 | */ |
| 65 | 74 | protected function load_options() { |
| 66 | - | |
| 67 | - static $defaults = array( | |
| 68 | - 'css' => array( | |
| 75 | + $defaults = array( | |
| 76 | + 'css' => array( | |
| 69 | 77 | 'background_color' => '', |
| 70 | - 'color' => '', | |
| 71 | - 'width' => '', | |
| 72 | - 'border_color' => '', | |
| 73 | - 'border_width' => '', | |
| 74 | - 'border_style' => '', | |
| 75 | - 'position' => 'bottom-right', | |
| 78 | + 'color' => '', | |
| 79 | + 'width' => '', | |
| 80 | + 'border_color' => '', | |
| 81 | + 'border_width' => '', | |
| 82 | + 'border_style' => '', | |
| 83 | + 'position' => 'bottom-right', | |
| 76 | 84 | ), |
| 77 | - 'rules' => array( | |
| 78 | - 0 => array('condition' => '', 'value' => '') | |
| 85 | + 'rules' => array( | |
| 86 | + 0 => array( | |
| 87 | + 'condition' => '', | |
| 88 | + 'value' => '', | |
| 89 | + ), | |
| 79 | 90 | ), |
| 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 | + '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' => 1, | |
| 108 | + 'show_close_icon' => 1, | |
| 91 | 109 | ); |
| 92 | - $box = $this; | |
| 110 | + $box = $this; | |
| 93 | 111 | |
| 94 | 112 | $options = get_post_meta( $this->ID, 'boxzilla_options', true ); |
| 95 | 113 | $options = is_array( $options ) ? $options : array(); |
| 96 | 114 | |
| @@ -130,10 +148,13 @@ | ||
| 130 | 148 | * |
| 131 | 149 | * @return string |
| 132 | 150 | */ |
| 133 | 151 | public function get_close_icon() { |
| 152 | + if ( ! $this->options['show_close_icon'] ) { | |
| 153 | + return ''; | |
| 154 | + } | |
| 134 | 155 | |
| 135 | - $box = $this; | |
| 156 | + $box = $this; | |
| 136 | 157 | $html = '×'; |
| 137 | 158 | |
| 138 | 159 | /** |
| 139 | 160 | * Filters the HTML for the close icon. |
| @@ -148,14 +169,26 @@ | ||
| 148 | 169 | |
| 149 | 170 | /** |
| 150 | 171 | * Get the content of this box |
| 151 | 172 | * |
| 152 | - * @return mixed|void | |
| 173 | + * @return string | |
| 153 | 174 | */ |
| 154 | 175 | public function get_content() { |
| 155 | 176 | $content = $this->content; |
| 156 | - $box = $this; | |
| 177 | + $box = $this; | |
| 157 | 178 | |
| 179 | + // replace boxzilla specific shortcodes | |
| 180 | + $close_link = "<a href=\"javascript:Boxzilla.dismiss({$this->ID});\">"; | |
| 181 | + | |
| 182 | + $replacements = array( | |
| 183 | + '[boxzilla_close]' => $close_link, // accept underscore and dash here for consistency with other shortcode | |
| 184 | + '[boxzilla-close]' => $close_link, | |
| 185 | + '[/boxzilla_close]' => '</a>', | |
| 186 | + '[/boxzilla-close]' => '</a>', | |
| 187 | + ); | |
| 188 | + | |
| 189 | + $content = str_replace( array_keys( $replacements ), array_values( $replacements ), $content ); | |
| 190 | + | |
| 158 | 191 | /** |
| 159 | 192 | * Filters the HTML for the box content |
| 160 | 193 | * |
| 161 | 194 | * @param string $content |
| @@ -165,48 +198,56 @@ | ||
| 165 | 198 | return $content; |
| 166 | 199 | } |
| 167 | 200 | |
| 168 | 201 | /** |
| 169 | - * Get the minimum allowed screen size for this box | |
| 202 | + * Get options object for JS script. | |
| 170 | 203 | * |
| 171 | - * @return int | |
| 204 | + * @return array | |
| 172 | 205 | */ |
| 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 | 206 | public function get_client_options() { |
| 185 | 207 | $box = $this; |
| 186 | 208 | |
| 187 | 209 | $trigger = false; |
| 188 | - if( $box->options['trigger'] ) { | |
| 210 | + if ( $box->options['trigger'] ) { | |
| 211 | + $trigger = array( | |
| 212 | + 'method' => $this->options['trigger'], | |
| 213 | + ); | |
| 189 | 214 | |
| 190 | - $trigger = array( 'method' => $this->options['trigger'] ); | |
| 191 | - | |
| 192 | - if( isset( $this->options[ 'trigger_' . $this->options['trigger'] ] ) ) { | |
| 215 | + if ( isset( $this->options[ 'trigger_' . $this->options['trigger'] ] ) ) { | |
| 193 | 216 | $trigger['value'] = $this->options[ 'trigger_' . $this->options['trigger'] ]; |
| 194 | 217 | } |
| 195 | 218 | } |
| 196 | 219 | |
| 220 | + // build screenWidthCondition object (or null) | |
| 221 | + $screen_width_condition = null; | |
| 222 | + if ( $box->options['screen_size_condition']['value'] > 0 ) { | |
| 223 | + $screen_width_condition = array( | |
| 224 | + 'condition' => $box->options['screen_size_condition']['condition'], | |
| 225 | + 'value' => intval( $box->options['screen_size_condition']['value'] ), | |
| 226 | + ); | |
| 227 | + } | |
| 228 | + | |
| 229 | + $post = $this->post; | |
| 197 | 230 | $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'], | |
| 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' => $post->ID, | |
| 247 | + 'title' => $post->post_title, | |
| 248 | + 'slug' => $post->post_name, | |
| 249 | + ), | |
| 209 | 250 | ); |
| 210 | 251 | |
| 211 | 252 | /** |
| 212 | 253 | * Filter the final options for the JS Boxzilla client. |
| @@ -217,6 +258,5 @@ | ||
| 217 | 258 | $client_options = apply_filters( 'boxzilla_box_client_options', $client_options, $box ); |
| 218 | 259 | |
| 219 | 260 | return $client_options; |
| 220 | 261 | } |
| 221 | - | |
| 222 | -} | |
| 262 | +} | |