PluginProbe
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid / 7.4.1
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid v7.4.1
7.9.4 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 All 87 releases
the-post-grid / app / Controllers / BlocksController.php

BlocksController.php in The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid 7.4.1, at app/Controllers/BlocksController.php

503 lines 16.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
240 global $pagenow;
241 $editor_type = 'edit-post';
242
243 if ( 'site-editor.php' === $pagenow ) {
244 $editor_type = 'edit-site';
245 }
246
247 $get_tax_object = get_taxonomies( [], 'objects' );
248 $exclude_tax = Fns::get_excluded_taxonomy();
249 foreach ( $exclude_tax as $_tax ) {
250 unset( $get_tax_object[ $_tax ] );
251 }
252
253 $settings = get_option( rtTPG()->options['settings'] );
254 $ssList = ! empty( $settings['social_share_items'] ) ? $settings['social_share_items'] : [];
255
256 wp_localize_script( 'rttpg-blocks-js', 'rttpgParams', [
257 'editor_type' => $editor_type,
258 'nonce' => wp_create_nonce( 'rttpg_nonce' ),
259 'ajaxurl' => admin_url( 'admin-ajax.php' ),
260 'site_url' => site_url(),
261 'admin_url' => admin_url(),
262 'plugin_url' => RT_THE_POST_GRID_PLUGIN_URL,
263 'plugin_pro_url' => rtTPG()->getProPath(),
264 'post_type' => Fns::get_post_types(),
265 'all_term_list' => Fns::get_all_taxonomy_guten(),
266 'get_taxonomies' => $get_tax_object,
267 'get_users' => Fns::rt_get_users(),
268 'hasPro' => rtTPG()->hasPro(),
269 'pageTitle' => get_the_title(),
270 'hasWoo' => Fns::is_woocommerce(),
271 'hasAcf' => Fns::is_acf(),
272 'ssList' => $ssList,
273 'current_user_id' => get_current_user_id(),
274 'disableImportButton' => apply_filters( 'rttpg_disable_gutenberg_import_button', 'no' ),
275 'iconFont' => Fns::tpg_option( 'tpg_icon_font' )
276 ]
277 );
278
279 }
280
281
282 /**
283 * Add inLine css for page or post
284 *
285 * @return void
286 */
287 public function add_block_inline_css() {
288
289 $post_id = apply_filters('tpg_page_id_for_block_css',get_the_ID());
290
291 if ( $post_id ) {
292 $rttpg_upload_dir = wp_upload_dir()['basedir'] . '/rttpg/';
293 $css_dir_path = $rttpg_upload_dir . "rttpg-block-$post_id.css";
294 if ( file_exists( $css_dir_path ) ) {
295 $blockCss = file_get_contents( $css_dir_path );
296 echo '<style>' . sanitize_textarea_field( $blockCss ) . '</style>';
297 } else if ( $metaCss = get_post_meta( $post_id, '_rttpg_block_css', true ) ) {
298 echo '<style>' . sanitize_textarea_field( $metaCss ) . '</style>';
299 }
300 }
301
302 $this->add_reusable_css();
303
304 }
305
306
307 /**
308 * Add reusable css
309 */
310 public function add_reusable_css() {
311 $post_id = get_the_ID();
312 $rttpg_upload_dir = wp_upload_dir()['basedir'] . '/rttpg/';
313 $rttpg_upload_url = wp_upload_dir()['baseurl'] . '/rttpg/';
314 if ( $post_id ) {
315 $content_post = get_post( $post_id );
316 if ( isset( $content_post->post_content ) ) {
317 $content = $content_post->post_content;
318 $parse_blocks = parse_blocks( $content );
319 $css_id = $this->reference_id( $parse_blocks );
320 if ( is_array( $css_id ) ) {
321 if ( ! empty( $css_id ) ) {
322 $css_id = array_unique( $css_id );
323 foreach ( $css_id as $value ) {
324 $css_dir_path = $rttpg_upload_dir . "rttpg-block-$value.css";
325 if ( file_exists( $css_dir_path ) ) {
326 wp_enqueue_style( "rttpg-block-{$value}", $rttpg_upload_url . "rttpg-block-{$value}.css", false, RTTPG_VERSION );
327 }
328 }
329 }
330 }
331 }
332 }
333 }
334
335 /**
336 * Save Import CSS in the top of the File
337 *
338 * @return void Array of the Custom Message
339 */
340 public function save_block_css() {
341
342 try {
343 if ( ! current_user_can( 'edit_posts' ) ) {
344 throw new Exception( __( 'User permission error', 'the-post-grid' ) );
345 }
346 check_ajax_referer( 'rttpg_nonce', 'nonce' );
347 global $wp_filesystem;
348 if ( ! $wp_filesystem ) {
349 require_once( ABSPATH . 'wp-admin/includes/file.php' );
350 }
351
352 $post_id = ! empty( $_POST['post_id'] ) ? sanitize_text_field( $_POST['post_id'] ) : '';
353 $blockCss = ! empty( $_POST['block_css'] ) ? sanitize_text_field( $_POST['block_css'] ) : '';
354
355 if ( $post_id == 'rttpg-widget' && isset( $_POST['has_block'] ) ) {
356 update_option( $post_id, $blockCss );
357 wp_send_json_success( [ 'message' => __( 'Widget CSS Saved', 'the-post-grid' ) ] );
358 }
359
360 $post_id = absint( $post_id );
361 $filename = "rttpg-block-css-{$post_id}.css";
362 $upload_dir_url = wp_upload_dir();
363 $dir = trailingslashit( $upload_dir_url['basedir'] ) . 'rttpg/';
364
365 if ( ! empty( $_POST['has_block'] ) ) {
366
367 update_post_meta( $post_id, '_rttpg_block_active', 1 );
368 $block_css = $this->set_top_css( $blockCss );
369
370 WP_Filesystem( false, $upload_dir_url['basedir'], true );
371 if ( ! $wp_filesystem->is_dir( $dir ) ) {
372 $wp_filesystem->mkdir( $dir );
373
374 }
375 if ( ! $wp_filesystem->put_contents( $dir . $filename, $block_css ) ) {
376 wp_send_json_error( [ 'message' => __( 'CSS can not be saved due to permission!!!', 'the-post-grid' ) ] );
377 }
378 update_post_meta( $post_id, '_rttpg_block_css', $block_css );
379 wp_send_json_success( [ 'message' => __( 'Css file has been updated', 'the-post-grid' ) ] );
380 } else {
381 delete_post_meta( $post_id, '_rttpg_block_active' );
382 if ( file_exists( $dir . $filename ) ) {
383 unlink( $dir . $filename );
384 }
385 delete_post_meta( $post_id, '_rttpg_block_css' );
386 wp_send_json_success( [ 'message' => __( 'Data Delete Done', 'the-post-grid' ) ] );
387 }
388 } catch ( Exception $e ) {
389 wp_send_json_error( [ 'message' => $e->getMessage() ] );
390 }
391 }
392
393 /**
394 * Save Import CSS in the top of the File
395 *
396 * @param STRING
397 *
398 * @return STRING
399 * @since v.1.0.0
400 */
401 public function set_top_css( $get_css = '' ) {
402 $css_url = "@import url('https://fonts.googleapis.com/css?family=";
403 $font_exists = substr_count( $get_css, $css_url );
404 if ( $font_exists ) {
405 $pattern = sprintf( '/%s(.+?)%s/ims', preg_quote( $css_url, '/' ), preg_quote( "');", '/' ) );
406 if ( preg_match_all( $pattern, $get_css, $matches ) ) {
407 $fonts = $matches[0];
408 $get_css = str_replace( $fonts, '', $get_css );
409 if ( preg_match_all( '/font-weight[ ]?:[ ]?[\d]{3}[ ]?;/', $get_css, $matche_weight ) ) {
410 $weight = array_map( function ( $val ) {
411 $process = trim( str_replace( [ 'font-weight', ':', ';' ], '', $val ) );
412 if ( is_numeric( $process ) ) {
413 return $process;
414 }
415 }, $matche_weight[0] );
416 foreach ( $fonts as $key => $val ) {
417 $fonts[ $key ] = str_replace( "');", '', $val ) . ':' . implode( ',', $weight ) . "');";
418 }
419 }
420 $fonts = array_unique( $fonts );
421 $get_css = implode( '', $fonts ) . $get_css;
422 }
423 }
424
425 return $get_css;
426 }
427
428 /**
429 * Save Import CSS in the top of the File
430 *
431 * @return void
432 */
433 public function get_posts_call() {
434 $post_id = absint( $_POST['postId'] );
435 check_ajax_referer( 'rttpg_nonce', 'nonce' );
436 if ( $post_id ) {
437 wp_send_json_success( get_post( $post_id )->post_content );
438 } else {
439 wp_send_json_error( new WP_Error( 'rttpg_block_data_not_found', __( 'Data not found!!', 'the-post-grid' ) ) );
440 }
441 }
442
443 /**
444 * Save Import CSS in the top of the File
445 *
446 *
447 * @return void
448 * @throws Exception
449 * @since v.1.0.0
450 */
451 public function appended( $server ) {
452 if ( ! current_user_can( 'edit_posts' ) ) {
453 wp_send_json_success( new WP_Error( 'rttpg_block_user_permission', __( 'User permission error', 'the-post-grid' ) ) );
454 }
455 check_ajax_referer( 'rttpg_nonce', 'nonce' );
456 global $wp_filesystem;
457 if ( ! $wp_filesystem ) {
458 require_once( ABSPATH . 'wp-admin/includes/file.php' );
459 }
460 $post = $server->get_params();
461 $css = $post['inner_css'];
462 $post_id = (int) sanitize_text_field( $post['post_id'] );
463
464 if ( $post_id ) {
465 $upload_dir_url = wp_upload_dir();
466 $filename = "rttpg-block-css-$post_id.css";
467 $dir = trailingslashit( $upload_dir_url['basedir'] ) . 'rttpg/';
468 WP_Filesystem( false, $upload_dir_url['basedir'], true );
469 if ( ! $wp_filesystem->is_dir( $dir ) ) {
470 $wp_filesystem->mkdir( $dir );
471 }
472 if ( ! $wp_filesystem->put_contents( $dir . $filename, $css ) ) {
473 wp_send_json_error( [ 'message' => __( 'CSS can not be saved due to permission!!!', 'the-post-grid' ) ] );
474 }
475 wp_send_json_success( [ 'message' => __( 'Data retrieve done', 'the-post-grid' ) ] );
476 } else {
477 wp_send_json_error( [ 'message' => __( 'Data not found!!', 'the-post-grid' ) ] );
478 }
479 }
480
481 /**
482 * Return reference id
483 *
484 * @param array $parse_blocks
485 *
486 * @return bool
487 */
488 public function reference_id( $parse_blocks ) {
489 $extra_id = [];
490 if ( ! empty( $parse_blocks ) ) {
491 foreach ( $parse_blocks as $key => $block ) {
492 if ( $block['blockName'] == 'core/block' ) {
493 $extra_id[] = $block['attrs']['ref'];
494 }
495 if ( count( $block['innerBlocks'] ) > 0 ) {
496 $extra_id = array_merge( $this->reference_id( $block['innerBlocks'] ), $extra_id );
497 }
498 }
499 }
500
501 return $extra_id;
502 }
503 }