$image_ids = oxford_get_featured_image_ids(get_the_ID()); $too_small = array_keys($image_ids, 0); if (count($image_ids) > count($too_small)) { $post_class = 'has-featured-image'; } ?> <article id="post-<?php the_ID(); ?> " <?php post_class($post_class); ?> > <div class="entry-content<?php if (oxford_get_post_char_count() >= oxford_get_post_char_threshold() && !oxford_get_post_meta('settings', get_the_ID(), 'post-single-column')) { echo ' content-columns'; } ?> "> <header class="entry-header"> <?php get_template_part('_post', 'title'); ?> <p class="entry-author-byline"> <?php printf(_x('by %s', 'author byline', 'oxford'), sprintf('<a class="vcard" href="%1$s">%2$s</a>', esc_url(get_author_posts_url(get_the_author_meta('ID'))), esc_html(get_the_author_meta('display_name')))); ?> </p> </header>
/** * Return the ids of all featured images for a given post. * * @since 1.0. * * @param int $post_id The id of the post to get the selected images from. * @return array The ids of the featured images. */ function oxford_get_featured_image_ids($post_id = 0) { if (0 === absint($post_id)) { $post_id = get_the_ID(); } // Check for cached data first $existing_data = oxford_get_post_meta('cache', $post_id, 'featured-images'); if (is_array($existing_data) && !empty($existing_data)) { return $existing_data; } // No cached data, so start with an empty array $image_ids = array(); // The post is an attachment if (is_attachment()) { $image_ids[] = $post_id; } else { // Featured Image $post_thumbnail = get_post_thumbnail_id($post_id); if (absint($post_thumbnail) > 0) { $image_ids[] = $post_thumbnail; } // Secondary Image $secondary_image = get_post_meta($post_id, '_oxford-secondary-image-id', true); if (absint($secondary_image) > 0) { $image_ids[] = $secondary_image; } } // Process the images $processed_ids = oxford_process_featured_images($image_ids); // Save the array to post meta, even if it's still empty oxford_update_post_meta('cache', $post_id, 'featured-images', $processed_ids); return $processed_ids; }
/** * Count and cache the number of characters in a post's content. Or * retrieve the count if it has already been cached. * * @since 1.0. * * @param int $post_id The post's id * @return int The character count of the post */ function oxford_get_post_char_count($post_id = 0) { if (0 === absint($post_id)) { $post_id = get_the_ID(); } // Use cached value if available $char_count = oxford_get_post_meta('cache', $post_id, 'char-count'); if (false !== $char_count) { return absint($char_count); } else { $content = wp_strip_all_tags(strip_shortcodes(get_post($post_id)->post_content), true); $char_count = strlen($content); // Cache the value oxford_update_post_meta('cache', $post_id, 'char-count', $char_count); return $char_count; } }
/** * Sanitize, validate, and save the meta for post background. * * @since 1.0. * * @param int $post_id The id of the post currently being edited. */ function oxford_post_meta_save($post_id) { // Checks save status $is_autosave = wp_is_post_autosave($post_id); $is_revision = wp_is_post_revision($post_id); $is_valid_nonce = isset($_POST['oxford_nonce']) && wp_verify_nonce($_POST['oxford_nonce'], basename(__FILE__)) ? 'true' : 'false'; // Exits script depending on save status if ($is_autosave || $is_revision || !$is_valid_nonce) { return; } // Sanitize/save Background Color if necessary if (isset($_POST['post-background-color']) && ($sanitized_hex = oxford_maybe_hash_hex_color($_POST['post-background-color']))) { if ('#ffffff' === $sanitized_hex) { oxford_update_post_meta('settings', $post_id, 'post-background-color', null); } else { if ($sanitized_hex !== oxford_get_post_meta('settings', $post_id, 'post-background-color')) { oxford_update_post_meta('settings', $post_id, 'post-background-color', $sanitized_hex); } } } // Sanitize/save Single Column if necessary $single_column = oxford_get_post_meta('settings', $post_id, 'post-single-column'); if (isset($_POST['post-single-column']) && !$single_column) { oxford_update_post_meta('settings', $post_id, 'post-single-column', true); } else { if (!isset($_POST['post-single-column']) && $single_column) { oxford_update_post_meta('settings', $post_id, 'post-single-column', null); } } // If there are no settings, remove the meta entry $post_meta_settings = get_post_meta($post_id, '_oxford-post-settings', true); if (empty($post_meta_settings)) { delete_post_meta($post_id, '_oxford-post-settings'); } }