diff --git a/inc/class-alpha-color-control.php b/inc/class-alpha-color-control.php new file mode 100644 index 0000000..0589091 --- /dev/null +++ b/inc/class-alpha-color-control.php @@ -0,0 +1,105 @@ +. + */ +class Alpha_Color_Control extends WP_Customize_Control { + + /** + * Official control name. + * + * @var string + */ + public $type = 'alpha-color'; + + /** + * Add support for palettes to be passed in. + * + * Supported palette values are true, false, or an array of RGBa and Hex colors. + * + * @var bool|array + */ + public $palette; + + /** + * Add support for showing the opacity value on the slider handle. + * + * @var int + */ + public $show_opacity; + + /** + * Enqueue scripts and styles. + * + * Ideally these would get registered and given proper paths before this control object + * gets initialized, then we could simply enqueue them here, but for completeness as a + * stand alone class we'll register and enqueue them here. + */ + public function enqueue() { + + } + + /** + * Render the control. + */ + public function render_content() { + + // Process the palette. + if ( is_array( $this->palette ) ) { + $palette = implode( '|', $this->palette ); + } else { + // Default to true. + $palette = ( false === $this->palette || 'false' === $this->palette ) ? 'false' : 'true'; + } + + // Support passing show_opacity as string or boolean. Default to true. + $show_opacity = ( false === $this->show_opacity || 'false' === $this->show_opacity ) ? 'false' : 'true'; + + // Begin the output. + ?> + + $op ) { + $args['fields'][ $key ]['id'] = $key; + if ( ! isset( $op['value'] ) ) { + if ( isset( $op['default'] ) ) { + $args['fields'][ $key ]['value'] = $op['default']; + } else { + $args['fields'][ $key ]['value'] = ''; + } + } + } + + $this->fields = $args['fields']; + $this->live_title_id = isset( $args['live_title_id'] ) ? $args['live_title_id'] : false; + $this->defined_values = isset( $args['defined_values'] ) ? $args['defined_values'] : false; + $this->id_key = isset( $args['id_key'] ) ? $args['id_key'] : false; + if ( isset( $args['title_format'] ) && '' !== $args['title_format'] ) { + $this->title_format = $args['title_format']; + } else { + $this->title_format = ''; + } + + if ( isset( $args['limited_msg'] ) && '' !== $args['limited_msg'] ) { + $this->limited_msg = $args['limited_msg']; + } else { + $this->limited_msg = ''; + } + + if ( ! isset( $args['max_item'] ) ) { + $args['max_item'] = 0; + } + + if ( ! isset( $args['allow_unlimited'] ) || false !== $args['allow_unlimited'] ) { + $this->max_item = apply_filters( 'coletivo_reepeatable_max_item', absint( $args['max_item'] ) ); + } else { + $this->max_item = absint( $args['max_item'] ); + } + + $this->changeable = isset( $args['changeable'] ) && 'no' === $args['changeable'] ? 'no' : 'yes'; + $this->default_empty_title = isset( $args['default_empty_title'] ) && '' !== $args['default_empty_title'] ? $args['default_empty_title'] : esc_html__( 'Item', 'coletivo' ); + } + + /** + * Merge data + * + * @access public + * @param array $array_value Array value. + * @param array $array_default Array value. + * + * @return array + */ + public function merge_data( $array_value, $array_default ) { + + if ( ! $this->id_key ) { + return $array_value; + } + + if ( ! is_array( $array_value ) ) { + $array_value = array(); + } + + if ( ! is_array( $array_default ) ) { + $array_default = array(); + } + + $new_array = array(); + foreach ( $array_value as $k => $a ) { + + if ( is_array( $a ) ) { + if ( isset( $a[ $this->id_key ] ) && '' !== $a[ $this->id_key ] ) { + $new_array[ $a[ $this->id_key ] ] = $a; + } else { + $new_array[ $k ] = $a; + } + } + } + + foreach ( $array_default as $k => $a ) { + if ( is_array( $a ) && isset( $a[ $this->id_key ] ) ) { + if ( ! isset( $new_array[ $a[ $this->id_key ] ] ) ) { + $new_array[ $a[ $this->id_key ] ] = $a; + } + } + } + + return array_values( $new_array ); + } + + /** + * To JSON + * + * @access public + * + * @return void + */ + public function to_json() { + parent::to_json(); + $value = $this->value(); + + if ( is_string( $value ) ) { + $value = json_decode( $value, true ); + } + if ( empty( $value ) ) { + $value = $this->defined_values; + } elseif ( is_array( $this->defined_values ) && ! empty( $this->defined_values ) ) { + $value = $this->merge_data( $value, $this->defined_values ); + } + + $this->json['live_title_id'] = $this->live_title_id; + $this->json['title_format'] = $this->title_format; + $this->json['max_item'] = $this->max_item; + $this->json['limited_msg'] = $this->limited_msg; + $this->json['changeable'] = $this->changeable; + $this->json['default_empty_title'] = $this->default_empty_title; + $this->json['value'] = $value; + $this->json['id_key'] = $this->id_key; + $this->json['fields'] = $this->fields; + } + + /** + * Enqueue scripts/styles. + * + * @since 1.0.0 + * @access public + * + * @return void + */ + public function enqueue() { + add_action( 'customize_controls_print_footer_scripts', array( $this, 'item_tpl' ), 66 ); + } + + /** + * Item TPL + * + * @access public + * + * @return void + */ + public function item_tpl() { + ?> + + + + input_attrs(); ?> value="" link(); ?> /> +