<?php
function inkthemes_editor_style() {
    add_editor_style();
}
add_action('after_setup_theme', 'inkthemes_editor_style');
function inkthemes_language_load() {
    //Load languages file
    load_theme_textdomain('themia-lite', get_template_directory() . '/languages');
}
add_action('after_setup_theme', 'inkthemes_language_load');
function inkthemes_post_thumbnail() {
    //Post Thumbnail Support
    add_theme_support('post-thumbnails');
}
add_action('after_setup_theme', 'inkthemes_post_thumbnail');
function inkthemes_add_thumbnail() {
    add_image_size('post_thumbnail', 472, 280, true);
    add_image_size('home_post_thumbnail', 193, 138, true);
}
add_action('after_setup_theme', 'inkthemes_add_thumbnail');
/* ----------------------------------------------------------------------------------- */
/* Get Category Id from category name
/*----------------------------------------------------------------------------------- */
function inkthemes_get_category_id($cat_name) {
    $term = get_term_by('name', $cat_name, 'category');
    return $term->term_id;
}
/* ----------------------------------------------------------------------------------- */
/* Auto Feed Links Support
/*----------------------------------------------------------------------------------- */
function inkthemes_theme_support() {
    add_theme_support('title-tag');
    add_theme_support('automatic-feed-links');
}
add_action('after_setup_theme', 'inkthemes_theme_support');
/* ----------------------------------------------------------------------------------- */
/* Custom Menus Function
/*----------------------------------------------------------------------------------- */
// Add CLASS attributes to the first <ul> occurence in wp_page_menu
function inkthemes_add_menuclass($ulclass) {
    return preg_replace('/<ul>/', '<ul class="ddsmoothmenu">', $ulclass, 1);
}
add_filter('wp_page_menu', 'inkthemes_add_menuclass');
function inkthemes_register_custom_menu() {
    register_nav_menu('custom_menu', __('Main Menu', 'themia-lite'));
}
add_action('after_setup_theme', 'inkthemes_register_custom_menu');
function inkthemes_nav() {
    if (function_exists('wp_nav_menu'))
        wp_nav_menu(array('theme_location' => 'custom_menu', 'container_id' => 'menu', 'menu_class' => 'sf-menu', 'fallback_cb' => 'inkthemes_nav_fallback'));
    else
        inkthemes_nav_fallback();
}
function inkthemes_nav_fallback() {
    ?>
    <div id="menu">
        <ul class="sf-menu">
            <?php
            wp_list_pages('title_li=&show_home=1&sort_column=menu_order');
            ?>
        </ul>
    </div>
    <?php
}
function inkthemes_home_nav_menu_items($items) {
    if (is_home()) {
        $homelink = '<li class="current_page_item">' . '<a href="' . esc_url(home_url('/')) . '">' . __('Home', 'themia-lite') . '</a></li>';
    } else {
        $homelink = '<li>' . '<a href="' . esc_url(home_url('/')) . '">' . __('Home', 'themia-lite') . '</a></li>';
    }
    $items = $homelink . $items;
    return $items;
}
add_filter('wp_list_pages', 'inkthemes_home_nav_menu_items');
/* ----------------------------------------------------------------------------------- */
/* Breadcrumbs Plugin
/*------------------------------------------------------------------------------------ */
function inkthemes_breadcrumbs() {
    $delimiter = '&raquo;';
    $home = __('Home', 'themia-lite'); // text for the 'Home' link
    $before = '<span class="current">'; // tag before the current crumb
    $after = '</span>'; // tag after the current crumb
    echo '<div id="crumbs">';
    global $post;
    $homeLink = esc_url(home_url('/'));
    echo '<a href="' . $homeLink . '">' . $home . '</a> ' . $delimiter . ' ';
    if (is_category()) {
        global $wp_query;
        $cat_obj = $wp_query->get_queried_object();
        $thisCat = $cat_obj->term_id;
        $thisCat = get_category($thisCat);
        $parentCat = get_category($thisCat->parent);
        if ($thisCat->parent != 0)
            echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));
        echo $before . __('Archive by category "', 'themia-lite') . single_cat_title('', false) . '"' . $after;
    } elseif (is_day()) {
        echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
        echo '<a href="' . get_month_link(get_the_time('Y'), get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
        echo $before . get_the_time('d') . $after;
    } elseif (is_month()) {
        echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
        echo $before . get_the_time('F') . $after;
    } elseif (is_year()) {
        echo $before . get_the_time('Y') . $after;
    } elseif (is_single() && !is_attachment()) {
        if (get_post_type() != 'post') {
            $post_type = get_post_type_object(get_post_type());
            $slug = $post_type->rewrite;
            echo '<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a> ' . $delimiter . ' ';
            echo $before . get_the_title() . $after;
        } else {
            $cat = get_the_category();
            $cat = $cat[0];
            echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
            echo $before . get_the_title() . $after;
        }
    } elseif (!is_single() && !is_page() && get_post_type() != 'post') {
        $post_type = get_post_type_object(get_post_type());
        echo $before . $post_type->labels->singular_name . $after;
    } elseif (is_attachment()) {
        $parent = get_post($post->post_parent);
        $cat = get_the_category($parent->ID);
        $cat = $cat[0];
        echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
        echo '<a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a> ' . $delimiter . ' ';
        echo $before . get_the_title() . $after;
    } elseif (is_page() && !$post->post_parent) {
        echo $before . get_the_title() . $after;
    } elseif (is_page() && $post->post_parent) {
        $parent_id = $post->post_parent;
        $breadcrumbs = array();
        while ($parent_id) {
            $page = get_page($parent_id);
            $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
            $parent_id = $page->post_parent;
        }
        $breadcrumbs = array_reverse($breadcrumbs);
        foreach ($breadcrumbs as $crumb)
            echo $crumb . ' ' . $delimiter . ' ';
        echo $before . get_the_title() . $after;
    } elseif (is_search()) {
        echo $before . __('Search results for "', 'themia-lite') . get_search_query() . '"' . $after;
    } elseif (is_tag()) {
        echo $before . __('Posts tagged "', 'themia-lite') . single_tag_title('', false) . '"' . $after;
    } elseif (is_author()) {
        global $author;
        $userdata = get_userdata($author);
        echo $before . __('Articles posted by ', 'themia-lite') . $userdata->display_name . $after;
    } elseif (is_404()) {
        echo $before . __('Error 404', 'themia-lite') . $after;
    }
    if (get_query_var('paged')) {
        if (is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author())
            echo ' (';
        echo __('Page', 'themia-lite') . ' ' . get_query_var('paged');
        if (is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author())
            echo ')';
    }
    echo '</div>';
}
/* ----------------------------------------------------------------------------------- */
/* Function to call first uploaded image in functions file
/*----------------------------------------------------------------------------------- */
function inkthemes_main_image() {
    global $post, $posts;
    //This is required to set to Null
    $id = '';
    $the_title = '';
    // Till Here
    $permalink = get_permalink($id);
    $homeLink = get_template_directory_uri();
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    if (isset($matches [1] [0])) {
        $first_img = $matches [1] [0];
    }
    if (empty($first_img)) { //Defines a default image  
        echo '';
    } else {
        print "<a href='$permalink'><img src='$first_img' width='472px' height='280px' class='postimg wp-post-image' alt='$the_title' /></a>";
    }
}
/* ----------------------------------------------------------------------------------- */
/* Function to change the excerpt length
/*----------------------------------------------------------------------------------- */
function inkthemes_excerpt_length($length) {
    return 50;
}
add_filter('excerpt_length', 'inkthemes_excerpt_length');
/* ----------------------------------------------------------------------------------- */
/* Attachment Page Design
  /*----------------------------------------------------------------------------------- */
