| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Shortcode |
| 4 |
* |
| 5 |
* @since 1.11.0 |
| 6 |
* @package QuillForms |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace QuillForms; |
| 10 |
|
| 11 |
/** |
| 12 |
* Shortcode Class |
| 13 |
* |
| 14 |
* @since 1.11.0 |
| 15 |
*/ |
| 16 |
class Shortcode |
| 17 |
{ |
| 18 |
|
| 19 |
/** |
| 20 |
* Class instance |
| 21 |
* |
| 22 |
* @var self instance |
| 23 |
*/ |
| 24 |
private static $instance = null; |
| 25 |
|
| 26 |
/** |
| 27 |
* Get class instance |
| 28 |
* |
| 29 |
* @return self |
| 30 |
*/ |
| 31 |
public static function instance() |
| 32 |
{ |
| 33 |
if (!self::$instance) { |
| 34 |
self::$instance = new self(); |
| 35 |
} |
| 36 |
return self::$instance; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Constructor |
| 41 |
* |
| 42 |
* @since 1.11.0 |
| 43 |
*/ |
| 44 |
private function __construct() |
| 45 |
{ |
| 46 |
add_action('init', array($this, 'register')); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Register [quillforms] shortcode |
| 51 |
* |
| 52 |
* @since 1.11.0 |
| 53 |
* |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public function register() |
| 57 |
{ |
| 58 |
add_shortcode('quillforms', array($this, 'handler')); |
| 59 |
add_shortcode('quillforms-popup', array($this, 'popup_handler')); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Handle shortcode render |
| 64 |
* |
| 65 |
* @since 1.11.0 |
| 66 |
* |
| 67 |
* @param array $atts Shortcode attributes. |
| 68 |
* @return string |
| 69 |
*/ |
| 70 |
public function handler($atts) |
| 71 |
{ |
| 72 |
$atts = shortcode_atts( |
| 73 |
array( |
| 74 |
'id' => null, |
| 75 |
'width' => '100%', |
| 76 |
'min_height' => null, |
| 77 |
'max_height' => null, |
| 78 |
'height' => null, |
| 79 |
), |
| 80 |
$atts, |
| 81 |
'quillforms' |
| 82 |
); |
| 83 |
|
| 84 |
$id = (int) $atts['id']; |
| 85 |
$width = esc_attr($atts['width']); |
| 86 |
$height = esc_attr($atts['height']); |
| 87 |
$min_height = esc_attr($atts['min_height']); |
| 88 |
$max_height = esc_attr($atts['max_height']); |
| 89 |
if (!$min_height && !$height) { |
| 90 |
$min_height = "500px"; |
| 91 |
} |
| 92 |
|
| 93 |
if (!$min_height && $height) { |
| 94 |
$min_height = $height; |
| 95 |
} |
| 96 |
if (!$max_height) { |
| 97 |
$max_height = 'auto'; |
| 98 |
} |
| 99 |
if (!$max_height && $height) { |
| 100 |
$max_height = $height; |
| 101 |
} |
| 102 |
|
| 103 |
if ('quill_forms' !== get_post_type($id)) { |
| 104 |
return esc_html__('Invalid form ID', 'quillforms'); |
| 105 |
} |
| 106 |
|
| 107 |
$src = get_permalink($id); |
| 108 |
$src = esc_url( |
| 109 |
add_query_arg( |
| 110 |
array( |
| 111 |
'quillforms-shortcode' => true, |
| 112 |
'quillforms-redirection' => 'top', // @deprecated 1.11.1 |
| 113 |
), |
| 114 |
$src |
| 115 |
) |
| 116 |
); |
| 117 |
|
| 118 |
// wp_enqueue_script('quillforms-iframe-resizer'); |
| 119 |
wp_enqueue_script('quillforms-iframe-resizer-implementer'); |
| 120 |
|
| 121 |
return sprintf( |
| 122 |
"<iframe data-max-height='%s' class='quillforms-iframe' scrolling='no' src='%s' width='%s' style='border:0;min-height:%s;width: 100%%; min-width: 100%%; max-height:%s'></iframe>", |
| 123 |
esc_attr($max_height), |
| 124 |
esc_url($src), |
| 125 |
esc_attr($width), |
| 126 |
esc_attr($min_height), |
| 127 |
esc_attr($max_height) |
| 128 |
); |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Handle popup shortcode render |
| 134 |
* |
| 135 |
* @since 2.12.0 |
| 136 |
* |
| 137 |
* @param array $atts Shortcode attributes. |
| 138 |
* @return string |
| 139 |
*/ |
| 140 |
|
| 141 |
public function popup_handler($atts) |
| 142 |
{ |
| 143 |
// Enqueue jQuery first |
| 144 |
wp_enqueue_script('jquery'); |
| 145 |
wp_enqueue_script('quillforms-popup', quillforms_url('includes/render/popup.js'), array('jquery'), QUILLFORMS_VERSION, true); |
| 146 |
wp_enqueue_style('quillforms-popup-style'); |
| 147 |
|
| 148 |
$atts = shortcode_atts( |
| 149 |
array( |
| 150 |
'id' => null, |
| 151 |
'buttontitle' => 'Open Form', |
| 152 |
'buttonbackgroundcolor' => '#000000', |
| 153 |
'buttontextcolor' => '#ffffff', |
| 154 |
'buttonborderradius' => '24', |
| 155 |
'buttonborderwidth' => '0', |
| 156 |
'buttonbordercolor' => '#000000', |
| 157 |
'buttonfontsize' => '16', |
| 158 |
'buttonpadding' => '10px 20px', |
| 159 |
'popupmaxwidth' => '90', |
| 160 |
'popupmaxwidthunit' => '%', |
| 161 |
'popupmaxheight' => '90', |
| 162 |
'popupmaxheightunit' => '%', |
| 163 |
), |
| 164 |
$atts, |
| 165 |
'quillforms-popup' |
| 166 |
); |
| 167 |
|
| 168 |
$id = (int) $atts['id']; |
| 169 |
if ('quill_forms' !== get_post_type($id)) { |
| 170 |
return esc_html__('Invalid form ID', 'quillforms'); |
| 171 |
} |
| 172 |
|
| 173 |
// Properly escape the button title |
| 174 |
$buttonTitle = wp_kses_post($atts['buttontitle']); |
| 175 |
|
| 176 |
// Rest of your attributes escaping... |
| 177 |
$buttonBackgroundColor = esc_attr($atts['buttonbackgroundcolor']); |
| 178 |
$buttonTextColor = esc_attr($atts['buttontextcolor']); |
| 179 |
$buttonBorderRadius = (int) $atts['buttonborderradius']; |
| 180 |
$buttonBorderWidth = (int) $atts['buttonborderwidth']; |
| 181 |
$buttonBorderColor = esc_attr($atts['buttonbordercolor']); |
| 182 |
$buttonFontSize = (int) $atts['buttonfontsize']; |
| 183 |
$buttonPadding = esc_attr($atts['buttonpadding']); |
| 184 |
$popupMaxWidth = esc_attr($atts['popupmaxwidth']); |
| 185 |
$popupMaxWidthUnit = esc_attr($atts['popupmaxwidthunit']); |
| 186 |
$popupMaxHeight = esc_attr($atts['popupmaxheight']); |
| 187 |
$popupMaxHeightUnit = esc_attr($atts['popupmaxheightunit']); |
| 188 |
|
| 189 |
$src = esc_url( |
| 190 |
add_query_arg( |
| 191 |
array( |
| 192 |
'quillforms-shortcode' => true, |
| 193 |
'quillforms-redirection' => 'top', |
| 194 |
), |
| 195 |
get_permalink($id) |
| 196 |
) |
| 197 |
); |
| 198 |
|
| 199 |
// Use wp_kses to allow only specific HTML tags if needed |
| 200 |
$buttonHtml = wp_kses($buttonTitle, array( |
| 201 |
'strong' => array(), |
| 202 |
'em' => array(), |
| 203 |
'span' => array('class' => array()), |
| 204 |
'br' => array() |
| 205 |
)); |
| 206 |
|
| 207 |
return '<div class="quillforms-popup-button-wrapper"> |
| 208 |
<a class="quillforms-popup-button" style=" |
| 209 |
background-color: ' . $buttonBackgroundColor . '; |
| 210 |
color: ' . $buttonTextColor . '; |
| 211 |
border-radius: ' . $buttonBorderRadius . 'px; |
| 212 |
border-width: ' . $buttonBorderWidth . 'px; |
| 213 |
border-color: ' . $buttonBorderColor . '; |
| 214 |
font-size: ' . $buttonFontSize . 'px; |
| 215 |
padding: ' . $buttonPadding . '; |
| 216 |
text-decoration: none; |
| 217 |
cursor: pointer; |
| 218 |
display: inline-block; |
| 219 |
" |
| 220 |
data-url="' . $src . '" |
| 221 |
data-formId="' . $id . '" |
| 222 |
> |
| 223 |
' . $buttonHtml . ' |
| 224 |
</a> |
| 225 |
<div class="quillforms-popup-overlay" style=" |
| 226 |
position: fixed; |
| 227 |
top: 0; |
| 228 |
left: 0; |
| 229 |
width: 100%; |
| 230 |
height: 100%; |
| 231 |
background-color: rgba(0, 0, 0, 0.8); |
| 232 |
display: flex; |
| 233 |
justify-content: center; |
| 234 |
align-items: center; |
| 235 |
z-index: -1; |
| 236 |
opacity: 0; |
| 237 |
transition: opacity 0.3s ease-in-out; |
| 238 |
pointer-events: none; |
| 239 |
visibility: hidden; |
| 240 |
" data-formId="' . $id . '"> |
| 241 |
<div class="quillforms-popup-container" style=" |
| 242 |
max-width: ' . $popupMaxWidth . $popupMaxWidthUnit . '; |
| 243 |
max-height: ' . $popupMaxHeight . $popupMaxHeightUnit . '; |
| 244 |
width: 100%; |
| 245 |
height: 100%; |
| 246 |
"> |
| 247 |
<div class="quillforms-popup-close"> |
| 248 |
<svg fill="currentColor" height="32" width="32" viewBox="0 0 24 24" style="display: inline-block; vertical-align: middle;"> |
| 249 |
<path d="M0 0h24v24H0z" fill="none"></path> |
| 250 |
<path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"></path> |
| 251 |
</svg> |
| 252 |
</div> |
| 253 |
<div class="quillforms-popup-iframe-wrapper"> |
| 254 |
<iframe data-no-lazy="true" src="' . $src . '" width="100%" height="100%" style="border:0;max-height:auto !important; max-width:auto !important;"></iframe> |
| 255 |
<div class="quillforms-popup-loader"><div class="quillforms-loading-circle"></div></div> |
| 256 |
</div> |
| 257 |
</div> |
| 258 |
</div> |
| 259 |
</div>'; |
| 260 |
} |
| 261 |
} |