|
4 | 4 |
|
5 | 5 | use BEA\Theme\Framework\Service; |
6 | 6 | use BEA\Theme\Framework\Service_Container; |
| 7 | +use WP_Grid_Builder\Includes\Database; |
| 8 | +use WP_Grid_Builder\Includes\Helpers; |
7 | 9 |
|
8 | 10 | /** |
9 | 11 | * WP Grid Builder service. |
@@ -34,6 +36,17 @@ public function boot( Service_Container $container ): void { |
34 | 36 | add_filter( 'wp_grid_builder/frontend/register_scripts', [ $this, 'wpgb_register_scripts' ], 10, 1 ); |
35 | 37 | add_filter( 'wp_grid_builder/frontend/register_styles', [ $this, 'wpgb_register_styles' ], 10, 1 ); |
36 | 38 | add_filter( 'wp_grid_builder/facet/title_tag', [ $this, 'facet_title_tag' ], 10, 1 ); |
| 39 | + |
| 40 | + add_action( |
| 41 | + 'init', |
| 42 | + function () { |
| 43 | + if ( did_action( 'wp_grid_builder/init' ) ) { |
| 44 | + $this->load_wpgb_configuration(); |
| 45 | + } else { |
| 46 | + add_action( 'wp_grid_builder/init', [ $this, 'load_wpgb_configuration' ] ); |
| 47 | + } |
| 48 | + } |
| 49 | + ); |
37 | 50 | } |
38 | 51 |
|
39 | 52 | /** |
@@ -112,4 +125,88 @@ public function wpgb_register_styles( $styles ): array { |
112 | 125 | public function facet_title_tag( $title_tag ): string { |
113 | 126 | return 'p'; |
114 | 127 | } |
| 128 | + |
| 129 | + /** |
| 130 | + * Load WP Grid Builder configuration files. |
| 131 | + * |
| 132 | + * Load exported configuration files from the folder `assets/wpgb` if they have changed. |
| 133 | + * Supported configuration files are : |
| 134 | + * - `facets.json` |
| 135 | + * - `grids.json` |
| 136 | + * - `cards.json` |
| 137 | + * - `styles.json` |
| 138 | + * |
| 139 | + * @return void |
| 140 | + */ |
| 141 | + public function load_wpgb_configuration(): void { |
| 142 | + if ( ! defined( 'WPGB_VERSION' ) ) { |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + $wpgb_types = [ |
| 147 | + 'facets', |
| 148 | + 'grids', |
| 149 | + 'cards', |
| 150 | + 'styles', |
| 151 | + ]; |
| 152 | + |
| 153 | + $wpgb_database = new Database(); |
| 154 | + foreach ( $wpgb_types as $wpgb_type ) { |
| 155 | + $config_filepath = get_theme_file_path( sprintf( '/assets/wpgb/%s.json', $wpgb_type ) ); |
| 156 | + if ( ! is_readable( $config_filepath ) ) { |
| 157 | + continue; |
| 158 | + } |
| 159 | + |
| 160 | + // Check is file has changed. |
| 161 | + $option_name = sprintf( 'wpgb_%s_hash', $wpgb_type ); |
| 162 | + $file_hash = md5_file( $config_filepath ); |
| 163 | + $current_hash = get_option( $option_name, '' ); |
| 164 | + if ( hash_equals( $file_hash, $current_hash ) ) { |
| 165 | + continue; |
| 166 | + } |
| 167 | + |
| 168 | + $content = file_get_contents( $config_filepath ); //phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 169 | + if ( empty( $content ) ) { |
| 170 | + continue; |
| 171 | + } |
| 172 | + |
| 173 | + // Decode data (use WPGB helper to decode inner fields properly). |
| 174 | + try { |
| 175 | + $wpgb_data = json_decode( $content, true, 512, JSON_THROW_ON_ERROR ); |
| 176 | + $wpgb_data = Helpers::maybe_json_decode( $wpgb_data, true ); |
| 177 | + } catch ( \JsonException $e ) { |
| 178 | + continue; |
| 179 | + } |
| 180 | + |
| 181 | + // Delete existing data before reimport. |
| 182 | + $existing_data = $wpgb_database::query_results( |
| 183 | + [ |
| 184 | + 'select' => 'id', |
| 185 | + 'from' => $wpgb_type, |
| 186 | + ] |
| 187 | + ); |
| 188 | + if ( is_array( $existing_data ) && ! empty( $existing_data ) ) { |
| 189 | + $wpgb_database::delete_row( $wpgb_type, wp_list_pluck( $existing_data, 'id' ) ); |
| 190 | + } |
| 191 | + |
| 192 | + // Prepare request body. |
| 193 | + $request_body = wp_json_encode( |
| 194 | + [ |
| 195 | + 'content' => $wpgb_data, |
| 196 | + ] |
| 197 | + ); |
| 198 | + |
| 199 | + // Import data via REST endpoint |
| 200 | + $request = new \WP_REST_Request( 'POST', '/wpgb/v2/import' ); |
| 201 | + $request->set_header( 'Content-Type', 'application/json' ); |
| 202 | + $request->set_body( $request_body ); |
| 203 | + |
| 204 | + $response = rest_do_request( $request ); |
| 205 | + |
| 206 | + // Update the stored hash if the request was successful. |
| 207 | + if ( ! $response->is_error() ) { |
| 208 | + update_option( $option_name, $file_hash ); |
| 209 | + } |
| 210 | + } |
| 211 | + } |
115 | 212 | } |
0 commit comments