Completed
Branch FET-9576-iframes (9d3203)
by
unknown
358:42 queued 343:43
created

Iframe::addEventEditorIframeEmbedButton()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 2
Metric Value
cc 1
eloc 10
c 6
b 1
f 2
nc 1
nop 1
dl 0
loc 16
rs 9.4285
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='
87
                                          . EVENT_ESPRESSO_VERSION,
88
                )
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='
98
                                       . EVENT_ESPRESSO_VERSION,
99
                )
100
            )
101
        );
102
    }
103
104
105
106
    /**
107
     * @param string $title
108
     * @throws \EE_Error
109
     */
110
    public function setTitle($title)
111
    {
112
        if (empty($title)) {
113
            throw new \EE_Error(
114
                __('You must provide a page title in order to create an iframe.', 'event_espresso')
115
            );
116
        }
117
        $this->title = $title;
118
    }
119
120
121
122
    /**
123
     * @param string $content
124
     * @throws \EE_Error
125
     */
126
    public function setContent($content)
127
    {
128
        if (empty($content)) {
129
            throw new \EE_Error(
130
                __('You must provide content in order to create an iframe.', 'event_espresso')
131
            );
132
        }
133
        $this->content = $content;
134
    }
135
136
137
138
    /**
139
     * @param boolean $enqueue_wp_assets
140
     */
141
    public function setEnqueueWpAssets($enqueue_wp_assets)
142
    {
143
        $this->enqueue_wp_assets = filter_var($enqueue_wp_assets, FILTER_VALIDATE_BOOLEAN);
144
    }
145
146
147
148
    /**
149
     * @param array $stylesheets
150
     * @throws \EE_Error
151
     */
152
    public function addStylesheets(array $stylesheets)
153
    {
154
        if (empty($stylesheets)) {
155
            throw new \EE_Error(
156
                __('A non-empty array of URLs, is required to add a CSS stylesheet to an iframe.', 'event_espresso')
157
            );
158
        }
159
        foreach ($stylesheets as $handle => $stylesheet) {
160
            $this->css[$handle] = $stylesheet;
161
        }
162
    }
163
164
165
166
    /**
167
     * @param array $scripts
168
     * @param bool  $add_to_header
169
     * @throws \EE_Error
170
     */
171
    public function addScripts(array $scripts, $add_to_header = false)
172
    {
173
        if (empty($scripts)) {
174
            throw new \EE_Error(
175
                __('A non-empty array of URLs, is required to add Javascript to an iframe.', 'event_espresso')
176
            );
177
        }
178
        foreach ($scripts as $handle => $script) {
179
            if ($add_to_header) {
180
                $this->header_js[$handle] = $script;
181
            } else {
182
                $this->footer_js[$handle] = $script;
183
            }
184
        }
185
    }
186
187
188
189
    /**
190
     * @param array  $vars
191
     * @param string $var_name
192
     * @throws \EE_Error
193
     */
194
    public function addLocalizedVars(array $vars, $var_name = 'eei18n')
195
    {
196
        if (empty($vars)) {
197
            throw new \EE_Error(
198
                __('A non-empty array of vars, is required to add localized Javascript vars to an iframe.',
199
                    'event_espresso')
200
            );
201
        }
202
        foreach ($vars as $handle => $var) {
203
            if ($var_name === 'eei18n') {
204
                \EE_Registry::$i18n_js_strings[$handle] = $var;
205
            } else {
206
                if ( ! isset($this->localized_vars[$var_name])) {
207
                    $this->localized_vars[$var_name] = array();
208
                }
209
                $this->localized_vars[$var_name][$handle] = $var;
210
            }
211
        }
212
    }
213
214
215
216
    /**
217
     * @return void
218
     */
219
    public function display()
220
    {
221
        echo $this->getTemplate();
222
        exit;
223
    }
224
225
226
227
    /**
228
     * @return string
229
     */
230
    public function getTemplate()
