get_rgba() public static méthode

Gets the rgba value of the $color.
public static get_rgba ( string $color = '#fff', integer | float $alpha = 1 ) : string
$color string The hex value of a color.
$alpha integer | float Opacity level (0-1).
Résultat string
 public function test()
 {
     $this->assertEquals(kirki_get_option(), Kirki::get_option());
     $this->assertEquals(kirki_sanitize_hex('#ffffff'), Kirki_Color::sanitize_hex('#ffffff'));
     $this->assertEquals(kirki_get_rgb('#ffffff'), Kirki_Color::get_rgb('#ffffff'));
     $this->assertEquals(kirki_get_rgba('#ffffff'), Kirki_Color::get_rgba('#ffffff'));
     $this->assertEquals(kirki_get_brightness('#ffffff'), Kirki_Color::get_brightness('#ffffff'));
     $font_registry = Kirki_Toolkit::fonts();
     $this->assertEquals(Kirki_Fonts::get_all_fonts(), $font_registry->get_all_fonts());
     $this->assertEquals(Kirki_Fonts::get_font_choices(), $font_registry->get_font_choices());
     $this->assertEquals(Kirki_Fonts::is_google_font('foo'), $font_registry->is_google_font('foo'));
     $this->assertEquals(Kirki_Fonts::get_google_font_uri(array('foo')), $font_registry->get_google_font_uri(array('foo')));
     $this->assertEquals(Kirki_Fonts::get_google_font_subsets(), $font_registry->get_google_font_subsets());
     $this->assertEquals(Kirki_Fonts::choose_google_font_variants('Roboto'), $font_registry->choose_google_font_variants('Roboto'));
     $this->assertEquals(Kirki_Fonts::get_standard_fonts(), $font_registry->get_standard_fonts());
     $this->assertEquals(Kirki_Fonts::get_font_stack('foo'), $font_registry->get_font_stack('foo'));
     $this->assertEquals(Kirki_Fonts::sanitize_font_choice('foo'), $font_registry->sanitize_font_choice('foo'));
     $this->assertEquals(Kirki_Fonts::get_google_fonts(), $font_registry->get_google_fonts());
 }
 /**
  * Get the styles for a single field.
  */
 public function setting_styles($field, $styles = '', $element = '', $property = '', $units = '', $prefix = '', $suffix = '', $callback = false)
 {
     $value = kirki_get_option($field['settings_raw']);
     $value = $callback ? call_user_func($callback, $value) : $value;
     $element = $prefix . $element;
     $units = $units . $suffix;
     // Color controls
     if ('color' == $field['type']) {
         $color = Kirki_Color::sanitize_hex($value);
         $styles[$element][$property] = $color . $units;
     } elseif ('background' == $field['type']) {
         if (isset($field['default']['color'])) {
             $color_mode = false !== strpos($field['default']['color'], 'rgba') ? 'color-alpha' : 'color';
             $value = kirki_get_option($field['settings_raw'] . '_color');
             if ('color-alpha' == $color_mode) {
                 $bg_color = kirki_sanitize_rgba($value);
             } else {
                 $bg_color = Kirki_Color::sanitize_hex($value);
             }
         }
         if (isset($field['default']['image'])) {
             $bg_image = kirki_get_option($field['settings_raw'] . '_image');
             $bg_image = esc_url_raw($bg_image);
         }
         if (isset($field['default']['repeat'])) {
             $bg_repeat = kirki_get_option($field['settings_raw'] . '_repeat');
             $bg_repeat = kirki_sanitize_bg_repeat($bg_repeat);
         }
         if (isset($field['default']['size'])) {
             $bg_size = kirki_get_option($field['settings_raw'] . '_size');
             $bg_size = kirki_sanitize_bg_size($bg_size);
         }
         if (isset($field['default']['attach'])) {
             $bg_attach = kirki_get_option($field['settings_raw'] . '_attach');
             $bg_attach = kirki_sanitize_bg_attach($bg_attach);
         }
         if (isset($field['default']['position'])) {
             $bg_position = kirki_get_option($field['settings_raw'] . '_position');
             $bg_position = kirki_sanitize_bg_position($bg_position);
         }
         if (isset($field['default']['opacity']) && $field['default']['opacity']) {
             $bg_opacity = kirki_get_option($field['settings_raw'] . '_opacity');
             $bg_opacity = kirki_sanitize_number($bg_opacity);
             if (isset($bg_color)) {
                 // If we're using an opacity other than 100, then convert the color to RGBA.
                 $bg_color = 100 != $bg_opacity ? Kirki_Color::get_rgba($bg_color, $bg_opacity) : $bg_color;
             } elseif (isset($bg_image)) {
                 $element_opacity = $bg_opacity / 100;
             }
         }
         if (isset($bg_color)) {
             $styles[$element]['background-color'] = $bg_color . $units;
         }
         if (isset($bg_image) && '' != $bg_image) {
             $styles[$element]['background-image'] = 'url("' . $bg_image . '")' . $units;
             if (isset($bg_repeat)) {
                 $styles[$element]['background-repeat'] = $bg_repeat . $units;
             }
             if (isset($bg_size)) {
                 $styles[$element]['background-size'] = $bg_size . $units;
             }
             if (isset($bg_attach)) {
                 $styles[$element]['background-attachment'] = $bg_attach . $units;
             }
             if (isset($bg_position)) {
                 $styles[$element]['background-position'] = str_replace('-', ' ', $bg_position) . $units;
             }
         }
     } elseif (array($field['output']) && isset($field['output']['property']) && in_array($field['output']['property'], array('font-family', 'font-size', 'font-weight'))) {
         $is_font_family = isset($field['output']['property']) && 'font-family' == $field['output']['property'] ? true : false;
         $is_font_size = isset($field['output']['property']) && 'font-size' == $field['output']['property'] ? true : false;
         $is_font_weight = isset($field['output']['property']) && 'font-weight' == $field['output']['property'] ? true : false;
         if ('font-family' == $property) {
             $styles[$field['output']['element']]['font-family'] = $value . $units;
         } else {
             if ('font-size' == $property) {
                 // Get the unit we're going to use for the font-size.
                 $units = empty($units) ? 'px' : $units;
                 $styles[$element]['font-size'] = $value . $units;
             } else {
                 if ('font-weight' == $property) {
                     $styles[$element]['font-weight'] = $value . $units;
                 }
             }
         }
     } else {
         $styles[$element][$property] = $value . $units;
     }
     return $styles;
 }
 /**
  * Build the background fields.
  * Takes a single field with type = background and explodes it to multiple controls.
  *
  * @param array
  * @return null|array
  */
 public static function explode($field)
 {
     $i18n = Kirki_Toolkit::i18n();
     $choices = self::background_choices();
     // Early exit if this is not a background field.
     if ('background' != $field['type']) {
         return;
     }
     // Sanitize field
     $field = Kirki_Field_Sanitize::sanitize_field($field);
     // No need to proceed any further if no defaults have been set.
     // We build the array of fields based on what default values have been defined.
     if (!isset($field['default']) || !is_array($field['default'])) {
         return;
     }
     $fields = array();
     $i = 0;
     foreach ($field['default'] as $key => $value) {
         // No need to process the opacity, it is factored in the color control.
         if ('opacity' == $key) {
             continue;
         }
         $key = esc_attr($key);
         $setting = $key;
         $help = $field['help'];
         $description = isset($i18n['background-' . $key]) ? $i18n['background-' . $key] : '';
         $output_property = 'background-' . $key;
         $label = 0 === $i ? $field['label'] : '';
         $type = 'select';
         $sanitize_callback = 'esc_attr';
         switch ($key) {
             case 'color':
                 /**
                  * Use 'color-alpha' instead of 'color' if default is an rgba value
                  * or if 'opacity' is set.
                  */
                 $type = false !== strpos($field['default']['color'], 'rgba') ? 'color-alpha' : 'color';
                 $type = isset($field['default']['opacity']) ? 'color-alpha' : $type;
                 if (isset($field['default']['opacity']) && false === strpos($value, 'rgb')) {
                     $value = Kirki_Color::get_rgba($value, $field['default']['opacity']);
                 }
                 $sanitize_callback = array('Kirki_Sanitize_Values', 'color');
                 break;
             case 'image':
                 $type = 'image';
                 $sanitize_callback = 'esc_url_raw';
                 break;
             case 'attach':
                 /**
                  * Small hack so that background attachments properly work.
                  */
                 $output_property = 'background-attachment';
                 $description = $i18n['background-attachment'];
                 break;
             default:
                 $help = '';
                 break;
         }
         /**
          * If we're using options & option_name is set, then we need to modify the setting.
          */
         if (isset($field['option_type']) && 'option' == $field['option_type'] && isset($field['option_name']) && !empty($field['option_name'])) {
             $property_setting = str_replace(']', '', str_replace($field['option_name'] . '[', '', $field['settings']));
             $property_setting = esc_attr($field['option_name']) . '[' . esc_attr($property_setting) . '_' . $setting . ']';
         } else {
             $property_setting = esc_attr($field['settings']) . '_' . $setting;
         }
         /**
          * Build the field.
          * We're merging with the original field here, so any extra properties are inherited.
          */
         $fields[$property_setting] = array_merge($field, array('type' => $type, 'label' => $label, 'settings' => $property_setting, 'help' => $help, 'section' => $field['section'], 'priority' => $field['priority'], 'required' => $field['required'], 'description' => $description, 'default' => $value, 'id' => Kirki_Field_Sanitize::sanitize_id(array('settings' => $field['settings'] . '_' . $setting)), 'choices' => isset($choices[$key]) ? $choices[$key] : array(), 'output' => '' != $field['output'] ? array(array('element' => $field['output'], 'property' => $output_property)) : '', 'sanitize_callback' => isset($sanitize_callback) ? $sanitize_callback : Kirki_Field_Sanitize::fallback_callback($type)));
         $i++;
     }
     return $fields;
 }
