Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.78% covered (success)
97.78%
44 / 45
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Head
97.78% covered (success)
97.78%
44 / 45
50.00% covered (danger)
50.00%
1 / 2
8
0.00% covered (danger)
0.00%
0 / 1
 init
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 add_meta_tags
97.73% covered (success)
97.73%
43 / 44
0.00% covered (danger)
0.00%
0 / 1
7
1<?php
2
3declare( strict_types = 1 );
4
5namespace BeyondWords\Post;
6
7/**
8 * General Post class.
9 *
10 * @package    Beyondwords
11 * @subpackage Beyondwords/includes
12 * @author     Stuart McAlpine <stu@beyondwords.io>
13 * @since      6.0.0
14 * @since      7.0.0 Refactored to BeyondWords namespace with snake_case methods.
15 */
16defined( 'ABSPATH' ) || exit;
17
18class Head {
19
20    /**
21     * Init.
22     *
23     * @since 6.0.0
24     * @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods.
25     */
26    public static function init() {
27        add_action( 'wp_head', [ self::class, 'add_meta_tags'] );
28    }
29
30    /**
31     * Sets meta[beyondwords-*] tags in the head tag of singular pages.
32     *
33     * Magic Embed only: the tags are hints for the BeyondWords crawler/SDK. The
34     * REST API integration already sends these values in the content payload.
35     *
36     * @since 6.0.0
37     * @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods.
38     * @since 7.0.0 Only emitted for the client-side (Magic Embed) integration.
39     *
40     * @return void
41     */
42    public static function add_meta_tags() {
43        if ( ! is_singular() ) {
44            return;
45        }
46
47        $post_id = get_queried_object_id();
48
49        if ( ! $post_id ) {
50            return;
51        }
52
53        $project_id = Meta::get_project_id( $post_id, true );
54
55        if ( ! $project_id ) {
56            return;
57        }
58
59        $post = get_post( $post_id );
60
61        if (
62            \BeyondWords\Settings\Fields::INTEGRATION_CLIENT_SIDE
63            !== \BeyondWords\Settings\Fields::get_integration_method( $post )
64        ) {
65            return;
66        }
67
68        $title = get_the_title( $post_id );
69
70        printf(
71            '<meta name="beyondwords-title" content="%s" data-beyondwords-title="%s" />' . "\n",
72            esc_attr( $title ),
73            esc_attr( $title )
74        );
75
76        $author_name = get_the_author_meta( 'display_name', get_post_field( 'post_author', $post_id ) );
77
78        printf(
79            '<meta name="beyondwords-author" content="%s" data-beyondwords-author="%s" />' . "\n",
80            esc_attr( $author_name ),
81            esc_attr( $author_name )
82        );
83
84        $publish_date = get_the_date( 'c', $post_id );
85
86        printf(
87            '<meta name="beyondwords-publish-date" content="%s" data-beyondwords-publish-date="%s" />' . "\n",
88            esc_attr( $publish_date ),
89            esc_attr( $publish_date )
90        );
91
92        $body_voice_id = get_post_meta( $post_id, 'beyondwords_body_voice_id', true );
93
94        if ( $body_voice_id ) {
95            printf(
96                '<meta name="beyondwords-body-voice-id" content="%d" data-beyondwords-body-voice-id="%d" />' . "\n",
97                esc_attr( $body_voice_id ),
98                esc_attr( $body_voice_id )
99            );
100        }
101
102        $language_code = get_post_meta( $post_id, 'beyondwords_language_code', true );
103
104        if ( $language_code ) {
105            printf(
106                '<meta name="beyondwords-article-language" content="%s" data-beyondwords-article-language="%s" />' . "\n", // phpcs:ignore Generic.Files.LineLength.TooLong
107                esc_attr( $language_code ),
108                esc_attr( $language_code )
109            );
110        }
111    }
112}