Completed
Branch BUG-10381-asset-loading (f91422)
by
unknown
170:16 queued 157:41
created

Iframe   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 337
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 337
rs 10
c 0
b 0
f 0
wmc 30
lcom 3
cbo 3

11 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 45 3
A setTitle() 0 9 2
A setContent() 0 9 2
A setEnqueueWpAssets() 0 4 1
A addStylesheets() 0 14 3
A addScripts() 0 18 4
B addLocalizedVars() 0 21 5
A display() 0 11 2
A getTemplate() 0 50 1
A localizeJsonVars() 0 13 3
A encodeJsonVars() 0 13 4
1
<?php
2
namespace EventEspresso\core\libraries\iframe_display;
3
4
if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) {
5
    exit( 'No direct script access allowed' );
6
}
7
8
9
10
/**
11
 * Class Iframe
12
 *
13
 * @package       Event Espresso
14
 * @subpackage    core
15
 * @author        Brent Christensen
16
 * @since         4.9
17
 */
18
class Iframe
19
{
20
21
    /*
22
    * HTML for notices and ajax gif
23
    * @var string $title
24
    */
25
    protected $title = '';
26
27
    /*
28
    * HTML for the content being displayed
29
    * @var string $content
30
    */
31
    protected $content = '';
32
33
    /*
34
    * whether or not to call wp_head() and wp_footer()
35
    * @var boolean $enqueue_wp_assets
36
    */
37
    protected $enqueue_wp_assets = false;
38
39
    /*
40
    * an array of CSS URLs
41
    * @var array $css
42
    */
43
    protected $css = array();
44
45
    /*
46
    * an array of JS URLs to be set in the HTML header.
47
    * @var array $header_js
48
    */
49
    protected $header_js = array();
50
51
    /*
52
    * an array of JS URLs to be displayed before the HTML </body> tag
53
    * @var array $footer_js
54
    */
55
    protected $footer_js = array();
56
57
    /*
58
    * an array of JSON vars to be set in the HTML header.
59
    * @var array $localized_vars
60
    */
61
    protected $localized_vars = array();
62
63
64
65
    /**
66
     * Iframe constructor
67
     *
68
     * @param string $title
69
     * @param string $content
70
     * @throws \DomainException
71
     */
72
    public function __construct( $title, $content )
73
    {
74
        global $wp_version;
75
        if ( ! defined( 'EE_IFRAME_DIR_URL' ) ) {
76
            define( 'EE_IFRAME_DIR_URL', plugin_dir_url( __FILE__ ) );
77
        }
78
        $this->setContent( $content );
79
        $this->setTitle( $title );
80
        $this->addStylesheets(
81
            apply_filters(
82
                'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__construct__default_css',
83
                array(
84
                    'dashicons'        => includes_url( 'css/dashicons.min.css?ver=' . $wp_version ),
85
                    'espresso_default' => EE_GLOBAL_ASSETS_URL
86
                                          . 'css/espresso_default.css?ver=' . EVENT_ESPRESSO_VERSION,
87
                ),
88
                $this
89
            )
90
        );
91
        $this->addScripts(
92
            apply_filters(
93
                'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__construct__default_js',
94
                array(
95
                    'jquery'        => includes_url( 'js/jquery/jquery.js?ver=' . $wp_version ),
96
                    'espresso_core' => EE_GLOBAL_ASSETS_URL
97
                                       . 'scripts/espresso_core.js?ver=' . EVENT_ESPRESSO_VERSION,
98
                ),
99
                $this
100
            )
101
        );
102
        if (
103
            apply_filters(
104
                'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__construct__load_default_theme_stylesheet',
105
                false
106
            )
107
        ) {
108
            $this->addStylesheets(
109
                apply_filters(
110
                    'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__construct__default_theme_stylesheet',
111
                    array('default_theme_stylesheet' => get_stylesheet_uri()),
112
                    $this
113
                )
114
            );
115
        }
116
    }
117
118
119
120
    /**
121
     * @param string $title
122
     * @throws \DomainException
123
     */
124
    public function setTitle( $title )
125
    {
126
        if ( empty( $title ) ) {
127
            throw new \DomainException(
128
                esc_html__( 'You must provide a page title in order to create an iframe.', 'event_espresso' )
129
            );
130
        }
131
        $this->title = $title;
132
    }
133
134
135
136
    /**
137
     * @param string $content
138
     * @throws \DomainException
139
     */
140
    public function setContent( $content )
141
    {
142
        if ( empty( $content ) ) {
143
            throw new \DomainException(
144
                esc_html__( 'You must provide content in order to create an iframe.', 'event_espresso' )
145
            );
146
        }
147
        $this->content = $content;
148
    }
149
150
151
152
    /**
153
     * @param boolean $enqueue_wp_assets
154
     */
155
    public function setEnqueueWpAssets( $enqueue_wp_assets )
156
    {
157
        $this->enqueue_wp_assets = filter_var( $enqueue_wp_assets, FILTER_VALIDATE_BOOLEAN );
158
    }
159
160
161
162
    /**
163
     * @param array $stylesheets
164
     * @throws \DomainException
165
     */
166
    public function addStylesheets( array $stylesheets )
167
    {
168
        if ( empty( $stylesheets ) ) {
169
            throw new \DomainException(
170
                esc_html__(
171
                    'A non-empty array of URLs, is required to add a CSS stylesheet to an iframe.',
172
                    'event_espresso'
173
                )
174
            );
175
        }
176
        foreach ( $stylesheets as $handle => $stylesheet ) {
177
            $this->css[ $handle ] = $stylesheet;
178
        }
179
    }
180
181
182
183
    /**
184
     * @param array $scripts
185
     * @param bool  $add_to_header
186
     * @throws \DomainException
187
     */
188
    public function addScripts( array $scripts, $add_to_header = false )
189
    {
190
        if ( empty( $scripts ) ) {
191
            throw new \DomainException(
192
                esc_html__(
193
                    'A non-empty array of URLs, is required to add Javascript to an iframe.',
194
                    'event_espresso'
195
                )
196
            );
197
        }
198
        foreach ( $scripts as $handle => $script ) {
199
            if ( $add_to_header ) {
200
                $this->header_js[ $handle ] = $script;
201
            } else {
202
                $this->footer_js[ $handle ] = $script;
203
            }
204
        }
205
    }
206
207
208
209
    /**
210
     * @param array  $vars
211
     * @param string $var_name
212
     * @throws \DomainException
213
     */
214
    public function addLocalizedVars( array $vars, $var_name = 'eei18n' )
215
    {
216
        if ( empty( $vars ) ) {
217
            throw new \DomainException(
218
                esc_html__(
219
                    'A non-empty array of vars, is required to add localized Javascript vars to an iframe.',
220
                    'event_espresso'
221
                )
222
            );
223
        }
224
        foreach ( $vars as $handle => $var ) {
225
            if ( $var_name === 'eei18n' ) {
226
                \EE_Registry::$i18n_js_strings[ $handle ] = $var;
227
            } else {
228
                if ( ! isset( $this->localized_vars[ $var_name ] ) ) {
229
                    $this->localized_vars[ $var_name ] = array();
230
                }
231
                $this->localized_vars[ $var_name ][ $handle ] = $var;
232
            }
233
        }
234
    }
235
236
237
238
    /**
239
     * @param string $utm_content
240
     * @throws \DomainException
241
     */
242
    public function display($utm_content = '')
243
    {
244
        $this->content .= \EEH_Template::powered_by_event_espresso(
245
            '',
246
            '',
247
            ! empty($utm_content) ? array('utm_content' => $utm_content) : array()
248
        );
249
        \EE_System::do_not_cache();
250
        echo $this->getTemplate();
251
        exit;
252
    }
253
254
255
256
    /**
257
     * @return string
258
     * @throws \DomainException
259
     */
260
    public function getTemplate()
261
    {
262
        return \EEH_Template::display_template(
263
            __DIR__ . DIRECTORY_SEPARATOR . 'iframe_wrapper.template.php',
264
            array(
265
                'title'             => apply_filters(
266
                    'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__title',
267
                    $this->title,
268
                    $this
269
                ),
270
                'content'           => apply_filters(
271
                    'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__content',
272
                    $this->content,
273
                    $this
274
                ),
275
                'enqueue_wp_assets' => apply_filters(
276
                    'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__enqueue_wp_assets',
277
                    $this->enqueue_wp_assets,
278
                    $this
279
                ),
280
                'css'               => (array)apply_filters(
281
                    'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__css_urls',
282
                    $this->css,
283
                    $this
284
                ),
285
                'header_js'         => (array)apply_filters(
286
                    'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__header_js_urls',
287
                    $this->header_js,
288
                    $this
289
                ),
290
                'footer_js'         => (array)apply_filters(
291
                    'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__footer_js_urls',
292
                    $this->footer_js,
293
                    $this
294
                ),
295
                'eei18n'            => apply_filters(
296
                    'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__eei18n_js_strings',
297
                    \EE_Registry::localize_i18n_js_strings() . $this->localizeJsonVars(),
298
                    $this
299
                ),
300
                'notices'           => \EEH_Template::display_template(
301
                    EE_TEMPLATES . 'espresso-ajax-notices.template.php',
302
                    array(),
303
                    true
304
                ),
305
            ),
306
            true,
307
            true
308
        );
309
    }
310
311
312
313
    /**
314
     * localizeJsonVars
315
     *
316
     * @return string
317
     */
318
    public function localizeJsonVars()
319
    {
320
        $JSON = '';
321
        foreach ( (array)$this->localized_vars as $var_name => $vars ) {
322
            foreach ( (array)$vars as $key => $value ) {
323
                $this->localized_vars[ $var_name ] = $this->encodeJsonVars( $value );
324
            }
325
            $JSON .= "/* <![CDATA[ */ var {$var_name} = ";
326
            $JSON .= wp_json_encode( $this->localized_vars[ $var_name ] );
327
            $JSON .= '; /* ]]> */';
328
        }
329
        return $JSON;
330
    }
331
332
333
334
    /**
335
     * @param bool|int|float|string|array $var
336
     * @return array
337
     */
338
    public function encodeJsonVars( $var )
339
    {
340
        if ( is_array( $var ) ) {
341
            $localized_vars = array();
342
            foreach ( (array)$var as $key => $value ) {
343
                $localized_vars[ $key ] = $this->encodeJsonVars( $value );
344
            }
345
            return $localized_vars;
346
        } else if ( is_scalar( $var ) ) {
347
            return html_entity_decode( (string)$var, ENT_QUOTES, 'UTF-8' );
348
        }
349
        return null;
350
    }
351
352
353
354
}
355
// End of file Iframe.php
356
// Location: /Iframe.php