/**
 * Sanitize RGBA colors
 *
 * @since 0.8.5
 */
function kirki_sanitize_rgba($value)
{
    // If empty or an array return transparent
    if (empty($value) || is_array($value)) {
        return 'rgba(0,0,0,0)';
    }
    // If string does not start with 'rgba', then treat as hex
    // sanitize the hex color and finally convert hex to rgba
    if (false === strpos($value, 'rgba')) {
        return Kirki_Color::get_rgba(Kirki_Color::sanitize_hex($value));
    }
    // By now we know the string is formatted as an rgba color so we need to further sanitize it.
    $value = str_replace(' ', '', $value);
    sscanf($value, 'rgba(%d,%d,%d,%f)', $red, $green, $blue, $alpha);
    return 'rgba(' . $red . ',' . $green . ',' . $blue . ',' . $alpha . ')';
}
 public function test_get_rgba()
 {
     // White
     $this->assertEquals('rgba(255,255,255,1)', Kirki_Color::get_rgba('#ffffff', 1));
     $this->assertEquals('rgba(255,255,255,1)', Kirki_Color::get_rgba('#ffffff', 100));
     // Transparent
     $this->assertEquals('rgba(255,255,255,0)', Kirki_Color::get_rgba('#ffffff', 0));
     // grey
     $this->assertEquals('rgba(0,0,0,0.5)', Kirki_Color::get_rgba('#000000', 0.5));
     // colors
     $this->assertEquals('rgba(255,0,0,0.5)', Kirki_Color::get_rgba('#ff0000', 0.5));
     $this->assertEquals('rgba(0,255,0,0.5)', Kirki_Color::get_rgba('#00ff00', 0.5));
     $this->assertEquals('rgba(0,0,255,0.5)', Kirki_Color::get_rgba('#0000ff', 0.5));
 }
