Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/wp-rocket/inc/Engine/Media/Fonts/Admin/Data.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + declare( strict_types=1 );
3 +
4 + namespace WP_Rocket\Engine\Media\Fonts\Admin;
5 +
6 + use RecursiveDirectoryIterator;
7 + use RecursiveIteratorIterator;
8 + use WP_Rocket\Admin\Options_Data;
9 + use WP_Rocket\Engine\Common\Queue\AbstractASQueue;
10 + use Exception;
11 +
12 + class Data extends AbstractASQueue {
13 + /**
14 + * Options data instance.
15 + *
16 + * @var Options_Data
17 + */
18 + private $options;
19 +
20 + /**
21 + * Base path.
22 + *
23 + * @var string
24 + */
25 + private $base_path;
26 +
27 + /**
28 + * Constructor.
29 + *
30 + * @param Options_Data $options Options data instance.
31 + */
32 + public function __construct( Options_Data $options ) {
33 + $this->options = $options;
34 + $this->base_path = rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_PATH', '' ) . 'fonts/' . get_current_blog_id() . '/';
35 + }
36 +
37 + /**
38 + * Schedule data collection.
39 + *
40 + * @return void
41 + */
42 + public function schedule_data_collection() {
43 + if ( ! $this->is_enabled() ) {
44 + return;
45 + }
46 +
47 + $this->schedule_recurring( time(), WEEK_IN_SECONDS, 'rocket_fonts_data_collection' );
48 + }
49 +
50 + /**
51 + * Unschedule data collection.
52 + *
53 + * @return void
54 + */
55 + public function unschedule_data_collection() {
56 + $this->cancel( 'rocket_fonts_data_collection' );
57 + }
58 +
59 + /**
60 + * Collect data.
61 + *
62 + * @return void
63 + */
64 + public function collect_data() {
65 + if ( ! $this->is_enabled() ) {
66 + return;
67 + }
68 +
69 + $fonts_data = get_transient( 'rocket_fonts_data_collection' );
70 +
71 + // If data has been populated, bail out early.
72 + if ( false !== $fonts_data ) {
73 + return;
74 + }
75 +
76 + try {
77 + $fonts = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $this->base_path . 'google-fonts/fonts/' ) );
78 + } catch ( Exception $exception ) {
79 + return;
80 + }
81 +
82 + $allowed_extensions = [
83 + 'woff',
84 + 'woff2',
85 + 'ttf',
86 + 'otf',
87 + ];
88 +
89 + $total_font_count = 0;
90 + $total_font_size = 0;
91 +
92 + foreach ( $fonts as $file ) {
93 + // check file is not a directory.
94 + if ( $file->isDir() ) {
95 + continue;
96 + }
97 +
98 + $extension = strtolower( pathinfo( $file->getFilename(), PATHINFO_EXTENSION ) );
99 +
100 + if ( in_array( $extension, $allowed_extensions, true ) ) {
101 + ++$total_font_count;
102 + $total_font_size += $file->getSize();
103 + }
104 + }
105 +
106 + set_transient(
107 + 'rocket_fonts_data_collection',
108 + [
109 + 'fonts_total_number' => $total_font_count,
110 + 'fonts_total_size' => size_format( $total_font_size ),
111 + ],
112 + WEEK_IN_SECONDS
113 + );
114 + }
115 +
116 + /**
117 + * Check if the feature & analytics are enabled.
118 + *
119 + * @return bool
120 + */
121 + private function is_enabled(): bool {
122 + return $this->options->get( 'host_fonts_locally', 0 ) && $this->options->get( 'analytics_enabled', 0 );
123 + }
124 + }
125 +