//For Attachment Page
if (!function_exists('inkthemes_posted_in')) :
    /**
     * Prints HTML with meta information for the current post (category, tags and permalink).
     *
     */
    function inkthemes_posted_in() {
        // Retrieves tag list of current post, separated by commas.
        $tag_list = get_the_tag_list('', ', ');
        if ($tag_list) {
            $posted_in = __('This entry was posted in %1$s and tagged %2$s. Bookmark the ', 'themia-lite') . '<a href="%3$s" title="' . __('Permalink to %4$s', 'themia-lite') . '" rel="bookmark">' . __('permalink</a>.', 'themia-lite');
        } elseif (is_object_in_taxonomy(get_post_type(), 'category')) {
            $posted_in = __('This entry was posted in %1$s. Bookmark the ', 'themia-lite') . '<a href="%3$s" title="' . __('Permalink to %4$s', 'themia-lite') . '" rel="bookmark">' . __('permalink</a>.', 'themia-lite');
        } else {
            $posted_in = __('Bookmark the ', 'themia-lite') . '<a href="%3$s" title="' . __('Permalink to %4$s', 'themia-lite') . '" rel="bookmark">' . __('permalink</a>.', 'themia-lite');
        }
        // Prints the string, replacing the placeholders.
        printf(
                $posted_in, get_the_category_list(', '), $tag_list, get_permalink(), the_title_attribute('echo=0')
        );
    }
