PluginProbe ʕ •ᴥ•ʔ
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid / 7.2.7
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid v7.2.7
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 2 years ago Api 2 years ago Blocks 2 years ago Hooks 2 years ago AjaxController.php 2 years ago BlocksController.php 2 years ago ElementorController.php 2 years ago GutenBergController.php 2 years ago PageTemplateController.php 2 years ago ScriptController.php 2 years ago ShortcodeController.php 2 years ago WidgetController.php 2 years ago
BlocksController.php
515 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( __( 'You don\'t have permission to perform this action', 'rttpg' ) );
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( __( 'You don\'t have proper authorization to perform this action', 'rttpg' ) );
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'] = [ 'status' => $status, 'layout_id' => $layout_id ];
80 $layoutRequest = wp_remote_post( $BASE_URL, $post_args );
81 if ( is_wp_error( $layoutRequest ) ) {
82 wp_send_json_error( [ 'messages' => $layoutRequest->get_error_messages() ] );
83 }
84 $layoutData = json_decode( $layoutRequest['body'], true );
85
86 wp_send_json_success( $layoutData );
87 }
88
89 /**
90 * @return void
91 */
92 public function rttpg_get_layouts() {
93 $BASE_URL = "https://www.radiustheme.com/demo/plugins/the-post-grid-gutenberg/wp-json/rttpgapi/v1/layouts/";
94
95 // Verify the request.
96 check_ajax_referer( 'rttpg_nonce', 'nonce' );
97
98 // It's good let's do some capability check.
99 $user = wp_get_current_user();
100 $allowed_roles = [ 'editor', 'administrator', 'author' ];
101
102 if ( ! array_intersect( $allowed_roles, $user->roles ) ) {
103 wp_die( __( 'You don\'t have permission to perform this action', 'rttpg' ) );
104 }
105
106 // Cool, we're almost there, let's check the user authenticity a little bit, shall we!
107 if ( ! is_user_logged_in() && $user->ID !== sanitize_text_field( $_REQUEST['user_id'] ) ) {
108 wp_die( __( 'You don\'t have proper authorization to perform this action', 'rttpg' ) );
109 }
110
111 $status = isset( $_REQUEST['status'] ) ? $_REQUEST['status'] : '';
112 $post_args = [ 'timeout' => 120 ];
113 $post_args['body'] = [ 'status' => $status ];
114 $layoutRequest = wp_remote_post( $BASE_URL, $post_args );
115 if ( is_wp_error( $layoutRequest ) ) {
116 wp_send_json_error( [ 'messages' => $layoutRequest->get_error_messages() ] );
117 }
118 $layoutData = json_decode( $layoutRequest['body'], true );
119
120 wp_send_json_success( $layoutData );
121 }
122
123
124 /**
125 * Block Category Add
126 *
127 * @param $categories
128 * @param $post
129 *
130 * @return array|\string[][]|\void[][]
131 */
132 public function rttpg_block_categories( $categories, $post ) {
133 return array_merge(
134 [
135 [
136 'slug' => 'rttpg',
137 'title' => __( 'The Post Grid', 'the-post-grid' ),
138 ],
139 ],
140 $categories
141 );
142 }
143
144
145 /**
146 * Add Block files foe css
147 *
148 * @return void
149 */
150 public function add_block_css_file() {
151
152 $post_id = get_the_ID();
153 $rttpg_upload_dir = wp_upload_dir()['basedir'] . '/rttpg/';
154 $rttpg_upload_url = wp_upload_dir()['baseurl'] . '/rttpg/';
155 // phpcs:ignore
156 if ( isset( $_GET['preview'] ) && ! empty( $_GET['preview'] ) ) {
157 $css_path = $rttpg_upload_dir . 'rttpg-block-preview.css';
158
159 if ( file_exists( $css_path ) ) {
160 if ( ! $this->is_editor_screen() ) {
161 wp_enqueue_style( 'rttpg-block-post-preview', $rttpg_upload_url . 'rttpg-block-preview.css', false, $this->version );
162 }
163 }
164 } else if ( $post_id ) {
165 $css_dir_path = $rttpg_upload_dir . "rttpg-block-$post_id.css";
166 $css_dir_url = $rttpg_upload_dir . "rttpg-block-$post_id.css";
167
168 if ( file_exists( $css_dir_path ) ) {
169 if ( ! $this->is_editor_screen() ) {
170 wp_enqueue_style( "rttpg-block-post-{$post_id}", $css_dir_url, false, $this->version );
171 }
172 $this->add_reusable_css();
173 } else {
174 // phpcs: ignore
175 wp_register_style( 'rttpg-post-data', false );
176 wp_enqueue_style( 'rttpg-post-data' );
177 wp_add_inline_style( 'rttpg-post-data', get_post_meta( get_the_ID(), '_rttpg_block_css', true ) );
178 }
179 }
180 }
181
182 /**
183 * Common css load
184 * @return void
185 */
186 public function tpg_block_enqueue() {
187
188 if ( ! is_admin() ) {
189 return;
190 }
191
192 wp_enqueue_style( 'rt-fontawsome' );
193 wp_enqueue_style( 'rt-flaticon' );
194 wp_enqueue_style( 'rt-tpg-block' );
195
196 //Custom CSS From Settings
197 $css = isset( $settings['custom_css'] ) ? stripslashes( $settings['custom_css'] ) : null;
198 if ( $css ) {
199 wp_add_inline_style( 'rt-tpg-block', $css );
200 }
201 }
202
203
204 /**
205 * Determine if wppb editor is open
206 *
207 * @return bool
208 *
209 * @since V.1.0.0
210 * @since v.1.0.0
211 */
212 private function is_editor_screen() {
213 if ( ! empty( $_GET['action'] ) && 'wppb_editor' === $_GET['action'] ) {
214 return true;
215 }
216
217 return false;
218 }
219
220
221 /**
222 * Load Editor Assets
223 * @return void
224 */
225 public function editor_assets() {
226
227 //Block editor css
228 wp_enqueue_style( 'rttpg-block-admin-css', rtTPG()->get_assets_uri( 'css/admin/block-admin.css' ), '', $this->version );
229
230 //Main compile css and js file
231 wp_enqueue_style( 'rttpg-blocks-css', rtTPG()->get_assets_uri( 'blocks/main.css' ), '', $this->version );
232 wp_enqueue_script( 'rttpg-blocks-js', rtTPG()->get_assets_uri( 'blocks/main.js' ), [
233 'wp-block-editor',
234 'wp-blocks',
235 'wp-components',
236 'wp-element',
237 'wp-i18n',
238 ], $this->version, true );
239 $all_dependencies = [
240 'lodash',
241 'wp-i18n',
242 'wp-element',
243 'wp-hooks',
244 'wp-util',
245 'wp-components',
246 'wp-blocks',
247 'wp-data',
248 'wp-editor',
249 'wp-block-editor',
250 ];
251 global $pagenow;
252 $editor_type = 'edit-post';
253
254 if ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) {
255 $all_dependencies[] = 'wp-edit-post';
256 }
257
258 if ( 'site-editor.php' === $pagenow ) {
259 $all_dependencies[] = 'wp-edit-site';
260 $editor_type = 'edit-site';
261 }
262
263 $get_tax_object = get_taxonomies( [], 'objects' );
264 $exclude_tax = Fns::get_excluded_taxonomy();
265 foreach ( $exclude_tax as $_tax ) {
266 unset( $get_tax_object[ $_tax ] );
267 }
268
269 $settings = get_option( rtTPG()->options['settings'] );
270 $ssList = ! empty( $settings['social_share_items'] ) ? $settings['social_share_items'] : [];
271
272 wp_localize_script( 'rttpg-blocks-js', 'rttpgParams', [
273 'editor_type' => $editor_type,
274 'nonce' => wp_create_nonce( 'rttpg_nonce' ),
275 'ajaxurl' => admin_url( 'admin-ajax.php' ),
276 'site_url' => site_url(),
277 'admin_url' => admin_url(),
278 'plugin_url' => RT_THE_POST_GRID_PLUGIN_URL,
279 'plugin_pro_url' => rtTPG()->getProPath(),
280 'post_type' => Fns::get_post_types(),
281 'all_term_list' => Fns::get_all_taxonomy_guten(),
282 'get_taxonomies' => $get_tax_object,
283 'get_users' => Fns::rt_get_users(),
284 'hasPro' => rtTPG()->hasPro(),
285 'pageTitle' => get_the_title(),
286 'hasWoo' => Fns::is_woocommerce(),
287 'hasAcf' => Fns::is_acf(),
288 'ssList' => $ssList,
289 'current_user_id' => get_current_user_id(),
290 'disableImportButton' => apply_filters( 'rttpg_disable_gutenberg_import_button', 'no' ),
291 'iconFont' => Fns::tpg_option( 'tpg_icon_font' )
292 ]
293 );
294
295 }
296
297
298 /**
299 * Add inLine css for page or post
300 *
301 * @return void
302 */
303 public function add_block_inline_css() {
304
305 $post_id = get_the_ID();
306 if ( $post_id ) {
307 $rttpg_upload_dir = wp_upload_dir()['basedir'] . '/rttpg/';
308 $css_dir_path = $rttpg_upload_dir . "rttpg-block-$post_id.css";
309 if ( file_exists( $css_dir_path ) ) {
310 $blockCss = file_get_contents( $css_dir_path );
311 echo '<style>' . sanitize_textarea_field( $blockCss ) . '</style>';
312 } else if ( $metaCss = get_post_meta( $post_id, '_rttpg_block_css', true ) ) {
313 echo '<style>' . sanitize_textarea_field( $metaCss ) . '</style>';
314 }
315 }
316
317 $this->add_reusable_css();
318
319 }
320
321
322 /**
323 * Add reusable css
324 */
325 public function add_reusable_css() {
326 $post_id = get_the_ID();
327 $rttpg_upload_dir = wp_upload_dir()['basedir'] . '/rttpg/';
328 $rttpg_upload_url = wp_upload_dir()['baseurl'] . '/rttpg/';
329 if ( $post_id ) {
330 $content_post = get_post( $post_id );
331 if ( isset( $content_post->post_content ) ) {
332 $content = $content_post->post_content;
333 $parse_blocks = parse_blocks( $content );
334 $css_id = $this->reference_id( $parse_blocks );
335 if ( is_array( $css_id ) ) {
336 if ( ! empty( $css_id ) ) {
337 $css_id = array_unique( $css_id );
338 foreach ( $css_id as $value ) {
339 $css_dir_path = $rttpg_upload_dir . "rttpg-block-$value.css";
340 if ( file_exists( $css_dir_path ) ) {
341 wp_enqueue_style( "rttpg-block-{$value}", $rttpg_upload_url . "rttpg-block-{$value}.css", false, RTTPG_VERSION );
342 }
343 }
344 }
345 }
346 }
347 }
348 }
349
350 /**
351 * Save Import CSS in the top of the File
352 *
353 * @return void Array of the Custom Message
354 */
355 public function save_block_css() {
356
357 try {
358 if ( ! current_user_can( 'edit_posts' ) ) {
359 throw new Exception( __( 'User permission error', 'the-post-grid' ) );
360 }
361 global $wp_filesystem;
362 if ( ! $wp_filesystem ) {
363 require_once( ABSPATH . 'wp-admin/includes/file.php' );
364 }
365
366 $post_id = ! empty( $_POST['post_id'] ) ? sanitize_text_field( $_POST['post_id'] ) : '';
367 $blockCss = ! empty( $_POST['block_css'] ) ? sanitize_text_field( $_POST['block_css'] ) : '';
368
369 if ( $post_id == 'rttpg-widget' && isset( $_POST['has_block'] ) ) {
370 update_option( $post_id, $blockCss );
371 wp_send_json_success( [ 'message' => __( 'Widget CSS Saved', 'the-post-grid' ) ] );
372 }
373
374 $post_id = absint( $post_id );
375 $filename = "rttpg-block-css-{$post_id}.css";
376 $upload_dir_url = wp_upload_dir();
377 $dir = trailingslashit( $upload_dir_url['basedir'] ) . 'rttpg/';
378
379 if ( ! empty( $_POST['has_block'] ) ) {
380
381 update_post_meta( $post_id, '_rttpg_block_active', 1 );
382 $block_css = $this->set_top_css( $blockCss );
383
384 WP_Filesystem( false, $upload_dir_url['basedir'], true );
385 if ( ! $wp_filesystem->is_dir( $dir ) ) {
386 $wp_filesystem->mkdir( $dir );
387
388 }
389 if ( ! $wp_filesystem->put_contents( $dir . $filename, $block_css ) ) {
390 wp_send_json_error( [ 'message' => __( 'CSS can not be saved due to permission!!!', 'the-post-grid' ) ] );
391 }
392 update_post_meta( $post_id, '_rttpg_block_css', $block_css );
393 wp_send_json_success( [ 'message' => __( 'Css file has been updated', 'the-post-grid' ) ] );
394 } else {
395 delete_post_meta( $post_id, '_rttpg_block_active' );
396 if ( file_exists( $dir . $filename ) ) {
397 unlink( $dir . $filename );
398 }
399 delete_post_meta( $post_id, '_rttpg_block_css' );
400 wp_send_json_success( [ 'message' => __( 'Data Delete Done', 'the-post-grid' ) ] );
401 }
402 } catch ( Exception $e ) {
403 wp_send_json_error( [ 'message' => $e->getMessage() ] );
404 }
405 }
406
407 /**
408 * Save Import CSS in the top of the File
409 *
410 * @param STRING
411 *
412 * @return STRING
413 * @since v.1.0.0
414 */
415 public function set_top_css( $get_css = '' ) {
416 $css_url = "@import url('https://fonts.googleapis.com/css?family=";
417 $font_exists = substr_count( $get_css, $css_url );
418 if ( $font_exists ) {
419 $pattern = sprintf( '/%s(.+?)%s/ims', preg_quote( $css_url, '/' ), preg_quote( "');", '/' ) );
420 if ( preg_match_all( $pattern, $get_css, $matches ) ) {
421 $fonts = $matches[0];
422 $get_css = str_replace( $fonts, '', $get_css );
423 if ( preg_match_all( '/font-weight[ ]?:[ ]?[\d]{3}[ ]?;/', $get_css, $matche_weight ) ) {
424 $weight = array_map( function ( $val ) {
425 $process = trim( str_replace( [ 'font-weight', ':', ';' ], '', $val ) );
426 if ( is_numeric( $process ) ) {
427 return $process;
428 }
429 }, $matche_weight[0] );
430 foreach ( $fonts as $key => $val ) {
431 $fonts[ $key ] = str_replace( "');", '', $val ) . ':' . implode( ',', $weight ) . "');";
432 }
433 }
434 $fonts = array_unique( $fonts );
435 $get_css = implode( '', $fonts ) . $get_css;
436 }
437 }
438
439 return $get_css;
440 }
441
442 /**
443 * Save Import CSS in the top of the File
444 *
445 * @return void
446 */
447 public function get_posts_call() {
448 $post_id = absint( $_POST['postId'] );
449 if ( $post_id ) {
450 wp_send_json_success( get_post( $post_id )->post_content );
451 } else {
452 wp_send_json_error( new WP_Error( 'rttpg_block_data_not_found', __( 'Data not found!!', 'the-post-grid' ) ) );
453 }
454 }
455
456 /**
457 * Save Import CSS in the top of the File
458 *
459 *
460 * @return void
461 * @throws Exception
462 * @since v.1.0.0
463 */
464 public function appended( $server ) {
465 if ( ! current_user_can( 'edit_posts' ) ) {
466 wp_send_json_success( new WP_Error( 'rttpg_block_user_permission', __( 'User permission error', 'the-post-grid' ) ) );
467 }
468 global $wp_filesystem;
469 if ( ! $wp_filesystem ) {
470 require_once( ABSPATH . 'wp-admin/includes/file.php' );
471 }
472 $post = $server->get_params();
473 $css = $post['inner_css'];
474 $post_id = (int) sanitize_text_field( $post['post_id'] );
475
476 if ( $post_id ) {
477 $upload_dir_url = wp_upload_dir();
478 $filename = "rttpg-block-css-$post_id.css";
479 $dir = trailingslashit( $upload_dir_url['basedir'] ) . 'rttpg/';
480 WP_Filesystem( false, $upload_dir_url['basedir'], true );
481 if ( ! $wp_filesystem->is_dir( $dir ) ) {
482 $wp_filesystem->mkdir( $dir );
483 }
484 if ( ! $wp_filesystem->put_contents( $dir . $filename, $css ) ) {
485 wp_send_json_error( [ 'message' => __( 'CSS can not be saved due to permission!!!', 'the-post-grid' ) ] );
486 }
487 wp_send_json_success( [ 'message' => __( 'Data retrieve done', 'the-post-grid' ) ] );
488 } else {
489 wp_send_json_error( [ 'message' => __( 'Data not found!!', 'the-post-grid' ) ] );
490 }
491 }
492
493 /**
494 * Return reference id
495 *
496 * @param array $parse_blocks
497 *
498 * @return bool
499 */
500 public function reference_id( $parse_blocks ) {
501 $extra_id = [];
502 if ( ! empty( $parse_blocks ) ) {
503 foreach ( $parse_blocks as $key => $block ) {
504 if ( $block['blockName'] == 'core/block' ) {
505 $extra_id[] = $block['attrs']['ref'];
506 }
507 if ( count( $block['innerBlocks'] ) > 0 ) {
508 $extra_id = array_merge( $this->reference_id( $block['innerBlocks'] ), $extra_id );
509 }
510 }
511 }
512
513 return $extra_id;
514 }
515 }