PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 3.12.1
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v3.12.1
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / includes / core / import-export / class-wpfnl-export-funnel.php

class-wpfnl-export-funnel.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.12.1, at includes/core/import-export/class-wpfnl-export-funnel.php

295 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPFunnels\Export;
4
5
6 use WPFunnels\Wpfnl_functions;
7 use WPFunnels\Traits\SingletonTrait;
8
9 /**
10 * This class is responsible export funnel
11 *
12 * Export json file
13 * @since 1.7.5
14 */
15 class Wpfnl_Export {
16
17 use SingletonTrait;
18
19
20 /**
21 * Initialize ajax callback
22 *
23 * @since 1.7.5
24 */
25
26 public function init_ajax()
27 {
28 wp_ajax_helper()->handle('wpfnl-export-funnel')
29 ->with_callback([ $this, 'export_funnel' ])
30 ->with_validation($this->get_validation_data());
31
32
33 wp_ajax_helper()->handle('wpfnl-export-all-funnels')
34 ->with_callback([ $this, 'export_all_funnels' ])
35 ->with_validation($this->get_validation_data());
36
37 }
38
39
40
41 /**
42 * Export a single funnel
43 *
44 * @param Array $payload
45 *
46 * @return Array
47 * @since 1.7.5
48 */
49 public function export_funnel( $payload ){
50
51 $funnel_id = isset( $payload['funnel_id'] ) ? $payload['funnel_id'] : '';
52 $response = [
53 'success' => false,
54 ];
55 if( $funnel_id ){
56 $data[] = $this->get_exported_funnel_data( $funnel_id );
57 if( $data ){
58 $response = [
59 'success' => true,
60 'title' => get_the_title( $funnel_id ),
61 'steps' => $data ? $data : [],
62 ];
63 }
64
65 }
66 return $response;
67 }
68
69
70 /**
71 * Export all funnels
72 * @param Array $payload
73 *
74 * @return Array
75 * @since 1.7.5
76 */
77 public function export_all_funnels( $payload ){
78 $status = isset($payload['status']) ? $payload['status'] : 'publish';
79 $funnels = Wpfnl_functions::get_all_funnels( $status );
80 $funnel_data = array();
81 $response = [
82 'success' => false,
83 'data' => $funnel_data,
84 ];
85
86 if( $funnels ){
87 if ( $funnels && is_array($funnels) ) {
88 foreach ( $funnels as $key => $funnel ) {
89 $funnel_data[] = $this->get_exported_funnel_data( $funnel->ID );
90 }
91 }
92 $response['success'] = true;
93 $response['data'] = $funnel_data;
94 }
95
96 return $response;
97 }
98
99 /**
100 * Export selected funnels
101 *
102 * @param array $payload Funnel ids and status.
103 *
104 * @return array|\WP_Rest_Response|\WP_Error
105 * @since 1.7.5
106 */
107 public function bulk_export_funnel ( $payload ){
108 $status = isset( $payload['status'] ) ? $payload['status'] : 'publish';
109 $selected_ids = isset($payload['ids']) ? $payload['ids'] : [];
110
111 $arg = [
112 "post_type" => WPFNL_FUNNELS_POST_TYPE,
113 "fields" => "ids",
114 "orderby" => "date",
115 "order" => 'ASC',
116 "post__in" => $selected_ids,
117 "numberposts" => -1
118 ];
119
120 if( 'all' !== $status ){
121 $arg['post_status'] = $status;
122 }
123
124 $funnels = get_posts( $arg );
125
126 $funnel_data = array();
127
128 $redirect_link = add_query_arg(
129 [
130 'page' => WPFNL_FUNNEL_PAGE_SLUG,
131 ],
132 admin_url('admin.php')
133 );
134
135 $response = [
136 'success' => false,
137 'data' => $funnel_data,
138 'redirectUrl' => $redirect_link
139 ];
140 if( $funnels ){
141 if ( $funnels && is_array($funnels) ) {
142 foreach ( $funnels as $key => $funnel_id ) {
143 $funnel_data[] = $this->get_exported_funnel_data( $funnel_id );
144 }
145 }
146 $response['success'] = true;
147 $response['data'] = $funnel_data;
148 $response['redirectUrl'] = $redirect_link;
149 }
150
151 return rest_ensure_response ( $response );
152 }
153
154 /**
155 * Get exported funnel data by funnel id
156 *
157 * @param Int $funnel_id
158 * @return Array
159 * @since 1.7.5
160 */
161 public function get_exported_funnel_data( $funnel_id ){
162
163 if( $funnel_id ){
164 $funnel_id = trim($funnel_id);
165 $funnel_id = (int)$funnel_id;
166 $is_export = apply_filters( 'wpfunnels/allow_to_export', true );
167 $steps = get_post_meta( $funnel_id, '_steps', true );
168 $all_funnel_meta = get_post_meta( $funnel_id );
169 $exclude_funnel_meta = ['funnel_automation_data', 'global_funnel_start_condition' ];
170 $all_funnel_meta = $this->exclude_meta( $all_funnel_meta, $exclude_funnel_meta );
171
172 $funnel_data['funnel_id'] = $funnel_id;
173 $funnel_data['funnel_name'] = get_the_title( $funnel_id );
174 $funnel_data['funnel_meta'] = $all_funnel_meta;
175 $funnel_data['steps_data'] = [];
176
177 if ( $steps && is_array($steps) ) {
178
179 foreach ( $steps as $key => $step ) {
180 if( isset( $step['id'], $step['step_type']) ){
181 $all_meta = get_post_meta( $step['id'] );
182 $exclude_step_meta = [
183 '_wpfnl_checkout_products',
184 '_wpfnl_checkout_discount_main_product',
185 'order-bump-settings',
186 '_wpfnl_upsell_products',
187 '_wpfnl_upsell_discount',
188 '_wpfnl_downsell_products',
189 '_wpfnl_downsell_discount',
190 'global_funnel_upsell_rules',
191 'global_funnel_downsell_rules',
192 ];
193
194 $all_meta = $this->exclude_meta( $all_meta, $exclude_step_meta );
195 $post_step = get_post($step['id']);
196 $step_slug = isset( $post_step->post_name) ? $post_step->post_name : '';
197 $step_data = array(
198 'id' => $step['id'],
199 'title' => get_the_title( $step['id'] ),
200 'slug' => $step_slug,
201 'type' => $step['step_type'],
202 'post_content' => '',
203 'page_template' => get_post_meta( $step['id'], '_wp_page_template', true),
204 'meta' => $all_meta
205 );
206 if ( $is_export ) {
207 $step_obj = get_post( $step['id'] );
208 $step_data['post_content'] = isset($step_obj->post_content) ? $step_obj->post_content : '';
209 }
210 $ab_settings = get_post_meta( $step['id'], '_wpfnl_ab_testing_start_settings', true );
211 if( $ab_settings ){
212 if( isset( $ab_settings['variations'] ) && is_array( $ab_settings['variations'] ) ){
213 foreach( $ab_settings['variations'] as $index => $variations ){
214 if( isset( $variations['stepId'] ) ){
215 $ab_step_meta = $this->export_ab_step_metas( $variations['stepId'] );
216 $ab_step_obj = get_post( $variations['stepId'] );
217 $step_data['ab_step_data'][] = [
218 'step_id' => $variations['stepId'],
219 'title' => get_the_title( $variations['stepId'] ),
220 'type' => get_post_meta( $variations['stepId'], '_step_type', true),
221 'post_content' => isset($ab_step_obj->post_content) ? $ab_step_obj->post_content : '',
222 'page_template' => get_post_meta( $variations['stepId'], '_wp_page_template', true),
223 'meta' => $ab_step_meta
224 ];
225 }
226 }
227 }
228 }
229 $funnel_data['steps_data'][] = $step_data;
230 }
231 }
232
233 return $funnel_data;
234 }
235 }
236 return false;
237 }
238
239
240 /**
241 * Export A/B testing steps
242 *
243 * @param Int $step_id
244 *
245 * @return Array
246 * @since 1.7.5
247 */
248 public function export_ab_step_metas( $step_id ){
249 $step_data = [];
250 if( $step_id ){
251 $is_export = apply_filters( 'wpfunnels/allow_to_export_ab_step', true );
252 if ( $is_export ) {
253 $all_meta = get_post_meta( $step_id );
254 $step_data = array(
255 'id' => $step_id,
256 'title' => get_the_title( $step_id ),
257 'type' => get_post_meta( $step_id, '_step_type', true),
258 'post_content' => '',
259 'page_template' => get_post_meta( $step_id, '_wp_page_template', true),
260 'meta' => $all_meta
261 );
262 $step_obj = get_post( $step_id );
263 $step_data['post_content'] = isset($step_obj->post_content) ? $step_obj->post_content : '';
264 }
265 }
266 return $step_data;
267 }
268
269
270 /**
271 * Exclude meta
272 * @param array $all_meta
273 * @param array $meta_key Which meta should be excluded
274 *
275 */
276 private function exclude_meta( $all_meta, $meta_keys ){
277 if( is_array( $meta_keys ) ){
278 foreach( $meta_keys as $meta_key ){
279 if( isset( $all_meta[$meta_key] ) ){
280 unset( $all_meta[$meta_key] );
281 }
282 }
283 }
284 return $all_meta;
285 }
286
287
288 public function get_validation_data()
289 {
290 return [
291 'logged_in' => true,
292 'user_can' => 'wpf_manage_funnels',
293 ];
294 }
295 }