Skip to content

Commit ea9f1ad

Browse files
authored
Merge pull request #955 from johnbillion/deferred-translations
Defer translation loading
2 parents 9144f49 + 40f2a1f commit ea9f1ad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1189
-476
lines changed

classes/Backtrace.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public function get_component() {
222222
$component = self::get_frame_component( $frame );
223223

224224
if ( $component ) {
225-
if ( 'plugin' === $component->type ) {
225+
if ( $component->is_plugin() ) {
226226
// If the component is a plugin then it can't be anything else,
227227
// so short-circuit and return early.
228228
$this->component = $component;
@@ -234,7 +234,7 @@ public function get_component() {
234234
}
235235

236236
$file_dirs = QM_Util::get_file_dirs();
237-
$file_dirs['dropin'] = WP_CONTENT_DIR;
237+
$file_dirs[ QM_Component::TYPE_DROPIN ] = WP_CONTENT_DIR;
238238

239239
foreach ( $file_dirs as $type => $dir ) {
240240
if ( isset( $components[ $type ] ) ) {
@@ -243,12 +243,7 @@ public function get_component() {
243243
}
244244
}
245245

246-
$component = new QM_Component();
247-
$component->type = 'unknown';
248-
$component->name = __( 'Unknown', 'query-monitor' );
249-
$component->context = 'unknown';
250-
251-
return $component;
246+
return QM_Component::from( QM_Component::TYPE_UNKNOWN, 'unknown' );
252247
}
253248

254249
/**
@@ -314,9 +309,12 @@ public function get_display_trace() {
314309
/**
315310
* @return array<int, array<string, mixed>>
316311
* @phpstan-return list<array{
312+
* id: string,
313+
* display: string,
314+
* calling_file: string,
315+
* calling_line: int,
317316
* file: string,
318317
* line: int,
319-
* display: string,
320318
* }>
321319
*/
322320
public function get_filtered_trace() {

classes/Collector.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,22 @@ protected function log_type( $type ) {
8383
* @return void
8484
*/
8585
protected function log_component( $component, $ltime, $type ) {
86-
if ( ! isset( $this->data->component_times[ $component->name ] ) ) {
87-
$this->data->component_times[ $component->name ] = array(
88-
'component' => $component->name,
86+
$key = $component->get_id();
87+
88+
if ( ! isset( $this->data->component_times[ $key ] ) ) {
89+
$this->data->component_times[ $key ] = array(
90+
'component' => $component,
8991
'ltime' => 0,
9092
'types' => array(),
9193
);
9294
}
9395

94-
$this->data->component_times[ $component->name ]['ltime'] += $ltime;
96+
$this->data->component_times[ $key ]['ltime'] += $ltime;
9597

96-
if ( isset( $this->data->component_times[ $component->name ]['types'][ $type ] ) ) {
97-
$this->data->component_times[ $component->name ]['types'][ $type ]++;
98+
if ( isset( $this->data->component_times[ $key ]['types'][ $type ] ) ) {
99+
$this->data->component_times[ $key ]['types'][ $type ]++;
98100
} else {
99-
$this->data->component_times[ $component->name ]['types'][ $type ] = 1;
101+
$this->data->component_times[ $key ]['types'][ $type ] = 1;
100102
}
101103

102104
}

classes/Component.php

Lines changed: 134 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,31 @@
55
* @package query-monitor
66
*/
77

8-
class QM_Component {
8+
/**
9+
* @phpstan-type QM_Component_Array array{
10+
* type: string,
11+
* name: string,
12+
* context: string,
13+
* }
14+
* @property-read string $name
15+
*/
16+
class QM_Component implements JsonSerializable {
17+
public const TYPE_ALTIS_VENDOR = 'altis-vendor';
18+
public const TYPE_CORE = 'core';
19+
public const TYPE_DROPIN = 'dropin';
20+
public const TYPE_GO_PLUGIN = 'go-plugin';
21+
public const TYPE_MU_PLUGIN = 'mu-plugin';
22+
public const TYPE_MU_VENDOR = 'mu-vendor';
23+
public const TYPE_OTHER = 'other';
24+
public const TYPE_PHP = 'php';
25+
public const TYPE_PLUGIN = 'plugin';
26+
public const TYPE_STYLESHEET = 'stylesheet';
27+
public const TYPE_TEMPLATE = 'template';
28+
public const TYPE_THEME = 'theme';
29+
public const TYPE_UNKNOWN = 'unknown';
30+
public const TYPE_VIP_CLIENT_MU_PLUGIN = 'vip-client-mu-plugin';
31+
public const TYPE_VIP_PLUGIN = 'vip-plugin';
32+
933
/**
1034
* @var string
1135
*/
@@ -14,10 +38,117 @@ class QM_Component {
1438
/**
1539
* @var string
1640
*/
17-
public $name;
41+
public $context;
1842

1943
/**
2044
* @var string
2145
*/
22-
public $context;
46+
public $file;
47+
48+
public function __construct( string $context, string $file = '', string $type = '' ) {
49+
$this->context = $context;
50+
$this->file = $file;
51+
$this->type = $type;
52+
}
53+
54+
public function get_name(): string {
55+
if ( isset( $this->name ) ) {
56+
return $this->name;
57+
}
58+
59+
return sprintf(
60+
$this->type,
61+
$this->context
62+
);
63+
}
64+
65+
final public function get_id(): string {
66+
return "{$this->type}-{$this->context}";
67+
}
68+
69+
final public function is_plugin(): bool {
70+
return ( $this->type === self::TYPE_PLUGIN );
71+
}
72+
73+
final public function is_core(): bool {
74+
return ( $this->type === self::TYPE_CORE );
75+
}
76+
77+
/**
78+
* @param QM_Component[] $components
79+
*/
80+
final public static function has_non_core( array $components ): bool {
81+
foreach ( $components as $component ) {
82+
if ( ! $component->is_core() ) {
83+
return true;
84+
}
85+
}
86+
87+
return false;
88+
}
89+
90+
final public static function from( string $type, string $context = '', string $file = '' ): QM_Component {
91+
switch ( $type ) {
92+
case self::TYPE_ALTIS_VENDOR:
93+
return new QM_Component_Altis_Vendor( $context, $file, $type );
94+
case self::TYPE_PLUGIN:
95+
return new QM_Component_Plugin( $context, $file, $type );
96+
case self::TYPE_MU_PLUGIN:
97+
return new QM_Component_MU_Plugin( $context, $file, $type );
98+
case self::TYPE_MU_VENDOR:
99+
return new QM_Component_MU_Vendor( $context, $file, $type );
100+
case self::TYPE_GO_PLUGIN:
101+
return new QM_Component_Go_Plugin( $context, $file, $type );
102+
case self::TYPE_VIP_PLUGIN:
103+
return new QM_Component_VIP_Plugin( $context, $file, $type );
104+
case self::TYPE_VIP_CLIENT_MU_PLUGIN:
105+
return new QM_Component_VIP_Client_MU_Plugin( $context, $file, $type );
106+
case self::TYPE_STYLESHEET:
107+
return new QM_Component_Stylesheet( $context, $file, $type );
108+
case self::TYPE_TEMPLATE:
109+
return new QM_Component_Template( $context, $file, $type );
110+
case self::TYPE_OTHER:
111+
return new QM_Component_Other( $context, $file, $type );
112+
case self::TYPE_CORE:
113+
return new QM_Component_Core( $context, $file, $type );
114+
case self::TYPE_DROPIN:
115+
return new QM_Component_Dropin( $context, $file, $type );
116+
case self::TYPE_PHP:
117+
return new QM_Component_PHP( $context, $file, $type );
118+
}
119+
120+
return new QM_Component_Unknown( $context, $file, $type );
121+
}
122+
123+
/**
124+
* @return mixed
125+
*/
126+
public function __get( string $key ) {
127+
if ( 'name' === $key ) {
128+
return $this->get_name();
129+
}
130+
}
131+
132+
/**
133+
* @phpstan-return QM_Component_Array
134+
* @return array<string, string>
135+
*/
136+
public function toArray(): array {
137+
return array(
138+
'type' => $this->type,
139+
'name' => $this->name,
140+
'context' => $this->context,
141+
);
142+
}
143+
/**
144+
* @phpstan-return QM_Component_Array
145+
* @return array<string, string>
146+
*/
147+
public function jsonSerialize(): array {
148+
return $this->toArray();
149+
}
150+
151+
public static function sort( QM_Component $a, QM_Component $b ): int {
152+
return strcasecmp( $a->get_name(), $b->get_name() );
153+
}
23154
}

classes/Component_Altis_Vendor.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1);
2+
/**
3+
* Class representing an Altis vendor dependency component.
4+
*
5+
* @package query-monitor
6+
*/
7+
8+
class QM_Component_Altis_Vendor extends QM_Component {
9+
public function get_name(): string {
10+
return sprintf(
11+
/* translators: %s: Dependency name */
12+
__( 'Dependency: %s', 'query-monitor' ),
13+
$this->context
14+
);
15+
}
16+
}

classes/Component_Core.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types = 1);
2+
/**
3+
* Class representing the WordPress core component.
4+
*
5+
* @package query-monitor
6+
*/
7+
8+
class QM_Component_Core extends QM_Component {
9+
public function get_name(): string {
10+
return __( 'WordPress Core', 'query-monitor' );
11+
}
12+
}

classes/Component_Dropin.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1);
2+
/**
3+
* Class representing a drop-in plugin component.
4+
*
5+
* @package query-monitor
6+
*/
7+
8+
class QM_Component_Dropin extends QM_Component {
9+
public function get_name(): string {
10+
return sprintf(
11+
/* translators: %s: Drop-in plugin file name */
12+
__( 'Drop-in: %s', 'query-monitor' ),
13+
$this->context
14+
);
15+
}
16+
}

classes/Component_Go_Plugin.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1);
2+
/**
3+
* Class representing a VIP Go mu-plugins/shared-plugins component.
4+
*
5+
* @package query-monitor
6+
*/
7+
8+
class QM_Component_Go_Plugin extends QM_Component {
9+
public function get_name(): string {
10+
return sprintf(
11+
/* translators: %s: Plugin name */
12+
__( 'VIP Plugin: %s', 'query-monitor' ),
13+
$this->context
14+
);
15+
}
16+
}

classes/Component_MU_Plugin.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1);
2+
/**
3+
* Class representing a mu-plugin component.
4+
*
5+
* @package query-monitor
6+
*/
7+
8+
class QM_Component_MU_Plugin extends QM_Component {
9+
public function get_name(): string {
10+
return sprintf(
11+
/* translators: %s: Plugin name */
12+
__( 'MU Plugin: %s', 'query-monitor' ),
13+
$this->context
14+
);
15+
}
16+
}

classes/Component_MU_Vendor.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1);
2+
/**
3+
* Class representing a mu-plugins/vendor dependency.
4+
*
5+
* @package query-monitor
6+
*/
7+
8+
class QM_Component_MU_Vendor extends QM_Component {
9+
public function get_name(): string {
10+
return sprintf(
11+
/* translators: %s: Plugin name */
12+
__( 'MU Plugin: %s', 'query-monitor' ),
13+
$this->context
14+
);
15+
}
16+
}

classes/Component_Other.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types = 1);
2+
/**
3+
* Class representing any other known component.
4+
*
5+
* @package query-monitor
6+
*/
7+
8+
class QM_Component_Other extends QM_Component {
9+
}

0 commit comments

Comments
 (0)