| 1 |
<?php |
| 2 |
/** |
| 3 |
* Store step data |
| 4 |
* |
| 5 |
* @package |
| 6 |
*/ |
| 7 |
namespace WPFunnels\Data_Store; |
| 8 |
|
| 9 |
use WPFunnels\Metas\Wpfnl_Step_Meta_keys; |
| 10 |
use WPFunnels\Wpfnl; |
| 11 |
use WPFunnels\Wpfnl_functions; |
| 12 |
|
| 13 |
class Wpfnl_Steps_Store_Data extends Wpfnl_Abstract_Store_data implements Wpfnl_Data_Store |
| 14 |
{ |
| 15 |
protected $id; |
| 16 |
|
| 17 |
protected $internal_keys = []; |
| 18 |
|
| 19 |
protected $funnel_id; |
| 20 |
|
| 21 |
protected $type; |
| 22 |
|
| 23 |
protected $step_title; |
| 24 |
|
| 25 |
protected $meta_values; |
| 26 |
|
| 27 |
|
| 28 |
public function create() |
| 29 |
{ |
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
/** |
| 35 |
* Create individual steps |
| 36 |
* |
| 37 |
* @param $funnel_id |
| 38 |
* @param string $title |
| 39 |
* @param string $type |
| 40 |
* @param string $post_content |
| 41 |
* @param bool $clone |
| 42 |
* |
| 43 |
* @return int|\WP_Error |
| 44 |
*/ |
| 45 |
public function create_step($funnel_id, $title = 'Landing', $type = 'landing', $post_content = '', $clone = false) |
| 46 |
{ |
| 47 |
|
| 48 |
$step_id = wp_insert_post( |
| 49 |
apply_filters( |
| 50 |
'wpfunnels/wpfunnels_new_step_params', |
| 51 |
[ |
| 52 |
'post_type' => WPFNL_STEPS_POST_TYPE, |
| 53 |
'post_title' => wp_strip_all_tags($title), |
| 54 |
'post_content' => $post_content, |
| 55 |
'post_status' => 'publish', |
| 56 |
] |
| 57 |
), |
| 58 |
true |
| 59 |
); |
| 60 |
if ($type == 'checkout' && !$clone) { |
| 61 |
$title = 'funnel-'.$title; |
| 62 |
wp_update_post( |
| 63 |
array ( |
| 64 |
'ID' => $step_id, |
| 65 |
'post_name' => sanitize_title($title) |
| 66 |
) |
| 67 |
); |
| 68 |
} |
| 69 |
if ($step_id && !is_wp_error($step_id)) { |
| 70 |
$this->funnel_id = $funnel_id; |
| 71 |
$this->type = $type; |
| 72 |
$this->set_id($step_id); |
| 73 |
$this->set_default_props(); |
| 74 |
$this->maybe_mark_step_as_divi_built($step_id, $post_content); |
| 75 |
} |
| 76 |
|
| 77 |
do_action('wpfunnels_after_step_creation'); |
| 78 |
update_post_meta($step_id, '_wp_page_template', 'wpfunnels_default'); |
| 79 |
return $step_id; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* Flag a blank Divi step as builder-enabled so Divi offers its editor toggle. |
| 85 |
* |
| 86 |
* Divi's block editor integration only injects the "Edit With The Divi Builder" |
| 87 |
* placeholder when et_pb_is_pagebuilder_used() is true, or when the post is a |
| 88 |
* brand new unsaved one. A step is wp_insert_post()'d and published before the |
| 89 |
* user ever opens it, so it is never "new" by the time Divi looks — without |
| 90 |
* this meta a scratch-built step opens in Gutenberg with no way to reach Divi. |
| 91 |
* Imported steps work today only because the template meta they copy already |
| 92 |
* carries _et_pb_use_builder. |
| 93 |
* |
| 94 |
* Only blank steps are touched: import and clone pass content through here too, |
| 95 |
* and those carry their own (possibly non-Divi) builder meta. |
| 96 |
* |
| 97 |
* @param int $step_id Newly created step id. |
| 98 |
* @param string $post_content Content the step was created with. |
| 99 |
* |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
private function maybe_mark_step_as_divi_built($step_id, $post_content) |
| 103 |
{ |
| 104 |
if ('' !== trim((string) $post_content)) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
if ('divi-builder' !== Wpfnl_functions::get_builder_type()) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
update_post_meta($step_id, '_et_pb_use_builder', 'on'); |
| 113 |
// Divi stores the post type the layout was built for; steps mirror pages, |
| 114 |
// which is what imported Divi templates carry. |
| 115 |
update_post_meta($step_id, '_et_pb_built_for_post_type', 'page'); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
public function set_default_props() |
| 120 |
{ |
| 121 |
$this->set_keys(); |
| 122 |
foreach ($this->internal_keys as $meta_key => $value) { |
| 123 |
$this->update_meta($this->id, $meta_key, $value); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
/** |
| 129 |
* Set data |
| 130 |
* |
| 131 |
* @param \WP_Post $step |
| 132 |
*/ |
| 133 |
public function set_data(\WP_Post $step) |
| 134 |
{ |
| 135 |
$this->set_id($step->ID); |
| 136 |
$this->step_title = $step->post_title; |
| 137 |
$this->funnel_id = $this->get_meta($this->id, '_funnel_id'); |
| 138 |
$this->type = $this->get_meta($this->id, '_step_type'); |
| 139 |
$meta_keys = Wpfnl_Step_Meta_keys::get_meta_keys($this->type); |
| 140 |
foreach ($meta_keys as $meta_key => $value) { |
| 141 |
$this->internal_keys[$meta_key] = $this->get_meta($this->id, $meta_key); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
/** |
| 147 |
* Read the step and its meta data |
| 148 |
* from DB |
| 149 |
* |
| 150 |
* @param $id |
| 151 |
* |
| 152 |
* @since 1.0.0 |
| 153 |
*/ |
| 154 |
public function read($id) |
| 155 |
{ |
| 156 |
$step = get_post($id); |
| 157 |
if ($step) { |
| 158 |
$this->set_data($step); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
/** |
| 164 |
* Delete step from DB |
| 165 |
* |
| 166 |
* @param $id |
| 167 |
* |
| 168 |
* @return bool|void |
| 169 |
*/ |
| 170 |
public function delete($id) |
| 171 |
{ |
| 172 |
wp_delete_post($id); |
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
/** |
| 178 |
* Set step basic keys as meta on |
| 179 |
* DB for further use |
| 180 |
* |
| 181 |
* @since 1.0.0 |
| 182 |
*/ |
| 183 |
public function set_keys() |
| 184 |
{ |
| 185 |
$this->internal_keys['_step_type'] = $this->type; |
| 186 |
$this->internal_keys['_funnel_id'] = $this->funnel_id; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
/** |
| 191 |
* Get title of the step |
| 192 |
* |
| 193 |
* @return string |
| 194 |
* @since 1.0.0 |
| 195 |
*/ |
| 196 |
public function get_title() |
| 197 |
{ |
| 198 |
$title = $this->step_title; |
| 199 |
if ($title == '') { |
| 200 |
$title = $this->get_type(); |
| 201 |
} |
| 202 |
return $title; |
| 203 |
} |
| 204 |
|
| 205 |
public function get_funnel_id() |
| 206 |
{ |
| 207 |
return $this->funnel_id; |
| 208 |
} |
| 209 |
|
| 210 |
public function get_type() |
| 211 |
{ |
| 212 |
return $this->type; |
| 213 |
} |
| 214 |
|
| 215 |
public function get_next_step($order, $current) |
| 216 |
{ |
| 217 |
$current_key = array_search($current, array_column($order, 'id')); |
| 218 |
if (isset($order[$current_key + 1])) { |
| 219 |
$next_key = $order[$current_key + 1]; |
| 220 |
$next_id = $next_key['id']; |
| 221 |
return $next_id; |
| 222 |
} else { |
| 223 |
return false; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
/** |
| 229 |
* Get meta value by key |
| 230 |
* |
| 231 |
* @param $key |
| 232 |
* |
| 233 |
* @return mixed |
| 234 |
*/ |
| 235 |
public function get_internal_metas_by_key($key) |
| 236 |
{ |
| 237 |
if (isset($this->internal_keys[$key])) { |
| 238 |
return $this->internal_keys[$key]; |
| 239 |
} |
| 240 |
return null; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
/** |
| 245 |
* Import post metas |
| 246 |
* |
| 247 |
* @param $step_id |
| 248 |
* @param array $post_metas |
| 249 |
* |
| 250 |
* @since 1.0.0 |
| 251 |
*/ |
| 252 |
public function import_metas($step_id, $post_metas = [], $isSingleStep = false ) |
| 253 |
{ |
| 254 |
if (!$post_metas) { |
| 255 |
return; |
| 256 |
} |
| 257 |
foreach ($post_metas as $meta_key => $meta_value) { |
| 258 |
$meta_value = isset($meta_value[0]) ? $meta_value[0] : ''; |
| 259 |
if ($meta_value) { |
| 260 |
if (is_serialized($meta_value, true)) { |
| 261 |
$raw_data = maybe_unserialize(stripslashes($meta_value)); |
| 262 |
} elseif (is_array($meta_value)) { |
| 263 |
$raw_data = json_decode(stripslashes($meta_value), true); |
| 264 |
} else { |
| 265 |
$raw_data = $meta_value; |
| 266 |
} |
| 267 |
|
| 268 |
if ( '_elementor_data' === $meta_key ) { |
| 269 |
if (is_array($raw_data)) { |
| 270 |
$raw_data = wp_slash(wp_json_encode($raw_data)); |
| 271 |
} else { |
| 272 |
$raw_data = wp_slash($raw_data); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
if ( ($meta_key != 'order-bump-settings' && $meta_key != '_wpfnl_checkout_products' && $meta_key != 'order-bump' && '_wpfnl_ab_testing_start_settings' !== $meta_key && '_wpfnl_next_step_after_condition' !== $meta_key )) { |
| 277 |
$single_step_condition_keys = array( '_wpfnl_maybe_enable_condition', '_wpfnl_step_conditions' ); |
| 278 |
|
| 279 |
// Condition settings are a Pro feature — skip them for free installs. |
| 280 |
$is_pro_active = apply_filters( 'wpfunnels/is_pro_license_activated', false ); |
| 281 |
if ( ! $is_pro_active && in_array( $meta_key, $single_step_condition_keys ) ) { |
| 282 |
continue; |
| 283 |
} |
| 284 |
|
| 285 |
if( !$isSingleStep || !in_array( $meta_key, $single_step_condition_keys ) ){ |
| 286 |
$this->update_meta($step_id, $meta_key, $raw_data); |
| 287 |
} |
| 288 |
|
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
$this->update_meta($step_id, '_is_imported', 'yes'); |
| 293 |
} |
| 294 |
} |
| 295 |
|