Completed
Branch BUG/11475/decode-site-title-fo... (bbd86e)
by
unknown
13:39 queued 25s
created

Iframe::addLocalizedVars()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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