| @@ -1,6 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | +defined('ABSPATH') || die('Restricted Access'); | |
| 2 | 3 | |
| 4 | +/** | |
| 5 | + * Add the attribute data-open (should be the ID of the modal) to any element to make it open the modal. | |
| 6 | + */ | |
| 3 | 7 | function acym_modal( |
| 4 | 8 | string $button, |
| 5 | 9 | string $data, |
| 6 | 10 | ?string $id = null, |
| @@ -8,15 +12,15 @@ | ||
| 8 | 12 | array $attributesButton = [], |
| 9 | 13 | bool $isButton = true, |
| 10 | 14 | bool $isLarge = true, |
| 11 | 15 | string $classesModal = '' |
| 12 | -): string { | |
| 16 | +): void { | |
| 13 | 17 | if (empty($id)) { |
| 14 | - $id = 'acymodal_'.rand(1000, 9000); | |
| 18 | + $id = 'acymodal_'.acym_rand(1000, 9000); | |
| 15 | 19 | } |
| 16 | 20 | |
| 21 | + //TODO test with data-iframe param since it's a partial URL | |
| 17 | 22 | $attributesButton['data-open'] = $id; |
| 18 | - $buttonParams = acym_getFormattedAttributes($attributesButton); | |
| 19 | 23 | |
| 20 | 24 | $attributesModal['class'] = 'reveal '.$classesModal; |
| 21 | 25 | $attributesModal['id'] = $id; |
| 22 | 26 | $attributesModal['data-reveal'] = ''; |
| @@ -22,18 +26,75 @@ | ||
| 22 | 26 | $attributesModal['data-reveal'] = ''; |
| 23 | 27 | if ($isLarge) { |
| 24 | 28 | $attributesModal['data-reveal-larger'] = ''; |
| 25 | 29 | } |
| 26 | - $modalParams = acym_getFormattedAttributes($attributesModal); | |
| 27 | 30 | |
| 28 | - $modal = $isButton ? '<button type="button" '.$buttonParams.'>'.$button.'</button>' : $button; | |
| 29 | - $modal .= '<div '.$modalParams.'>'; | |
| 30 | - $modal .= $data; | |
| 31 | - $modal .= '<button class="close-button" data-close aria-label="Close reveal" type="button">'; | |
| 32 | - $modal .= '<span aria-hidden="true">×</span>'; | |
| 33 | - $modal .= '</button></div>'; | |
| 31 | + if ($isButton) { | |
| 32 | + echo '<button type="button" '; | |
| 33 | + foreach ($attributesButton as $oneAttribute => $oneValue) { | |
| 34 | + if (is_array($oneValue) || is_object($oneValue)) { | |
| 35 | + $oneValue = json_encode($oneValue); | |
| 36 | + } elseif ($oneValue === true) { | |
| 37 | + $oneValue = $oneAttribute; | |
| 38 | + } | |
| 34 | 39 | |
| 35 | - return $modal; | |
| 40 | + echo ' '.esc_attr($oneAttribute).'="'.esc_attr($oneValue).'"'; | |
| 41 | + } | |
| 42 | + echo '>'; | |
| 43 | + } | |
| 44 | + echo wp_kses( | |
| 45 | + $button, | |
| 46 | + [ | |
| 47 | + 'i' => [ | |
| 48 | + 'class' => true, | |
| 49 | + 'data-acy-elementid' => true, | |
| 50 | + 'data-open' => true, | |
| 51 | + 'data-ajax' => true, | |
| 52 | + 'data-iframe' => true, | |
| 53 | + 'data-iframe-class' => true, | |
| 54 | + 'id' => true, | |
| 55 | + 'acym-data-domain' => true, | |
| 56 | + ], | |
| 57 | + 'input' => [ | |
| 58 | + 'data-open' => true, | |
| 59 | + 'class' => true, | |
| 60 | + 'type' => true, | |
| 61 | + 'value' => true, | |
| 62 | + 'readonly' => true, | |
| 63 | + 'onchange' => true, | |
| 64 | + ], | |
| 65 | + 'span' => [ | |
| 66 | + 'class' => true, | |
| 67 | + ], | |
| 68 | + 'div' => [ | |
| 69 | + 'class' => true, | |
| 70 | + 'id' => true, | |
| 71 | + 'data-iframe' => true, | |
| 72 | + 'data-iframe-class' => true, | |
| 73 | + 'data-open' => true, | |
| 74 | + ], | |
| 75 | + ] | |
| 76 | + ); | |
| 77 | + if ($isButton) { | |
| 78 | + echo '</button>'; | |
| 79 | + } | |
| 80 | + | |
| 81 | + echo '<div '; | |
| 82 | + foreach ($attributesModal as $oneAttribute => $oneValue) { | |
| 83 | + if (is_array($oneValue) || is_object($oneValue)) { | |
| 84 | + $oneValue = json_encode($oneValue); | |
| 85 | + } elseif ($oneValue === true) { | |
| 86 | + $oneValue = $oneAttribute; | |
| 87 | + } | |
| 88 | + | |
| 89 | + echo ' '.esc_attr($oneAttribute).'="'.esc_attr($oneValue).'"'; | |
| 90 | + } | |
| 91 | + echo '>'; | |
| 92 | + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped by the caller, too many cases. | |
| 93 | + echo $data; | |
| 94 | + echo '<button class="close-button" data-close aria-label="Close reveal" type="button">'; | |
| 95 | + echo '<span aria-hidden="true">×</span>'; | |
| 96 | + echo '</button></div>'; | |
| 36 | 97 | } |
| 37 | 98 | |
| 38 | 99 | function acym_modalInclude( |
| 39 | 100 | string $button, |
| @@ -41,27 +102,47 @@ | ||
| 41 | 102 | string $id, |
| 42 | 103 | array $data, |
| 43 | 104 | string $classModal = '', |
| 44 | 105 | array $containerAttributes = [] |
| 45 | -): string { | |
| 106 | +): void { | |
| 46 | 107 | if (empty($id)) { |
| 47 | - $id = 'acymodal_'.rand(1000, 9000); | |
| 108 | + $id = 'acymodal_'.acym_rand(1000, 9000); | |
| 48 | 109 | } |
| 49 | 110 | |
| 111 | + // Both can be used in the included file | |
| 50 | 112 | $dataModal = $data; |
| 51 | 113 | |
| 52 | 114 | $containerAttributes['data-open'] = $id; |
| 53 | - $containerParams = acym_getFormattedAttributes($containerAttributes); | |
| 54 | - $modal = '<div '.$containerParams.'>'.$button; | |
| 55 | - $modal .= '<div class="reveal '.acym_escape($classModal).'" id="'.acym_escape($id).'" data-reveal>'; | |
| 56 | - ob_start(); | |
| 115 | + echo '<div '; | |
| 116 | + foreach ($containerAttributes as $oneAttribute => $oneValue) { | |
| 117 | + if (is_array($oneValue) || is_object($oneValue)) { | |
| 118 | + $oneValue = json_encode($oneValue); | |
| 119 | + } elseif ($oneValue === true) { | |
| 120 | + $oneValue = $oneAttribute; | |
| 121 | + } | |
| 122 | + | |
| 123 | + echo ' '.esc_attr($oneAttribute).'="'.esc_attr($oneValue).'"'; | |
| 124 | + } | |
| 125 | + echo '>'; | |
| 126 | + echo wp_kses( | |
| 127 | + $button, | |
| 128 | + [ | |
| 129 | + 'button' => [ | |
| 130 | + 'type' => true, | |
| 131 | + 'id' => true, | |
| 132 | + 'class' => true, | |
| 133 | + ], | |
| 134 | + 'span' => [ | |
| 135 | + 'class' => true, | |
| 136 | + ], | |
| 137 | + 'b' => [], | |
| 138 | + ] | |
| 139 | + ); | |
| 140 | + echo '<div class="reveal '.esc_attr($classModal).'" id="'.esc_attr($id).'" data-reveal>'; | |
| 57 | 141 | include $file; |
| 58 | - $modal .= ob_get_clean(); | |
| 59 | - $modal .= '<button type="button" class="close-button" data-close aria-label="Close reveal">'; | |
| 60 | - $modal .= '<span aria-hidden="true">×</span>'; | |
| 61 | - $modal .= '</button></div></div>'; | |
| 62 | - | |
| 63 | - return $modal; | |
| 142 | + echo '<button type="button" class="close-button" data-close aria-label="Close reveal">'; | |
| 143 | + echo '<span aria-hidden="true">×</span>'; | |
| 144 | + echo '</button></div></div>'; | |
| 64 | 145 | } |
| 65 | 146 | |
| 66 | 147 | function acym_modalPaginationLists( |
| 67 | 148 | string $inputEventId, |
| @@ -66,42 +147,44 @@ | ||
| 66 | 147 | function acym_modalPaginationLists( |
| 67 | 148 | string $inputEventId, |
| 68 | 149 | string $checkedLists = '[]', |
| 69 | 150 | bool $needDisplaySubscribers = false |
| 70 | -): string { | |
| 71 | - $searchField = acym_filterSearch('', 'modal_search_lists'); | |
| 151 | +): void { | |
| 152 | + ?> | |
| 153 | + <input type="hidden" id="<?php echo esc_attr($inputEventId); ?>"> | |
| 72 | 154 | |
| 73 | - $data = '<input type="hidden" id="'.acym_escape($inputEventId).'">'; | |
| 155 | + <?php if ($needDisplaySubscribers) { ?> | |
| 156 | + <input type="hidden" id="modal__pagination__need__display__sub"> | |
| 157 | + <?php } ?> | |
| 74 | 158 | |
| 75 | - if ($needDisplaySubscribers) { | |
| 76 | - $data .= '<input type="hidden" id="modal__pagination__need__display__sub">'; | |
| 77 | - } | |
| 78 | - | |
| 79 | - $data .= '<div class="cell grid-x" style="display: none;" id="acym__popup__plugin__subscription__lists__modal"> | |
| 80 | - <input type="hidden" name="show_selected" value="false" id="modal__pagination__show-information"> | |
| 81 | - <input type="hidden" id="modal__pagination__search__lists"> | |
| 82 | - <input type="hidden" name="lists_selected" id="acym__modal__lists-selected" value="'.acym_escape($checkedLists).'"> | |
| 83 | - <div class="cell grid-x"> | |
| 84 | - <h4 class="cell text-center acym__title acym__title__secondary">'.acym_escape(acym_translation('ACYM_CHOOSE_LISTS')).'</h4> | |
| 85 | - </div> | |
| 86 | - <div class="cell grid-x modal__pagination__search"> | |
| 87 | - '.$searchField.' | |
| 88 | - </div> | |
| 89 | - <div class="cell text-center padding-top-1" id="modal__pagination__search__spinner" style="display: none"> | |
| 90 | - <i class="acymicon-circle-o-notch acymicon-spin"></i> | |
| 91 | - </div> | |
| 92 | - <div class="cell medium-6 modal__pagination__show"> | |
| 93 | - <a href="#" class="acym__color__blue modal__pagination__show-selected modal__pagination__show-button selected">'.acym_escape( | |
| 94 | - acym_translation('ACYM_SHOW_SELECTED_LISTS') | |
| 95 | - ).'</a> | |
| 96 | - <a href="#" class="acym__color__blue modal__pagination__show-all modal__pagination__show-button">'.acym_escape(acym_translation('ACYM_SHOW_ALL_LISTS')).'</a> | |
| 97 | - </div> | |
| 98 | - <div class="cell grid-x modal__pagination__listing__lists"> | |
| 99 | - <div class="cell modal__pagination__listing__lists__in-form"></div> | |
| 100 | - </div> | |
| 101 | - </div>'; | |
| 102 | - | |
| 103 | - return $data; | |
| 159 | + <div class="cell grid-x" style="display: none;" id="acym__popup__plugin__subscription__lists__modal"> | |
| 160 | + <input type="hidden" name="show_selected" value="false" id="modal__pagination__show-information"> | |
| 161 | + <input type="hidden" id="modal__pagination__search__lists"> | |
| 162 | + <input type="hidden" name="lists_selected" id="acym__modal__lists-selected" value="<?php echo esc_attr($checkedLists); ?>"> | |
| 163 | + <div class="cell grid-x"> | |
| 164 | + <h4 class="cell text-center acym__title acym__title__secondary"> | |
| 165 | + <?php echo esc_html(acym_translation('ACYM_CHOOSE_LISTS')); ?> | |
| 166 | + </h4> | |
| 167 | + </div> | |
| 168 | + <div class="cell grid-x modal__pagination__search"> | |
| 169 | + <?php acym_filterSearch('', 'modal_search_lists'); ?> | |
| 170 | + </div> | |
| 171 | + <div class="cell text-center padding-top-1" id="modal__pagination__search__spinner" style="display: none"> | |
| 172 | + <i class="acymicon-circle-o-notch acymicon-spin"></i> | |
| 173 | + </div> | |
| 174 | + <div class="cell medium-6 modal__pagination__show"> | |
| 175 | + <a href="#" class="acym__color__blue modal__pagination__show-selected modal__pagination__show-button selected"> | |
| 176 | + <?php echo esc_html(acym_translation('ACYM_SHOW_SELECTED_LISTS')); ?> | |
| 177 | + </a> | |
| 178 | + <a href="#" class="acym__color__blue modal__pagination__show-all modal__pagination__show-button"> | |
| 179 | + <?php echo esc_html(acym_translation('ACYM_SHOW_ALL_LISTS')); ?> | |
| 180 | + </a> | |
| 181 | + </div> | |
| 182 | + <div class="cell grid-x modal__pagination__listing__lists"> | |
| 183 | + <div class="cell modal__pagination__listing__lists__in-form"></div> | |
| 184 | + </div> | |
| 185 | + </div> | |
| 186 | + <?php | |
| 104 | 187 | } |
| 105 | 188 | |
| 106 | 189 | function acym_frontModal( |
| 107 | 190 | string $iframeSrc, |
| @@ -117,9 +200,9 @@ | ||
| 117 | 200 | acym_addScript(false, ACYM_JS.'modal.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'modal.min.js')); |
| 118 | 201 | } |
| 119 | 202 | |
| 120 | 203 | if (empty($identifier)) { |
| 121 | - $identifier = 'identifier_'.rand(1000, 9000); | |
| 204 | + $identifier = 'identifier_'.acym_rand(1000, 9000); | |
| 122 | 205 | } |
| 123 | 206 | |
| 124 | 207 | if (empty($iframeClass)) { |
| 125 | 208 | $iframeClass = 'acym__modal__iframe'; |
| @@ -126,15 +209,15 @@ | ||
| 126 | 209 | } |
| 127 | 210 | |
| 128 | 211 | ob_start(); |
| 129 | 212 | ?> |
| 130 | - <a class="<?php echo $isButton ? 'btn ' : ''; ?>acym__modal__handle" data-acym-modal="<?php echo acym_escape($identifier); ?>" href="#"> | |
| 131 | - <?php echo acym_escape(acym_translation($buttonText)); ?> | |
| 213 | + <a class="<?php echo $isButton ? 'btn ' : ''; ?>acym__modal__handle" data-acym-modal="<?php echo esc_attr($identifier); ?>" href="#"> | |
| 214 | + <?php echo esc_html(acym_translation($buttonText)); ?> | |
| 132 | 215 | </a> |
| 133 | - <div class="acym__modal" id="acym__modal__<?php echo acym_escape($identifier); ?>" style="display: none;"> | |
| 216 | + <div class="acym__modal" id="acym__modal__<?php echo esc_attr($identifier); ?>" style="display: none;"> | |
| 134 | 217 | <div class="acym__modal__content"> |
| 135 | 218 | <div class="acym__modal__close"><span>×</span></div> |
| 136 | - <iframe class="<?php echo acym_escape($iframeClass); ?>" src="<?php echo acym_escapeUrl($iframeSrc); ?>"></iframe> | |
| 219 | + <iframe class="<?php echo esc_attr($iframeClass); ?>" src="<?php echo esc_url($iframeSrc); ?>"></iframe> | |
| 137 | 220 | </div> |
| 138 | 221 | </div> |
| 139 | 222 | <?php |
| 140 | 223 | return ob_get_clean(); |