| 1 |
<?php |
| 2 |
/** |
| 3 |
* Widgets loader for Cartflows. |
| 4 |
* |
| 5 |
* @package Cartflows |
| 6 |
* */ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Set up Widgets Loader class |
| 12 |
*/ |
| 13 |
class Cartflows_Widgets_Loader { |
| 14 |
|
| 15 |
/** |
| 16 |
* Member Variable |
| 17 |
* |
| 18 |
* @var object instance |
| 19 |
*/ |
| 20 |
private static $instance; |
| 21 |
|
| 22 |
/** |
| 23 |
* Initiator |
| 24 |
*/ |
| 25 |
public static function get_instance() { |
| 26 |
if ( ! isset( self::$instance ) ) { |
| 27 |
self::$instance = new self(); |
| 28 |
} |
| 29 |
return self::$instance; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Setup actions and filters. |
| 34 |
* |
| 35 |
* @since 1.6.15 |
| 36 |
*/ |
| 37 |
private function __construct() { |
| 38 |
|
| 39 |
// Register category. |
| 40 |
add_action( 'elementor/elements/categories_registered', array( $this, 'register_widget_category' ) ); |
| 41 |
|
| 42 |
// Remove this in CartFlows major update and keep only `register`. |
| 43 |
$action_name = ( true === version_compare( ELEMENTOR_VERSION, '3.5.0', '>=' ) ) ? 'register' : 'widgets_registered'; |
| 44 |
|
| 45 |
// Register widgets. |
| 46 |
add_action( 'elementor/widgets/' . $action_name, array( $this, 'register_widgets' ) ); |
| 47 |
|
| 48 |
add_action( 'elementor/editor/after_enqueue_styles', array( $this, 'admin_enqueue_styles' ) ); |
| 49 |
|
| 50 |
add_action( 'wp_enqueue_scripts', array( $this, 'override_flow_level_global_colors_elementor' ), 25 ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Override the global colors of every steps inside the funnel for elementor |
| 55 |
* with the global colors selected in the funnel's setting by the help of elementor's global color. |
| 56 |
* |
| 57 |
* Note: Currently the GCP support is added for Elementor and Block Builder. |
| 58 |
* |
| 59 |
* @since 2.0.0 |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function override_flow_level_global_colors_elementor() { |
| 63 |
|
| 64 |
if ( wcf()->utils->is_step_post_type() ) { |
| 65 |
|
| 66 |
$flow_id = wcf()->utils->get_flow_id(); |
| 67 |
|
| 68 |
// Return if no flow ID is found. |
| 69 |
if ( empty( $flow_id ) ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
if ( ! Cartflows_Helper::is_gcp_styling_enabled( (int) $flow_id ) ) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
$style = ''; |
| 78 |
$page_builder = Cartflows_Helper::get_common_setting( 'default_page_builder' ); |
| 79 |
|
| 80 |
// Override the CSS variables of selected page builder to override the page colors. |
| 81 |
if ( 'elementor' === $page_builder ) { |
| 82 |
|
| 83 |
$style .= '--e-global-color-primary: var( --wcf-gcp-primary-color ) !important;'; |
| 84 |
|
| 85 |
$style .= '--e-global-color-secondary: var( --wcf-gcp-secondary-color ) !important;'; |
| 86 |
|
| 87 |
$style .= '--e-global-color-accent: var( --wcf-gcp-accent-color ) !important;'; |
| 88 |
|
| 89 |
$style .= '--e-global-color-text: var( --wcf-gcp-text-color ) !important;'; |
| 90 |
// $style .= '--e-global-typography-primary-font-family: ' . $gcp_primary_font_family . ' !important; --e-global-typography-text-font-family: ' . $gcp_primary_font_family . ' !important;'; |
| 91 |
} |
| 92 |
|
| 93 |
// Don't print the inline CSS style if the no style is generated. |
| 94 |
if ( ! empty( $style ) ) { |
| 95 |
$output = 'body.cartflows_step-template { ' . $style . ' }'; |
| 96 |
wp_add_inline_style( 'wcf-frontend-global', $output ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Enqueue admin scripts |
| 104 |
* |
| 105 |
* @since 1.6.15 |
| 106 |
* @param string $hook Current page hook. |
| 107 |
* @access public |
| 108 |
*/ |
| 109 |
public function admin_enqueue_styles( $hook ) { |
| 110 |
|
| 111 |
// Register the icons styles. |
| 112 |
wp_register_style( |
| 113 |
'cartflows-elementor-icons-style', |
| 114 |
CARTFLOWS_URL . 'assets/elementor-assets/css/style.css', |
| 115 |
array(), |
| 116 |
CARTFLOWS_VER |
| 117 |
); |
| 118 |
|
| 119 |
wp_enqueue_style( 'cartflows-elementor-icons-style' ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Returns Script array. |
| 124 |
* |
| 125 |
* @return array() |
| 126 |
* @since 1.6.15 |
| 127 |
*/ |
| 128 |
public static function get_widget_list() { |
| 129 |
|
| 130 |
$widget_list = array( |
| 131 |
'checkout-form', |
| 132 |
'order-details-form', |
| 133 |
'next-step-button', |
| 134 |
'optin-form', |
| 135 |
); |
| 136 |
|
| 137 |
return $widget_list; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
/** |
| 142 |
* Include Widgets files |
| 143 |
* |
| 144 |
* Load widgets files |
| 145 |
* |
| 146 |
* @since 1.6.15 |
| 147 |
* @access public |
| 148 |
*/ |
| 149 |
public function include_widgets_files() { |
| 150 |
|
| 151 |
/* Required files */ |
| 152 |
require_once CARTFLOWS_DIR . 'modules/elementor/classes/class-cartflows-elementor-editor.php'; |
| 153 |
|
| 154 |
$widget_list = $this->get_widget_list(); |
| 155 |
|
| 156 |
if ( ! empty( $widget_list ) ) { |
| 157 |
foreach ( $widget_list as $handle => $data ) { |
| 158 |
$file_path = CARTFLOWS_DIR . 'modules/elementor/widgets/class-cartflows-el-' . $data . '.php'; |
| 159 |
if ( file_exists( $file_path ) ) { |
| 160 |
require_once $file_path; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
// Emqueue the widgets style. |
| 166 |
wp_enqueue_style( 'cartflows-elementor-style', CARTFLOWS_URL . 'modules/elementor/widgets-css/frontend.css', array(), CARTFLOWS_VER ); |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Register Category |
| 172 |
* |
| 173 |
* @since 1.6.15 |
| 174 |
* @param object $this_cat class. |
| 175 |
*/ |
| 176 |
public function register_widget_category( $this_cat ) { |
| 177 |
$category = __( 'Cartflows', 'cartflows' ); |
| 178 |
|
| 179 |
$this_cat->add_category( |
| 180 |
'cartflows-widgets', |
| 181 |
array( |
| 182 |
'title' => $category, |
| 183 |
'icon' => 'eicon-font', |
| 184 |
) |
| 185 |
); |
| 186 |
|
| 187 |
return $this_cat; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Register Widgets |
| 192 |
* |
| 193 |
* Register new Elementor widgets. |
| 194 |
* |
| 195 |
* @since 1.6.15 |
| 196 |
* @access public |
| 197 |
*/ |
| 198 |
public function register_widgets() { |
| 199 |
|
| 200 |
global $post; |
| 201 |
|
| 202 |
if ( ! isset( $post ) ) { |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
$post_type = $post->post_type; |
| 207 |
|
| 208 |
$step_type = get_post_meta( $post->ID, 'wcf-step-type', true ); |
| 209 |
|
| 210 |
if ( 'cartflows_step' === $post_type && class_exists( '\Elementor\Plugin' ) ) { |
| 211 |
|
| 212 |
$widget_manager = \Elementor\Plugin::$instance->widgets_manager; |
| 213 |
|
| 214 |
$widget_list = $this->get_widget_list(); |
| 215 |
|
| 216 |
// Its is now safe to include Widgets files. |
| 217 |
$this->include_widgets_files(); |
| 218 |
|
| 219 |
$fn_name = ( true === version_compare( ELEMENTOR_VERSION, '3.5.0', '>=' ) ) ? 'register' : 'register_widget_type'; |
| 220 |
|
| 221 |
foreach ( $widget_list as $widget ) { |
| 222 |
|
| 223 |
$widget_name = str_replace( '-', ' ', $widget ); |
| 224 |
|
| 225 |
$class_name = 'Cartflows_' . str_replace( ' ', '_', ucwords( $widget_name ) ); |
| 226 |
|
| 227 |
if ( $class_name::is_enable( $step_type ) ) { |
| 228 |
$widget_manager->{ $fn_name }( new $class_name() ); |
| 229 |
|
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Initiate the class. |
| 239 |
*/ |
| 240 |
Cartflows_Widgets_Loader::get_instance(); |
| 241 |
|