|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * The WordPress.org global footer, from wordpress.org's footer endpoint. |
| 4 | + * |
| 5 | + * WordPress.com doesn't run wporg-mu-plugins, so the footer block itself isn't |
| 6 | + * available here; the endpoint serves its markup to sites outside the network. |
| 7 | + * |
| 8 | + * @package WordPressTV |
| 9 | + */ |
| 10 | + |
| 11 | +declare( strict_types = 1 ); |
| 12 | + |
| 13 | +namespace WordPressTV\GlobalFooter; |
| 14 | + |
| 15 | +if ( ! defined( 'ABSPATH' ) ) { |
| 16 | + exit; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Endpoint that renders the global footer. |
| 21 | + */ |
| 22 | +const ENDPOINT = 'https://wordpress.org/wp-json/global-header-footer/v1/footer'; |
| 23 | + |
| 24 | +/** |
| 25 | + * Stylesheet the global header and footer share. |
| 26 | + */ |
| 27 | +const STYLESHEET = 'https://s.w.org/wp-content/mu-plugins/pub-sync/blocks/global-header-footer/build/style.css'; |
| 28 | + |
| 29 | +/** |
| 30 | + * Option caching the footer markup and when to fetch it again. |
| 31 | + */ |
| 32 | +const OPTION = 'wptv_global_footer'; |
| 33 | + |
| 34 | +/** |
| 35 | + * Seconds a fetched footer is kept. |
| 36 | + */ |
| 37 | +const LIFETIME = 12 * HOUR_IN_SECONDS; |
| 38 | + |
| 39 | +/** |
| 40 | + * Seconds until a failed fetch is retried. |
| 41 | + */ |
| 42 | +const RETRY = 5 * MINUTE_IN_SECONDS; |
| 43 | + |
| 44 | +/** |
| 45 | + * Registers the block the footer template part renders. |
| 46 | + * |
| 47 | + * @return void |
| 48 | + */ |
| 49 | +function register_block(): void { |
| 50 | + register_block_type( |
| 51 | + 'wptv/global-footer', |
| 52 | + array( |
| 53 | + 'api_version' => 3, |
| 54 | + 'title' => __( 'WordPress.org footer', 'wptv' ), |
| 55 | + 'category' => 'theme', |
| 56 | + 'supports' => array( |
| 57 | + 'autoRegister' => true, |
| 58 | + 'html' => false, |
| 59 | + 'inserter' => false, |
| 60 | + ), |
| 61 | + 'render_callback' => __NAMESPACE__ . '\render', |
| 62 | + ) |
| 63 | + ); |
| 64 | +} |
| 65 | +add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 66 | + |
| 67 | +/** |
| 68 | + * Renders the global footer, with the styles it needs. |
| 69 | + * |
| 70 | + * @return string Footer markup, or an empty string when it was never fetched. |
| 71 | + */ |
| 72 | +function render(): string { |
| 73 | + $markup = get_markup(); |
| 74 | + if ( '' === $markup ) { |
| 75 | + return ''; |
| 76 | + } |
| 77 | + |
| 78 | + wp_enqueue_style( 'wporg-global-header-footer', STYLESHEET, array(), gmdate( 'Ymd' ) ); |
| 79 | + // Core only loads block styles for blocks it renders, and the footer's social links aren't rendered here. |
| 80 | + wp_enqueue_style( 'wp-block-social-links' ); |
| 81 | + |
| 82 | + return $markup; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Returns the cached footer markup, fetching it again once it expires. |
| 87 | + * |
| 88 | + * A failed fetch keeps the last footer and tries again a few minutes later. |
| 89 | + * |
| 90 | + * @return string Footer markup, or an empty string when it was never fetched. |
| 91 | + */ |
| 92 | +function get_markup(): string { |
| 93 | + $cached = get_option( OPTION ); |
| 94 | + if ( is_array( $cached ) && $cached['expires'] > time() ) { |
| 95 | + return $cached['markup']; |
| 96 | + } |
| 97 | + |
| 98 | + $fetched = fetch_markup(); |
| 99 | + $markup = $fetched ?? ( $cached['markup'] ?? '' ); |
| 100 | + update_option( |
| 101 | + OPTION, |
| 102 | + array( |
| 103 | + 'markup' => $markup, |
| 104 | + 'expires' => time() + ( null === $fetched ? RETRY : LIFETIME ), |
| 105 | + ), |
| 106 | + false |
| 107 | + ); |
| 108 | + |
| 109 | + return $markup; |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Fetches the footer from wordpress.org. |
| 114 | + * |
| 115 | + * @return string|null Footer markup, or null when the request fails. |
| 116 | + */ |
| 117 | +function fetch_markup(): ?string { |
| 118 | + $response = wp_safe_remote_get( ENDPOINT, array( 'timeout' => 3 ) ); |
| 119 | + if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + return extract_footer( wp_remote_retrieve_body( $response ) ); |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * Takes the footer element from the endpoint's response. |
| 128 | + * |
| 129 | + * The response goes on with wordpress.org's `wp_footer()` scripts and closes the |
| 130 | + * document, so only the element itself is kept. |
| 131 | + * |
| 132 | + * @param string $body Response body. |
| 133 | + * @return string|null Footer element, or null when the body doesn't start with one. |
| 134 | + */ |
| 135 | +function extract_footer( string $body ): ?string { |
| 136 | + $body = ltrim( $body ); |
| 137 | + $end = strpos( $body, '</footer>' ); |
| 138 | + if ( ! str_starts_with( $body, '<footer ' ) || false === $end ) { |
| 139 | + return null; |
| 140 | + } |
| 141 | + |
| 142 | + return substr( $body, 0, $end + strlen( '</footer>' ) ); |
| 143 | +} |
0 commit comments