| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | if ( ! defined( 'ABSPATH' ) ) { |
| 14: | exit; |
| 15: | } |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | class EPL_FORM_BUILDER { |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | private $prefix = 'epl_form_builder_'; |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | private $text_domain = 'easy-property-listings'; |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | public $form_fields = array(); |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | public $form_sections = array(); |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | private $form_classes = array(); |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | public $configuration = array(); |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | public $has_nonce = false; |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | public $form_attributes = array(); |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | public $nonce_key; |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | public $nonce_value; |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | public function __construct( $config = array() ) { |
| 113: | |
| 114: | $defaults = array( |
| 115: | 'form_tag' => 'on', |
| 116: | 'form_wrap' => 'on', |
| 117: | 'form_context' => 'default', |
| 118: | 'callback_action' => null, |
| 119: | 'is_ajax' => false, |
| 120: | ); |
| 121: | |
| 122: | $this->configuration = shortcode_atts( $defaults, $config ); |
| 123: | |
| 124: | |
| 125: | $this->set_defaults(); |
| 126: | } |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | public function __destruct() { |
| 135: | $this->callbacks(); |
| 136: | } |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | public function __get( $key ) { |
| 146: | return isset( $this->{$key} ) ? $this->{$key} : null; |
| 147: | } |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | public function callbacks() { |
| 155: | |
| 156: | if ( isset( $_REQUEST[ $this->prefix . 'form_submit' ] ) ) { |
| 157: | |
| 158: | |
| 159: | do_action( $this->prefix . 'save_form', get_object_vars( $this ), $_REQUEST ); |
| 160: | |
| 161: | |
| 162: | if ( ! is_null( $this->configuration['callback_action'] ) ) { |
| 163: | do_action( $this->prefix . $this->configuration['callback_action'], get_object_vars( $this ), $_REQUEST ); |
| 164: | } |
| 165: | } |
| 166: | } |
| 167: | |
| 168: | |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | |
| 175: | |
| 176: | public function get_configuration( $key = '' ) { |
| 177: | return isset( $this->configuration[ $key ] ) ? $this->configuration[ $key ] : false; |
| 178: | } |
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | public function get_value( $field ) { |
| 189: | |
| 190: | switch ( $this->configuration['form_context'] ) { |
| 191: | |
| 192: | case 'default': |
| 193: | return isset( $field['value'] ) ? $field['value'] : ( isset( $field['default'] ) ? $field['default'] : '' ); |
| 194: | break; |
| 195: | |
| 196: | case 'meta': |
| 197: | global $post; |
| 198: | return isset( $field['name'] ) ? get_post_meta( $post->ID, $field['name'], true ) : ''; |
| 199: | break; |
| 200: | |
| 201: | case 'settings': |
| 202: | global $epl_settings; |
| 203: | return isset( $field['name'] ) ? $epl_settings[ $field['name'] ] : ''; |
| 204: | break; |
| 205: | |
| 206: | } |
| 207: | |
| 208: | } |
| 209: | |
| 210: | |
| 211: | |
| 212: | |
| 213: | |
| 214: | |
| 215: | public function set_form_classes() { |
| 216: | |
| 217: | $this->form_classes = apply_filters( |
| 218: | $this->prefix . 'form_classes', |
| 219: | array( |
| 220: | 'form_container' => array( |
| 221: | $this->prefix . 'form_container', |
| 222: | 'row', |
| 223: | ), |
| 224: | 'form' => array( |
| 225: | $this->prefix . 'form', |
| 226: | 'col-md-12', |
| 227: | ), |
| 228: | 'field_container' => array( |
| 229: | $this->prefix . 'field_container', |
| 230: | 'col-md-12', |
| 231: | ), |
| 232: | 'field' => array( |
| 233: | $this->prefix . 'field', |
| 234: | 'col-md-12', |
| 235: | ), |
| 236: | 'label_container' => array( |
| 237: | $this->prefix . 'label_container', |
| 238: | 'col-md-12', |
| 239: | ), |
| 240: | 'label' => array( |
| 241: | $this->prefix . 'label', |
| 242: | 'col-md-12', |
| 243: | ), |
| 244: | ), |
| 245: | $this->form_attributes |
| 246: | ); |
| 247: | } |
| 248: | |
| 249: | |
| 250: | |
| 251: | |
| 252: | |
| 253: | |
| 254: | public function invalid_attributes() { |
| 255: | return apply_filters( |
| 256: | $this->prefix . 'invalid_attributes', |
| 257: | array( |
| 258: | 'label' => array( 'all' ), |
| 259: | 'default' => array( 'all' ), |
| 260: | 'opts' => array( 'all' ), |
| 261: | ) |
| 262: | ); |
| 263: | } |
| 264: | |
| 265: | |
| 266: | |
| 267: | |
| 268: | |
| 269: | |
| 270: | |
| 271: | |
| 272: | |
| 273: | |
| 274: | public function get_class( $key = '', $field = array() ) { |
| 275: | |
| 276: | if ( isset( $this->form_classes[ $key ] ) ) { |
| 277: | $classes = implode( ' ', array_map( 'sanitize_html_class', $this->form_classes[ $key ] ) ); |
| 278: | if ( isset( $field['type'] ) ) { |
| 279: | $classes .= ' ' . $this->prefix . $key . '_' . $field['type']; |
| 280: | } |
| 281: | |
| 282: | if ( 'field' === $key && isset( $field['class'] ) ) { |
| 283: | |
| 284: | if ( is_array( $field['class'] ) && count( $field['class'] ) > 0 ) { |
| 285: | $classes .= implode( ' ', array_map( 'sanitize_html_class', $field['class'] ) ); |
| 286: | } else { |
| 287: | $classes .= ' ' . $field['class']; |
| 288: | } |
| 289: | } |
| 290: | return $classes; |
| 291: | } |
| 292: | } |
| 293: | |
| 294: | |
| 295: | |
| 296: | |
| 297: | |
| 298: | |
| 299: | |
| 300: | |
| 301: | |
| 302: | public function get_attributes( $field ) { |
| 303: | |
| 304: | $html = ''; |
| 305: | $field['id'] = isset( $field['id'] ) ? $field['id'] : ''; |
| 306: | |
| 307: | if ( empty( $field['id'] ) ) { |
| 308: | $field['id'] = isset( $field['name'] ) ? epl_sanitize_key( $field['name'] ) : ''; |
| 309: | } |
| 310: | |
| 311: | $invalid_attributes = $this->invalid_attributes(); |
| 312: | |
| 313: | foreach ( $field as $key => $value ) { |
| 314: | |
| 315: | if ( isset( $invalid_attributes[ $key ] ) && ( in_array( $field['type'], $invalid_attributes[ $key ], true ) || in_array( 'all', $invalid_attributes[ $key ], true ) ) ) { |
| 316: | |
| 317: | } else { |
| 318: | |
| 319: | if ( 'name' === $key ) { |
| 320: | $value = esc_attr( $value ); |
| 321: | } |
| 322: | if ( isset( $field['multiple'] ) && in_array( $field['type'], array( 'select', 'checkbox' ), true ) && 'name' === $key ) { |
| 323: | $value = $value . '[]'; |
| 324: | } |
| 325: | $value = is_array( $value ) ? implode( ',', $value ) : $value; |
| 326: | $html .= $key . '="' . $value . '" '; |
| 327: | } |
| 328: | } |
| 329: | return $html; |
| 330: | } |
| 331: | |
| 332: | |
| 333: | |
| 334: | |
| 335: | |
| 336: | |
| 337: | |
| 338: | |
| 339: | |
| 340: | public function set_form_attributes( $key = '', $value = '' ) { |
| 341: | |
| 342: | |
| 343: | if ( ! empty( $key ) && is_string( $key ) ) { |
| 344: | $this->form_attributes[ $this->escape( 'text', $key ) ] = $this->escape( 'attribute', $value ); |
| 345: | |
| 346: | } elseif ( is_array( $key ) & ! empty( $key ) ) { |
| 347: | |
| 348: | |
| 349: | foreach ( $key as $index => $val ) { |
| 350: | $this->form_attributes[ $this->escape( 'text', $index ) ] = $this->escape( 'attribute', $val ); |
| 351: | } |
| 352: | } |
| 353: | } |
| 354: | |
| 355: | |
| 356: | |
| 357: | |
| 358: | |
| 359: | |
| 360: | private function set_default_form_attributes() { |
| 361: | |
| 362: | $this->form_attributes = apply_filters( |
| 363: | $this->prefix . 'form_default_attributes', |
| 364: | array( |
| 365: | 'action' => '', |
| 366: | 'id' => '', |
| 367: | 'class' => '', |
| 368: | 'enctype' => '', |
| 369: | 'method' => 'POST', |
| 370: | 'name' => '', |
| 371: | ) |
| 372: | ); |
| 373: | |
| 374: | if ( true === $this->configuration['is_ajax'] ) { |
| 375: | $this->form_attributes[ 'data-' . $this->prefix . 'ajax_submit' ] = 'true'; |
| 376: | } |
| 377: | } |
| 378: | |
| 379: | |
| 380: | |
| 381: | |
| 382: | |
| 383: | |
| 384: | private function set_defaults() { |
| 385: | |
| 386: | do_action( $this->prefix . 'before_setup_defaults' ); |
| 387: | $this->set_default_form_attributes(); |
| 388: | $this->set_form_classes(); |
| 389: | do_action( $this->prefix . 'after_setup_defaults' ); |
| 390: | } |
| 391: | |
| 392: | |
| 393: | |
| 394: | |
| 395: | |
| 396: | |
| 397: | |
| 398: | |
| 399: | |
| 400: | |
| 401: | private function escape( $type = '', $value = '' ) { |
| 402: | |
| 403: | switch ( $type ) { |
| 404: | |
| 405: | case 'url': |
| 406: | return esc_url( $value ); |
| 407: | break; |
| 408: | |
| 409: | case 'class': |
| 410: | case 'id': |
| 411: | return sanitize_html_class( $value ); |
| 412: | break; |
| 413: | |
| 414: | case 'text': |
| 415: | return sanitize_text_field( $value ); |
| 416: | break; |
| 417: | case 'attribute': |
| 418: | return esc_attr( $value ); |
| 419: | } |
| 420: | } |
| 421: | |
| 422: | |
| 423: | |
| 424: | |
| 425: | |
| 426: | |
| 427: | |
| 428: | public function add_sections( $sections = array() ) { |
| 429: | |
| 430: | foreach ( $sections as $section ) { |
| 431: | $this->add_section( $section ); |
| 432: | } |
| 433: | } |
| 434: | |
| 435: | |
| 436: | |
| 437: | |
| 438: | |
| 439: | |
| 440: | |
| 441: | public function add_section( $section = array() ) { |
| 442: | $this->form_sections[] = $section; |
| 443: | } |
| 444: | |
| 445: | |
| 446: | |
| 447: | |
| 448: | |
| 449: | |
| 450: | |
| 451: | public function add_field( $field = array() ) { |
| 452: | foreach ( $field as $key => &$val ) { |
| 453: | if ( is_string( $val ) ) { |
| 454: | $val = esc_attr( $val ); |
| 455: | } |
| 456: | } |
| 457: | $this->form_fields[] = $field; |
| 458: | } |
| 459: | |
| 460: | |
| 461: | |
| 462: | |
| 463: | |
| 464: | |
| 465: | |
| 466: | public function add_fields( $fields = array() ) { |
| 467: | |
| 468: | foreach ( $fields as $field ) { |
| 469: | $this->add_field( $field ); |
| 470: | } |
| 471: | |
| 472: | $this->add_field( |
| 473: | array( |
| 474: | 'type' => 'hidden', |
| 475: | 'name' => $this->prefix . 'form_submit', |
| 476: | 'value' => 'true', |
| 477: | ) |
| 478: | ); |
| 479: | |
| 480: | if ( ! is_null( $this->configuration['callback_action'] ) ) { |
| 481: | $this->add_field( |
| 482: | array( |
| 483: | 'type' => 'hidden', |
| 484: | 'name' => $this->prefix . 'form_action', |
| 485: | 'value' => $this->prefix . $this->configuration['callback_action'], |
| 486: | ) |
| 487: | ); |
| 488: | } |
| 489: | } |
| 490: | |
| 491: | |
| 492: | |
| 493: | |
| 494: | |
| 495: | |
| 496: | public function render_form() { |
| 497: | |
| 498: | ob_start(); |
| 499: | |
| 500: | if ( 'on' === $this->get_configuration( 'form_wrap' ) ) { |
| 501: | do_action( 'before_' . $this->prefix . 'form_container_open' ); |
| 502: | $this->render_form_container_open(); |
| 503: | } |
| 504: | |
| 505: | if ( 'on' === $this->get_configuration( 'form_tag' ) ) { |
| 506: | do_action( 'before_' . $this->prefix . 'form_open' ); |
| 507: | $this->render_form_open(); |
| 508: | } |
| 509: | |
| 510: | do_action( 'before_' . $this->prefix . 'form_sections' ); |
| 511: | $this->render_sections(); |
| 512: | do_action( 'after_' . $this->prefix . 'form_sections' ); |
| 513: | |
| 514: | do_action( 'before_' . $this->prefix . 'form_fields' ); |
| 515: | $this->render_fields(); |
| 516: | do_action( 'after_' . $this->prefix . 'form_fields' ); |
| 517: | |
| 518: | if ( 'on' === $this->get_configuration( 'form_tag' ) ) { |
| 519: | $this->render_form_close(); |
| 520: | do_action( 'after_' . $this->prefix . 'form_close' ); |
| 521: | } |
| 522: | |
| 523: | if ( 'on' === $this->get_configuration( 'form_wrap' ) ) { |
| 524: | $this->render_form_container_close(); |
| 525: | do_action( 'after_' . $this->prefix . 'form_container_close' ); |
| 526: | } |
| 527: | |
| 528: | echo ob_get_clean(); |
| 529: | } |
| 530: | |
| 531: | |
| 532: | |
| 533: | |
| 534: | |
| 535: | |
| 536: | public function render_form_container_open() { |
| 537: | |
| 538: | $html = "\n<div "; |
| 539: | $html .= 'class ="' . esc_attr( $this->get_class( 'form_container' ) ) . '" '; |
| 540: | $html .= '>'; |
| 541: | echo apply_filters( $this->prefix . 'form_container_open_tag', $html, $this->form_attributes ); |
| 542: | } |
| 543: | |
| 544: | |
| 545: | |
| 546: | |
| 547: | |
| 548: | |
| 549: | public function render_form_container_close() { |
| 550: | |
| 551: | echo apply_filters( $this->prefix . 'form_container_close_tag', "\n</div>", $this->form_attributes ); |
| 552: | } |
| 553: | |
| 554: | |
| 555: | |
| 556: | |
| 557: | |
| 558: | |
| 559: | public function render_form_open() { |
| 560: | |
| 561: | $html = "\n<form "; |
| 562: | foreach ( $this->form_attributes as $key => $value ) { |
| 563: | $html .= $key . '="' . $value . '" '; |
| 564: | } |
| 565: | $html .= ' >'; |
| 566: | echo apply_filters( $this->prefix . 'form_open_tag', $html, $this->form_attributes ); |
| 567: | } |
| 568: | |
| 569: | |
| 570: | |
| 571: | |
| 572: | |
| 573: | |
| 574: | public function render_form_close() { |
| 575: | |
| 576: | echo apply_filters( $this->prefix . 'form_close_tag', "\n</form>", $this->form_attributes ); |
| 577: | } |
| 578: | |
| 579: | |
| 580: | |
| 581: | |
| 582: | |
| 583: | |
| 584: | private function render_sections() { |
| 585: | |
| 586: | if ( ! empty( $this->form_sections ) ) { |
| 587: | foreach ( $this->form_sections as $section ) { |
| 588: | $this->render_section( $section ); |
| 589: | |
| 590: | } |
| 591: | } |
| 592: | } |
| 593: | |
| 594: | |
| 595: | |
| 596: | |
| 597: | |
| 598: | |
| 599: | |
| 600: | private function render_section( $section ) { |
| 601: | |
| 602: | $section_class = $this->prefix . 'form_section ' . isset( $section['class'] ) ? $section['class'] : ''; |
| 603: | $section_id = isset( $section['id'] ) ? $section['id'] : ''; |
| 604: | $section_label = isset( $section['label'] ) ? $section['label'] : ''; |
| 605: | $section_help = isset( $section['help'] ) ? $section['help'] : ''; ?> |
| 606: | |
| 607: | <div id="<?php echo esc_attr( $section_id ); ?>" class="<?php echo esc_attr( $section_class ); ?>" > |
| 608: | |
| 609: | <span class="<?php echo esc_attr( $this->prefix . 'section_label' ); ?>"> |
| 610: | <?php |
| 611: | echo esc_attr( $section_label ); |
| 612: | ?> |
| 613: | </span> |
| 614: | |
| 615: | <span class="<?php echo esc_attr( $this->prefix . 'section_help' ); ?>"> |
| 616: | <?php |
| 617: | echo wp_kses_post( $section_help ); |
| 618: | ?> |
| 619: | </span> |
| 620: | |
| 621: | <div class="<?php echo esc_attr( $this->prefix . 'section_fields' ); ?>"> |
| 622: | <?php |
| 623: | if ( ! empty( $section['fields'] ) ) { |
| 624: | foreach ( $section['fields'] as $field ) { |
| 625: | $this->render_field( $field ); |
| 626: | |
| 627: | } |
| 628: | } |
| 629: | ?> |
| 630: | </div> |
| 631: | |
| 632: | </div> |
| 633: | <?php |
| 634: | } |
| 635: | |
| 636: | |
| 637: | |
| 638: | |
| 639: | |
| 640: | |
| 641: | private function render_fields() { |
| 642: | |
| 643: | if ( ! empty( $this->form_fields ) ) { |
| 644: | foreach ( $this->form_fields as $field ) { |
| 645: | $this->render_field( $field ); |
| 646: | |
| 647: | } |
| 648: | } |
| 649: | } |
| 650: | |
| 651: | |
| 652: | |
| 653: | |
| 654: | |
| 655: | |
| 656: | |
| 657: | private function render_field( $field ) { |
| 658: | |
| 659: | $this->render_field_container_open(); |
| 660: | $this->render_field_label( $field ); |
| 661: | |
| 662: | switch ( $field['type'] ) { |
| 663: | |
| 664: | case 'text': |
| 665: | case 'url': |
| 666: | case 'number': |
| 667: | case 'tel': |
| 668: | case 'hidden': |
| 669: | $this->render_text( $field ); |
| 670: | break; |
| 671: | case 'textarea': |
| 672: | $this->render_textarea( $field ); |
| 673: | break; |
| 674: | case 'radio': |
| 675: | $this->render_radio( $field ); |
| 676: | break; |
| 677: | case 'select': |
| 678: | case 'select_single': |
| 679: | $this->render_select( $field ); |
| 680: | break; |
| 681: | case 'checkbox': |
| 682: | case 'checkbox_single': |
| 683: | $this->render_checkbox( $field ); |
| 684: | break; |
| 685: | case 'wp_editor': |
| 686: | $this->render_wp_editor( $field ); |
| 687: | break; |
| 688: | |
| 689: | default: |
| 690: | $this->render_text( $field ); |
| 691: | break; |
| 692: | |
| 693: | } |
| 694: | $this->render_field_container_close(); |
| 695: | } |
| 696: | |
| 697: | |
| 698: | |
| 699: | |
| 700: | |
| 701: | |
| 702: | public function render_field_container_open() { |
| 703: | |
| 704: | $html = "\n<div "; |
| 705: | $html .= 'class ="' . $this->get_class( 'field_container' ) . '" '; |
| 706: | $html .= '>'; |
| 707: | echo apply_filters( $this->prefix . 'field_container_open_tag', $html, $this->form_attributes ); |
| 708: | } |
| 709: | |
| 710: | |
| 711: | |
| 712: | |
| 713: | |
| 714: | |
| 715: | public function render_field_container_close() { |
| 716: | |
| 717: | echo apply_filters( $this->prefix . 'field_container_close_tag', "\n</div>", $this->form_attributes ); |
| 718: | } |
| 719: | |
| 720: | |
| 721: | |
| 722: | |
| 723: | |
| 724: | |
| 725: | |
| 726: | public function render_field_label( $field = array() ) { |
| 727: | |
| 728: | $wrapper = "\n<span "; |
| 729: | $wrapper .= 'class ="' . $this->get_class( 'label_container', $field ) . '" '; |
| 730: | $wrapper .= '>'; |
| 731: | |
| 732: | if ( isset( $field['label'] ) ) { |
| 733: | |
| 734: | $html = "\n<label "; |
| 735: | $html .= 'class ="' . $this->get_class( 'label', $field ) . '" '; |
| 736: | $html .= '>'; |
| 737: | $html .= apply_filters( $this->prefix . 'field_label', $field['label'], $field ); |
| 738: | $html .= "\n</label>"; |
| 739: | $wrapper .= $html; |
| 740: | } |
| 741: | |
| 742: | $wrapper .= "\n</span>"; |
| 743: | |
| 744: | echo apply_filters( $this->prefix . 'field_label_html', $wrapper, $field ); |
| 745: | } |
| 746: | |
| 747: | |
| 748: | |
| 749: | |
| 750: | |
| 751: | |
| 752: | |
| 753: | private function render_text( $field ) { |
| 754: | |
| 755: | $field['value'] = esc_attr( stripslashes( $this->get_value( $field ) ) ); |
| 756: | $field['class'] = $this->get_class( 'field', $field ); |
| 757: | $html = "\n<input "; |
| 758: | $html .= $this->get_attributes( $field ); |
| 759: | $html .= ' />'; |
| 760: | echo apply_filters( $this->prefix . 'form_' . $field['type'] . '_tag', $html, $field ); |
| 761: | } |
| 762: | |
| 763: | |
| 764: | |
| 765: | |
| 766: | |
| 767: | |
| 768: | |
| 769: | private function render_textarea( $field ) { |
| 770: | |
| 771: | $value = $this->get_value( $field ); |
| 772: | $field['class'] = $this->get_class( 'field', $field ); |
| 773: | $html = "\n<textarea "; |
| 774: | $html .= $this->get_attributes( $field ); |
| 775: | $html .= '>'; |
| 776: | $html .= esc_attr( $value ); |
| 777: | $html .= '</textarea>'; |
| 778: | echo apply_filters( $this->prefix . 'form_' . $field['type'] . '_tag', $html, $field ); |
| 779: | } |
| 780: | |
| 781: | |
| 782: | |
| 783: | |
| 784: | |
| 785: | |
| 786: | |
| 787: | private function render_radio( $field ) { |
| 788: | |
| 789: | $options = $field['opts']; |
| 790: | $value = $this->get_value( $field ); |
| 791: | |
| 792: | unset( $field['opts'] ); |
| 793: | |
| 794: | foreach ( $options as $option => $label ) { |
| 795: | |
| 796: | if ( $option === $value ) { |
| 797: | $field['checked'] = 'checked'; |
| 798: | } |
| 799: | |
| 800: | $field['value'] = esc_attr( stripslashes( $option ) ); |
| 801: | $field['class'] = $this->get_class( 'field', $field ); |
| 802: | $html = "\n<input "; |
| 803: | $html .= $this->get_attributes( $field ); |
| 804: | $html .= ' >' . $label; |
| 805: | echo apply_filters( $this->prefix . 'form_' . $field['type'] . '_tag', $html, $field ); |
| 806: | unset( $field['checked'] ); |
| 807: | } |
| 808: | } |
| 809: | |
| 810: | |
| 811: | |
| 812: | |
| 813: | |
| 814: | |
| 815: | |
| 816: | private function render_checkbox( $field ) { |
| 817: | |
| 818: | $options = $field['opts']; |
| 819: | $value = $this->get_value( $field ); |
| 820: | |
| 821: | unset( $field['opts'] ); |
| 822: | |
| 823: | foreach ( $options as $option => $label ) { |
| 824: | |
| 825: | if ( $option === $value ) { |
| 826: | $field['checked'] = 'checked'; |
| 827: | } |
| 828: | |
| 829: | $field['value'] = esc_attr( stripslashes( $option ) ); |
| 830: | $field['class'] = $this->get_class( 'field', $field ); |
| 831: | $html = "\n<input "; |
| 832: | $html .= $this->get_attributes( $field ); |
| 833: | $html .= ' >' . $label; |
| 834: | echo apply_filters( $this->prefix . 'form_' . $field['type'] . '_tag', $html, $field ); |
| 835: | unset( $field['checked'] ); |
| 836: | } |
| 837: | } |
| 838: | |
| 839: | |
| 840: | |
| 841: | |
| 842: | |
| 843: | |
| 844: | |
| 845: | private function render_wp_editor( $field ) { |
| 846: | $value = stripslashes( $this->get_value( $field ) ); |
| 847: | $field['class'] = $this->get_class( 'field', $field ); |
| 848: | wp_editor( $value, $field['name'] ); |
| 849: | } |
| 850: | |
| 851: | |
| 852: | |
| 853: | |
| 854: | |
| 855: | |
| 856: | |
| 857: | private function render_select( $field ) { |
| 858: | global $post; |
| 859: | $options = $field['opts']; |
| 860: | unset( $field['opts'] ); |
| 861: | |
| 862: | $value = $this->get_value( $field ); |
| 863: | $field['class'] = $this->get_class( 'field', $field ); |
| 864: | $html = "\n<select "; |
| 865: | $html .= $this->get_attributes( $field ); |
| 866: | $html .= ' >'; |
| 867: | $value = (array) $value; |
| 868: | $value = array_map( 'sanitize_text_field', $value ); |
| 869: | |
| 870: | foreach ( $options as $option => $label ) { |
| 871: | |
| 872: | if ( is_array( $label ) ) { |
| 873: | if ( isset( $label['exclude'] ) && ! empty( $label['exclude'] ) ) { |
| 874: | if ( in_array( $post->post_type, $label['exclude'], true ) ) { |
| 875: | continue; |
| 876: | } |
| 877: | } |
| 878: | |
| 879: | if ( isset( $label['include'] ) && ! empty( $label['include'] ) ) { |
| 880: | if ( ! in_array( $post->post_type, $label['include'], true ) ) { |
| 881: | continue; |
| 882: | } |
| 883: | } |
| 884: | $label = $label['label']; |
| 885: | } |
| 886: | $html .= "\n<option "; |
| 887: | $html .= 'value = "' . stripslashes( $option ) . '"'; |
| 888: | |
| 889: | if ( in_array( $option, $value, true ) ) { |
| 890: | $html .= ' selected = "selected" '; |
| 891: | } |
| 892: | |
| 893: | $html .= ' >' . $label . "\n</option>"; |
| 894: | |
| 895: | } |
| 896: | $html .= "\n</select>"; |
| 897: | echo apply_filters( $this->prefix . 'form_' . $field['type'] . '_tag', $html, $field ); |
| 898: | } |
| 899: | |
| 900: | |
| 901: | |
| 902: | |
| 903: | |
| 904: | |
| 905: | |
| 906: | public function add_nonce( $action = '' ) { |
| 907: | |
| 908: | $this->has_nonce = true; |
| 909: | $this->nonce_key = ! empty( $action ) ? $action : $this->prefix . 'nonce_action'; |
| 910: | $this->nonce_value = wp_create_nonce( $action ); |
| 911: | |
| 912: | $this->add_field( |
| 913: | array( |
| 914: | 'type' => 'hidden', |
| 915: | 'name' => $this->nonce_key, |
| 916: | 'value' => $this->nonce_value, |
| 917: | ) |
| 918: | ); |
| 919: | } |
| 920: | } |
| 921: | |