| 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 |
|
| 43 |
/** |
| 44 |
* Load page builder compatibility files |
| 45 |
*/ |
| 46 |
public function load_files() { |
| 47 |
if ( class_exists( '\Elementor\Plugin' ) ) { |
| 48 |
require_once CARTFLOWS_DIR . 'classes/class-cartflows-elementor-compatibility.php'; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Check if elementor preview mode is on. |
| 54 |
*/ |
| 55 |
public function is_elementor_preview_mode() { |
| 56 |
|
| 57 |
if ( class_exists( '\Elementor\Plugin' ) ) { |
| 58 |
|
| 59 |
if ( \Elementor\Plugin::$instance->preview->is_preview_mode() ) { |
| 60 |
return true; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Check if it is beaver builder preview mode |
| 69 |
*/ |
| 70 |
public function is_bb_preview_mode() { |
| 71 |
|
| 72 |
if ( class_exists( 'FLBuilderModel' ) ) { |
| 73 |
if ( FLBuilderModel::is_builder_active() ) { |
| 74 |
return true; |
| 75 |
} else { |
| 76 |
return false; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Check for page builder preview mode. |
| 85 |
*/ |
| 86 |
public function is_page_builder_preview() { |
| 87 |
|
| 88 |
if ( $this->is_elementor_preview_mode() || $this->is_bb_preview_mode() ) { |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Check if divi builder enabled for post id. |
| 97 |
* |
| 98 |
* @param int $post_id post id. |
| 99 |
*/ |
| 100 |
public function is_divi_builder_enabled( $post_id ) { |
| 101 |
|
| 102 |
if ( function_exists( 'et_pb_is_pagebuilder_used' ) && et_pb_is_pagebuilder_used( $post_id ) ) { |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Check if divi builder enabled for post id. |
| 111 |
*/ |
| 112 |
function is_divi_enabled() { |
| 113 |
$theme = wp_get_theme(); |
| 114 |
|
| 115 |
if ( 'Divi' == $theme->name || 'Divi' == $theme->parent_theme ) { |
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Check for thrive architect edit page. |
| 124 |
* |
| 125 |
* @param int $post_id post id. |
| 126 |
*/ |
| 127 |
public function is_thrive_edit_page( $post_id ) { |
| 128 |
|
| 129 |
if ( true === $this->is_thrive_builder_page( $post_id ) ) { |
| 130 |
return true; |
| 131 |
} else { |
| 132 |
return false; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Check if the page being rendered is the main ID on the editor page. |
| 138 |
* |
| 139 |
* @since 1.0.0 |
| 140 |
* @param String $post_id Post ID which is to be rendered. |
| 141 |
* @return boolean True if current if is being rendered is not being edited. |
| 142 |
*/ |
| 143 |
private function is_thrive_builder_page( $post_id ) { |
| 144 |
$tve = ( isset( $_GET['tve'] ) && 'true' == $_GET['tve'] ) ? true : false; |
| 145 |
$post = isset( $_GET['post'] ) ? sanitize_text_field( $_GET['post'] ) : false; |
| 146 |
|
| 147 |
return ( true == $tve && $post_id !== $post ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Overwrite meta for page |
| 152 |
*/ |
| 153 |
public function override_meta() { |
| 154 |
|
| 155 |
// don't override meta for `elementor_library` post type. |
| 156 |
if ( 'elementor_library' == get_post_type() ) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
if ( ! is_singular() ) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
global $post; |
| 165 |
$post_id = $post->ID; |
| 166 |
$post_type = get_post_type(); |
| 167 |
|
| 168 |
if ( 'cartflows_step' == $post_type && ( $this->is_elementor_preview_mode() |
| 169 |
|| $this->is_bb_preview_mode() || $this->is_thrive_edit_page( $post_id ) |
| 170 |
|| $this->is_divi_builder_enabled( $post_id ) ) ) { |
| 171 |
|
| 172 |
if ( '' == $post->post_content ) { |
| 173 |
|
| 174 |
$this->overwrite_template( $post_id ); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Assign cartflow canvas template to page. |
| 181 |
* |
| 182 |
* @param int $post_id post ID. |
| 183 |
*/ |
| 184 |
public function overwrite_template( $post_id ) { |
| 185 |
|
| 186 |
$template = 'cartflows-canvas'; |
| 187 |
$key = '_wp_page_template'; |
| 188 |
|
| 189 |
$record_exists = get_post_meta( $post_id, $key, true ); |
| 190 |
|
| 191 |
if ( 'cartflows-canvas' == $record_exists ) { |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
// As elementor doesn't allow update post meta using update_post_meta, run wpdb query to update post meta. |
| 196 |
if ( class_exists( '\Elementor\Plugin' ) ) { |
| 197 |
|
| 198 |
global $wpdb; |
| 199 |
|
| 200 |
if ( '' == $record_exists || ! $record_exists ) { |
| 201 |
|
| 202 |
$wpdb->insert( |
| 203 |
$wpdb->prefix . 'postmeta', |
| 204 |
array( |
| 205 |
'post_id' => $post_id, |
| 206 |
'meta_key' => $key, |
| 207 |
'meta_value' => $template, // ... and so on |
| 208 |
) |
| 209 |
); |
| 210 |
} else { |
| 211 |
|
| 212 |
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = %s WHERE meta_key = %s AND post_id = %s;", $template, $key, $post_id ) ); |
| 213 |
} |
| 214 |
} else { |
| 215 |
|
| 216 |
update_post_meta( $post_id, $key, $template ); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
Cartflows_Compatibility::get_instance(); |
| 224 |
|
| 225 |
|