PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta1
Elementor Website Builder – more than just a page builder v4.3.0-beta1
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / includes / heartbeat.php

heartbeat.php in Elementor Website Builder – more than just a page builder 4.3.0-beta1, at includes/heartbeat.php

110 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if ( array_key_exists( 'elementor_has_unsaved', $data ) ) {
46 do_action( 'elementor/heartbeat/unsaved_signal', $post_id, $data['elementor_has_unsaved'] );
47 }
48
49 $mutation_marker = apply_filters( 'elementor/heartbeat/mutation_marker', null, $post_id );
50 if ( $mutation_marker ) {
51 $response['elementor_mcp_mutation'] = $mutation_marker;
52 }
53
54 $response['elementor_server_time'] = time();
55
56 /** @var Core\Common\Modules\Ajax\Module $ajax */
57 $ajax = Plugin::$instance->common->get_component( 'ajax' );
58
59 $response['elementorNonce'] = $ajax->create_nonce();
60 }
61
62 return $response;
63 }
64
65 /**
66 * Refresh nonces.
67 *
68 * Filter the nonces to send to the editor when editing with Elementor. Used
69 * to refresh the nonce when the nonce expires while editing. This way the
70 * user doesn't need to log-in again as Elementor fetches the new nonce from
71 * the server using ajax.
72 *
73 * Fired by `wp_refresh_nonces` filter.
74 *
75 * @since 1.8.0
76 * @access public
77 *
78 * @param array $response The no-priv Heartbeat response object or array.
79 * @param array $data The `$_POST` data sent.
80 *
81 * @return array Refreshed nonces.
82 */
83 public function refresh_nonces( $response, $data ) {
84 if ( isset( $data['elementor_post_lock']['post_ID'] ) ) {
85 /** @var Core\Common\Modules\Ajax\Module $ajax */
86 $ajax = Plugin::$instance->common->get_component( 'ajax' );
87
88 $response['elementor-refresh-nonces'] = [
89 'elementorNonce' => $ajax->create_nonce(),
90 'heartbeatNonce' => wp_create_nonce( 'heartbeat-nonce' ),
91 ];
92 }
93
94 return $response;
95 }
96
97 /**
98 * Heartbeat constructor.
99 *
100 * Initializing Elementor heartbeat.
101 *
102 * @since 1.0.0
103 * @access public
104 */
105 public function __construct() {
106 add_filter( 'heartbeat_received', [ $this, 'heartbeat_received' ], 10, 2 );
107 add_filter( 'wp_refresh_nonces', [ $this, 'refresh_nonces' ], 30, 2 );
108 }
109 }
110