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 |
||
54 | public function do_sync() { |
||
55 | // don't sync if importing |
||
56 | if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
||
57 | $this->schedule_sync( "+1 minute" ); |
||
58 | |||
59 | return false; |
||
60 | } |
||
61 | |||
62 | // don't sync if we are throttled |
||
63 | $sync_wait = $this->get_sync_wait_time(); |
||
64 | $last_sync = $this->get_last_sync_time(); |
||
65 | |||
66 | if ( $last_sync && $sync_wait && $last_sync + $sync_wait > microtime( true ) ) { |
||
67 | return false; |
||
68 | } |
||
69 | |||
70 | $this->set_last_sync_time(); |
||
71 | |||
72 | do_action( 'jetpack_sync_before_send' ); |
||
73 | |||
74 | if ( $this->sync_queue->size() === 0 ) { |
||
75 | return false; |
||
76 | } |
||
77 | |||
78 | // now that we're sure we are about to sync, try to |
||
79 | // ignore user abort so we can avoid getting into a |
||
80 | // bad state |
||
81 | if ( function_exists( 'ignore_user_abort' ) ) { |
||
82 | ignore_user_abort( true ); |
||
83 | } |
||
84 | |||
85 | $buffer = $this->sync_queue->checkout_with_memory_limit( $this->dequeue_max_bytes, $this->upload_max_rows ); |
||
86 | |||
87 | if ( ! $buffer ) { |
||
88 | // buffer has no items |
||
89 | return false; |
||
90 | } |
||
91 | |||
92 | if ( is_wp_error( $buffer ) ) { |
||
93 | // another buffer is currently sending |
||
94 | return false; |
||
95 | } |
||
96 | |||
97 | $upload_size = 0; |
||
98 | $items_to_send = array(); |
||
99 | $actions_to_send = array(); |
||
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 ( $buffer->get_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] ); |
||
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 | $actions_to_send[ $key ] = $item[0]; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Fires when data is ready to send to the server. |
||
128 | * Return false or WP_Error to abort the sync (e.g. if there's an error) |
||
129 | * The items will be automatically re-sent later |
||
130 | * |
||
131 | * @since 4.2.0 |
||
132 | * |
||
133 | * @param array $data The action buffer |
||
134 | */ |
||
135 | $result = apply_filters( 'jetpack_sync_send_data', $items_to_send, $this->codec->name() ); |
||
136 | |||
137 | if ( ! $result || is_wp_error( $result ) ) { |
||
138 | $result = $this->sync_queue->checkin( $buffer ); |
||
139 | |||
140 | if ( is_wp_error( $result ) ) { |
||
141 | error_log( "Error checking in buffer: " . $result->get_error_message() ); |
||
142 | $this->sync_queue->force_checkin(); |
||
143 | } |
||
144 | // try again in 1 minute |
||
145 | $this->schedule_sync( "+1 minute" ); |
||
146 | } else { |
||
147 | $processed_actions = array(); |
||
148 | foreach( $result as $result_id ) { |
||
149 | if ( isset( $actions_to_send[ $result_id ] ) ) { |
||
150 | $processed_actions[] = $actions_to_send[ $result_id ]; |
||
151 | } |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Allows us to keep track of all the actions that have been sent. |
||
156 | * Allows us to calculate the progress of specific actions. |
||
157 | * |
||
158 | * @since 4.2.0 |
||
159 | * |
||
160 | * @param array $processed_actions The actions that we send successfully. |
||
161 | */ |
||
162 | do_action( 'jetpack_sync_processed_actions', $processed_actions ); |
||
163 | |||
164 | |||
165 | $this->sync_queue->close( $buffer, $result ); |
||
166 | // check if there are any more events in the buffer |
||
167 | // if so, schedule a cron job to happen soon |
||
168 | if ( $this->sync_queue->has_any_items() ) { |
||
169 | $this->schedule_sync( "+1 minute" ); |
||
170 | } |
||
171 | } |
||
172 | } |
||
173 | |||
284 |