PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / 1.3.2
FrontBlocks for Gutenberg/GeneratePress v1.3.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 / Frontend / InsertPost.php
frontblocks / includes / Frontend Last commit date
Animations.php 4 months ago BackButton.php 7 months ago BlockPatterns.php 4 months ago Carousel.php 6 months ago ContainerEdgeAlignment.php 7 months ago Counter.php 4 months ago Events.php 6 months ago FluidTypography.php 4 months ago Gallery.php 8 months ago GravityFormsInline.php 7 months ago Headline.php 4 months ago InsertPost.php 8 months ago ProductCategories.php 8 months ago ReadingProgress.php 7 months ago ReadingTime.php 8 months ago ShapeAnimations.php 7 months ago StackedImages.php 4 months ago StickyColumn.php 8 months ago Testimonials.php 8 months ago
InsertPost.php
321 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 // Set script translations for JavaScript.
77 wp_set_script_translations(
78 'frontblocks-insert-post-option',
79 'frontblocks'
80 );
81
82 // Localize script with AJAX URL and nonce.
83 wp_localize_script(
84 'frontblocks-insert-post-option',
85 'frblInsertPost',
86 array(
87 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
88 'nonce' => wp_create_nonce( 'frbl_insert_post_nonce' ),
89 )
90 );
91 }
92
93 /**
94 * AJAX callback for searching posts.
95 *
96 * @return void
97 */
98 public function search_posts_callback() {
99 // Verify nonce.
100 $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
101 if ( ! wp_verify_nonce( $nonce, 'frbl_insert_post_nonce' ) ) {
102 wp_die( 'Security check failed' );
103 }
104
105 $search_term = sanitize_text_field( wp_unslash( $_POST['search'] ?? '' ) );
106 $post_type = sanitize_text_field( wp_unslash( $_POST['post_type'] ?? 'post' ) );
107
108 if ( empty( $search_term ) ) {
109 wp_send_json_error( 'Search term is required' );
110 }
111
112 $args = array(
113 'post_type' => $post_type,
114 'post_status' => 'publish',
115 'posts_per_page' => 10,
116 's' => $search_term,
117 'orderby' => 'title',
118 'order' => 'ASC',
119 );
120
121 $query = new \WP_Query( $args );
122 $posts = array();
123
124 if ( $query->have_posts() ) {
125 while ( $query->have_posts() ) {
126 $query->the_post();
127 $posts[] = array(
128 'id' => get_the_ID(),
129 'title' => get_the_title(),
130 'type' => get_post_type(),
131 );
132 }
133 wp_reset_postdata();
134 }
135
136 wp_send_json_success( $posts );
137 }
138
139 /**
140 * Register the Insert Post block.
141 *
142 * @return void
143 */
144 public function register_insert_post_block() {
145 register_block_type(
146 'frontblocks/insert-post',
147 array(
148 'editor_script' => 'frontblocks-insert-post-option',
149 'render_callback' => array( $this, 'render_insert_post_block' ),
150 'attributes' => array(
151 'selectedPostId' => array(
152 'type' => 'number',
153 'default' => 0,
154 ),
155 'selectedPostType' => array(
156 'type' => 'string',
157 'default' => 'post',
158 ),
159 'selectedPostTitle' => array(
160 'type' => 'string',
161 'default' => '',
162 ),
163 'selectedPostContent' => array(
164 'type' => 'string',
165 'default' => '',
166 ),
167 'className' => array(
168 'type' => 'string',
169 'default' => '',
170 ),
171 ),
172 )
173 );
174 }
175
176 /**
177 * Render the Insert Post block on frontend.
178 *
179 * @param array $attributes Block attributes.
180 * @return string HTML output.
181 */
182 public function render_insert_post_block( $attributes ) {
183 $post_id = $attributes['selectedPostId'] ?? 0;
184
185 if ( ! $post_id ) {
186 return '<div class="frbl-insert-post-empty">' . __( 'No post selected', 'frontblocks' ) . '</div>';
187 }
188
189 $post = get_post( $post_id );
190
191 if ( ! $post || 'publish' !== $post->post_status ) {
192 return '<div class="frbl-insert-post-error">' . __( 'Selected post not found or not published', 'frontblocks' ) . '</div>';
193 }
194
195 $title = get_the_title( $post_id );
196 $content = $post->post_content;
197
198 if ( empty( $content ) ) {
199 return '';
200 }
201
202 $wrapper_class = 'frbl-insert-post';
203 if ( ! empty( $attributes['className'] ) ) {
204 $wrapper_class .= ' ' . esc_attr( $attributes['className'] );
205 }
206
207 ob_start();
208 ?>
209 <div class="<?php echo esc_attr( $wrapper_class ); ?>">
210 <?php if ( ! empty( $title ) ) : ?>
211 <h2 class="frbl-insert-post-title"><?php echo esc_html( $title ); ?></h2>
212 <?php endif; ?>
213
214 <div class="frbl-insert-post-content">
215 <?php echo wp_kses_post( apply_filters( 'the_content', $content ) ); ?>
216 </div>
217 </div>
218 <?php
219 return ob_get_clean();
220 }
221
222 /**
223 * Add insert post attributes to grid block.
224 *
225 * @param string $block_content Block content.
226 * @param array $block Block attributes.
227 * @return string
228 */
229 public function add_insert_post_attributes_to_grid_block( $block_content, $block ) {
230 $attrs = $block['attrs'] ?? array();
231 $insert_post_enabled = isset( $attrs['frblInsertPostEnabled'] ) ? (bool) $attrs['frblInsertPostEnabled'] : false;
232
233 if ( $insert_post_enabled ) {
234 // Add data attribute to indicate insert post functionality.
235 $block_content = preg_replace(
236 '/<div([^>]*)class="([^"]*gb-grid-wrapper[^"]*)"([^>]*)>/',
237 '<div$1class="$2 frbl-insert-post-grid"$3 data-insert-post="true">',
238 $block_content,
239 1
240 );
241 }
242
243 return $block_content;
244 }
245
246 /**
247 * Register custom attributes for blocks.
248 *
249 * @return void
250 */
251 public function register_custom_attributes() {
252 add_filter(
253 'generateblocks_blocks_registered_block',
254 array( $this, 'register_insert_post_attributes_for_grid_block' ),
255 9,
256 2
257 );
258
259 add_action(
260 'enqueue_block_editor_assets',
261 array( $this, 'add_inline_script_for_attributes' )
262 );
263 }
264
265 /**
266 * Register insert post attributes for GenerateBlocks Grid block.
267 *
268 * @param array $block_args The block arguments.
269 * @param string $block_type The name of the block.
270 * @return array Modified block arguments.
271 */
272 public function register_insert_post_attributes_for_grid_block( $block_args, $block_type ) {
273 if ( 'generateblocks/grid' !== $block_type ) {
274 return $block_args;
275 }
276
277 if ( ! isset( $block_args['attributes'] ) ) {
278 $block_args['attributes'] = array();
279 }
280
281 $block_args['attributes']['frblInsertPostEnabled'] = array(
282 'type' => 'boolean',
283 'default' => false,
284 );
285
286 return $block_args;
287 }
288
289 /**
290 * Add inline script for block attributes.
291 *
292 * @return void
293 */
294 public function add_inline_script_for_attributes() {
295 wp_add_inline_script(
296 'wp-blocks',
297 "
298 wp.hooks.addFilter(
299 'blocks.registerBlockType',
300 'frontblocks/insert-post-grid-attributes',
301 function( settings, name ) {
302 if ( name !== 'generateblocks/grid' ) {
303 return settings;
304 }
305
306 settings.attributes = {
307 ...settings.attributes,
308 frblInsertPostEnabled: {
309 type: 'boolean',
310 default: false
311 }
312 };
313
314 return settings;
315 }
316 );
317 "
318 );
319 }
320 }
321