Completed
Branch BUG-9042-ics-organizer-email (7b2e23)
by
unknown
29:24 queued 16:15
created

Iframe   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 339
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Importance

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

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 47 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
                    'site_theme'       => get_stylesheet_directory_uri() . DS
85
                                          . 'style.css?ver=' . EVENT_ESPRESSO_VERSION,
86
                    'dashicons'        => includes_url( 'css/dashicons.min.css?ver=' . $wp_version ),
87
                    'espresso_default' => EE_GLOBAL_ASSETS_URL
88
                                          . 'css/espresso_default.css?ver=' . EVENT_ESPRESSO_VERSION,
89
                ),
90
                $this
91
            )
92
        );
93
        $this->addScripts(
94
            apply_filters(
95
                'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__construct__default_js',
96
                array(
97
                    'jquery'        => includes_url( 'js/jquery/jquery.js?ver=' . $wp_version ),
98
                    'espresso_core' => EE_GLOBAL_ASSETS_URL
99
                                       . 'scripts/espresso_core.js?ver=' . EVENT_ESPRESSO_VERSION,
100
                ),
101
                $this
102
            )
103
        );
104
        if (
105
            apply_filters(
106
                'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__construct__load_default_theme_stylesheet',
107
                false
108
            )
109
        ) {
110
            $this->addStylesheets(
111
                apply_filters(
112
                    'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__construct__default_theme_stylesheet',
113
                    array('default_theme_stylesheet' => get_stylesheet_uri()),
114
                    $this
115
                )
116
            );
117
        }
118
    }
119
120
121
122
    /**
123
     * @param string $title
124
     * @throws \DomainException
125
     */
126
    public function setTitle( $title )
127
    {
128
        if ( empty( $title ) ) {
129
            throw new \DomainException(
130
                esc_html__( 'You must provide a page title in order to create an iframe.', 'event_espresso' )
131
            );
132
        }
133
        $this->title = $title;
134
    }
135
136
137
138
    /**
139
     * @param string $content
140
     * @throws \DomainException
141
     */
142
    public function setContent( $content )
143
    {
144
        if ( empty( $content ) ) {
145
            throw new \DomainException(
146
                esc_html__( 'You must provide content in order to create an iframe.', 'event_espresso' )
147
            );
148
        }
149
        $this->content = $content;
150
    }
151
152
153
154
    /**
155
     * @param boolean $enqueue_wp_assets
156
     */
157
    public function setEnqueueWpAssets( $enqueue_wp_assets )
158
    {
159
        $this->enqueue_wp_assets = filter_var( $enqueue_wp_assets, FILTER_VALIDATE_BOOLEAN );
160
    }
161
162
163
164
    /**
165
     * @param array $stylesheets
166
     * @throws \DomainException
167
     */
168
    public function addStylesheets( array $stylesheets )
169
    {
170
        if ( empty( $stylesheets ) ) {
171
            throw new \DomainException(
172
                esc_html__(
173
                    'A non-empty array of URLs, is required to add a CSS stylesheet to an iframe.',
174
                    'event_espresso'
175
                )
176
            );
177
        }
178
        foreach ( $stylesheets as $handle => $stylesheet ) {
179
            $this->css[ $handle ] = $stylesheet;
180
        }
181
    }
182
183
184
185
    /**
186
     * @param array $scripts
187
     * @param bool  $add_to_header
188
     * @throws \DomainException
189
     */
190
    public function addScripts( array $scripts, $add_to_header = false )
191
    {
192
        if ( empty( $scripts ) ) {
193
            throw new \DomainException(
194
                esc_html__(
195
                    'A non-empty array of URLs, is required to add Javascript to an iframe.',
196
                    'event_espresso'
197
                )
198
            );
199
        }
200
        foreach ( $scripts as $handle => $script ) {
201
            if ( $add_to_header ) {
202
                $this->header_js[ $handle ] = $script;
203
            } else {
204
                $this->footer_js[ $handle ] = $script;
205
            }
206
        }
207
    }
208
209
210
211
    /**
212
     * @param array  $vars
213
     * @param string $var_name
214
     * @throws \DomainException
215
     */
216
    public function addLocalizedVars( array $vars, $var_name = 'eei18n' )
