| Conditions | 7 |
| Paths | 13 |
| Total Lines | 131 |
| Code Lines | 72 |
| 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 |
||
| 263 | public function form( $instance ) { |
||
| 264 | |||
| 265 | // Set up some default widget settings. |
||
| 266 | $defaults = array( |
||
| 267 | 'title' => __('Recent Entries', 'gravityview'), |
||
| 268 | 'view_id' => NULL, |
||
| 269 | 'post_id' => NULL, |
||
| 270 | 'limit' => 10, |
||
| 271 | 'link_format' => __('Entry #{entry_id}', 'gravityview'), |
||
| 272 | 'after_link' => '' |
||
| 273 | ); |
||
| 274 | |||
| 275 | $instance = wp_parse_args( (array) $instance, $defaults ); |
||
| 276 | |||
| 277 | ?> |
||
| 278 | |||
| 279 | <!-- Title --> |
||
| 280 | <p> |
||
| 281 | <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'gravityview' ) ?></label> |
||
| 282 | <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> |
||
| 283 | </p> |
||
| 284 | |||
| 285 | <!-- Download --> |
||
| 286 | <?php |
||
| 287 | $args = array( |
||
| 288 | 'post_type' => 'gravityview', |
||
| 289 | 'posts_per_page' => -1, |
||
| 290 | 'post_status' => 'publish', |
||
| 291 | ); |
||
| 292 | $views = get_posts( $args ); |
||
| 293 | |||
| 294 | // If there are no views set up yet, we get outta here. |
||
| 295 | if( empty( $views ) ) { |
||
| 296 | echo '<div id="select_gravityview_view"><div class="wrap">' . GravityView_Admin::no_views_text() . '</div></div>'; |
||
| 297 | return; |
||
| 298 | } |
||
| 299 | |||
| 300 | ?> |
||
| 301 | |||
| 302 | <?php |
||
| 303 | /** |
||
| 304 | * Display errors generated for invalid embed IDs |
||
| 305 | * @see GravityView_View_Data::is_valid_embed_id |
||
| 306 | */ |
||
| 307 | if( isset( $instance['updated'] ) && empty( $instance['view_id'] ) ) { |
||
| 308 | ?> |
||
| 309 | <div class="error inline hide-on-view-change"> |
||
| 310 | <p><?php esc_html_e('Please select a View to search.', 'gravityview'); ?></p> |
||
| 311 | </div> |
||
| 312 | <?php |
||
| 313 | unset ( $error ); |
||
| 314 | } |
||
| 315 | ?> |
||
| 316 | |||
| 317 | <p> |
||
| 318 | <label for="<?php echo esc_attr( $this->get_field_id( 'view_id' ) ); ?>"><?php esc_html_e('Select a View', 'gravityview'); ?></label> |
||
| 319 | <select class="widefat gv-recent-entries-select-view" name="<?php echo esc_attr( $this->get_field_name( 'view_id' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'view_id' ) ); ?>"> |
||
| 320 | <option value=""><?php esc_html_e( '— Select a View as Entries Source —', 'gravityview' ); ?></option> |
||
| 321 | <?php |
||
| 322 | |||
| 323 | foreach( $views as $view ) { |
||
| 324 | $title = empty( $view->post_title ) ? __('(no title)', 'gravityview') : $view->post_title; |
||
| 325 | echo '<option value="'. $view->ID .'"'.selected( absint( $instance['view_id'] ), $view->ID ).'>'. esc_html( sprintf('%s #%d', $title, $view->ID ) ) .'</option>'; |
||
| 326 | } |
||
| 327 | |||
| 328 | ?> |
||
| 329 | </select> |
||
| 330 | </p> |
||
| 331 | |||
| 332 | <?php |
||
| 333 | /** |
||
| 334 | * Display errors generated for invalid embed IDs |
||
| 335 | * @see GravityView_View_Data::is_valid_embed_id |
||
| 336 | */ |
||
| 337 | if( !empty( $instance['error_post_id'] ) ) { |
||
| 338 | ?> |
||
| 339 | <div class="error inline"> |
||
| 340 | <p><?php echo $instance['error_post_id']; ?></p> |
||
| 341 | </div> |
||
| 342 | <?php |
||
| 343 | unset ( $error ); |
||
| 344 | } |
||
| 345 | ?> |
||
| 346 | |||
| 347 | <p> |
||
| 348 | <label for="<?php echo $this->get_field_id('post_id'); ?>"><?php esc_html_e( 'If Embedded, Page ID:', 'gravityview' ); ?></label> |
||
| 349 | <input class="code" size="3" id="<?php echo $this->get_field_id('post_id'); ?>" name="<?php echo $this->get_field_name('post_id'); ?>" type="text" value="<?php echo esc_attr( $instance['post_id'] ); ?>" /> |
||
| 350 | <span class="howto"><?php |
||
| 351 | esc_html_e('To have a search performed on an embedded View, enter the ID of the post or page where the View is embedded.', 'gravityview' ); |
||
| 352 | echo ' '.gravityview_get_link('http://docs.gravityview.co/article/222-the-search-widget', __('Learn more…', 'gravityview' ), 'target=_blank' ); |
||
| 353 | ?></span> |
||
| 354 | </p> |
||
| 355 | |||
| 356 | <p> |
||
| 357 | <label for="<?php echo $this->get_field_id( 'limit' ); ?>"> |
||
| 358 | <span><?php _e( 'Number of entries to show:', 'gravityview' ); ?></span> |
||
| 359 | </label> |
||
| 360 | <input class="code" id="<?php echo $this->get_field_id( 'limit' ); ?>" name="<?php echo $this->get_field_name( 'limit' ); ?>" type="number" value="<?php echo intval( $instance['limit'] ); ?>" size="3" /> |
||
| 361 | </p> |
||
| 362 | |||
| 363 | <p> |
||
| 364 | <label for="<?php echo $this->get_field_id( 'link_format' ); ?>"> |
||
| 365 | <span><?php _e( 'Entry link text (required)', 'gravityview' ); ?></span> |
||
| 366 | </label> |
||
| 367 | <input id="<?php echo $this->get_field_id( 'link_format' ); ?>" name="<?php echo $this->get_field_name( 'link_format' ); ?>" type="text" value="<?php echo esc_attr( $instance['link_format'] ); ?>" class="widefat merge-tag-support mt-position-right mt-hide_all_fields" /> |
||
| 368 | </p> |
||
| 369 | |||
| 370 | <p> |
||
| 371 | <label for="<?php echo $this->get_field_id( 'after_link' ); ?>"> |
||
| 372 | <span><?php _e( 'Text or HTML to display after the link (optional)', 'gravityview' ); ?></span> |
||
| 373 | </label> |
||
| 374 | <textarea id="<?php echo $this->get_field_id( 'after_link' ); ?>" name="<?php echo $this->get_field_name( 'after_link' ); ?>" rows="5" class="widefat code merge-tag-support mt-position-right mt-hide_all_fields"><?php echo esc_textarea( $instance['after_link'] ); ?></textarea> |
||
| 375 | </p> |
||
| 376 | |||
| 377 | <?php |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @action `gravityview_recent_entries_widget_form` Displayed at the bottom of the Recent Entries widget admin form |
||
| 381 | * @param GravityView_Recent_Entries_Widget $this WP_Widget object |
||
| 382 | * @param array $instance Current widget instance |
||
| 383 | */ |
||
| 384 | do_action( 'gravityview_recent_entries_widget_form' , $this, $instance ); |
||
| 385 | |||
| 386 | ?> |
||
| 387 | |||
| 388 | <script> |
||
| 389 | // When the widget is saved or added, refresh the Merge Tags (here for backward compatibility) |
||
| 390 | // WordPress 3.9 added widget-added and widget-updated actions |
||
| 391 | jQuery('#<?php echo $this->get_field_id( 'view_id' ); ?>').trigger( 'change' ); |
||
| 392 | </script> |
||
| 393 | <?php } |
||
| 394 | |||
| 395 | } |
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.