From 4d15da915a095807c26a4034e575cb4c8465a74b Mon Sep 17 00:00:00 2001 From: benazeer1909 Date: Thu, 5 Dec 2024 13:50:10 +0530 Subject: [PATCH 1/4] Adding page state in data view --- lib/rest-api.php | 21 ++++++ .../fields/src/fields/title/title-view.tsx | 66 +++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/lib/rest-api.php b/lib/rest-api.php index 424927acf1f4a0..ce199e6a1615d3 100644 --- a/lib/rest-api.php +++ b/lib/rest-api.php @@ -36,3 +36,24 @@ function gutenberg_register_edit_site_export_controller_endpoints() { $edit_site_export_controller->register_routes(); } add_action( 'rest_api_init', 'gutenberg_register_edit_site_export_controller_endpoints' ); +add_action( 'rest_api_init', function () { + register_rest_route( + 'page-options/v1', + '/options', + [ + 'methods' => 'GET', + 'callback' => 'get_page_options', + 'permission_callback' => '__return_true', // Adjust permissions as necessary + ] + ); +} ); + +function get_page_options() { + return [ + 'privacyPolicyPageId' => get_option( 'wp_page_for_privacy_policy' ), + 'cartPageId' => get_option( 'woocommerce_cart_page_id' ), + 'checkoutPageId' => get_option( 'woocommerce_checkout_page_id' ), + 'accountPageId' => get_option( 'woocommerce_myaccount_page_id' ), + 'shopPageId' => get_option( 'woocommerce_shop_page_id' ), + ]; +} diff --git a/packages/fields/src/fields/title/title-view.tsx b/packages/fields/src/fields/title/title-view.tsx index f6bf5fb1817d93..9cefdd44b7be8d 100644 --- a/packages/fields/src/fields/title/title-view.tsx +++ b/packages/fields/src/fields/title/title-view.tsx @@ -7,6 +7,7 @@ import { __ } from '@wordpress/i18n'; import { useSelect } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; import type { Settings } from '@wordpress/core-data'; +import { useState, useEffect } from '@wordpress/element'; /** * Internal dependencies @@ -26,6 +27,39 @@ const TitleView = ( { item }: { item: BasePost } ) => { postsPageId: siteSettings?.page_for_posts, }; }, [] ); + const [ options, setOptions ] = useState< { + privacyPolicyPageId: number | null; + cartPageId: number | null; + shopPageId: number | null; + accountPageId: number | null; + checkoutPageId: number | null; + } | null >( null ); + + useEffect( () => { + const fetchOptions = async () => { + try { + const response = await fetch( + '/wp-json/page-options/v1/options' + ); + const data = await response.json(); + setOptions( data ); + } catch ( error ) {} + }; + + fetchOptions(); + }, [] ); + + if ( ! options ) { + return null; // Or a loader while options are being fetched + } + + const { + privacyPolicyPageId, + cartPageId, + shopPageId, + accountPageId, + checkoutPageId, + } = options; const renderedTitle = getItemTitle( item ); @@ -42,6 +76,38 @@ const TitleView = ( { item }: { item: BasePost } ) => { { __( 'Posts Page' ) } ); + } else if ( item.id === Number( privacyPolicyPageId ) ) { + suffix = ( + + { __( 'Privacy Policy' ) } + + ); + } else if ( item.id === Number( cartPageId ) ) { + suffix = ( + + { __( 'Cart' ) } + + ); + } else if ( item.id === Number( shopPageId ) ) { + suffix = ( + + { __( 'Shop' ) } + + ); + } else if ( item.id === Number( accountPageId ) ) { + suffix = ( + + { __( 'Account' ) } + + ); + } else if ( item.id === Number( checkoutPageId ) ) { + suffix = ( + + { __( 'Checkout' ) } + + ); + } else { + suffix = null; } return ( From 37cf3a2fd395c386b5e9671edc278b799a34fa1b Mon Sep 17 00:00:00 2001 From: benazeer1909 Date: Thu, 5 Dec 2024 14:07:30 +0530 Subject: [PATCH 2/4] PHPCS FIX --- lib/rest-api.php | 51 +++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/lib/rest-api.php b/lib/rest-api.php index ce199e6a1615d3..2ef4f936868411 100644 --- a/lib/rest-api.php +++ b/lib/rest-api.php @@ -36,24 +36,39 @@ function gutenberg_register_edit_site_export_controller_endpoints() { $edit_site_export_controller->register_routes(); } add_action( 'rest_api_init', 'gutenberg_register_edit_site_export_controller_endpoints' ); -add_action( 'rest_api_init', function () { - register_rest_route( - 'page-options/v1', - '/options', - [ - 'methods' => 'GET', - 'callback' => 'get_page_options', - 'permission_callback' => '__return_true', // Adjust permissions as necessary - ] - ); -} ); +add_action( + 'rest_api_init', + function () { + register_rest_route( + 'page-options/v1', + '/options', + array( + 'methods' => 'GET', + 'callback' => 'get_page_options', + 'permission_callback' => '__return_true', + ) + ); + } +); +/** + * Returns an array of page IDs used in various parts of WordPress. + * + * The pages included are: + * - Privacy policy page ID + * - Cart page ID (from WooCommerce) + * - Checkout page ID (from WooCommerce) + * - Account page ID (from WooCommerce) + * - Shop page ID (from WooCommerce) + * + * @return array IDs of the pages in the format: array( 'privacyPolicyPageId' => int, ... ) + */ function get_page_options() { - return [ - 'privacyPolicyPageId' => get_option( 'wp_page_for_privacy_policy' ), - 'cartPageId' => get_option( 'woocommerce_cart_page_id' ), - 'checkoutPageId' => get_option( 'woocommerce_checkout_page_id' ), - 'accountPageId' => get_option( 'woocommerce_myaccount_page_id' ), - 'shopPageId' => get_option( 'woocommerce_shop_page_id' ), - ]; + return array( + 'privacyPolicyPageId' => get_option( 'wp_page_for_privacy_policy' ), + 'cartPageId' => get_option( 'woocommerce_cart_page_id' ), + 'checkoutPageId' => get_option( 'woocommerce_checkout_page_id' ), + 'accountPageId' => get_option( 'woocommerce_myaccount_page_id' ), + 'shopPageId' => get_option( 'woocommerce_shop_page_id' ), + ); } From d5bce0a28a8f0691e29ecf65130de9a56f4068b6 Mon Sep 17 00:00:00 2001 From: benazeer1909 Date: Thu, 5 Dec 2024 15:04:15 +0530 Subject: [PATCH 3/4] PHPCS FIX --- lib/rest-api.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/rest-api.php b/lib/rest-api.php index 2ef4f936868411..10b108196f3d78 100644 --- a/lib/rest-api.php +++ b/lib/rest-api.php @@ -63,12 +63,15 @@ function () { * * @return array IDs of the pages in the format: array( 'privacyPolicyPageId' => int, ... ) */ -function get_page_options() { - return array( - 'privacyPolicyPageId' => get_option( 'wp_page_for_privacy_policy' ), - 'cartPageId' => get_option( 'woocommerce_cart_page_id' ), - 'checkoutPageId' => get_option( 'woocommerce_checkout_page_id' ), - 'accountPageId' => get_option( 'woocommerce_myaccount_page_id' ), - 'shopPageId' => get_option( 'woocommerce_shop_page_id' ), - ); +if (!function_exists('get_page_options')) { + function get_page_options() + { + return array( + 'privacyPolicyPageId' => get_option('wp_page_for_privacy_policy'), + 'cartPageId' => get_option('woocommerce_cart_page_id'), + 'checkoutPageId' => get_option('woocommerce_checkout_page_id'), + 'accountPageId' => get_option('woocommerce_myaccount_page_id'), + 'shopPageId' => get_option('woocommerce_shop_page_id'), + ); + } } From 35b27b6dd582c2e53f9ead3d6be6863f518d4ee8 Mon Sep 17 00:00:00 2001 From: benazeer1909 Date: Fri, 6 Dec 2024 17:21:49 +0530 Subject: [PATCH 4/4] PHPCS FIX --- lib/rest-api.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/rest-api.php b/lib/rest-api.php index 10b108196f3d78..29c99dc7910da9 100644 --- a/lib/rest-api.php +++ b/lib/rest-api.php @@ -63,15 +63,14 @@ function () { * * @return array IDs of the pages in the format: array( 'privacyPolicyPageId' => int, ... ) */ -if (!function_exists('get_page_options')) { - function get_page_options() - { +if ( ! function_exists( 'get_page_options' ) ) { + function get_page_options() { return array( - 'privacyPolicyPageId' => get_option('wp_page_for_privacy_policy'), - 'cartPageId' => get_option('woocommerce_cart_page_id'), - 'checkoutPageId' => get_option('woocommerce_checkout_page_id'), - 'accountPageId' => get_option('woocommerce_myaccount_page_id'), - 'shopPageId' => get_option('woocommerce_shop_page_id'), + 'privacyPolicyPageId' => get_option( 'wp_page_for_privacy_policy' ), + 'cartPageId' => get_option( 'woocommerce_cart_page_id' ), + 'checkoutPageId' => get_option( 'woocommerce_checkout_page_id' ), + 'accountPageId' => get_option( 'woocommerce_myaccount_page_id' ), + 'shopPageId' => get_option( 'woocommerce_shop_page_id' ), ); } }