| @@ -30,8 +30,13 @@ | ||
| 30 | 30 | * @var bool |
| 31 | 31 | */ |
| 32 | 32 | public $enabled = false; |
| 33 | 33 | |
| 34 | + /** | |
| 35 | + * @var WP_Post | |
| 36 | + */ | |
| 37 | + private $post; | |
| 38 | + | |
| 34 | 39 | /** |
| 35 | 40 | * @param WP_Post|int $post |
| 36 | 41 | */ |
| 37 | 42 | public function __construct( $post ) { |
| @@ -40,8 +45,11 @@ | ||
| 40 | 45 | if( ! $post instanceof WP_Post ) { |
| 41 | 46 | $post = get_post( $post ); |
| 42 | 47 | } |
| 43 | 48 | |
| 49 | + // store ref to post | |
| 50 | + $this->post = $post; | |
| 51 | + | |
| 44 | 52 | // store ID in property for quick access |
| 45 | 53 | $this->ID = $post->ID; |
| 46 | 54 | |
| 47 | 55 | // store title in property |
| @@ -91,10 +99,14 @@ | ||
| 91 | 99 | 'trigger_time_on_site' => 0, |
| 92 | 100 | 'trigger_time_on_page' => 0, |
| 93 | 101 | 'animation' => 'fade', |
| 94 | 102 | 'auto_hide' => 0, |
| 95 | - 'hide_on_screen_size' => '', | |
| 96 | - 'closable' => true, | |
| 103 | + 'screen_size_condition' => array( | |
| 104 | + 'condition' => 'larger', | |
| 105 | + 'value' => 0, | |
| 106 | + ), | |
| 107 | + 'closable' => 1, | |
| 108 | + 'show_close_icon' => 1, | |
| 97 | 109 | ); |
| 98 | 110 | $box = $this; |
| 99 | 111 | |
| 100 | 112 | $options = get_post_meta( $this->ID, 'boxzilla_options', true ); |
| @@ -99,8 +111,9 @@ | ||
| 99 | 111 | |
| 100 | 112 | $options = get_post_meta( $this->ID, 'boxzilla_options', true ); |
| 101 | 113 | $options = is_array( $options ) ? $options : array(); |
| 102 | 114 | |
| 115 | + | |
| 103 | 116 | // merge options with default options |
| 104 | 117 | $options = array_replace_recursive( $defaults, $options ); |
| 105 | 118 | |
| 106 | 119 | // allow others to filter the final array of options |
| @@ -137,8 +150,12 @@ | ||
| 137 | 150 | * @return string |
| 138 | 151 | */ |
| 139 | 152 | public function get_close_icon() { |
| 140 | 153 | |
| 154 | + if( ! $this->options['show_close_icon'] ) { | |
| 155 | + return ''; | |
| 156 | + } | |
| 157 | + | |
| 141 | 158 | $box = $this; |
| 142 | 159 | $html = '×'; |
| 143 | 160 | |
| 144 | 161 | /** |
| @@ -154,15 +171,27 @@ | ||
| 154 | 171 | |
| 155 | 172 | /** |
| 156 | 173 | * Get the content of this box |
| 157 | 174 | * |
| 158 | - * @return mixed|void | |
| 175 | + * @return string | |
| 159 | 176 | */ |
| 160 | 177 | public function get_content() { |
| 161 | 178 | $content = $this->content; |
| 162 | 179 | $box = $this; |
| 163 | 180 | |
| 164 | - /** | |
| 181 | + // replace boxzilla specific shortcodes | |
| 182 | + $close_link = sprintf('<a href="javascript:Boxzilla.dismiss(%d);">', $this->ID ); | |
| 183 | + | |
| 184 | + $replacements = array( | |
| 185 | + '[boxzilla_close]' => $close_link, // accept underscore and dash here for consistency with other shortcode | |
| 186 | + '[boxzilla-close]' => $close_link, | |
| 187 | + '[/boxzilla_close]' => '</a>', | |
| 188 | + '[/boxzilla-close]' => '</a>', | |
| 189 | + ); | |
| 190 | + | |
| 191 | + $content = str_replace( array_keys( $replacements ), array_values( $replacements ), $content ); | |
| 192 | + | |
| 193 | + /** | |
| 165 | 194 | * Filters the HTML for the box content |
| 166 | 195 | * |
| 167 | 196 | * @param string $content |
| 168 | 197 | * @param Box $box |
| @@ -170,37 +199,36 @@ | ||
| 170 | 199 | $content = apply_filters( 'boxzilla_box_content', $content, $box ); |
| 171 | 200 | return $content; |
| 172 | 201 | } |
| 173 | 202 | |
| 174 | - /** | |
| 175 | - * Get the minimum allowed screen size for this box | |
| 176 | - * | |
| 177 | - * @return int | |
| 178 | - */ | |
| 179 | - public function get_minimum_screen_size() { | |
| 180 | - | |
| 181 | - if( $this->options['hide_on_screen_size'] > 0 ) { | |
| 182 | - $minimum_screen_size = absint( $this->options['hide_on_screen_size'] ); | |
| 183 | - } else { | |
| 184 | - $minimum_screen_size = 0; | |
| 185 | - } | |
| 186 | - | |
| 187 | - return $minimum_screen_size; | |
| 188 | - } | |
| 189 | - | |
| 203 | + /** | |
| 204 | + * Get options object for JS script. | |
| 205 | + * | |
| 206 | + * @return array | |
| 207 | + */ | |
| 190 | 208 | public function get_client_options() { |
| 191 | 209 | $box = $this; |
| 192 | 210 | |
| 193 | 211 | $trigger = false; |
| 194 | 212 | if( $box->options['trigger'] ) { |
| 213 | + $trigger = array( | |
| 214 | + 'method' => $this->options['trigger'] | |
| 215 | + ); | |
| 195 | 216 | |
| 196 | - $trigger = array( 'method' => $this->options['trigger'] ); | |
| 197 | - | |
| 198 | 217 | if( isset( $this->options[ 'trigger_' . $this->options['trigger'] ] ) ) { |
| 199 | 218 | $trigger['value'] = $this->options[ 'trigger_' . $this->options['trigger'] ]; |
| 200 | 219 | } |
| 201 | 220 | } |
| 202 | 221 | |
| 222 | + // build screenWidthCondition object (or null) | |
| 223 | + $screen_width_condition = null; | |
| 224 | + if( $box->options['screen_size_condition']['value'] > 0 ) { | |
| 225 | + $screen_width_condition = array( | |
| 226 | + 'condition' => $box->options['screen_size_condition']['condition'], | |
| 227 | + 'value' => intval( $box->options['screen_size_condition']['value'] ), | |
| 228 | + ); | |
| 229 | + } | |
| 230 | + | |
| 203 | 231 | $client_options = array( |
| 204 | 232 | 'id' => $box->ID, |
| 205 | 233 | 'icon' => $box->get_close_icon(), |
| 206 | 234 | 'content' => $box->get_content(), |
| @@ -212,10 +240,15 @@ | ||
| 212 | 240 | 'dismissed' => absint( $box->options['cookie']['dismissed'] ), |
| 213 | 241 | ), |
| 214 | 242 | 'rehide' => (bool) $box->options['auto_hide'], |
| 215 | 243 | 'position' => $box->options['css']['position'], |
| 216 | - 'minimumScreenWidth' => $box->get_minimum_screen_size(), | |
| 217 | - 'closable' => $box->options['closable'], | |
| 244 | + 'screenWidthCondition' => $screen_width_condition, | |
| 245 | + 'closable' => !!$box->options['closable'], | |
| 246 | + 'post' => array( | |
| 247 | + 'id' => $this->post->ID, | |
| 248 | + 'title' => $this->post->post_title, | |
| 249 | + 'slug' => $this->post->post_name, | |
| 250 | + ), | |
| 218 | 251 | ); |
| 219 | 252 | |
| 220 | 253 | /** |
| 221 | 254 | * Filter the final options for the JS Boxzilla client. |