/**
  * The class constructor.
  *
  * @var 	string		the setting ID.
  * @var 	string		theme_mod / option
  * @var 	array 		an array of arrays of the output arguments.
  * @var 	mixed		a callable function.
  */
 public static function css($field)
 {
     /**
      * Make sure the field is sanitized before proceeding any further.
      */
     $field_sanitized = Kirki_Field_Sanitize::sanitize_field($field);
     /**
      * Set class vars
      */
     self::$settings = $field_sanitized['settings'];
     self::$callback = $field_sanitized['sanitize_callback'];
     self::$field_type = $field_sanitized['type'];
     self::$output = $field_sanitized['output'];
     if (!is_array(self::$output)) {
         self::$output = array(array('element' => self::$output, 'sanitize_callback' => null));
     }
     /**
      * Get the value of this field
      */
     self::$value = Kirki_Values::get_sanitized_field_value($field_sanitized);
     /**
      * Returns the styles
      */
     return self::styles();
 }
 public function google_link()
 {
     /**
      * Get the array of fields from the Kirki object.
      */
     $fields = Kirki::$fields;
     /**
      * Early exit if no fields are found.
      */
     if (empty($fields)) {
         return;
     }
     $fonts = array();
     /**
      * Run a loop for our fields
      */
     foreach ($fields as $field) {
         /**
          * No reason to proceed any further if no 'output' has been defined
          * or if it's not defined as an array.
          */
         if (!isset($field['output']) || !is_array($field['output'])) {
             continue;
         }
         /**
          * Run through each of our "output" items in the array separately.
          */
         foreach ($field['output'] as $output) {
             $valid = false;
             /**
              * If the field-type exists and is set to "typography"
              * then we need some extra checks to figure out if we need to proceed.
              */
             if (isset($field['type']) && 'typography' == $field['type']) {
                 if (isset($field['choices']) && isset($field['choices']['font-family']) && $field['choices']['font-family']) {
                     $valid = true;
                 }
             }
             /**
              * Check if the "property" of this item is related to typography.
              */
             if (isset($output['property']) && in_array($output['property'], array('font-family', 'font-weight', 'font-subset'))) {
                 $valid = true;
             }
             /**
              * If the $valid var is not true, then we don't need to proceed.
              * Continue to the next item in the array.
              */
             if (!$valid) {
                 continue;
             }
             /**
              * Get the value of this field
              */
             $value = Kirki_Values::get_sanitized_field_value($field);
             /**
              * Typography fields arew a bit more complex than usual fields.
              * We need to get the sub-items of the array
              * and then base our calculations on these.
              */
             if ('typography' == $field['type']) {
                 /**
                  * Add the font-family to the array
                  */
                 if (isset($value['font-family'])) {
                     $fonts[]['font-family'] = $value['font-family'];
                 }
                 /**
                  * Add the font-weight to the array
                  */
                 if (isset($value['font-weight'])) {
                     $fonts[]['font-weight'] = $value['font-weight'];
                 }
             } else {
                 if ('font-family' == $output['property']) {
                     /**
                      * Add the font-family to the array
                      */
                     $fonts[]['font-family'] = $value;
                 } else {
                     if ('font-weight' == $output['property']) {
                         /**
                          * Add font-weight to the array
                          */
                         $fonts[]['font-weight'] = $value;
                     } else {
                         if ('font-subset' == $output['property']) {
                             /**
                              * add font subsets to the array
                              */
                             $fonts[]['subsets'] = $value;
                         }
                     }
                 }
             }
         }
     }
     /**
      * Start going through all the items in the $fonts array.
      */
     foreach ($fonts as $font) {
         /**
          * Do we have font-families?
          */
         if (isset($font['font-family'])) {
             $font_families = !isset($font_families) ? array() : $font_families;
             $font_families[] = $font['font-family'];
             /**
              * Determine if we need to create a google-fonts link or not.
              */
             if (!isset($has_google_font)) {
                 if (Kirki_Toolkit::fonts()->is_google_font($font['font-family'])) {
                     $has_google_font = true;
                 }
             }
         }
         /**
          * Do we have font-weights?
          */
         if (isset($font['font-weight'])) {
             $font_weights = !isset($font_weights) ? array() : $font_weights;
             $font_weights[] = $font['font-weight'];
         }
         /**
          * Do we have font-subsets?
          */
         if (isset($font['subsets'])) {
             $font_subsets = !isset($font_subsets) ? array() : $font_subsets;
             $font_subsets[] = $font['subsets'];
         }
     }
     /**
      * Make sure there are no empty values and define some sane defaults.
      */
     $font_families = !isset($font_families) || empty($font_families) ? false : $font_families;
     $font_weights = !isset($font_weights) || empty($font_weights) ? array('400') : $font_weights;
     $font_subsets = !isset($font_subsets) || empty($font_subsets) ? array('all') : $font_subsets;
     /**
      * Get rid of duplicate values
      */
     if (is_array($font_families) && !empty($font_families)) {
         $font_families = array_unique($font_families);
     }
     if (is_array($font_weights) && !empty($font_weights)) {
         $font_weights = array_unique($font_weights);
     }
     if (is_array($font_subsets) && !empty($font_subsets)) {
         $font_subsets = array_unique($font_subsets);
     }
     if (!isset($has_google_font) || !$has_google_font) {
         $font_families = false;
     }
     // Return the font URL.
     return $font_families ? Kirki_Toolkit::fonts()->get_google_font_uri($font_families, $font_weights, $font_subsets) : false;
 }
