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