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