PluginProbe
Core Framework / 2.0.2
Core Framework v2.0.2
2.0.2 2.0.1 2.0.0 1.10.4 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.3.10 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.1.1 1.5.2 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.8.0 All 32 releases
core-framework / wp / StylesheetStorage.php

StylesheetStorage.php in Core Framework 2.0.2, at wp/StylesheetStorage.php

169 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace CoreFramework;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 /**
10 * Stores generated CSS in the current site's WordPress uploads directory.
11 *
12 * This class was introduced with Core Framework 2.0 so it cannot collide with
13 * an opcode-cached 1.10.4 Helper class during an in-place plugin update.
14 */
15 final class StylesheetStorage {
16 /**
17 * Get the generated stylesheet directory details.
18 *
19 * @return array{path: string, url: string}
20 */
21 private static function get_directory(): array {
22 $uploads = wp_upload_dir();
23
24 return array(
25 'path' => trailingslashit( $uploads['basedir'] ) . 'core-framework/css',
26 'url' => trailingslashit( $uploads['baseurl'] ) . 'core-framework/css',
27 );
28 }
29
30 /**
31 * Get the stylesheet path used by versions through 1.10.4.
32 */
33 private static function get_legacy_path(): string {
34 $filename = is_multisite()
35 ? 'core_framework_' . get_current_blog_id() . '.css'
36 : 'core_framework.css';
37
38 return plugin_dir_path( CORE_FRAMEWORK_ABSOLUTE ) . 'assets/public/css/' . $filename;
39 }
40
41 /**
42 * Initialize the WordPress filesystem API.
43 *
44 * @return \WP_Filesystem_Base|null
45 */
46 private static function get_filesystem() {
47 global $wp_filesystem;
48
49 if ( ! function_exists( 'WP_Filesystem' ) ) {
50 require_once ABSPATH . 'wp-admin/includes/file.php';
51 }
52
53 if ( ! WP_Filesystem() || ! is_object( $wp_filesystem ) ) {
54 return null;
55 }
56
57 return $wp_filesystem;
58 }
59
60 /**
61 * Get the public stylesheet URL for the current site.
62 */
63 public static function get_url(): string {
64 $directory = self::get_directory();
65
66 return trailingslashit( $directory['url'] ) . 'core_framework.css';
67 }
68
69 /**
70 * Get the stylesheet filesystem path for the current site.
71 */
72 public static function get_path(): string {
73 $directory = self::get_directory();
74
75 return trailingslashit( $directory['path'] ) . 'core_framework.css';
76 }
77
78 /**
79 * Persist generated CSS.
80 *
81 * @return int|false Number of bytes saved, or false on failure.
82 */
83 public static function write( string $css ) {
84 $filesystem = self::get_filesystem();
85
86 if ( null === $filesystem ) {
87 return false;
88 }
89
90 $directory = self::get_directory();
91 if ( ! $filesystem->is_dir( $directory['path'] ) && ! wp_mkdir_p( $directory['path'] ) ) {
92 return false;
93 }
94
95 $written = $filesystem->put_contents(
96 self::get_path(),
97 $css,
98 defined( 'FS_CHMOD_FILE' ) ? FS_CHMOD_FILE : 0644
99 );
100
101 return $written ? strlen( $css ) : false;
102 }
103
104 /**
105 * Read the generated stylesheet.
106 *
107 * @return string|false
108 */
109 public static function read() {
110 $filesystem = self::get_filesystem();
111
112 return null === $filesystem ? false : $filesystem->get_contents( self::get_path() );
113 }
114
115 /**
116 * Ensure a stylesheet exists, migrating legacy CSS or the database backup.
117 */
118 public static function ensure(): bool {
119 $filesystem = self::get_filesystem();
120 $stylesheet_path = self::get_path();
121
122 if ( null === $filesystem ) {
123 return false;
124 }
125
126 $stylesheet_exists = $filesystem->is_file( $stylesheet_path );
127 $stylesheet_size = $stylesheet_exists ? $filesystem->size( $stylesheet_path ) : false;
128
129 if ( false !== $stylesheet_size && $stylesheet_size > 0 ) {
130 return true;
131 }
132
133 $css = '';
134 $legacy_path = self::get_legacy_path();
135
136 if ( $filesystem->is_file( $legacy_path ) && $filesystem->size( $legacy_path ) > 0 ) {
137 $legacy_css = $filesystem->get_contents( $legacy_path );
138 $css = false !== $legacy_css ? $legacy_css : '';
139 }
140
141 if ( '' === $css ) {
142 $css = (string) get_option( 'core_framework_selected_preset_backup', '' );
143 }
144
145 if ( '' === $css && $stylesheet_exists ) {
146 return true;
147 }
148
149 return false !== self::write( $css );
150 }
151
152 /**
153 * Get a cache-busting version for the generated stylesheet.
154 */
155 public static function get_version(): string {
156 $stylesheet_path = self::get_path();
157
158 if ( is_file( $stylesheet_path ) ) {
159 $version = filemtime( $stylesheet_path );
160
161 if ( false !== $version ) {
162 return (string) $version;
163 }
164 }
165
166 return CORE_FRAMEWORK_VERSION;
167 }
168 }
169