| 1 |
<?php |
| 2 |
/** |
| 3 |
* block-recovery module — repairs Gutenberg blocks that a finished import left invalid. |
| 4 |
* |
| 5 |
* Gutenberg validates a static block by re-running its JS `save(attributes)` and comparing the |
| 6 |
* output to the markup stored in the post. Two things make imported content fail that check: |
| 7 |
* |
| 8 |
* 1. The pack's markup was produced by a DIFFERENT version of the block plugin than the one |
| 9 |
* installed here. When the site is BEHIND on a block whose `save()` changed, the stored |
| 10 |
* markup is simply the old shape and can be regenerated losslessly. |
| 11 |
* 2. A merge wrote new copy into a block's attributes or its markup but not both. `WS1` |
| 12 |
* closed the known cases at the source; this pass is the net for the rest. |
| 13 |
* |
| 14 |
* `save()` exists only in JS, and third-party blocks only register inside a block-editor |
| 15 |
* context, so no amount of PHP can regenerate this markup. The repair therefore runs in the |
| 16 |
* BROWSER (see `assets/js/`), and this module's PHP is only the queue + the report sink: the |
| 17 |
* Finalizer leaves a list of imported post ids, the browser walks it, and what it could not |
| 18 |
* repair comes back here to be surfaced. |
| 19 |
* |
| 20 |
* THE GATE IS THE POINT. `parse` silently drops any attribute the installed block type does not |
| 21 |
* declare, so regenerating a block from its parsed attributes DELETES whatever the newer plugin |
| 22 |
* version wrote. Measured on the SaaStark pack against Essential Blocks 6.4.0: a rebuild turned |
| 23 |
* 13 invalid blocks into 0 and stripped `data-gsap`/`ebGsap` from all 13 in the process — |
| 24 |
* "repairing" them into permanent data loss, when leaving them alone lets them heal by |
| 25 |
* themselves the moment the user updates the plugin. So a block is rebuilt only when nothing |
| 26 |
* would be dropped; everything else is reported, never touched. |
| 27 |
* |
| 28 |
* @package Templately |
| 29 |
*/ |
| 30 |
|
| 31 |
namespace Templately\Modules\BlockRecovery; |
| 32 |
|
| 33 |
use Templately\Core\Module_Base; |
| 34 |
|
| 35 |
class Module extends Module_Base { |
| 36 |
|
| 37 |
/** Off switch for the whole pass. Detection still runs; only the repair stops. */ |
| 38 |
const ENABLED_OPTION = 'templately_block_rebuild_enabled'; |
| 39 |
|
| 40 |
public function get_name(): string { |
| 41 |
return 'block-recovery'; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Declared: the queue is filled from full-site-import's `templately_fsi_import_complete` |
| 46 |
* hook and reads its session data for the merge-time parity warnings. |
| 47 |
*/ |
| 48 |
public function get_dependencies(): array { |
| 49 |
return [ 'full-site-import' ]; |
| 50 |
} |
| 51 |
|
| 52 |
protected function init_hooks(): void { |
| 53 |
Queue::get_instance(); |
| 54 |
// The on-open trigger (spec 060): enqueues the editor-side repair for eligible posts. |
| 55 |
Editor::get_instance(); |
| 56 |
|
| 57 |
add_action( 'rest_api_init', function () { |
| 58 |
( new REST\Rebuild() )->register_routes(); |
| 59 |
} ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Whether the browser-side repair may run. |
| 64 |
* |
| 65 |
* The queue is written either way, so the pass can be switched back on later for imports |
| 66 |
* that already happened instead of losing them. |
| 67 |
*/ |
| 68 |
public static function is_enabled(): bool { |
| 69 |
$enabled = (bool) get_option( self::ENABLED_OPTION, true ); |
| 70 |
|
| 71 |
/** |
| 72 |
* Filter whether the post-import block rebuild runs. |
| 73 |
* |
| 74 |
* @param bool $enabled |
| 75 |
*/ |
| 76 |
return (bool) apply_filters( 'templately_block_rebuild_enabled', $enabled ); |
| 77 |
} |
| 78 |
} |
| 79 |
|