Skip to content

Commit

Permalink
feat: prevent homepage from being unpublished (#2307)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoo authored Mar 1, 2023
1 parent 3943b1a commit a151d53
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 31 deletions.
63 changes: 32 additions & 31 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions includes/class-patches.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static function init() {
add_action( 'admin_menu', [ __CLASS__, 'add_reusable_blocks_menu_link' ] );
add_filter( 'wpseo_opengraph_url', [ __CLASS__, 'http_ogurls' ] );
add_filter( 'map_meta_cap', [ __CLASS__, 'prevent_accidental_page_deletion' ], 10, 4 );
add_action( 'pre_post_update', [ __CLASS__, 'prevent_unpublish_front_page' ], 10, 2 );
add_action( 'pre_get_posts', [ __CLASS__, 'maybe_display_author_page' ] );
add_action( 'pre_get_posts', [ __CLASS__, 'restrict_others_posts' ] );
add_filter( 'ajax_query_attachments_args', [ __CLASS__, 'restrict_media_library_access_ajax' ] );
Expand Down Expand Up @@ -231,6 +232,31 @@ public static function prevent_accidental_page_deletion( $caps, $cap, $user_id,
return $caps;
}

/**
* Prevent unpublishing of the homepage.
*
* @param int $post_id ID of post being updated.
* @param array $data Array of unslashed post data.
*/
public static function prevent_unpublish_front_page( $post_id, $data ) {
$homepage_id = intval( get_option( 'page_on_front', 0 ) );

// No need to run if not the homepage, or the homepage isn't a static page.
if ( ! $homepage_id || $homepage_id !== $post_id ) {
return;
}

$post = get_post( $post_id );
$old_status = $post->post_status;
$new_status = $data['post_status'];
$is_published = 'publish' === $old_status;

// Prevent attempts to unpublish a published homepage.
if ( $is_published && 'publish' !== $new_status ) {
wp_die( __( 'Please choose a new homepage before attempting to unpublish this page.', 'newspack-plugin' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}

/**
* Force author pages for non-valid author roles to 404.
* Prevents author pages for users like subscribers and donors from being publicly accessible.
Expand Down

0 comments on commit a151d53

Please sign in to comment.