| 1 |
<?php |
| 2 |
/** |
| 3 |
* Utils. |
| 4 |
* |
| 5 |
* @package CARTFLOWS |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Cartflows_Utils. |
| 14 |
*/ |
| 15 |
class Cartflows_Utils { |
| 16 |
|
| 17 |
/** |
| 18 |
* Member Variable |
| 19 |
* |
| 20 |
* @var instance |
| 21 |
*/ |
| 22 |
private static $instance; |
| 23 |
|
| 24 |
/** |
| 25 |
* Initiator |
| 26 |
*/ |
| 27 |
public static function get_instance() { |
| 28 |
if ( ! isset( self::$instance ) ) { |
| 29 |
self::$instance = new self; |
| 30 |
} |
| 31 |
return self::$instance; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Constructor |
| 36 |
*/ |
| 37 |
function __construct() { |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Get current post type |
| 42 |
* |
| 43 |
* @param string $post_type post type. |
| 44 |
* @return string |
| 45 |
*/ |
| 46 |
function current_post_type( $post_type = '' ) { |
| 47 |
|
| 48 |
if ( '' === $post_type ) { |
| 49 |
$post_type = get_post_type(); |
| 50 |
} |
| 51 |
|
| 52 |
return $post_type; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Check if post type is of step. |
| 57 |
* |
| 58 |
* @param string $post_type post type. |
| 59 |
* @return bool |
| 60 |
*/ |
| 61 |
function is_step_post_type( $post_type = '' ) { |
| 62 |
|
| 63 |
if ( $this->get_step_post_type() === $this->current_post_type( $post_type ) ) { |
| 64 |
|
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Check if post type is of flow. |
| 73 |
* |
| 74 |
* @param string $post_type post type. |
| 75 |
* @return bool |
| 76 |
*/ |
| 77 |
function is_flow_post_type( $post_type = '' ) { |
| 78 |
|
| 79 |
if ( $this->get_flow_post_type() === $this->current_post_type( $post_type ) ) { |
| 80 |
|
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get post type of step. |
| 89 |
* |
| 90 |
* @return string |
| 91 |
*/ |
| 92 |
function get_step_post_type() { |
| 93 |
|
| 94 |
return CARTFLOWS_STEP_POST_TYPE; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Get post type of flow. |
| 99 |
* |
| 100 |
* @return string |
| 101 |
*/ |
| 102 |
function get_flow_post_type() { |
| 103 |
|
| 104 |
return CARTFLOWS_FLOW_POST_TYPE; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Get flow id |
| 109 |
* |
| 110 |
* @return int |
| 111 |
*/ |
| 112 |
function get_flow_id() { |
| 113 |
|
| 114 |
global $post; |
| 115 |
|
| 116 |
return get_post_meta( $post->ID, 'wcf-flow-id', true ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Get flow id by step |
| 121 |
* |
| 122 |
* @param int $step_id step ID. |
| 123 |
* @return int |
| 124 |
*/ |
| 125 |
function get_flow_id_from_step_id( $step_id ) { |
| 126 |
|
| 127 |
return get_post_meta( $step_id, 'wcf-flow-id', true ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Get flow steps by id |
| 132 |
* |
| 133 |
* @param int $flow_id flow ID. |
| 134 |
* @return int |
| 135 |
*/ |
| 136 |
function get_flow_steps( $flow_id ) { |
| 137 |
|
| 138 |
$steps = get_post_meta( $flow_id, 'wcf-steps', true ); |
| 139 |
|
| 140 |
if ( is_array( $steps ) && ! empty( $steps ) ) { |
| 141 |
return $steps; |
| 142 |
} |
| 143 |
|
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Get template type of step |
| 149 |
* |
| 150 |
* @param int $step_id step ID. |
| 151 |
* @return int |
| 152 |
*/ |
| 153 |
function get_step_type( $step_id ) { |
| 154 |
|
| 155 |
return get_post_meta( $step_id, 'wcf-step-type', true ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Get next id for step |
| 160 |
* |
| 161 |
* @param int $flow_id flow ID. |
| 162 |
* @param int $step_id step ID. |
| 163 |
* @return bool |
| 164 |
*/ |
| 165 |
function get_next_step_id( $flow_id, $step_id ) { |
| 166 |
|
| 167 |
$steps = $this->get_flow_steps( $flow_id ); |
| 168 |
$step_id = intval( $step_id ); |
| 169 |
|
| 170 |
if ( ! $steps ) { |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
foreach ( $steps as $i => $step ) { |
| 175 |
|
| 176 |
if ( intval( $step['id'] ) === $step_id ) { |
| 177 |
|
| 178 |
$next_i = $i + 1; |
| 179 |
|
| 180 |
if ( isset( $steps[ $next_i ] ) ) { |
| 181 |
|
| 182 |
$navigation = $steps[ $next_i ]; |
| 183 |
|
| 184 |
return intval( $navigation['id'] ); |
| 185 |
} |
| 186 |
|
| 187 |
break; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Get next id for step |
| 196 |
* |
| 197 |
* @param int $order_id order ID. |
| 198 |
* @return int |
| 199 |
*/ |
| 200 |
function get_flow_id_from_order( $order_id ) { |
| 201 |
|
| 202 |
$flow_id = get_post_meta( $order_id, '_wcf_flow_id', true ); |
| 203 |
|
| 204 |
return intval( $flow_id ); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Get checkout id for order |
| 209 |
* |
| 210 |
* @param int $order_id order ID. |
| 211 |
* @return int |
| 212 |
*/ |
| 213 |
function get_checkout_id_from_order( $order_id ) { |
| 214 |
|
| 215 |
$checkout_id = get_post_meta( $order_id, '_wcf_checkout_id', true ); |
| 216 |
|
| 217 |
return intval( $checkout_id ); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* We are using this function mostly in ajax on checkout page |
| 222 |
* |
| 223 |
* @return bool |
| 224 |
*/ |
| 225 |
function get_checkout_id_from_post_data() { |
| 226 |
|
| 227 |
if ( isset( $_POST['_wcf_checkout_id'] ) ) { |
| 228 |
|
| 229 |
$checkout_id = filter_var( $_POST['_wcf_checkout_id'], FILTER_SANITIZE_NUMBER_INT ); |
| 230 |
|
| 231 |
return intval( $checkout_id ); |
| 232 |
} |
| 233 |
|
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* We are using this function mostly in ajax on checkout page |
| 239 |
* |
| 240 |
* @return bool |
| 241 |
*/ |
| 242 |
function get_flow_id_from_post_data() { |
| 243 |
|
| 244 |
if ( isset( $_POST['_wcf_flow_id'] ) ) { |
| 245 |
|
| 246 |
$flow_id = filter_var( $_POST['_wcf_flow_id'], FILTER_SANITIZE_NUMBER_INT ); |
| 247 |
|
| 248 |
return intval( $flow_id ); |
| 249 |
} |
| 250 |
|
| 251 |
return false; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Check for thank you page |
| 256 |
* |
| 257 |
* @param int $step_id step ID. |
| 258 |
* @return bool |
| 259 |
*/ |
| 260 |
function check_is_thankyou_page( $step_id ) { |
| 261 |
|
| 262 |
$step_type = $this->get_step_type( $step_id ); |
| 263 |
|
| 264 |
if ( 'thankyou' === $step_type ) { |
| 265 |
|
| 266 |
return true; |
| 267 |
} |
| 268 |
|
| 269 |
return false; |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Check for offer page |
| 274 |
* |
| 275 |
* @param int $step_id step ID. |
| 276 |
* @return bool |
| 277 |
*/ |
| 278 |
function check_is_offer_page( $step_id ) { |
| 279 |
|
| 280 |
$step_type = $this->get_step_type( $step_id ); |
| 281 |
|
| 282 |
if ( 'upsell' === $step_type || 'downsell' === $step_type ) { |
| 283 |
|
| 284 |
return true; |
| 285 |
} |
| 286 |
|
| 287 |
return false; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Define constant for cache |
| 292 |
* |
| 293 |
* @return void |
| 294 |
*/ |
| 295 |
function do_not_cache() { |
| 296 |
|
| 297 |
wc_maybe_define_constant( 'DONOTCACHEPAGE', true ); |
| 298 |
wc_maybe_define_constant( 'DONOTCACHEOBJECT', true ); |
| 299 |
wc_maybe_define_constant( 'DONOTCACHEDB', true ); |
| 300 |
|
| 301 |
nocache_headers(); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Get linking url |
| 306 |
* |
| 307 |
* @param array $args query args. |
| 308 |
* @return string |
| 309 |
*/ |
| 310 |
function get_linking_url( $args = array() ) { |
| 311 |
|
| 312 |
$url = get_home_url(); |
| 313 |
|
| 314 |
$url = add_query_arg( $args, $url ); |
| 315 |
|
| 316 |
return $url; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Get a specific property of an array without needing to check if that property exists. |
| 322 |
* |
| 323 |
* Provide a default value if you want to return a specific value if the property is not set. |
| 324 |
* |
| 325 |
* @param array $array Array from which the property's value should be retrieved. |
| 326 |
* @param string $prop Name of the property to be retrieved. |
| 327 |
* @param string $default Optional. Value that should be returned if the property is not set or empty. Defaults to null. |
| 328 |
* |
| 329 |
* @return null|string|mixed The value |
| 330 |
*/ |
| 331 |
function wcf_get_prop( $array, $prop, $default = null ) { |
| 332 |
|
| 333 |
if ( ! is_array( $array ) && ! ( is_object( $array ) && $array instanceof ArrayAccess ) ) { |
| 334 |
return $default; |
| 335 |
} |
| 336 |
|
| 337 |
if ( isset( $array[ $prop ] ) ) { |
| 338 |
$value = $array[ $prop ]; |
| 339 |
} else { |
| 340 |
$value = ''; |
| 341 |
} |
| 342 |
|
| 343 |
return empty( $value ) && null !== $default ? $default : $value; |
| 344 |
} |
| 345 |
|