| Conditions | 6 |
| Paths | 16 |
| Total Lines | 56 |
| Code Lines | 30 |
| 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 |
||
| 45 | public function rebuild() { |
||
| 46 | set_time_limit( 21600 ); // 6 hours |
||
| 47 | |||
| 48 | // Send textual output. |
||
| 49 | header( 'Content-type: text/plain; charset=utf-8' ); |
||
| 50 | |||
| 51 | // We start at 0 by default and get to max. |
||
| 52 | $offset = $_GET['offset'] ?: 0; |
||
| 53 | $limit = $_GET['limit'] ?: 1; |
||
| 54 | $max = $offset + $limit; |
||
| 55 | |||
| 56 | $this->log->debug( 'Processing references...' ); |
||
| 57 | |||
| 58 | // Go through the list of published entities and posts and call the (legacy) |
||
| 59 | // `wl_linked_data_save_post` function for each one. We're using the `process` |
||
| 60 | // function which is provided by the parent `Wordlift_Listable` abstract class |
||
| 61 | // and will cycle through all the posts w/ a very small memory footprint |
||
| 62 | // in order to avoid memory errors. |
||
| 63 | |||
| 64 | $count = 0; |
||
| 65 | $log = $this->log; |
||
| 66 | $entity_service = $this->entity_service; |
||
| 67 | $linked_data_service = $this->linked_data_service; |
||
| 68 | |||
| 69 | $this->process( |
||
| 70 | function ( $post_id ) use ( &$count, $log, $entity_service, $linked_data_service ) { |
||
| 71 | $count ++; |
||
| 72 | |||
| 73 | if ( $entity_service->is_entity( $post_id ) ) { |
||
| 74 | $log->trace( "Post $post_id is an entity, skipping..." ); |
||
| 75 | return; |
||
| 76 | } |
||
| 77 | |||
| 78 | $log->trace( "Going to save post $count, ID $post_id..." ); |
||
| 79 | $linked_data_service->push( $post_id ); |
||
| 80 | }, |
||
| 81 | array(), |
||
| 82 | $offset, |
||
| 83 | $max |
||
| 84 | ); |
||
| 85 | |||
| 86 | // Redirect to the next chunk. |
||
| 87 | if ( $count == $limit ) { |
||
| 88 | $log->trace( 'Redirecting to post #' . ( $offset + 1 ) . '...' ); |
||
| 89 | $url = admin_url( 'admin-ajax.php?action=wl_rebuild_references&offset=' . ( $offset + $limit ) . '&limit=' . $limit ); |
||
| 90 | $this->redirect( $url ); |
||
| 91 | } |
||
| 92 | |||
| 93 | $this->log->info( "Rebuild complete" ); |
||
| 94 | echo( "Rebuild complete" ); |
||
| 95 | |||
| 96 | // If we're being called as AJAX, die here. |
||
| 97 | if ( DOING_AJAX ) { |
||
| 98 | wp_die(); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 121 |