PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.4.1
JetFormBuilder — Dynamic Blocks Form Builder v3.6.4.1
3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / modules / framework / blocks-style-manager / migrator / post-handler.php
jetformbuilder / modules / framework / blocks-style-manager / migrator Last commit date
data-normalizer.php 2 months ago post-handler.php 2 months ago ui.php 2 months ago
post-handler.php
82 lines
1 <?php
2 namespace Crocoblock\Blocks_Style\Migrator;
3
4 class Post_Handler {
5
6 private $post_id;
7 private $data;
8 private $normalizer;
9
10 /**
11 * Migrator_Post_Handler constructor.
12 *
13 * @param int $post_id Post ID.
14 * @param string $meta_value Meta value to migrate.
15 */
16 public function __construct( int $post_id, string $json_data ) {
17 $this->post_id = $post_id;
18 $this->data = json_decode( $json_data, true );
19 $this->normalizer = new Data_Normalizer();
20 }
21
22 /**
23 * Migrate data.
24 *
25 * return bool
26 */
27 public function migrate_data() {
28
29 if ( ! $this->post_id || ! $this->data ) {
30 return false;
31 }
32
33 foreach ( $this->data as $block_id => $block_controls ) {
34 $new_data = $this->normalizer->normalize( $block_controls );
35 $post_blocks[ $block_id ] = $new_data;
36 }
37
38 $post = get_post( $this->post_id );
39
40 if ( ! $post ) {
41 return false;
42 }
43
44 $post_content = $post->post_content;
45
46 foreach ( $post_blocks as $block_id => $controls ) {
47 $controls_json = $this->controls_json( $controls );
48 $block_id_pattern = '"blockID":"' . $block_id . '"';
49
50 // Add new controls only if they are not already present.
51 if ( false === strpos( $post_content, $block_id_pattern . ',"crocoblock_styles"' ) ) {
52 $post_content = str_replace(
53 $block_id_pattern,
54 $block_id_pattern . ',' . $controls_json,
55 $post_content
56 );
57 }
58 }
59
60 $post_content = wp_slash( $post_content );
61
62 // Update post content with new controls.
63 wp_update_post( [
64 'ID' => $this->post_id,
65 'post_content' => $post_content,
66 ] );
67
68 return true;
69 }
70
71 /**
72 * Convert controls to JSON.
73 *
74 * @param array $controls Controls to convert.
75 *
76 * @return string JSON encoded controls.
77 */
78 private function controls_json( array $controls ): string {
79 $controls['_uniqueClassName'] = substr( uniqid('cb-'), 0, 11 );
80 return '"crocoblock_styles":' . json_encode( $controls, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
81 }
82 }