| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gutenberg widgets manager |
| 4 |
* |
| 5 |
* @package |
| 6 |
*/ |
| 7 |
namespace WPFunnels\Widgets\Gutenberg; |
| 8 |
|
| 9 |
use WPFunnels\Wpfnl; |
| 10 |
use WPFunnels\Wpfnl_functions; |
| 11 |
|
| 12 |
/** |
| 13 |
* BlockTypesController class. |
| 14 |
* |
| 15 |
* @since 5.0.0 |
| 16 |
* @internal |
| 17 |
*/ |
| 18 |
final class Manager { |
| 19 |
|
| 20 |
/** |
| 21 |
* Instance |
| 22 |
* |
| 23 |
* @since 1.0.0 |
| 24 |
* |
| 25 |
* @access private |
| 26 |
* @static |
| 27 |
* |
| 28 |
* @var Gutenberg_Block The single instance of the class. |
| 29 |
*/ |
| 30 |
private static $_instance = null; |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* Instance |
| 35 |
* |
| 36 |
* Ensures only one instance of the class is loaded or can be loaded. |
| 37 |
* |
| 38 |
* @return Gutenberg_Block An instance of the class. |
| 39 |
* @since 1.0.0 |
| 40 |
* |
| 41 |
* @access public |
| 42 |
* @static |
| 43 |
*/ |
| 44 |
public static function instance() |
| 45 |
{ |
| 46 |
|
| 47 |
if (is_null(self::$_instance)) { |
| 48 |
self::$_instance = new self(); |
| 49 |
} |
| 50 |
return self::$_instance; |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
public function __construct( ) { |
| 55 |
$this->init(); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Initialize class features. |
| 60 |
*/ |
| 61 |
protected function init() { |
| 62 |
add_action('enqueue_block_editor_assets', array( $this, 'wpfnl_checkout_block_editor_assets' )); |
| 63 |
add_action( 'init', array( $this, 'register_assets' ) ); |
| 64 |
add_action( 'init', array( $this, 'register_blocks' ) ); |
| 65 |
add_action( 'wp_footer', array( $this, 'wpfnl_inline_footer_scripts' ) ); |
| 66 |
add_action( 'wp_head', array( $this, 'add_block_inline_css' ), 100 ); |
| 67 |
add_filter( 'block_categories_all', array( $this, 'register_block_category' ), 10, 2 ); |
| 68 |
new Wpfnl_Gutenberg_Editor(); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Inline jquery for editor-end floating label checkout. |
| 73 |
*/ |
| 74 |
public function wpfnl_checkout_block_editor_assets() { |
| 75 |
wp_enqueue_script( 'wpfunnels-editor-script', plugins_url( 'block/wpfunnels-editor-script.js', __DIR__ ), ['jquery'], WPFNL_VERSION, true ); |
| 76 |
$this->enqueue_imported_template_inline_css(); |
| 77 |
|
| 78 |
$post_id = get_the_ID(); |
| 79 |
if ( ! $post_id && isset($_GET['post']) ) { |
| 80 |
$post_id = intval($_GET['post']); |
| 81 |
} |
| 82 |
|
| 83 |
if ( 'yes' === (get_post_meta($post_id, '_wpfnl_enhanced_phone_field_enabled', true) ? get_post_meta($post_id, '_wpfnl_enhanced_phone_field_enabled', true) : 'no') ) { |
| 84 |
wp_enqueue_style('intl-tel-input-style', WPFNL_URL . 'public/assets/phone/css/intlTelInput.css', array(), WPFNL_VERSION); |
| 85 |
wp_enqueue_script('intl-tel-input-script', WPFNL_URL . 'public/assets/phone/js/intlTelInput.min.js', array('jquery'), WPFNL_VERSION, true); |
| 86 |
wp_enqueue_script('intl-tel-input-utils', WPFNL_URL . 'public/assets/phone/js/utils.js', array('intl-tel-input-script'), WPFNL_VERSION, true); |
| 87 |
|
| 88 |
wp_localize_script( |
| 89 |
'wpfunnels-editor-script', |
| 90 |
'wpfnl_obj', |
| 91 |
[ |
| 92 |
'phone_utils_url' => WPFNL_URL . 'public/assets/phone/js/utils.js', |
| 93 |
'phone_help_text' => get_post_meta($post_id, '_wpfnl_phone_help_text', true) ? get_post_meta($post_id, '_wpfnl_phone_help_text', true) : '', |
| 94 |
] |
| 95 |
); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Enqueue imported Gutenberg template inline CSS in editor mode. |
| 101 |
*/ |
| 102 |
private function enqueue_imported_template_inline_css() { |
| 103 |
$post_id = get_the_ID(); |
| 104 |
|
| 105 |
if ( ! $post_id && isset( $_GET['post'] ) ) { |
| 106 |
$post_id = intval( $_GET['post'] ); |
| 107 |
} |
| 108 |
|
| 109 |
if ( ! $post_id ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
$custom_css = get_post_meta( $post_id, 'rex_gutenberg_css', true ); |
| 114 |
|
| 115 |
if ( ! $custom_css ) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
wp_register_style( 'rex-gutenberg-css', false ); |
| 120 |
wp_enqueue_style( 'rex-gutenberg-css' ); |
| 121 |
wp_add_inline_style( 'rex-gutenberg-css', $custom_css ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
/** |
| 126 |
* Register blocks, hooking up assets and render functions as needed. |
| 127 |
*/ |
| 128 |
public function register_blocks() { |
| 129 |
$block_types = $this->get_block_types(); |
| 130 |
|
| 131 |
foreach ( $block_types as $block_type ) { |
| 132 |
$block_type_class = __NAMESPACE__ . '\\BlockTypes\\' . $block_type; |
| 133 |
$block_type_instance = new $block_type_class(); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
/** |
| 139 |
* Get list of block types. |
| 140 |
* |
| 141 |
* @return array |
| 142 |
*/ |
| 143 |
protected function get_block_types() { |
| 144 |
$block_types = array( |
| 145 |
'NextStepButton', |
| 146 |
'CheckoutForm', |
| 147 |
'OrderDetails', |
| 148 |
'OptinForm', |
| 149 |
); |
| 150 |
|
| 151 |
// Add OfferButton only if Pro is not active |
| 152 |
if ( ! Wpfnl_functions::is_wpfnl_pro_activated() ) { |
| 153 |
$block_types[] = 'OfferButton'; |
| 154 |
} |
| 155 |
|
| 156 |
return apply_filters('wpfunnels/gutenberg_block_types', $block_types); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
/** |
| 161 |
* Register assets for gutenberg |
| 162 |
* |
| 163 |
* @since 2.0.3 |
| 164 |
*/ |
| 165 |
public function register_assets() { |
| 166 |
wp_register_style( 'wpfnl-blocks-editor-style', plugins_url( 'block/assets/dist/wpfnl-blocks-editor-style.css', __DIR__ ), [], '2.0.3', 'all' ); |
| 167 |
wp_register_style( 'wpfnl-blocks-style',plugins_url( 'block/assets/dist/wpfnl-blocks-style.css', __DIR__ ), [], '2.0.3', 'all' ); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
/** |
| 172 |
* Load Inline Footer Script |
| 173 |
* |
| 174 |
* @since 1.3.0 |
| 175 |
*/ |
| 176 |
public function wpfnl_inline_footer_scripts() { |
| 177 |
global $wp_query; |
| 178 |
$is_previewing= $wp_query->is_preview(); |
| 179 |
$can_edit= current_user_can( 'edit_posts' ); |
| 180 |
if($is_previewing || $can_edit){ |
| 181 |
?> |
| 182 |
<script> |
| 183 |
// Set Preview CSS |
| 184 |
document.addEventListener("DOMContentLoaded", function() { |
| 185 |
const cussrent_url = window.location.href; |
| 186 |
let cssInline = document.createElement('style'); |
| 187 |
cssInline.type = 'text/css'; |
| 188 |
cssInline.id = 'wpfnl-block-js-preview'; |
| 189 |
cssInline.innerHTML =JSON.parse( localStorage.getItem('qubelyCSS')); |
| 190 |
window.document.getElementsByTagName("head")[0].appendChild(cssInline); |
| 191 |
}) |
| 192 |
|
| 193 |
</script> |
| 194 |
<?php |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
/** |
| 200 |
* Check current post page open and css path exists |
| 201 |
* Then read the css file content from css path |
| 202 |
* Then add inline css to the header |
| 203 |
*/ |
| 204 |
public function add_block_inline_css() { |
| 205 |
$upload_dir = wp_get_upload_dir(); |
| 206 |
$upload_css_dir = trailingslashit( $upload_dir['basedir'] ); |
| 207 |
$post_id = get_the_ID(); |
| 208 |
if ( $post_id ) { |
| 209 |
$css_path = $upload_css_dir . "wpfunnels/css/wpfnl-css-{$post_id}.css"; |
| 210 |
$json_path = $upload_css_dir . "wpfunnels/css/wpfnl-json-{$post_id}.json"; |
| 211 |
|
| 212 |
if ( file_exists( $css_path ) ) { |
| 213 |
$blockCss = file_get_contents( $css_path ); |
| 214 |
echo '<style type="text/css">' . $blockCss . '</style>'; |
| 215 |
} else { |
| 216 |
echo '<style type="text/css">' . get_post_meta( get_the_ID(), '_wpfunnels_gb_css', true ) . '</style>'; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Register WPFunnels block category |
| 223 |
* |
| 224 |
* @param array $categories Block categories. |
| 225 |
* @param object $post Post object. |
| 226 |
* @return array Modified block categories. |
| 227 |
*/ |
| 228 |
public function register_block_category( $categories, $post ) { |
| 229 |
return array_merge( |
| 230 |
$categories, |
| 231 |
array( |
| 232 |
array( |
| 233 |
'slug' => 'wpfunnels', |
| 234 |
'title' => __( 'WPFunnels', 'wpfnl' ), |
| 235 |
'icon' => null, |
| 236 |
), |
| 237 |
) |
| 238 |
); |
| 239 |
} |
| 240 |
} |
| 241 |
|