231
    {
232
        return \EEH_Template::display_template(
233
            __DIR__ . '\iframe_wrapper.template.php',
234
            array(
235
                'title'             => apply_filters(
236
                    'FHEE__\EventEspresso\core\libraries\iframe_display\Iframe__getTemplate__title',
237
                    $this->title
238
                ),
239
                'content'           => apply_filters(
240
                    'FHEE__\EventEspresso\core\libraries\iframe_display\Iframe__getTemplate__content',
241
                    $this->content
242
                ),
243
                'enqueue_wp_assets' => apply_filters(
244
                    'FHEE__\EventEspresso\core\libraries\iframe_display\Iframe__getTemplate__enqueue_wp_assets',
245
                    $this->enqueue_wp_assets
246
                ),
247
                'css'               => (array)apply_filters(
248
                    'FHEE__\EventEspresso\core\libraries\iframe_display\Iframe__getTemplate__css_urls',
249
                    $this->css
250
                ),
251
                'header_js'         => (array)apply_filters(
252
                    'FHEE__\EventEspresso\core\libraries\iframe_display\Iframe__getTemplate__header_js_urls',
253
                    $this->header_js
254
                ),
255
                'footer_js'         => (array)apply_filters(
256
                    'FHEE__\EventEspresso\core\libraries\iframe_display\Iframe__getTemplate__footer_js_urls',
257
                    $this->footer_js
258
                ),
259
                'eei18n'            => apply_filters(
260
                    'FHEE__\EventEspresso\core\libraries\iframe_display\Iframe__getTemplate__eei18n_js_strings',
261
                    \EE_Registry::localize_i18n_js_strings() . $this->localizeJsonVars()
262
                ),
263
                'notices'           => \EEH_Template::display_template(
264
                    EE_TEMPLATES . 'espresso-ajax-notices.template.php',
265
                    array(),
266
                    true
267
                ),
268
            ),
269
            true
270
        );
271
    }
272
273
274
275
    /**
276
     * localizeJsonVars
277
     *
278
     * @return string
279
     */
280
    public function localizeJsonVars()
281
    {
282
        $JSON = '';
283
        $localized_vars = (array)$this->localized_vars;
284
        foreach ($localized_vars as $var_name => $vars) {
285 View Code Duplication
            foreach ($vars as $key => $value) {
286
                if (is_scalar($value)) {
287
                    $localized_vars[$key] = html_entity_decode((string)$value, ENT_QUOTES, 'UTF-8');
288
                }
289
            }
290
            $JSON .= "/* <![CDATA[ */ var $var_name = " . wp_json_encode($localized_vars) . '; /* ]]> */';
291
        }
292
        return $JSON;
293
    }
294
295
296
297
    /**
298
     * Adds an iframe embed code button to the Event editor.
299
     *
300
     * @param string $route_name the named module route that generates the iframe
301
     */
302
    public static function addEventEditorIframeEmbedButton($route_name)
303
    {
304
        // add button for iframe code to event editor.
305
        add_filter(
306
            'get_sample_permalink_html',
307
            function ($permalink_string, $id, $new_title, $new_slug) use ($route_name) {
308
                return Iframe::eventListIframeEmbedButton($permalink_string, $id, $new_title, $new_slug, $route_name);
309
            },
310
            10, 2
311
        );
312
        add_action(
313
            'admin_enqueue_scripts',
314
            array('\EventEspresso\core\libraries\iframe_display\Iframe', 'EventsAdminEmbedButtonAssets'),
315
            10
316
        );
317
    }
318
319
320
321
    /**
322
     * iframe embed code button to the Event editor.
323
     *
324
     * @param string $permalink_string
325
     * @param int    $id
326
     * @param string $new_title  (unused)
327
     * @param string $new_slug   (unused)
328
     * @param string $route_name the named module route that generates the iframe
329
     * @return string
330
     */
331
    public static function eventListIframeEmbedButton(
332
        $permalink_string,
333
        $id,
334
        $new_title = '',
0 ignored issues
show
Unused Code introduced by
The parameter $new_title is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
335
        $new_slug = '',
0 ignored issues
show
Unused Code introduced by
The parameter $new_slug is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
336
        $route_name = ''
337
    ) {
338
        //make sure this is ONLY when editing and the event id has been set.
339
        if ( ! empty($id)) {
340
            $post = get_post($id);
341
            //if NOT event then let's get out.
342
            if ($post->post_type !== 'espresso_events') {
343
                return $permalink_string;
344
            }
345
            $permalink_string .= Iframe::embedButtonHtml(
346
                'Event',
347
                $route_name,
348
                array('event' => $id),
349
                'button-small'
350
            );
351
        }
352
        return $permalink_string;
353
    }
354
355
356
357
    /**
358
     * Adds an iframe embed code button to the Event editor.
359
     *
360
     * @param string $route_name the named module route that generates the iframe
361
     */
362
    public static function addEventListIframeEmbedButton($route_name)
