From e4ffdfd82e1e9e5cfc72162b47bd7bd755ec6adf Mon Sep 17 00:00:00 2001
From: mmtr <1233880+mmtr@users.noreply.github.com>
Date: Thu, 19 Dec 2024 14:50:58 +0100
Subject: [PATCH 1/3] Add quick actions
---
.../src/class-jetpack-mu-wpcom.php | 1 +
.../src/features/pages/pages.php | 3 +
.../src/features/pages/quick-actions.php | 176 ++++++++++++++++++
3 files changed, 180 insertions(+)
create mode 100644 projects/packages/jetpack-mu-wpcom/src/features/pages/pages.php
create mode 100644 projects/packages/jetpack-mu-wpcom/src/features/pages/quick-actions.php
diff --git a/projects/packages/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php b/projects/packages/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php
index 89597cf7ce895..3a715382f1c2a 100644
--- a/projects/packages/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php
+++ b/projects/packages/jetpack-mu-wpcom/src/class-jetpack-mu-wpcom.php
@@ -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';
diff --git a/projects/packages/jetpack-mu-wpcom/src/features/pages/pages.php b/projects/packages/jetpack-mu-wpcom/src/features/pages/pages.php
new file mode 100644
index 0000000000000..7f51bb4bb27d8
--- /dev/null
+++ b/projects/packages/jetpack-mu-wpcom/src/features/pages/pages.php
@@ -0,0 +1,3 @@
+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(
+ '%3$s',
+ esc_url( $set_homepage_link ),
+ /* translators: page title */
+ esc_attr( sprintf( __( 'Set “%s” 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 “%s” as the page that displays your latest posts', 'jetpack-mu-wpcom' ), $page->post_title ) : sprintf( __( 'Set “%s” 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(
+ '%3$s',
+ 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
From 3927b2757ec21edcb825e1227e275884f0aad99c Mon Sep 17 00:00:00 2001
From: mmtr <1233880+mmtr@users.noreply.github.com>
Date: Fri, 20 Dec 2024 11:41:37 +0100
Subject: [PATCH 2/3] Add changelog
---
.../update-remove-duplicate-views-pages-quick-actions | 4 ++++
1 file changed, 4 insertions(+)
create mode 100644 projects/packages/jetpack-mu-wpcom/changelog/update-remove-duplicate-views-pages-quick-actions
diff --git a/projects/packages/jetpack-mu-wpcom/changelog/update-remove-duplicate-views-pages-quick-actions b/projects/packages/jetpack-mu-wpcom/changelog/update-remove-duplicate-views-pages-quick-actions
new file mode 100644
index 0000000000000..f7863e4d48100
--- /dev/null
+++ b/projects/packages/jetpack-mu-wpcom/changelog/update-remove-duplicate-views-pages-quick-actions
@@ -0,0 +1,4 @@
+Significance: minor
+Type: added
+
+Pages: Add quick actions to change the homepage and the posts page
From 47fbb064e1996076c84790b7792b22eb7780f5e7 Mon Sep 17 00:00:00 2001
From: mmtr <1233880+mmtr@users.noreply.github.com>
Date: Fri, 20 Dec 2024 18:43:07 +0100
Subject: [PATCH 3/3] Clean action URL
---
.../src/features/pages/quick-actions.php | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/projects/packages/jetpack-mu-wpcom/src/features/pages/quick-actions.php b/projects/packages/jetpack-mu-wpcom/src/features/pages/quick-actions.php
index 5eb6495e652ae..a1ea4f3c53920 100644
--- a/projects/packages/jetpack-mu-wpcom/src/features/pages/quick-actions.php
+++ b/projects/packages/jetpack-mu-wpcom/src/features/pages/quick-actions.php
@@ -32,9 +32,12 @@ function wpcom_page_quick_actions( $actions, $page ) {
return $actions;
}
- $set_homepage_action = 'set-homepage';
+ $set_homepage_action = 'set-homepage';
+ $set_posts_page_action = 'set-posts-page';
+
+ $cleaned_current_url = remove_query_arg( array( $set_homepage_action, $set_posts_page_action, '_wpnonce' ) );
- $set_homepage_link = add_query_arg( array( $set_homepage_action => $page->ID ) );
+ $set_homepage_link = add_query_arg( $set_homepage_action, $page->ID, $cleaned_current_url );
$set_homepage_link = wp_nonce_url( $set_homepage_link, $set_homepage_action . '_' . $page->ID );
$actions[ $set_homepage_action ] = sprintf(
@@ -45,10 +48,9 @@ function wpcom_page_quick_actions( $actions, $page ) {
esc_html( __( 'Set as homepage', 'jetpack-mu-wpcom' ) )
);
- $set_posts_page_action = 'set-posts-page';
- $new_posts_page = $is_posts_page ? 0 : $page->ID;
+ $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 = add_query_arg( $set_posts_page_action, $new_posts_page, $cleaned_current_url );
$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 “%s” as the page that displays your latest posts', 'jetpack-mu-wpcom' ), $page->post_title ) : sprintf( __( 'Set “%s” as the page that displays your latest posts', 'jetpack-mu-wpcom' ), $page->post_title );