| @@ -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 = sellkit_htmlspecialchars( INPUT_GET, 'nonce' ); | |
| 46 | - $step_id = sellkit_htmlspecialchars( INPUT_GET, 'step_id' ); | |
| 47 | - $funnel_id = sellkit_htmlspecialchars( INPUT_GET, 'funnel_id' ); | |
| 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,12 +79,16 @@ | ||
| 64 | 79 | * |
| 65 | 80 | * @since 1.1.0 |
| 66 | 81 | */ |
| 67 | 82 | public function get_steps() { |
| 83 | + check_ajax_referer( 'sellkit', 'nonce' ); | |
| 84 | + | |
| 68 | 85 | $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' ); |
| 69 | 86 | $type = sellkit_htmlspecialchars( INPUT_GET, 'type' ); |
| 70 | 87 | |
| 71 | - wp_verify_nonce( $nonce, 'sellkit' ); | |
| 88 | + if ( ! wp_verify_nonce( $nonce, 'sellkit' ) ) { | |
| 89 | + wp_send_json_error( __( 'Invalid nonce.', 'sellkit' ) ); | |
| 90 | + } | |
| 72 | 91 | |
| 73 | 92 | $response = wp_remote_get( "https://templates.getsellkit.com/wp-json/sellkit/v1/steps/type={$type}", [ |
| 74 | 93 | 'timeout' => 10, |
| 75 | 94 | ] ); |
| @@ -88,11 +107,15 @@ | ||
| 88 | 107 | * |
| 89 | 108 | * @since 1.1.0 |
| 90 | 109 | */ |
| 91 | 110 | public function get_funnels() { |
| 111 | + check_ajax_referer( 'sellkit', 'nonce' ); | |
| 112 | + | |
| 92 | 113 | $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' ); |
| 93 | 114 | |
| 94 | - wp_verify_nonce( $nonce, 'sellkit' ); | |
| 115 | + if ( ! wp_verify_nonce( $nonce, 'sellkit' ) ) { | |
| 116 | + wp_send_json_error( __( 'Invalid nonce.', 'sellkit' ) ); | |
| 117 | + } | |
| 95 | 118 | |
| 96 | 119 | $response = wp_remote_get( 'https://templates.getsellkit.com/wp-json/sellkit/v1/funnels', [ |
| 97 | 120 | 'timeout' => 10, |
| 98 | 121 | ] ); |
| @@ -111,12 +134,16 @@ | ||
| 111 | 134 | * |
| 112 | 135 | * @since 1.1.0 |
| 113 | 136 | */ |
| 114 | 137 | public function get_funnel_data() { |
| 138 | + check_ajax_referer( 'sellkit', 'nonce' ); | |
| 139 | + | |
| 115 | 140 | $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' ); |
| 116 | 141 | $funnel_id = sellkit_htmlspecialchars( INPUT_GET, 'funnel_id' ); |
| 117 | 142 | |
| 118 | - wp_verify_nonce( $nonce, 'sellkit' ); | |
| 143 | + if ( ! wp_verify_nonce( $nonce, 'sellkit' ) ) { | |
| 144 | + wp_send_json_error( __( 'Invalid nonce.', 'sellkit' ) ); | |
| 145 | + } | |
| 119 | 146 | |
| 120 | 147 | $funnel_data = $this->import_funnel_data_from_api( $funnel_id ); |
| 121 | 148 | |
| 122 | 149 | if ( ! empty( $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() { |
| 162 | + check_ajax_referer( 'sellkit', 'nonce' ); | |
| 163 | + | |
| 135 | 164 | $nonce = sellkit_htmlspecialchars( INPUT_GET, 'nonce' ); |
| 136 | 165 | $funnel_id = sellkit_htmlspecialchars( INPUT_GET, 'funnel_id' ); |
| 166 | + $page = sellkit_htmlspecialchars( INPUT_GET, 'page' ); | |
| 137 | 167 | |
| 138 | - wp_verify_nonce( $nonce, 'sellkit' ); | |
| 168 | + if ( ! wp_verify_nonce( $nonce, 'sellkit' ) ) { | |
| 169 | + wp_send_json_error( __( 'Invalid nonce.', 'sellkit' ) ); | |
| 170 | + } | |
| 139 | 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 | |
| @@ -163,16 +229,20 @@ | ||
| 163 | 229 | if ( ! empty( $step->bump[0]->data->products->list ) ) { |
| 164 | 230 | unset( $step->bump[0]->data->products->list ); |
| 165 | 231 | } |
| 166 | 232 | |
| 167 | - $new_step_data[] = (array) $step; | |
| 233 | + $new_step_data[ $key ] = json_decode( wp_json_encode( $step ), true ); | |
| 168 | 234 | } |
| 169 | 235 | |
| 170 | - 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 | + } | |
| 171 | 241 | |
| 172 | 242 | $this->step_importer->run(); |
| 173 | 243 | |
| 174 | - wp_send_json_success( [ 'funnelId' => $new_funnel_id ] ); | |
| 244 | + return $new_funnel_id; | |
| 175 | 245 | } |
| 176 | 246 | |
| 177 | 247 | /** |
| 178 | 248 | * It creates new funnel. |