PluginProbe ʕ •ᴥ•ʔ
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid / 5.0.3
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid v5.0.3
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
437 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
9 // use RT\ThePostGrid\Controllers\Blocks\SliderLayout;
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 SliderLayout();
26
27 $this->version = defined( 'WP_DEBUG' ) && WP_DEBUG ? time() : RT_THE_POST_GRID_VERSION;
28 add_action( 'enqueue_block_editor_assets', [ $this, 'editor_assets' ] );
29
30 //All css/js file load in back-end and front-end
31 add_action( 'wp_enqueue_scripts', [ $this, 'tpg_block_enqueue' ] );
32 add_action( 'enqueue_block_editor_assets', [ $this, 'tpg_block_enqueue' ] );
33
34 if ( version_compare( get_bloginfo( 'version' ), '5.8', '>=' ) ) {
35 add_filter( 'block_categories_all', array( $this, 'rttpg_block_categories' ), 1, 2 );
36 } else {
37 add_filter( 'block_categories', array( $this, 'rttpg_block_categories' ), 1, 2 );
38 }
39
40 add_action( 'wp_ajax_rttpg_block_css_save', [ $this, 'save_block_css' ] );
41 add_action( 'wp_ajax_rttpg_block_css_get_posts', [ $this, 'get_posts_call' ] );
42 add_action( 'wp_ajax_rttpg_block_css_appended', [ $this, 'appended' ] );
43
44 // Decide how css file will be loaded. default filesystem eg: filesystem or at header
45 $option_data = get_option( 'rttpg_options' );
46 if ( isset( $option_data['css_save_as'] ) && 'filesystem' === $option_data['css_save_as'] ) {
47 add_action( 'wp_enqueue_scripts', [ $this, 'add_block_css_file' ] );
48 } else {
49 add_action( 'wp_head', [ $this, 'add_block_inline_css' ], 100 );
50 }
51 }
52
53 /**
54 * Block Category Add
55 *
56 * @param $categories
57 * @param $post
58 *
59 * @return array|\string[][]|\void[][]
60 */
61 public function rttpg_block_categories( $categories, $post ) {
62 return array_merge(
63 array(
64 array(
65 'slug' => 'rttpg',
66 'title' => __( 'The Post Grid', 'the-post-grid' ),
67 ),
68 ),
69 $categories
70 );
71 }
72
73
74 /**
75 * Add Block files foe css
76 *
77 * @return void
78 */
79 public function add_block_css_file() {
80 $post_id = get_the_ID();
81 $rttpg_upload_dir = wp_upload_dir()['basedir'] . '/rttpg/';
82 $rttpg_upload_url = wp_upload_dir()['baseurl'] . '/rttpg/';
83 // phpcs:ignore
84 if ( isset( $_GET['preview'] ) && ! empty( $_GET['preview'] ) ) {
85 $css_path = $rttpg_upload_dir . 'rttpg-block-preview.css';
86
87 if ( file_exists( $css_path ) ) {
88 if ( ! $this->is_editor_screen() ) {
89 wp_enqueue_style( 'rttpg-block-post-preview', $rttpg_upload_url . 'rttpg-block-preview.css', false, $this->version );
90 }
91 }
92 } elseif ( $post_id ) {
93 $css_dir_path = $rttpg_upload_dir . "rttpg-block-$post_id.css";
94 $css_dir_url = $rttpg_upload_dir . "rttpg-block-$post_id.css";
95
96 if ( file_exists( $css_dir_path ) ) {
97 if ( ! $this->is_editor_screen() ) {
98 wp_enqueue_style( "rttpg-block-post-{$post_id}", $css_dir_url, false, $this->version );
99 }
100 $this->add_reusable_css();
101 } else {
102 // phpcs: ignore
103 wp_register_style( 'rttpg-post-data', false );
104 wp_enqueue_style( 'rttpg-post-data' );
105 wp_add_inline_style( 'rttpg-post-data', get_post_meta( get_the_ID(), '_rttpg_block_css', true ) );
106 }
107 }
108 }
109
110 /**
111 * Common css load
112 * @return void
113 */
114 public function tpg_block_enqueue() {
115
116 if ( ! is_admin() ) {
117 return;
118 }
119 wp_enqueue_style( 'rt-fontawsome' );
120 wp_enqueue_style( 'rt-tpg-block' );
121
122 //Custom CSS From Settings
123 $css = isset( $settings['custom_css'] ) ? stripslashes( $settings['custom_css'] ) : null;
124 if ( $css ) {
125 wp_add_inline_style( 'rt-tpg-block', $css );
126 }
127 }
128
129
130 /**
131 * Determine if wppb editor is open
132 *
133 * @return bool
134 *
135 * @since V.1.0.0
136 * @since v.1.0.0
137 */
138 private function is_editor_screen() {
139 if ( ! empty( $_GET['action'] ) && 'wppb_editor' === $_GET['action'] ) {
140 return true;
141 }
142
143 return false;
144 }
145
146
147 /**
148 * Load Editor Assets
149 * @return void
150 */
151 public function editor_assets() {
152
153 //Block editor css
154 wp_enqueue_style( 'rttpg-block-admin-css', rtTPG()->get_assets_uri( 'css/admin/block-admin.css' ), '', $this->version );
155
156 //Main compile css and js file
157 wp_enqueue_style( 'rttpg-blocks-css', rtTPG()->get_assets_uri( 'blocks/main.css' ), '', $this->version );
158 wp_enqueue_script( 'rttpg-blocks-js', rtTPG()->get_assets_uri( 'blocks/main.js' ), [
159 'wp-block-editor',
160 'wp-blocks',
161 'wp-components',
162 'wp-element',
163 'wp-i18n',
164 ], time(), true );
165 $all_dependencies = [
166 'lodash',
167 'wp-i18n',
168 'wp-element',
169 'wp-hooks',
170 'wp-util',
171 'wp-components',
172 'wp-blocks',
173 'wp-data',
174 'wp-editor',
175 'wp-block-editor',
176 ];
177 global $pagenow;
178 $editor_type = 'edit-post';
179
180 if ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) {
181 $all_dependencies[] = 'wp-edit-post';
182 }
183
184 if ( 'site-editor.php' === $pagenow ) {
185 $all_dependencies[] = 'wp-edit-site';
186 $editor_type = 'edit-site';
187 }
188
189 $get_tax_object = get_taxonomies( [], 'objects' );
190 $exclude_tax = Fns::get_excluded_taxonomy();
191 foreach ( $exclude_tax as $_tax ) {
192 unset( $get_tax_object[ $_tax ] );
193 }
194
195 $settings = get_option( rtTPG()->options['settings'] );
196 $ssList = ! empty( $settings['social_share_items'] ) ? $settings['social_share_items'] : [];
197
198 wp_localize_script( 'rttpg-blocks-js', 'rttpgParams', [
199 'editor_type' => $editor_type,
200 'nonce' => wp_create_nonce( 'rttpg_nonce' ),
201 'ajaxurl' => admin_url( 'admin-ajax.php' ),
202 'site_url' => site_url(),
203 'admin_url' => admin_url(),
204 'plugin_url' => RT_THE_POST_GRID_PLUGIN_URL,
205 'plugin_pro_url' => rtTPG()->getProPath(),
206 'post_type' => Fns::get_post_types(),
207 'all_term_list' => Fns::get_all_taxonomy_guten(),
208 'get_taxonomies' => $get_tax_object,
209 'get_users' => Fns::rt_get_users(),
210 'hasPro' => rtTPG()->hasPro(),
211 'pageTitle' => get_the_title(),
212 'hasWoo' => Fns::is_woocommerce(),
213 'hasAcf' => Fns::is_acf(),
214 'ssList' => $ssList
215 ]
216 );
217
218 }
219
220
221 /**
222 * Add inLine css for page or post
223 *
224 * @return void
225 */
226 public function add_block_inline_css() {
227
228 $post_id = get_the_ID();
229 if ( $post_id ) {
230 $rttpg_upload_dir = wp_upload_dir()['basedir'] . '/rttpg/';
231 $css_dir_path = $rttpg_upload_dir . "rttpg-block-$post_id.css";
232 if ( file_exists( $css_dir_path ) ) {
233 $blockCss = file_get_contents( $css_dir_path );
234 echo '<style>' . sanitize_textarea_field( $blockCss ) . '</style>';
235 } else if ( $metaCss = get_post_meta( $post_id, '_rttpg_block_css', true ) ) {
236 echo '<style>' . sanitize_textarea_field( $metaCss ) . '</style>';
237 }
238 }
239 $this->add_reusable_css();
240
241 }
242
243
244 /**
245 * Add reusable css
246 */
247 public function add_reusable_css() {
248 $post_id = get_the_ID();
249 $rttpg_upload_dir = wp_upload_dir()['basedir'] . '/rttpg/';
250 $rttpg_upload_url = wp_upload_dir()['baseurl'] . '/rttpg/';
251 if ( $post_id ) {
252 $content_post = get_post( $post_id );
253 if ( isset( $content_post->post_content ) ) {
254 $content = $content_post->post_content;
255 $parse_blocks = parse_blocks( $content );
256 $css_id = $this->reference_id( $parse_blocks );
257 if ( is_array( $css_id ) ) {
258 if ( ! empty( $css_id ) ) {
259 $css_id = array_unique( $css_id );
260 foreach ( $css_id as $value ) {
261 $css_dir_path = $rttpg_upload_dir . "rttpg-block-$value.css";
262 if ( file_exists( $css_dir_path ) ) {
263 wp_enqueue_style( "rttpg-block-{$value}", $rttpg_upload_url . "rttpg-block-{$value}.css", false, RTTPG_VERSION );
264 }
265 }
266 }
267 }
268 }
269 }
270 }
271
272 /**
273 * Save Import CSS in the top of the File
274 *
275 * @return void Array of the Custom Message
276 */
277 public function save_block_css() {
278
279 try {
280 if ( ! current_user_can( 'edit_posts' ) ) {
281 throw new Exception( __( 'User permission error', 'the-post-grid' ) );
282 }
283 global $wp_filesystem;
284 if ( ! $wp_filesystem ) {
285 require_once( ABSPATH . 'wp-admin/includes/file.php' );
286 }
287
288 $post_id = ! empty( $_POST['post_id'] ) ? sanitize_text_field( $_POST['post_id'] ) : '';
289 $blockCss = ! empty( $_POST['block_css'] ) ? sanitize_text_field( $_POST['block_css'] ) : '';
290
291 if ( $post_id == 'rttpg-widget' && isset( $_POST['has_block'] ) ) {
292 update_option( $post_id, $blockCss );
293 wp_send_json_success( [ 'message' => __( 'Widget CSS Saved', 'the-post-grid' ) ] );
294 }
295
296 $post_id = absint( $post_id );
297 $filename = "rttpg-block-css-{$post_id}.css";
298 $upload_dir_url = wp_upload_dir();
299 $dir = trailingslashit( $upload_dir_url['basedir'] ) . 'rttpg/';
300
301 if ( ! empty( $_POST['has_block'] ) ) {
302
303 update_post_meta( $post_id, '_rttpg_block_active', 1 );
304 $block_css = $this->set_top_css( $blockCss );
305
306 WP_Filesystem( false, $upload_dir_url['basedir'], true );
307 if ( ! $wp_filesystem->is_dir( $dir ) ) {
308 $wp_filesystem->mkdir( $dir );
309
310 }
311 if ( ! $wp_filesystem->put_contents( $dir . $filename, $block_css ) ) {
312 wp_send_json_error( [ 'message' => __( 'CSS can not be saved due to permission!!!', 'the-post-grid' ) ] );
313 }
314 update_post_meta( $post_id, '_rttpg_block_css', $block_css );
315 wp_send_json_success( [ 'message' => __( 'Css file has been updated', 'the-post-grid' ) ] );
316 } else {
317 delete_post_meta( $post_id, '_rttpg_block_active' );
318 if ( file_exists( $dir . $filename ) ) {
319 unlink( $dir . $filename );
320 }
321 delete_post_meta( $post_id, '_rttpg_block_css' );
322 wp_send_json_success( [ 'message' => __( 'Data Delete Done', 'the-post-grid' ) ] );
323 }
324 } catch ( Exception $e ) {
325 wp_send_json_error( [ 'message' => $e->getMessage() ] );
326 }
327 }
328
329 /**
330 * Save Import CSS in the top of the File
331 *
332 * @param STRING
333 *
334 * @return STRING
335 * @since v.1.0.0
336 */
337 public function set_top_css( $get_css = '' ) {
338 $css_url = "@import url('https://fonts.googleapis.com/css?family=";
339 $font_exists = substr_count( $get_css, $css_url );
340 if ( $font_exists ) {
341 $pattern = sprintf( '/%s(.+?)%s/ims', preg_quote( $css_url, '/' ), preg_quote( "');", '/' ) );
342 if ( preg_match_all( $pattern, $get_css, $matches ) ) {
343 $fonts = $matches[0];
344 $get_css = str_replace( $fonts, '', $get_css );
345 if ( preg_match_all( '/font-weight[ ]?:[ ]?[\d]{3}[ ]?;/', $get_css, $matche_weight ) ) {
346 $weight = array_map( function ( $val ) {
347 $process = trim( str_replace( [ 'font-weight', ':', ';' ], '', $val ) );
348 if ( is_numeric( $process ) ) {
349 return $process;
350 }
351 }, $matche_weight[0] );
352 foreach ( $fonts as $key => $val ) {
353 $fonts[ $key ] = str_replace( "');", '', $val ) . ':' . implode( ',', $weight ) . "');";
354 }
355 }
356 $fonts = array_unique( $fonts );
357 $get_css = implode( '', $fonts ) . $get_css;
358 }
359 }
360
361 return $get_css;
362 }
363
364 /**
365 * Save Import CSS in the top of the File
366 *
367 * @return void
368 */
369 public function get_posts_call() {
370 $post_id = absint( $_POST['postId'] );
371 if ( $post_id ) {
372 wp_send_json_success( get_post( $post_id )->post_content );
373 } else {
374 wp_send_json_error( new WP_Error( 'rttpg_block_data_not_found', __( 'Data not found!!', 'the-post-grid' ) ) );
375 }
376 }
377
378 /**
379 * Save Import CSS in the top of the File
380 *
381 *
382 * @return void
383 * @throws Exception
384 * @since v.1.0.0
385 */
386 public function appended( $server ) {
387 if ( ! current_user_can( 'edit_posts' ) ) {
388 wp_send_json_success( new WP_Error( 'rttpg_block_user_permission', __( 'User permission error', 'the-post-grid' ) ) );
389 }
390 global $wp_filesystem;
391 if ( ! $wp_filesystem ) {
392 require_once( ABSPATH . 'wp-admin/includes/file.php' );
393 }
394 $post = $server->get_params();
395 $css = $post['inner_css'];
396 $post_id = (int) sanitize_text_field( $post['post_id'] );
397
398 if ( $post_id ) {
399 $upload_dir_url = wp_upload_dir();
400 $filename = "rttpg-block-css-$post_id.css";
401 $dir = trailingslashit( $upload_dir_url['basedir'] ) . 'rttpg/';
402 WP_Filesystem( false, $upload_dir_url['basedir'], true );
403 if ( ! $wp_filesystem->is_dir( $dir ) ) {
404 $wp_filesystem->mkdir( $dir );
405 }
406 if ( ! $wp_filesystem->put_contents( $dir . $filename, $css ) ) {
407 wp_send_json_error( [ 'message' => __( 'CSS can not be saved due to permission!!!', 'the-post-grid' ) ] );
408 }
409 wp_send_json_success( [ 'message' => __( 'Data retrieve done', 'the-post-grid' ) ] );
410 } else {
411 wp_send_json_error( [ 'message' => __( 'Data not found!!', 'the-post-grid' ) ] );
412 }
413 }
414
415 /**
416 * Return reference id
417 *
418 * @param array $parse_blocks
419 *
420 * @return bool
421 */
422 public function reference_id( $parse_blocks ) {
423 $extra_id = [];
424 if ( ! empty( $parse_blocks ) ) {
425 foreach ( $parse_blocks as $key => $block ) {
426 if ( $block['blockName'] == 'core/block' ) {
427 $extra_id[] = $block['attrs']['ref'];
428 }
429 if ( count( $block['innerBlocks'] ) > 0 ) {
430 $extra_id = array_merge( $this->reference_id( $block['innerBlocks'] ), $extra_id );
431 }
432 }
433 }
434
435 return $extra_id;
436 }
437 }