/** * Checks if a post of any post type has a custom template. This is the equivalent of WordPress' * `is_page_template()` function with the exception that it works for all post types. * * @since 1.2.0 * @access public * @param string $template The name of the template to check for. * @param int $post_id * @return bool */ function hybrid_has_post_template($template = '', $post_id = '') { if (!$post_id) { $post_id = get_the_ID(); } // Get the post template, which is saved as metadata. $post_template = hybrid_get_post_template($post_id); // If a specific template was input, check that the post template matches. if ($template && $template === $post_template) { return true; } // Return whether we have a post template. return !empty($post_template); }
/** * Filters the WordPress body class with a better set of classes that are more consistently handled and * are backwards compatible with the original body class functionality that existed prior to WordPress * core adopting this feature. * * @since 2.0.0 * @access public * @param array $classes * @param string|array $class * @return array */ function hybrid_body_class_filter($classes, $class) { // WordPress class for uses when WordPress isn't always the only system on the site. $classes = array('wordpress'); // Text direction. $classes[] = is_rtl() ? 'rtl' : 'ltr'; // Locale and language. $locale = get_locale(); $lang = hybrid_get_language($locale); if ($locale !== $lang) { $classes[] = $lang; } $classes[] = strtolower(str_replace('_', '-', $locale)); // Check if the current theme is a parent or child theme. $classes[] = is_child_theme() ? 'child-theme' : 'parent-theme'; // Multisite check adds the 'multisite' class and the blog ID. if (is_multisite()) { $classes[] = 'multisite'; $classes[] = 'blog-' . get_current_blog_id(); } // Date classes. $time = time() + get_option('gmt_offset') * 3600; $classes[] = strtolower(gmdate('\\yY \\mm \\dd \\hH l', $time)); // Is the current user logged in. $classes[] = is_user_logged_in() ? 'logged-in' : 'logged-out'; // WP admin bar. if (is_admin_bar_showing()) { $classes[] = 'admin-bar'; } // Use the '.custom-background' class to integrate with the WP background feature. if (get_background_image() || get_background_color()) { $classes[] = 'custom-background'; } // Add the '.custom-header' class if the user is using a custom header. if (get_header_image() || display_header_text() && get_header_textcolor()) { $classes[] = 'custom-header'; } // Add the '.display-header-text' class if the user chose to display it. if (display_header_text()) { $classes[] = 'display-header-text'; } // Plural/multiple-post view (opposite of singular). if (hybrid_is_plural()) { $classes[] = 'plural'; } // Merge base contextual classes with $classes. $classes = array_merge($classes, hybrid_get_context()); // Singular post (post_type) classes. if (is_singular()) { // Get the queried post object. $post = get_queried_object(); // Checks for custom template. $template = str_replace(array("{$post->post_type}-template-", "{$post->post_type}-"), '', basename(hybrid_get_post_template($post->ID), '.php')); if ($template) { $classes[] = "{$post->post_type}-template-{$template}"; } // Post format. if (current_theme_supports('post-formats') && post_type_supports($post->post_type, 'post-formats')) { $post_format = get_post_format(get_queried_object_id()); $classes[] = $post_format || is_wp_error($post_format) ? "{$post->post_type}-format-standard" : "{$post->post_type}-format-{$post_format}"; } // Attachment mime types. if (is_attachment()) { foreach (explode('/', get_post_mime_type()) as $type) { $classes[] = "attachment-{$type}"; } } } // Paged views. if (is_paged()) { $classes[] = 'paged'; $classes[] = 'paged-' . intval(get_query_var('paged')); } elseif (is_singular() && 1 < get_query_var('page')) { $classes[] = 'paged'; $classes[] = 'paged-' . intval(get_query_var('page')); } // Theme layouts. if (current_theme_supports('theme-layouts')) { $classes[] = sanitize_html_class('layout-' . hybrid_get_theme_layout()); } // Input class. if (!empty($class)) { $class = is_array($class) ? $class : preg_split('#\\s+#', $class); $classes = array_merge($classes, $class); } return array_map('esc_attr', $classes); }
/** * Saves the post template meta box settings as post metadata. Note that this meta is sanitized using the * hybrid_sanitize_meta() callback function prior to being saved. * * @since 1.2.0 * @access public * @param int $post_id The ID of the current post being saved. * @param object $post The post object currently being saved. * @return void|int */ function hybrid_meta_box_post_save_template($post_id, $post = '') { // Fix for attachment save issue in WordPress 3.5. @link http://core.trac.wordpress.org/ticket/21963 if (!is_object($post)) { $post = get_post(); } // Verify the nonce before proceeding. if (!isset($_POST['hybrid-post-template-nonce']) || !wp_verify_nonce($_POST['hybrid-post-template-nonce'], basename(__FILE__))) { return $post_id; } // Return here if the template is not set. There's a chance it won't be if the post type doesn't have any templates. if (!isset($_POST['hybrid-post-template']) || !current_user_can('edit_post', $post_id)) { return $post_id; } // Get the posted meta value. $new_meta_value = sanitize_text_field($_POST['hybrid-post-template']); // Get the meta value of the meta key. $meta_value = hybrid_get_post_template($post_id); // If there is no new meta value but an old value exists, delete it. if ('' == $new_meta_value && $meta_value) { hybrid_delete_post_template($post_id); } elseif ($new_meta_value != $meta_value) { hybrid_set_post_template($post_id, $new_meta_value); } }
/** * Overrides the default single (singular post) template. Post templates can be loaded using a custom * post template, by slug, or by ID. * * Attachment templates are handled slightly differently. Rather than look for the slug or ID, templates * can be loaded by attachment-$mime[0]_$mime[1].php, attachment-$mime[1].php, or attachment-$mime[0].php. * * @since 0.7.0 * @access public * @param string $template The default WordPress post template. * @return string $template The theme post template after all templates have been checked for. */ function hybrid_singular_template($template) { $templates = array(); // Get the queried post. $post = get_queried_object(); // Check for a custom post template by custom field key '_wp_post_template'. $custom = hybrid_get_post_template(get_queried_object_id()); if ($custom) { $templates[] = $custom; } // If viewing an attachment page, handle the files by mime type. if (is_attachment()) { // Split the mime_type into two distinct parts. $type = hybrid_get_attachment_type(); $subtype = hybrid_get_attachment_subtype(); if ($subtype) { $templates[] = "attachment-{$type}-{$subtype}.php"; $templates[] = "attachment-{$subtype}.php"; } $templates[] = "attachment-{$type}.php"; } else { // Add a post name (slug) template. $templates[] = "{$post->post_type}-{$post->post_name}.php"; // Add a post ID template. $templates[] = "{$post->post_type}-{$post->ID}.php"; } // Add a template based off the post type name. $templates[] = "{$post->post_type}.php"; // Allow for WP standard 'single' templates for compatibility. $templates[] = "single-{$post->post_type}.php"; $templates[] = 'single.php'; // Add a general template of singular.php. $templates[] = "singular.php"; // Return the found template. return locate_template($templates); }