| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bricks modules loader |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Class for Bricks theme modules |
| 15 |
*/ |
| 16 |
class Cartflows_Bricks_Elements_Loader { |
| 17 |
|
| 18 |
/** |
| 19 |
* Member Variable |
| 20 |
* |
| 21 |
* @var instance |
| 22 |
*/ |
| 23 |
private static $instance; |
| 24 |
|
| 25 |
/** |
| 26 |
* Initiator |
| 27 |
* |
| 28 |
* @return object instance |
| 29 |
*/ |
| 30 |
public static function get_instance() { |
| 31 |
if ( ! isset( self::$instance ) ) { |
| 32 |
self::$instance = new self(); |
| 33 |
} |
| 34 |
return self::$instance; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor |
| 39 |
*/ |
| 40 |
public function __construct() { |
| 41 |
// Register Elements. |
| 42 |
add_action( 'init', array( $this, 'register_elements' ), 99 ); |
| 43 |
add_action( 'wp', array( $this, 'load_bricks_styles' ) ); |
| 44 |
add_filter( 'woocommerce_locate_template', array( $this, 'override_bricks_woocommerce_template' ), 10, 3 ); |
| 45 |
add_action( 'bricks/frontend/after_render_data', array( $this, 'update_required_step_meta_data' ), 10, 2 ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Returns Script array. |
| 50 |
* |
| 51 |
* @return array() |
| 52 |
* @since 1.6.15 |
| 53 |
*/ |
| 54 |
public static function get_module_list() { |
| 55 |
|
| 56 |
$widget_list = array( |
| 57 |
'class-cartflows-bricks-checkout-form', |
| 58 |
'class-cartflows-bricks-optin-form', |
| 59 |
'class-cartflows-bricks-order-details-form', |
| 60 |
); |
| 61 |
|
| 62 |
return $widget_list; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Include Widgets files |
| 67 |
* |
| 68 |
* Load widgets files |
| 69 |
* |
| 70 |
* @since 2.1.0 |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public function register_elements() { |
| 74 |
// Bail early if Bricks is not available. |
| 75 |
if ( ! class_exists( '\Bricks\Elements' ) ) { |
| 76 |
return; |
| 77 |
} |
| 78 |
// Register Dynamic Content Tags. |
| 79 |
require_once CARTFLOWS_DIR . 'modules/bricks/class-cartflows-bricks-dynamic-data.php'; |
| 80 |
|
| 81 |
$element_files = $this->get_module_list(); |
| 82 |
foreach ( $element_files as $file ) { |
| 83 |
|
| 84 |
$file = CARTFLOWS_DIR . 'modules/bricks/elements/' . $file . '.php'; |
| 85 |
|
| 86 |
if ( file_exists( $file ) ) { |
| 87 |
\Bricks\Elements::register_element( $file ); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Load Bricks styles |
| 94 |
* |
| 95 |
* @since 2.1.0 |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
public function load_bricks_styles() { |
| 99 |
if ( wcf()->utils->is_step_post_type() && file_exists( CARTFLOWS_DIR . 'modules/bricks/widgets-css/frontend.css' ) ) { |
| 100 |
wp_enqueue_style( 'cartflows-bricks-style', CARTFLOWS_URL . 'modules/bricks/widgets-css/frontend.css', array(), CARTFLOWS_VER ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Override WooCommerce template |
| 106 |
* |
| 107 |
* @since 2.1.0 |
| 108 |
* @param string $template Template path. |
| 109 |
* @param string $template_name Template name. |
| 110 |
* @param string $template_path Template path. |
| 111 |
* @return string |
| 112 |
*/ |
| 113 |
public function override_bricks_woocommerce_template( $template, $template_name, $template_path ) { |
| 114 |
$plugin_path = CARTFLOWS_DIR . 'woocommerce/template/'; |
| 115 |
$file = $plugin_path . $template_name; |
| 116 |
if ( file_exists( $file ) ) { |
| 117 |
$template = $file; |
| 118 |
} |
| 119 |
|
| 120 |
return $template; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Before checkout shortcode actions. |
| 125 |
* |
| 126 |
* @since 2.1.0 |
| 127 |
* @param array $elements array elements data. |
| 128 |
* @param string $area string area. |
| 129 |
* @return void |
| 130 |
*/ |
| 131 |
public function update_required_step_meta_data( $elements, $area ) { |
| 132 |
$post_ID = get_the_ID(); |
| 133 |
|
| 134 |
if ( ! $post_ID ) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
$step_type = get_post_meta( $post_ID, 'wcf-step-type', true ); |
| 139 |
$block_data = ''; |
| 140 |
$meta_keys = array(); |
| 141 |
switch ( $step_type ) { |
| 142 |
|
| 143 |
case 'checkout': |
| 144 |
$block_data = $this->bricks_find_block_recursive( $elements ); |
| 145 |
$meta_keys = array( |
| 146 |
'layout' => 'wcf-checkout-layout', |
| 147 |
); |
| 148 |
break; |
| 149 |
|
| 150 |
case 'optin': |
| 151 |
break; |
| 152 |
|
| 153 |
default: |
| 154 |
} |
| 155 |
|
| 156 |
if ( is_array( $block_data ) && ! empty( $block_data ) ) { |
| 157 |
|
| 158 |
foreach ( $meta_keys as $key => $meta_key ) { |
| 159 |
|
| 160 |
if ( isset( $block_data['settings'][ $key ] ) ) { |
| 161 |
update_post_meta( $post_ID, $meta_key, $block_data['settings'][ $key ] ); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Get the bricks widget data. |
| 169 |
* |
| 170 |
* @param array $elements elements data. |
| 171 |
* @return bool|array |
| 172 |
*/ |
| 173 |
public function bricks_find_block_recursive( $elements ) { |
| 174 |
foreach ( $elements as $element ) { |
| 175 |
if ( ! empty( $element['settings'] ) ) { |
| 176 |
if ( isset( $element['name'] ) && 'bricks-checkout-form' === $element['name'] ) { |
| 177 |
return $element; |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
return false; |
| 182 |
} |
| 183 |
} |
| 184 |
/** |
| 185 |
* Kicking this off by calling 'get_instance()' method |
| 186 |
*/ |
| 187 |
Cartflows_Bricks_Elements_Loader::get_instance(); |
| 188 |
|