endif;
/**
 * Set the content width based on the theme's design and stylesheet.
 *
 * Used to set the width of images and content. Should be equal to the width the theme
 * is designed for, generally via the style.css stylesheet.
 */
if (!isset($content_width))
    $content_width = 472;
/**
 * Register widgetized areas, including two sidebars and four widget-ready columns in the footer.
 *
 * To override inkthemes_widgets_init() in a child theme, remove the action hook and add your own
 * function tied to the init hook.
 *
 * @uses register_sidebar
 */
function inkthemes_widgets_init() {
    // Area 1, located at the top of the sidebar.
    register_sidebar(array(
        'name' => __('Primary Widget Area', 'themia-lite'),
        'id' => 'primary-widget-area',
        'description' => __('The primary widget area', 'themia-lite'),
        'before_widget' => '<div class="wrap_sidebar">',
        'after_widget' => '</div>',
        'before_title' => '<h2 class="title">',
        'after_title' => '</h2>',
    ));
    // Area 2, located below the Primary Widget Area in the sidebar. Empty by default.
    register_sidebar(array(
        'name' => __('Secondary Widget Area', 'themia-lite'),
        'id' => 'secondary-widget-area',
        'description' => __('The secondary widget area', 'themia-lite'),
        'before_widget' => '<div class="wrap_sidebar">',
        'after_widget' => '</div>',
        'before_title' => '<h2 class="title">',
        'after_title' => '</h2>',
    ));
    // Area 3, located in the footer. Empty by default.
    register_sidebar(array(
        'name' => __('First Footer Widget Area', 'themia-lite'),
        'id' => 'first-footer-widget-area',
        'description' => __('The first footer widget area', 'themia-lite'),
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '<h6>',
        'after_title' => '</h6>',
    ));
    // Area 4, located in the footer. Empty by default.
    register_sidebar(array(
        'name' => __('Second Footer Widget Area', 'themia-lite'),
        'id' => 'second-footer-widget-area',
        'description' => __('The second footer widget area', 'themia-lite'),
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '<h6>',
        'after_title' => '</h6>',
    ));
    // Area 5, located in the footer. Empty by default.
    register_sidebar(array(
        'name' => __('Third Footer Widget Area', 'themia-lite'),
        'id' => 'third-footer-widget-area',
        'description' => __('The third footer widget area', 'themia-lite'),
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '<h6>',
        'after_title' => '</h6>',
    ));
    // Area 6, located in the footer. Empty by default.
    register_sidebar(array(
        'name' => __('Fourth Footer Widget Area', 'themia-lite'),
        'id' => 'fourth-footer-widget-area',
        'description' => __('The fourth footer widget area', 'themia-lite'),
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '<h6>',
        'after_title' => '</h6>',
    ));
}
/** Register sidebars by running inkthemes_widgets_init() on the widgets_init hook. */
add_action('widgets_init', 'inkthemes_widgets_init');
/////////Theme Options
/* ----------------------------------------------------------------------------------- */
/* Add Favicon
  /*----------------------------------------------------------------------------------- */
