heartbeat.php (2635B)
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 }