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