| Conditions | 24 |
| Paths | 10768 |
| Total Lines | 117 |
| Code Lines | 69 |
| 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 |
||
| 134 | function ppp_v22_postmeta_upgrade() { |
||
| 135 | |||
| 136 | if( ! current_user_can( 'manage_options' ) ) { |
||
| 137 | wp_die( __( 'You do not have permission to do upgrades', 'ppp-txt' ), __( 'Error', 'ppp-txt' ), array( 'response' => 403 ) ); |
||
| 138 | } |
||
| 139 | |||
| 140 | ignore_user_abort( true ); |
||
| 141 | |||
| 142 | if ( ! ini_get( 'safe_mode' ) ) { |
||
| 143 | @set_time_limit(0); |
||
|
|
|||
| 144 | } |
||
| 145 | |||
| 146 | global $wpdb; |
||
| 147 | |||
| 148 | |||
| 149 | $step = isset( $_GET['step'] ) ? absint( $_GET['step'] ) : 1; |
||
| 150 | $number = 25; |
||
| 151 | $offset = $step == 1 ? 0 : ( $step - 1 ) * $number; |
||
| 152 | |||
| 153 | if ( $step < 2 ) { |
||
| 154 | // Check if we have any payments before moving on |
||
| 155 | $sql = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_ppp_post_override_data' LIMIT 1"; |
||
| 156 | $has_overrides = $wpdb->get_col( $sql ); |
||
| 157 | |||
| 158 | if( empty( $has_overrides ) ) { |
||
| 159 | // We had no payments, just complete |
||
| 160 | update_option( 'ppp_version', preg_replace( '/[^0-9.].*/', '', PPP_VERSION ) ); |
||
| 161 | ppp_set_upgrade_complete( 'upgrade_post_meta' ); |
||
| 162 | delete_option( 'ppp_doing_upgrade' ); |
||
| 163 | wp_redirect( admin_url() ); exit; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | $total = isset( $_GET['total'] ) ? absint( $_GET['total'] ) : false; |
||
| 168 | |||
| 169 | if ( empty( $total ) || $total <= 1 ) { |
||
| 170 | $total_sql = "SELECT COUNT(post_id) as total FROM $wpdb->postmeta WHERE meta_key = '_ppp_post_override_data'"; |
||
| 171 | $results = $wpdb->get_row( $total_sql, 0 ); |
||
| 172 | |||
| 173 | $total = $results->total; |
||
| 174 | } |
||
| 175 | |||
| 176 | $results = $wpdb->get_results( $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_ppp_post_override_data' ORDER BY meta_id DESC LIMIT %d,%d;", $offset, $number ) ); |
||
| 177 | $new_post_meta = array(); |
||
| 178 | |||
| 179 | if ( $results ) { |
||
| 180 | foreach ( $results as $result ) { |
||
| 181 | |||
| 182 | $share_key = 1; |
||
| 183 | |||
| 184 | $override_data = unserialize( $result->meta_value ); |
||
| 185 | |||
| 186 | foreach ( $override_data as $day => $values ) { |
||
| 187 | |||
| 188 | if ( ! isset( $values['enabled'] ) ) { |
||
| 189 | continue; |
||
| 190 | } |
||
| 191 | |||
| 192 | $text = ! empty( $values['text'] ) ? $values['text'] : ''; |
||
| 193 | $time = ! empty( $values['time'] ) ? $values['time'] : '8:00am'; |
||
| 194 | |||
| 195 | $post = get_post( $result->post_id ); |
||
| 196 | $days_ahead = substr( $day, -1 ); |
||
| 197 | $date = date( 'm\/d\/Y', strtotime( $post->post_date . '+' . $days_ahead . ' days' ) ); |
||
| 198 | $image = ''; |
||
| 199 | $attachment_id = ''; |
||
| 200 | |||
| 201 | if ( ! empty( $values['use_image'] ) ) { |
||
| 202 | $thumb_id = get_post_thumbnail_id( $result->post_id ); |
||
| 203 | $thumb_url = wp_get_attachment_image_src( $thumb_id, 'ppp-tw-share-image', true ); |
||
| 204 | |||
| 205 | if ( isset( $thumb_url[0] ) && ! empty( $thumb_url[0] ) && !strpos( $thumb_url[0], 'wp-includes/images/media/default.png' ) ) { |
||
| 206 | $thumb_url = $thumb_url[0]; |
||
| 207 | } |
||
| 208 | |||
| 209 | if ( ! empty( $thumb_id ) && ! empty( $thumb_url ) ) { |
||
| 210 | $attachment_id = $thumb_id; |
||
| 211 | $image = $thumb_url; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | $new_post_meta[$share_key] = array ( |
||
| 216 | 'date' => $date, |
||
| 217 | 'time' => $time, |
||
| 218 | 'text' => $text, |
||
| 219 | 'image' => ! empty( $image ) ? $image : '', |
||
| 220 | 'attachment_id' => ! empty( $attachment_id ) ? $attachment_id : '' |
||
| 221 | ); |
||
| 222 | |||
| 223 | $share_key++; |
||
| 224 | |||
| 225 | } |
||
| 226 | |||
| 227 | update_post_meta( $result->post_id, '_ppp_tweets', $new_post_meta ); |
||
| 228 | } |
||
| 229 | |||
| 230 | // Postmeta found so upgrade them |
||
| 231 | $step++; |
||
| 232 | $redirect = add_query_arg( array( |
||
| 233 | 'page' => 'ppp-upgrades', |
||
| 234 | 'ppp-upgrade' => 'upgrade_post_meta', |
||
| 235 | 'step' => $step, |
||
| 236 | 'number' => $number, |
||
| 237 | 'total' => $total |
||
| 238 | ), admin_url( 'index.php' ) ); |
||
| 239 | wp_redirect( $redirect ); exit; |
||
| 240 | |||
| 241 | } else { |
||
| 242 | |||
| 243 | // No more postmeta found, finish up |
||
| 244 | update_option( 'ppp_version', preg_replace( '/[^0-9.].*/', '', PPP_VERSION ) ); |
||
| 245 | ppp_set_upgrade_complete( 'upgrade_post_meta' ); |
||
| 246 | delete_option( 'ppp_doing_upgrade' ); |
||
| 247 | wp_redirect( admin_url() ); exit; |
||
| 248 | |||
| 249 | } |
||
| 250 | } |
||
| 251 | add_action( 'ppp_upgrade_post_meta', 'ppp_v22_postmeta_upgrade' ); |
||
| 252 |
If you suppress an error, we recommend checking for the error condition explicitly: