Conditions | 14 |
Paths | 25 |
Total Lines | 108 |
Code Lines | 40 |
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 |
||
54 | public function do_sync() { |
||
55 | // don't sync if importing |
||
56 | if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
||
57 | return false; |
||
58 | } |
||
59 | |||
60 | // don't sync if we are throttled |
||
61 | if ( $this->next_sync_time() > microtime( true ) ) { |
||
62 | return false; |
||
63 | } |
||
64 | |||
65 | $this->set_last_sync_time(); |
||
66 | |||
67 | do_action( 'jetpack_sync_before_send' ); |
||
68 | |||
69 | if ( $this->sync_queue->size() === 0 ) { |
||
70 | return false; |
||
71 | } |
||
72 | |||
73 | // now that we're sure we are about to sync, try to |
||
74 | // ignore user abort so we can avoid getting into a |
||
75 | // bad state |
||
76 | if ( function_exists( 'ignore_user_abort' ) ) { |
||
77 | ignore_user_abort( true ); |
||
78 | } |
||
79 | |||
80 | $buffer = $this->sync_queue->checkout_with_memory_limit( $this->dequeue_max_bytes, $this->upload_max_rows ); |
||
81 | |||
82 | if ( ! $buffer ) { |
||
83 | // buffer has no items |
||
84 | return false; |
||
85 | } |
||
86 | |||
87 | if ( is_wp_error( $buffer ) ) { |
||
88 | // another buffer is currently sending |
||
89 | return false; |
||
90 | } |
||
91 | |||
92 | $upload_size = 0; |
||
93 | $items_to_send = array(); |
||
94 | $items = $buffer->get_items(); |
||
95 | |||
96 | // set up current screen to avoid errors rendering content |
||
97 | require_once(ABSPATH . 'wp-admin/includes/class-wp-screen.php'); |
||
98 | require_once(ABSPATH . 'wp-admin/includes/screen.php'); |
||
99 | set_current_screen( 'sync' ); |
||
100 | |||
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 ( $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 | } |
||
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 | $processed_item_ids = apply_filters( 'jetpack_sync_send_data', $items_to_send, $this->codec->name(), microtime( true ) ); |
||
136 | |||
137 | if ( ! $processed_item_ids || is_wp_error( $processed_item_ids ) ) { |
||
138 | $processed_item_ids = $this->sync_queue->checkin( $buffer ); |
||
139 | |||
140 | if ( is_wp_error( $processed_item_ids ) ) { |
||
141 | error_log( 'Error checking in buffer: ' . $processed_item_ids->get_error_message() ); |
||
142 | $this->sync_queue->force_checkin(); |
||
143 | } |
||
144 | } else { |
||
145 | $processed_items = array_intersect_key( $items, array_flip( $processed_item_ids ) ); |
||
146 | |||
147 | /** |
||
148 | * Allows us to keep track of all the actions that have been sent. |
||
149 | * Allows us to calculate the progress of specific actions. |
||
150 | * |
||
151 | * @since 4.2.0 |
||
152 | * |
||
153 | * @param array $processed_actions The actions that we send successfully. |
||
154 | */ |
||
155 | do_action( 'jetpack_sync_processed_actions', $processed_items ); |
||
156 | |||
157 | $this->sync_queue->close( $buffer, $processed_item_ids ); |
||
158 | } |
||
159 | |||
160 | return true; |
||
161 | } |
||
162 | |||
252 |