Exemple #6
0
 public function test_kirki_get_rgba()
 {
     $random_color = str_pad(dechex(mt_rand(0, 255)), 2, '0', STR_PAD_LEFT) . str_pad(dechex(mt_rand(0, 255)), 2, '0', STR_PAD_LEFT) . str_pad(dechex(mt_rand(0, 255)), 2, '0', STR_PAD_LEFT);
     $this->assertEquals(kirki_get_rgba($random_color), Kirki_Color::get_rgba($random_color));
 }
Exemple #7
0
 public static function setSection($config_id, $args = array())
 {
     if (!isset($args['fields']) || !isset($args['subsection']) || isset($args['subsection']) && !$args['subsection']) {
         // This is a panel
         Kirki::$panels[] = array('id' => isset($args['id']) ? sanitize_key($args['id']) : substr(str_shuffle('abcdefghijklmnopqrstuvwxyz-_'), 0, 7), 'title' => isset($args['title']) ? $args['title'] : '', 'priority' => isset($args['priority']) ? $args['priority'] : 10, 'description' => isset($args['desc']) ? $args['desc'] : '');
     } else {
         // This is a section
         // Get the section ID
         if (isset($args['subsection']) && $args['subsection']) {
             $panel = end(array_values(Kirki::$panels));
             $panel_id = $panel['id'];
         }
         Kirki::$sections[] = array('id' => isset($args['id']) ? sanitize_key($args['id']) : substr(str_shuffle("abcdefghijklmnopqrstuvwxyz-_"), 0, 7), 'title' => $args['title'], 'priority' => isset($args['priority']) ? $args['priority'] : 10, 'panel' => isset($panel_id) ? $panel_id : '', 'description' => isset($args['desc']) ? $args['desc'] : '');
         foreach ($args['fields'] as $field) {
             $field['section'] = isset($args['id']) ? sanitize_key($args['id']) : substr(str_shuffle("abcdefghijklmnopqrstuvwxyz-_"), 0, 7);
             $field['settings'] = $field['id'];
             $field['help'] = isset($field['desc']) ? $field['desc'] : '';
             $field['description'] = isset($field['subtitle']) ? $field['subtitle'] : '';
             $field['choices'] = isset($field['options']) ? $field['options'] : '';
             $field['label'] = isset($field['title']) ? $field['title'] : '';
             switch ($field['type']) {
                 case 'ace_editor':
                     $field['type'] = 'textarea';
                     break;
                 case 'button_set':
                     $field['type'] = 'radio-buttonset';
                     break;
                 case 'checkbox':
                     if (isset($field['options']) && is_array($field['options'])) {
                         $field['type'] = 'multicheck';
                     }
                 case 'color_rgba':
                     $field['type'] = 'color-alpha';
                     if (isset($field['default']) && is_array($field['default'])) {
                         $field['default']['color'] = isset($field['default']['color']) ? Kirki_Color::sanitize_hex($field['default']['color'], true) : '#ffffff';
                         $field['default']['alpha'] = isset($field['default']['alpha']) ? $field['default']['alpha'] : '1';
                         $field['default'] = Kirki_Color::get_rgba($field['default']['color'], $field['default']['alpha']);
                     }
                     break;
                 case 'image_select':
                     $field['type'] = 'radio-image';
                     break;
                 case 'info':
                     $fiel['label'] = '';
                     $field['help'] = '';
                     $field['type'] = 'custom';
                     $background_color = '#fcf8e3';
                     $border_color = '#faebcc';
                     $text_color = '#8a6d3b';
                     if (isset($field['style'])) {
                         if ('success' == $field['style']) {
                             $background_color = '#dff0d8';
                             $border_color = '#d6e9c6';
                             $text_color = '#3c763d';
                         } elseif ('critical' == $field['style']) {
                             $background_color = '#f2dede';
                             $border_color = '#ebccd1';
                             $text_color = '#a94442';
                         }
                     }
                     $field['default'] = '<div style="padding: 10px;background:' . $background_color . ';border-radius:4px;border:1px solid ' . $border_color . ';color:' . $text_color . ';">';
                     $field['default'] .= isset($field['title']) ? '<h4>' . $field['title'] . '</h4>' : '';
                     $field['default'] .= isset($field['desc']) ? $field['desc'] : '';
                     $field['default'] .= '</div>';
                     break;
                 case 'palette':
                     $field['choices'] = $field['palettes'];
                     break;
                 case 'raw':
                     $field['default'] = $field['content'];
                     break;
                 case 'select':
                     if (is_array($field['choices'])) {
                         foreach ($field['choices'] as $key => $value) {
                             if (is_array($value)) {
                                 foreach ($value as $child_key => $child_value) {
                                     $field['choices'][$child_key] = $child_value;
                                 }
                                 unset($field['choices'][$key]);
                             }
                         }
                     }
                     break;
                 case 'slider':
                     $field['choices'] = array('min' => $field['min'], 'max' => $field['max'], 'step' => $field['step']);
                     break;
                 case 'spinner':
                     $field['type'] = 'number';
                     break;
                 case 'background':
                 case 'border':
                 case 'color_gradient':
                 case 'date':
                 case 'dimensions':
                 case 'divide':
                 case 'gallery':
                 case 'import_export':
                 case 'link_color':
                 case 'media':
                 case 'multi_text':
                 case 'password':
                 case 'section':
                 case 'select_image':
                 case 'sortable':
                 case 'sorter':
                 case 'spacing':
                 case 'spinner':
                 case 'switch':
                 case 'typography':
                 case 'slides':
                     // TODO
                     break;
             }
             Kirki::add_field($config_id, $field);
         }
     }
 }
 function kirki_get_rgba($hex = '#fff', $opacity = 100)
 {
     return Kirki_Color::get_rgba($hex, $opacity);
 }