/**
 * Prepares the contents of the export file.
 *
 * @author Gary Jones
 * @since 0.9.6
 * @return array $output multi-dimensional array holding CSS data
 * @uses prose_get_mapping()
 * @version 1.0
 */
function prose_prepare_export()
{
    $mapping = prose_get_mapping();
    foreach ($mapping as $selector => $declaration) {
        if (!is_array($declaration)) {
            $output[$selector] = prose_get_design_option($declaration);
        } else {
            foreach ($declaration as $property => $value) {
                if (!is_array($value)) {
                    $output[$selector][$property] = prose_get_design_option($value);
                } else {
                    foreach ($value as $index => $composite_value) {
                        $val = $composite_value[0];
                        $type = $composite_value[1];
                        if ('fixed_string' == $type) {
                            $output[$selector][$property][$index]['value'] = $val;
                        } else {
                            $output[$selector][$property][$index]['value'] = prose_get_design_option($val);
                        }
                        $output[$selector][$property][$index]['type'] = $type;
                    }
                }
            }
        }
    }
    // Add in contents of custom stylesheet
    $css = file_get_contents(prose_get_custom_stylesheet_path());
    $output['custom_css'] = $css;
    return apply_filters('prose_prepare_export', $output);
}
/**
 * If a file has been uploaded to import options, it parses the file, formats into a nice array
 * via the $mapping, then updates the setting in the DB.
 *
 * @author Gary Jones
 * @since 0.9.6
 * @uses prose_get_mapping
 */
function prose_process_import()
{
    if (isset($_POST['prose'])) {
        if ('import' == $_POST['prose']) {
            check_admin_referer('prose-import', '_wpnonce-prose-import');
            if (strpos($_FILES['file']['name'], prose_get_export_filename_prefix()) === false) {
                wp_redirect(admin_url('admin.php?page=design-settings&prose=wrongfile'));
            } elseif ($_FILES['file']['error'] > 0) {
                wp_redirect(admin_url('admin.php?page=design-settings&prose=file'));
            } else {
                $raw_options = file_get_contents($_FILES['file']['tmp_name']);
                $options = unserialize($raw_options);
                $mapping = prose_get_mapping();
                foreach ($options as $selector => $declaration) {
                    if (!is_array($declaration)) {
                        // custom_css or minify_css
                        if ('custom_css' == $selector) {
                            prose_create_custom_stylesheet($declaration);
                        } else {
                            $opt = $selector;
                            $newvalue = $declaration;
                            $newarray[$opt] = $newvalue;
                        }
                    } else {
                        foreach ($declaration as $property => $value) {
                            if (!is_array($value)) {
                                // color, font-style, text-decoration etc
                                $opt = $mapping[$selector][$property];
                                $newvalue = $value;
                                $newarray[$opt] = $newvalue;
                            } else {
                                // multi-value properties: margin, padding, etc
                                foreach ($value as $index => $composite_value) {
                                    $type = $mapping[$selector][$property][$index][1];
                                    if ('fixed_string' != $type) {
                                        $opt = $mapping[$selector][$property][$index][0];
                                        $newvalue = $composite_value['value'];
                                        $newarray[$opt] = $newvalue;
                                    }
                                }
                            }
                        }
                    }
                }
                update_option(PROSE_SETTINGS_FIELD, $newarray);
                wp_redirect(admin_url('admin.php?page=design-settings&prose=import'));
            }
        }
    }
}
/**
 * Loops through the mapping to prepare the CSS output.
 *
 * @author Gary Jones
 * @since 0.9.6
 * @return string $output Beautified CSS
 * @uses prose_get_mapping()
 * @version 1.0
 */
function prose_prepare_settings_stylesheet()
{
    $mapping = prose_get_mapping();
    $output = '';
    foreach ($mapping as $selector => $declaration) {
        if ('custom_css' != $selector && 'minify_css' != $selector) {
            $output .= $selector . ' {' . "\n";
            foreach ($declaration as $property => $value) {
                if (strpos($property, '_select')) {
                    if (prose_get_fresh_design_option($value) == 'hex') {
                        continue;
                    } else {
                        $property = substr($property, 0, strlen($property) - 7);
                    }
                }
                $output .= "\t" . $property . ':';
                if (is_array($value)) {
                    foreach ($value as $composite_value) {
                        $output .= ' ';
                        $val = $composite_value[0];
                        $type = $composite_value[1];
                        if ('fixed_string' == $type) {
                            $output .= $val;
                        } elseif ('string' == $type) {
                            $output .= prose_get_fresh_design_option($val);
                        } else {
                            $cache_val = prose_get_fresh_design_option($val);
                            $output .= $cache_val;
                            $output .= (int) $cache_val > 0 ? $type : null;
                        }
                    }
                } elseif ('#nav_width_calc' == $value) {
                    $output .= prose_calculate_nav_width('primary');
                } elseif ('#nav_ul_width_calc' == $value) {
                    $output .= prose_calculate_nav_width('primary', true);
                } elseif ('#subnav_width_calc' == $value) {
                    $output .= prose_calculate_nav_width('secondary');
                } elseif ('#subnav_ul_width_calc' == $value) {
                    $output .= prose_calculate_nav_width('secondary', true);
                } else {
                    $output .= ' ' . prose_get_fresh_design_option($value);
                }
                $output .= ';' . "\n";
            }
            $output .= '}' . "\n";
        } elseif ('custom_css' == $selector) {
            $output .= prose_get_fresh_design_option($declaration);
        }
    }
    return apply_filters('prose_prepare_stylesheet', $output);
}