PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / trunk
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! vtrunk
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 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.1 All 112 releases
templately / modules / block-recovery / Editor.php

Editor.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! trunk, at modules/block-recovery/Editor.php

93 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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