admin-templates
3 years ago
base
2 years ago
controls
1 year ago
editor-templates
1 year ago
elements
2 years ago
interfaces
1 year ago
libraries
3 years ago
managers
1 year ago
settings
1 year ago
template-library
1 year ago
widgets
1 year ago
api.php
2 years ago
autoloader.php
1 year ago
beta-testers.php
3 years ago
compatibility.php
3 years ago
conditions.php
3 years ago
db.php
2 years ago
embed.php
1 year ago
fonts.php
1 year ago
frontend.php
1 year ago
heartbeat.php
3 years ago
maintenance-mode.php
2 years ago
maintenance.php
3 years ago
plugin.php
2 years ago
preview.php
2 years ago
rollback.php
3 years ago
shapes.php
1 year ago
stylesheet.php
3 years ago
tracker.php
2 years ago
user.php
2 years ago
utils.php
2 years ago
heartbeat.php
98 lines
| 1 | <?php |
| 2 | namespace Elementor; |
| 3 | |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | exit; // Exit if accessed directly. |
| 6 | } |
| 7 | |
| 8 | /** |
| 9 | * Elementor heartbeat. |
| 10 | * |
| 11 | * Elementor heartbeat handler class is responsible for initializing Elementor |
| 12 | * heartbeat. The class communicates with WordPress Heartbeat API while working |
| 13 | * with Elementor. |
| 14 | * |
| 15 | * @since 1.0.0 |
| 16 | */ |
| 17 | class Heartbeat { |
| 18 | |
| 19 | /** |
| 20 | * Heartbeat received. |
| 21 | * |
| 22 | * Locks the Heartbeat response received when editing with Elementor. |
| 23 | * |
| 24 | * Fired by `heartbeat_received` filter. |
| 25 | * |
| 26 | * @since 1.0.0 |
| 27 | * @access public |
| 28 | * |
| 29 | * @param array $response The Heartbeat response. |
| 30 | * @param array $data The `$_POST` data sent. |
| 31 | * |
| 32 | * @return array Heartbeat response received. |
| 33 | */ |
| 34 | public function heartbeat_received( $response, $data ) { |
| 35 | if ( isset( $data['elementor_post_lock']['post_ID'] ) ) { |
| 36 | $post_id = $data['elementor_post_lock']['post_ID']; |
| 37 | $locked_user = Plugin::$instance->editor->get_locked_user( $post_id ); |
| 38 | |
| 39 | if ( ! $locked_user || ! empty( $data['elementor_force_post_lock'] ) ) { |
| 40 | Plugin::$instance->editor->lock_post( $post_id ); |
| 41 | } else { |
| 42 | $response['locked_user'] = $locked_user->display_name; |
| 43 | } |
| 44 | |
| 45 | /** @var Core\Common\Modules\Ajax\Module $ajax */ |
| 46 | $ajax = Plugin::$instance->common->get_component( 'ajax' ); |
| 47 | |
| 48 | $response['elementorNonce'] = $ajax->create_nonce(); |
| 49 | } |
| 50 | return $response; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Refresh nonces. |
| 55 | * |
| 56 | * Filter the nonces to send to the editor when editing with Elementor. Used |
| 57 | * to refresh the nonce when the nonce expires while editing. This way the |
| 58 | * user doesn't need to log-in again as Elementor fetches the new nonce from |
| 59 | * the server using ajax. |
| 60 | * |
| 61 | * Fired by `wp_refresh_nonces` filter. |
| 62 | * |
| 63 | * @since 1.8.0 |
| 64 | * @access public |
| 65 | * |
| 66 | * @param array $response The no-priv Heartbeat response object or array. |
| 67 | * @param array $data The `$_POST` data sent. |
| 68 | * |
| 69 | * @return array Refreshed nonces. |
| 70 | */ |
| 71 | public function refresh_nonces( $response, $data ) { |
| 72 | if ( isset( $data['elementor_post_lock']['post_ID'] ) ) { |
| 73 | /** @var Core\Common\Modules\Ajax\Module $ajax */ |
| 74 | $ajax = Plugin::$instance->common->get_component( 'ajax' ); |
| 75 | |
| 76 | $response['elementor-refresh-nonces'] = [ |
| 77 | 'elementorNonce' => $ajax->create_nonce(), |
| 78 | 'heartbeatNonce' => wp_create_nonce( 'heartbeat-nonce' ), |
| 79 | ]; |
| 80 | } |
| 81 | |
| 82 | return $response; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Heartbeat constructor. |
| 87 | * |
| 88 | * Initializing Elementor heartbeat. |
| 89 | * |
| 90 | * @since 1.0.0 |
| 91 | * @access public |
| 92 | */ |
| 93 | public function __construct() { |
| 94 | add_filter( 'heartbeat_received', [ $this, 'heartbeat_received' ], 10, 2 ); |
| 95 | add_filter( 'wp_refresh_nonces', [ $this, 'refresh_nonces' ], 30, 2 ); |
| 96 | } |
| 97 | } |
| 98 |