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