| 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 |
|