/** * Get the CSS classes generated by our theme mods. * * @param string $output_as Output as an array or string. * @return mixed The classes generated by our theme mods, either as a string or an array. */ function get_classes($output_as = 'array') { // Will hold the classes. $classes = array(); // Fire up our theme mods class. $theme_mods_class = new CSST_TMD_Theme_Mods(); // Grab all the settings. $settings = $theme_mods_class->get_settings(); // For each setting... foreach ($settings as $setting_id => $setting) { // Grab the value of that setting. $value = $setting['value']; // Make that setting into a CSS class. $class = sanitize_html_class(CSST_TMD . "-{$setting_id}-{$value}"); $classes[] = $class; } // There shouldn't be any duplicates, but just in case. $classes = array_unique($classes); // Let's alphabetize them, just to be a jerk. sort($classes); // Do we want the classes as a string? if ($output_as == 'string') { $classes = implode(' ', $classes); } return $classes; }
/** * Add our panels, sections, and settings to the customizer. * * @param object $wp_customize An instance of the WP_Customize_Manager class. */ public function register($wp_customize) { // Fire up our theme mods class. $theme_mods_class = new CSST_TMD_Theme_Mods(); // Grab our panels, sections, and settings. $panels = $theme_mods_class->get_panels(); // For each panel... foreach ($panels as $panel_id => $panel) { // Add this panel to the UI. $wp_customize->add_panel($panel_id, array('title' => $panel['title'], 'description' => $panel['description'], 'priority' => $panel['priority'])); // For each section in this panel, add it to the UI and add settings to it. foreach ($panel['sections'] as $section_id => $section) { // Add this section to the UI. $wp_customize->add_section($panel_id . '-' . $section_id, array('title' => $section['title'], 'description' => $section['description'], 'priority' => $section['priority'], 'panel' => $panel_id)); // For each setting in this section, add it to the UI. foreach ($section['settings'] as $setting_id => $setting) { // Start building an array of args for adding the setting. $setting_args = array('default' => $setting['default'], 'sanitize_callback' => $setting['sanitize_callback'], 'sanitize_js_callback' => $setting['sanitize_js_callback']); // Register the setting. $wp_customize->add_setting($panel_id . '-' . $section_id . '-' . $setting_id, $setting_args); // Start building an array of args for adding the control. $control_args = array('label' => $setting['label'], 'section' => $panel_id . '-' . $section_id, 'type' => $setting['type'], 'description' => $setting['description']); // Settings of the type 'color' get a special type of control. if ($setting['type'] == 'color') { $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, $panel_id . '-' . $section_id . '-' . $setting_id, $control_args)); // Else, WordPress will use a default control. } else { $wp_customize->add_control($panel_id . '-' . $section_id . '-' . $setting_id, $control_args); } // End this setting. } // End this section. } // End this panel. } }
/** * Loop through our theme mods and build a string of CSS rules. * * @param string $wrapped Whether or not to wrap the styles in a style tag. Expects 'wrapped' or 'unwrapped'. * @param string $output_for The context for these styles. Expects 'front_end' or 'tinymce'. * @return string CSS, either wrapped in a style tag, or not. */ public function get_inline_styles($wrapped = 'wrapped', $output_for = 'front_end') { // This will hold all of our customizer styles. $out = ''; // If we are outputting for the front end... if ($output_for == 'front_end') { // Skip any settings that don't pertain to css. $exclude_if_empty = array('css'); // Else if we are outputting for tinymce... } elseif ($output_for == 'tinymce') { // ... Skip any settings that don't pertain to tinymce. $exclude_if_empty = array('tinymce_css'); } // Fire up our theme mods class. $theme_mods_class = new CSST_TMD_Theme_Mods(); // Get the theme mods, but skip theme mods according to $exclude_if_empty. $settings = $theme_mods_class->get_settings($exclude_if_empty); // For each setting... foreach ($settings as $setting_id => $setting) { // Grab the css for this setting. $css_rules = $setting['css']; // Grab the current value for this setting. $value = $setting['value']; // For each css rule... foreach ($css_rules as $css_rule) { // The css selector. $selector = $css_rule['selector']; // The css property. $property = $css_rule['property']; // Build this into a CSS rule. $rule_string = "{$selector} { {$property} : {$value} ; }"; // Does this css rule have media queries? if (isset($css_rule['queries'])) { $queries = $css_rule['queries']; // How many media queries? $query_count = count($queries); // Will hold the media query string. $query = ''; $i = 0; foreach ($queries as $query_key => $query_value) { $i++; // Add the media query key and value. $query .= "( {$query_key} : {$query_value} )"; // If this isn't the last query, add the "and" operator. if ($i < $query_count) { $query .= ' and '; } } // Wrap the rule string in the media query. $rule_string = " @media {$query} { {$rule_string} } "; } // Add the rule, which might be wrapped in a media query, to the output. $out .= $rule_string; } } // Didn't find any? Bail. if (empty($out)) { return FALSE; } $class = __CLASS__; // Grab our class to add a helpful debug comment. if ($wrapped == 'wrapped') { $out = "<!-- Added by {$class} --><style>{$out}</style>"; } elseif ($wrapped == 'unwrapped') { $out = "/* Added by {$class} */ {$out}"; } return $out; }