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