/** * Saves the post template meta box settings as post metadata. * * @since 1.0.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 carelib_metabox_post_template_save($post_id, $post = '') { $no = "{$GLOBALS['carelib_prefix']}_post_template_nonce"; $act = "{$GLOBALS['carelib_prefix']}_update_post_template"; // Verify the nonce for the post formats meta box. if (!isset($_POST[$no]) || !wp_verify_nonce($_POST[$no], $act)) { return false; } $input = isset($_POST['carelib-post-template']) ? $_POST['carelib-post-template'] : ''; $current = carelib_get_post_template($post_id); if ($input === $current) { return false; } if (empty($input)) { return carelib_delete_post_template($post_id); } return carelib_set_post_template($post_id, sanitize_text_field($input)); }
/** * Check 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.0.0 * @access public * @param string $template The name of the template to check for. * @param int $post_id * @return bool */ function carelib_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 = carelib_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); }
/** * Determine whether the layout is forced on the current page. * * @since 1.0.0 * @access public * @param int $post_id The post ID for the post to be checked. * @return bool true if the current page template layout is forced, false otherwise */ function _carelib_is_template_layout_forced($post_id = '') { $templates = (array) apply_filters("{$GLOBALS['carelib_prefix']}_forced_templates", array()); if (empty($post_id)) { $post_id = get_the_ID(); } foreach ($templates as $template) { if (get_page_template_slug($post_id) === $template) { return true; } if (carelib_get_post_template($post_id) === $template) { return true; } } return false; }