Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.75% covered (success)
93.75%
15 / 16
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Amp
93.75% covered (success)
93.75%
15 / 16
50.00% covered (danger)
50.00%
1 / 2
3.00
0.00% covered (danger)
0.00%
0 / 1
 check
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 render
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Beyondwords\Wordpress\Core\Player\Renderer;
4
5use Beyondwords\Wordpress\Component\Post\PostMetaUtils;
6use Beyondwords\Wordpress\Core\CoreUtils;
7use Beyondwords\Wordpress\Core\Environment;
8
9/**
10 * Class Amp
11 *
12 * Renders the AMP-compatible BeyondWords player.
13 */
14class Amp extends Base
15{
16    /**
17     * Check whether we should use the AMP player for the current post.
18     *
19     * @param \WP_Post $post WordPress post object.
20     *
21     * @return bool True if AMP player should be used.
22     */
23    public static function check(\WP_Post $post): bool
24    {
25        if (! CoreUtils::isAmp()) {
26            return false;
27        }
28
29        return parent::check($post);
30    }
31
32    /**
33     * Render AMP player HTML.
34     *
35     *
36     * @return string HTML markup for AMP player.
37     */
38    public static function render(\WP_Post $post): string
39    {
40        $projectId = PostMetaUtils::getProjectId($post->ID);
41        $contentId = PostMetaUtils::getContentId($post->ID, true); // Fallback to Post ID if Content ID is not set
42
43        $src = sprintf(Environment::getAmpPlayerUrl(), $projectId, $contentId);
44
45        ob_start();
46        ?>
47        <amp-iframe
48            frameborder="0"
49            height="43"
50            layout="responsive"
51            sandbox="allow-scripts allow-same-origin allow-popups"
52            scrolling="no"
53            src="<?php echo esc_url($src); ?>"
54            width="295"
55        >
56            <amp-img
57                height="150"
58                layout="responsive"
59                placeholder
60                src="<?php echo esc_url(Environment::getAmpImgUrl()); ?>"
61                width="643"
62            ></amp-img>
63        </amp-iframe>
64        <?php
65        return ob_get_clean();
66    }
67}