//function inkthemes_childtheme_favicon() {
//    if (inkthemes_get_option('inkthemes_favicon') != '') {
//        echo '<link rel="shortcut icon" href="' . esc_url(inkthemes_get_option('inkthemes_favicon')) . '"/>' . "\n";
//    }
//}
//add_action('wp_head', 'inkthemes_childtheme_favicon');
/**
 * Sanitization for text input
 *
 * @link http://developer.wordpress.org/reference/functions/sanitize_text_field/
 */
add_filter('of_sanitize_text', 'sanitize_text_field');
/**
 * Sanitization for password input
 *
 * @link http://developer.wordpress.org/reference/functions/sanitize_text_field/
 */
add_filter('of_sanitize_password', 'sanitize_text_field');
/**
 * Sanitization for select input
 *
 * Validates that the selected option is a valid option.
 */
add_filter('of_sanitize_select', 'of_sanitize_enum', 10, 2);
/**
 * Sanitization for radio input
 *
 * Validates that the selected option is a valid option.
 */
add_filter('of_sanitize_radio', 'of_sanitize_enum', 10, 2);
/**
 * Sanitization for image selector
 *
 * Validates that the selected option is a valid option.
 */
add_filter('of_sanitize_images', 'of_sanitize_enum', 10, 2);
/**
 * Sanitization for textarea field
 *
 * @param $input string
 * @return $output sanitized string
 */
function of_sanitize_textarea($input) {
    global $allowedposttags;
    $output = wp_kses($input, $allowedposttags);
    return $output;
}
add_filter('of_sanitize_textarea', 'of_sanitize_textarea');
/**
 * Sanitization for checkbox input
 *
 * @param $input string (1 or empty) checkbox state
 * @return $output '1' or false
 */
function of_sanitize_checkbox($input) {
    if ($input) {
        $output = '1';
    } else {
        $output = false;
    }
    return $output;
}
add_filter('of_sanitize_checkbox', 'of_sanitize_checkbox');
/**
 * Sanitization for multicheck
 *
 * @param array of checkbox values
 * @return array of sanitized values ('1' or false)
 */
function of_sanitize_multicheck($input, $option) {
    $output = '';
    if (is_array($input)) {
        foreach ($option['options'] as $key => $value) {
            $output[$key] = false;
        }
        foreach ($input as $key => $value) {
            if (array_key_exists($key, $option['options']) && $value) {
                $output[$key] = '1';
            }
        }
    }
    return $output;
}
add_filter('of_sanitize_multicheck', 'of_sanitize_multicheck', 10, 2);
/**
 * File upload sanitization.
 *
 * Returns a sanitized filepath if it has a valid extension.
 *
 * @param string $input filepath
 * @returns string $output filepath
 */
function of_sanitize_upload($input) {
    $output = '';
    $filetype = wp_check_filetype($input);
    if ($filetype["ext"]) {
        $output = esc_url($input);
    }
    return $output;
}
add_filter('of_sanitize_upload', 'of_sanitize_upload');
/**
 * Sanitization for editor input.
 *
 * Returns unfiltered HTML if user has permissions.
 *
 * @param string $input
 * @returns string $output
 */
function of_sanitize_editor($input) {
    if (current_user_can('unfiltered_html')) {
        $output = $input;
    } else {
        global $allowedposttags;
        $output = wp_kses($input, $allowedposttags);
    }
    return $output;
}
add_filter('of_sanitize_editor', 'of_sanitize_editor');
/**
 * Sanitization of input with allowed tags and wpautotop.
 *
 * Allows allowed tags in html input and ensures tags close properly.
 *
 * @param string $input
 * @returns string $output
 */
function of_sanitize_allowedtags($input) {
    global $allowedtags;
    $output = wpautop(wp_kses($input, $allowedtags));
    return $output;
}
/**
 * Sanitization of input with allowed post tags and wpautotop.
 *
 * Allows allowed post tags in html input and ensures tags close properly.
 *
 * @param string $input
 * @returns string $output
 */
function of_sanitize_allowedposttags($input) {
    global $allowedposttags;
    $output = wpautop(wp_kses($input, $allowedposttags));
    return $output;
}
/**
 * Validates that the $input is one of the avilable choices
 * for that specific option.
 *
 * @param string $input
 * @returns string $output
 */
