| 1 |
<?php |
| 2 |
/** |
| 3 |
* Divi Builder Integration for Smart Post Show. |
| 4 |
* |
| 5 |
* @link https://shapedplugin.com/ |
| 6 |
* |
| 7 |
* @package Smart_Post_Show |
| 8 |
* @author ShapedPlugin <[email protected]> |
| 9 |
*/ |
| 10 |
|
| 11 |
use SmartPostShow\Blocks\Helper; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly. |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Smart Post Show Divi Module. |
| 19 |
* |
| 20 |
* This module allows selecting a template via a Select field |
| 21 |
* and renders the corresponding shortcode output. |
| 22 |
* |
| 23 |
* @since 4.0.0 |
| 24 |
*/ |
| 25 |
|
| 26 |
add_action( 'et_builder_ready', 'smart_post_template_divi_modules' ); |
| 27 |
|
| 28 |
/** |
| 29 |
* Register the Smart Post Template module with Divi Builder. |
| 30 |
* |
| 31 |
* This function registers the custom module once Divi |
| 32 |
* Builder has fully loaded. |
| 33 |
* |
| 34 |
* @since 4.0.0 |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
function smart_post_template_divi_modules() { |
| 38 |
|
| 39 |
if ( ! class_exists( 'ET_Builder_Module' ) ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
// Bail if the module class is already declared to avoid a fatal |
| 44 |
// "Cannot declare class ... already in use" when this runs more than once. |
| 45 |
if ( class_exists( 'Smart_Post_Template_Module' ) ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Smart Post Template Module Class. |
| 51 |
* |
| 52 |
* Integrates Smart Post Show with Divi Builder. |
| 53 |
* |
| 54 |
* @since 4.0.0 |
| 55 |
*/ |
| 56 |
class Smart_Post_Template_Module extends ET_Builder_Module { |
| 57 |
|
| 58 |
/** |
| 59 |
* Module slug. |
| 60 |
* |
| 61 |
* @var string |
| 62 |
*/ |
| 63 |
public $slug = 'smart_post_template'; |
| 64 |
|
| 65 |
/** |
| 66 |
* Module icon path. |
| 67 |
* |
| 68 |
* @var string |
| 69 |
*/ |
| 70 |
public $icon_path = ''; |
| 71 |
|
| 72 |
/** |
| 73 |
* Visual Builder support. |
| 74 |
* |
| 75 |
* @var string |
| 76 |
*/ |
| 77 |
public $vb_support = 'partial'; |
| 78 |
|
| 79 |
/** |
| 80 |
* Initialize the module. |
| 81 |
* |
| 82 |
* Sets up the module name, icon, and enqueues necessary scripts. |
| 83 |
* |
| 84 |
* @since 4.0.0 |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
public function init() { |
| 88 |
$this->name = esc_html__( 'Saved Templates', 'post-carousel' ); |
| 89 |
$this->icon_path = plugin_dir_path( __FILE__ ) . 'icon.svg'; |
| 90 |
|
| 91 |
// Enqueue scripts for page builder. |
| 92 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_page_builder_scripts' ) ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Enqueue scripts for Divi builder. |
| 97 |
* |
| 98 |
* Registers and enqueues JavaScript files for the Divi builder functionality. |
| 99 |
* |
| 100 |
* @since 4.0.0 |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function enqueue_page_builder_scripts() { |
| 104 |
$scripts = array( |
| 105 |
'sp_pc_blocks_script_js' => 'blocks/assets/js/builder-scripts.js', |
| 106 |
'sp_pc_news_ticker_script' => 'blocks/assets/js/news-ticker.min.js', |
| 107 |
'sp_pc_video_gallery_slider' => 'blocks/assets/js/video-and-gallery-slider.min.js', |
| 108 |
'sp_pc_smart_search_script' => 'blocks/assets/js/smart-search.min.js', |
| 109 |
'sp_pc_smart_toc_script' => 'blocks/assets/js/table-of-content.min.js', |
| 110 |
); |
| 111 |
|
| 112 |
foreach ( $scripts as $handle => $path ) { |
| 113 |
wp_enqueue_script( |
| 114 |
$handle, |
| 115 |
SP_PC_URL . $path, |
| 116 |
array( 'jquery' ), |
| 117 |
SMART_POST_SHOW_VERSION, |
| 118 |
true |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
// Add inline script to reinitialize after Divi builder updates. |
| 123 |
wp_add_inline_script( |
| 124 |
'sp_pc_blocks_script_js', |
| 125 |
'jQuery(document).ready(function($) { |
| 126 |
// Re-initialize when Divi builder updates modules |
| 127 |
$(document).on("et_pb_after_module_render et_fb_ajax_render_shortcode", function() { |
| 128 |
if (typeof window.init === "function") { |
| 129 |
window.init(); |
| 130 |
} |
| 131 |
}); |
| 132 |
});' |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Define module fields (controls). |
| 138 |
* |
| 139 |
* @return array |
| 140 |
*/ |
| 141 |
public function get_fields() { |
| 142 |
return array( |
| 143 |
'templates' => array( |
| 144 |
'label' => esc_html__( 'Select Your Template', 'post-carousel' ), |
| 145 |
'type' => 'select', |
| 146 |
'options' => Helper::get_save_template_list(), |
| 147 |
'default' => 'none', |
| 148 |
), |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Render the module output on the frontend. |
| 154 |
* |
| 155 |
* @param array $attrs Module attributes/settings. |
| 156 |
* @param string $content Inner content (if any). |
| 157 |
* @param string $render_slug Module slug. |
| 158 |
* @return string HTML output. |
| 159 |
*/ |
| 160 |
public function render( $attrs, $content = null, $render_slug = '' ) { |
| 161 |
|
| 162 |
// Get the selected template ID from module settings. |
| 163 |
$template_id = (int) $this->props['templates']; |
| 164 |
|
| 165 |
// Show a placeholder if no template has been selected. |
| 166 |
if ( empty( $template_id ) ) { |
| 167 |
return '<div style=" |
| 168 |
text-align: center; |
| 169 |
padding: 20px; |
| 170 |
border: 2px dashed #ccc; |
| 171 |
color: #999; |
| 172 |
font-size: 14px; |
| 173 |
"> |
| 174 |
Please Select a Smart Post Saved Templates |
| 175 |
</div>'; |
| 176 |
} |
| 177 |
|
| 178 |
// Check if we're in Divi Visual Builder. |
| 179 |
$is_divi_builder = $this->is_divi_builder_render(); |
| 180 |
|
| 181 |
$output = ''; |
| 182 |
|
| 183 |
// In Divi Builder, enqueue CSS. |
| 184 |
if ( $is_divi_builder ) { |
| 185 |
$upload_dir = wp_upload_dir(); |
| 186 |
$css_file = $upload_dir['basedir'] . '/smart-post-show/static/sp-post-' . $template_id . '.css'; |
| 187 |
$css_url = $upload_dir['baseurl'] . '/smart-post-show/static/sp-post-' . $template_id . '.css'; |
| 188 |
|
| 189 |
// Enqueue CSS file if exists. |
| 190 |
if ( file_exists( $css_file ) ) { |
| 191 |
$output .= '<link rel="stylesheet" href="' . esc_url( $css_url ) . '?v=' . esc_attr( filemtime( $css_file ) ) . '">'; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet |
| 192 |
} |
| 193 |
|
| 194 |
// Enqueue dynamic CSS. |
| 195 |
$dynamic_css = get_post_meta( $template_id, '_sp_smart_css', true ); |
| 196 |
if ( ! empty( $dynamic_css ) ) { |
| 197 |
$output .= '<style>' . $dynamic_css . '</style>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 198 |
} |
| 199 |
|
| 200 |
// Hide preloader in all page builder editors. |
| 201 |
$output .= '<style> |
| 202 |
.fl-builder-edit .sp-smart-post-preloader, |
| 203 |
.oxygen-builder-body .sp-smart-post-preloader, |
| 204 |
.vc_editor .sp-smart-post-preloader, |
| 205 |
.wp-theme-Divi .sp-smart-post-preloader, |
| 206 |
.bricks-edit-mode .sp-smart-post-preloader { |
| 207 |
display: none !important; |
| 208 |
} |
| 209 |
</style>'; |
| 210 |
} |
| 211 |
|
| 212 |
// Execute the shortcode. |
| 213 |
$shortcode_output = do_shortcode( '[smart_post id="' . $template_id . '"]' ); |
| 214 |
|
| 215 |
// Wrap with div on frontend only (not in Divi Builder). |
| 216 |
if ( ! $is_divi_builder ) { |
| 217 |
$output .= '<div class="sp-smart-post-builder-wrap" data-builderTemplateId="' . esc_attr( $template_id ) . '">' . $shortcode_output . '</div>'; |
| 218 |
} else { |
| 219 |
$output .= $shortcode_output; |
| 220 |
} |
| 221 |
|
| 222 |
return $output; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Check if currently rendering in Divi Visual Builder. |
| 227 |
* |
| 228 |
* Determines if the current render context is within the Divi |
| 229 |
* Visual Builder by checking for builder-specific parameters. |
| 230 |
* |
| 231 |
* @since 4.0.0 |
| 232 |
* @return bool True if in Divi builder, false otherwise. |
| 233 |
*/ |
| 234 |
protected function is_divi_builder_render() { |
| 235 |
// Check for Divi builder specific parameters or context. |
| 236 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Divi builder context check, not form processing. |
| 237 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Divi specific parameters, compared as strict strings. |
| 238 |
return ( isset( $_GET['et_fb'] ) && '1' === $_GET['et_fb'] ) || |
| 239 |
( isset( $_POST['action'] ) && 'et_fb_ajax_render_shortcode' === $_POST['action'] ) || |
| 240 |
( function_exists( 'et_core_is_fb_enabled' ) && et_core_is_fb_enabled() ); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
new Smart_Post_Template_Module(); |
| 245 |
} |
| 246 |
|