-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathBlockSources.php
More file actions
165 lines (140 loc) · 4.41 KB
/
BlockSources.php
File metadata and controls
165 lines (140 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* Class BlockSources
*
* Captures the themes and plugins responsible for dynamically registered editor blocks.
*
* @since 2.1
*
* @package AmpProject\AmpWP
*/
namespace AmpProject\AmpWP\DevTools;
use AmpProject\AmpWP\Infrastructure\Conditional;
use AmpProject\AmpWP\Infrastructure\Registerable;
use AmpProject\AmpWP\Infrastructure\Service;
use AmpProject\AmpWP\PluginRegistry;
/**
* BlockSources class.
*
* @since 2.1
* @internal
*/
final class BlockSources implements Conditional, Service, Registerable {
/**
* Key of the cached block source data.
*
* @var string
*/
const CACHE_KEY = 'amp_block_sources';
/**
* The amount of time to store the block source data in cache.
*
* @var int
*/
const CACHE_TIMEOUT = DAY_IN_SECONDS;
/**
* Block source data.
*
* @var array|null
*/
private $block_sources;
/**
* Plugin registry instance.
*
* @var PluginRegistry
*/
private $plugin_registry;
/**
* Likely culprit detector instance.
*
* @var LikelyCulpritDetector
*/
private $likely_culprit_detector;
/**
* Check whether the conditional object is currently needed.
*
* @return bool Whether the conditional object is needed.
*/
public static function is_needed() {
return is_admin() || wp_doing_ajax() || wp_doing_cron() || ( defined( 'REST_REQUEST' ) && REST_REQUEST );
}
/**
* Class constructor.
*
* @param PluginRegistry $plugin_registry Plugin registry instance.
* @param LikelyCulpritDetector $likely_culprit_detector Likely culprit detector instance.
*/
public function __construct( PluginRegistry $plugin_registry, LikelyCulpritDetector $likely_culprit_detector ) {
$this->plugin_registry = $plugin_registry;
$this->likely_culprit_detector = $likely_culprit_detector;
}
/**
* Runs on instantiation.
*/
public function register() {
if ( empty( $this->get_block_sources() ) ) {
add_filter( 'register_block_type_args', [ $this, 'capture_block_type_source' ] );
// All blocks should be registered well before admin_enqueue_scripts.
add_action( 'admin_enqueue_scripts', [ $this, 'cache_block_sources' ], PHP_INT_MAX );
}
add_action( 'activated_plugin', [ $this, 'clear_block_sources_cache' ] );
add_action( 'after_switch_theme', [ $this, 'clear_block_sources_cache' ] );
add_action( 'upgrader_process_complete', [ $this, 'clear_block_sources_cache' ] );
}
/**
* Registers the google font style.
*
* @param array $args Array of arguments for registering a block type.
* @return array Filtered block type args.
*/
public function capture_block_type_source( $args ) {
if ( isset( $this->get_block_sources()[ $args['name'] ] ) ) {
return $args;
}
$likely_culprit = $this->likely_culprit_detector->analyze_backtrace();
if ( in_array( $likely_culprit[ FileReflection::SOURCE_TYPE ], [ FileReflection::TYPE_PLUGIN, FileReflection::TYPE_MU_PLUGIN ], true ) ) {
$plugin = $this->plugin_registry->get_plugin_from_slug(
$likely_culprit[ FileReflection::SOURCE_NAME ],
FileReflection::TYPE_MU_PLUGIN === $likely_culprit[ FileReflection::SOURCE_TYPE ]
);
$likely_culprit['title'] = isset( $plugin['data']['Title'] ) ? $plugin['data']['Title'] : $likely_culprit[ FileReflection::SOURCE_NAME ];
} elseif ( FileReflection::TYPE_THEME === $likely_culprit[ FileReflection::SOURCE_TYPE ] ) {
$theme = wp_get_theme( $likely_culprit['name'] );
$likely_culprit['title'] = $theme->get( 'Name' ) ?: $likely_culprit[ FileReflection::SOURCE_NAME ];
} else {
$likely_culprit['title'] = __( 'WordPress core', 'amp' );
}
$this->block_sources[ $args['name'] ] = $likely_culprit;
return $args;
}
/**
* Saves the block source data to cache.
*/
public function cache_block_sources() {
set_transient( __CLASS__ . self::CACHE_KEY, $this->block_sources, self::CACHE_TIMEOUT );
}
/**
* Clears the cached block source data.
*/
public function clear_block_sources_cache() {
delete_transient( __CLASS__ . self::CACHE_KEY );
}
/**
* Retrieves block source data from cache.
*/
private function set_block_sources_from_cache() {
$from_cache = get_transient( __CLASS__ . self::CACHE_KEY );
$this->block_sources = is_array( $from_cache ) ? $from_cache : [];
}
/**
* Retrieves block source data.
*
* @return array
*/
public function get_block_sources() {
if ( is_null( $this->block_sources ) ) {
$this->set_block_sources_from_cache();
}
return $this->block_sources;
}
}