Example #1
0
/**
 * Chooses which template to display for the comment context based on meta data
 * 
 * @param array $files A list of files to search through to find the correct template
 * @return mixed Path to the file, false if no file exists
 * 
**/
function cfct_choose_comment_template_meta($files)
{
    global $comment;
    $meta_files = cfct_meta_templates('', $files);
    if (count($meta_files)) {
        $meta = get_metadata('comment', $comment->comment_ID);
        if (count($meta)) {
            // check key, value matches first
            foreach ($meta as $k => $v) {
                $val = $v[0];
                $file = 'meta-' . $k . '-' . $val . '.php';
                if (in_array($file, $meta_files)) {
                    return $file;
                }
            }
            // check key matches only
            if (!$filename) {
                foreach ($meta as $k => $v) {
                    $file = 'meta-' . $k . '.php';
                    if (in_array($file, $meta_files)) {
                        return $file;
                    }
                }
            }
        }
    }
    return false;
}
Example #2
0
function cfct_choose_single_template($files = array(), $filter = '*')
{
    // must be called within the_loop - cfct_choose_general_template_single() approximates a loop for this reason.
    $exec_order = array('author', 'meta', 'category', 'role', 'tag', 'parent', 'default');
    $exec_order = apply_filters('cfct_single_match_order', $exec_order);
    $filename = false;
    global $post;
    foreach ($exec_order as $type) {
        switch ($type) {
            case 'author':
                $author_files = cfct_author_templates('', $files);
                if (count($author_files)) {
                    $author = get_the_author_login();
                    $file = cfct_filename_filter('author-' . $author . '.php', $filter);
                    if (in_array($file, $author_files)) {
                        $filename = $file;
                    }
                }
                break;
            case 'meta':
                $meta_files = cfct_meta_templates('', $files);
                if (count($meta_files)) {
                    $meta = get_post_custom($post->ID);
                    if (count($meta)) {
                        // check key, value matches first
                        foreach ($meta as $k => $v) {
                            $val = $v[0];
                            $file = cfct_filename_filter('meta-' . $k . '-' . $val . '.php', $filter);
                            if (in_array($file, $meta_files)) {
                                $filename = $file;
                                break;
                            }
                        }
                        // check key matches only
                        if (!$filename) {
                            foreach ($meta as $k => $v) {
                                $file = cfct_filename_filter('meta-' . $k . '.php', $filter);
                                if (in_array($file, $meta_files)) {
                                    $filename = $file;
                                    break;
                                }
                            }
                        }
                    }
                }
                break;
            case 'category':
                $cat_files = cfct_cat_templates($type, $files);
                if (count($cat_files)) {
                    foreach ($cat_files as $file) {
                        $cat_id = cfct_cat_filename_to_id($file);
                        if (in_category($cat_id)) {
                            $filename = $file;
                            break;
                        }
                    }
                }
                break;
            case 'role':
                $role_files = cfct_role_templates($type, $files);
                if (count($role_files)) {
                    $user = new WP_User(get_the_author_ID());
                    if (count($user->roles)) {
                        foreach ($role_files as $file) {
                            foreach ($user->roles as $role) {
                                if (cfct_role_filename_to_name($file) == $role) {
                                    $filename = $file;
                                    break;
                                }
                            }
                        }
                    }
                }
                break;
            case 'tag':
                $tag_files = cfct_tag_templates($type, $files);
                if (count($tag_files)) {
                    $tags = get_the_tags($post->ID);
                    if (is_array($tags) && count($tags)) {
                        foreach ($tag_files as $file) {
                            foreach ($tags as $tag) {
                                if ($tag->slug == cfct_tag_filename_to_name($file)) {
                                    $filename = $file;
                                    break;
                                }
                            }
                        }
                    }
                }
                break;
            case 'parent':
                $parent_files = cfct_parent_templates($type, $files);
                if (count($parent_files) && $post->post_parent > 0) {
                    $parent = cfct_post_id_to_slug($post->post_parent);
                    $file = cfct_filename_filter('parent-' . $parent . '.php', $filter);
                    if (in_array($file, $parent_files)) {
                        $filename = $file;
                    }
                }
                break;
            case 'default':
                break;
        }
        if ($filename) {
            break;
        }
    }
    return apply_filters('cfct_choose_single_template', $filename);
}
Example #3
0
function cfct_choose_content_template_meta($type = 'content', $files = null)
{
    global $post;
    $files = cfct_meta_templates($type, $files);
    if (count($files)) {
        $meta = get_post_custom($post->ID);
        if (count($meta)) {
            // check key, value matches first
            foreach ($meta as $k => $v) {
                $val = $v[0];
                $file = 'meta-' . $k . '-' . $val . '.php';
                if (in_array($file, $files)) {
                    $keys = array($k => $val);
                    return cfct_filename($type, 'meta', $keys);
                }
            }
            // check key matches only
            foreach ($meta as $k => $v) {
                $file = 'meta-' . $k . '.php';
                if (in_array($file, $files)) {
                    $keys = array($k => '');
                    return cfct_filename($type, 'meta', $keys);
                }
            }
        }
    }
    return false;
}
Example #4
0
function cfct_choose_content_template_meta($type = 'content')
{
    global $post;
    $files = cfct_meta_templates($type);
    if (count($files)) {
        $meta = get_post_custom($post->ID);
        if (count($meta)) {
            foreach ($meta as $k => $v) {
                $file = 'meta-' . $k . '-' . $key . '.php';
                if (in_array($file, $files)) {
                    $keys = array($k => $v);
                    return cfct_filename($type, 'meta', $keys);
                }
            }
        }
    }
    return false;
}