| 1 |
<?php |
| 2 |
namespace Elementor\Modules\Components; |
| 3 |
|
| 4 |
use Elementor\Modules\Components\Documents\Component as Component_Document; |
| 5 |
use Elementor\Modules\Components\Document_Lock_Manager; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
class Component_Lock_Manager extends Document_Lock_Manager { |
| 12 |
const ONE_HOUR = 60 * 60; |
| 13 |
private static $instance = null; |
| 14 |
public function __construct() { |
| 15 |
parent::__construct( self::ONE_HOUR ); |
| 16 |
} |
| 17 |
|
| 18 |
public static function get_instance() { |
| 19 |
if ( null === self::$instance ) { |
| 20 |
self::$instance = new self(); |
| 21 |
} |
| 22 |
return self::$instance; |
| 23 |
} |
| 24 |
|
| 25 |
public function register_hooks() { |
| 26 |
add_filter( 'heartbeat_received', [ $this, 'heartbeat_received' ], 10, 2 ); |
| 27 |
} |
| 28 |
|
| 29 |
public function heartbeat_received( $response, $data ) { |
| 30 |
if ( ! isset( $data['elementor_post_lock']['post_ID'] ) ) { |
| 31 |
return $response; |
| 32 |
} |
| 33 |
|
| 34 |
$post_id = $data['elementor_post_lock']['post_ID']; |
| 35 |
|
| 36 |
if ( ! $this->is_component_post( $post_id ) ) { |
| 37 |
return $response; |
| 38 |
} |
| 39 |
|
| 40 |
$lock_data = $this->get_lock_data( $post_id ); |
| 41 |
$user_id = get_current_user_id(); |
| 42 |
if ( $user_id === (int) $lock_data['locked_by'] ) { |
| 43 |
$this->extend_lock( $post_id ); |
| 44 |
} |
| 45 |
|
| 46 |
return $response; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
/** |
| 51 |
* Unlock a component. |
| 52 |
* |
| 53 |
* @param int $post_id The component ID to unlock |
| 54 |
* @return bool True if unlock was successful, false otherwise |
| 55 |
* @throws \Exception If post is not a component type. |
| 56 |
*/ |
| 57 |
public function unlock( $post_id ) { |
| 58 |
if ( ! $this->is_component_post( $post_id ) ) { |
| 59 |
throw new \Exception( 'Post is not a component type' ); |
| 60 |
} |
| 61 |
|
| 62 |
$lock_data = $this->get_lock_data( $post_id ); |
| 63 |
$current_user_id = get_current_user_id(); |
| 64 |
if ( $lock_data['locked_by'] && (int) $lock_data['locked_by'] !== (int) $current_user_id ) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
return parent::unlock( $post_id ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Lock a component. |
| 72 |
* |
| 73 |
* @param int $post_id The component ID to lock |
| 74 |
* @return bool|null True if lock was successful, null if locked by another user, false otherwise |
| 75 |
* @throws \Exception If post is not a component type. |
| 76 |
*/ |
| 77 |
public function lock( $post_id ) { |
| 78 |
if ( ! $this->is_component_post( $post_id ) ) { |
| 79 |
throw new \Exception( 'Post is not a component type' ); |
| 80 |
} |
| 81 |
|
| 82 |
$lock_data = $this->get_lock_data( $post_id ); |
| 83 |
$is_expired = $this->is_lock_expired( $post_id ); |
| 84 |
|
| 85 |
if ( $is_expired ) { |
| 86 |
parent::unlock( $post_id ); |
| 87 |
} elseif ( $lock_data['locked_by'] ) { |
| 88 |
return null; |
| 89 |
} |
| 90 |
|
| 91 |
return parent::lock( $post_id ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Get lock data for a component. |
| 96 |
* |
| 97 |
* @param int $post_id The component ID |
| 98 |
* @return array Lock data with 'locked_by' (int|null), 'locked_at' (int|null) |
| 99 |
* @throws \Exception If post is not a component type. |
| 100 |
*/ |
| 101 |
public function get_lock_data( $post_id ) { |
| 102 |
if ( ! $this->is_component_post( $post_id ) ) { |
| 103 |
throw new \Exception( 'Post is not a component type' ); |
| 104 |
} |
| 105 |
|
| 106 |
return parent::get_lock_data( $post_id ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Extend the lock for a component. |
| 111 |
* |
| 112 |
* @param int $post_id The component ID |
| 113 |
* @return bool|null True if extended successfully, null if not locked or locked by another user |
| 114 |
* @throws \Exception If post is not a component type. |
| 115 |
*/ |
| 116 |
public function extend_lock( $post_id ) { |
| 117 |
if ( ! $this->is_component_post( $post_id ) ) { |
| 118 |
throw new \Exception( 'Post is not a component type' ); |
| 119 |
} |
| 120 |
|
| 121 |
$lock_data = $this->get_lock_data( $post_id ); |
| 122 |
if ( ! $lock_data['locked_by'] ) { |
| 123 |
return null; |
| 124 |
} |
| 125 |
|
| 126 |
$current_user_id = get_current_user_id(); |
| 127 |
if ( (int) $lock_data['locked_by'] !== (int) $current_user_id ) { |
| 128 |
return null; |
| 129 |
} |
| 130 |
|
| 131 |
return parent::extend_lock( $post_id ); |
| 132 |
} |
| 133 |
|
| 134 |
private function is_component_post( $post_id ) { |
| 135 |
return get_post_type( $post_id ) === Component_Document::TYPE; |
| 136 |
} |
| 137 |
} |
| 138 |
|