PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.14.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.14.0
2.14.0 2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 All 81 releases
ablocks / addons / link-guard / link-guard.php

link-guard.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.14.0, at addons/link-guard/link-guard.php

80 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ABlocksLinkGuard;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly.
7 }
8
9 use ABlocks\Interfaces\AddonInterface;
10 use ABlocks\Classes\CacheBackend;
11
12 /**
13 * Link Guard addon.
14 *
15 * Writers link to posts that are scheduled but not live yet. Until the target
16 * publishes, a visitor who follows that link gets an error page. This addon
17 * takes those links out of the rendered content — the anchor text stays, the
18 * `<a>` goes — and puts them back the moment the target goes live.
19 *
20 * Nothing is rewritten in the database. The decision is made at render time
21 * from the target's current status, so publishing, unpublishing, trashing or
22 * making a post private all take effect without touching the posts that link
23 * to it. What *is* stored is only the expensive part: which post a URL points
24 * at (see `Frontend::lookup()`).
25 *
26 * The rendered HTML never depends on who is viewing, so page caches stay valid.
27 * The one thing a page cache cannot see is a *different* post changing status,
28 * which is why `Invalidation` keeps a reverse index and announces the posts
29 * whose output just changed.
30 */
31 final class LinkGuard implements AddonInterface {
32
33 private $addon_name = 'link-guard';
34
35 private function __construct() {
36 $this->define_constants();
37 $this->init_addon();
38 }
39
40 public function define_constants() {
41 define( 'ABLOCKS_LINK_GUARD_VERSION', '1.0' );
42 define( 'ABLOCKS_LINK_GUARD_ADDON_NAME', $this->addon_name );
43 define( 'ABLOCKS_LINK_GUARD_GENERATION_OPTION', 'ablocks_link_guard_generation' );
44 }
45
46 public function init_addon() {
47 add_action( "ablocks/addons/activated_{$this->addon_name}", [ $this, 'addon_activation_hook' ] );
48
49 if ( ! \ABlocks\Helper::get_addon_active_status( $this->addon_name ) ) {
50 return;
51 }
52
53 // Status changes happen in the admin, over REST and in cron (a scheduled
54 // post publishes from wp-cron), so invalidation listens everywhere.
55 Invalidation::init();
56
57 if ( ! is_admin() || wp_doing_ajax() ) {
58 Frontend::init();
59 }
60 }
61
62 public static function init() {
63 static $instance = false;
64
65 if ( ! $instance ) {
66 $instance = new self();
67 }
68
69 return $instance;
70 }
71
72 /**
73 * While the addon was off, nothing tracked posts being created or renamed,
74 * so any "this URL is not a post" answer stored before then may be stale.
75 */
76 public function addon_activation_hook() {
77 CacheBackend::bump_generation( ABLOCKS_LINK_GUARD_GENERATION_OPTION );
78 }
79 }
80