Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
CTB
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 4
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 ctb_scripts
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
2
 ctb_footer
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 add_to_runtime
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace NewfoldLabs\WP\Module\GlobalCTB;
3
4use NewfoldLabs\WP\ModuleLoader\Container;
5use function NewfoldLabs\WP\ModuleLoader\container;
6
7/**
8 * This class adds click to buy functionality.
9 **/
10class CTB {
11
12    /**
13     * Dependency injection container.
14     *
15     * @var Container
16     */
17    protected $container;
18
19
20    /**
21     * Constructor.
22     *
23     * @param Container $container The module container.
24     */
25    public function __construct( Container $container ) {
26        $this->container = $container;
27
28        // Module functionality goes here
29        add_action( 'rest_api_init', array( CTBApi::class, 'registerRoutes' ) );
30        add_action( 'admin_enqueue_scripts', array( $this, 'ctb_scripts' ) );
31        add_action( 'admin_footer', array( $this, 'ctb_footer' ) );
32        add_filter( 'newfold-runtime', array( $this, 'add_to_runtime' ) );
33    }
34
35    /**
36     * Enqueue admin scripts
37     *
38     * @return void
39     */
40    public function ctb_scripts() {
41        $assetsDir = container()->plugin()->url . 'vendor/newfold-labs/wp-module-global-ctb/static/';
42
43        // load the a11y dialog lib
44        wp_register_script(
45            'a11y-dialog',
46            $assetsDir . 'a11y-dialog.min.js',
47            array(),
48            '7.4.0',
49            false
50        );
51
52        // load ctb script
53        wp_enqueue_script(
54            'newfold-global-ctb',
55            $assetsDir . 'ctb.js',
56            array( 'a11y-dialog', 'wp-api-fetch', 'nfd-runtime' ),
57            container()->plugin()->version,
58            true
59        );
60
61        // Inline script for global vars for ctb
62        wp_localize_script(
63            'newfold-global-ctb', // script handle
64            'nfdgctb', // js object
65            array(
66                'eventendpoint' => \esc_url_raw( \get_home_url() . '/index.php?rest_route=/newfold-data/v1/events/' ),
67                'brand'         => container()->plugin()->brand,
68            )
69        );
70
71        // Styles
72        wp_enqueue_style(
73            'newfold-global-ctb-style',
74            $assetsDir . 'ctb.css',
75            array(),
76            container()->plugin()->version
77        );
78    }
79
80    /**
81     * Add container to footer for modal components
82     *
83     * @return void
84     */
85    public function ctb_footer() {
86        echo "<div id='nfd-global-ctb-container' aria-hidden='true'></div>";
87    }
88
89    /**
90     * Adds values to the runtime object.
91     *
92     * @param array $sdk The runtime object.
93     *
94     * @return array Modified runtime object.
95     *
96     * @see newfold/wp-module-runtime
97     */
98    public function add_to_runtime( $sdk ) {
99
100        return array_merge( $sdk, array( 'locale' => get_locale() ) );
101    }
102}