Example #1
0
/**
 * Sanitize style fields.
 *
 * @param $section
 * @param $styles
 *
 * @return Sanitized styles
 */
function siteorigin_panels_sanitize_style_fields($section, $styles)
{
    // Use the filter to get the fields for this section.
    if (empty($fields_cache[$section])) {
        // This filter doesn't pass in the arguments $post_id and $args
        // Plugins looking to extend fields, should always add their fields if these are empty
        $fields_cache[$section] = apply_filters('siteorigin_panels_' . $section . '_style_fields', array(), false, false);
    }
    $fields = $fields_cache[$section];
    $return = array();
    foreach ($fields as $k => $field) {
        // Skip this if no field type is set
        if (empty($field['type'])) {
            continue;
        }
        // Handle the special case of a checkbox
        if ($field['type'] == 'checkbox') {
            $return[$k] = !empty($styles[$k]) ? true : '';
            continue;
        }
        // Ignore this if we don't even have a value for the style
        if (!isset($styles[$k]) || $styles[$k] == '') {
            continue;
        }
        switch ($field['type']) {
            case 'color':
                $color = $styles[$k];
                if (preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color)) {
                    $return[$k] = $color;
                } else {
                    $return[$k] = '';
                }
                break;
            case 'image':
                $return[$k] = !empty($styles[$k]) ? intval($styles[$k]) : false;
                break;
            case 'url':
                $return[$k] = esc_url_raw($styles[$k]);
                break;
            case 'measurement':
                $measurements = array_map('preg_quote', siteorigin_panels_style_get_measurements_list());
                if (!empty($field['multiple'])) {
                    if (preg_match_all('/(?:([0-9\\.,]+).*?(' . implode('|', $measurements) . ')+)/', $styles[$k], $match)) {
                        $return[$k] = $styles[$k];
                    } else {
                        $return[$k] = '';
                    }
                } else {
                    if (preg_match('/([0-9\\.,]+).*?(' . implode('|', $measurements) . ')/', $styles[$k], $match)) {
                        $return[$k] = $match[1] . $match[2];
                    } else {
                        $return[$k] = '';
                    }
                }
                break;
            case 'select':
                if (!empty($styles[$k]) && in_array($styles[$k], array_keys($field['options']))) {
                    $return[$k] = $styles[$k];
                }
                break;
            default:
                // Just pass the value through.
                $return[$k] = $styles[$k];
                break;
        }
    }
    return $return;
}
Example #2
0
/**
 * Sanitize style fields.
 *
 * @param $section
 * @param $styles
 *
 * @return Sanitized styles
 */
function siteorigin_panels_sanitize_style_fields($section, $styles)
{
    static $fields_cache = array();
    // Use the filter to get the fields for this section.
    if (empty($fields_cache[$section])) {
        $fields_cache[$section] = apply_filters('siteorigin_panels_' . $section . '_style_fields', array());
    }
    $fields = $fields_cache[$section];
    $return = array();
    foreach ($fields as $k => $field) {
        // Ignore this if we don't even have a value for the style
        if (!isset($styles[$k]) || $styles[$k] == '') {
            continue;
        }
        switch ($field['type']) {
            case 'color':
                $color = $styles[$k];
                if (preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color)) {
                    $return[$k] = $color;
                } else {
                    $return[$k] = '';
                }
                break;
            case 'image':
                $return[$k] = !empty($styles[$k]) ? intval($styles[$k]) : false;
                break;
            case 'url':
                $return[$k] = esc_url_raw($styles[$k]);
                break;
            case 'checkbox':
                $return[$k] = !empty($styles[$k]);
                break;
            case 'measurement':
                $measurements = array_map('preg_quote', siteorigin_panels_style_get_measurements_list());
                preg_match('/([0-9\\.,]+).*?(' . implode('|', $measurements) . ')/', $styles[$k], $match);
                if (!empty($match[0]) && $match[0] != '' && !empty($match[2])) {
                    $return[$k] = $match[1] . $match[2];
                } else {
                    $return[$k] = '';
                }
                break;
            case 'select':
                if (!empty($styles[$k]) && in_array($styles[$k], array_keys($field['options']))) {
                    $return[$k] = $styles[$k];
                }
                break;
            default:
                // Just pass the value through.
                $return[$k] = $styles[$k];
                break;
        }
    }
    return $return;
}