| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Modules\BlockRecovery; |
| 4 |
|
| 5 |
use Templately\Modules\FullSiteImport\Runners\BaseRunner; |
| 6 |
use Templately\Utils\Base; |
| 7 |
|
| 8 |
/** |
| 9 |
* The on-open trigger's server half (spec 060 US1). |
| 10 |
* |
| 11 |
* When a user opens a post in the block editor, decide — here, at enqueue time — whether the |
| 12 |
* repair should run in that editor session, and if so put the editor-side bundle on the page |
| 13 |
* with its config. Deciding in PHP means a clean open costs no request: everything the decision |
| 14 |
* needs (post id, capability, the kill switch, provenance, the queue) is already in hand. |
| 15 |
* |
| 16 |
* Eligible = the current user can edit the post AND the pass is enabled AND the post was |
| 17 |
* imported by Templately. "Imported" is answered by provenance meta (durable — a page opened a |
| 18 |
* year later still qualifies) OR by a pending queue entry (the bridge for content imported |
| 19 |
* before single imports recorded provenance; see 060 FR-002). |
| 20 |
* |
| 21 |
* The bundle is a screen-bound entry (constitution XIII): its only host is the block editor, |
| 22 |
* so it is enqueued by path, only here, and never joins the SPA graph. |
| 23 |
*/ |
| 24 |
class Editor extends Base { |
| 25 |
|
| 26 |
const HANDLE = 'templately-block-recovery-editor'; |
| 27 |
|
| 28 |
/** How long the editor bundle waits for the editor to parse blocks before giving up. */ |
| 29 |
const READY_TIMEOUT_MS = 15000; |
| 30 |
|
| 31 |
public function __construct() { |
| 32 |
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue' ] ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function enqueue() { |
| 39 |
$post_id = (int) get_the_ID(); |
| 40 |
|
| 41 |
// `post-new.php` has no post yet — and nothing imported is ever "new". |
| 42 |
if ( $post_id <= 0 || ! current_user_can( 'edit_post', $post_id ) || ! Module::is_enabled() ) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
$reason = self::eligibility( $post_id ); |
| 47 |
|
| 48 |
if ( '' === $reason ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
templately()->assets->enqueue( |
| 53 |
self::HANDLE, |
| 54 |
'js/block-recovery-editor.js', |
| 55 |
[ 'wp-data', 'wp-blocks', 'wp-editor', 'wp-api-fetch' ] |
| 56 |
); |
| 57 |
|
| 58 |
templately()->assets->localize( |
| 59 |
self::HANDLE, |
| 60 |
'templatelyBlockRecoveryEditor', |
| 61 |
[ |
| 62 |
'postId' => $post_id, |
| 63 |
'trigger' => Report::TRIGGER_ON_OPEN, |
| 64 |
'reason' => $reason, |
| 65 |
'readyTimeoutMs' => self::READY_TIMEOUT_MS, |
| 66 |
] |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Why a post qualifies for on-open repair, or '' when it does not. |
| 72 |
* |
| 73 |
* @param int $post_id |
| 74 |
* @return string 'provenance' | 'queued' | 'both' | '' |
| 75 |
*/ |
| 76 |
public static function eligibility( int $post_id ): string { |
| 77 |
$provenance = metadata_exists( 'post', $post_id, BaseRunner::META_SESSION_KEY ); |
| 78 |
$queued = Queue::is_pending( $post_id ); |
| 79 |
|
| 80 |
if ( $provenance && $queued ) { |
| 81 |
return 'both'; |
| 82 |
} |
| 83 |
if ( $provenance ) { |
| 84 |
return 'provenance'; |
| 85 |
} |
| 86 |
if ( $queued ) { |
| 87 |
return 'queued'; |
| 88 |
} |
| 89 |
|
| 90 |
return ''; |
| 91 |
} |
| 92 |
} |
| 93 |
|