PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 4.0.7
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v4.0.7
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 2.3.5 2.3.6 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.15 2.4.16 2.4.17 2.4.18 2.4.19 2.4.2 2.4.20 2.4.21 All 88 releases
post-carousel / admin / page-builder / wpBakery / smart-post-show-wpbakery.php

smart-post-show-wpbakery.php in Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News 4.0.7, at admin/page-builder/wpBakery/smart-post-show-wpbakery.php

256 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPBakery Page Builder - smart post show Element
4 *
5 * @package Smart_Post_Show_Pro
6 */
7
8 use SmartPostShow\Blocks\Helper;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly.
12 }
13
14 /**
15 * Register the smart post show element with WPBakery.
16 *
17 * This function maps a new shortcode element in WPBakery
18 * with a Select field to choose a Smart Post Show template.
19 *
20 * @since 4.0.0
21 * @return void
22 */
23 function sp_wpbakery_register_element() {
24
25 // Check if WPBakery Page Builder is active.
26 if ( ! defined( 'WPB_VC_VERSION' ) ) {
27 return;
28 }
29
30 vc_map(
31 array(
32 // ── Basic Info ────────────────────────────────────────────────
33 'name' => __( 'Smart Post', 'post-carousel' ),
34 'base' => 'sp_smart_wpbakery_shortcode',
35 'description' => __( 'Display a Smart Post template.', 'post-carousel' ),
36 'category' => __( 'Smart Post', 'post-carousel' ),
37 'icon' => 'sps-wpbakery-icon',
38
39 // ── Fields (Params) ───────────────────────────────────────────
40 'params' => array(
41
42 // Select field to choose a template.
43 array(
44 'type' => 'dropdown',
45 'heading' => __( 'Saved Templates', 'post-carousel' ),
46 'param_name' => 'template_id',
47 'value' => sp_wpbakery_get_template_list(),
48 'std' => '',
49 'description' => __( 'Select a Smart Post saved template to display.', 'post-carousel' ),
50 'save_always' => true,
51 ),
52
53 ),
54 )
55 );
56 }
57
58 // Register the element after WPBakery has fully loaded.
59 add_action( 'vc_before_init', 'sp_wpbakery_register_element' );
60
61 // Enqueue scripts for WPBakery frontend.
62 add_action( 'wp_enqueue_scripts', 'sp_wpbakery_enqueue_scripts' );
63
64 // Enqueue styles for WPBakery admin editor.
65 add_action( 'admin_enqueue_scripts', 'sp_wpbakery_admin_enqueue_styles' );
66
67 /**
68 * Enqueue scripts for WPBakery frontend.
69 *
70 * @since 4.0.0
71 * @return void
72 */
73 function sp_wpbakery_enqueue_scripts() {
74 // Only enqueue if we're on the frontend.
75 if ( is_admin() ) {
76 return;
77 }
78
79 $scripts = array(
80 'sp_pc_blocks_script_js' => 'blocks/assets/js/builder-scripts.js',
81 'sp_pc_news_ticker_script' => 'blocks/assets/js/news-ticker.min.js',
82 'sp_pc_video_gallery_slider' => 'blocks/assets/js/video-and-gallery-slider.min.js',
83 'sp_pc_smart_search_script' => 'blocks/assets/js/smart-search.min.js',
84 'sp_pc_smart_toc_script' => 'blocks/assets/js/table-of-content.min.js',
85 );
86
87 foreach ( $scripts as $handle => $path ) {
88 wp_enqueue_script(
89 $handle,
90 SP_PC_URL . $path,
91 array( 'jquery' ),
92 SMART_POST_SHOW_VERSION,
93 true
94 );
95 }
96
97 // Add inline script to reinitialize after WPBakery frontend editor updates.
98 wp_add_inline_script(
99 'sp_pc_blocks_script_js',
100 'jQuery(document).ready(function($) {
101 // Re-initialize scripts when WPBakery updates content
102 function reinitSmartPostScripts() {
103 if (typeof window.init === "function") {
104 window.init();
105 }
106 }
107
108 // Listen for various WPBakery events
109 $(document).on("vc_js_reload", reinitSmartPostScripts);
110 $(document).on("vc_frontend_default_editor_loaded", reinitSmartPostScripts);
111 $(document).on("vc_frontend_render", reinitSmartPostScripts);
112
113 // Also observe DOM changes as fallback - watch for .sp-smart-post-builder-wrap
114 var observer = new MutationObserver(function(mutations) {
115 mutations.forEach(function(mutation) {
116 if (mutation.addedNodes.length) {
117 for (var i = 0; i < mutation.addedNodes.length; i++) {
118 var node = mutation.addedNodes[i];
119 if (node.nodeType === 1) {
120 if ($(node).hasClass("sp-smart-post-builder-wrap") || $(node).find(".sp-smart-post-builder-wrap").length) {
121 reinitSmartPostScripts();
122 break;
123 }
124 }
125 }
126 }
127 });
128 });
129
130 // Start observing the document for changes
131 if (document.body) {
132 observer.observe(document.body, { childList: true, subtree: true });
133 }
134 });'
135 );
136 }
137
138 /**
139 * Enqueue styles for WPBakery admin editor.
140 *
141 * @since 4.0.0
142 * @return void
143 */
144 function sp_wpbakery_admin_enqueue_styles() {
145 // Check if WPBakery is active and we're on a valid edit screen.
146 if ( ! defined( 'WPB_VC_VERSION' ) ) {
147 return;
148 }
149
150 $screen = get_current_screen();
151 if ( ! $screen ) {
152 return;
153 }
154
155 // Only enqueue on post edit screens.
156 if ( 'post' !== $screen->base ) {
157 return;
158 }
159
160 // Enqueue fontello icons and other required styles.
161 wp_enqueue_style( 'pcp-font-awesome' );
162 wp_enqueue_style( 'pcp_swiper' );
163 wp_enqueue_style( 'pcp-bxslider' );
164 wp_enqueue_style( 'pcp-likes' );
165 wp_enqueue_style( 'pcp-popup' );
166 wp_enqueue_style( 'pcp_fonttello_icon' );
167 wp_enqueue_style( 'pcp-style' );
168 }
169
170 /**
171 * Retrieve all published Smart Post Templates for the dropdown.
172 *
173 * WPBakery dropdown options format: array( 'Label' => 'value' )
174 *
175 * @return array
176 */
177 /**
178 * Retrieve all published Smart Post Templates.
179 *
180 * @since 4.0.0
181 * @return array Template list.
182 */
183 function sp_wpbakery_get_template_list() {
184 $list = array( '' => esc_html__( 'Select Template', 'post-carousel' ) );
185
186 $query = new \WP_Query(
187 array(
188 'post_type' => 'sp_post_template',
189 'post_status' => 'publish',
190 'posts_per_page' => 10000,
191 )
192 );
193
194 if ( $query->have_posts() ) {
195 foreach ( $query->posts as $post ) {
196 $list[ $post->ID ] = $post->post_title;
197 }
198
199 // Sort by ID descending so the latest template appears first.
200 krsort( $list );
201 }
202 // WPBakery format: label => ID (flip from Helper's ID => label).
203 $template_list = array();
204 foreach ( $list as $id => $title ) {
205 $template_list[ $title ] = $id;
206 }
207 return $template_list;
208 }
209
210 /**
211 * Shortcode handler for [sp_smart_wpbakery_shortcode].
212 *
213 * This function is called when WPBakery renders the element
214 * on the frontend. It executes the Smart Post Show shortcode
215 * with the selected template ID.
216 *
217 * @since 4.0.0
218 * @param array $atts Shortcode attributes.
219 * @param string $content Inner content (not used).
220 * @return string HTML output.
221 */
222 function sp_wpbakery_render_element( $atts, $content = null ) {
223
224 // Extract and sanitize shortcode attributes.
225 $atts = shortcode_atts(
226 array(
227 'template_id' => '',
228 ),
229 $atts,
230 'sp_smart_wpbakery_shortcode'
231 );
232
233 $template_id = (int) $atts['template_id'];
234
235 // Show a placeholder if no template has been selected.
236 if ( empty( $template_id ) ) {
237 return '<div style="
238 text-align: center;
239 padding: 20px;
240 border: 2px dashed #ccc;
241 color: #999;
242 font-size: 14px;
243 ">
244 Please Select a Smart Post Saved Templates
245 </div>';
246 }
247
248 // Execute the shortcode and return the rendered output.
249 $output = do_shortcode( '[smart_post id="' . $template_id . '"]' );
250
251 return '<div class="sp-smart-post-builder-wrap" data-builderTemplateId="' . esc_attr( $template_id ) . '">' . $output . '</div>';
252 }
253
254 // Register the shortcode handler for the WPBakery element.
255 add_shortcode( 'sp_smart_wpbakery_shortcode', 'sp_wpbakery_render_element' );
256