/** * Make Post Featured * ----------------------------------------------------------------------------- * @param int/object $post Post object. * @param bool $feature Make post featured. * @reutrn array $featured Array of all featured posts. */ function kaitain_update_featured_posts($post = null, $make_featured = true) { $featured_key = $GLOBALS['kaitain_featured_keys']['featured']; $featured = kaitain_get_featured_list(true); $is_featured_post = kaitain_is_featured_post($post); if (($post = get_post($post)) && $make_featured && !$is_featured_post) { $featured[] = $post->ID; } else { if ($post && !$make_featured && $is_featured_post) { $featured = array_diff($featured, [$post->ID]); } } update_option($featured_key, $featured); return $featured; }
/** * Add Post Editor Meta Box * ----------------------------------------------------------------------------- * @param object $post Post object. */ function kaitain_featured_box_content($post) { wp_nonce_field('kaitain_featured_box_data', 'kaitain_featured_box_nonce'); // These three values will be echoed out as JavaScript variables. $is_featured = kaitain_is_featured_post($post); $is_sticky = false; $expiry = false; if ($is_featured) { $is_sticky = kaitain_is_post_sticky($post); if ($is_featured && $is_sticky) { if (!($expiry = get_option('kaitain_sticky_post')['expires'])) { // 0 is falsy in both JavaScript and PHP. $expiry = 0; } else { // To make it a JavaScript Unix timestamp. $expiry *= 1000; } } } ?> <script> var postmetaFeatured = { featured: <?php printf('%s', $is_featured ? 'true' : 'false'); ?> , sticky: <?php printf('%s', $is_sticky ? 'true' : 'false'); ?> , expiry: <?php printf('%u', $expiry); ?> }; </script> <p> <?php _e('Featured posts are displayed on the website\'s homepage in the lead articles widget.', 'kaitain'); ?> </p> <ul> <li> <fieldset> <input id="kaitain-featured-checkbox" name="make_featured" type="checkbox"> <label for="kaitain-featured-checkbox"><?php _e('Feature Post', 'kaitain'); ?> </label> </fieldset> </li> <li class="kaitain-stickycheck"> <fieldset> <input id="kaitain-sticky-checkbox" name="make_sticky" type="checkbox"> <label for="meta-kaitain-sticky"><?php _e('Sticky Post', 'kaitain'); ?> </label> </fieldset> </li> <li class="kaitain-sticky-expiryinfo" id="kaitain-sticky-expiry"> <?php // Inputs are added and set via JS. ?> </li> </ul> <p class="kaitain-sticky-expiryinfo" id="meta-kaitain-sticky-info"> <em><?php _e('A sticky post will remain in the top position on the front page until either the set time passes, or another post is set to replace it.', 'kaitain'); ?> </em> </p> <?php }