| Conditions | 17 |
| Paths | 31 |
| Total Lines | 113 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 |
||
| 53 | public function do_sync() { |
||
| 54 | // don't sync if importing |
||
| 55 | if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
||
| 56 | $this->schedule_sync( '+1 minute' ); |
||
| 57 | |||
| 58 | return false; |
||
| 59 | } |
||
| 60 | |||
| 61 | // don't sync if we are throttled |
||
| 62 | $sync_wait = $this->get_sync_wait_time(); |
||
| 63 | $last_sync = $this->get_last_sync_time(); |
||
| 64 | |||
| 65 | if ( $last_sync && $sync_wait && $last_sync + $sync_wait > microtime( true ) ) { |
||
| 66 | return false; |
||
| 67 | } |
||
| 68 | |||
| 69 | $this->set_last_sync_time(); |
||
| 70 | |||
| 71 | do_action( 'jetpack_sync_before_send' ); |
||
| 72 | |||
| 73 | if ( $this->sync_queue->size() === 0 ) { |
||
| 74 | return false; |
||
| 75 | } |
||
| 76 | |||
| 77 | // now that we're sure we are about to sync, try to |
||
| 78 | // ignore user abort so we can avoid getting into a |
||
| 79 | // bad state |
||
| 80 | if ( function_exists( 'ignore_user_abort' ) ) { |
||
| 81 | ignore_user_abort( true ); |
||
| 82 | } |
||
| 83 | |||
| 84 | $buffer = $this->sync_queue->checkout_with_memory_limit( $this->dequeue_max_bytes, $this->upload_max_rows ); |
||
| 85 | |||
| 86 | if ( ! $buffer ) { |
||
| 87 | // buffer has no items |
||
| 88 | return false; |
||
| 89 | } |
||
| 90 | |||
| 91 | if ( is_wp_error( $buffer ) ) { |
||
| 92 | // another buffer is currently sending |
||
| 93 | return false; |
||
| 94 | } |
||
| 95 | |||
| 96 | $upload_size = 0; |
||
| 97 | $items_to_send = array(); |
||
| 98 | $items = $buffer->get_items(); |
||
| 99 | |||
| 100 | // we estimate the total encoded size as we go by encoding each item individually |
||
| 101 | // this is expensive, but the only way to really know :/ |
||
| 102 | foreach ( $items as $key => $item ) { |
||
| 103 | /** |
||
| 104 | * Modify the data within an action before it is serialized and sent to the server |
||
| 105 | * For example, during full sync this expands Post ID's into full Post objects, |
||
| 106 | * so that we don't have to serialize the whole object into the queue. |
||
| 107 | * |
||
| 108 | * @since 4.2.0 |
||
| 109 | * |
||
| 110 | * @param array The action parameters |
||
| 111 | */ |
||
| 112 | $item[1] = apply_filters( 'jetpack_sync_before_send_' . $item[0], $item[1], $item[2] ); |
||
| 113 | |||
| 114 | $encoded_item = $this->codec->encode( $item ); |
||
| 115 | |||
| 116 | $upload_size += strlen( $encoded_item ); |
||
| 117 | |||
| 118 | if ( $upload_size > $this->upload_max_bytes && count( $items_to_send ) > 0 ) { |
||
| 119 | break; |
||
| 120 | } |
||
| 121 | |||
| 122 | $items_to_send[ $key ] = $encoded_item; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Fires when data is ready to send to the server. |
||
| 127 | * Return false or WP_Error to abort the sync (e.g. if there's an error) |
||
| 128 | * The items will be automatically re-sent later |
||
| 129 | * |
||
| 130 | * @since 4.2.0 |
||
| 131 | * |
||
| 132 | * @param array $data The action buffer |
||
| 133 | */ |
||
| 134 | $processed_item_ids = apply_filters( 'jetpack_sync_send_data', $items_to_send, $this->codec->name(), microtime( true ) ); |
||
| 135 | |||
| 136 | if ( ! $processed_item_ids || is_wp_error( $processed_item_ids ) ) { |
||
| 137 | $processed_item_ids = $this->sync_queue->checkin( $buffer ); |
||
| 138 | |||
| 139 | if ( is_wp_error( $processed_item_ids ) ) { |
||
| 140 | error_log( 'Error checking in buffer: ' . $processed_item_ids->get_error_message() ); |
||
| 141 | $this->sync_queue->force_checkin(); |
||
| 142 | } |
||
| 143 | // try again in 1 minute |
||
| 144 | $this->schedule_sync( '+1 minute' ); |
||
| 145 | } else { |
||
| 146 | $processed_items = array_intersect_key( $items, array_flip( $processed_item_ids ) ); |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Allows us to keep track of all the actions that have been sent. |
||
| 150 | * Allows us to calculate the progress of specific actions. |
||
| 151 | * |
||
| 152 | * @since 4.2.0 |
||
| 153 | * |
||
| 154 | * @param array $processed_actions The actions that we send successfully. |
||
| 155 | */ |
||
| 156 | do_action( 'jetpack_sync_processed_actions', $processed_items ); |
||
| 157 | |||
| 158 | $this->sync_queue->close( $buffer, $processed_item_ids ); |
||
| 159 | // check if there are any more events in the buffer |
||
| 160 | // if so, schedule a cron job to happen soon |
||
| 161 | if ( $this->sync_queue->has_any_items() ) { |
||
| 162 | $this->schedule_sync( '+1 minute' ); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 260 |