| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bricks Builder Integration for Smart Post Show. |
| 4 |
* |
| 5 |
* @link https://shapedplugin.com/ |
| 6 |
* |
| 7 |
* @package Smart_Post_Show |
| 8 |
* @author ShapedPlugin <support@shapedplugin.com> |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
use SmartPostShow\Blocks\Helper; |
| 16 |
|
| 17 |
/** |
| 18 |
* Smart Post Bricks Integration Class. |
| 19 |
* |
| 20 |
* Integrates Smart Post Show with Bricks page builder. |
| 21 |
* |
| 22 |
* @since 4.0.0 |
| 23 |
*/ |
| 24 |
class Smart_Post_Bricks_Integration extends \Bricks\Element { |
| 25 |
/** |
| 26 |
* Element category. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
public $category = 'general'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Element name. |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
public $name = 'smart-post-addon'; |
| 38 |
|
| 39 |
/** |
| 40 |
* Element icon. |
| 41 |
* |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
public $icon = 'sps-icon-elementor'; |
| 45 |
|
| 46 |
/** |
| 47 |
* Get the element label. |
| 48 |
* |
| 49 |
* Returns the localized label for the element. |
| 50 |
* |
| 51 |
* @since 4.0.0 |
| 52 |
* @return string Element label. |
| 53 |
*/ |
| 54 |
public function get_label() { |
| 55 |
return esc_html__( 'Saved Templates', 'post-carousel' ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Enqueue scripts for Bricks builder. |
| 60 |
* |
| 61 |
* Registers and enqueues JavaScript files for the Bricks builder functionality. |
| 62 |
* |
| 63 |
* @since 4.0.0 |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function enqueue_scripts() { |
| 67 |
$scripts = array( |
| 68 |
'sp_pc_blocks_script_js' => 'blocks/assets/js/builder-scripts.js', |
| 69 |
'sp_pc_news_ticker_script' => 'blocks/assets/js/news-ticker.min.js', |
| 70 |
'sp_pc_video_gallery_slider' => 'blocks/assets/js/video-and-gallery-slider.min.js', |
| 71 |
'sp_pc_smart_search_script' => 'blocks/assets/js/smart-search.min.js', |
| 72 |
'sp_pc_smart_toc_script' => 'blocks/assets/js/table-of-content.min.js', |
| 73 |
); |
| 74 |
|
| 75 |
foreach ( $scripts as $handle => $path ) { |
| 76 |
wp_enqueue_script( |
| 77 |
$handle, |
| 78 |
SP_PC_URL . $path, |
| 79 |
array( 'jquery' ), |
| 80 |
SMART_POST_SHOW_VERSION, |
| 81 |
true |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
// Add inline script to reinitialize after Bricks builder updates. |
| 86 |
wp_add_inline_script( |
| 87 |
'sp_pc_blocks_script_js', |
| 88 |
'jQuery(document).ready(function($) { |
| 89 |
// Re-initialize when Bricks builder updates elements |
| 90 |
$(document).on("bricksAjaxRender", function() { |
| 91 |
if (typeof window.init === "function") { |
| 92 |
window.init(); |
| 93 |
} |
| 94 |
}); |
| 95 |
});' |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Enqueue styles for Bricks builder. |
| 101 |
* |
| 102 |
* Registers and enqueues CSS stylesheets for the Bricks builder. |
| 103 |
* |
| 104 |
* @since 4.0.0 |
| 105 |
* @return void |
| 106 |
*/ |
| 107 |
public function enqueue_styles() { |
| 108 |
// Enqueue fontello icons. |
| 109 |
wp_enqueue_style( |
| 110 |
'pcp_fonttello_icon', |
| 111 |
SP_PC_URL . 'admin/css/fontello.css', |
| 112 |
array(), |
| 113 |
SMART_POST_SHOW_VERSION, |
| 114 |
'all' |
| 115 |
); |
| 116 |
|
| 117 |
wp_enqueue_style( 'pcp-font-awesome' ); |
| 118 |
wp_enqueue_style( 'pcp_swiper' ); |
| 119 |
wp_enqueue_style( 'pcp-bxslider' ); |
| 120 |
wp_enqueue_style( 'pcp-likes' ); |
| 121 |
wp_enqueue_style( 'pcp-popup' ); |
| 122 |
wp_enqueue_style( 'pcp_fonttello_icon' ); |
| 123 |
wp_enqueue_style( 'pcp-style' ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Set up element controls. |
| 128 |
* |
| 129 |
* Defines the controls for the element in the Bricks builder panel. |
| 130 |
* |
| 131 |
* @since 4.0.0 |
| 132 |
* @return void |
| 133 |
*/ |
| 134 |
public function set_controls() { |
| 135 |
$this->controls['template_id'] = array( |
| 136 |
'type' => 'select', |
| 137 |
'label' => esc_html__( 'Select Template', 'post-carousel' ), |
| 138 |
'options' => Helper::get_save_template_list(), |
| 139 |
'clearable' => false, |
| 140 |
'default' => '0', |
| 141 |
'pasteStyles' => true, |
| 142 |
'inline' => true, |
| 143 |
); |
| 144 |
$this->controls['separator'] = array( |
| 145 |
'type' => 'separator', |
| 146 |
); |
| 147 |
$this->controls['help'] = array( |
| 148 |
'type' => 'info', |
| 149 |
'content' => 'Please Select a Smart Post Saved Templates', |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Render the element output on the frontend. |
| 155 |
* |
| 156 |
* Called by Bricks Builder when rendering the element |
| 157 |
* on the page — both in the editor and on the frontend. |
| 158 |
* Outputs the Smart Post Show template with optional CSS. |
| 159 |
* |
| 160 |
* @since 4.0.0 |
| 161 |
* @return void |
| 162 |
*/ |
| 163 |
public function render() { |
| 164 |
|
| 165 |
// Get the selected template ID from element settings. |
| 166 |
$template_id = isset( $this->settings['template_id'] ) ? (int) $this->settings['template_id'] : 0; |
| 167 |
|
| 168 |
// Show a placeholder if no template has been selected. |
| 169 |
if ( empty( $template_id ) ) { |
| 170 |
echo '<div style=" |
| 171 |
text-align: center; |
| 172 |
padding: 20px; |
| 173 |
border: 2px dashed #ccc; |
| 174 |
color: #999; |
| 175 |
font-size: 14px; |
| 176 |
">' . esc_html__( 'Please select a Smart Post Saved Templates', 'post-carousel' ) . '</div>'; |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
$dynamic_css = get_post_meta( $template_id, '_sp_smart_css', true ); // phpcs:ignore WordPress.WP.DeprecatedParameters |
| 181 |
|
| 182 |
// Execute the Smart Post Show shortcode. |
| 183 |
$shortcode_output = do_shortcode( '[smart_post id="' . $template_id . '"]' ); |
| 184 |
|
| 185 |
// Output the wrapping element tag (Bricks handles root attributes). |
| 186 |
echo '<div ' . $this->render_attributes( '_root' ) . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 187 |
|
| 188 |
echo '<style>' . $dynamic_css . '</style>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 189 |
echo '<div class="sp-smart-post-builder-wrap" data-builderTemplateId="' . esc_attr( $template_id ) . '">'; |
| 190 |
echo $shortcode_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 191 |
echo '</div>'; |
| 192 |
|
| 193 |
echo '</div>'; |
| 194 |
} |
| 195 |
} |
| 196 |
|