Total Complexity | 66 |
Total Lines | 735 |
Duplicated Lines | 2.04 % |
Changes | 0 |
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:
Complex classes like WordPoints_Points_Hooks often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WordPoints_Points_Hooks, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
54 | final class WordPoints_Points_Hooks { |
||
55 | |||
56 | // |
||
57 | // Private Vars. |
||
58 | // |
||
59 | |||
60 | /** |
||
61 | * A list of registered hook type class names. |
||
62 | * |
||
63 | * Holds the list of classes registered with the self::register() method, |
||
64 | * accessed only by self::initialize_hooks(). |
||
65 | * |
||
66 | * @since 1.5.0 |
||
67 | * |
||
68 | * @param string[] $classes |
||
69 | */ |
||
70 | private static $classes; |
||
71 | |||
72 | /** |
||
73 | * The instances of the handlers for the registered types of points hooks. |
||
74 | * |
||
75 | * @since 1.5.0 |
||
76 | * |
||
77 | * @type WordPoints_Points_Hook[] $hook_types |
||
78 | */ |
||
79 | private static $hook_types = array(); |
||
80 | |||
81 | /** |
||
82 | * Whether to display network hooks. |
||
83 | * |
||
84 | * @since 1.2.0 |
||
85 | * |
||
86 | * @type bool $network_mode |
||
87 | */ |
||
88 | private static $network_mode = false; |
||
89 | |||
90 | // |
||
91 | // Public Methods. |
||
92 | // |
||
93 | |||
94 | /** |
||
95 | * Initialize the class. |
||
96 | * |
||
97 | * @since 1.0.0 |
||
98 | * @deprecated 2.1.0 |
||
99 | */ |
||
100 | public static function init() { |
||
101 | _deprecated_function( __METHOD__, '2.1.0' ); |
||
|
|||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Register a points hook type's handler class. |
||
106 | * |
||
107 | * @since 1.0.0 |
||
108 | * |
||
109 | * @param string $class_name A 'WordPoints_Points_Hook' class name. |
||
110 | */ |
||
111 | public static function register( $class_name ) { |
||
112 | |||
113 | self::$classes[] = $class_name; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Register all points hooks. |
||
118 | * |
||
119 | * @since 1.0.0 |
||
120 | * |
||
121 | * @action wordpoints_extensions_loaded Added by the init() method. |
||
122 | */ |
||
123 | public static function initialize_hooks() { |
||
124 | |||
125 | /** |
||
126 | * Points hooks may be registered on this action. |
||
127 | * |
||
128 | * @since 1.4.0 |
||
129 | */ |
||
130 | do_action( 'wordpoints_points_hooks_register' ); |
||
131 | |||
132 | $classes = array_unique( self::$classes ); |
||
133 | |||
134 | foreach ( $classes as $class_name ) { |
||
135 | |||
136 | $hook_type = new $class_name(); |
||
137 | self::$hook_types[ $hook_type->get_id_base() ] = $hook_type; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * All points hooks registered and initialized. |
||
142 | * |
||
143 | * @since 1.0.0 |
||
144 | */ |
||
145 | do_action( 'wordpoints_points_hooks_registered' ); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Get a list of registered points hook handlers. |
||
150 | * |
||
151 | * @since 1.5.0 |
||
152 | * |
||
153 | * @return WordPoints_Points_Hook[] The registered points hook types. |
||
154 | */ |
||
155 | public static function get_handlers() { |
||
156 | |||
157 | return self::$hook_types; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Get the object representing the hook type of a hook by its ID. |
||
162 | * |
||
163 | * @since 1.0.0 |
||
164 | * |
||
165 | * @param string $hook_id The unique ID of the hook to get the handler for. |
||
166 | * |
||
167 | * @return WordPoints_Points_Hook|false The hook object, or false for invalid ID. |
||
168 | */ |
||
169 | public static function get_handler( $hook_id ) { |
||
170 | |||
171 | list( $hook_type, $id_number ) = explode( '-', $hook_id ); |
||
172 | |||
173 | $hook_type = self::get_handler_by_id_base( $hook_type ); |
||
174 | |||
175 | if ( false === $hook_type ) { |
||
176 | return false; |
||
177 | } |
||
178 | |||
179 | $type = ( self::$network_mode ) ? 'network' : 'standard'; |
||
180 | |||
181 | $instances = $hook_type->get_instances( $type ); |
||
182 | |||
183 | if ( ! isset( $instances[ $id_number ] ) ) { |
||
184 | return false; |
||
185 | } |
||
186 | |||
187 | $hook_type->set_number( $id_number ); |
||
188 | |||
189 | return $hook_type; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Get the handler object for a hook by id_base (hook type). |
||
194 | * |
||
195 | * This is used to get the handler for a new hook instance so that it can be |
||
196 | * created. |
||
197 | * |
||
198 | * @since 1.0.0 |
||
199 | * |
||
200 | * @param string $id_base The basic identifier the type of hook. |
||
201 | * |
||
202 | * @return WordPoints_Points_Hook|false False if no handler found. |
||
203 | */ |
||
204 | public static function get_handler_by_id_base( $id_base ) { |
||
205 | |||
206 | if ( ! isset( self::$hook_types[ $id_base ] ) ) { |
||
207 | return false; |
||
208 | } |
||
209 | |||
210 | return self::$hook_types[ $id_base ]; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Delete the database data for a list of hook types. |
||
215 | * |
||
216 | * @since 1.7.0 |
||
217 | * @deprecated 2.0.0 Use an un/installer class instead. |
||
218 | * |
||
219 | * @param array|string $hook_types The hook type(s) to uninstall. |
||
220 | * @param int[] $site_ids List of site IDs where this hook type is |
||
221 | * installed. Only needed if on multisite. If |
||
222 | * omitted, the current site ID is used. |
||
223 | */ |
||
224 | public static function uninstall_hook_types( $hook_types, array $site_ids = null ) { |
||
225 | |||
226 | _deprecated_function( __METHOD__, '2.0.0', 'WordPoints_Un_Installer_Base::$uninstall' ); |
||
227 | |||
228 | $hook_types = (array) $hook_types; |
||
229 | |||
230 | if ( is_multisite() ) { |
||
231 | |||
232 | foreach ( $hook_types as $hook_type ) { |
||
233 | delete_site_option( "wordpoints_hook-{$hook_type}" ); |
||
234 | } |
||
235 | |||
236 | if ( ! isset( $site_ids ) ) { |
||
237 | $site_ids = array( get_current_blog_id() ); |
||
238 | } |
||
239 | |||
240 | foreach ( $site_ids as $site_id ) { |
||
241 | |||
242 | switch_to_blog( $site_id ); |
||
243 | foreach ( $hook_types as $hook_type ) { |
||
244 | delete_option( "wordpoints_hook-{$hook_type}" ); |
||
245 | } |
||
246 | restore_current_blog(); |
||
247 | } |
||
248 | |||
249 | } else { |
||
250 | |||
251 | foreach ( $hook_types as $hook_type ) { |
||
252 | delete_option( "wordpoints_hook-{$hook_type}" ); |
||
253 | } |
||
254 | } |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Displays a list of available hooks for the Points Hooks administration panel. |
||
259 | * |
||
260 | * @since 1.0.0 |
||
261 | * |
||
262 | * @uses WordPoints_Points_Hooks::sort_name_callback() |
||
263 | * @uses WordPoints_Points_Hooks::list_hook() |
||
264 | */ |
||
265 | public static function list_hooks() { |
||
266 | |||
267 | // Sort the hooks by name. |
||
268 | $hook_types = self::$hook_types; |
||
269 | uasort( $hook_types, array( __CLASS__, 'sort_name_callback' ) ); |
||
270 | |||
271 | $disabled_hooks = wordpoints_get_maybe_network_array_option( |
||
272 | 'wordpoints_legacy_points_hooks_disabled' |
||
273 | , is_network_admin() |
||
274 | ); |
||
275 | |||
276 | $i = 0; |
||
277 | |||
278 | // Display a representative for each hook type. |
||
279 | foreach ( $hook_types as $id_base => $hook_type ) { |
||
280 | |||
281 | if ( isset( $disabled_hooks[ $id_base ] ) ) { |
||
282 | continue; |
||
283 | } |
||
284 | |||
285 | $i++; |
||
286 | |||
287 | $args = $hook_type->get_options(); |
||
288 | |||
289 | $args['_add'] = 'multi'; |
||
290 | $args['_display'] = 'template'; |
||
291 | $args['_multi_num'] = $hook_type->next_hook_id_number(); |
||
292 | $args['_id_slug'] = $i; |
||
293 | |||
294 | $hook_type->set_options( $args ); |
||
295 | |||
296 | self::list_hook( $hook_type->get_id( 0 ), $hook_type ); |
||
297 | } |
||
298 | |||
299 | // If there were none, give the user a message. |
||
300 | if ( empty( $hook_types ) ) { |
||
301 | |||
302 | echo '<div class="wordpoints-no-hooks">' |
||
303 | . esc_html__( 'There are no points hooks currently available.', 'wordpoints' ) |
||
304 | . '</div>'; |
||
305 | } |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Display hooks by points type. |
||
310 | * |
||
311 | * @since 1.0.0 |
||
312 | * @since 1.2.0 Now displays only the forms for the hooks, not the points type. |
||
313 | * |
||
314 | * @uses wordpoints_is_points_type() To check if $slug is valid. |
||
315 | * @uses self::get_points_type_hooks() To get all hooks for this points type. |
||
316 | * |
||
317 | * @param string $slug The slug of the points type to display the hooks for. |
||
318 | * |
||
319 | * @return void |
||
320 | */ |
||
321 | public static function list_by_points_type( $slug ) { |
||
322 | |||
323 | if ( '_inactive_hooks' !== $slug && ! wordpoints_is_points_type( $slug ) ) { |
||
324 | return; |
||
325 | } |
||
326 | |||
327 | $points_type_hooks = self::get_points_type_hooks( $slug ); |
||
328 | |||
329 | foreach ( $points_type_hooks as $hook_id ) { |
||
330 | |||
331 | list( $hook_type ) = explode( '-', $hook_id ); |
||
332 | |||
333 | $hook_type = self::get_handler_by_id_base( $hook_type ); |
||
334 | |||
335 | if ( false === $hook_type ) { |
||
336 | continue; |
||
337 | } |
||
338 | |||
339 | $options = $hook_type->get_options(); |
||
340 | |||
341 | $options['_display'] = 'instance'; |
||
342 | |||
343 | unset( $options['_add'] ); |
||
344 | |||
345 | $options['_id_slug'] = $slug; |
||
346 | |||
347 | $hook_type->set_options( $options ); |
||
348 | |||
349 | self::list_hook( $hook_id, $hook_type, $slug ); |
||
350 | } |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Set network mode. |
||
355 | * |
||
356 | * When network mode is on, the network-wide hooks will be displayed. This is |
||
357 | * only relevant on multisite installs. |
||
358 | * |
||
359 | * Network mode is off by default. |
||
360 | * |
||
361 | * @since 1.2.0 |
||
362 | * |
||
363 | * @param bool $on Whether to turn network mode on or off. |
||
364 | */ |
||
365 | public static function set_network_mode( $on ) { |
||
366 | |||
367 | if ( $on !== self::$network_mode ) { |
||
368 | self::$network_mode = (bool) $on; |
||
369 | } |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * Get the network mode. |
||
374 | * |
||
375 | * @see WordPoints_Points_Hooks::set_network_mode() |
||
376 | * |
||
377 | * @since 1.2.0 |
||
378 | * |
||
379 | * @return bool Whether network mode is on. |
||
380 | */ |
||
381 | public static function get_network_mode() { |
||
382 | |||
383 | return self::$network_mode; |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * Retrieve full list of points types and their hooks. |
||
388 | * |
||
389 | * @since 1.0.0 |
||
390 | * |
||
391 | * @return array |
||
392 | */ |
||
393 | public static function get_points_types_hooks() { |
||
394 | |||
395 | return wordpoints_get_maybe_network_array_option( |
||
396 | 'wordpoints_points_types_hooks' |
||
397 | , self::$network_mode |
||
398 | ); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Retrieve the hooks for a points type. |
||
403 | * |
||
404 | * @since 1.0.0 |
||
405 | * |
||
406 | * @uses WordPoints_Points_Hooks::get_points_types_hooks() |
||
407 | * @param string $slug The slug for the points type. |
||
408 | * |
||
409 | * @return array |
||
410 | */ |
||
411 | public static function get_points_type_hooks( $slug ) { |
||
412 | |||
413 | $points_types_hooks = self::get_points_types_hooks(); |
||
414 | |||
415 | if ( isset( $points_types_hooks[ $slug ] ) && is_array( $points_types_hooks[ $slug ] ) ) { |
||
416 | $points_type_hooks = $points_types_hooks[ $slug ]; |
||
417 | } else { |
||
418 | $points_type_hooks = array(); |
||
419 | } |
||
420 | |||
421 | return $points_type_hooks; |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * Save a full list of points types and their hooks. |
||
426 | * |
||
427 | * @since 1.0.0 |
||
428 | * |
||
429 | * @param array $points_types_hooks The list of points types and their hooks. |
||
430 | */ |
||
431 | public static function save_points_types_hooks( array $points_types_hooks ) { |
||
432 | |||
433 | if ( self::$network_mode ) { |
||
434 | update_site_option( 'wordpoints_points_types_hooks', $points_types_hooks ); |
||
435 | } else { |
||
436 | update_option( 'wordpoints_points_types_hooks', $points_types_hooks ); |
||
437 | } |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Retrieve points type by hook ID. |
||
442 | * |
||
443 | * @since 1.0.0 |
||
444 | * |
||
445 | * @param string $hook_id The ID of the hook. |
||
446 | * |
||
447 | * @return string|false The points type for the hook. False if not found. |
||
448 | */ |
||
449 | public static function get_points_type( $hook_id ) { |
||
450 | |||
451 | foreach ( self::get_points_types_hooks() as $points_type => $hooks ) { |
||
452 | |||
453 | if ( in_array( $hook_id, $hooks, true ) ) { |
||
454 | return $points_type; |
||
455 | } |
||
456 | } |
||
457 | |||
458 | return false; |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * Retrieve empty settings for hooks. |
||
463 | * |
||
464 | * @since 1.0.0 |
||
465 | * |
||
466 | * @return array[] An array of empty arrays indexed by points type slugs. |
||
467 | */ |
||
468 | public static function get_defaults() { |
||
469 | |||
470 | $defaults = array(); |
||
471 | |||
472 | foreach ( wordpoints_get_points_types() as $slug => $settings ) { |
||
473 | |||
474 | $defaults[ $slug ] = array(); |
||
475 | } |
||
476 | |||
477 | return $defaults; |
||
478 | } |
||
479 | |||
480 | /** |
||
481 | * Display a settings form for a type of points. |
||
482 | * |
||
483 | * By default, this function wraps the form in a widget like container. To over- |
||
484 | * ride this, the seccond parameter may be set to 'none'. If $slug is not set, |
||
485 | * $wrap will always be 'none'. If the inputs should be wrapped only in a form |
||
486 | * and the .hook-content div, then $wrap may be set to 'hook-content'. |
||
487 | * |
||
488 | * @since 1.0.0 |
||
489 | * @deprecated 2.1.0 |
||
490 | * |
||
491 | * @uses do_action() Calls 'wordpoints_points_type_form_top' at the top of the |
||
492 | * settings form with $slug and $settings. A null slug indicated a new |
||
493 | * points type is being added. Calls 'wordpoints_points_type_form_bottom' |
||
494 | * at the bottom of the form, with the same values. |
||
495 | * |
||
496 | * @param string $slug The slug for this type of points. |
||
497 | * @param string $wrap Whether to wrap the form inputs in a "widget" or not. |
||
498 | */ |
||
499 | public static function points_type_form( $slug = null, $wrap = 'hook' ) { |
||
659 | <?php |
||
660 | } |
||
661 | |||
662 | /** |
||
663 | * Display the administration form for a hook. |
||
664 | * |
||
665 | * The $points_type parameter is only needed if the hook is hooked to a points |
||
666 | * type. |
||
667 | * |
||
668 | * @since 1.0.0 |
||
669 | * |
||
670 | * @param string $hook_id The ID of a hook. |
||
671 | * @param WordPoints_Points_Hook $hook A points hook object. |
||
672 | * @param string $points_type The slug for a points type. |
||
673 | */ |
||
674 | private static function list_hook( $hook_id, $hook, $points_type = null ) { |
||
675 | |||
676 | $number = $hook->get_number_by_id( $hook_id ); |
||
677 | $id_base = $hook->get_id_base(); |
||
678 | $options = $hook->get_options(); |
||
679 | |||
680 | $id_format = $hook_id; |
||
681 | |||
682 | $multi_number = ( isset( $options['_multi_num'] ) ) ? $options['_multi_num'] : ''; |
||
683 | $add_new = ( isset( $options['_add'] ) ) ? $options['_add'] : ''; |
||
684 | |||
685 | // Prepare the URL query string. |
||
686 | $query_arg = array( 'edithook' => $id_format ); |
||
687 | |||
688 | if ( $add_new ) { |
||
689 | |||
690 | $query_arg['addnew'] = 1; |
||
691 | |||
692 | if ( $multi_number ) { |
||
693 | |||
694 | $query_arg['num'] = $multi_number; |
||
695 | $query_arg['base'] = $id_base; |
||
696 | } |
||
697 | |||
698 | } else { |
||
699 | |||
700 | $query_arg['points_type'] = $points_type; |
||
701 | } |
||
702 | |||
703 | if ( isset( $options['_display'] ) && 'template' === $options['_display'] ) { |
||
704 | |||
705 | /* |
||
706 | * We aren't outputting the form for a hook, but a template form for this |
||
707 | * hook type. (In other words, we are in the "Available Hooks" section.) |
||
708 | */ |
||
709 | |||
710 | // number === 0 implies a template where id numbers are replaced by a generic '__i__'. |
||
711 | $number = 0; |
||
712 | |||
713 | // With id_base hook id's are constructed like {$id_base}-{$id_number}. |
||
714 | $id_format = "{$id_base}-__i__"; |
||
715 | } |
||
716 | |||
717 | ?> |
||
718 | |||
719 | <div id="hook-<?php echo esc_html( $options['_id_slug'] ); ?>_<?php echo esc_attr( $id_format ); ?>" class="hook <?php echo esc_attr( $options['_classname'] ); ?>"> |
||
720 | <div class="hook-top"> |
||
721 | <div class="hook-title-action"> |
||
722 | <a class="hook-action hide-if-no-js" href="#available-hooks"></a> |
||
723 | <a class="hook-control-edit hide-if-js" href="<?php echo esc_url( add_query_arg( $query_arg ) ); ?>"> |
||
724 | <span class="edit"><?php echo esc_html_x( 'Edit', 'hook', 'wordpoints' ); ?></span> |
||
725 | <span class="add"><?php echo esc_html_x( 'Add', 'hook', 'wordpoints' ); ?></span> |
||
726 | <span class="screen-reader-text"><?php echo esc_html( strip_tags( $hook->get_name() ) ); ?></span> |
||
727 | </a> |
||
728 | </div> |
||
729 | <div class="hook-title"><h3><?php echo esc_html( strip_tags( $hook->get_name() ) ); ?><span class="in-hook-title"></span></h3></div> |
||
730 | </div> |
||
731 | |||
732 | <div class="hook-inside"> |
||
733 | <form method="post"> |
||
734 | <div class="hook-content"> |
||
735 | <?php |
||
736 | |||
737 | $has_form = $hook->form_callback( $number ); |
||
738 | |||
739 | ?> |
||
740 | </div> |
||
741 | |||
742 | <input type="hidden" name="hook-id" class="hook-id" value="<?php echo esc_attr( $id_format ); ?>" /> |
||
743 | <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr( $id_base ); ?>" /> |
||
744 | <input type="hidden" name="hook-width" class="hook-width" value="<?php echo isset( $options['width'] ) ? esc_attr( $options['width'] ) : ''; ?>" /> |
||
745 | <input type="hidden" name="hook-height" class="hook-height" value="<?php echo isset( $options['height'] ) ? esc_attr( $options['height'] ) : ''; ?>" /> |
||
746 | <input type="hidden" name="hook_number" class="hook_number" value="<?php echo esc_attr( $number ); ?>" /> |
||
747 | <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr( $multi_number ); ?>" /> |
||
748 | <input type="hidden" name="add_new" class="add_new" value="<?php echo esc_attr( $add_new ); ?>" /> |
||
749 | |||
750 | <div class="hook-control-actions"> |
||
751 | <div class="alignleft"> |
||
752 | <a class="hook-control-remove" href="#remove"><?php esc_html_e( 'Delete', 'wordpoints' ); ?></a> | |
||
753 | <a class="hook-control-close" href="#close"><?php esc_html_e( 'Close', 'wordpoints' ); ?></a> |
||
754 | </div> |
||
755 | <div class="alignright<?php echo ( false === $has_form ) ? ' hook-control-noform' : ''; ?>"> |
||
756 | <?php submit_button( __( 'Save', 'wordpoints' ), 'primary hook-control-save right', 'savehook', false, array( 'id' => "hook-{$id_format}-savehook" ) ); ?> |
||
757 | <span class="spinner"></span> |
||
758 | </div> |
||
759 | <br class="clear" /> |
||
760 | </div> |
||
761 | </form> |
||
762 | </div> |
||
763 | |||
764 | <div class="hook-description"> |
||
765 | <?php if ( ! empty( $options['description'] ) ) : ?> |
||
766 | <?php echo esc_html( $options['description'] ); ?> |
||
767 | <?php endif; ?> |
||
768 | </div> |
||
769 | </div> |
||
770 | |||
771 | <?php |
||
772 | } |
||
773 | |||
774 | /** |
||
775 | * Callback to sort hooks by name. |
||
776 | * |
||
777 | * @see https://www.php.net/strnatcasecmp strnatcasecmp() |
||
778 | * |
||
779 | * @since 1.0.0 |
||
780 | * |
||
781 | * @param WordPoints_Points_Hook $a The first hook object. |
||
782 | * @param WordPoints_Points_Hook $b The second hook object. |
||
783 | * |
||
784 | * @return int |
||
785 | */ |
||
786 | private static function sort_name_callback( $a, $b ) { |
||
789 | } |
||
790 | |||
791 | } // class WordPoints_Points_Hooks |
||
792 | |||
793 | // EOF |
||
794 |