| 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 = array_merge( |
| 170 |
[ 'funnel_automation_data', 'global_funnel_start_condition' ], |
| 171 |
Wpfnl_functions::VOLATILE_TRANSFER_META_KEYS |
| 172 |
); |
| 173 |
$all_funnel_meta = $this->exclude_meta( $all_funnel_meta, $exclude_funnel_meta ); |
| 174 |
|
| 175 |
$funnel_data['funnel_id'] = $funnel_id; |
| 176 |
$funnel_data['funnel_name'] = get_the_title( $funnel_id ); |
| 177 |
$funnel_data['funnel_meta'] = $all_funnel_meta; |
| 178 |
$funnel_data['steps_data'] = []; |
| 179 |
|
| 180 |
if ( $steps && is_array($steps) ) { |
| 181 |
|
| 182 |
foreach ( $steps as $key => $step ) { |
| 183 |
if( isset( $step['id'], $step['step_type']) ){ |
| 184 |
$all_meta = get_post_meta( $step['id'] ); |
| 185 |
$exclude_step_meta = array_merge( |
| 186 |
[ |
| 187 |
'_wpfnl_checkout_products', |
| 188 |
'_wpfnl_checkout_discount_main_product', |
| 189 |
'order-bump-settings', |
| 190 |
'_wpfnl_upsell_products', |
| 191 |
'_wpfnl_upsell_discount', |
| 192 |
'_wpfnl_downsell_products', |
| 193 |
'_wpfnl_downsell_discount', |
| 194 |
'global_funnel_upsell_rules', |
| 195 |
'global_funnel_downsell_rules', |
| 196 |
], |
| 197 |
Wpfnl_functions::VOLATILE_TRANSFER_META_KEYS |
| 198 |
); |
| 199 |
|
| 200 |
|
| 201 |
$all_meta = $this->exclude_meta( $all_meta, $exclude_step_meta ); |
| 202 |
$post_step = get_post($step['id']); |
| 203 |
$step_slug = isset( $post_step->post_name) ? $post_step->post_name : ''; |
| 204 |
$step_data = array( |
| 205 |
'id' => $step['id'], |
| 206 |
'title' => get_the_title( $step['id'] ), |
| 207 |
'slug' => $step_slug, |
| 208 |
'type' => $step['step_type'], |
| 209 |
'post_content' => '', |
| 210 |
'page_template' => get_post_meta( $step['id'], '_wp_page_template', true), |
| 211 |
'meta' => $all_meta |
| 212 |
); |
| 213 |
if ( $is_export ) { |
| 214 |
$step_obj = get_post( $step['id'] ); |
| 215 |
$step_data['post_content'] = isset($step_obj->post_content) ? $step_obj->post_content : ''; |
| 216 |
} |
| 217 |
$ab_settings = get_post_meta( $step['id'], '_wpfnl_ab_testing_start_settings', true ); |
| 218 |
if( $ab_settings ){ |
| 219 |
if( isset( $ab_settings['variations'] ) && is_array( $ab_settings['variations'] ) ){ |
| 220 |
foreach( $ab_settings['variations'] as $index => $variations ){ |
| 221 |
if( isset( $variations['stepId'] ) ){ |
| 222 |
$ab_step_meta = $this->export_ab_step_metas( $variations['stepId'] ); |
| 223 |
$ab_step_obj = get_post( $variations['stepId'] ); |
| 224 |
$step_data['ab_step_data'][] = [ |
| 225 |
'step_id' => $variations['stepId'], |
| 226 |
'title' => get_the_title( $variations['stepId'] ), |
| 227 |
'type' => get_post_meta( $variations['stepId'], '_step_type', true), |
| 228 |
'post_content' => isset($ab_step_obj->post_content) ? $ab_step_obj->post_content : '', |
| 229 |
'page_template' => get_post_meta( $variations['stepId'], '_wp_page_template', true), |
| 230 |
'meta' => $ab_step_meta |
| 231 |
]; |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
$funnel_data['steps_data'][] = $step_data; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
return $funnel_data; |
| 241 |
} |
| 242 |
} |
| 243 |
return false; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
/** |
| 248 |
* Export A/B testing steps |
| 249 |
* |
| 250 |
* @param Int $step_id |
| 251 |
* |
| 252 |
* @return Array |
| 253 |
* @since 1.7.5 |
| 254 |
*/ |
| 255 |
public function export_ab_step_metas( $step_id ){ |
| 256 |
$step_data = []; |
| 257 |
if( $step_id ){ |
| 258 |
$is_export = apply_filters( 'wpfunnels/allow_to_export_ab_step', true ); |
| 259 |
if ( $is_export ) { |
| 260 |
$all_meta = get_post_meta( $step_id ); |
| 261 |
$all_meta = $this->exclude_meta( $all_meta, Wpfnl_functions::VOLATILE_TRANSFER_META_KEYS ); |
| 262 |
$step_data = array( |
| 263 |
'id' => $step_id, |
| 264 |
'title' => get_the_title( $step_id ), |
| 265 |
'type' => get_post_meta( $step_id, '_step_type', true), |
| 266 |
'post_content' => '', |
| 267 |
'page_template' => get_post_meta( $step_id, '_wp_page_template', true), |
| 268 |
'meta' => $all_meta |
| 269 |
); |
| 270 |
$step_obj = get_post( $step_id ); |
| 271 |
$step_data['post_content'] = isset($step_obj->post_content) ? $step_obj->post_content : ''; |
| 272 |
} |
| 273 |
} |
| 274 |
return $step_data; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
/** |
| 279 |
* Exclude meta |
| 280 |
* @param array $all_meta |
| 281 |
* @param array $meta_key Which meta should be excluded |
| 282 |
* |
| 283 |
*/ |
| 284 |
private function exclude_meta( $all_meta, $meta_keys ){ |
| 285 |
if( is_array( $meta_keys ) ){ |
| 286 |
foreach( $meta_keys as $meta_key ){ |
| 287 |
if( isset( $all_meta[$meta_key] ) ){ |
| 288 |
unset( $all_meta[$meta_key] ); |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
return $all_meta; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
public function get_validation_data() |
| 297 |
{ |
| 298 |
return [ |
| 299 |
'logged_in' => true, |
| 300 |
'user_can' => 'wpf_manage_funnels', |
| 301 |
]; |
| 302 |
} |
| 303 |
} |