Пример #3
0
 /**
  * Get the value of an option from the db.
  *
  * @static
  * @access public
  * @param string $config_id The ID of the configuration corresponding to this field.
  * @param string $field_id  The field_id (defined as 'settings' in the field arguments).
  * @return mixed The saved value of the field.
  */
 public static function get_option($config_id = '', $field_id = '')
 {
     return Kirki_Values::get_value($config_id, $field_id);
 }
 /**
  * Get the CSS for a field.
  *
  * @static
  * @access public
  * @param array $field The field.
  * @return array
  */
 public static function css($field)
 {
     // Set class vars.
     self::$settings = $field['settings'];
     self::$callback = $field['sanitize_callback'];
     self::$field_type = $field['type'];
     self::$output = $field['output'];
     if (!is_array(self::$output)) {
         self::$output = array(array('element' => self::$output, 'sanitize_callback' => null));
     }
     // Get the value of this field.
     self::$value = Kirki_Values::get_sanitized_field_value($field);
     // Find the class that will handle the outpout for this field.
     $classname = 'Kirki_Output';
     $field_output_classes = apply_filters('kirki/' . $field['kirki_config'] . '/output/control-classnames', array('kirki-spacing' => 'Kirki_Output_Field_Spacing', 'kirki-typography' => 'Kirki_Output_Field_Typography', 'kirki-multicolor' => 'Kirki_Output_Field_Multicolor'));
     if (array_key_exists(self::$field_type, $field_output_classes)) {
         $classname = $field_output_classes[self::$field_type];
     }
     $obj = new $classname($field['kirki_config'], self::$output, self::$value);
     return $obj->get_styles();
 }
 /**
  * Loop through all fields and create an array of style definitions.
  *
  * @static
  * @access public
  * @param string $config_id The configuration ID.
  */
 public static function loop_controls($config_id)
 {
     // Get an instance of the Kirki_Styles_Output_CSS class.
     // This will make sure google fonts and backup fonts are loaded.
     Kirki_Styles_Output_CSS::get_instance();
     $fields = Kirki::$fields;
     $css = array();
     // Early exit if no fields are found.
     if (empty($fields)) {
         return;
     }
     foreach ($fields as $field) {
         // Only process fields that belong to $config_id.
         if ($config_id != $field['kirki_config']) {
             continue;
         }
         // Only continue if field dependencies are met.
         if (!empty($field['required'])) {
             $valid = true;
             foreach ($field['required'] as $requirement) {
                 if (isset($requirement['setting']) && isset($requirement['value']) && isset($requirement['operator'])) {
                     $controller_value = Kirki_Values::get_value($config_id, $requirement['setting']);
                     if (!Kirki_Active_Callback::compare($controller_value, $requirement['value'], $requirement['operator'])) {
                         $valid = false;
                     }
                 }
             }
             if (!$valid) {
                 continue;
             }
         }
         // Only continue if $field['output'] is set.
         if (isset($field['output']) && !empty($field['output']) && 'background' != $field['type']) {
             $css = Kirki_Helper::array_replace_recursive($css, Kirki_Styles_Output_CSS::css($field));
             // Add the globals.
             if (isset(self::$css_array[$config_id]) && !empty(self::$css_array[$config_id])) {
                 Kirki_Helper::array_replace_recursive($css, self::$css_array[$config_id]);
             }
         }
     }
     $css = apply_filters('kirki/' . $config_id . '/styles', $css);
     if (is_array($css)) {
         return Kirki_Styles_Output_CSS::styles_parse(Kirki_Styles_Output_CSS::add_prefixes($css));
     }
     return;
 }
 /**
  * Processes the arguments of a field
  * determines if it's a typography field
  * and if it is, then takes appropriate actions.
  *
  * @param array $args The field arguments.
  */
 private function generate_google_font($args)
 {
     // Process typography fields.
     if (isset($args['type']) && 'kirki-typography' === $args['type']) {
         // Get the value.
         $value = Kirki_Values::get_sanitized_field_value($args);
         // If we don't have a font-family then we can skip this.
         if (!isset($value['font-family'])) {
             return;
         }
         // Add support for older formats of the typography control.
         // We used to have font-weight instead of variant.
         if (isset($value['font-weight']) && (!isset($value['variant']) || empty($value['variant']))) {
             $value['variant'] = $value['font-weight'];
         }
         // Set a default value for variants.
         if (!isset($value['variant'])) {
             $value['variant'] = 'regular';
         }
         if (isset($value['subsets'])) {
             // Add the subset directly to the array of subsets in the Kirki_GoogleFonts_Manager object.
             // Subsets must be applied to ALL fonts if possible.
             if (!is_array($value['subsets'])) {
                 $this->subsets[] = $value['subsets'];
             } else {
                 foreach ($value['subsets'] as $subset) {
                     $this->subsets[] = $subset;
                 }
             }
         }
         // Add the requested google-font.
         if (!isset($this->fonts[$value['font-family']])) {
             $this->fonts[$value['font-family']] = array();
         }
         if (!in_array($value['variant'], $this->fonts[$value['font-family']], true)) {
             $this->fonts[$value['font-family']][] = $value['variant'];
         }
     } else {
         // Process non-typography fields.
         if (isset($args['output']) && is_array($args['output'])) {
             foreach ($args['output'] as $output) {
                 // If we don't have a typography-related output argument we can skip this.
                 if (!isset($output['property']) || !in_array($output['property'], array('font-family', 'font-weight', 'font-subset', 'subset', 'subsets'), true)) {
                     continue;
                 }
                 // Get the value.
                 $value = Kirki_Values::get_sanitized_field_value($args);
                 if ('font-family' === $output['property']) {
                     if (!array_key_exists($value, $this->fonts)) {
                         $this->fonts[$value] = array();
                     }
                 } elseif ('font-weight' === $output['property']) {
                     foreach ($this->fonts as $font => $variants) {
                         if (!in_array($value, $variants, true)) {
                             $this->fonts[$font][] = $value;
                         }
                     }
                 } elseif ('font-subset' === $output['property'] || 'subset' === $output['property'] || 'subsets' === $output['property']) {
                     if (!is_array($value)) {
                         if (!in_array($value, $this->subsets, true)) {
                             $this->subsets[] = $value;
                         }
                     } else {
                         foreach ($value as $subset) {
                             if (!in_array($subset, $this->subsets, true)) {
                                 $this->subsets[] = $subset;
                             }
                         }
                     }
                 }
             }
         }
     }
 }