Giftlight): make the gift guide page in one click, choose guide * or quiz, and see the guide as shoppers will. * * @package Giftlight */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * The admin page's URL. * * @return string */ function giftlight_admin_url() { return admin_url( 'admin.php?page=giftlight' ); } /** * The gift guide page this plugin made, if it still exists and isn't in the trash. * * @return WP_Post|null */ function giftlight_page() { $id = (int) get_option( 'giftlight_page_id', 0 ); $p = $id ? get_post( $id ) : null; return ( $p && 'page' === $p->post_type && 'trash' !== $p->post_status ) ? $p : null; } add_action( 'admin_menu', function () { add_submenu_page( 'woocommerce', __( 'Giftlight', 'giftlight' ), __( 'Giftlight', 'giftlight' ), 'manage_woocommerce', 'giftlight', 'giftlight_admin_page' ); }, 60 ); add_filter( 'plugin_action_links_' . plugin_basename( GIFTLIGHT_FILE ), function ( $links ) { array_unshift( $links, '' . esc_html__( 'Set up', 'giftlight' ) . '' ); return $links; } ); /** * Until the gift guide page exists, one line on the Plugins and Dashboard screens points to it. */ add_action( 'admin_notices', function () { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || ! in_array( $screen->id, array( 'plugins', 'dashboard' ), true ) ) { return; } if ( giftlight_page() || ! current_user_can( 'manage_woocommerce' ) || get_option( 'giftlight_notice_dismissed' ) ) { return; } printf( '

%1$s %3$s

', esc_html__( 'Giftlight is ready to build your gift guide from your products.', 'giftlight' ), esc_url( giftlight_admin_url() ), esc_html__( 'Make the gift guide page', 'giftlight' ) ); } ); /** * Form posts from the admin page: make the page, or save "Show it as". */ add_action( 'admin_post_giftlight_make_page', function () { if ( ! current_user_can( 'edit_pages' ) || ! check_admin_referer( 'giftlight_make_page' ) ) { wp_die( esc_html__( 'You can’t do that here.', 'giftlight' ) ); } $existing = giftlight_page(); if ( $existing ) { wp_safe_redirect( get_edit_post_link( $existing->ID, 'url' ) ); exit; } $id = wp_insert_post( array( 'post_type' => 'page', 'post_status' => 'draft', 'post_title' => __( 'Gift Guide', 'giftlight' ), 'post_content' => '', ), true ); if ( is_wp_error( $id ) ) { wp_die( esc_html( $id->get_error_message() ) ); } update_option( 'giftlight_page_id', (int) $id, false ); wp_safe_redirect( get_edit_post_link( $id, 'url' ) ); exit; } ); add_action( 'admin_post_giftlight_save', function () { if ( ! current_user_can( 'manage_woocommerce' ) || ! check_admin_referer( 'giftlight_save' ) ) { wp_die( esc_html__( 'You can’t do that here.', 'giftlight' ) ); } $layout = isset( $_POST['giftlight_layout'] ) && 'quiz' === sanitize_key( wp_unslash( $_POST['giftlight_layout'] ) ) ? 'quiz' : 'guide'; update_option( 'giftlight_layout', $layout ); giftlight_flush(); wp_safe_redirect( add_query_arg( 'saved', '1', giftlight_admin_url() ) ); exit; } ); /** * The admin page. */ function giftlight_admin_page() { if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } $catalog = giftlight_catalog(); $count = count( $catalog['products'] ); $page = giftlight_page(); $layout = get_option( 'giftlight_layout', 'guide' ); echo '
'; echo '

' . esc_html__( 'Giftlight: your gift guide', 'giftlight' ) . '

'; if ( isset( $_GET['saved'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display only. echo '

' . esc_html__( 'Saved.', 'giftlight' ) . '

'; } echo '

' . esc_html__( 'Giftlight sorts your products into a gift guide: who it’s for, what to spend and what they’re into, read from your own categories, tags and prices. A short gift quiz sits one tap away for shoppers who’d rather be asked. It follows your shop by itself: add a product or change a price and the guide changes with it.', 'giftlight' ) . '

'; echo '

' . esc_html__( '1. Put it on your site', 'giftlight' ) . '

'; if ( $page ) { $status = 'publish' === $page->post_status ? __( 'published', 'giftlight' ) : __( 'a draft', 'giftlight' ); /* translators: %s: published or a draft */ echo '

' . esc_html( sprintf( __( 'Your Gift Guide page is %s.', 'giftlight' ), $status ) ) . ' '; echo '' . esc_html__( 'Edit the page', 'giftlight' ) . ' '; echo '' . esc_html( 'publish' === $page->post_status ? __( 'View it', 'giftlight' ) : __( 'Preview it', 'giftlight' ) ) . '

'; if ( 'publish' !== $page->post_status ) { echo '

' . esc_html__( 'Publish it when you’re happy with it, then add it to your menu (Appearance > Menus, or the Navigation block).', 'giftlight' ) . '

'; } } else { echo '
'; wp_nonce_field( 'giftlight_make_page' ); echo ''; echo '

' . esc_html__( 'Makes a draft page called Gift Guide with the guide on it. You check it and publish it.', 'giftlight' ) . '

'; submit_button( __( 'Make the gift guide page', 'giftlight' ), 'primary', 'submit', false ); echo '
'; } echo '

' . esc_html__( 'Or add it anywhere: the “Gift guide” block, or the shortcode', 'giftlight' ) . ' [giftlight] ([giftlight layout="quiz"] ' . esc_html__( 'for the quiz).', 'giftlight' ) . '

'; echo '

' . esc_html__( '2. Show it as', 'giftlight' ) . '

'; echo '
'; wp_nonce_field( 'giftlight_save' ); echo ''; echo '


'; echo '

'; echo '

' . esc_html__( 'The default for the shortcode. Each block has its own setting in the block’s sidebar.', 'giftlight' ) . '

'; submit_button( __( 'Save', 'giftlight' ), 'secondary', 'submit', false ); echo '
'; echo '

' . esc_html__( '3. What shoppers will see', 'giftlight' ) . '

'; /* translators: 1: number of products, 2: number of categories */ echo '

' . esc_html( sprintf( _n( 'Built from %1$d product in stock with a photo and a price, across %2$d categories.', 'Built from %1$d products in stock with a photo and a price, across %2$d categories.', $count, 'giftlight' ), $count, count( $catalog['collections'] ) ) ) . ' '; echo esc_html__( 'Category names like “Kitchen” or “For him” and tags become sections; names like “Sale”, “New” or “Uncategorized” are left out.', 'giftlight' ) . '

'; echo '
'; echo giftlight_render( array( 'layout' => $layout ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- built and escaped by giftlight_render(). echo '
'; echo '

' . esc_html__( 'Nothing leaves your site: the guide is built in the shopper’s browser from your product data, with no tracking and no outside requests.', 'giftlight' ) . '

'; echo '
'; }