Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 4
CRAP
n/a
0 / 0
wpcom_enqueue_paragraph_block_placeholder_assets
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
6
is_post_with_write_intent
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
12
wpcom_enter_title_here
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
wpcom_write_your_story
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Overrides the paragraph block placeholder for sites with write intent.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8use Automattic\Jetpack\Jetpack_Mu_Wpcom;
9
10/**
11 * Enqueue block editor assets.
12 */
13function wpcom_enqueue_paragraph_block_placeholder_assets() {
14    global $post;
15
16    if ( ! is_post_with_write_intent( $post ) ) {
17        return;
18    }
19
20    $asset_file          = include Jetpack_Mu_Wpcom::BASE_DIR . 'build/paragraph-block-placeholder/paragraph-block-placeholder.asset.php';
21    $script_dependencies = $asset_file['dependencies'] ?? array();
22    $version             = $asset_file['version'] ?? filemtime( Jetpack_Mu_Wpcom::BASE_DIR . 'build/paragraph-block-placeholder/paragraph-block-placeholder.js' );
23
24    wp_enqueue_script(
25        'paragraph-block-placeholder-script',
26        plugins_url( 'build/paragraph-block-placeholder/paragraph-block-placeholder.js', Jetpack_Mu_Wpcom::BASE_FILE ),
27        $script_dependencies,
28        $version,
29        true
30    );
31
32    wp_localize_script(
33        'paragraph-block-placeholder-script',
34        'wpcomParagraphBlock',
35        array( 'placeholder' => __( "Start writing or type '/' to insert a block", 'jetpack-mu-wpcom' ) )
36    );
37}
38add_action( 'enqueue_block_editor_assets', 'wpcom_enqueue_paragraph_block_placeholder_assets' );
39
40/**
41 * Check is it a post with “write” intent
42 *
43 * @param WP_Post|null $post Current post object. It would be null if the current post is not retrieved.
44 */
45function is_post_with_write_intent( $post ) {
46    return isset( $post ) && 'post' === $post->post_type && 'write' === get_option( 'site_intent', '' );
47}
48
49/**
50 * Replace the title placeholder if it's the post with “write” intent
51 *
52 * @param string       $text Text to shown.
53 * @param WP_Post|null $post Current post object. It would be null if the current post is not retrieved.
54 */
55function wpcom_enter_title_here( $text, $post ) {
56    if ( is_post_with_write_intent( $post ) ) {
57        return __( 'Add a post title', 'jetpack-mu-wpcom' );
58    }
59
60    return $text;
61}
62add_filter( 'enter_title_here', 'wpcom_enter_title_here', 0, 2 );
63
64/**
65 * Replace the body placeholder if it's the post with “write” intent
66 *
67 * @param string       $text Text to shown.
68 * @param WP_Post|null $post Current post object. It would be null if the current post is not retrieved.
69 */
70function wpcom_write_your_story( $text, $post ) {
71    if ( is_post_with_write_intent( $post ) ) {
72        return __( "Start writing or type '/' to insert a block", 'jetpack-mu-wpcom' );
73    }
74
75    return $text;
76}
77add_filter( 'write_your_story', 'wpcom_write_your_story', 0, 2 );