| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bricks theme compatibility |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
use Bricks\Database; |
| 14 |
|
| 15 |
if ( ! class_exists( 'Cartflows_Bricks_Compatibility' ) ) : |
| 16 |
|
| 17 |
/** |
| 18 |
* Class for Bricks theme compatibility |
| 19 |
*/ |
| 20 |
class Cartflows_Bricks_Compatibility { |
| 21 |
|
| 22 |
/** |
| 23 |
* Member Variable |
| 24 |
* |
| 25 |
* @var instance |
| 26 |
*/ |
| 27 |
private static $instance; |
| 28 |
|
| 29 |
/** |
| 30 |
* Initiator |
| 31 |
* |
| 32 |
* @since 1.5.7 |
| 33 |
*/ |
| 34 |
public static function get_instance() { |
| 35 |
if ( ! isset( self::$instance ) ) { |
| 36 |
self::$instance = new self(); |
| 37 |
} |
| 38 |
return self::$instance; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Constructor |
| 43 |
* |
| 44 |
* @since 1.5.7 |
| 45 |
*/ |
| 46 |
public function __construct() { |
| 47 |
|
| 48 |
add_filter( 'cartflows_remove_theme_styles', array( $this, 'wcf_load_theme_files' ), 10, 1 ); |
| 49 |
add_filter( 'cartflows_remove_theme_scripts', array( $this, 'wcf_load_theme_files' ), 10, 1 ); |
| 50 |
add_filter( 'cartflows_allow_to_change_page_template', array( $this, 'allow_to_change_page_template' ), 10 ); |
| 51 |
add_action( 'wp', array( $this, 'remove_bricks_render_header_footer' ) ); |
| 52 |
add_action( 'wp', array( $this, 'remove_bricks_editor_option_instant_checkout' ) ); |
| 53 |
add_action( 'wp_enqueue_scripts', array( $this, 'bricks_compatibility_external_css' ), 101 ); |
| 54 |
add_filter( 'option_bricks_global_settings', array( $this, 'add_step_post_type_to_supported_post_types' ) ); |
| 55 |
add_filter( 'wp_get_attachment_image_attributes', array( $this, 'disable_lazy_loading_for_cartflows_steps' ), 9, 3 ); |
| 56 |
add_action( 'admin_bar_menu', array( $this, 'remove_editor_links_from_admin_toolbar' ), 999 ); |
| 57 |
add_filter( 'cartflows_instant_checkout_header_logo', array( $this, 'fetch_header_logo_from_theme' ), 10, 2 ); |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Fetch the header logo from the theme |
| 63 |
* |
| 64 |
* @since 2.1.0 |
| 65 |
* @param string $logo The logo URL. |
| 66 |
* @param string $site_title The Site Title. |
| 67 |
* @return string $bricks_logo The modified logo HTML. |
| 68 |
*/ |
| 69 |
public function fetch_header_logo_from_theme( $logo, $site_title ) { |
| 70 |
|
| 71 |
$template_header_id = Database::$active_templates['header']; |
| 72 |
$template_data = $template_header_id ? Database::get_data( $template_header_id, 'header' ) : array(); |
| 73 |
|
| 74 |
$logo_element = array(); |
| 75 |
|
| 76 |
if ( is_array( $template_data ) && ! empty( $template_data ) ) { |
| 77 |
|
| 78 |
// Find logo element in header. |
| 79 |
$logo_element = array_filter( |
| 80 |
$template_data, |
| 81 |
function( $element ) { |
| 82 |
return isset( $element['name'] ) && 'logo' === $element['name']; |
| 83 |
} |
| 84 |
); |
| 85 |
|
| 86 |
$logo_element = reset( $logo_element ); |
| 87 |
} |
| 88 |
|
| 89 |
// Get logo settings if found. |
| 90 |
if ( ! empty( $logo_element ) && ! empty( $logo_element['settings'] ) ) { |
| 91 |
$logo_settings = $logo_element['settings']; |
| 92 |
|
| 93 |
$logo_id = ! empty( $logo_settings['logo']['id'] ) ? $logo_settings['logo']['id'] : ''; |
| 94 |
$logo_size = ! empty( $logo_settings['logo']['size'] ) ? $logo_settings['logo']['size'] : ''; |
| 95 |
|
| 96 |
$logo_width = ! empty( $logo_settings['logoWidth'] ) ? intval( $logo_settings['logoWidth'] ) : ''; |
| 97 |
$logo_height = ! empty( $logo_settings['logoHeight'] ) ? intval( $logo_settings['logoHeight'] ) : ''; |
| 98 |
$logo_text = ! empty( $logo_settings['logoText'] ) ? $logo_settings['logoText'] : ''; |
| 99 |
|
| 100 |
$logo_image_src = wp_get_attachment_image_src( $logo_id, $logo_size ); |
| 101 |
|
| 102 |
if ( ! empty( $logo_image_src[0] ) ) { |
| 103 |
$style = ''; |
| 104 |
|
| 105 |
if ( ! empty( $logo_width ) ) { |
| 106 |
$style .= 'width:' . intval( $logo_width ) . 'px;'; |
| 107 |
} |
| 108 |
|
| 109 |
if ( ! empty( $logo_height ) ) { |
| 110 |
$style .= 'height:' . intval( $logo_height ) . 'px;'; |
| 111 |
} |
| 112 |
|
| 113 |
$logo = '<img src="' . esc_url( $logo_image_src[0] ) . '" alt="' . esc_attr( ! empty( $logo_text ) ? $logo_text : $site_title ) . '" style="' . $style . '">'; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return $logo; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Disable the Bricks Builder's Lazy Loading feature on the CartFlows page. |
| 122 |
* |
| 123 |
* @param array $attr Image attributes. |
| 124 |
* @param object $attachment WP_POST object of image. |
| 125 |
* @param string|array $size Requested image size. |
| 126 |
* @return array $attr Modified Image Attributes. |
| 127 |
*/ |
| 128 |
public function disable_lazy_loading_for_cartflows_steps( $attr, $attachment, $size ) { |
| 129 |
|
| 130 |
if ( wcf()->utils->is_step_post_type() ) { |
| 131 |
$attr['_brx_disable_lazy_loading'] = 1; |
| 132 |
} |
| 133 |
|
| 134 |
return $attr; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Allow to load theme files for bricks. We need JS of bricks theme for editor. |
| 139 |
* |
| 140 |
* @param bool $remove_styles Whether to remove styles. Default false. |
| 141 |
* @return bool Returns false to prevent the loading of theme files. |
| 142 |
*/ |
| 143 |
public function wcf_load_theme_files( $remove_styles ) { |
| 144 |
$is_bricks_builder = self::bricks_editor_mode(); |
| 145 |
if ( $is_bricks_builder ) { |
| 146 |
$remove_styles = false; |
| 147 |
} |
| 148 |
return $remove_styles; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
/** |
| 153 |
* Allows or disallows changing the page template for bricks. |
| 154 |
* |
| 155 |
* @param bool $is_allow Whether to allow changing the page template. |
| 156 |
* @return bool Returns false to prevent changing the page template. |
| 157 |
*/ |
| 158 |
public function allow_to_change_page_template( $is_allow ) { |
| 159 |
$is_bricks_builder = self::bricks_editor_mode(); |
| 160 |
if ( $is_bricks_builder ) { |
| 161 |
$is_allow = false; |
| 162 |
} |
| 163 |
return $is_allow; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
/** |
| 168 |
* Removes the header rendering action from the Bricks theme. |
| 169 |
* |
| 170 |
* This function checks if the Bricks\Frontend class exists. If it does, it creates an instance of it. |
| 171 |
* It then checks if the 'render_header' action is hooked to the 'render_header' method of the Bricks\Frontend instance. |
| 172 |
* If the action is hooked, it removes the action. |
| 173 |
* |
| 174 |
* @return void |
| 175 |
*/ |
| 176 |
public function remove_bricks_render_header_footer() { |
| 177 |
$page_template = self::get_page_template(); |
| 178 |
if ( _wcf_supported_template( $page_template ) && class_exists( 'Bricks\Theme' ) ) { |
| 179 |
$theme_instance = Bricks\Theme::instance(); |
| 180 |
|
| 181 |
if ( method_exists( $theme_instance, 'init' ) ) { |
| 182 |
remove_action( 'render_header', array( $theme_instance->frontend, 'render_header' ) ); |
| 183 |
remove_action( 'render_footer', array( $theme_instance->frontend, 'render_footer' ) ); |
| 184 |
|
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Checks if the current CartFlows step is in Bricks editor mode. |
| 193 |
* |
| 194 |
* This function checks if the Bricks\Helpers class exists. If it does, it retrieves the ID of the current |
| 195 |
* CartFlows step and uses the Bricks\Helpers::get_editor_mode method to determine if the step is in Bricks editor mode. |
| 196 |
* |
| 197 |
* @return bool Returns Bricks editor mode. |
| 198 |
*/ |
| 199 |
public function bricks_editor_mode() { |
| 200 |
$editor_mode = false; |
| 201 |
if ( class_exists( 'Bricks\Helpers' ) ) { |
| 202 |
$post_id = _get_wcf_step_id(); |
| 203 |
$editor_mode = ( ( isset( $_GET['bricks'] ) && 'run' === $_GET['bricks'] ) || 'bricks' === Bricks\Helpers::get_editor_mode( $post_id ) ) ? true : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 204 |
} |
| 205 |
return $editor_mode; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Get page template of current step. |
| 210 |
* |
| 211 |
* @return string |
| 212 |
*/ |
| 213 |
public function get_page_template() { |
| 214 |
$page_template = get_post_meta( _get_wcf_step_id(), '_wp_page_template', true ); |
| 215 |
$page_template = apply_filters( 'cartflows_page_template', $page_template ); |
| 216 |
return $page_template; |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Load the CSS |
| 222 |
* |
| 223 |
* @since 1.5.7 |
| 224 |
* |
| 225 |
* @return void |
| 226 |
*/ |
| 227 |
public function bricks_compatibility_external_css() { |
| 228 |
|
| 229 |
wp_enqueue_style( 'wcf-checkout-bricks-compatibility', CARTFLOWS_URL . 'compatibilities/themes/bricks/css/bricks-compatibility.css', '', CARTFLOWS_VER ); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Add the support of CartFlows Post Type in Bricks Builder. |
| 234 |
* |
| 235 |
* This function updates the 'postTypes' array in $global_settings with specified post types, |
| 236 |
* ensuring CartFlows steps can be editable by Bricks. |
| 237 |
* |
| 238 |
* @since 2.1.0 |
| 239 |
* @param array $global_settings The global settings of the Bricks Builder. |
| 240 |
* @return array $global_settings The updated array with CartFlows step post type. |
| 241 |
*/ |
| 242 |
public function add_step_post_type_to_supported_post_types( $global_settings ) { |
| 243 |
|
| 244 |
// Initilize the empty if array is empty. |
| 245 |
if ( empty( $global_settings['postTypes'] ) ) { |
| 246 |
$global_settings['postTypes'] = array(); |
| 247 |
} |
| 248 |
|
| 249 |
// Add CartFlows step post type. |
| 250 |
$global_settings['postTypes'] = array_merge( |
| 251 |
$global_settings['postTypes'], |
| 252 |
array( |
| 253 |
'cartflows_step', |
| 254 |
) |
| 255 |
); |
| 256 |
|
| 257 |
// Return the updated post type array. |
| 258 |
return $global_settings; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
/** |
| 263 |
* Remove Bricks Editor option for Instant Checkout. |
| 264 |
* |
| 265 |
* @since 2.1.0 |
| 266 |
* @return void |
| 267 |
*/ |
| 268 |
public function remove_bricks_editor_option_instant_checkout() { |
| 269 |
$is_bricks_builder = self::bricks_editor_mode(); |
| 270 |
$step_id = _get_wcf_step_id(); |
| 271 |
if ( wcf()->utils->is_step_post_type() ) { |
| 272 |
if ( $is_bricks_builder && Cartflows_Helper::is_instant_layout_enabled() && ( _is_wcf_checkout_type() || _is_wcf_thankyou_type() ) ) { |
| 273 |
// Reason: The WordPress is kept in small caps as it's value is used by Bricks in a small caps. |
| 274 |
update_post_meta( $step_id, BRICKS_DB_EDITOR_MODE, 'wordpress' ); // phpcs:ignore WordPress.WP.CapitalPDangit.Misspelled |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Remove Bricks Editor links from admin toolbar. |
| 281 |
* |
| 282 |
* @since 2.1.0 |
| 283 |
* @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance. |
| 284 |
* @return void |
| 285 |
*/ |
| 286 |
public function remove_editor_links_from_admin_toolbar( $wp_admin_bar ) { |
| 287 |
if ( Cartflows_Helper::is_instant_layout_enabled() && ( _is_wcf_checkout_type() || _is_wcf_thankyou_type() ) ) { |
| 288 |
$wp_admin_bar->remove_node( 'editor_mode' ); |
| 289 |
$wp_admin_bar->remove_node( 'edit_with_bricks' ); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
} |
| 296 |
/** |
| 297 |
* Kicking this off by calling 'get_instance()' method |
| 298 |
*/ |
| 299 |
Cartflows_Bricks_Compatibility::get_instance(); |
| 300 |
|
| 301 |
endif; |
| 302 |
|