newColor() public static method

We use a separate instance per color because there's no need to create a completely new instance each time we call this class. Instead using instances helps us improve performance & footprint.
public static newColor ( $color, $mode = 'auto' ) : ariColor
$color string|array
$mode string
return ariColor (object)
Beispiel #1
0
 public function test_getNew_lightness()
 {
     $colors = $this->get_material_design_main_colors();
     foreach ($colors as $color) {
         $c = ariColor::newColor($color);
         $this->assertEquals(max(0, min(360, $c->hue)), $c->getNew('lightness', $c->lightness - 5)->hue);
         $this->assertEquals(max(0, min(100, $c->saturation)), $c->getNew('lightness', $c->lightness - 5)->saturation);
         $this->assertEquals(max(0, min(100, $c->lightness - 5)), $c->getNew('lightness', $c->lightness - 5)->lightness);
         if (5 < $c->lightness) {
             $this->assertTrue($c->toCSS('hex') !== $c->getNew('lightness', $c->hue - 5)->toCSS('hex'));
         }
     }
 }
 /**
  * Sanitizes typography controls
  *
  * @since 2.2.0
  * @param array $value The value.
  * @return array
  */
 public static function sanitize($value)
 {
     if (!is_array($value)) {
         return array();
     }
     // Escape the font-family.
     if (isset($value['font-family'])) {
         $value['font-family'] = esc_attr($value['font-family']);
     }
     // Make sure we're using a valid variant.
     // We're adding checks for font-weight as well for backwards-compatibility
     // Versions 2.0 - 2.2 were using an integer font-weight.
     if (isset($value['variant']) || isset($value['font-weight'])) {
         if (isset($value['font-weight']) && !empty($value['font-weight'])) {
             if (!isset($value['variant']) || empty($value['variant'])) {
                 $value['variant'] = $value['font-weight'];
             }
             unset($value['font-weight']);
         }
         $valid_variants = Kirki_Fonts::get_all_variants();
         if (!array_key_exists($value['variant'], $valid_variants)) {
             $value['variant'] = 'regular';
         }
     }
     // Make sure the saved value is "subsets" (plural) and not "subset".
     // This is for compatibility with older versions.
     if (isset($value['subset'])) {
         if (!empty($value['subset'])) {
             if (!isset($value['subsets']) || empty($value['subset'])) {
                 $value['subsets'] = $value['subset'];
             }
         }
         unset($value['subset']);
     }
     // Make sure we're using a valid subset.
     if (isset($value['subsets'])) {
         $valid_subsets = Kirki_Fonts::get_google_font_subsets();
         $subsets_ok = array();
         if (is_array($value['subsets'])) {
             foreach ($value['subsets'] as $subset) {
                 if (array_key_exists($subset, $valid_subsets)) {
                     $subsets_ok[] = $subset;
                 }
             }
             $value['subsets'] = $subsets_ok;
         }
     }
     // Sanitize the font-size.
     if (isset($value['font-size']) && !empty($value['font-size'])) {
         $value['font-size'] = Kirki_Sanitize_Values::css_dimension($value['font-size']);
         if (is_numeric($value['font-size'])) {
             $value['font-size'] .= 'px';
         }
     }
     // Sanitize the line-height.
     if (isset($value['line-height']) && !empty($value['line-height'])) {
         $value['line-height'] = Kirki_Sanitize_Values::css_dimension($value['line-height']);
     }
     // Sanitize the letter-spacing.
     if (isset($value['letter-spacing']) && !empty($value['letter-spacing'])) {
         $value['letter-spacing'] = Kirki_Sanitize_Values::css_dimension($value['letter-spacing']);
         if (is_numeric($value['letter-spacing'])) {
             $value['letter-spacing'] .= 'px';
         }
     }
     // Sanitize the text-align.
     if (isset($value['text-align']) && !empty($value['text-align'])) {
         if (!in_array($value['text-align'], array('inherit', 'left', 'center', 'right', 'justify'))) {
             $value['text-align'] = 'inherit';
         }
     }
     // Sanitize the text-transform.
     if (isset($value['text-transform']) && !empty($value['text-transform'])) {
         if (!in_array($value['text-transform'], array('none', 'capitalize', 'uppercase', 'lowercase', 'initial', 'inherit'))) {
             $value['text-transform'] = 'none';
         }
     }
     // Sanitize the color.
     if (isset($value['color']) && !empty($value['color'])) {
         $color = ariColor::newColor($value['color']);
         $value['color'] = $color->toCSS('hex');
     }
     return $value;
 }
 /**
  * The sanitize method that will be used as a falback
  *
  * @param string|array $value The control's value.
  */
 public function sanitize($value)
 {
     // is the value formatted as a string?
     if (is_string($value)) {
         $value = rawurldecode($value);
         $value = json_decode($value, true);
     }
     // Nothing to sanitize if we don't have fields.
     if (empty($this->fields)) {
         return $value;
     }
     foreach ($value as $row_id => $row_value) {
         // Make sure the row is formatted as an array.
         if (!is_array($row_value)) {
             $value[$row_id] = array();
             continue;
         }
         // Start parsing sub-fields in rows.
         foreach ($row_value as $subfield_id => $subfield_value) {
             // Make sure this is a valid subfield.
             // If it's not, then unset it.
             if (!isset($this->fields[$subfield_id])) {
                 unset($value[$row_id][$subfield_id]);
             }
             // Get the subfield-type.
             $subfield_type = $this->fields[$subfield_id]['type'];
             // Allow using a sanitize-callback on a per-field basis.
             if (isset($this->fields[$subfield_id]['sanitize_callback'])) {
                 $subfield_value = call_user_func($this->fields[$subfield_id]['sanitize_callback'], $subfield_value);
             } else {
                 switch ($subfield_type) {
                     case 'image':
                     case 'cropped_image':
                     case 'upload':
                         if (!is_numeric($subfield_value) && is_string($subfield_value)) {
                             $subfield_value = esc_url_raw($subfield_value);
                         }
                         break;
                     case 'dropdown-pages':
                         $subfield_value = (int) $subfield_value;
                         break;
                     case 'color':
                         // Instantiate the object.
                         $color_obj = ariColor::newColor($subfield_value);
                         ${$subfield_value} = $color_obj->toCSS($color_obj->mode);
                         break;
                     case 'text':
                         $subfield_value = esc_textarea($subfield_value);
                         break;
                     case 'url':
                     case 'link':
                         $subfield_value = esc_url_raw($subfield_value);
                         break;
                     case 'email':
                         $subfield_value = filter_var($subfield_value, FILTER_SANITIZE_EMAIL);
                         break;
                     case 'tel':
                         $subfield_value = esc_attr($subfield_value);
                         break;
                     case 'checkbox':
                         $subfield_value = (string) intval($subfield_value);
                         break;
                     case 'select':
                     case 'radio':
                     case 'radio-image':
                         $subfield_value = esc_attr($subfield_value);
                         break;
                     case 'textarea':
                         $subfield_value = wp_kses_post($subfield_value);
                 }
             }
             $value[$row_id][$subfield_id] = $subfield_value;
         }
     }
     return $value;
 }
Beispiel #4
0
 /**
  * Strips the alpha value from an RGBA color string.
  *
  * @static
  * @access public
  * @param string $color The RGBA color string.
  * @return string       The corresponding RGB string.
  */
 public static function rgba_to_rgb($color)
 {
     $obj = ariColor::newColor($color);
     return $obj->toCSS('rgb');
 }
 /**
  * Sanitize colors.
  *
  * @static
  * @since 0.8.5
  * @param string $value The value to be sanitized.
  * @return string
  */
 public static function color($value)
 {
     // If the value is empty, then return empty.
     if ('' === $value) {
         return '';
     }
     // If transparent, then return 'transparent'.
     if (is_string($value) && 'transparent' === trim($value)) {
         return 'transparent';
     }
     // Instantiate the object.
     $color = ariColor::newColor($value);
     // Return a CSS value, using the auto-detected mode.
     return $color->toCSS($color->mode);
 }