| 1 |
<?php |
| 2 |
/** |
| 3 |
* Page builder compatibility |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! class_exists( 'Cartflows_Compatibility' ) ) { |
| 9 |
|
| 10 |
/** |
| 11 |
* Class for page builder compatibility |
| 12 |
*/ |
| 13 |
class Cartflows_Compatibility { |
| 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 |
* Constructor |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
|
| 37 |
$this->load_files(); |
| 38 |
|
| 39 |
// Override post meta. |
| 40 |
add_action( 'wp', array( $this, 'override_meta' ), 0 ); |
| 41 |
|
| 42 |
add_action( 'wp_enqueue_scripts', array( $this, 'load_fontawesome' ), 10000 ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Load page builder compatibility files |
| 47 |
*/ |
| 48 |
public function load_files() { |
| 49 |
if ( class_exists( '\Elementor\Plugin' ) ) { |
| 50 |
require_once CARTFLOWS_DIR . 'classes/class-cartflows-elementor-compatibility.php'; |
| 51 |
} |
| 52 |
|
| 53 |
if ( $this->is_divi_enabled() ) { |
| 54 |
require_once CARTFLOWS_DIR . 'classes/class-cartflows-divi-compatibility.php'; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Check if elementor preview mode is on. |
| 60 |
*/ |
| 61 |
public function is_elementor_preview_mode() { |
| 62 |
|
| 63 |
if ( class_exists( '\Elementor\Plugin' ) ) { |
| 64 |
|
| 65 |
if ( \Elementor\Plugin::$instance->preview->is_preview_mode() ) { |
| 66 |
return true; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get Current Theme. |
| 75 |
*/ |
| 76 |
public function get_current_theme() { |
| 77 |
|
| 78 |
$theme_name = ''; |
| 79 |
$theme = wp_get_theme(); |
| 80 |
|
| 81 |
if ( isset( $theme->parent_theme ) && '' != $theme->parent_theme || null != $theme->parent_theme ) { |
| 82 |
$theme_name = $theme->parent_theme; |
| 83 |
} else { |
| 84 |
$theme_name = $theme->name; |
| 85 |
} |
| 86 |
|
| 87 |
return $theme_name; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Check if it is beaver builder preview mode |
| 92 |
*/ |
| 93 |
public function is_bb_preview_mode() { |
| 94 |
|
| 95 |
if ( class_exists( 'FLBuilderModel' ) ) { |
| 96 |
if ( FLBuilderModel::is_builder_active() ) { |
| 97 |
return true; |
| 98 |
} else { |
| 99 |
return false; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Check for page builder preview mode. |
| 108 |
*/ |
| 109 |
public function is_page_builder_preview() { |
| 110 |
|
| 111 |
if ( $this->is_elementor_preview_mode() || $this->is_bb_preview_mode() ) { |
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Check if divi builder enabled for post id. |
| 120 |
* |
| 121 |
* @param int $post_id post id. |
| 122 |
*/ |
| 123 |
public function is_divi_builder_enabled( $post_id ) { |
| 124 |
|
| 125 |
if ( function_exists( 'et_pb_is_pagebuilder_used' ) && et_pb_is_pagebuilder_used( $post_id ) ) { |
| 126 |
return true; |
| 127 |
} |
| 128 |
|
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Check if compatibility theme enabled. |
| 134 |
*/ |
| 135 |
function is_compatibility_theme_enabled() { |
| 136 |
|
| 137 |
$theme = wp_get_theme(); |
| 138 |
|
| 139 |
if ( $this->is_divi_enabled( $theme ) || $this->is_flatsome_enabled( $theme ) || $this->is_oceanwp_enabled( $theme ) ) { |
| 140 |
|
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Check if divi builder enabled for post id. |
| 149 |
* |
| 150 |
* @param object $theme theme data. |
| 151 |
* @return boolean |
| 152 |
*/ |
| 153 |
function is_divi_enabled( $theme = false ) { |
| 154 |
|
| 155 |
if ( ! $theme ) { |
| 156 |
$theme = wp_get_theme(); |
| 157 |
} |
| 158 |
|
| 159 |
if ( 'Divi' == $theme->name || 'Divi' == $theme->parent_theme ) { |
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Check if Flatsome enabled for post id. |
| 168 |
* |
| 169 |
* @param object $theme theme data. |
| 170 |
* @return boolean |
| 171 |
*/ |
| 172 |
function is_flatsome_enabled( $theme = false ) { |
| 173 |
|
| 174 |
if ( ! $theme ) { |
| 175 |
$theme = wp_get_theme(); |
| 176 |
} |
| 177 |
|
| 178 |
if ( 'Flatsome' == $theme->name || 'Flatsome' == $theme->parent_theme ) { |
| 179 |
return true; |
| 180 |
} |
| 181 |
|
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Check if OceanWp enabled for post id. |
| 187 |
* |
| 188 |
* @param object $theme theme data. |
| 189 |
* @return boolean |
| 190 |
*/ |
| 191 |
function is_oceanwp_enabled( $theme = false ) { |
| 192 |
|
| 193 |
if ( ! $theme ) { |
| 194 |
$theme = wp_get_theme(); |
| 195 |
} |
| 196 |
|
| 197 |
if ( 'OceanWP' == $theme->name || 'OceanWP' == $theme->parent_theme ) { |
| 198 |
return true; |
| 199 |
} |
| 200 |
|
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Check for thrive architect edit page. |
| 206 |
* |
| 207 |
* @param int $post_id post id. |
| 208 |
*/ |
| 209 |
public function is_thrive_edit_page( $post_id ) { |
| 210 |
|
| 211 |
if ( true === $this->is_thrive_builder_page( $post_id ) ) { |
| 212 |
return true; |
| 213 |
} else { |
| 214 |
return false; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Check if the page being rendered is the main ID on the editor page. |
| 220 |
* |
| 221 |
* @since 1.0.0 |
| 222 |
* @param String $post_id Post ID which is to be rendered. |
| 223 |
* @return boolean True if current if is being rendered is not being edited. |
| 224 |
*/ |
| 225 |
private function is_thrive_builder_page( $post_id ) { |
| 226 |
$tve = ( isset( $_GET['tve'] ) && 'true' == $_GET['tve'] ) ? true : false; |
| 227 |
$post = isset( $_GET['post'] ) ? sanitize_text_field( $_GET['post'] ) : false; |
| 228 |
|
| 229 |
return ( true == $tve && $post_id !== $post ); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Overwrite meta for page |
| 234 |
*/ |
| 235 |
public function override_meta() { |
| 236 |
|
| 237 |
// don't override meta for `elementor_library` post type. |
| 238 |
if ( 'elementor_library' == get_post_type() ) { |
| 239 |
return; |
| 240 |
} |
| 241 |
|
| 242 |
if ( ! is_singular() ) { |
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
global $post; |
| 247 |
$post_id = $post->ID; |
| 248 |
$post_type = get_post_type(); |
| 249 |
|
| 250 |
if ( 'cartflows_step' == $post_type && ( $this->is_elementor_preview_mode() |
| 251 |
|| $this->is_bb_preview_mode() || $this->is_thrive_edit_page( $post_id ) |
| 252 |
|| $this->is_divi_builder_enabled( $post_id ) ) ) { |
| 253 |
|
| 254 |
if ( '' == $post->post_content ) { |
| 255 |
|
| 256 |
$this->overwrite_template( $post_id ); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Assign cartflow canvas template to page. |
| 263 |
* |
| 264 |
* @param int $post_id post ID. |
| 265 |
*/ |
| 266 |
public function overwrite_template( $post_id ) { |
| 267 |
|
| 268 |
$template = 'cartflows-canvas'; |
| 269 |
$key = '_wp_page_template'; |
| 270 |
|
| 271 |
$record_exists = get_post_meta( $post_id, $key, true ); |
| 272 |
|
| 273 |
if ( 'cartflows-canvas' == $record_exists ) { |
| 274 |
return; |
| 275 |
} |
| 276 |
|
| 277 |
// As elementor doesn't allow update post meta using update_post_meta, run wpdb query to update post meta. |
| 278 |
if ( class_exists( '\Elementor\Plugin' ) ) { |
| 279 |
|
| 280 |
global $wpdb; |
| 281 |
|
| 282 |
if ( '' == $record_exists || ! $record_exists ) { |
| 283 |
|
| 284 |
$wpdb->insert( |
| 285 |
$wpdb->prefix . 'postmeta', |
| 286 |
array( |
| 287 |
'post_id' => $post_id, |
| 288 |
'meta_key' => $key, |
| 289 |
'meta_value' => $template, // ... and so on |
| 290 |
) |
| 291 |
); |
| 292 |
} else { |
| 293 |
|
| 294 |
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = %s WHERE meta_key = %s AND post_id = %s;", $template, $key, $post_id ) ); |
| 295 |
} |
| 296 |
} else { |
| 297 |
|
| 298 |
update_post_meta( $post_id, $key, $template ); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Load font awesome style from oceanwp on checkout page. |
| 304 |
*/ |
| 305 |
public function load_fontawesome() { |
| 306 |
|
| 307 |
$theme = get_template(); |
| 308 |
|
| 309 |
if ( 'oceanwp' == strtolower( $theme ) && wcf()->utils->is_step_post_type() ) { |
| 310 |
|
| 311 |
$load_fa = apply_filters( 'cartflows_maybe_load_font_awesome', true ); |
| 312 |
|
| 313 |
if ( $load_fa ) { |
| 314 |
|
| 315 |
wp_enqueue_style( 'font-awesome', OCEANWP_CSS_DIR_URI . 'third/font-awesome.min.css', false ); |
| 316 |
} |
| 317 |
|
| 318 |
$custom_css = ' |
| 319 |
#oceanwp-cart-sidebar-wrap, |
| 320 |
#owp-qv-wrap{ |
| 321 |
display: none; |
| 322 |
}'; |
| 323 |
|
| 324 |
wp_add_inline_style( 'wcf-frontend-global', $custom_css ); |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
Cartflows_Compatibility::get_instance(); |
| 331 |
|
| 332 |
|