| 1 |
<?php |
| 2 |
/** |
| 3 |
* Reporting error |
| 4 |
* |
| 5 |
* @since 3.1.3 |
| 6 |
* @package CartFlows |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Reporting error |
| 15 |
*/ |
| 16 |
class CartFlows_Reporting { |
| 17 |
|
| 18 |
/** |
| 19 |
* Instance |
| 20 |
* |
| 21 |
* @since 3.1.3 |
| 22 |
* @access private |
| 23 |
* @var self Class object. |
| 24 |
*/ |
| 25 |
private static $instance = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* Initiator |
| 29 |
* |
| 30 |
* @since 3.1.3 |
| 31 |
* @return self |
| 32 |
*/ |
| 33 |
public static function get_instance() { |
| 34 |
if ( null === self::$instance ) { |
| 35 |
self::$instance = new self(); |
| 36 |
} |
| 37 |
return self::$instance; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Constructor |
| 42 |
* |
| 43 |
* @since 3.1.3 |
| 44 |
*/ |
| 45 |
public function __construct() { |
| 46 |
add_action( 'cartflows_generate_analytics_lead', array( $this, 'schedule_reporting_event' ), 10, 2 ); |
| 47 |
add_action( 'cartflows_track_template_import_lead', array( $this, 'send_analytics_lead' ), 10, 2 ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Schedule the reporting of Error. |
| 52 |
* |
| 53 |
* @param array<string, mixed> $flow Flow data. |
| 54 |
* @param bool $is_error Is error. |
| 55 |
* @since 3.1.3 |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public function schedule_reporting_event( $flow, $is_error ) { |
| 59 |
$has_sent_error_report = get_option( 'cartflows_has_sent_error_report', 'no' ); |
| 60 |
if ( 'no' === $has_sent_error_report ) { |
| 61 |
// Schedule and event in next 30 secs to send error report. |
| 62 |
wp_schedule_single_event( time() + 30, 'cartflows_track_template_import_lead', array( $flow, $is_error ) ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Send Error. |
| 68 |
* |
| 69 |
* @param array<string, mixed> $flow Flow data. |
| 70 |
* @param bool $status Status of the import. |
| 71 |
* @since 3.1.3 |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function send_analytics_lead( $flow, $status ) { |
| 75 |
|
| 76 |
if ( ! is_array( $flow ) ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
$remote_flow_id = ( isset( $flow['ID'] ) && is_scalar( $flow['ID'] ) ) ? (string) $flow['ID'] : ''; |
| 81 |
|
| 82 |
// Nothing to attribute the import to without the remote template ID. |
| 83 |
if ( empty( $remote_flow_id ) ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
$default_builder = \Cartflows_Helper::get_common_setting( 'default_page_builder' ); |
| 88 |
$page_builder = ( ! empty( $flow['page_builder'] ) && is_string( $flow['page_builder'] ) ) ? $flow['page_builder'] : ( is_string( $default_builder ) ? $default_builder : 'gutenberg' ); |
| 89 |
|
| 90 |
$report_data = array( |
| 91 |
'id' => $remote_flow_id, |
| 92 |
'page_builder' => $page_builder, |
| 93 |
'import_status' => $status, |
| 94 |
'version' => CARTFLOWS_VER, |
| 95 |
); |
| 96 |
|
| 97 |
$this->report( $report_data ); |
| 98 |
|
| 99 |
delete_option( 'cartflows_has_sent_error_report' ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Report Error. |
| 104 |
* |
| 105 |
* @param array<string, mixed> $data Error data. |
| 106 |
* @since 3.1.3 |
| 107 |
* |
| 108 |
* @return array<string, mixed> |
| 109 |
*/ |
| 110 |
public function report( $data ) { |
| 111 |
$id = isset( $data['id'] ) ? $data['id'] : 0; |
| 112 |
$import_status = isset( $data['import_status'] ) ? sanitize_text_field( $data['import_status'] ) : 'true'; |
| 113 |
$type = isset( $data['type'] ) ? sanitize_text_field( $data['type'] ) : 'astra-sites'; |
| 114 |
$page_builder = isset( $data['page_builder'] ) ? sanitize_text_field( $data['page_builder'] ) : 'gutenberg'; |
| 115 |
$error_text = isset( $data['error_text'] ) ? sanitize_text_field( $data['error_text'] ) : ''; |
| 116 |
|
| 117 |
$api_args = array( |
| 118 |
'timeout' => 60, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout |
| 119 |
'blocking' => true, |
| 120 |
'body' => array( |
| 121 |
'url' => esc_url( site_url() ), |
| 122 |
'import_status' => $import_status, |
| 123 |
'id' => $id, |
| 124 |
'version' => CARTFLOWS_VER, |
| 125 |
'type' => $type, |
| 126 |
'builder' => $page_builder, |
| 127 |
'error_text' => $error_text, |
| 128 |
), |
| 129 |
); |
| 130 |
|
| 131 |
$import_analytics_url = CARTFLOWS_TEMPLATES_URL . 'wp-json/cartflows-server/v1/import'; |
| 132 |
|
| 133 |
$request = wp_safe_remote_post( $import_analytics_url, $api_args ); |
| 134 |
|
| 135 |
/** |
| 136 |
* Action fired after analytics reporting. |
| 137 |
* |
| 138 |
* @param array<string, mixed> $data The data sent for reporting. |
| 139 |
* @param WP_Error|array<string,mixed> $request The response from the reporting request. |
| 140 |
* |
| 141 |
* @since 3.1.3 |
| 142 |
*/ |
| 143 |
do_action( 'cartflows_after_analytics_reporting', $data, $request ); |
| 144 |
|
| 145 |
if ( is_wp_error( $request ) ) { |
| 146 |
return array( |
| 147 |
'status' => false, |
| 148 |
'data' => $request, |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
$code = (int) wp_remote_retrieve_response_code( $request ); |
| 153 |
$data = json_decode( wp_remote_retrieve_body( $request ), true ); |
| 154 |
|
| 155 |
if ( 200 === $code ) { |
| 156 |
return array( |
| 157 |
'status' => true, |
| 158 |
'data' => $data, |
| 159 |
); |
| 160 |
} |
| 161 |
return array( |
| 162 |
'status' => false, |
| 163 |
'data' => $data, |
| 164 |
); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
CartFlows_Reporting::get_instance(); |
| 169 |
|