1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
<?php
namespace SIW\Elements;
use SIW\HTML;
class Modal {
const MICROMODAL_VERSION = '0.4.2';
protected $id;
protected $title;
protected $content;
public function __construct( string $id = null ) {
$this->enqueue_styles();
$this->enqueue_scripts();
$this->id = ( null === $id ) ? uniqid( 'siw-modal-' ) : "siw-modal-{$id}";
add_action( 'wp_footer', [ $this, 'render_modal'] );
add_filter( 'rocket_exclude_js', [ $this, 'exclude_js_from_combine' ] );
}
protected function enqueue_styles() {
wp_register_style( 'siw-modal', SIW_ASSETS_URL . 'css/elements/siw-modal.css', [], SIW_PLUGIN_VERSION );
wp_enqueue_style( 'siw-modal' );
}
protected function enqueue_scripts() {
wp_register_script( 'micromodal', SIW_ASSETS_URL . 'modules/micromodal/micromodal.js', [], self::MICROMODAL_VERSION, true );
wp_register_script( 'siw-modal', SIW_ASSETS_URL . 'js/elements/siw-modal.js', [ 'micromodal' ], SIW_PLUGIN_VERSION, true );
wp_localize_script(
'siw-modal',
'siw_modal',
[
'openTrigger' => 'data-modal-open',
'closeTrigger' => 'data-modal-close',
'disableScroll' => true,
'disableFocus' => false,
'awaitOpenAnimation' => true,
'awaitCloseAnimation' => true,
'debugMode' => defined( 'WP_DEBUG' ) && WP_DEBUG,
]);
wp_enqueue_script( 'siw-modal' );
}
public function exclude_js_from_combine( array $excluded_files ) {
$excluded_files[] = wp_make_link_relative( SIW_ASSETS_URL . 'modules/micromodal/micromodal.js' );
$excluded_files[] = wp_make_link_relative( SIW_ASSETS_URL . 'js/elements/siw-modal.js' );
return $excluded_files;
}
public function render_modal() {
$modal = sprintf( '<div class="modal micromodal-slide" id="%s" aria-hidden="true">', $this->id );
$modal .= '<div class="modal-overlay" tabindex="-1" data-micromodal-close>';
$modal .= sprintf( '<div class="modal-container" role="dialog" aria-modal="true" aria-labelledby="%s-title">', $this->id );
$modal .= $this->generate_header() . $this->generate_body() . $this->generate_footer();
$modal .= '</div>';
$modal .= '</div>';
$modal .= '</div>';
$modal .= '</div>';
echo $modal;
}
protected function generate_header() {
$header = '<header class="modal-header">';
$header .= sprintf( '<h4 class="modal-title" id="%s-title">%s</h4>', $this->id, $this->title );
$header .= sprintf( '<button class="modal-close" aria-label="%s" data-micromodal-close></button>', esc_html__( 'Sluiten', 'siw' ) );
$header .= '</header>';
return $header;
}
protected function generate_body() {
return HTML::generate_tag(
'main',
[
'class' => 'modal-body',
'id' => "{$this->id}-content"
],
wpautop( wp_kses_post( $this->content ) ),
true
);
}
protected function generate_footer() {
$button = sprintf(
'<button class="kad-btn" data-micromodal-close aria-label="%s">%s</button>',
esc_html__( 'Sluit deze dialoog', 'siw' ),
esc_html__( 'Sluiten', 'siw' )
);
return HTML::generate_tag( 'footer', [ 'class' => 'modal-footer'], $button, true );
}
public function set_title( string $title ) {
$this->title = $title;
}
public function generate_link( string $text, string $link = null ) {
$link = HTML::generate_link(
$link ?? '#',
$text,
[ 'data-micromodal-trigger' => $this->id, 'target' => '_blank' ]
);
return $link;
}
public function set_content( string $content ) {
$this->content = $content;
}
public function get_id() {
return $this->id;
}
}