function of_sanitize_enum($input, $option) {
    $output = '';
    if (array_key_exists($input, $option['options'])) {
        $output = $input;
    }
    return $output;
}
/**
 * Sanitization for background option.
 *
 * @returns array $output
 */
function of_sanitize_background($input) {
    $output = wp_parse_args($input, array(
        'color' => '',
        'image' => '',
        'repeat' => 'repeat',
        'position' => 'top center',
        'attachment' => 'scroll'
            ));
    $output['color'] = apply_filters('of_sanitize_hex', $input['color']);
    $output['image'] = apply_filters('of_sanitize_upload', $input['image']);
    $output['repeat'] = apply_filters('of_background_repeat', $input['repeat']);
    $output['position'] = apply_filters('of_background_position', $input['position']);
    $output['attachment'] = apply_filters('of_background_attachment', $input['attachment']);
    return $output;
}
add_filter('of_sanitize_background', 'of_sanitize_background');
/**
 * Sanitization for background repeat
 *
 * @returns string $value if it is valid
 */
function of_sanitize_background_repeat($value) {
    $recognized = of_recognized_background_repeat();
    if (array_key_exists($value, $recognized)) {
        return $value;
    }
    return apply_filters('of_default_background_repeat', current($recognized));
}
add_filter('of_background_repeat', 'of_sanitize_background_repeat');
/**
 * Sanitization for background position
 *
 * @returns string $value if it is valid
 */
function of_sanitize_background_position($value) {
    $recognized = of_recognized_background_position();
    if (array_key_exists($value, $recognized)) {
        return $value;
    }
    return apply_filters('of_default_background_position', current($recognized));
}
add_filter('of_background_position', 'of_sanitize_background_position');
/**
 * Sanitization for background attachment
 *
 * @returns string $value if it is valid
 */
function of_sanitize_background_attachment($value) {
    $recognized = of_recognized_background_attachment();
    if (array_key_exists($value, $recognized)) {
        return $value;
    }
    return apply_filters('of_default_background_attachment', current($recognized));
}
add_filter('of_background_attachment', 'of_sanitize_background_attachment');
/**
 * Sanitization for typography option.
 */
function of_sanitize_typography($input, $option) {
    $output = wp_parse_args($input, array(
        'size' => '',
        'face' => '',
        'style' => '',
        'color' => ''
            ));
    if (isset($option['options']['faces']) && isset($input['face'])) {
        if (!( array_key_exists($input['face'], $option['options']['faces']) )) {
            $output['face'] = '';
        }
    } else {
        $output['face'] = apply_filters('of_font_face', $output['face']);
    }
    $output['size'] = apply_filters('of_font_size', $output['size']);
    $output['style'] = apply_filters('of_font_style', $output['style']);
    $output['color'] = apply_filters('of_sanitize_color', $output['color']);
    return $output;
}
add_filter('of_sanitize_typography', 'of_sanitize_typography', 10, 2);
/**
 * Sanitization for font size
 */
function of_sanitize_font_size($value) {
    $recognized = of_recognized_font_sizes();
    $value_check = preg_replace('/px/', '', $value);
    if (in_array((int) $value_check, $recognized)) {
        return $value;
    }
    return apply_filters('of_default_font_size', $recognized);
}
add_filter('of_font_size', 'of_sanitize_font_size');
/**
 * Sanitization for font style
 */
function of_sanitize_font_style($value) {
    $recognized = of_recognized_font_styles();
    if (array_key_exists($value, $recognized)) {
        return $value;
    }
    return apply_filters('of_default_font_style', current($recognized));
}
add_filter('of_font_style', 'of_sanitize_font_style');
/**
 * Sanitization for font face
 */
function of_sanitize_font_face($value) {
    $recognized = of_recognized_font_faces();
    if (array_key_exists($value, $recognized)) {
        return $value;
    }
    return apply_filters('of_default_font_face', current($recognized));
}
add_filter('of_font_face', 'of_sanitize_font_face');
/**
 * Get recognized background repeat settings
 *
 * @return   array
 */
