| 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 |
|
| 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 |
public function set_default_props() |
| 84 |
{ |
| 85 |
$this->set_keys(); |
| 86 |
foreach ($this->internal_keys as $meta_key => $value) { |
| 87 |
$this->update_meta($this->id, $meta_key, $value); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
/** |
| 93 |
* Set data |
| 94 |
* |
| 95 |
* @param \WP_Post $step |
| 96 |
*/ |
| 97 |
public function set_data(\WP_Post $step) |
| 98 |
{ |
| 99 |
$this->set_id($step->ID); |
| 100 |
$this->step_title = $step->post_title; |
| 101 |
$this->funnel_id = $this->get_meta($this->id, '_funnel_id'); |
| 102 |
$this->type = $this->get_meta($this->id, '_step_type'); |
| 103 |
$meta_keys = Wpfnl_Step_Meta_keys::get_meta_keys($this->type); |
| 104 |
foreach ($meta_keys as $meta_key => $value) { |
| 105 |
$this->internal_keys[$meta_key] = $this->get_meta($this->id, $meta_key); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
/** |
| 111 |
* Read the step and its meta data |
| 112 |
* from DB |
| 113 |
* |
| 114 |
* @param $id |
| 115 |
* |
| 116 |
* @since 1.0.0 |
| 117 |
*/ |
| 118 |
public function read($id) |
| 119 |
{ |
| 120 |
$step = get_post($id); |
| 121 |
if ($step) { |
| 122 |
$this->set_data($step); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* Delete step from DB |
| 129 |
* |
| 130 |
* @param $id |
| 131 |
* |
| 132 |
* @return bool|void |
| 133 |
*/ |
| 134 |
public function delete($id) |
| 135 |
{ |
| 136 |
wp_delete_post($id); |
| 137 |
return true; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
/** |
| 142 |
* Set step basic keys as meta on |
| 143 |
* DB for further use |
| 144 |
* |
| 145 |
* @since 1.0.0 |
| 146 |
*/ |
| 147 |
public function set_keys() |
| 148 |
{ |
| 149 |
$this->internal_keys['_step_type'] = $this->type; |
| 150 |
$this->internal_keys['_funnel_id'] = $this->funnel_id; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
/** |
| 155 |
* Get title of the step |
| 156 |
* |
| 157 |
* @return string |
| 158 |
* @since 1.0.0 |
| 159 |
*/ |
| 160 |
public function get_title() |
| 161 |
{ |
| 162 |
$title = $this->step_title; |
| 163 |
if ($title == '') { |
| 164 |
$title = $this->get_type(); |
| 165 |
} |
| 166 |
return $title; |
| 167 |
} |
| 168 |
|
| 169 |
public function get_funnel_id() |
| 170 |
{ |
| 171 |
return $this->funnel_id; |
| 172 |
} |
| 173 |
|
| 174 |
public function get_type() |
| 175 |
{ |
| 176 |
return $this->type; |
| 177 |
} |
| 178 |
|
| 179 |
public function get_next_step($order, $current) |
| 180 |
{ |
| 181 |
$current_key = array_search($current, array_column($order, 'id')); |
| 182 |
if (isset($order[$current_key + 1])) { |
| 183 |
$next_key = $order[$current_key + 1]; |
| 184 |
$next_id = $next_key['id']; |
| 185 |
return $next_id; |
| 186 |
} else { |
| 187 |
return false; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
/** |
| 193 |
* Get meta value by key |
| 194 |
* |
| 195 |
* @param $key |
| 196 |
* |
| 197 |
* @return mixed |
| 198 |
*/ |
| 199 |
public function get_internal_metas_by_key($key) |
| 200 |
{ |
| 201 |
if (isset($this->internal_keys[$key])) { |
| 202 |
return $this->internal_keys[$key]; |
| 203 |
} |
| 204 |
return null; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
/** |
| 209 |
* Import post metas |
| 210 |
* |
| 211 |
* @param $step_id |
| 212 |
* @param array $post_metas |
| 213 |
* |
| 214 |
* @since 1.0.0 |
| 215 |
*/ |
| 216 |
public function import_metas($step_id, $post_metas = []) |
| 217 |
{ |
| 218 |
foreach ($post_metas as $meta_key => $meta_value) { |
| 219 |
$meta_value = isset($meta_value[0]) ? $meta_value[0] : ''; |
| 220 |
if ($meta_value) { |
| 221 |
if (is_serialized($meta_value, true)) { |
| 222 |
$raw_data = maybe_unserialize(stripslashes($meta_value)); |
| 223 |
} elseif (is_array($meta_value)) { |
| 224 |
$raw_data = json_decode(stripslashes($meta_value), true); |
| 225 |
} else { |
| 226 |
$raw_data = $meta_value; |
| 227 |
} |
| 228 |
|
| 229 |
if ( '_elementor_data' === $meta_key ) { |
| 230 |
if (is_array($raw_data)) { |
| 231 |
$raw_data = wp_slash(wp_json_encode($raw_data)); |
| 232 |
} else { |
| 233 |
$raw_data = wp_slash($raw_data); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
if ($meta_key != 'order-bump-settings' && $meta_key != '_wpfnl_checkout_products' && $meta_key != 'order-bump') { |
| 238 |
$this->update_meta($step_id, $meta_key, $raw_data); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
$this->update_meta($step_id, '_is_imported', 'yes'); |
| 243 |
} |
| 244 |
} |
| 245 |
|