__( 'Archived', 'archived-post-status' ), 'public' => apply_filters( 'aps_status_arg_public', aps_current_user_can_view() ), 'private' => apply_filters( 'aps_status_arg_private', true ), 'exclude_from_search' => apply_filters( 'aps_status_arg_exclude_from_search', ! aps_current_user_can_view() ), 'show_in_admin_all_list' => apply_filters( 'aps_status_arg_show_in_admin_all_list', aps_current_user_can_view() ), 'show_in_admin_status_list' => apply_filters( 'aps_status_arg_show_in_admin_status_list', aps_current_user_can_view() ), 'label_count' => _n_noop( 'Archived (%s)', 'Archived (%s)', 'archived-post-status' ), ); register_post_status( 'archive', $args ); } add_action( 'init', 'aps_register_archive_post_status' ); /** * Returns TRUE if on the frontend, otherwise FALSE * * @filter aps_status_arg_exclude_from_search * * @return bool */ function aps_is_frontend() { return ! is_admin(); } add_filter( 'aps_status_arg_exclude_from_search', 'aps_is_frontend' ); /** * Returns TRUE if current user can view, otherwise FALSE * * @return bool */ function aps_current_user_can_view() { /** * Default capability to grant ability to view Archived content * * @since 0.3.0 * * @return string */ $capability = apply_filters( 'aps_default_read_capability', 'read_private_posts' ); return current_user_can( $capability ); } /** * Filter archived post titles on the frontend * * @param string $title * @param int $post_id (optional) * * @return string */ function aps_the_title( $title, $post_id = null ) { $post = get_post( $post_id ); if ( ! is_admin() && isset( $post->post_status ) && 'archive' === $post->post_status ) { $title = sprintf( '%s: %s', __( 'Archived', 'archived-post-status' ), $title ); } return $title; } add_filter( 'the_title', 'aps_the_title', 10, 2 ); /** * Check if a post type should NOT be using the Archived status * * @param string $post_type * * @return bool */ function aps_is_excluded_post_type( $post_type ) { /** * Prevent the Archived status from being used on these post types * * @since 0.1.0 * * @return array */ $excluded = apply_filters( 'aps_excluded_post_types', array( 'attachment' ) ); if ( in_array( $post_type, $excluded ) ) { return true; } return false; } /** * Modify the DOM on post screens * * @action admin_footer-post.php * * @return void */ function aps_post_screen_js() { global $post; if ( aps_is_excluded_post_type( $post->post_type ) ) { return; } if ( 'draft' !== $post->post_status && 'pending' !== $post->post_status ) { ?> post_type ) || 'archive' !== $post->post_status ) { return; } // Redirect to list table after saving as Archived if ( 'edit' === $action && 1 === $message ) { wp_safe_redirect( add_query_arg( array( 'post_type' => $post->post_type ), admin_url( 'edit.php' ) ), 302 ); exit; } wp_die( __( "You can't edit this item because it has been Archived. Please change the post status and try again.", 'archived-post-status' ), translate( 'WordPress › Error' ) ); } add_action( 'load-post.php', 'aps_load_post_screen' ); /** * Display custom post state text next to post titles that are Archived * * @filter display_post_states * * @param array $post_states An array of post display states * @param object $post WP_Post * * @return array */ function aps_display_post_states( $post_states, $post ) { if ( aps_is_excluded_post_type( $post->post_type ) || 'archive' !== $post->post_status || 'archive' === get_query_var( 'post_status' ) ) { return $post_states; } return array_merge( $post_states, array( 'archive' => __( 'Archived', 'archived-post-status' ) ) ); } add_filter( 'display_post_states', 'aps_display_post_states', 10, 2 ); /** * Close comments and pings when content is archived * * @action save_post * * @param int $post_id Post ID * @param object $post WP_Post * @param bool $update Whether this is an existing post being updated or not * * @return void */ function aps_save_post( $post_id, $post, $update ) { if ( aps_is_excluded_post_type( $post->post_type ) || wp_is_post_revision( $post ) ) { return; } if ( 'archive' === $post->post_status ) { // Unhook to prevent infinite loop remove_action( 'save_post', __FUNCTION__ ); $args = array( 'ID' => $post->ID, 'comment_status' => 'closed', 'ping_status' => 'closed', ); wp_update_post( $args ); // Add hook back again add_action( 'save_post', __FUNCTION__, 10, 3 ); } } add_action( 'save_post', 'aps_save_post', 10, 3 );