Completed
Branch BUG-10381-asset-loading (f91422)
by
unknown
170:16 queued 157:41
created

IframeEmbedButton   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 280
Duplicated Lines 6.79 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 19
loc 280
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 3

11 Methods

Rating   Name   Duplication   Size   Complexity  
A addFilterIframeEmbedButtonCallback() 0 14 4
A __construct() 0 6 1
A addEventEditorIframeEmbedButtonFilter() 0 15 1
A appendIframeEmbedButtonToSamplePermalinkHtml() 0 7 1
A eventEditorIframeEmbedButton() 0 18 3
A addActionIframeEmbedButton() 0 9 1
A addActionIframeEmbedButtonCallback() 0 4 1
A addFilterIframeEmbedButton() 0 10 1
B embedButtonHtml() 0 42 2
A addIframeEmbedButtonsSection() 0 19 1
A embedButtonAssets() 19 19 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace EventEspresso\core\libraries\iframe_display;
3
4
defined( 'ABSPATH' ) || exit;
5
6
7
8
/**
9
 * Class IframeEmbedButton
10
 *
11
 * @package       Event Espresso
12
 * @author        Brent Christensen
13
 * @since         4.9
14
 */
15
abstract class IframeEmbedButton
16
{
17
18
19
    /**
20
     * @var string $iframe_name
21
     */
22
    private $iframe_name;
23
24
    /**
25
     * @var string $route_name
26
     */
27
    private $route_name;
28
29
    /**
30
     * @var string $slug
31
     */
32
    private $slug;
33
34
    /**
35
     * @var boolean $append_filterable_content
36
     */
37
    private $append_filterable_content;
38
39
40
41
    /**
42
     * IframeEmbedButton constructor.
43
     *
44
     * @param string $iframe_name i18n name for the iframe. This will be used in HTML
45
     * @param string $route_name  the name of the registered route
46
     * @param string $slug        URL slug used for the thing the iframe button is being embedded in.
47
     *                            will most likely be "event" since that's the only usage atm
48
     */
49
    public function __construct( $iframe_name, $route_name, $slug = 'event' )
50
    {
51
        $this->iframe_name = $iframe_name;
52
        $this->route_name = $route_name;
53
        $this->slug = $slug;
54
    }
55
56
57
58
    /**
59
     * Adds an iframe embed code button to the Event editor.
60
     */
61
    public function addEventEditorIframeEmbedButtonFilter()
62
    {
63
        // add button for iframe code to event editor.
64
        add_filter(
65
            'get_sample_permalink_html',
66
            array( $this, 'appendIframeEmbedButtonToSamplePermalinkHtml' ),
67
            10,
68
            2
69
        );
70
        add_action(
71
            'admin_enqueue_scripts',
72
            array( $this, 'embedButtonAssets' ),
73
            10
74
        );
75
    }
76
77
78
79
    /**
80
     * @param $permalink_string
81
     * @param $id
82
     * @return string
83
     */
84
    public function appendIframeEmbedButtonToSamplePermalinkHtml( $permalink_string, $id )
85
    {
86
        return $this->eventEditorIframeEmbedButton(
87
            $permalink_string,
88
            $id
89
        );
90
    }
91
92
93
94
    /**
95
     * iframe embed code button to the Event editor.
96
     *
97
     * @param string $permalink_string
98
     * @param int    $id
99
     * @return string
100
     */
101
    public function eventEditorIframeEmbedButton(
102
        $permalink_string,
103
        $id
104
    ) {
105
        //make sure this is ONLY when editing and the event id has been set.
106
        if ( ! empty( $id ) ) {
107
            $post = get_post( $id );
108
            //if NOT event then let's get out.
109
            if ( $post->post_type !== 'espresso_events' ) {
110
                return $permalink_string;
111
            }
112
            $permalink_string .= $this->embedButtonHtml(
113
                array( $this->slug => $id ),
114
                'button-small'
115
            );
116
        }
117
        return $permalink_string;
118
    }
119
120
121
122
    /**
123
     * Adds an iframe embed code button via a WP do_action() as determined by the first parameter
124
     *
125
     * @param string $action name of the WP do_action() to hook into
126
     */
127
    public function addActionIframeEmbedButton( $action )
128
    {
129
        // add button for iframe code to event editor.
130
        add_action(
131
            $action,
132
            array( $this, 'addActionIframeEmbedButtonCallback' ),
133
            10, 2
134
        );
135
    }
136
137
138
139
    /**
140
     * @return void
141
     */
142
    public function addActionIframeEmbedButtonCallback()
143
    {
144
        echo $this->embedButtonHtml();
145
    }
146
147
148
149
    /**
150
     * Adds an iframe embed code button via a WP apply_filters() as determined by the first parameter
151
     *
152
     * @param string $filter     name of the WP apply_filters() to hook into
153
     * @param bool   $append     if true, will add iframe embed button to end of content,
154
     *                           else if false, will add to the beginning of the content
155
     */
156
    public function addFilterIframeEmbedButton( $filter, $append = true )
157
    {
158
        $this->append_filterable_content = $append;
159
        // add button for iframe code to event editor.
160
        add_filter(
161
            $filter,
162
            array( $this, 'addFilterIframeEmbedButtonCallback' ),
163
            10
164
        );
165
    }
166
167
168
169
    /**
170
     * @param array|string $filterable_content
171
     * @return array|string
172
     */
173
    public function addFilterIframeEmbedButtonCallback( $filterable_content )
174
    {
175
        $embedButtonHtml = $this->embedButtonHtml();
176
        if ( is_array( $filterable_content ) ) {
177
            $filterable_content = $this->append_filterable_content
178
                ? $filterable_content + array( $this->route_name => $embedButtonHtml )
179
                : array( $this->route_name => $embedButtonHtml ) + $filterable_content;
180
        } else {
181
            $filterable_content = $this->append_filterable_content
182
                ? $filterable_content . $embedButtonHtml
183
                : $embedButtonHtml . $filterable_content;
184
        }
185
        return $filterable_content;
186
    }
187
188
189
190
    /**
191
     * iframe_embed_html
192
     *
193
     * @param array  $query_args
194
     * @param string $button_class
195
     * @return string
196
     */
197
    public function embedButtonHtml( $query_args = array(), $button_class = '' )
198
    {
199
        // incoming args will replace the defaults listed here in the second array (union preserves first array)
200
        $query_args = (array)$query_args + array( $this->route_name => 'iframe' );
201
        $query_args = (array)apply_filters(
202
            'FHEE__EventEspresso_core_libraries_iframe_display_IframeEmbedButton__embedButtonHtml__query_args',
203
            $query_args
204
        );
205
        // add this route to our localized vars
206
        $iframe_module_routes = isset( \EE_Registry::$i18n_js_strings[ 'iframe_module_routes' ] )
207
            ? \EE_Registry::$i18n_js_strings[ 'iframe_module_routes' ]
208
            : array();
209
        $iframe_module_routes[ $this->route_name ] = $this->route_name;
210
        \EE_Registry::$i18n_js_strings[ 'iframe_module_routes' ] = $iframe_module_routes;
211
        $iframe_embed_html = \EEH_HTML::link(
212
            '#',
213
            sprintf( esc_html__( 'Embed %1$s', 'event_espresso' ), $this->iframe_name ),
214
            sprintf(
215
                esc_html__(
216
                    'click here to generate code for embedding %1$s iframe into another site.',
217
                    'event_espresso'
218
                ),
219
                \EEH_Inflector::add_indefinite_article( $this->iframe_name )
220
            ),
221
            "{$this->route_name}-iframe-embed-trigger-js",
222
            'iframe-embed-trigger-js button ' . $button_class,
223
            '',
224
            ' data-iframe_embed_button="#' . $this->route_name . '-iframe-js" tabindex="-1"'
225
        );
226
        $iframe_embed_html .= \EEH_HTML::div( '', "{$this->route_name}-iframe-js", 'iframe-embed-wrapper-js',
227
                                              'display:none;' );
228
        $iframe_embed_html .= esc_html(
229
            \EEH_HTML::div(
230
                '<iframe src="' . add_query_arg( $query_args, site_url() ) . '" width="100%" height="100%"></iframe>',
231
                '',
232
                '',
233
                'width:100%; height: 500px;'
234
            )
235
        );
236
        $iframe_embed_html .= \EEH_HTML::divx();
237
        return $iframe_embed_html;
238
    }
239
240
241
242
    /**
243
     * enqueue iframe button js
244
     */
245 View Code Duplication
    public 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...
246
    {
247
        \EE_Registry::$i18n_js_strings[ 'iframe_embed_title' ] = esc_html__(
248
            'copy and paste the following into any other site\'s content to display this event:',
249
            'event_espresso'
250
        );
251
        \EE_Registry::$i18n_js_strings[ 'iframe_embed_close_msg' ] = esc_html__(
252
            'click anywhere outside of this window to close it.',
253
            'event_espresso'
254
        );
255
        wp_register_script(
256
            'iframe_embed_button',
257
            plugin_dir_url( __FILE__ ) . 'iframe-embed-button.js',
258
            array( 'ee-dialog' ),
259
            EVENT_ESPRESSO_VERSION,
260
            true
261
        );
262
        wp_enqueue_script( 'iframe_embed_button' );
263
    }
264
265
266
267
    /**
268
     * generates embed button sections for admin pages
269
     *
270
     * @param array $embed_buttons
271
     * @return string
272
     */
273
    public function addIframeEmbedButtonsSection( array $embed_buttons )
274
    {
275
        $embed_buttons = (array)apply_filters(
276
            'FHEE__EventEspresso_core_libraries_iframe_display_IframeEmbedButton__addIframeEmbedButtonsSection__embed_buttons',
277
            $embed_buttons
278
        );
279
        // add button for iframe code to event editor.
280
        $html = \EEH_HTML::br( 2 );
281
        $html .= \EEH_HTML::h3( esc_html__( 'iFrame Embed Code', 'event_espresso' ) );
282
        $html .= \EEH_HTML::p(
283
            esc_html__(
284
                'Click the following button(s) to generate iframe HTML that will allow you to embed your event content within the content of other websites.',
285
                'event_espresso'
286
            )
287
        );
288
        $html .= ' &nbsp; ' . implode( ' &nbsp; ', $embed_buttons ) . ' ';
289
        $html .= \EEH_HTML::br( 2 );
290
        return $html;
291
    }
292
293
294
}
295
// End of file IframeEmbedButton.php
296
// Location: EventEspresso\core\libraries\iframe_display/IframeEmbedButton.php