363
    {
364
        // add button for iframe code to event editor.
365
        add_action(
366
            'AHEE__admin_list_wrapper_template__after_list_table',
367
            function ($page_slug) use ($route_name) {
368
                //make sure this is ONLY when editing and the event id has been set.
369
                if ($page_slug === 'espresso_events') {
370
                    echo \EEH_HTML::h3(__('iFrame Embed Code', 'event_espresso'));
371
                    echo \EEH_HTML::p(__('Click the following buttons to generate iframe HTML that will allow you to embed your event content within the content of other websites.',
372
                        'event_espresso'));
373
                    echo Iframe::embedButtonHtml(
374
                        'Events List',
375
                        $route_name
376
                    );
377
                }
378
            },
379
            10, 1
380
        );
381
        add_action(
382
            'admin_enqueue_scripts',
383
            array('\EventEspresso\core\libraries\iframe_display\Iframe', 'EventsAdminEmbedButtonAssets'),
384
            10
385
        );
386
    }
387
388
389
390
    /**
391
     * Adds an iframe embed code button via a WP do_action() as determined by the first parameter
392
     *
393
     * @param string $iframe_name
394
     * @param string $route_name the named module route that generates the iframe
395
     * @param string $action     name of the WP do_action() to hook into
396
     */
397
    public static function addActionIframeEmbedButton($iframe_name, $route_name, $action)
398
    {
399
        // add button for iframe code to event editor.
400
        add_action(
401
            $action,
402
            function () use ($iframe_name, $route_name) {
403
                echo Iframe::embedButtonHtml($iframe_name, $route_name);
404
            },
405
            10
406
        );
407
    }
408
409
410
411
    /**
412
     * iframe_embed_html
413
     *
414
     * @param string $iframe_name
415
     * @param string $route_name the named module route that generates the iframe
416
     * @param array  $query_args
417
     * @param string $button_class
418
     * @return string
419
     */
420
    public static function embedButtonHtml($iframe_name, $route_name, $query_args = array(), $button_class = '')
421
    {
422
        $query_args = ! empty($query_args) ? $query_args : array($route_name => 'iframe');
423
        // add this route to our localized vars
424
        $iframe_module_routes = isset(\EE_Registry::$i18n_js_strings['iframe_module_routes'])
425
            ? \EE_Registry::$i18n_js_strings['iframe_module_routes']
426
            : array();
427
        $iframe_module_routes[$route_name] = $route_name;
428
        \EE_Registry::$i18n_js_strings['iframe_module_routes'] = $iframe_module_routes;
429
        $iframe_embed_html = \EEH_HTML::link(
430
            '#',
431
            sprintf(__('Embed %1$s', 'event_espresso'), $iframe_name),
432
            sprintf(
433
                __(
434
                    'click here to generate code for embedding an %1$s iframe into another site.',
435
                    'event_espresso'
436
                ),
437
                $iframe_name
438
            ),
439
            "$route_name-iframe-embed-trigger-js",
440
            'iframe-embed-trigger-js button ' . $button_class,
441
            '',
442
            ' data-iframe_embed_button="#' . $route_name . '-iframe-js" tabindex="-1"'
443
        );
444
        $iframe_embed_html .= \EEH_HTML::div('', "$route_name-iframe-js", 'iframe-embed-wrapper-js', 'display:none;');
445
        $iframe_embed_html .= \EEH_HTML::div(
446
            '<iframe src="' . add_query_arg($query_args, site_url()) . '" width="100%" height="100%"></iframe>',
447
            '', '', 'width:100%; height: 500px;'
448
        );
449
        $iframe_embed_html .= \EEH_HTML::divx();
450
        return $iframe_embed_html;
451
    }
452
453
454
455
    /**
456
     * iframe button js on admin events list or event editor page
457
     */
458
    public static function EventsAdminEmbedButtonAssets()
459
    {
460
        if (\EE_Registry::instance()->REQ->get('page') === 'espresso_events') {
461
            Iframe::embedButtonAssets();
462
            \EE_Registry::$i18n_js_strings['iframe_embed_title'] = __(
463
                'copy and paste the following into any other site\'s content to display this event:',
464
                'event_espresso'
465
            );
466
        }
467
    }
468
469
470
471
    /**
472
     * enqueue iframe button js
473
     */
474 View Code Duplication
    public static function embedButtonAssets()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
475
    {
476
        \EE_Registry::$i18n_js_strings['iframe_embed_close_msg'] = __(
477
            'click anywhere outside of this window to close it.',
478
            'event_espresso'
479
        );
480
        wp_register_script(
481
            'iframe_embed_button',
482
            plugin_dir_url(__FILE__) . 'iframe-embed-button.js',
483
            array('ee-dialog'),
484
            EVENT_ESPRESSO_VERSION,
485
            true
486
        );
487
        wp_enqueue_script('iframe_embed_button');
488
    }
489
490
491
492
}
493
// End of file Iframe.php
494
// Location: /Iframe.php