| 1 |
<?php |
| 2 |
|
| 3 |
namespace ModalWindow\Maker; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
class Script { |
| 8 |
private array $script; |
| 9 |
/** |
| 10 |
* @var mixed |
| 11 |
*/ |
| 12 |
private $param; |
| 13 |
/** |
| 14 |
* @var mixed |
| 15 |
*/ |
| 16 |
private $id; |
| 17 |
|
| 18 |
public function __construct( $id, $param ) { |
| 19 |
$this->id = $id; |
| 20 |
$this->param = $param; |
| 21 |
$this->script = []; |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
public function init(): array { |
| 26 |
$this->script['overlay'] = ! empty( $this->param['include_overlay'] ); |
| 27 |
$this->script['blockPage'] = isset( $this->param['modal_position'] ) && $this->param['modal_position'] === 'fixed'; |
| 28 |
$this->script['action'] = [ |
| 29 |
$this->param['modal_show'], |
| 30 |
absint( $this->param['modal_timer'] ), |
| 31 |
]; |
| 32 |
$this->script['closeAction'] = [ |
| 33 |
! empty( $this->param['close_button_overlay'] ), |
| 34 |
! empty( $this->param['close_button_esc'] ), |
| 35 |
]; |
| 36 |
$mobile_unit = ( $this->param['mobile_width_par'] !== 'px' ) ? '%' : 'px'; |
| 37 |
$this->script['mobile'] = [ |
| 38 |
absint( $this->param['screen_size'] ), |
| 39 |
absint( $this->param['mobile_width'] ), |
| 40 |
$mobile_unit, |
| 41 |
]; |
| 42 |
$this->script['triggers'] = [ |
| 43 |
'wow-modal-id-' . absint( $this->id ), |
| 44 |
'wow-modal-close-' . absint( $this->id ), |
| 45 |
'wow-button-close' . absint( $this->id ), |
| 46 |
]; |
| 47 |
|
| 48 |
|
| 49 |
$this->animation(); |
| 50 |
$this->scrolled(); |
| 51 |
$this->screenMax(); |
| 52 |
$this->screenMin(); |
| 53 |
$this->cookies(); |
| 54 |
$this->modal_style(); |
| 55 |
|
| 56 |
return $this->script; |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
private function animation(): void { |
| 61 |
$this->script['animation'] = [ |
| 62 |
$this->param['window_animate'] ?? 'no', |
| 63 |
isset($this->param['speed_window']) ? absint($this->param['speed_window']) : 400, |
| 64 |
$this->param['window_animate_out'] ?? 'no', |
| 65 |
isset($this->param['speed_window_out']) ? absint($this->param['speed_window_out']) : 400, |
| 66 |
]; |
| 67 |
} |
| 68 |
|
| 69 |
private function scrolled(): void { |
| 70 |
if ( $this->param['modal_show'] === 'scroll' ) { |
| 71 |
$this->script['scrolled'] = [ |
| 72 |
$this->param['reach_window'], |
| 73 |
$this->param['reach_window_unit'], |
| 74 |
]; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
private function screenMax(): void { |
| 80 |
if ( ! empty( $this->param['include_more_screen'] ) ) { |
| 81 |
$this->script['screenMax'] = [ |
| 82 |
true, |
| 83 |
absint( $this->param['screen_more'] ), |
| 84 |
]; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
private function screenMin(): void { |
| 89 |
if ( ! empty( $this->param['include_mobile'] ) ) { |
| 90 |
$this->script['screenMin'] = [ |
| 91 |
true, |
| 92 |
absint( $this->param['screen'] ), |
| 93 |
]; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
private function cookies(): void { |
| 98 |
if ( $this->param['use_cookies'] === 'yes' ) { |
| 99 |
$this->script['cookie'] = [ |
| 100 |
true, |
| 101 |
absint( $this->param['modal_cookies'] ), |
| 102 |
'wow-modal-id-' . $this->id, |
| 103 |
]; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
private function modal_style(): void { |
| 109 |
|
| 110 |
$general = $this->general_style(); |
| 111 |
$location = $this->location(); |
| 112 |
$title = $this->title(); |
| 113 |
$close_btn = $this->close_btn(); |
| 114 |
$float_btn = $this->float_btn(); |
| 115 |
|
| 116 |
$args = array_merge( $general, $location, $title, $close_btn, $float_btn ); |
| 117 |
|
| 118 |
$esc_args = array_map( 'esc_attr', $args ); |
| 119 |
|
| 120 |
$this->script['style'] = $esc_args; |
| 121 |
} |
| 122 |
|
| 123 |
private function general_style(): array { |
| 124 |
$param = $this->param; |
| 125 |
$args = [ |
| 126 |
'--mw-zindex' => $param['modal_zindex'] ?? '999999', |
| 127 |
'--mw-position' => $param['modal_position'] ?? 'fixed', |
| 128 |
'--mw-radius' => ( $param['border_radius'] ?? '5' ) . 'px', |
| 129 |
'--mw-padding' => ( $param['modal_padding'] ?? '10' ) . 'px', |
| 130 |
'--mw-font-size' => ( $param['content_size'] ?? '16' ) . 'px', |
| 131 |
'--mw-font-family' => $param['content_font'] ?? 'inherit', |
| 132 |
'--mw-bg-color' => $param['bg_color'] ?? '#ffffff', |
| 133 |
'--mw-overlay' => $param['overlay_color'] ?? 'rgba(0,0,0,.7)', |
| 134 |
'--mw-scrollbar-width' => $param['scrollbar_width'] ?? 'thin', |
| 135 |
'--mw-scrollbar-color' => $param['scrollbar_color'] ?? '#4F4F4F', |
| 136 |
'--mw-scrollbar-track' => $param['scrollbar_background'] ?? 'rgba(255,255,255, 0)', |
| 137 |
]; |
| 138 |
|
| 139 |
$scrollbar = $param['scrollbar_width'] ?? 'thin'; |
| 140 |
if ( $scrollbar === 'thin' ) { |
| 141 |
$args['--mw-scrollbar-thin'] = '6px'; |
| 142 |
} |
| 143 |
if ( $scrollbar === 'auto' ) { |
| 144 |
$args['--mw-scrollbar-thin'] = '8px'; |
| 145 |
} |
| 146 |
if ( $scrollbar === 'none' ) { |
| 147 |
$args['--mw-scrollbar-thin'] = '0'; |
| 148 |
} |
| 149 |
|
| 150 |
$width = $param['modal_width'] ?? '662'; |
| 151 |
$width_unit = $param['modal_width_par'] ?? 'px'; |
| 152 |
$width_unit = ( $width_unit === 'pr' ) ? '%' : 'px'; |
| 153 |
$args['--mw-width'] = $width . $width_unit; |
| 154 |
|
| 155 |
$height = $param['modal_height'] ?? '350'; |
| 156 |
$height_unit = $param['modal_height_par'] ?? 'auto'; |
| 157 |
$height_unit = ( $height_unit === 'pr' ) ? '%' : $height_unit; |
| 158 |
$args['--mw-height'] = ( $height_unit === 'auto' ) ? 'auto' : $height . $height_unit; |
| 159 |
|
| 160 |
if ( ! empty( $param['modal_background_img'] ) ) { |
| 161 |
$args['--mw-bg-img'] = 'url(' . esc_url( $param['modal_background_img'] ) . ')'; |
| 162 |
} |
| 163 |
|
| 164 |
if ( isset($param['shadow']) && $param['shadow'] !== 'none' ) { |
| 165 |
$inset = ( $param['shadow'] === 'inset' ) ? 'inset ' : ''; |
| 166 |
$offset_x = ( $param['shadow_h_offset'] ?? '0' ) . 'px'; |
| 167 |
$offset_y = ( $param['shadow_v_offset'] ?? '0' ) . 'px'; |
| 168 |
$blur = ( $param['shadow_blur'] ?? '0' ) . 'px'; |
| 169 |
$spread = ( $param['shadow_spread'] ?? '0' ) . 'px'; |
| 170 |
$shadow_color = $param['shadow_color'] ?? '#ffffff'; |
| 171 |
|
| 172 |
$args['--mw-shadow'] = "$inset$offset_x $offset_y $blur $spread $shadow_color"; |
| 173 |
} |
| 174 |
|
| 175 |
if ( $param['border_style'] !== 'none' ) { |
| 176 |
$border_width = ( $param['border_width'] ?? '0' ) . 'px'; |
| 177 |
$border_style = $param['border_style'] ?? 'solid'; |
| 178 |
$border_color = $param['border_color'] ?? '#ffffff'; |
| 179 |
|
| 180 |
$args['--mw-border'] = "$border_width $border_style $border_color"; |
| 181 |
} |
| 182 |
|
| 183 |
return $args; |
| 184 |
} |
| 185 |
|
| 186 |
private function location(): array { |
| 187 |
$param = $this->param; |
| 188 |
$args = []; |
| 189 |
|
| 190 |
if ( ! empty( $param['include_modal_top'] ) ) { |
| 191 |
$modal_top_unit = ( $param['modal_top_unit'] ?? '' ) === '%' ? 'vh' : 'px'; |
| 192 |
$args['--mw-inset-top'] = ( $param['modal_top'] ?? '0' ) . $modal_top_unit; |
| 193 |
} |
| 194 |
|
| 195 |
if ( ! empty( $param['include_modal_right'] ) ) { |
| 196 |
$modal_right_unit = ( $param['modal_right_unit'] ?? '' ) === '%' ? '%' : 'px'; |
| 197 |
$args['--mw-inset-right'] = ( $param['modal_right'] ?? '0' ) . $modal_right_unit; |
| 198 |
} |
| 199 |
|
| 200 |
if ( ! empty( $param['include_modal_bottom'] ) ) { |
| 201 |
$modal_bottom_unit = ( $param['modal_bottom_unit'] ?? '' ) === '%' ? 'vh' : 'px'; |
| 202 |
$args['--mw-inset-bottom'] = ( $param['modal_bottom'] ?? '0' ) . $modal_bottom_unit; |
| 203 |
} |
| 204 |
|
| 205 |
if ( ! empty( $param['include_modal_left'] ) ) { |
| 206 |
$modal_left_unit = ( $param['modal_left_unit'] ?? '' ) === '%' ? '%' : 'px'; |
| 207 |
$args['--mw-inset-left'] = ( $param['modal_left'] ?? '0' ) . $modal_left_unit; |
| 208 |
} |
| 209 |
|
| 210 |
return $args; |
| 211 |
} |
| 212 |
|
| 213 |
private function title(): array { |
| 214 |
$param = $this->param; |
| 215 |
if ( empty( $param['popup_title'] ) ) { |
| 216 |
return []; |
| 217 |
} |
| 218 |
$args = [ |
| 219 |
'--mw-title-size' => ( $param['title_size'] ?? '32' ) . 'px', |
| 220 |
'--mw-title-line-height' => ( $param['title_line_height'] ?? '32' ) . 'px', |
| 221 |
'--mw-title-font' => $param['title_font'] ?? 'inherit', |
| 222 |
'--mw-title-weight' => $param['title_font_weight'] ?? '400', |
| 223 |
'--mw-title-style' => $param['title_font_style'] ?? 'normal', |
| 224 |
'--mw-title-align' => $param['title_align'] ?? 'left', |
| 225 |
'--mw-title-color' => $param['title_color'] ?? '#383838', |
| 226 |
'--mw-title-bg' => $param['title_background'] ?? 'rgba(255, 255, 255, 0)', |
| 227 |
]; |
| 228 |
|
| 229 |
return $args; |
| 230 |
} |
| 231 |
|
| 232 |
private function close_btn(): array { |
| 233 |
$param = $this->param; |
| 234 |
if ( ! empty( $param['close_button_remove'] ) ) { |
| 235 |
return []; |
| 236 |
} |
| 237 |
$args = [ |
| 238 |
'--mw-close-padding' => $param['close_padding'] ?? '6px 12px', |
| 239 |
'--mw-close-size' => ( $param['close_size'] ?? '12' ) . 'px', |
| 240 |
'--mw-close-font' => $param['close_font'] ?? 'inherit', |
| 241 |
'--mw-close-weight' => $param['close_weight'] ?? '400', |
| 242 |
'--mw-close-style' => $param['close_font_style'] ?? 'normal', |
| 243 |
'--mw-close-radius' => ( $param['close_border_radius'] ?? '0' ) . 'px', |
| 244 |
'--mw-close-box' => ( $param['close_box_size'] ?? '24' ) . 'px', |
| 245 |
'--mw-close-color' => $param['close_content_color'] ?? '#ffffff', |
| 246 |
'--mw-close-h-color' => $param['close_content_color_hover'] ?? '#000000', |
| 247 |
'--mw-close-bg' => $param['close_background_color'] ?? '#000000', |
| 248 |
'--mw-close-h-bg' => $param['close_background_hover'] ?? '#ffffff', |
| 249 |
]; |
| 250 |
|
| 251 |
$top = ( $param['close_top_position'] ?? '0' ) . 'px'; |
| 252 |
$right = ( $param['close_right_position'] ?? '0' ) . 'px'; |
| 253 |
$left = ( $param['close_left_position'] ?? '0' ) . 'px'; |
| 254 |
$bottom = ( $param['close_bottom_position'] ?? '0' ) . 'px'; |
| 255 |
|
| 256 |
if ( $param['close_location'] === 'topRight' ) { |
| 257 |
$args['--mw-close-inset'] = "$top $right auto auto"; |
| 258 |
} |
| 259 |
|
| 260 |
if ( $param['close_location'] === 'topLeft' ) { |
| 261 |
$args['--mw-close-inset'] = "$top auto auto $left"; |
| 262 |
} |
| 263 |
|
| 264 |
if ( $param['close_location'] === 'bottomRight' ) { |
| 265 |
$args['--mw-close-inset'] = "auto $right $bottom auto"; |
| 266 |
} |
| 267 |
|
| 268 |
if ( $param['close_location'] === 'bottomLeft' ) { |
| 269 |
$args['--mw-close-inset'] = "auto auto $bottom $left"; |
| 270 |
} |
| 271 |
|
| 272 |
return $args; |
| 273 |
} |
| 274 |
|
| 275 |
private function float_btn(): array { |
| 276 |
$param = $this->param; |
| 277 |
$type = $param['umodal_button'] ?? 'no'; |
| 278 |
if ( $type === 'no' ) { |
| 279 |
return []; |
| 280 |
} |
| 281 |
$args = [ |
| 282 |
'--mw-flbtn-padding' => $param['button_padding'] ?? '14px 14px', |
| 283 |
'--mw-flbtn-radius' => ( $param['button_radius'] ?? '4' ) . 'px', |
| 284 |
'--mw-flbtn-size' => ( $param['button_text_size'] ?? '1.2' ) . ( $param['button_text_size_unit'] ?? 'em' ), |
| 285 |
'--mw-flbtn-color' => $param['button_text_color'] ?? '#ffffff', |
| 286 |
'--mw-flbtn-h-color' => $param['button_text_hcolor'] ?? '#ffffff', |
| 287 |
'--mw-flbtn-bg' => $param['umodal_button_color'] ?? '#383838', |
| 288 |
'--mw-flbtn-h-bg' => $param['umodal_button_hover'] ?? '#797979', |
| 289 |
'--mw-anime-duration' => ( $param['button_animate_duration'] ?? '5' ) . 's', |
| 290 |
'--mw-anime-iteration' => $param['button_animate_time'] ?? '5', |
| 291 |
]; |
| 292 |
|
| 293 |
$offset = ( $param['button_position'] ?? '50' ) . '%'; |
| 294 |
$margin = ( $param['button_margin'] ?? '-4' ) . 'px'; |
| 295 |
|
| 296 |
if ( $param['umodal_button_position'] === 'wow_modal_button_right' ) { |
| 297 |
$args['--mw-flbtn-inset'] = "$offset $margin auto auto"; |
| 298 |
} |
| 299 |
|
| 300 |
if ( $param['umodal_button_position'] === 'wow_modal_button_left' ) { |
| 301 |
$args['--mw-flbtn-inset'] = "$offset auto auto $margin"; |
| 302 |
} |
| 303 |
|
| 304 |
if ( $param['umodal_button_position'] === 'wow_modal_button_top' ) { |
| 305 |
$args['--mw-flbtn-inset'] = "$margin auto auto $offset"; |
| 306 |
} |
| 307 |
|
| 308 |
if ( $param['umodal_button_position'] === 'wow_modal_button_bottom' ) { |
| 309 |
$args['--mw-flbtn-inset'] = "auto auto $margin $offset"; |
| 310 |
} |
| 311 |
|
| 312 |
return $args; |
| 313 |
} |
| 314 |
|
| 315 |
} |