Conditions | 11 |
Paths | 2 |
Total Lines | 29 |
Code Lines | 16 |
Lines | 0 |
Ratio | 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 |
||
58 | static function maybe_set_preview( $posts ) { |
||
|
|||
59 | if ( is_array( $posts ) && isset( $_GET['preview'] ) && $_GET['preview'] |
||
60 | && isset( $_GET['preview_id'] ) && $_GET['preview_id'] |
||
61 | && current_user_can( 'edit_post', $_GET['preview_id'] ) ) { |
||
62 | // No need to check the nonce, that already happened in _show_post_preview on init |
||
63 | |||
64 | $preview_id = $_GET['preview_id']; |
||
65 | foreach( $posts as &$post ) { |
||
66 | if ( is_object( $post ) && $post->ID == $preview_id ) { |
||
67 | // Based on _set_preview( $post ), but adds import_custom |
||
68 | $preview = wp_get_post_autosave( $preview_id ); |
||
69 | if ( is_object($preview) ) { |
||
70 | |||
71 | $preview = sanitize_post($preview); |
||
72 | |||
73 | $post->post_content = $preview->post_content; |
||
74 | $post->post_title = $preview->post_title; |
||
75 | $post->post_excerpt = $preview->post_excerpt; |
||
76 | $post->import_custom( $preview_id ); |
||
77 | |||
78 | add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 ); |
||
79 | } |
||
80 | } |
||
81 | } |
||
82 | |||
83 | } |
||
84 | |||
85 | return $posts; |
||
86 | } |
||
87 | |||
98 |
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.