PluginProbe ʕ •ᴥ•ʔ
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid / 7.0.1
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid v7.0.1
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
510 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-tpg-block' );
191
192 //Custom CSS From Settings
193 $css = isset( $settings['custom_css'] ) ? stripslashes( $settings['custom_css'] ) : null;
194 if ( $css ) {
195 wp_add_inline_style( 'rt-tpg-block', $css );
196 }
197 }
198
199
200 /**
201 * Determine if wppb editor is open
202 *
203 * @return bool
204 *
205 * @since V.1.0.0
206 * @since v.1.0.0
207 */
208 private function is_editor_screen() {
209 if ( ! empty( $_GET['action'] ) && 'wppb_editor' === $_GET['action'] ) {
210 return true;
211 }
212
213 return false;
214 }
215
216
217 /**
218 * Load Editor Assets
219 * @return void
220 */
221 public function editor_assets() {
222
223 //Block editor css
224 wp_enqueue_style( 'rttpg-block-admin-css', rtTPG()->get_assets_uri( 'css/admin/block-admin.css' ), '', $this->version );
225
226 //Main compile css and js file
227 wp_enqueue_style( 'rttpg-blocks-css', rtTPG()->get_assets_uri( 'blocks/main.css' ), '', $this->version );
228 wp_enqueue_script( 'rttpg-blocks-js', rtTPG()->get_assets_uri( 'blocks/main.js' ), [
229 'wp-block-editor',
230 'wp-blocks',
231 'wp-components',
232 'wp-element',
233 'wp-i18n',
234 ], $this->version, true );
235 $all_dependencies = [
236 'lodash',
237 'wp-i18n',
238 'wp-element',
239 'wp-hooks',
240 'wp-util',
241 'wp-components',
242 'wp-blocks',
243 'wp-data',
244 'wp-editor',
245 'wp-block-editor',
246 ];
247 global $pagenow;
248 $editor_type = 'edit-post';
249
250 if ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) {
251 $all_dependencies[] = 'wp-edit-post';
252 }
253
254 if ( 'site-editor.php' === $pagenow ) {
255 $all_dependencies[] = 'wp-edit-site';
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
268 wp_localize_script( 'rttpg-blocks-js', 'rttpgParams', [
269 'editor_type' => $editor_type,
270 'nonce' => wp_create_nonce( 'rttpg_nonce' ),
271 'ajaxurl' => admin_url( 'admin-ajax.php' ),
272 'site_url' => site_url(),
273 'admin_url' => admin_url(),
274 'plugin_url' => RT_THE_POST_GRID_PLUGIN_URL,
275 'plugin_pro_url' => rtTPG()->getProPath(),
276 'post_type' => Fns::get_post_types(),
277 'all_term_list' => Fns::get_all_taxonomy_guten(),
278 'get_taxonomies' => $get_tax_object,
279 'get_users' => Fns::rt_get_users(),
280 'hasPro' => rtTPG()->hasPro(),
281 'pageTitle' => get_the_title(),
282 'hasWoo' => Fns::is_woocommerce(),
283 'hasAcf' => Fns::is_acf(),
284 'ssList' => $ssList,
285 'current_user_id' => get_current_user_id(),
286 'disableImportButton' => apply_filters('rttpg_disable_gutenberg_import_button', 'no') //Send 'yes' if you would like to remove the button for gutenberg
287 ]
288 );
289
290 }
291
292
293 /**
294 * Add inLine css for page or post
295 *
296 * @return void
297 */
298 public function add_block_inline_css() {
299
300 $post_id = get_the_ID();
301 if ( $post_id ) {
302 $rttpg_upload_dir = wp_upload_dir()['basedir'] . '/rttpg/';
303 $css_dir_path = $rttpg_upload_dir . "rttpg-block-$post_id.css";
304 if ( file_exists( $css_dir_path ) ) {
305 $blockCss = file_get_contents( $css_dir_path );
306 echo '<style>' . sanitize_textarea_field( $blockCss ) . '</style>';
307 } else if ( $metaCss = get_post_meta( $post_id, '_rttpg_block_css', true ) ) {
308 echo '<style>' . sanitize_textarea_field( $metaCss ) . '</style>';
309 }
310 }
311
312 $this->add_reusable_css();
313
314 }
315
316
317 /**
318 * Add reusable css
319 */
320 public function add_reusable_css() {
321 $post_id = get_the_ID();
322 $rttpg_upload_dir = wp_upload_dir()['basedir'] . '/rttpg/';
323 $rttpg_upload_url = wp_upload_dir()['baseurl'] . '/rttpg/';
324 if ( $post_id ) {
325 $content_post = get_post( $post_id );
326 if ( isset( $content_post->post_content ) ) {
327 $content = $content_post->post_content;
328 $parse_blocks = parse_blocks( $content );
329 $css_id = $this->reference_id( $parse_blocks );
330 if ( is_array( $css_id ) ) {
331 if ( ! empty( $css_id ) ) {
332 $css_id = array_unique( $css_id );
333 foreach ( $css_id as $value ) {
334 $css_dir_path = $rttpg_upload_dir . "rttpg-block-$value.css";
335 if ( file_exists( $css_dir_path ) ) {
336 wp_enqueue_style( "rttpg-block-{$value}", $rttpg_upload_url . "rttpg-block-{$value}.css", false, RTTPG_VERSION );
337 }
338 }
339 }
340 }
341 }
342 }
343 }
344
345 /**
346 * Save Import CSS in the top of the File
347 *
348 * @return void Array of the Custom Message
349 */
350 public function save_block_css() {
351
352 try {
353 if ( ! current_user_can( 'edit_posts' ) ) {
354 throw new Exception( __( 'User permission error', 'the-post-grid' ) );
355 }
356 global $wp_filesystem;
357 if ( ! $wp_filesystem ) {
358 require_once( ABSPATH . 'wp-admin/includes/file.php' );
359 }
360
361 $post_id = ! empty( $_POST['post_id'] ) ? sanitize_text_field( $_POST['post_id'] ) : '';
362 $blockCss = ! empty( $_POST['block_css'] ) ? sanitize_text_field( $_POST['block_css'] ) : '';
363
364 if ( $post_id == 'rttpg-widget' && isset( $_POST['has_block'] ) ) {
365 update_option( $post_id, $blockCss );
366 wp_send_json_success( [ 'message' => __( 'Widget CSS Saved', 'the-post-grid' ) ] );
367 }
368
369 $post_id = absint( $post_id );
370 $filename = "rttpg-block-css-{$post_id}.css";
371 $upload_dir_url = wp_upload_dir();
372 $dir = trailingslashit( $upload_dir_url['basedir'] ) . 'rttpg/';
373
374 if ( ! empty( $_POST['has_block'] ) ) {
375
376 update_post_meta( $post_id, '_rttpg_block_active', 1 );
377 $block_css = $this->set_top_css( $blockCss );
378
379 WP_Filesystem( false, $upload_dir_url['basedir'], true );
380 if ( ! $wp_filesystem->is_dir( $dir ) ) {
381 $wp_filesystem->mkdir( $dir );
382
383 }
384 if ( ! $wp_filesystem->put_contents( $dir . $filename, $block_css ) ) {
385 wp_send_json_error( [ 'message' => __( 'CSS can not be saved due to permission!!!', 'the-post-grid' ) ] );
386 }
387 update_post_meta( $post_id, '_rttpg_block_css', $block_css );
388 wp_send_json_success( [ 'message' => __( 'Css file has been updated', 'the-post-grid' ) ] );
389 } else {
390 delete_post_meta( $post_id, '_rttpg_block_active' );
391 if ( file_exists( $dir . $filename ) ) {
392 unlink( $dir . $filename );
393 }
394 delete_post_meta( $post_id, '_rttpg_block_css' );
395 wp_send_json_success( [ 'message' => __( 'Data Delete Done', 'the-post-grid' ) ] );
396 }
397 } catch ( Exception $e ) {
398 wp_send_json_error( [ 'message' => $e->getMessage() ] );
399 }
400 }
401
402 /**
403 * Save Import CSS in the top of the File
404 *
405 * @param STRING
406 *
407 * @return STRING
408 * @since v.1.0.0
409 */
410 public function set_top_css( $get_css = '' ) {
411 $css_url = "@import url('https://fonts.googleapis.com/css?family=";
412 $font_exists = substr_count( $get_css, $css_url );
413 if ( $font_exists ) {
414 $pattern = sprintf( '/%s(.+?)%s/ims', preg_quote( $css_url, '/' ), preg_quote( "');", '/' ) );
415 if ( preg_match_all( $pattern, $get_css, $matches ) ) {
416 $fonts = $matches[0];
417 $get_css = str_replace( $fonts, '', $get_css );
418 if ( preg_match_all( '/font-weight[ ]?:[ ]?[\d]{3}[ ]?;/', $get_css, $matche_weight ) ) {
419 $weight = array_map( function ( $val ) {
420 $process = trim( str_replace( [ 'font-weight', ':', ';' ], '', $val ) );
421 if ( is_numeric( $process ) ) {
422 return $process;
423 }
424 }, $matche_weight[0] );
425 foreach ( $fonts as $key => $val ) {
426 $fonts[ $key ] = str_replace( "');", '', $val ) . ':' . implode( ',', $weight ) . "');";
427 }
428 }
429 $fonts = array_unique( $fonts );
430 $get_css = implode( '', $fonts ) . $get_css;
431 }
432 }
433
434 return $get_css;
435 }
436
437 /**
438 * Save Import CSS in the top of the File
439 *
440 * @return void
441 */
442 public function get_posts_call() {
443 $post_id = absint( $_POST['postId'] );
444 if ( $post_id ) {
445 wp_send_json_success( get_post( $post_id )->post_content );
446 } else {
447 wp_send_json_error( new WP_Error( 'rttpg_block_data_not_found', __( 'Data not found!!', 'the-post-grid' ) ) );
448 }
449 }
450
451 /**
452 * Save Import CSS in the top of the File
453 *
454 *
455 * @return void
456 * @throws Exception
457 * @since v.1.0.0
458 */
459 public function appended( $server ) {
460 if ( ! current_user_can( 'edit_posts' ) ) {
461 wp_send_json_success( new WP_Error( 'rttpg_block_user_permission', __( 'User permission error', 'the-post-grid' ) ) );
462 }
463 global $wp_filesystem;
464 if ( ! $wp_filesystem ) {
465 require_once( ABSPATH . 'wp-admin/includes/file.php' );
466 }
467 $post = $server->get_params();
468 $css = $post['inner_css'];
469 $post_id = (int) sanitize_text_field( $post['post_id'] );
470
471 if ( $post_id ) {
472 $upload_dir_url = wp_upload_dir();
473 $filename = "rttpg-block-css-$post_id.css";
474 $dir = trailingslashit( $upload_dir_url['basedir'] ) . 'rttpg/';
475 WP_Filesystem( false, $upload_dir_url['basedir'], true );
476 if ( ! $wp_filesystem->is_dir( $dir ) ) {
477 $wp_filesystem->mkdir( $dir );
478 }
479 if ( ! $wp_filesystem->put_contents( $dir . $filename, $css ) ) {
480 wp_send_json_error( [ 'message' => __( 'CSS can not be saved due to permission!!!', 'the-post-grid' ) ] );
481 }
482 wp_send_json_success( [ 'message' => __( 'Data retrieve done', 'the-post-grid' ) ] );
483 } else {
484 wp_send_json_error( [ 'message' => __( 'Data not found!!', 'the-post-grid' ) ] );
485 }
486 }
487
488 /**
489 * Return reference id
490 *
491 * @param array $parse_blocks
492 *
493 * @return bool
494 */
495 public function reference_id( $parse_blocks ) {
496 $extra_id = [];
497 if ( ! empty( $parse_blocks ) ) {
498 foreach ( $parse_blocks as $key => $block ) {
499 if ( $block['blockName'] == 'core/block' ) {
500 $extra_id[] = $block['attrs']['ref'];
501 }
502 if ( count( $block['innerBlocks'] ) > 0 ) {
503 $extra_id = array_merge( $this->reference_id( $block['innerBlocks'] ), $extra_id );
504 }
505 }
506 }
507
508 return $extra_id;
509 }
510 }