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