heartbeat.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor heartbeat.
  8. *
  9. * Elementor heartbeat handler class is responsible for initializing Elementor
  10. * heartbeat. The class communicates with WordPress Heartbeat API while working
  11. * with Elementor.
  12. *
  13. * @since 1.0.0
  14. */
  15. class Heartbeat {
  16. /**
  17. * Heartbeat received.
  18. *
  19. * Locks the Heartbeat response received when editing with Elementor.
  20. *
  21. * Fired by `heartbeat_received` filter.
  22. *
  23. * @since 1.0.0
  24. * @access public
  25. *
  26. * @param array $response The Heartbeat response.
  27. * @param array $data The `$_POST` data sent.
  28. *
  29. * @return array Heartbeat response received.
  30. */
  31. public function heartbeat_received( $response, $data ) {
  32. if ( isset( $data['elementor_post_lock']['post_ID'] ) ) {
  33. $post_id = $data['elementor_post_lock']['post_ID'];
  34. $locked_user = Plugin::$instance->editor->get_locked_user( $post_id );
  35. if ( ! $locked_user || ! empty( $data['elementor_force_post_lock'] ) ) {
  36. Plugin::$instance->editor->lock_post( $post_id );
  37. } else {
  38. $response['locked_user'] = $locked_user->display_name;
  39. }
  40. $response['elementorNonce'] = Plugin::$instance->editor->create_nonce( get_post_type( $post_id ) );
  41. }
  42. return $response;
  43. }
  44. /**
  45. * Refresh nonces.
  46. *
  47. * Filter the nonces to send to the editor when editing with Elementor. Used
  48. * to refresh the nonce when the nonce expires while editing. This way the
  49. * user doesn't need to log-in again as Elementor fetches the new nonce from
  50. * the server using ajax.
  51. *
  52. * Fired by `wp_refresh_nonces` filter.
  53. *
  54. * @since 1.8.0
  55. * @access public
  56. *
  57. * @param array $response The no-priv Heartbeat response object or array.
  58. * @param array $data The `$_POST` data sent.
  59. *
  60. * @return array Refreshed nonces.
  61. */
  62. public function refresh_nonces( $response, $data ) {
  63. if ( isset( $data['elementor_post_lock']['post_ID'] ) ) {
  64. $post_type = get_post_type( $data['elementor_post_lock']['post_ID'] );
  65. $response['elementor-refresh-nonces'] = [
  66. 'elementorNonce' => Plugin::$instance->editor->create_nonce( $post_type ),
  67. 'heartbeatNonce' => wp_create_nonce( 'heartbeat-nonce' ),
  68. ];
  69. }
  70. return $response;
  71. }
  72. /**
  73. * Heartbeat constructor.
  74. *
  75. * Initializing Elementor heartbeat.
  76. *
  77. * @since 1.0.0
  78. * @access public
  79. */
  80. public function __construct() {
  81. add_filter( 'heartbeat_received', [ $this, 'heartbeat_received' ], 10, 2 );
  82. add_filter( 'wp_refresh_nonces', [ $this, 'refresh_nonces' ], 30, 2 );
  83. }
  84. }