PluginProbe ʕ •ᴥ•ʔ
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid / trunk
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid vtrunk
7.9.3 7.9.2 trunk 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.1.5 4.2.0 4.2.1 4.2.2 4.2.3 5.0.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 6.0.0 7.0.0 7.0.1 7.0.2 7.1.0 7.2.0 7.2.1 7.2.10 7.2.11 7.2.2 7.2.3 7.2.4 7.2.5 7.2.6 7.2.7 7.2.8 7.2.9 7.3.0 7.3.1 7.4.0 7.4.1 7.4.2 7.4.3 7.5.0 7.6.0 7.6.1 7.7.0 7.7.1 7.7.10 7.7.11 7.7.12 7.7.13 7.7.14 7.7.15 7.7.16 7.7.17 7.7.18 7.7.19 7.7.2 7.7.20 7.7.21 7.7.22 7.7.3 7.7.4 7.7.5 7.7.6 7.7.7 7.7.8 7.7.9 7.8.0 7.8.1 7.8.2 7.8.3 7.8.4 7.8.5 7.8.6 7.8.7 7.8.8 7.8.9 7.9.0 7.9.1
the-post-grid / app / Controllers / BlocksController.php
the-post-grid / app / Controllers Last commit date
Admin 1 month ago Api 1 month ago Blocks 2 months ago Hooks 1 month ago AjaxController.php 1 month ago BlocksController.php 1 month ago DiviController.php 1 year ago ElementorController.php 1 year ago GutenBergController.php 1 month ago PageTemplateController.php 2 years ago ScriptController.php 1 month ago ShortcodeController.php 5 months ago WidgetController.php 2 years ago
BlocksController.php
538 lines
1 <?php
2
3 namespace RT\ThePostGrid\Controllers;
4
5 use RT\ThePostGrid\Controllers\Blocks\GridLayout;
6 use RT\ThePostGrid\Controllers\Blocks\ListLayout;
7 use RT\ThePostGrid\Controllers\Blocks\GridHoverLayout;
8 use RT\ThePostGrid\Controllers\Blocks\SectionTitle;
9 use RT\ThePostGrid\Controllers\Blocks\RttpgRow;
10 use RT\ThePostGrid\Helpers\Fns;
11
12 class BlocksController {
13
14 /**
15 * Css Handler to generate dynamic ss for guten blocks
16 */
17 private $version;
18
19 public function __construct() {
20
21 // Layout initialize
22 new GridLayout();
23 new ListLayout();
24 new GridHoverLayout();
25 new SectionTitle();
26 new RttpgRow();
27
28 $this->version = defined( 'WP_DEBUG' ) && WP_DEBUG ? time() : RT_THE_POST_GRID_VERSION;
29 add_action( 'enqueue_block_editor_assets', [ $this, 'editor_assets' ] );
30
31 // All css/js file load in back-end and front-end
32 add_action( 'wp_enqueue_scripts', [ $this, 'tpg_block_enqueue' ] );
33 add_action( 'enqueue_block_editor_assets', [ $this, 'tpg_block_enqueue' ] );
34
35 if ( version_compare( get_bloginfo( 'version' ), '5.8', '>=' ) ) {
36 add_filter( 'block_categories_all', [ $this, 'rttpg_block_categories' ], 1, 2 );
37 } else {
38 add_filter( 'block_categories', [ $this, 'rttpg_block_categories' ], 1, 2 );
39 }
40
41 add_action( 'wp_ajax_rttpg_block_css_save', [ $this, 'save_block_css' ] );
42 add_action( 'wp_ajax_rttpg_block_css_get_posts', [ $this, 'get_posts_call' ] );
43 add_action( 'wp_ajax_rttpg_block_css_appended', [ $this, 'appended' ] );
44 add_action( 'wp_ajax_rttpg_get_layouts', [ $this, 'rttpg_get_layouts' ] );
45 add_action( 'wp_ajax_rttpg_guten_layout_count', [ $this, 'rttpg_guten_layout_count' ] );
46
47 // Decide how css file will be loaded. default filesystem eg: filesystem or at header
48 $option_data = get_option( 'rttpg_options' );
49 if ( isset( $option_data['css_save_as'] ) && 'filesystem' === $option_data['css_save_as'] ) {
50 add_action( 'wp_enqueue_scripts', [ $this, 'add_block_css_file' ] );
51 } else {
52 add_action( 'wp_head', [ $this, 'add_block_inline_css' ], 100 );
53 }
54 }
55
56 public function rttpg_guten_layout_count() {
57 $BASE_URL = 'https://www.radiustheme.com/demo/plugins/the-post-grid-gutenberg/wp-json/rttpgapi/v1/layoutinfo/';
58
59 // Verify the request.
60 check_ajax_referer( 'rttpg_nonce', 'nonce' );
61
62 // It's good let's do some capability check.
63 $user = wp_get_current_user();
64 $allowed_roles = [ 'editor', 'administrator', 'author' ];
65
66 if ( ! array_intersect( $allowed_roles, $user->roles ) ) {
67 wp_die( esc_html__( 'You don\'t have permission to perform this action', 'the-post-grid' ) );
68 }
69
70 // Cool, we're almost there, let's check the user authenticity a little bit, shall we!
71 if ( ! is_user_logged_in() && $user->ID !== sanitize_text_field( $_REQUEST['user_id'] ) ) {
72 wp_die( esc_html__( 'You don\'t have proper authorization to perform this action', 'the-post-grid' ) );
73 }
74
75 $status = isset( $_REQUEST['status'] ) ? $_REQUEST['status'] : '';
76 $layout_id = isset( $_REQUEST['layout_id'] ) ? $_REQUEST['layout_id'] : '';
77
78 $post_args = [ 'timeout' => 120 ];
79 $post_args['body'] = [
80 'status' => $status,
81 'layout_id' => $layout_id,
82 ];
83 $layoutRequest = wp_remote_get( $BASE_URL, $post_args );
84 if ( is_wp_error( $layoutRequest ) ) {
85 wp_send_json_error( [ 'messages' => $layoutRequest->get_error_messages() ] );
86 }
87 $layoutData = json_decode( $layoutRequest['body'], true );
88
89 wp_send_json_success( $layoutData );
90 }
91
92 /**
93 * @return void
94 */
95 public function rttpg_get_layouts() {
96 $BASE_URL = 'https://www.radiustheme.com/demo/plugins/the-post-grid-gutenberg/wp-json/rttpgapi/v1/layouts/';
97
98 // Verify the request.
99 check_ajax_referer( 'rttpg_nonce', 'nonce' );
100
101 // It's good let's do some capability check.
102 $user = wp_get_current_user();
103 $allowed_roles = [ 'editor', 'administrator', 'author' ];
104
105 if ( ! array_intersect( $allowed_roles, $user->roles ) ) {
106 wp_die( esc_html__( 'You don\'t have permission to perform this action', 'the-post-grid' ) );
107 }
108
109 // Cool, we're almost there, let's check the user authenticity a little bit, shall we!
110 if ( ! is_user_logged_in() && $user->ID !== sanitize_text_field( $_REQUEST['user_id'] ) ) {
111 wp_die( esc_html__( 'You don\'t have proper authorization to perform this action', 'the-post-grid' ) );
112 }
113
114 $status = isset( $_REQUEST['status'] ) ? $_REQUEST['status'] : '';
115 $post_args = [ 'timeout' => 120 ];
116 $post_args['body'] = [ 'status' => $status ];
117 $layoutRequest = wp_remote_get( $BASE_URL, $post_args );
118 if ( is_wp_error( $layoutRequest ) ) {
119 wp_send_json_error( [ 'messages' => $layoutRequest->get_error_messages() ] );
120 }
121 $layoutData = json_decode( $layoutRequest['body'], true );
122
123 wp_send_json_success( $layoutData );
124 }
125
126
127 /**
128 * Block Category Add
129 *
130 * @param $categories
131 * @param $post
132 *
133 * @return array|\string[][]|\void[][]
134 */
135 public function rttpg_block_categories( $categories, $post ) {
136 return array_merge(
137 [
138 [
139 'slug' => 'rttpg',
140 'title' => __( 'The Post Grid', 'the-post-grid' ),
141 ],
142 ],
143 $categories
144 );
145 }
146
147
148 /**
149 * Add Block files foe css
150 *
151 * @return void
152 */
153 public function add_block_css_file() {
154
155 $post_id = get_the_ID();
156 $rttpg_upload_dir = wp_upload_dir()['basedir'] . '/rttpg/';
157 $rttpg_upload_url = wp_upload_dir()['baseurl'] . '/rttpg/';
158 // phpcs:ignore
159 if ( isset( $_GET['preview'] ) && ! empty( $_GET['preview'] ) ) {
160 $css_path = $rttpg_upload_dir . 'rttpg-block-preview.css';
161
162 if ( file_exists( $css_path ) ) {
163 if ( ! $this->is_editor_screen() ) {
164 wp_enqueue_style( 'rttpg-block-post-preview', $rttpg_upload_url . 'rttpg-block-preview.css', false, $this->version );
165 }
166 }
167 } elseif ( $post_id ) {
168 $css_dir_path = $rttpg_upload_dir . "rttpg-block-$post_id.css";
169 $css_dir_url = $rttpg_upload_dir . "rttpg-block-$post_id.css";
170
171 if ( file_exists( $css_dir_path ) ) {
172 if ( ! $this->is_editor_screen() ) {
173 wp_enqueue_style( "rttpg-block-post-{$post_id}", $css_dir_url, false, $this->version );
174 }
175 $this->add_reusable_css();
176 } else {
177 // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
178 wp_register_style( 'rttpg-post-data', false );
179 wp_enqueue_style( 'rttpg-post-data' );
180 wp_add_inline_style( 'rttpg-post-data', get_post_meta( get_the_ID(), '_rttpg_block_css', true ) );
181 }
182 }
183 }
184
185 /**
186 * Common css load
187 *
188 * @return void
189 */
190 public function tpg_block_enqueue() {
191
192 if ( ! is_admin() ) {
193 return;
194 }
195
196 wp_enqueue_style( 'rt-fontawsome' );
197 wp_enqueue_style( 'rt-flaticon' );
198 wp_enqueue_style( 'rt-tpg-block' );
199
200 // Custom CSS From Settings
201 $css = isset( $settings['custom_css'] ) ? stripslashes( $settings['custom_css'] ) : null;
202 if ( $css ) {
203 wp_add_inline_style( 'rt-tpg-block', $css );
204 }
205 }
206
207
208 /**
209 * Determine if wppb editor is open
210 *
211 * @return bool
212 *
213 * @since V.1.0.0
214 * @since v.1.0.0
215 */
216 private function is_editor_screen() {
217 //phpcs:ignore WordPress.Security.NonceVerification.Recommended
218 if ( ! empty( $_GET['action'] ) && 'wppb_editor' === $_GET['action'] ) {
219 return true;
220 }
221
222 return false;
223 }
224
225
226 /**
227 * Load Editor Assets
228 *
229 * @return void
230 */
231 public function editor_assets() {
232
233 // Block editor css
234 wp_enqueue_style( 'rttpg-block-admin-css', rtTPG()->get_assets_uri( 'css/admin/block-admin.css' ), '', $this->version );
235
236 // Main compile css and js file
237 wp_enqueue_style( 'rttpg-blocks-css', rtTPG()->get_assets_uri( 'blocks/main.css' ), '', $this->version );
238 wp_enqueue_script(
239 'rttpg-blocks-js',
240 rtTPG()->get_assets_uri( 'blocks/main.js' ),
241 [
242 'wp-block-editor',
243 'wp-blocks',
244 'wp-components',
245 'wp-element',
246 'wp-i18n',
247 ],
248 $this->version,
249 true
250 );
251
252 global $pagenow;
253 $editor_type = 'edit-post';
254
255 if ( 'site-editor.php' === $pagenow ) {
256 $editor_type = 'edit-site';
257 }
258
259 $get_tax_object = get_taxonomies( [], 'objects' );
260 $exclude_tax = Fns::get_excluded_taxonomy();
261 foreach ( $exclude_tax as $_tax ) {
262 unset( $get_tax_object[ $_tax ] );
263 }
264
265 $settings = get_option( rtTPG()->options['settings'] );
266 $ssList = ! empty( $settings['social_share_items'] ) ? $settings['social_share_items'] : [];
267 $enable_ai_button = ! empty( $settings['ai_type'] ) ? $settings['ai_type'] : '';
268 $chatgpt_status = ! empty( $settings['chatgpt_status'] ) ? $settings['chatgpt_status'] : '';
269 $gemini_status = ! empty( $settings['gemini_status'] ) ? $settings['gemini_status'] : '';
270
271 wp_localize_script(
272 'rttpg-blocks-js',
273 'rttpgParams',
274 [
275 'editor_type' => $editor_type,
276 'nonce' => wp_create_nonce( 'rttpg_nonce' ),
277 'ajaxurl' => admin_url( 'admin-ajax.php' ),
278 'site_url' => site_url(),
279 'admin_url' => admin_url(),
280 'plugin_url' => RT_THE_POST_GRID_PLUGIN_URL,
281 'plugin_pro_url' => rtTPG()->getProPath(),
282 'post_type' => Fns::get_post_types(),
283 'all_term_list' => Fns::get_all_taxonomy_guten(),
284 'get_taxonomies' => $get_tax_object,
285 'get_users' => Fns::rt_get_users(),
286 'hasPro' => rtTPG()->hasPro(),
287 'pageTitle' => get_the_title(),
288 'hasWoo' => Fns::is_woocommerce(),
289 'hasAcf' => Fns::is_acf(),
290 'ssList' => $ssList,
291 'current_user_id' => get_current_user_id(),
292 'disableImportButton' => apply_filters( 'rttpg_disable_gutenberg_import_button', 'no' ),
293 'enableAIButton' => $enable_ai_button,
294 'enableChatGPTButton' => $chatgpt_status ? 'yes' : 'no',
295 'enableGeminiButton' => $gemini_status ? 'yes' : 'no',
296 'iconFont' => Fns::tpg_option( 'tpg_icon_font', 'flaticon' ),
297 'avatar' => esc_url( get_avatar_url( get_current_user_id() ) ),
298 ]
299 );
300 }
301
302
303 /**
304 * Add inLine css for page or post
305 *
306 * @return void
307 */
308 public function add_block_inline_css() {
309
310 $post_id = apply_filters( 'tpg_page_id_for_block_css', get_the_ID() );
311
312 if ( $post_id ) {
313 $rttpg_upload_dir = wp_upload_dir()['basedir'] . '/rttpg/';
314 $css_dir_path = $rttpg_upload_dir . "rttpg-block-$post_id.css";
315 if ( file_exists( $css_dir_path ) ) {
316 $blockCss = file_get_contents( $css_dir_path ); //phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
317 echo '<style>' . sanitize_textarea_field( $blockCss ) . '</style>'; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
318 } elseif ( $metaCss = get_post_meta( $post_id, '_rttpg_block_css', true ) ) {
319 echo '<style>' . sanitize_textarea_field( $metaCss ) . '</style>'; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
320 }
321 }
322
323 $this->add_reusable_css();
324 }
325
326
327 /**
328 * Add reusable css
329 */
330 public function add_reusable_css() {
331 $post_id = get_the_ID();
332 $rttpg_upload_dir = wp_upload_dir()['basedir'] . '/rttpg/';
333 $rttpg_upload_url = wp_upload_dir()['baseurl'] . '/rttpg/';
334 if ( $post_id ) {
335 $content_post = get_post( $post_id );
336 if ( isset( $content_post->post_content ) ) {
337 $content = $content_post->post_content;
338 $parse_blocks = parse_blocks( $content );
339 $css_id = $this->reference_id( $parse_blocks );
340 if ( is_array( $css_id ) ) {
341 if ( ! empty( $css_id ) ) {
342 $css_id = array_unique( $css_id );
343 foreach ( $css_id as $value ) {
344 $css_dir_path = $rttpg_upload_dir . "rttpg-block-$value.css";
345 if ( file_exists( $css_dir_path ) ) {
346 wp_enqueue_style( "rttpg-block-{$value}", $rttpg_upload_url . "rttpg-block-{$value}.css", false, RTTPG_VERSION );
347 }
348 }
349 }
350 }
351 }
352 }
353 }
354
355 /**
356 * Save Import CSS in the top of the File
357 *
358 * @return void Array of the Custom Message
359 */
360 public function save_block_css() {
361
362 try {
363 if ( ! current_user_can( 'edit_others_posts' ) ) {
364 throw new Exception( __( 'User permission error', 'the-post-grid' ) );
365 }
366 check_ajax_referer( 'rttpg_nonce', 'nonce' );
367 global $wp_filesystem;
368 if ( ! $wp_filesystem ) {
369 require_once ABSPATH . 'wp-admin/includes/file.php';
370 }
371
372 $post_id = ! empty( $_POST['post_id'] ) ? sanitize_text_field( $_POST['post_id'] ) : '';
373 $blockCss = ! empty( $_POST['block_css'] ) ? sanitize_text_field( $_POST['block_css'] ) : '';
374
375 if ( $post_id == 'rttpg-widget' && isset( $_POST['has_block'] ) ) {
376 update_option( $post_id, $blockCss );
377 wp_send_json_success( [ 'message' => __( 'Widget CSS Saved', 'the-post-grid' ) ] );
378 }
379
380 $post_id = absint( $post_id );
381 $filename = "rttpg-block-css-{$post_id}.css";
382 $upload_dir_url = wp_upload_dir();
383 $dir = trailingslashit( $upload_dir_url['basedir'] ) . 'rttpg/';
384
385 if ( ! empty( $_POST['has_block'] ) ) {
386
387 update_post_meta( $post_id, '_rttpg_block_active', 1 );
388 $block_css = $this->set_top_css( $blockCss );
389
390 WP_Filesystem( false, $upload_dir_url['basedir'], true );
391 if ( ! $wp_filesystem->is_dir( $dir ) ) {
392 $wp_filesystem->mkdir( $dir );
393
394 }
395 if ( ! $wp_filesystem->put_contents( $dir . $filename, $block_css ) ) {
396 wp_send_json_error( [ 'message' => __( 'CSS can not be saved due to permission!!!', 'the-post-grid' ) ] );
397 }
398 update_post_meta( $post_id, '_rttpg_block_css', $block_css );
399 wp_send_json_success( [ 'message' => __( 'Css file has been updated', 'the-post-grid' ) ] );
400 } else {
401 delete_post_meta( $post_id, '_rttpg_block_active' );
402 if ( file_exists( $dir . $filename ) ) {
403 unlink( $dir . $filename ); //phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
404 }
405 delete_post_meta( $post_id, '_rttpg_block_css' );
406 wp_send_json_success( [ 'message' => __( 'Data Delete Done', 'the-post-grid' ) ] );
407 }
408 } catch ( Exception $e ) {
409 wp_send_json_error( [ 'message' => $e->getMessage() ] );
410 }
411 }
412
413 /**
414 * Save Import CSS in the top of the File
415 *
416 * @param STRING
417 *
418 * @return STRING
419 * @since v.1.0.0
420 */
421 public function set_top_css( $get_css = '' ) {
422 $css_url = "@import url('https://fonts.googleapis.com/css?family=";
423 $font_exists = substr_count( $get_css, $css_url );
424 if ( $font_exists ) {
425 $pattern = sprintf( '/%s(.+?)%s/ims', preg_quote( $css_url, '/' ), preg_quote( "');", '/' ) );
426 if ( preg_match_all( $pattern, $get_css, $matches ) ) {
427 $fonts = $matches[0];
428 $get_css = str_replace( $fonts, '', $get_css );
429 if ( preg_match_all( '/font-weight[ ]?:[ ]?[\d]{3}[ ]?;/', $get_css, $matche_weight ) ) {
430 $weight = array_map(
431 function ( $val ) {
432 $process = trim( str_replace( [ 'font-weight', ':', ';' ], '', $val ) );
433 if ( is_numeric( $process ) ) {
434 return $process;
435 }
436 },
437 $matche_weight[0]
438 );
439 foreach ( $fonts as $key => $val ) {
440 $fonts[ $key ] = str_replace( "');", '', $val ) . ':' . implode( ',', $weight ) . "');";
441 }
442 }
443 $fonts = array_unique( $fonts );
444 $get_css = implode( '', $fonts ) . $get_css;
445 }
446 }
447
448 return $get_css;
449 }
450
451 /**
452 * Save Import CSS in the top of the File
453 *
454 * @return void
455 */
456 public function get_posts_call() {
457 check_ajax_referer( 'rttpg_nonce', 'nonce' );
458
459 $post_id = isset( $_POST['postId'] ) ? absint( $_POST['postId'] ) : 0;
460
461 if ( ! $post_id ) {
462 wp_send_json_error( [ 'message' => __( 'Data not found!!', 'the-post-grid' ) ] );
463 }
464
465 if ( ! current_user_can( 'read_post', $post_id ) ) {
466 wp_send_json_error( [ 'message' => __( 'User permission error', 'the-post-grid' ) ] );
467 }
468
469 $post = get_post( $post_id );
470
471 if ( ! $post ) {
472 wp_send_json_error( [ 'message' => __( 'Data not found!!', 'the-post-grid' ) ] );
473 }
474
475 wp_send_json_success( $post->post_content );
476 }
477
478 /**
479 * Save Import CSS in the top of the File
480 *
481 * @return void
482 * @throws Exception
483 * @since v.1.0.0
484 */
485 public function appended( $server ) {
486 check_ajax_referer( 'rttpg_nonce', 'nonce' );
487
488 $post = $server->get_params();
489 $post_id = isset( $post['post_id'] ) ? absint( $post['post_id'] ) : 0;
490
491 if ( ! $post_id || ! current_user_can( 'edit_post', $post_id ) ) {
492 wp_send_json_error( [ 'message' => __( 'User permission error', 'the-post-grid' ) ] );
493 }
494
495 global $wp_filesystem;
496 if ( ! $wp_filesystem ) {
497 require_once ABSPATH . 'wp-admin/includes/file.php';
498 }
499
500 $css = isset( $post['inner_css'] ) ? wp_strip_all_tags( (string) $post['inner_css'] ) : '';
501
502 $upload_dir_url = wp_upload_dir();
503 $filename = "rttpg-block-css-$post_id.css";
504 $dir = trailingslashit( $upload_dir_url['basedir'] ) . 'rttpg/';
505 WP_Filesystem( false, $upload_dir_url['basedir'], true );
506 if ( ! $wp_filesystem->is_dir( $dir ) ) {
507 $wp_filesystem->mkdir( $dir );
508 }
509 if ( ! $wp_filesystem->put_contents( $dir . $filename, $css ) ) {
510 wp_send_json_error( [ 'message' => __( 'CSS can not be saved due to permission!!!', 'the-post-grid' ) ] );
511 }
512 wp_send_json_success( [ 'message' => __( 'Data retrieve done', 'the-post-grid' ) ] );
513 }
514
515 /**
516 * Return reference id
517 *
518 * @param array $parse_blocks
519 *
520 * @return bool
521 */
522 public function reference_id( $parse_blocks ) {
523 $extra_id = [];
524 if ( ! empty( $parse_blocks ) ) {
525 foreach ( $parse_blocks as $key => $block ) {
526 if ( $block['blockName'] == 'core/block' ) {
527 $extra_id[] = $block['attrs']['ref'];
528 }
529 if ( count( $block['innerBlocks'] ) > 0 ) {
530 $extra_id = array_merge( $this->reference_id( $block['innerBlocks'] ), $extra_id );
531 }
532 }
533 }
534
535 return $extra_id;
536 }
537 }
538