MetaBoxes
2 months ago
COTRedirectionController.php
1 year ago
Edit.php
1 year ago
EditLock.php
1 year ago
ListTable.php
4 weeks ago
PageController.php
9 months ago
PostsRedirectionController.php
9 months ago
EditLock.php
214 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Internal\Admin\Orders; |
| 3 | |
| 4 | /** |
| 5 | * This class takes care of the edit lock logic when HPOS is enabled. |
| 6 | * For better interoperability with WordPress, edit locks are stored in the same format as posts. That is, as a metadata |
| 7 | * in the order object (key: '_edit_lock') in the format "timestamp:user_id". |
| 8 | * |
| 9 | * @since 7.8.0 |
| 10 | */ |
| 11 | class EditLock { |
| 12 | |
| 13 | const META_KEY_NAME = '_edit_lock'; |
| 14 | |
| 15 | /** |
| 16 | * Obtains lock information for a given order. If the lock has expired or it's assigned to an invalid user, |
| 17 | * the order is no longer considered locked. |
| 18 | * |
| 19 | * @param \WC_Order $order Order to check. |
| 20 | * @return bool|array |
| 21 | */ |
| 22 | public function get_lock( \WC_Order $order ) { |
| 23 | $lock = $order->get_meta( self::META_KEY_NAME, true, 'edit' ); |
| 24 | if ( ! $lock ) { |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | $lock = explode( ':', $lock ); |
| 29 | if ( 2 !== count( $lock ) ) { |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | $time = absint( $lock[0] ); |
| 34 | $user_id = isset( $lock[1] ) ? absint( $lock[1] ) : 0; |
| 35 | |
| 36 | if ( ! $time || ! get_user_by( 'id', $user_id ) ) { |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | /** This filter is documented in WP's wp-admin/includes/ajax-actions.php */ |
| 41 | $time_window = apply_filters( 'wp_check_post_lock_window', 150 ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingSinceComment |
| 42 | if ( time() >= ( $time + $time_window ) ) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | return compact( 'time', 'user_id' ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Checks whether the order is being edited (i.e. locked) by another user. |
| 51 | * |
| 52 | * @param \WC_Order $order Order to check. |
| 53 | * @return bool TRUE if order is locked and currently being edited by another user. FALSE otherwise. |
| 54 | */ |
| 55 | public function is_locked_by_another_user( \WC_Order $order ) : bool { |
| 56 | $lock = $this->get_lock( $order ); |
| 57 | return $lock && ( get_current_user_id() !== $lock['user_id'] ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Checks whether the order is being edited by any user. |
| 62 | * |
| 63 | * @param \WC_Order $order Order to check. |
| 64 | * @return boolean TRUE if order is locked and currently being edited by a user. FALSE otherwise. |
| 65 | */ |
| 66 | public function is_locked( \WC_Order $order ) : bool { |
| 67 | return (bool) $this->get_lock( $order ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Assigns an order's edit lock to the current user. |
| 72 | * |
| 73 | * @param \WC_Order $order The order to apply the lock to. |
| 74 | * @return array|bool FALSE if no user is logged-in, an array in the same format as {@see get_lock()} otherwise. |
| 75 | */ |
| 76 | public function lock( \WC_Order $order ) { |
| 77 | $user_id = get_current_user_id(); |
| 78 | |
| 79 | if ( ! $user_id ) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | $order->update_meta_data( self::META_KEY_NAME, time() . ':' . $user_id ); |
| 84 | $order->save_meta_data(); |
| 85 | |
| 86 | return $order->get_meta( self::META_KEY_NAME, true, 'edit' ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Hooked to 'heartbeat_received' on the edit order page to refresh the lock on an order being edited by the current user. |
| 91 | * |
| 92 | * @param array $response The heartbeat response to be sent. |
| 93 | * @param array $data Data sent through the heartbeat. |
| 94 | * @return array Response to be sent. |
| 95 | */ |
| 96 | public function refresh_lock_ajax( $response, $data ) { |
| 97 | $order_id = absint( $data['wc-refresh-order-lock'] ?? 0 ); |
| 98 | if ( ! $order_id ) { |
| 99 | return $response; |
| 100 | } |
| 101 | |
| 102 | unset( $response['wp-refresh-post-lock'] ); |
| 103 | |
| 104 | $order = wc_get_order( $order_id ); |
| 105 | if ( ! $order || ! is_a( $order, \WC_Order::class ) || ( ! current_user_can( get_post_type_object( $order->get_type() )->cap->edit_post, $order->get_id() ) && ! current_user_can( 'manage_woocommerce' ) ) ) { |
| 106 | return $response; |
| 107 | } |
| 108 | |
| 109 | $response['wc-refresh-order-lock'] = array(); |
| 110 | |
| 111 | if ( ! $this->is_locked_by_another_user( $order ) ) { |
| 112 | $response['wc-refresh-order-lock']['lock'] = $this->lock( $order ); |
| 113 | } else { |
| 114 | $current_lock = $this->get_lock( $order ); |
| 115 | $user = get_user_by( 'id', $current_lock['user_id'] ); |
| 116 | |
| 117 | $response['wc-refresh-order-lock']['error'] = array( |
| 118 | // translators: %s is a user's name. |
| 119 | 'message' => sprintf( __( '%s has taken over and is currently editing.', 'woocommerce' ), $user->display_name ), |
| 120 | 'user_name' => $user->display_name, |
| 121 | 'user_avatar_src' => get_option( 'show_avatars' ) ? get_avatar_url( $user->ID, array( 'size' => 64 ) ) : '', |
| 122 | 'user_avatar_src_2x' => get_option( 'show_avatars' ) ? get_avatar_url( $user->ID, array( 'size' => 128 ) ) : '', |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | return $response; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Hooked to 'heartbeat_received' on the orders screen to refresh the locked status of orders in the list table. |
| 131 | * |
| 132 | * @param array $response The heartbeat response to be sent. |
| 133 | * @param array $data Data sent through the heartbeat. |
| 134 | * @return array Response to be sent. |
| 135 | */ |
| 136 | public function check_locked_orders_ajax( $response, $data ) { |
| 137 | if ( empty( $data['wc-check-locked-orders'] ) || ! is_array( $data['wc-check-locked-orders'] ) ) { |
| 138 | return $response; |
| 139 | } |
| 140 | |
| 141 | $response['wc-check-locked-orders'] = array(); |
| 142 | |
| 143 | $order_ids = array_unique( array_map( 'absint', $data['wc-check-locked-orders'] ) ); |
| 144 | foreach ( $order_ids as $order_id ) { |
| 145 | $order = wc_get_order( $order_id ); |
| 146 | if ( ! $order || ! is_a( $order, \WC_Order::class ) ) { |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | if ( ! $this->is_locked_by_another_user( $order ) || ( ! current_user_can( get_post_type_object( $order->get_type() )->cap->edit_post, $order->get_id() ) && ! current_user_can( 'manage_woocommerce' ) ) ) { |
| 151 | continue; |
| 152 | } |
| 153 | |
| 154 | $response['wc-check-locked-orders'][ $order_id ] = true; |
| 155 | } |
| 156 | |
| 157 | return $response; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Outputs HTML for the lock dialog based on the status of the lock on the order (if any). |
| 162 | * Depending on who owns the lock, this could be a message with the chance to take over or a message indicating that |
| 163 | * someone else has taken over the order. |
| 164 | * |
| 165 | * @param \WC_Order $order Order object. |
| 166 | * @return void |
| 167 | */ |
| 168 | public function render_dialog( $order ) { |
| 169 | $lock = $this->get_lock( $order ); |
| 170 | $user = $lock ? get_user_by( 'id', $lock['user_id'] ) : false; |
| 171 | $locked = $user && ( get_current_user_id() !== $user->ID ); |
| 172 | |
| 173 | $edit_url = wc_get_container()->get( \Automattic\WooCommerce\Internal\Admin\Orders\PageController::class )->get_edit_url( $order->get_id() ); |
| 174 | |
| 175 | $sendback_url = wp_get_referer(); |
| 176 | if ( ! $sendback_url ) { |
| 177 | $sendback_url = wc_get_container()->get( \Automattic\WooCommerce\Internal\Admin\Orders\PageController::class )->get_base_page_url( $order->get_type() ); |
| 178 | } |
| 179 | |
| 180 | $sendback_text = __( 'Go back', 'woocommerce' ); |
| 181 | ?> |
| 182 | <div id="post-lock-dialog" class="notification-dialog-wrap <?php echo $locked ? '' : 'hidden'; ?> order-lock-dialog"> |
| 183 | <div class="notification-dialog-background"></div> |
| 184 | <div class="notification-dialog"> |
| 185 | <?php if ( $locked ) : ?> |
| 186 | <div class="post-locked-message"> |
| 187 | <div class="post-locked-avatar"><?php echo get_avatar( $user->ID, 64 ); ?></div> |
| 188 | <p class="currently-editing wp-tab-first" tabindex="0"> |
| 189 | <?php |
| 190 | // translators: %s is a user's name. |
| 191 | echo esc_html( sprintf( __( '%s is currently editing this order. Do you want to take over?', 'woocommerce' ), esc_html( $user->display_name ) ) ); |
| 192 | ?> |
| 193 | </p> |
| 194 | <p> |
| 195 | <a class="button" href="<?php echo esc_url( $sendback_url ); ?>"><?php echo esc_html( $sendback_text ); ?></a> |
| 196 | <a class="button button-primary wp-tab-last" href="<?php echo esc_url( add_query_arg( 'claim-lock', '1', wp_nonce_url( $edit_url, 'claim-lock-' . $order->get_id() ) ) ); ?>"><?php esc_html_e( 'Take over', 'woocommerce' ); ?></a> |
| 197 | </p> |
| 198 | </div> |
| 199 | <?php else : ?> |
| 200 | <div class="post-taken-over"> |
| 201 | <div class="post-locked-avatar"></div> |
| 202 | <p class="wp-tab-first" tabindex="0"> |
| 203 | <span class="currently-editing"></span><br /> |
| 204 | </p> |
| 205 | <p><a class="button button-primary wp-tab-last" href="<?php echo esc_url( $sendback_url ); ?>"><?php echo esc_html( $sendback_text ); ?></a></p> |
| 206 | </div> |
| 207 | <?php endif; ?> |
| 208 | </div> |
| 209 | </div> |
| 210 | <?php |
| 211 | } |
| 212 | |
| 213 | } |
| 214 |