| 1 |
<?php |
| 2 |
/** |
| 3 |
* PPOM Frontend Form Rendering Class |
| 4 |
* |
| 5 |
* It control inputs base templates |
| 6 |
* |
| 7 |
* @version 1.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
/* |
| 11 |
**========== Block direct access =========== |
| 12 |
*/ |
| 13 |
if( ! defined('ABSPATH' ) ){ exit; } |
| 14 |
|
| 15 |
class WPComment_Form { |
| 16 |
|
| 17 |
private static $ins = null; |
| 18 |
public static $fields; |
| 19 |
|
| 20 |
function __construct(){ |
| 21 |
|
| 22 |
$wpcomment_meta = wpcomment_get_saved_meta(); |
| 23 |
// filter out if fields are not active |
| 24 |
$wpcomment_meta = array_filter($wpcomment_meta, function($f){ |
| 25 |
return 'on' === $f['status']; |
| 26 |
}); |
| 27 |
|
| 28 |
self::$fields = $wpcomment_meta; |
| 29 |
} |
| 30 |
|
| 31 |
public static function get_instance() { |
| 32 |
|
| 33 |
// create a new object if it doesn't exist. |
| 34 |
is_null(self::$ins) && self::$ins = new self; |
| 35 |
return self::$ins; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* PPOM main wrapper 2 classes |
| 40 |
* |
| 41 |
* @return string |
| 42 |
*/ |
| 43 |
function wrapper_inner_classes() { |
| 44 |
|
| 45 |
$classes = ['form-row','wpcomment-rendering-fields','align-items-center','wpcomment-section-collapse']; |
| 46 |
|
| 47 |
$classes = apply_filters('wpcomment_wrapper2_classes', $classes); |
| 48 |
|
| 49 |
return implode(' ', $classes); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* PPOM fields rendering callback |
| 54 |
* |
| 55 |
* @return fields html |
| 56 |
*/ |
| 57 |
function fields_render(){ |
| 58 |
|
| 59 |
$posted_values = ''; |
| 60 |
|
| 61 |
$section_started = false; |
| 62 |
$wpcomment_field_counter = 0; |
| 63 |
$wpcomment_collapse_counter = 0; |
| 64 |
$allow_nextprev = wpcomment_get_option('wpcomment-collapse-nextprev'); |
| 65 |
|
| 66 |
// posted value being |
| 67 |
// used wpcomment-pro |
| 68 |
$posted_values = apply_filters('wpcomment_default_values', $posted_values, $_POST, self::$fields); |
| 69 |
|
| 70 |
foreach( self::$fields as $meta ) { |
| 71 |
|
| 72 |
$type = isset($meta['type']) ? $meta['type'] : ''; |
| 73 |
$title = isset($meta['title']) ? wpcomment_wpml_translate($meta['title'], 'wp-comment-fields') : ''; |
| 74 |
$data_name = isset($meta['data_name']) ? $meta['data_name'] : $title; |
| 75 |
$wpcomment_field_counter++; |
| 76 |
|
| 77 |
// Set ID on meta against dataname |
| 78 |
$meta['id'] = $data_name; |
| 79 |
|
| 80 |
// Dataname senatize |
| 81 |
$data_name = sanitize_key( $data_name ); |
| 82 |
|
| 83 |
$field_html = ''; |
| 84 |
|
| 85 |
// checking field visibility |
| 86 |
if( ! wpcomment_is_field_visible($meta) ) continue; |
| 87 |
|
| 88 |
if( empty($data_name) ) { printf(__("Please provide data name property for %s", "wpcomment"), $title); continue; } |
| 89 |
|
| 90 |
$default_value = $this->get_field_default_value($posted_values, $data_name, $meta); |
| 91 |
|
| 92 |
$wpcomment_cond_data = wpcomment_get_conditional_data_attributes($meta); |
| 93 |
|
| 94 |
// Text|Email|Date|Number |
| 95 |
$wpcomment_field_attributes = $this->get_attributes($meta, $type); |
| 96 |
|
| 97 |
// Set inputs attr into meta |
| 98 |
$meta['attributes'] = $wpcomment_field_attributes; |
| 99 |
|
| 100 |
$field_wrapper_class = $this->field_main_wrapper_classes($meta); |
| 101 |
|
| 102 |
|
| 103 |
// Collapse Fields Section |
| 104 |
if( $type == 'collapse' ) { |
| 105 |
$collapse_type = isset($meta['collapse_type']) ? $meta['collapse_type'] : ''; |
| 106 |
|
| 107 |
if( $section_started ) { |
| 108 |
|
| 109 |
echo '<div class="wpcomment-loop-fields" style="clear:both"></div>'; |
| 110 |
|
| 111 |
if ($allow_nextprev == 'yes') { |
| 112 |
echo '<div class="wpcomment-collapse-nextprev-btn" data-collapse-index="'.esc_attr($wpcomment_collapse_counter).'">'; |
| 113 |
echo '<button class="wpcomment-collapse-prev">'.__("Prev", "wpcomment").'</button>'; |
| 114 |
echo '<button class="wpcomment-collapse-next">'.__("Next", "wpcomment").'</button>'; |
| 115 |
echo '</div>'; |
| 116 |
} |
| 117 |
echo '</div>'; |
| 118 |
} |
| 119 |
|
| 120 |
if ($collapse_type == 'end') { |
| 121 |
echo '<div class="wpcomment-collapsed-child-end">'; |
| 122 |
} |
| 123 |
|
| 124 |
if ($collapse_type != 'end' ) { |
| 125 |
echo '<h4 data-collapse-id="'.esc_attr($data_name).'" class="wpcomment-collapsed-title">'.$title.'</h4>'; |
| 126 |
echo '<div class="collapsed-child">'; |
| 127 |
} |
| 128 |
|
| 129 |
$section_started = true; |
| 130 |
$wpcomment_collapse_counter++; |
| 131 |
} |
| 132 |
|
| 133 |
// skip collapse div |
| 134 |
if ($type == 'collapse') continue; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
$field_wrapper_div = '<div data-data_name='.esc_attr($data_name).' '.$wpcomment_cond_data.' class="'.esc_attr($field_wrapper_class).'">'; |
| 139 |
$field_html .= apply_filters('wpcomment_field_wrapper_div', $field_wrapper_div, $meta); |
| 140 |
|
| 141 |
/** |
| 142 |
* creating action space to render more addons |
| 143 |
* |
| 144 |
* Legacy Hook: wpcomment_rendering_inputs |
| 145 |
* |
| 146 |
* Template based load addons Hook: wpcomment_rendering_inputs_{$type} |
| 147 |
* |
| 148 |
* Updated by Najeeb on May 24, 2021 |
| 149 |
* Now the CORE inputs will be rendered via function rather hooks |
| 150 |
**/ |
| 151 |
ob_start(); |
| 152 |
|
| 153 |
|
| 154 |
$core_inputs = $this->get_core_inputs(); |
| 155 |
|
| 156 |
if( in_array($type, $core_inputs) ) { |
| 157 |
$this->render_input_template($meta, $default_value); |
| 158 |
} |
| 159 |
|
| 160 |
do_action("wpcomment_rendering_inputs_{$type}", $meta, $default_value); |
| 161 |
|
| 162 |
|
| 163 |
$field_html .= ob_get_clean(); |
| 164 |
|
| 165 |
$field_html .= '</div>'; |
| 166 |
|
| 167 |
if( count(self::$fields) == $wpcomment_field_counter && $section_started ) { |
| 168 |
$field_html .= '</div>'; |
| 169 |
} |
| 170 |
|
| 171 |
// Filter: nmforms_input_htmls |
| 172 |
// @TODO need to change with relevant name |
| 173 |
echo apply_filters("nmforms_input_html", $field_html, $meta, $default_value); |
| 174 |
|
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
function render_input_template($meta, $default_value){ |
| 179 |
|
| 180 |
$type = isset($meta['type']) ? $meta['type'] : ''; |
| 181 |
|
| 182 |
$template_path = "frontend/inputs/{$type}.php"; |
| 183 |
$template_vars = array( |
| 184 |
'field_meta' => $meta, |
| 185 |
'default_value' => $default_value |
| 186 |
); |
| 187 |
|
| 188 |
$template_vars = apply_filters('wpcomment_input_templates_vars', $template_vars, $this); |
| 189 |
|
| 190 |
wpcomment_load_input_templates( $template_path, $template_vars ); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Field Main Wrapper Classes |
| 195 |
* |
| 196 |
* @hook wpcomment_field_main_wrapper_class |
| 197 |
*/ |
| 198 |
function field_main_wrapper_classes($meta) { |
| 199 |
|
| 200 |
$width = $this->field_wrapper_width($meta); |
| 201 |
$dataname = isset($meta['data_name']) ? sanitize_key($meta['data_name']): ''; |
| 202 |
|
| 203 |
$classes = array(); |
| 204 |
$classes[] = 'wpcomment-field-wrapper'; |
| 205 |
$classes[] = 'wpcomment-col'; |
| 206 |
$classes[] = 'col-md-'. $width; |
| 207 |
$classes[] = $dataname; |
| 208 |
$classes[] = "wpcomment-wrapper_outer-{$dataname}"; |
| 209 |
|
| 210 |
$wrapper_classes = implode(' ',$classes); |
| 211 |
|
| 212 |
$wrapper_classes = apply_filters('wpcomment_field_main_wrapper_class', $wrapper_classes, $classes, $meta); |
| 213 |
|
| 214 |
return $wrapper_classes; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Field Colunm Width |
| 219 |
* |
| 220 |
* @hook wpcomment_{$field_type}_input_meta_width |
| 221 |
*/ |
| 222 |
function field_wrapper_width($input_meta) { |
| 223 |
|
| 224 |
$field_column = isset($input_meta['width'] ) ? $input_meta['width']: 12; |
| 225 |
|
| 226 |
// Check width has old settings |
| 227 |
if( strpos( $field_column, '%' ) !== false ) { |
| 228 |
|
| 229 |
$field_column = 12; |
| 230 |
} elseif( intval($field_column) > 12 ) { |
| 231 |
$field_column = 12; |
| 232 |
} |
| 233 |
|
| 234 |
return apply_filters("wpcomment_input_meta_width", $field_column, $input_meta); |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Rendering form extra contents |
| 239 |
*/ |
| 240 |
function form_contents(){ |
| 241 |
|
| 242 |
ob_start(); |
| 243 |
wpcomment_load_input_templates( 'frontend/component/form-data.php', |
| 244 |
apply_filters('wpcomment_form_extra_contents', $this) |
| 245 |
); |
| 246 |
echo ob_get_clean(); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Check If PPOM Fields Empty |
| 251 |
*/ |
| 252 |
function has_wpcomment_fields(){ |
| 253 |
|
| 254 |
$return = false; |
| 255 |
if( self::$fields ) { |
| 256 |
$return = true; |
| 257 |
} |
| 258 |
|
| 259 |
return apply_filters('has_wpcomment_fields', $return); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Get default input/posted values |
| 264 |
* |
| 265 |
* @return defual_value |
| 266 |
*/ |
| 267 |
function get_field_default_value($posted_values, $data_name, $meta){ |
| 268 |
|
| 269 |
$default_value = isset($meta['default_value'] ) ? $meta['default_value']: ''; |
| 270 |
$type = isset($meta['type']) ? $meta['type'] : ''; |
| 271 |
|
| 272 |
// current values from $_GET/$_POST |
| 273 |
if( isset($posted_values[$data_name]) ) { |
| 274 |
|
| 275 |
switch ($type) { |
| 276 |
|
| 277 |
case 'image': |
| 278 |
$image_data = $posted_values[$data_name]; |
| 279 |
unset($default_value); |
| 280 |
foreach($image_data as $data){ |
| 281 |
$default_value[] = json_decode( stripslashes($data), true); |
| 282 |
} |
| 283 |
break; |
| 284 |
|
| 285 |
default: |
| 286 |
$default_value = $posted_values[$data_name]; |
| 287 |
break; |
| 288 |
} |
| 289 |
|
| 290 |
} else if( isset($_GET[$data_name]) ) { |
| 291 |
// When Cart Edit addon used |
| 292 |
$default_value = sanitize_text_field($_GET[$data_name]); |
| 293 |
}else if( isset($_POST['wp-comment-fields']['fields'][$data_name]) && apply_filters('wpcomment_retain_after_add_to_cart', true) ) { |
| 294 |
$default_value = sanitize_text_field($_POST['wp-comment-fields']['fields'][$data_name]); |
| 295 |
} else { |
| 296 |
// Default values in settings |
| 297 |
switch ($type) { |
| 298 |
|
| 299 |
case 'textarea': |
| 300 |
|
| 301 |
if( is_numeric($default_value) ) { |
| 302 |
$content_post = get_post( intval($default_value) ); |
| 303 |
$content = !empty($content_post) ? $content_post->post_content : ''; |
| 304 |
$content = apply_filters('the_content', $content); |
| 305 |
$default_value = str_replace(']]>', ']]>', $content); |
| 306 |
} |
| 307 |
break; |
| 308 |
|
| 309 |
case 'checkbox': |
| 310 |
$default_value = isset($meta['checked']) ? explode("\r\n", $meta['checked']) : ''; |
| 311 |
break; |
| 312 |
|
| 313 |
case 'select': |
| 314 |
case 'radio': |
| 315 |
case 'timezone': |
| 316 |
case 'palettes': |
| 317 |
case 'image': |
| 318 |
case 'cropper': |
| 319 |
$default_value = isset($meta['selected']) ? $meta['selected'] : ''; |
| 320 |
break; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
// Stripslashes: default values |
| 325 |
$default_value = ! is_array($default_value) ? stripslashes($default_value) : $default_value; |
| 326 |
|
| 327 |
return apply_filters("wpcomment_field_default_value", $default_value, $meta); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
/** |
| 332 |
* Get PPOM inputs |
| 333 |
*/ |
| 334 |
function get_core_inputs(){ |
| 335 |
|
| 336 |
$coreinputs = array( |
| 337 |
'text', |
| 338 |
'textarea', |
| 339 |
'select', |
| 340 |
'radio', |
| 341 |
'checkbox', |
| 342 |
'email', |
| 343 |
'date', |
| 344 |
'number', |
| 345 |
'hidden', |
| 346 |
'daterange', |
| 347 |
'color', |
| 348 |
'file', |
| 349 |
'image', |
| 350 |
'timezone', |
| 351 |
'section', |
| 352 |
'palettes', |
| 353 |
'divider' |
| 354 |
); |
| 355 |
|
| 356 |
return apply_filters('wpcomment_core_input_types', $coreinputs); |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
// Since 15.1: checking if all meta has unique datanames |
| 361 |
public function has_unique_datanames() { |
| 362 |
|
| 363 |
if( ! self::$fields ) return false; |
| 364 |
|
| 365 |
$has_unique = true; |
| 366 |
$datanames_array = array(); |
| 367 |
|
| 368 |
// wpcomment_pa(self::$fields); |
| 369 |
|
| 370 |
foreach( self::$fields as $field ) { |
| 371 |
|
| 372 |
$type = isset($field['type']) ? $field['type'] : ''; |
| 373 |
|
| 374 |
// ignore collapased fields |
| 375 |
if( $type == 'collapse' ) continue; |
| 376 |
|
| 377 |
if( !isset($field['data_name']) ) { |
| 378 |
$has_unique = false; |
| 379 |
break; |
| 380 |
} |
| 381 |
|
| 382 |
if( in_array($field['data_name'], $datanames_array) ) { |
| 383 |
|
| 384 |
$has_unique = false; |
| 385 |
break; |
| 386 |
} |
| 387 |
|
| 388 |
$datanames_array[] = $field['data_name']; |
| 389 |
|
| 390 |
} |
| 391 |
|
| 392 |
return apply_filters('wpcomment_has_unique_fields', $has_unique, $this); |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
// While rendering fields return attributes for fields |
| 397 |
function get_attributes($field_meta, $type) { |
| 398 |
|
| 399 |
$wpcomment_attribtues = array(); |
| 400 |
|
| 401 |
$wpcomment_attribtues['data-errormsg'] = isset($field_meta['error_message']) ? wpcomment_wpml_translate($field_meta['error_message'], 'PPOM') : null; |
| 402 |
|
| 403 |
switch( $type ) { |
| 404 |
|
| 405 |
case 'text': |
| 406 |
|
| 407 |
$wpcomment_attribtues['maxlength'] = isset($field_meta['maxlength']) ? $field_meta['maxlength'] : null; |
| 408 |
$wpcomment_attribtues['minlength'] = isset($field_meta['minlength']) ? $field_meta['minlength'] : null; |
| 409 |
break; |
| 410 |
|
| 411 |
case 'textarea': |
| 412 |
|
| 413 |
$wpcomment_attribtues['maxlength'] = isset($field_meta['max_length']) ? $field_meta['max_length'] : null; |
| 414 |
break; |
| 415 |
|
| 416 |
|
| 417 |
case 'number': |
| 418 |
|
| 419 |
$wpcomment_attribtues['min'] = isset($field_meta['min']) ? $field_meta['min'] : null; |
| 420 |
$wpcomment_attribtues['max'] = isset($field_meta['max']) ? $field_meta['max'] : null; |
| 421 |
$wpcomment_attribtues['step'] = isset($field_meta['step']) ? $field_meta['step'] : null; |
| 422 |
break; |
| 423 |
|
| 424 |
} |
| 425 |
|
| 426 |
return apply_filters('wpcomment_field_attributes', $wpcomment_attribtues, $field_meta, $type); |
| 427 |
} |
| 428 |
} |