217
    {
218
        if ( empty( $vars ) ) {
219
            throw new \DomainException(
220
                esc_html__(
221
                    'A non-empty array of vars, is required to add localized Javascript vars to an iframe.',
222
                    'event_espresso'
223
                )
224
            );
225
        }
226
        foreach ( $vars as $handle => $var ) {
227
            if ( $var_name === 'eei18n' ) {
228
                \EE_Registry::$i18n_js_strings[ $handle ] = $var;
229
            } else {
230
                if ( ! isset( $this->localized_vars[ $var_name ] ) ) {
231
                    $this->localized_vars[ $var_name ] = array();
232
                }
233
                $this->localized_vars[ $var_name ][ $handle ] = $var;
234
            }
235
        }
236
    }
237
238
239
240
    /**
241
     * @param string $utm_content
242
     * @throws \DomainException
243
     */
244
    public function display($utm_content = '')
245
    {
246
        $this->content .= \EEH_Template::powered_by_event_espresso(
247
            '',
248
            '',
249
            ! empty($utm_content) ? array('utm_content' => $utm_content) : array()
250
        );
251
        \EE_System::do_not_cache();
252
        echo $this->getTemplate();
253
        exit;
254
    }
255
256
257
258
    /**
259
     * @return string
260
     * @throws \DomainException
261
     */
262
    public function getTemplate()
263
    {
264
        return \EEH_Template::display_template(
265
            __DIR__ . DIRECTORY_SEPARATOR . 'iframe_wrapper.template.php',
266
            array(
267
                'title'             => apply_filters(
268
                    'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__title',
269
                    $this->title,
270
                    $this
271
                ),
272
                'content'           => apply_filters(
273
                    'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__content',
274
                    $this->content,
275
                    $this
276
                ),
277
                'enqueue_wp_assets' => apply_filters(
278
                    'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__enqueue_wp_assets',
279
                    $this->enqueue_wp_assets,
280
                    $this
281
                ),
282
                'css'               => (array)apply_filters(
283
                    'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__css_urls',
284
                    $this->css,
285
                    $this
286
                ),
287
                'header_js'         => (array)apply_filters(
288
                    'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__header_js_urls',
289
                    $this->header_js,
290
                    $this
291
                ),
292
                'footer_js'         => (array)apply_filters(
293
                    'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__footer_js_urls',
294
                    $this->footer_js,
295
                    $this
296
                ),
297
                'eei18n'            => apply_filters(
298
                    'FHEE___EventEspresso_core_libraries_iframe_display_Iframe__getTemplate__eei18n_js_strings',
299
                    \EE_Registry::localize_i18n_js_strings() . $this->localizeJsonVars(),
300
                    $this
301
                ),
302
                'notices'           => \EEH_Template::display_template(
303
                    EE_TEMPLATES . 'espresso-ajax-notices.template.php',
304
                    array(),
305
                    true
306
                ),
307
            ),
308
            true,
309
            true
310
        );
311
    }
312
313
314
315
    /**
316
     * localizeJsonVars
317
     *
318
     * @return string
319
     */
320
    public function localizeJsonVars()
321
    {
322
        $JSON = '';
323
        foreach ( (array)$this->localized_vars as $var_name => $vars ) {
324
            foreach ( (array)$vars as $key => $value ) {
325
                $this->localized_vars[ $var_name ] = $this->encodeJsonVars( $value );
326
            }
327
            $JSON .= "/* <![CDATA[ */ var {$var_name} = ";
328
            $JSON .= wp_json_encode( $this->localized_vars[ $var_name ] );
329
            $JSON .= '; /* ]]> */';
330
        }
331
        return $JSON;
332
    }
333
334
335
336
    /**
337
     * @param bool|int|float|string|array $var
338
     * @return array
339
     */
340
    public function encodeJsonVars( $var )
341
    {
342
        if ( is_array( $var ) ) {
343
            $localized_vars = array();
344
            foreach ( (array)$var as $key => $value ) {
345
                $localized_vars[ $key ] = $this->encodeJsonVars( $value );
346
            }
347
            return $localized_vars;
348
        } else if ( is_scalar( $var ) ) {
349
            return html_entity_decode( (string)$var, ENT_QUOTES, 'UTF-8' );
350
        }
351
        return null;
352
    }
353
354
355
356
}
357
// End of file Iframe.php
358
// Location: /Iframe.php