| Conditions | 6 |
| Paths | 12 |
| Total Lines | 51 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 151 | public function update( &$discount ) { |
||
| 152 | $discount->save_meta_data(); |
||
| 153 | $discount->set_version( WPINV_VERSION ); |
||
| 154 | |||
| 155 | if ( null === $discount->get_date_created( 'edit' ) ) { |
||
| 156 | $discount->set_date_created( current_time('mysql') ); |
||
| 157 | } |
||
| 158 | |||
| 159 | // Grab the current status so we can compare. |
||
| 160 | $previous_status = get_post_status( $discount->get_id() ); |
||
| 161 | |||
| 162 | $changes = $discount->get_changes(); |
||
| 163 | |||
| 164 | // Only update the post when the post data changes. |
||
| 165 | if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'post_excerpt' ), array_keys( $changes ) ) ) { |
||
| 166 | $post_data = array( |
||
| 167 | 'post_date' => $discount->get_date_created( 'edit' ), |
||
| 168 | 'post_status' => $discount->get_status( 'edit' ), |
||
| 169 | 'post_title' => $discount->get_name( 'edit' ), |
||
| 170 | 'post_author' => $discount->get_author( 'edit' ), |
||
| 171 | 'post_modified' => $discount->get_date_modified( 'edit' ), |
||
| 172 | 'post_excerpt' => $discount->get_description( 'edit' ), |
||
| 173 | ); |
||
| 174 | |||
| 175 | /** |
||
| 176 | * When updating this object, to prevent infinite loops, use $wpdb |
||
| 177 | * to update data, since wp_update_post spawns more calls to the |
||
| 178 | * save_post action. |
||
| 179 | * |
||
| 180 | * This ensures hooks are fired by either WP itself (admin screen save), |
||
| 181 | * or an update purely from CRUD. |
||
| 182 | */ |
||
| 183 | if ( doing_action( 'save_post' ) ) { |
||
| 184 | $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $discount->get_id() ) ); |
||
| 185 | clean_post_cache( $discount->get_id() ); |
||
| 186 | } else { |
||
| 187 | wp_update_post( array_merge( array( 'ID' => $discount->get_id() ), $post_data ) ); |
||
| 188 | } |
||
| 189 | $discount->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
||
| 190 | } |
||
| 191 | $this->update_post_meta( $discount ); |
||
| 192 | $discount->apply_changes(); |
||
| 193 | $this->clear_caches( $discount ); |
||
| 194 | |||
| 195 | // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
||
| 196 | $new_status = $discount->get_status( 'edit' ); |
||
| 197 | |||
| 198 | if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
||
| 199 | do_action( 'getpaid_new_discount', $discount->get_id(), $discount ); |
||
| 200 | } else { |
||
| 201 | do_action( 'getpaid_update_discount', $discount->get_id(), $discount ); |
||
| 202 | } |
||
| 213 |