Completed
Branch FET/asset-manager (433489)
by
unknown
32:42 queued 18:11
created

Iframe::encodeJsonVars()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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