| @@ -1,15 +1,16 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Sellkit\Admin\Funnel\Importer; |
| 4 | 4 | |
| 5 | -use Elementor\Plugin; | |
| 5 | +defined( 'ABSPATH' ) || die(); | |
| 6 | 6 | |
| 7 | -defined( 'ABSPATH' ) || die(); | |
| 7 | +use Sellkit\Global_Checkout\Checkout; | |
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | 10 | * Class Ajax handler. |
| 11 | 11 | * |
| 12 | + * @SuppressWarnings(PHPMD.NPathComplexity) | |
| 12 | 13 | * @since 1.1.0 |
| 13 | 14 | */ |
| 14 | 15 | class Ajax_Handler { |
| 15 | 16 | |
| @@ -41,23 +42,37 @@ | ||
| 41 | 42 | * |
| 42 | 43 | * @since 1.1.0 |
| 43 | 44 | */ |
| 44 | 45 | public function import_steps_data() { |
| 45 | - $nonce = filter_input( INPUT_GET, 'nonce', FILTER_SANITIZE_STRING ); | |
| 46 | - $step_id = filter_input( INPUT_GET, 'step_id', FILTER_SANITIZE_STRING ); | |
| 47 | - $funnel_id = filter_input( INPUT_GET, 'funnel_id', FILTER_SANITIZE_STRING ); | |
| 46 | + check_ajax_referer( 'sellkit', 'nonce' ); | |
| 48 | 47 | |
| 49 | - wp_verify_nonce( $nonce, 'sellkit' ); | |
| 48 | + $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' ); | |
| 49 | + $step_id = sellkit_htmlspecialchars( INPUT_GET, 'step_id' ); | |
| 50 | + $funnel_id = sellkit_htmlspecialchars( INPUT_GET, 'funnel_id' ); | |
| 51 | + $origin_node = filter_input( INPUT_GET, 'origin_node', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); | |
| 52 | + $target_node = filter_input( INPUT_GET, 'target_node', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); | |
| 53 | + $target_index = filter_input( INPUT_GET, 'target_index', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); | |
| 54 | + $origin_node = isset( $origin_node ) ? $origin_node : 'none'; | |
| 55 | + $target_node = ! empty( $target_node ) ? $target_node : 'none'; | |
| 50 | 56 | |
| 51 | - $data = $this->step_importer->import_from_api( $step_id ); | |
| 52 | - $new_step_id = $this->step_importer->import_to_funnel( $data, $funnel_id ); | |
| 57 | + if ( ! wp_verify_nonce( $nonce, 'sellkit' ) ) { | |
| 58 | + wp_send_json_error( __( 'Invalid nonce.', 'sellkit' ) ); | |
| 59 | + } | |
| 53 | 60 | |
| 61 | + $data = $this->step_importer->import_from_api( $step_id ); | |
| 62 | + | |
| 63 | + if ( empty( $data ) || is_wp_error( $data ) ) { | |
| 64 | + wp_send_json_error( __( 'Something went wrong.', 'sellkit' ) ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + $new_step_id = $this->step_importer->import_to_funnel( $data, $funnel_id, $origin_node, $target_node, $target_index ); | |
| 68 | + | |
| 54 | 69 | $this->step_importer->import_template( $data, $new_step_id ); |
| 55 | 70 | $this->step_importer->run(); |
| 56 | 71 | |
| 57 | 72 | sleep( 3 ); |
| 58 | 73 | |
| 59 | - wp_send_json_success( esc_html__( 'The data has been imported successfully.' ) ); | |
| 74 | + wp_send_json_success( esc_html__( 'The data has been imported successfully.', 'sellkit' ) ); | |
| 60 | 75 | } |
| 61 | 76 | |
| 62 | 77 | /** |
| 63 | 78 | * Gets steps. |
| @@ -64,13 +79,17 @@ | ||
| 64 | 79 | * |
| 65 | 80 | * @since 1.1.0 |
| 66 | 81 | */ |
| 67 | 82 | public function get_steps() { |
| 68 | - $nonce = filter_input( INPUT_GET, 'nonce', FILTER_SANITIZE_STRING ); | |
| 69 | - $type = filter_input( INPUT_GET, 'type', FILTER_SANITIZE_STRING ); | |
| 83 | + check_ajax_referer( 'sellkit', 'nonce' ); | |
| 70 | 84 | |
| 71 | - wp_verify_nonce( $nonce, 'sellkit' ); | |
| 85 | + $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' ); | |
| 86 | + $type = sellkit_htmlspecialchars( INPUT_GET, 'type' ); | |
| 72 | 87 | |
| 88 | + if ( ! wp_verify_nonce( $nonce, 'sellkit' ) ) { | |
| 89 | + wp_send_json_error( __( 'Invalid nonce.', 'sellkit' ) ); | |
| 90 | + } | |
| 91 | + | |
| 73 | 92 | $response = wp_remote_get( "https://templates.getsellkit.com/wp-json/sellkit/v1/steps/type={$type}", [ |
| 74 | 93 | 'timeout' => 10, |
| 75 | 94 | ] ); |
| 76 | 95 | |
| @@ -88,12 +107,16 @@ | ||
| 88 | 107 | * |
| 89 | 108 | * @since 1.1.0 |
| 90 | 109 | */ |
| 91 | 110 | public function get_funnels() { |
| 92 | - $nonce = filter_input( INPUT_GET, 'nonce', FILTER_SANITIZE_STRING ); | |
| 111 | + check_ajax_referer( 'sellkit', 'nonce' ); | |
| 93 | 112 | |
| 94 | - wp_verify_nonce( $nonce, 'sellkit' ); | |
| 113 | + $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' ); | |
| 95 | 114 | |
| 115 | + if ( ! wp_verify_nonce( $nonce, 'sellkit' ) ) { | |
| 116 | + wp_send_json_error( __( 'Invalid nonce.', 'sellkit' ) ); | |
| 117 | + } | |
| 118 | + | |
| 96 | 119 | $response = wp_remote_get( 'https://templates.getsellkit.com/wp-json/sellkit/v1/funnels', [ |
| 97 | 120 | 'timeout' => 10, |
| 98 | 121 | ] ); |
| 99 | 122 | |
| @@ -111,13 +134,17 @@ | ||
| 111 | 134 | * |
| 112 | 135 | * @since 1.1.0 |
| 113 | 136 | */ |
| 114 | 137 | public function get_funnel_data() { |
| 115 | - $nonce = filter_input( INPUT_GET, 'nonce', FILTER_SANITIZE_STRING ); | |
| 116 | - $funnel_id = filter_input( INPUT_GET, 'funnel_id', FILTER_SANITIZE_STRING ); | |
| 138 | + check_ajax_referer( 'sellkit', 'nonce' ); | |
| 117 | 139 | |
| 118 | - wp_verify_nonce( $nonce, 'sellkit' ); | |
| 140 | + $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' ); | |
| 141 | + $funnel_id = sellkit_htmlspecialchars( INPUT_GET, 'funnel_id' ); | |
| 119 | 142 | |
| 143 | + if ( ! wp_verify_nonce( $nonce, 'sellkit' ) ) { | |
| 144 | + wp_send_json_error( __( 'Invalid nonce.', 'sellkit' ) ); | |
| 145 | + } | |
| 146 | + | |
| 120 | 147 | $funnel_data = $this->import_funnel_data_from_api( $funnel_id ); |
| 121 | 148 | |
| 122 | 149 | if ( ! empty( $funnel_data ) ) { |
| 123 | 150 | wp_send_json_success( $funnel_data ); |
| @@ -122,9 +149,9 @@ | ||
| 122 | 149 | if ( ! empty( $funnel_data ) ) { |
| 123 | 150 | wp_send_json_success( $funnel_data ); |
| 124 | 151 | } |
| 125 | 152 | |
| 126 | - wp_send_json_error( esc_html__( 'Something went wrong', 'sellkit' ) ); | |
| 153 | + wp_send_json_error( esc_html__( 'Something went wrong.', 'sellkit' ) ); | |
| 127 | 154 | } |
| 128 | 155 | |
| 129 | 156 | /** |
| 130 | 157 | * Imports step data. |
| @@ -131,13 +158,22 @@ | ||
| 131 | 158 | * |
| 132 | 159 | * @since 1.1.0 |
| 133 | 160 | */ |
| 134 | 161 | public function import_funnel() { |
| 135 | - $nonce = filter_input( INPUT_GET, 'nonce', FILTER_SANITIZE_STRING ); | |
| 136 | - $funnel_id = filter_input( INPUT_GET, 'funnel_id', FILTER_SANITIZE_STRING ); | |
| 162 | + check_ajax_referer( 'sellkit', 'nonce' ); | |
| 137 | 163 | |
| 138 | - wp_verify_nonce( $nonce, 'sellkit' ); | |
| 164 | + $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' ); | |
| 165 | + $funnel_id = sellkit_htmlspecialchars( INPUT_GET, 'funnel_id' ); | |
| 166 | + $page = sellkit_htmlspecialchars( INPUT_GET, 'page' ); | |
| 139 | 167 | |
| 168 | + if ( ! wp_verify_nonce( $nonce, 'sellkit' ) ) { | |
| 169 | + wp_send_json_error( __( 'Invalid nonce.', 'sellkit' ) ); | |
| 170 | + } | |
| 171 | + | |
| 172 | + if ( 'checkout' === $page && get_post_status( get_option( Checkout::SELLKIT_GLOBAL_CHECKOUT_OPTION, 0 ) ) ) { | |
| 173 | + wp_send_json_error( __( 'In order to iport a new global checkout,Please remove previous one.', 'sellkit' ) ); | |
| 174 | + } | |
| 175 | + | |
| 140 | 176 | $funnel_data = $this->import_funnel_data_from_api( $funnel_id ); |
| 141 | 177 | |
| 142 | 178 | if ( empty( $funnel_data ) ) { |
| 143 | 179 | wp_send_json_error( __( 'something went wrong', 'sellkit' ) ); |
| @@ -142,14 +178,44 @@ | ||
| 142 | 178 | if ( empty( $funnel_data ) ) { |
| 143 | 179 | wp_send_json_error( __( 'something went wrong', 'sellkit' ) ); |
| 144 | 180 | } |
| 145 | 181 | |
| 182 | + $new_funnel_id = $this->import_funnel_by_data( $funnel_data ); | |
| 183 | + | |
| 184 | + if ( 'checkout' === $page ) { | |
| 185 | + update_option( Checkout::SELLKIT_GLOBAL_CHECKOUT_OPTION, $new_funnel_id ); | |
| 186 | + } | |
| 187 | + | |
| 188 | + wp_send_json_success( [ 'funnelId' => $new_funnel_id ] ); | |
| 189 | + } | |
| 190 | + | |
| 191 | + /** | |
| 192 | + * Import funnel. | |
| 193 | + * | |
| 194 | + * @since 1.5.0 | |
| 195 | + * @param object $funnel_data funnel data. | |
| 196 | + * @param boolean|object $steps_data steps data. | |
| 197 | + */ | |
| 198 | + public function import_funnel_by_data( $funnel_data, $steps_data = false ) { | |
| 199 | + $new_funnel_id = $this->create_new_funnel( $funnel_data ); | |
| 146 | 200 | $steps = $funnel_data->sellkit_steps; |
| 147 | - $new_funnel_id = $this->create_new_funnel( $funnel_data ); | |
| 148 | 201 | $new_step_data = []; |
| 202 | + $data = ''; | |
| 149 | 203 | |
| 150 | - foreach ( $steps as $step ) { | |
| 151 | - $data = $this->step_importer->import_from_api( $step->page_id ); | |
| 204 | + foreach ( $steps as $key => $step ) { | |
| 205 | + if ( false !== $steps_data && property_exists( $step, 'page_id' ) ) { | |
| 206 | + $step_id = $step->page_id; | |
| 207 | + $data = $steps_data->$step_id; | |
| 208 | + } | |
| 209 | + | |
| 210 | + if ( false === $steps_data && property_exists( $step, 'page_id' ) ) { | |
| 211 | + $data = $this->step_importer->import_from_api( $step->page_id ); | |
| 212 | + } | |
| 213 | + | |
| 214 | + if ( is_wp_error( $data ) ) { | |
| 215 | + wp_send_json_error( __( 'Something went wrong.', 'sellkit' ) ); | |
| 216 | + } | |
| 217 | + | |
| 152 | 218 | $new_step_id = $this->step_importer->import_to_funnel( $data, $new_funnel_id ); |
| 153 | 219 | |
| 154 | 220 | $this->step_importer->import_template( $data, $new_step_id ); |
| 155 | 221 | |
| @@ -159,16 +225,24 @@ | ||
| 159 | 225 | if ( ! empty( $step->data->products->list ) ) { |
| 160 | 226 | unset( $step->data->products->list ); |
| 161 | 227 | } |
| 162 | 228 | |
| 163 | - $new_step_data[] = (array) $step; | |
| 229 | + if ( ! empty( $step->bump[0]->data->products->list ) ) { | |
| 230 | + unset( $step->bump[0]->data->products->list ); | |
| 231 | + } | |
| 232 | + | |
| 233 | + $new_step_data[ $key ] = json_decode( wp_json_encode( $step ), true ); | |
| 164 | 234 | } |
| 165 | 235 | |
| 166 | - update_post_meta( $new_funnel_id, 'sellkit_steps', $new_step_data ); | |
| 236 | + if ( false === $steps_data ) { | |
| 237 | + update_post_meta( $new_funnel_id, 'sellkit_steps', $new_step_data ); | |
| 238 | + } else { | |
| 239 | + update_post_meta( $new_funnel_id, 'nodes', $new_step_data ); | |
| 240 | + } | |
| 167 | 241 | |
| 168 | 242 | $this->step_importer->run(); |
| 169 | 243 | |
| 170 | - wp_send_json_success( [ 'funnelId' => $new_funnel_id ] ); | |
| 244 | + return $new_funnel_id; | |
| 171 | 245 | } |
| 172 | 246 | |
| 173 | 247 | /** |
| 174 | 248 | * It creates new funnel. |