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 |
||
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 ) |
||
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 = '' ) |
||
239 | |||
240 | |||
241 | |||
242 | /** |
||
243 | * enqueue iframe button js |
||
244 | */ |
||
245 | View Code Duplication | public function embedButtonAssets() |
|
|
|||
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 ) |
||
292 | |||
293 | |||
294 | } |
||
295 | // End of file IframeEmbedButton.php |
||
296 | // Location: EventEspresso\core\libraries\iframe_display/IframeEmbedButton.php |
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.