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