Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.62% covered (warning)
84.62%
11 / 13
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Product_Overlay_Template
90.91% covered (success)
90.91%
10 / 11
80.00% covered (warning)
80.00%
4 / 5
6.03
0.00% covered (danger)
0.00%
0 / 1
 labels
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 post_title
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 read_seed_content
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 forbidden_message
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create_failure_message
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Singleton CPT + lifecycle for the WooCommerce product variant of the
4 * block-template overlay.
5 *
6 * @package automattic/jetpack-search
7 */
8
9namespace Automattic\Jetpack\Search;
10
11if ( ! defined( 'ABSPATH' ) ) {
12    exit( 0 );
13}
14
15/**
16 * Theme-agnostic customization surface for the product Search overlay template
17 * via post.php. Falls back to `templates/jetpack-search-overlay-product.html`
18 * when the singleton doesn't exist. Lifecycle lives on
19 * {@see Singleton_Template_Cpt}.
20 *
21 * Sibling of {@see Overlay_Template}: same overlay experience, but rendered on
22 * a WooCommerce product search when the override option is on.
23 */
24class Product_Overlay_Template extends Singleton_Template_Cpt {
25
26    const POST_TYPE          = 'jp_search_prod_ovl';
27    const REST_BASE          = 'jetpack-search-product-overlay';
28    const OPTION_POST_ID     = 'jetpack_search_product_overlay_template_post_id';
29    const EDITOR_REQUEST_KEY = 'jetpack_search_open_product_overlay_editor';
30    const EDITOR_NONCE       = 'jetpack_search_product_overlay_editor';
31    const SEED_META_KEY      = '_jetpack_search_product_overlay_seeded_version';
32
33    /**
34     * Subclass hook — CPT labels.
35     *
36     * @return array{name:string,singular_name:string}
37     */
38    protected static function labels(): array {
39        return array(
40            'name'          => __( 'Product search overlay template', 'jetpack-search-pkg' ),
41            'singular_name' => __( 'Product search overlay template', 'jetpack-search-pkg' ),
42        );
43    }
44
45    /**
46     * Subclass hook — default post title.
47     *
48     * @return string
49     */
50    protected static function post_title(): string {
51        return __( 'Jetpack Search product overlay', 'jetpack-search-pkg' );
52    }
53
54    /**
55     * Subclass hook — seed `post_content`. Reads the bundled file directly,
56     * not via `Search_Blocks::get_overlay_template_content()` (that would
57     * loop back through this class's customization check).
58     *
59     * @return string
60     */
61    protected static function read_seed_content(): string {
62        $path = __DIR__ . '/templates/jetpack-search-overlay-product.html';
63        if ( ! is_readable( $path ) ) {
64            return '';
65        }
66        // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- local, bundled template.
67        return (string) file_get_contents( $path );
68    }
69
70    /**
71     * Subclass hook — forbidden-response copy.
72     *
73     * @return string
74     */
75    protected static function forbidden_message(): string {
76        return __( 'You do not have permission to customize the product Search overlay.', 'jetpack-search-pkg' );
77    }
78
79    /**
80     * Subclass hook — create-failure copy.
81     *
82     * @return string
83     */
84    protected static function create_failure_message(): string {
85        return __( 'Could not create the product Search overlay template.', 'jetpack-search-pkg' );
86    }
87}