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