balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-redux-checkbox.php (4330B)


      1 <?php
      2 /**
      3  * Checkbox Field.
      4  *
      5  * @package     ReduxFramework/Fields
      6  * @author      Dovy Paukstys & Kevin Provance (kprovance)
      7  * @version     4.0.0
      8  */
      9 
     10 defined( 'ABSPATH' ) || exit;
     11 
     12 // Don't duplicate me!
     13 if ( ! class_exists( 'Redux_Checkbox', false ) ) {
     14 
     15 	/**
     16 	 * Main Redux_checkbox class
     17 	 *
     18 	 * @since       1.0.0
     19 	 */
     20 	class Redux_Checkbox extends Redux_Field {
     21 
     22 		/**
     23 		 * Field Render Function.
     24 		 * Takes the vars and outputs the HTML for the field in the settings
     25 		 *
     26 		 * @since       1.0.0
     27 		 * @access      public
     28 		 * @return      void
     29 		 */
     30 		public function render() {
     31 			if ( ! empty( $this->field['data'] ) && empty( $this->field['options'] ) ) {
     32 				if ( empty( $this->field['args'] ) ) {
     33 					$this->field['args'] = array();
     34 				}
     35 
     36 				$this->field['options'] = $this->parent->get_wordpress_data( $this->field['data'], $this->field['args'], $this->value );
     37 				if ( empty( $this->field['options'] ) ) {
     38 					return;
     39 				}
     40 			}
     41 
     42 			$this->field['data_class'] = ( isset( $this->field['multi_layout'] ) ) ? 'data-' . $this->field['multi_layout'] : 'data-full';
     43 
     44 			if ( ! empty( $this->field['options'] ) && ( is_array( $this->field['options'] ) || is_array( $this->field['default'] ) ) ) {
     45 
     46 				echo '<ul class="' . esc_attr( $this->field['data_class'] ) . '">';
     47 
     48 				if ( ! isset( $this->value ) ) {
     49 					$this->value = array();
     50 				}
     51 
     52 				if ( ! is_array( $this->value ) ) {
     53 					$this->value = array();
     54 				}
     55 
     56 				if ( empty( $this->field['options'] ) && isset( $this->field['default'] ) && is_array( $this->field['default'] ) ) {
     57 					$this->field['options'] = $this->field['default'];
     58 				}
     59 
     60 				foreach ( $this->field['options'] as $k => $v ) {
     61 
     62 					if ( empty( $this->value[ $k ] ) ) {
     63 						$this->value[ $k ] = '';
     64 					}
     65 
     66 					echo '<li>';
     67 
     68 					$ident_1 = strtr(
     69 						$this->parent->args['opt_name'] . '[' . $this->field['id'] . '][' . $k . ']',
     70 						array(
     71 							'[' => '_',
     72 							']' => '',
     73 						)
     74 					);
     75 
     76 					$ident_2 = array_search( $k, array_keys( $this->field['options'] ), true );
     77 					$id      = $ident_1 . '_' . $ident_2;
     78 
     79 					echo '<label for="' . esc_attr( $id ) . '">';
     80 					echo '<input type="hidden" class="checkbox-check" data-val="1" name="' . esc_attr( $this->field['name'] . '[' . $k . ']' . $this->field['name_suffix'] ) . '" value="' . esc_attr( $this->value[ $k ] ) . '"/>';
     81 					echo '<input type="checkbox" class="checkbox ' . esc_attr( $this->field['class'] ) . '" id="' . esc_attr( $id ) . '" value="1" ' . checked( $this->value[ $k ], '1', false ) . '/>';
     82 					echo ' ' . esc_attr( $v ) . '</label>';
     83 					echo '</li>';
     84 				}
     85 
     86 				echo '</ul>';
     87 			} elseif ( empty( $this->field['data'] ) ) {
     88 				echo '<ul class="data-full">';
     89 				echo '<li>';
     90 
     91 				if ( ! empty( $this->field['label'] ) ) {
     92 					echo '<label>';
     93 				}
     94 
     95 				$ident_1 = strtr(
     96 					$this->parent->args['opt_name'] . '[' . $this->field['id'] . ']',
     97 					array(
     98 						'[' => '_',
     99 						']' => '',
    100 					)
    101 				);
    102 
    103 				// Got the "Checked" status as "0" or "1" then insert it as the "value" option.
    104 				echo '<input type="hidden" class="checkbox-check" data-val="1" name="' . esc_attr( $this->field['name'] . $this->field['name_suffix'] ) . '" value="' . esc_attr( $this->value ) . '"/>';
    105 				echo '<input type="checkbox" id="' . esc_attr( $ident_1 ) . '" value="1" class="checkbox ' . esc_attr( $this->field['class'] ) . '" ' . checked( $this->value, '1', false ) . '/>';
    106 
    107 				if ( ! empty( $this->field['label'] ) ) {
    108 					echo ' ' . esc_html( $this->field['label'] );
    109 					echo '</label>';
    110 				}
    111 
    112 				echo '</li>';
    113 				echo '</ul>';
    114 			}
    115 		}
    116 
    117 		/**
    118 		 * Enqueue Function.
    119 		 * If this field requires any scripts, or css define this function and register/enqueue the scripts/css
    120 		 *
    121 		 * @since       1.0.0
    122 		 * @access      public
    123 		 * @return      void
    124 		 */
    125 		public function enqueue() {
    126 			if ( $this->parent->args['dev_mode'] ) {
    127 				wp_enqueue_style(
    128 					'redux-field-checkbox-css',
    129 					Redux_Core::$url . 'inc/fields/checkbox/redux-checkbox.css',
    130 					array(),
    131 					$this->timestamp
    132 				);
    133 			}
    134 
    135 			wp_enqueue_script(
    136 				'redux-field-checkbox-js',
    137 				Redux_Core::$url . 'inc/fields/checkbox/redux-checkbox' . Redux_Functions::is_min() . '.js',
    138 				array( 'jquery', 'redux-js' ),
    139 				$this->timestamp,
    140 				true
    141 			);
    142 		}
    143 	}
    144 }
    145 
    146 class_alias( 'Redux_Checkbox', 'ReduxFramework_Checkbox' );