Completed
Branch FET-9576-iframes (0c4243)
by
unknown
62:05 queued 49:26
created

Iframe::getTemplate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 51
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 36
nc 2
nop 0
dl 0
loc 51
rs 9.4109
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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