function of_recognized_background_repeat() {
    $default = array(
        'no-repeat' => __('No Repeat', 'themia-lite'),
        'repeat-x' => __('Repeat Horizontally', 'themia-lite'),
        'repeat-y' => __('Repeat Vertically', 'themia-lite'),
        'repeat' => __('Repeat All', 'themia-lite'),
    );
    return apply_filters('of_recognized_background_repeat', $default);
}
/**
 * Get recognized background positions
 *
 * @return   array
 */
function of_recognized_background_position() {
    $default = array(
        'top left' => __('Top Left', 'themia-lite'),
        'top center' => __('Top Center', 'themia-lite'),
        'top right' => __('Top Right', 'themia-lite'),
        'center left' => __('Middle Left', 'themia-lite'),
        'center center' => __('Middle Center', 'themia-lite'),
        'center right' => __('Middle Right', 'themia-lite'),
        'bottom left' => __('Bottom Left', 'themia-lite'),
        'bottom center' => __('Bottom Center', 'themia-lite'),
        'bottom right' => __('Bottom Right', 'themia-lite')
    );
    return apply_filters('of_recognized_background_position', $default);
}
/**
 * Get recognized background attachment
 *
 * @return   array
 */
function of_recognized_background_attachment() {
    $default = array(
        'scroll' => __('Scroll Normally', 'themia-lite'),
        'fixed' => __('Fixed in Place', 'themia-lite')
    );
    return apply_filters('of_recognized_background_attachment', $default);
}
/**
 * Sanitize a color represented in hexidecimal notation.
 *
 * @param    string    Color in hexidecimal notation. "#" may or may not be prepended to the string.
 * @param    string    The value that this function should return if it cannot be recognized as a color.
 * @return   string
 */
function of_sanitize_hex($hex, $default = '') {
    if (of_validate_hex($hex)) {
        return $hex;
    }
    return $default;
}
add_filter('of_sanitize_color', 'of_sanitize_hex');
/**
 * Get recognized font sizes.
 *
 * Returns an indexed array of all recognized font sizes.
 * Values are integers and represent a range of sizes from
 * smallest to largest.
 *
 * @return   array
 */
function of_recognized_font_sizes() {
    $sizes = range(9, 71);
    $sizes = apply_filters('of_recognized_font_sizes', $sizes);
    $sizes = array_map('absint', $sizes);
    return $sizes;
}
/**
 * Get recognized font faces.
 *
 * Returns an array of all recognized font faces.
 * Keys are intended to be stored in the database
 * while values are ready for display in in html.
 *
 * @return   array
 */
function of_recognized_font_faces() {
    $default = array(
        'arial' => 'Arial',
        'verdana' => 'Verdana, Geneva',
        'trebuchet' => 'Trebuchet',
        'georgia' => 'Georgia',
        'times' => 'Times New Roman',
        'tahoma' => 'Tahoma, Geneva',
        'palatino' => 'Palatino',
        'helvetica' => 'Helvetica*'
    );
    return apply_filters('of_recognized_font_faces', $default);
}
/**
 * Get recognized font styles.
 *
 * Returns an array of all recognized font styles.
 * Keys are intended to be stored in the database
 * while values are ready for display in in html.
 *
 * @return   array
 */
function of_recognized_font_styles() {
    $default = array(
        'normal' => __('Normal', 'themia-lite'),
        'italic' => __('Italic', 'themia-lite'),
        'bold' => __('Bold', 'themia-lite'),
        'bold italic' => __('Bold Italic', 'themia-lite')
    );
    return apply_filters('of_recognized_font_styles', $default);
}
/**
 * Is a given string a color formatted in hexidecimal notation?
 *
 * @param    string    Color in hexidecimal notation. "#" may or may not be prepended to the string.
 * @return   bool
 */
function of_validate_hex($hex) {
    $hex = trim($hex);
    /* Strip recognized prefixes. */
    if (0 === strpos($hex, '#')) {
        $hex = substr($hex, 1);
    } elseif (0 === strpos($hex, '%23')) {
        $hex = substr($hex, 3);
    }
    /* Regex match. */
    if (0 === preg_match('/^[0-9a-fA-F]{6}$/', $hex)) {
        return false;
    } else {
        return true;
    }
}