| 1 |
<?php |
| 2 |
|
| 3 |
namespace SiteLeads\Features; |
| 4 |
|
| 5 |
use SiteLeads\Constants; |
| 6 |
use SiteLeads\Core\Singleton; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class FCPreviewWidget { |
| 13 |
use Singleton; |
| 14 |
|
| 15 |
protected function __construct() { |
| 16 |
// Add ajax endpoints |
| 17 |
add_action( 'wp_ajax_siteleads_set_preview_data', array( $this, 'setPreviewData' ) ); |
| 18 |
add_action( 'wp_ajax_siteleads_set_preview_agent_data', array( $this, 'setAgentPreviewData' ) ); |
| 19 |
// add action to clean up preview data when post with type Constants::$siteLeadsDataPostType updates |
| 20 |
add_action( 'save_post_' . Constants::$siteLeadsDataPostType, array( __CLASS__, 'cleanupPreviewData' ) ); |
| 21 |
} |
| 22 |
|
| 23 |
public function setPreviewData() { |
| 24 |
check_ajax_referer( 'events_nonce', 'security' ); |
| 25 |
|
| 26 |
//phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- we will validate the JSON in the next step, so no need to sanitize here |
| 27 |
$settings = isset( $_POST['data'] ) ? wp_unslash( $_POST['data'] ) : ''; |
| 28 |
|
| 29 |
$parsed_settings = json_decode( $settings, true ); |
| 30 |
if ( json_last_error() !== JSON_ERROR_NONE || ! is_array( $parsed_settings ) ) { |
| 31 |
delete_transient( 'siteleads_preview_data' ); |
| 32 |
wp_send_json_error( 'Invalid JSON data' ); |
| 33 |
} |
| 34 |
|
| 35 |
set_transient( 'siteleads_preview_data', $settings, 60 * 5 ); |
| 36 |
wp_send_json_success( $settings ); |
| 37 |
} |
| 38 |
|
| 39 |
public function setAgentPreviewData() { |
| 40 |
check_ajax_referer( 'events_nonce', 'security' ); |
| 41 |
|
| 42 |
//phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- we will validate the JSON in the next step, so no need to sanitize here |
| 43 |
$data = isset( $_POST['data'] ) ? wp_unslash( $_POST['data'] ) : ''; |
| 44 |
|
| 45 |
$transient_key = 'siteleads_agent_preview_data'; |
| 46 |
|
| 47 |
if ( empty( $data ) ) { |
| 48 |
delete_transient( $transient_key ); |
| 49 |
wp_send_json_success( null ); |
| 50 |
} |
| 51 |
|
| 52 |
$parsed_settings = json_decode( $data, true ); |
| 53 |
|
| 54 |
$agent_id = isset( $parsed_settings['id'] ) ? $parsed_settings['id'] : null; |
| 55 |
$agent_settings = isset( $parsed_settings['settings'] ) ? $parsed_settings['settings'] : null; |
| 56 |
|
| 57 |
if ( ! $agent_id || ! $agent_settings ) { |
| 58 |
delete_transient( $transient_key ); |
| 59 |
wp_send_json_success( null ); |
| 60 |
} |
| 61 |
|
| 62 |
$transient_data = get_transient( $transient_key ); |
| 63 |
|
| 64 |
if ( empty( $transient_data ) ) { |
| 65 |
$transient_data = array(); |
| 66 |
} |
| 67 |
|
| 68 |
$transient_data[ $agent_id ] = $agent_settings; |
| 69 |
|
| 70 |
set_transient( 'siteleads_agent_preview_data', $transient_data, 60 * 5 ); |
| 71 |
wp_send_json_success( $parsed_settings ); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
public static function cleanupPreviewData() { |
| 76 |
delete_transient( 'siteleads_preview_data' ); |
| 77 |
delete_transient( 'siteleads_agent_preview_data' ); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
public static function getAgentPreviewData( $agent_id ) { |
| 82 |
$transient_data = get_transient( 'siteleads_agent_preview_data' ); |
| 83 |
if ( empty( $transient_data ) || ! is_array( $transient_data ) || ! isset( $transient_data[ $agent_id ] ) ) { |
| 84 |
return null; |
| 85 |
} |
| 86 |
|
| 87 |
return $transient_data[ $agent_id ]; |
| 88 |
} |
| 89 |
} |
| 90 |
|