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 |
||
62 | function options_page() { |
||
|
|||
63 | $page = isset( $_GET['page'] ) ? strtolower( $_GET['page'] ) : false; |
||
64 | |||
65 | if ( $page !== 'wpinv-settings' ) { |
||
66 | return; |
||
67 | } |
||
68 | |||
69 | $settings_tabs = wpinv_get_settings_tabs(); |
||
70 | $settings_tabs = empty($settings_tabs) ? array() : $settings_tabs; |
||
71 | $active_tab = isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $settings_tabs ) ? sanitize_text_field( $_GET['tab'] ) : 'general'; |
||
72 | $sections = wpinv_get_settings_tab_sections( $active_tab ); |
||
73 | $key = 'main'; |
||
74 | |||
75 | if ( is_array( $sections ) ) { |
||
76 | $key = key( $sections ); |
||
77 | } |
||
78 | |||
79 | $registered_sections = wpinv_get_settings_tab_sections( $active_tab ); |
||
80 | $section = isset( $_GET['section'] ) && ! empty( $registered_sections ) && array_key_exists( $_GET['section'], $registered_sections ) ? $_GET['section'] : $key; |
||
81 | ob_start(); |
||
82 | ?> |
||
83 | <div class="wrap"> |
||
84 | <h1 class="nav-tab-wrapper"> |
||
85 | <?php |
||
86 | foreach( wpinv_get_settings_tabs() as $tab_id => $tab_name ) { |
||
87 | $tab_url = add_query_arg( array( |
||
88 | 'settings-updated' => false, |
||
89 | 'tab' => $tab_id, |
||
90 | ) ); |
||
91 | |||
92 | // Remove the section from the tabs so we always end up at the main section |
||
93 | $tab_url = remove_query_arg( 'section', $tab_url ); |
||
94 | $tab_url = remove_query_arg( 'wpi_sub', $tab_url ); |
||
95 | |||
96 | $active = $active_tab == $tab_id ? ' nav-tab-active' : ''; |
||
97 | |||
98 | echo '<a href="' . esc_url( $tab_url ) . '" title="' . esc_attr( $tab_name ) . '" class="nav-tab' . $active . '">'; |
||
99 | echo esc_html( $tab_name ); |
||
100 | echo '</a>'; |
||
101 | } |
||
102 | ?> |
||
103 | </h1> |
||
104 | <?php |
||
105 | $number_of_sections = count( $sections ); |
||
106 | $number = 0; |
||
107 | if ( $number_of_sections > 1 ) { |
||
108 | echo '<div><ul class="subsubsub">'; |
||
109 | foreach( $sections as $section_id => $section_name ) { |
||
110 | echo '<li>'; |
||
111 | $number++; |
||
112 | $tab_url = add_query_arg( array( |
||
113 | 'settings-updated' => false, |
||
114 | 'tab' => $active_tab, |
||
115 | 'section' => $section_id |
||
116 | ) ); |
||
117 | $tab_url = remove_query_arg( 'wpi_sub', $tab_url ); |
||
118 | $class = ''; |
||
119 | if ( $section == $section_id ) { |
||
120 | $class = 'current'; |
||
121 | } |
||
122 | echo '<a class="' . $class . '" href="' . esc_url( $tab_url ) . '">' . $section_name . '</a>'; |
||
123 | |||
124 | if ( $number != $number_of_sections ) { |
||
125 | echo ' | '; |
||
126 | } |
||
127 | echo '</li>'; |
||
128 | } |
||
129 | echo '</ul></div>'; |
||
130 | } |
||
131 | ?> |
||
132 | <div id="tab_container"> |
||
133 | <form method="post" action="options.php"> |
||
134 | <table class="form-table"> |
||
135 | <?php |
||
136 | settings_fields( 'wpinv_settings' ); |
||
137 | |||
138 | if ( 'main' === $section ) { |
||
139 | do_action( 'wpinv_settings_tab_top', $active_tab ); |
||
140 | } |
||
141 | |||
142 | do_action( 'wpinv_settings_tab_top_' . $active_tab . '_' . $section, $active_tab, $section ); |
||
143 | do_settings_sections( 'wpinv_settings_' . $active_tab . '_' . $section, $active_tab, $section ); |
||
144 | do_action( 'wpinv_settings_tab_bottom_' . $active_tab . '_' . $section, $active_tab, $section ); |
||
145 | |||
146 | // For backwards compatibility |
||
147 | if ( 'main' === $section ) { |
||
148 | do_action( 'wpinv_settings_tab_bottom', $active_tab ); |
||
149 | } |
||
150 | ?> |
||
151 | </table> |
||
152 | <?php submit_button(); ?> |
||
153 | </form> |
||
154 | </div><!-- #tab_container--> |
||
155 | </div><!-- .wrap --> |
||
156 | <?php |
||
157 | $content = ob_get_clean(); |
||
158 | echo $content; |
||
159 | } |
||
160 | |||
311 | 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.