Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicate views: Replace "Pages" screen #40689

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public static function load_wpcom_user_features() {
if ( ! class_exists( 'A8C\FSE\Help_Center' ) ) {
require_once __DIR__ . '/features/help-center/class-help-center.php';
}
require_once __DIR__ . '/features/pages/pages.php';
require_once __DIR__ . '/features/replace-site-visibility/replace-site-visibility.php';
require_once __DIR__ . '/features/wpcom-admin-bar/wpcom-admin-bar.php';
require_once __DIR__ . '/features/wpcom-admin-interface/wpcom-admin-interface.php';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php // phpcs:ignore Squiz.Commenting.FileComment.Missing

require_once __DIR__ . '/quick-actions.php';
require_once __DIR__ . '/template-notices.php';
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php
/**
* Quick actions for the Pages list page.
*
* @package automattic/jetpack-mu-wpcom
*/

/**
* Adds quick actions to change the homepage and post page.
*
* @param string[] $actions An array of action links to be displayed.
* @param WP_Post $page Page object.
*
* @return string[] Filtered actions.
*/
function wpcom_page_quick_actions( $actions, $page ) {
if ( ! current_user_can( 'manage_options' ) ) {
return $actions;
}

if ( $page->post_status !== 'publish' ) {
return $actions;
}

$homepage_id = (int) get_option( 'page_on_front' );
$posts_page_id = (int) get_option( 'page_for_posts' );
$has_static_homepage = 'page' === get_option( 'show_on_front' ) && (bool) $homepage_id;
$is_homepage = $page->ID === $homepage_id;
$is_posts_page = $page->ID === $posts_page_id;

if ( ! $has_static_homepage || $is_homepage ) {
return $actions;
}

$set_homepage_action = 'set-homepage';

$set_homepage_link = add_query_arg( array( $set_homepage_action => $page->ID ) );
$set_homepage_link = wp_nonce_url( $set_homepage_link, $set_homepage_action . '_' . $page->ID );

$actions[ $set_homepage_action ] = sprintf(
'<a href="%1$s" aria-label="%2$s">%3$s</a>',
esc_url( $set_homepage_link ),
/* translators: page title */
esc_attr( sprintf( __( 'Set &#8220;%s&#8221; as your site\'s homepage', 'jetpack-mu-wpcom' ), $page->post_title ) ),
esc_html( __( 'Set as homepage', 'jetpack-mu-wpcom' ) )
);

$set_posts_page_action = 'set-posts-page';
$new_posts_page = $is_posts_page ? 0 : $page->ID;

$set_posts_page_link = add_query_arg( array( $set_posts_page_action => $new_posts_page ) );
$set_posts_page_link = wp_nonce_url( $set_posts_page_link, $set_posts_page_action . '_' . $new_posts_page );
/* translators: page title */
$set_posts_page_label = $is_posts_page ? sprintf( __( 'Unset &#8220;%s&#8221; as the page that displays your latest posts', 'jetpack-mu-wpcom' ), $page->post_title ) : sprintf( __( 'Set &#8220;%s&#8221; as the page that displays your latest posts', 'jetpack-mu-wpcom' ), $page->post_title );
$set_posts_page_text = $is_posts_page ? __( 'Unset as posts page', 'jetpack-mu-wpcom' ) : __( 'Set as posts page', 'jetpack-mu-wpcom' );

$actions[ $set_posts_page_action ] = sprintf(
'<a href="%1$s" aria-label="%2$s">%3$s</a>',
esc_url( $set_posts_page_link ),
esc_attr( $set_posts_page_label ),
esc_html( $set_posts_page_text )
);

return $actions;
}
add_filter( 'page_row_actions', 'wpcom_page_quick_actions', 10, 2 );

/**
* Checks if the current request can perform a quick action valid for a given page.
*
* @param string $action Action name ('set-homepage', 'set-posts-page').
*
* @return false|int The page ID is the request is valid, false otherwise.
*/
function wpcom_validate_quick_action( $action ) {
global $pagenow;

if ( 'edit.php' !== $pagenow ) {
return false;
}

if ( ! isset( $_GET['post_type'] ) || 'page' !== sanitize_text_field( wp_unslash( $_GET['post_type'] ) ) ) {
return false;
}

if ( ! current_user_can( 'manage_options' ) ) {
return false;
}

if ( ! isset( $_GET[ $action ] ) ) {
return false;
}

$page_id = sanitize_text_field( wp_unslash( $_GET[ $action ] ) );
if ( ! is_numeric( $page_id ) ) {
return false;
}

check_admin_referer( $action . '_' . $page_id );

$homepage_id = (int) get_option( 'page_on_front' );
$has_static_homepage = 'page' === get_option( 'show_on_front' ) && (bool) $homepage_id;

if ( ! $has_static_homepage ) {
return false;
}

$page_id = (int) $page_id;

if ( $action === 'set-posts-page' && $page_id === 0 ) {
return $page_id;
}

if ( $page_id === $homepage_id ) {
return false;
}

$page = get_post( $page_id );
if ( ! ( $page instanceof WP_Post ) || $page->post_type !== 'page' || $page->post_status !== 'publish' ) {
return false;
}

return $page_id;
}

/**
* Changes the homepage.
*/
function wpcom_set_homepage() {
$new_homepage_id = wpcom_validate_quick_action( 'set-homepage' );
if ( ! is_int( $new_homepage_id ) ) {
return;
}

update_option( 'page_on_front', $new_homepage_id );

add_action(
'admin_notices',
function () {
wp_admin_notice(
__( 'Homepage changed successfully.', 'jetpack-mu-wpcom' ),
array(
'type' => 'success',
'dismissible' => true,
)
);
}
);
}
add_action( 'init', 'wpcom_set_homepage', 0 ); // Before masterbar_init_wp_posts_list

/**
* Changes the posts_page.
*/
function wpcom_set_posts_page() {
$new_posts_page_id = wpcom_validate_quick_action( 'set-posts-page' );
if ( ! is_int( $new_posts_page_id ) ) {
return;
}

update_option( 'page_for_posts', $new_posts_page_id );

add_action(
'admin_notices',
function () {
wp_admin_notice(
__( 'Posts page changed successfully.', 'jetpack-mu-wpcom' ),
array(
'type' => 'success',
'dismissible' => true,
)
);
}
);
}
add_action( 'init', 'wpcom_set_posts_page', 0 ); // Before masterbar_init_wp_posts_list
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
* Display notices in the Pages list page when the front page is controlled by the theme templates.
*
* @package automattic/jetpack-mu-wpcom
*/

/**
* Determines if the current view is the "All" view.
*
* Inspired by WP_Posts_List_Table::is_base_request()
*
* @return bool Whether the current view is the "All" view.
*/
function wpcom_pages_is_base_request() {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$vars = $_GET;
unset( $vars['paged'] );

if ( empty( $vars ) ) {
return true;
} elseif ( 1 === count( $vars ) && ! empty( $vars['post_type'] ) ) {
return 'page' === $vars['post_type'];
}

return 1 === count( $vars ) && ! empty( $vars['mode'] );
// phpcs:enable WordPress.Security.NonceVerification.Recommended
}

/**
* Displays a notice at the top of the Pages list page if the homepage is controlled by
* a theme template.
*/
function wpcom_show_homepage_notice() {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( ! isset( $_GET['post_type'] ) || 'page' !== sanitize_text_field( wp_unslash( $_GET['post_type'] ) ) ) {
return;
}

if ( ! wp_is_block_theme() ) {
return;
}

$show_on_front = get_option( 'show_on_front' );
$page_on_front = get_option( 'page_on_front' );
$posts_on_front = $show_on_front === 'posts' || ( $show_on_front === 'page' && ! $page_on_front );
if ( ! $posts_on_front ) {
return;
}

$is_all_view = wpcom_pages_is_base_request() || isset( $_REQUEST['all_posts'] );
$is_publish_view = isset( $_REQUEST['post_status'] ) && 'publish' === $_REQUEST['post_status'];
$is_private_view = isset( $_REQUEST['post_status'] ) && 'private' === $_REQUEST['post_status'];

if ( ! $is_all_view && ! $is_publish_view && ! $is_private_view ) {
return;
}

if ( isset( $_REQUEST['s'] ) ) {
$search = sanitize_text_field( wp_unslash( $_REQUEST['s'] ) );
if ( ! str_contains( strtolower( __( 'Homepage', 'jetpack-mu-wpcom' ) ), strtolower( $search ) ) ) {
return;
}
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended
?>
<template id="wpcom-homepate-notice-template">
<div class="wpcom-homepage-notice card">
<div class="icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path d="M12 4L4 7.9V20h16V7.9L12 4zm6.5 14.5H14V13h-4v5.5H5.5V8.8L12 5.7l6.5 3.1v9.7z" />
</svg>
</div>
<div>
<h2><?php esc_html_e( 'Homepage', 'jetpack-mu-wpcom' ); ?></h2>
<p><?php esc_html_e( 'Your homepage uses the Blog Home template', 'jetpack-mu-wpcom' ); ?></p>
</div>
</div>
</template>
<style>
.wpcom-homepage-notice {
clear: both;
max-width: none;
min-width: auto;
padding: 1.5em;
display: flex;
align-items: center;
gap: 16px;
}

.wpcom-homepage-notice h2,
.wpcom-homepage-notice p {
margin: 0;
}

.wpcom-homepage-notice h2 {
margin-bottom: 0.25em;
}

.wpcom-homepage-notice svg {
width: 24px;
height: 24px;
}
</style>
<script type="text/javascript">
document.addEventListener( 'DOMContentLoaded', function() {
const template = document.getElementById( 'wpcom-homepate-notice-template' );
const notice = template.content.cloneNode( true );
const tablenav = document.querySelector( '#posts-filter .tablenav.top' );
if ( ! tablenav ) {
return;
}
tablenav.parentNode.insertBefore( notice, tablenav );
} );
</script>
<?php
}
add_action( 'admin_print_footer_scripts-edit.php', 'wpcom_show_homepage_notice' );
Loading