PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / 1.0.2
FrontBlocks for Gutenberg/GeneratePress v1.0.2
trunk 0.2.0 0.2.1 0.2.2 0.2.3 0.2.4 0.2.5 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 ci-artifacts
frontblocks / includes / post / frontblocks-insert-post.php
frontblocks / includes / post Last commit date
frontblocks-insert-post-option.jsx 11 months ago frontblocks-insert-post.css 11 months ago frontblocks-insert-post.php 11 months ago
frontblocks-insert-post.php
286 lines
1 <?php
2 /**
3 * Class Insert Post Block
4 *
5 * @package WordPress
6 * @author David Perez <david@close.technology>
7 * @copyright 2023 Closemarketing
8 * @version 1.0
9 */
10
11 defined( 'ABSPATH' ) || exit;
12
13 add_action( 'wp_enqueue_scripts', 'frbl_insert_post_scripts', 99 );
14 /**
15 * Loads Scripts for Insert Post Block
16 *
17 * @return void
18 */
19 function frbl_insert_post_scripts() {
20 $dist_dir = WP_DEBUG ? 'post/' : 'dist/';
21 wp_enqueue_style(
22 'frontblocks-insert-post',
23 FRBL_PLUGIN_URL . 'includes/' . $dist_dir . 'frontblocks-insert-post.css',
24 array(),
25 FRBL_VERSION
26 );
27 }
28
29 add_action( 'enqueue_block_editor_assets', 'frbl_enqueue_insert_post_editor_assets' );
30 /**
31 * Enqueue insert post block editor script
32 *
33 * @return void
34 */
35 function frbl_enqueue_insert_post_editor_assets() {
36 // Enqueue jQuery UI for autocomplete
37 wp_enqueue_script( 'jquery-ui-autocomplete' );
38 wp_enqueue_style( 'jquery-ui', 'https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css' );
39
40 wp_enqueue_script(
41 'frontblocks-insert-post-option',
42 FRBL_PLUGIN_URL . 'includes/dist/frontblocks-insert-post-option.js',
43 array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-data', 'wp-edit-post', 'wp-api-fetch', 'jquery', 'jquery-ui-autocomplete' ),
44 FRBL_VERSION,
45 true
46 );
47
48 // Localize script with AJAX URL and nonce
49 wp_localize_script(
50 'frontblocks-insert-post-option',
51 'frblInsertPost',
52 array(
53 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
54 'nonce' => wp_create_nonce( 'frbl_insert_post_nonce' ),
55 )
56 );
57 }
58
59 // AJAX handler for searching posts
60 add_action( 'wp_ajax_frbl_search_posts', 'frbl_search_posts_callback' );
61 add_action( 'wp_ajax_nopriv_frbl_search_posts', 'frbl_search_posts_callback' );
62
63 /**
64 * AJAX callback for searching posts
65 *
66 * @return void
67 */
68 function frbl_search_posts_callback() {
69 // Verify nonce
70 if ( ! wp_verify_nonce( $_POST['nonce'], 'frbl_insert_post_nonce' ) ) {
71 wp_die( 'Security check failed' );
72 }
73
74 $search_term = sanitize_text_field( $_POST['search'] ?? '' );
75 $post_type = sanitize_text_field( $_POST['post_type'] ?? 'post' );
76
77 if ( empty( $search_term ) ) {
78 wp_send_json_error( 'Search term is required' );
79 }
80
81 $args = array(
82 'post_type' => $post_type,
83 'post_status' => 'publish',
84 'posts_per_page' => 10,
85 's' => $search_term,
86 'orderby' => 'title',
87 'order' => 'ASC',
88 );
89
90 $query = new WP_Query( $args );
91 $posts = array();
92
93 if ( $query->have_posts() ) {
94 while ( $query->have_posts() ) {
95 $query->the_post();
96 $posts[] = array(
97 'id' => get_the_ID(),
98 'title' => get_the_title(),
99 'type' => get_post_type(),
100 );
101 }
102 wp_reset_postdata();
103 }
104
105 wp_send_json_success( $posts );
106 }
107
108 // Register the custom block
109 add_action( 'init', 'frbl_register_insert_post_block' );
110
111 /**
112 * Register the Insert Post block
113 *
114 * @return void
115 */
116 function frbl_register_insert_post_block() {
117 register_block_type(
118 'frontblocks/insert-post',
119 array(
120 'editor_script' => 'frontblocks-insert-post-option',
121 'render_callback' => 'frbl_render_insert_post_block',
122 'attributes' => array(
123 'selectedPostId' => array(
124 'type' => 'number',
125 'default' => 0,
126 ),
127 'selectedPostType' => array(
128 'type' => 'string',
129 'default' => 'post',
130 ),
131 'selectedPostTitle' => array(
132 'type' => 'string',
133 'default' => '',
134 ),
135 'selectedPostContent' => array(
136 'type' => 'string',
137 'default' => '',
138 ),
139 'className' => array(
140 'type' => 'string',
141 'default' => '',
142 ),
143 ),
144 )
145 );
146 }
147
148 /**
149 * Render the Insert Post block on frontend
150 *
151 * @param array $attributes Block attributes.
152 * @return string HTML output.
153 */
154 function frbl_render_insert_post_block( $attributes ) {
155 $post_id = $attributes['selectedPostId'] ?? 0;
156
157 if ( ! $post_id ) {
158 return '<div class="frbl-insert-post-empty">' . __( 'No post selected', 'frontblocks' ) . '</div>';
159 }
160
161 $post = get_post( $post_id );
162
163 if ( ! $post || 'publish' !== $post->post_status ) {
164 return '<div class="frbl-insert-post-error">' . __( 'Selected post not found or not published', 'frontblocks' ) . '</div>';
165 }
166
167 $title = get_the_title( $post_id );
168 $content = apply_filters( 'the_content', $post->post_content );
169
170 $wrapper_class = 'frbl-insert-post';
171 if ( ! empty( $attributes['className'] ) ) {
172 $wrapper_class .= ' ' . esc_attr( $attributes['className'] );
173 }
174
175 ob_start();
176 ?>
177 <div class="<?php echo esc_attr( $wrapper_class ); ?>">
178 <?php if ( ! empty( $title ) ) : ?>
179 <h2 class="frbl-insert-post-title"><?php echo esc_html( $title ); ?></h2>
180 <?php endif; ?>
181
182 <?php if ( ! empty( $content ) ) : ?>
183 <div class="frbl-insert-post-content">
184 <?php echo wp_kses_post( $content ); ?>
185 </div>
186 <?php endif; ?>
187 </div>
188 <?php
189 return ob_get_clean();
190 }
191
192 // Add custom attributes to existing GenerateBlocks blocks
193 add_filter( 'render_block_generateblocks/grid', 'frbl_add_insert_post_attributes_to_grid_block', 10, 2 );
194
195 /**
196 * Hook to filter the block output on frontend for insert post functionality
197 *
198 * @param string $block_content Block content.
199 * @param array $block Block attributes.
200 * @return string Modified block content.
201 */
202 function frbl_add_insert_post_attributes_to_grid_block( $block_content, $block ) {
203 $attrs = $block['attrs'] ?? array();
204 $insert_post_enabled = isset( $attrs['frblInsertPostEnabled'] ) ? (bool) $attrs['frblInsertPostEnabled'] : false;
205
206 if ( $insert_post_enabled ) {
207 // Add data attribute to indicate insert post functionality
208 $block_content = preg_replace(
209 '/<div([^>]*)class="([^"]*gb-grid-wrapper[^"]*)"([^>]*)>/',
210 '<div$1class="$2 frbl-insert-post-grid"$3 data-insert-post="true">',
211 $block_content,
212 1
213 );
214 }
215
216 return $block_content;
217 }
218
219 // Register attributes for existing blocks
220 add_action(
221 'init',
222 function () {
223 add_filter(
224 'generateblocks_blocks_registered_block',
225 'frbl_register_insert_post_attributes_for_grid_block',
226 9,
227 2
228 );
229
230 add_action(
231 'enqueue_block_editor_assets',
232 function () {
233 wp_add_inline_script(
234 'wp-blocks',
235 "
236 wp.hooks.addFilter(
237 'blocks.registerBlockType',
238 'frontblocks/insert-post-grid-attributes',
239 function( settings, name ) {
240 if ( name !== 'generateblocks/grid' ) {
241 return settings;
242 }
243
244 settings.attributes = {
245 ...settings.attributes,
246 frblInsertPostEnabled: {
247 type: 'boolean',
248 default: false
249 }
250 };
251
252 return settings;
253 }
254 );
255 "
256 );
257 }
258 );
259 },
260 5
261 );
262
263 /**
264 * Register insert post attributes for GenerateBlocks Grid block
265 *
266 * @param array $block_args The block arguments.
267 * @param string $block_type The name of the block.
268 * @return array Modified block arguments.
269 */
270 function frbl_register_insert_post_attributes_for_grid_block( $block_args, $block_type ) {
271 if ( 'generateblocks/grid' !== $block_type ) {
272 return $block_args;
273 }
274
275 if ( ! isset( $block_args['attributes'] ) ) {
276 $block_args['attributes'] = array();
277 }
278
279 $block_args['attributes']['frblInsertPostEnabled'] = array(
280 'type' => 'boolean',
281 'default' => false,
282 );
283
284 return $block_args;
285 }
286