| Conditions | 10 |
| Paths | 16 |
| Total Lines | 56 |
| 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 |
||
| 80 | function jetpack_create_autosave_revision( $post_ID, $post_after, $post_before ) { |
||
| 81 | // we are only interested in post changes done during autosave |
||
| 82 | if ( ! defined( 'DOING_AUTOSAVE' ) || ! DOING_AUTOSAVE ) { |
||
| 83 | return; |
||
| 84 | } |
||
| 85 | |||
| 86 | // get the actual post whose revision is to be saved: we might need to reach out for the parent |
||
| 87 | // post if the update is for autosave revision. It's simply the $post_before for draft autosave. |
||
| 88 | $revision_post = $post_before; |
||
| 89 | |||
| 90 | if ( $post_before->post_type === 'revision' ) { |
||
| 91 | if ( strpos( $post_before->post_name, "{$post_before->post_parent}-autosave" ) === false ) { |
||
| 92 | // update of a non-autosave revision: we're not interested in this kind of update |
||
| 93 | return; |
||
| 94 | } |
||
| 95 | |||
| 96 | // it's an update of autosave revision, retrieve the parent post |
||
| 97 | $revision_post = get_post( $post_before->post_parent ); |
||
| 98 | } |
||
| 99 | |||
| 100 | // bail out if the post type doesn't support revisions |
||
| 101 | if ( ! wp_revisions_enabled( $revision_post ) ) { |
||
| 102 | return; |
||
| 103 | } |
||
| 104 | |||
| 105 | // the autosave revision can either have fresh content, if it's newer than the saved post, or |
||
| 106 | // be stale: we don't delete the autosave revision when saving the post. We'll reuse it later |
||
| 107 | // on the next autosave instead of creating a new one. |
||
| 108 | // If the autosave is indeed fresh, update the parent post with its content and timestamp before |
||
| 109 | // saving it as revision. |
||
| 110 | if ( $post_before->post_modified > $revision_post->post_modified) { |
||
| 111 | foreach ( array_keys( _wp_post_revision_fields( $revision_post ) ) as $field ) { |
||
| 112 | $revision_post->$field = $post_before->$field; |
||
| 113 | } |
||
| 114 | $revision_post->post_modified = $post_before->post_modified; |
||
| 115 | $revision_post->post_modified_gmt = $post_before->post_modified_gmt; |
||
| 116 | } |
||
| 117 | |||
| 118 | // don't save a revision if it would be identical to the last saved revision |
||
| 119 | if ( ! jetpack_post_has_changed_since_last_revision( $revision_post->ID, $revision_post ) ) { |
||
| 120 | return; |
||
| 121 | } |
||
| 122 | |||
| 123 | // we'll save a post revision only if the difference between the old and new autosave is big. |
||
| 124 | // then the old autosave is worth preserving: it would be overwritten and lost otherwise. |
||
| 125 | if ( ! jetpack_is_big_edit( $revision_post, $post_after ) ) { |
||
| 126 | return; |
||
| 127 | } |
||
| 128 | |||
| 129 | _wp_put_post_revision( $revision_post ); |
||
| 130 | |||
| 131 | // record stats about count and size of created autosave revisions |
||
| 132 | $revision_content_length = strlen( $revision_post->post_content ); |
||
| 133 | do_action( 'jetpack_stats_statsd', 'autosave_revision', "1|c" ); |
||
| 134 | do_action( 'jetpack_stats_statsd', 'autosave_revision', "{$revision_content_length}|g" ); |
||
| 135 | } |
||
| 136 | |||
| 138 |