Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.18% covered (success)
98.18%
54 / 55
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Post
98.18% covered (success)
98.18%
54 / 55
50.00% covered (danger)
50.00%
1 / 2
9
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
 addMetaTags
98.15% covered (success)
98.15%
53 / 54
0.00% covered (danger)
0.00%
0 / 1
8
1<?php
2
3declare(strict_types=1);
4
5namespace Beyondwords\Wordpress\Component\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 */
15defined('ABSPATH') || exit;
16
17class Post
18{
19    /**
20     * Init.
21     *
22     * @since 6.0.0
23     */
24    public static function init()
25    {
26        add_action('wp_head', [self::class, 'addMetaTags']);
27    }
28
29    /**
30     * Sets meta[beyondwords-*] tags in the head tag of singular pages.
31     * We set both the [content] attribute and a custom data attribute for compatibility.
32     *
33     * @since 6.0.0
34     *
35     * @return void
36     */
37    public static function addMetaTags()
38    {
39        if (! is_singular()) {
40            return;
41        }
42
43        $postId = get_queried_object_id();
44
45        if (! $postId) {
46            return;
47        }
48
49        $projectId = PostMetaUtils::getProjectId($postId, true);
50
51        if (! $projectId) {
52            return;
53        }
54
55        $title = get_the_title($postId);
56
57        printf(
58            '<meta name="beyondwords-title" content="%s" data-beyondwords-title="%s" />' . "\n",
59            esc_attr($title),
60            esc_attr($title)
61        );
62
63        $authorName = get_the_author_meta('display_name', get_post_field('post_author', $postId));
64
65        printf(
66            '<meta name="beyondwords-author" content="%s" data-beyondwords-author="%s" />' . "\n",
67            esc_attr($authorName),
68            esc_attr($authorName)
69        );
70
71        $publishDate = get_the_date('c', $postId);
72
73        printf(
74            '<meta name="beyondwords-publish-date" content="%s" data-beyondwords-publish-date="%s" />' . "\n",
75            esc_attr($publishDate),
76            esc_attr($publishDate)
77        );
78
79        $titleVoiceId = get_post_meta($postId, 'beyondwords_title_voice_id', true);
80
81        if ($titleVoiceId) {
82            printf(
83                '<meta name="beyondwords-title-voice-id" content="%d" data-beyondwords-title-voice-id="%d" />' . "\n",
84                esc_attr($titleVoiceId),
85                esc_attr($titleVoiceId)
86            );
87        }
88
89        $bodyVoiceId = get_post_meta($postId, 'beyondwords_body_voice_id', true);
90
91        if ($bodyVoiceId) {
92            printf(
93                '<meta name="beyondwords-body-voice-id" content="%d" data-beyondwords-body-voice-id="%d" />' . "\n",
94                esc_attr($bodyVoiceId),
95                esc_attr($bodyVoiceId)
96            );
97        }
98
99        $summaryVoiceId = get_post_meta($postId, 'beyondwords_summary_voice_id', true);
100
101        if ($summaryVoiceId) {
102            printf(
103                '<meta name="beyondwords-summary-voice-id" content="%d" data-beyondwords-summary-voice-id="%d" />' . "\n", // phpcs:ignore Generic.Files.LineLength.TooLong
104                esc_attr($summaryVoiceId),
105                esc_attr($summaryVoiceId)
106            );
107        }
108
109        $languageCode = get_post_meta($postId, 'beyondwords_language_code', true);
110
111        if ($languageCode) {
112            printf(
113                '<meta name="beyondwords-article-language" content="%s" data-beyondwords-article-language="%s" />' . "\n", // phpcs:ignore Generic.Files.LineLength.TooLong
114                esc_attr($languageCode),
115                esc_attr($languageCode)
116            );
117        }
118    }
119}