Conditions | 18 |
Paths | 4610 |
Total Lines | 98 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
41 | function options_page() { |
||
|
|||
42 | $page = isset( $_GET['page'] ) ? strtolower( $_GET['page'] ) : false; |
||
43 | |||
44 | if ( $page !== 'wpinv-settings' ) { |
||
45 | return; |
||
46 | } |
||
47 | |||
48 | $settings_tabs = wpinv_get_settings_tabs(); |
||
49 | $settings_tabs = empty($settings_tabs) ? array() : $settings_tabs; |
||
50 | $active_tab = isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $settings_tabs ) ? sanitize_text_field( $_GET['tab'] ) : 'general'; |
||
51 | $sections = wpinv_get_settings_tab_sections( $active_tab ); |
||
52 | $key = 'main'; |
||
53 | |||
54 | if ( is_array( $sections ) ) { |
||
55 | $key = key( $sections ); |
||
56 | } |
||
57 | |||
58 | $registered_sections = wpinv_get_settings_tab_sections( $active_tab ); |
||
59 | $section = isset( $_GET['section'] ) && ! empty( $registered_sections ) && array_key_exists( $_GET['section'], $registered_sections ) ? $_GET['section'] : $key; |
||
60 | ob_start(); |
||
61 | ?> |
||
62 | <div class="wrap"> |
||
63 | <h1 class="nav-tab-wrapper"> |
||
64 | <?php |
||
65 | foreach( wpinv_get_settings_tabs() as $tab_id => $tab_name ) { |
||
66 | $tab_url = add_query_arg( array( |
||
67 | 'settings-updated' => false, |
||
68 | 'tab' => $tab_id, |
||
69 | ) ); |
||
70 | |||
71 | // Remove the section from the tabs so we always end up at the main section |
||
72 | $tab_url = remove_query_arg( 'section', $tab_url ); |
||
73 | $tab_url = remove_query_arg( 'wpi_sub', $tab_url ); |
||
74 | |||
75 | $active = $active_tab == $tab_id ? ' nav-tab-active' : ''; |
||
76 | |||
77 | echo '<a href="' . esc_url( $tab_url ) . '" title="' . esc_attr( $tab_name ) . '" class="nav-tab' . $active . '">'; |
||
78 | echo esc_html( $tab_name ); |
||
79 | echo '</a>'; |
||
80 | } |
||
81 | ?> |
||
82 | </h1> |
||
83 | <?php |
||
84 | $number_of_sections = count( $sections ); |
||
85 | $number = 0; |
||
86 | if ( $number_of_sections > 1 ) { |
||
87 | echo '<div><ul class="subsubsub">'; |
||
88 | foreach( $sections as $section_id => $section_name ) { |
||
89 | echo '<li>'; |
||
90 | $number++; |
||
91 | $tab_url = add_query_arg( array( |
||
92 | 'settings-updated' => false, |
||
93 | 'tab' => $active_tab, |
||
94 | 'section' => $section_id |
||
95 | ) ); |
||
96 | $tab_url = remove_query_arg( 'wpi_sub', $tab_url ); |
||
97 | $class = ''; |
||
98 | if ( $section == $section_id ) { |
||
99 | $class = 'current'; |
||
100 | } |
||
101 | echo '<a class="' . $class . '" href="' . esc_url( $tab_url ) . '">' . $section_name . '</a>'; |
||
102 | |||
103 | if ( $number != $number_of_sections ) { |
||
104 | echo ' | '; |
||
105 | } |
||
106 | echo '</li>'; |
||
107 | } |
||
108 | echo '</ul></div>'; |
||
109 | } |
||
110 | ?> |
||
111 | <div id="tab_container"> |
||
112 | <form method="post" action="options.php"> |
||
113 | <table class="form-table"> |
||
114 | <?php |
||
115 | settings_fields( 'wpinv_settings' ); |
||
116 | |||
117 | if ( 'main' === $section ) { |
||
118 | do_action( 'wpinv_settings_tab_top', $active_tab ); |
||
119 | } |
||
120 | |||
121 | do_action( 'wpinv_settings_tab_top_' . $active_tab . '_' . $section, $active_tab, $section ); |
||
122 | do_settings_sections( 'wpinv_settings_' . $active_tab . '_' . $section, $active_tab, $section ); |
||
123 | do_action( 'wpinv_settings_tab_bottom_' . $active_tab . '_' . $section, $active_tab, $section ); |
||
124 | |||
125 | // For backwards compatibility |
||
126 | if ( 'main' === $section ) { |
||
127 | do_action( 'wpinv_settings_tab_bottom', $active_tab ); |
||
128 | } |
||
129 | ?> |
||
130 | </table> |
||
131 | <?php submit_button(); ?> |
||
132 | </form> |
||
133 | </div><!-- #tab_container--> |
||
134 | </div><!-- .wrap --> |
||
135 | <?php |
||
136 | $content = ob_get_clean(); |
||
137 | echo $content; |
||
138 | } |
||
139 | |||
290 | return new WPInv_Admin_Menus(); |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.