Skip to content

Commit c83b060

Browse files
authored
JSON Schemas: fix too-early validation of schemas in FTP hosts (#275)
* Switch to using file_get_contents * Exclude the `file_get_contents` rule from PHPStan * Remove schema validation on construct
1 parent 34a448e commit c83b060

File tree

2 files changed

+6
-42
lines changed

2 files changed

+6
-42
lines changed

.phpcs.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
<!-- Refactoring of this scale is not in scope yet.-->
2525
<exclude name="WordPress.Files.FileName" />
2626
<!-- Exclude the entire filename rule -->
27+
<exclude name="WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents" />
28+
<!-- file_get_contents is safe for reading local plugin files bundled with the plugin. -->
2729
</rule>
2830

2931
<rule ref="WordPress.Security.EscapeOutput">

includes/class-scf-json-schema-validator.php

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,9 @@ class SCF_JSON_Schema_Validator {
4444

4545
/**
4646
* Constructor.
47-
*
48-
* Validates that all required schemas are available on initialization.
49-
* If schemas are missing, registers an admin notice and prevents usage.
5047
*/
5148
public function __construct() {
5249
$this->schema_path = acf_get_path( 'schemas/' );
53-
54-
// Validate schemas exist on initialization (skip during static analysis)
55-
if ( defined( 'ABSPATH' ) && ! $this->validate_required_schemas() ) {
56-
add_action( 'admin_notices', array( $this, 'show_schema_error' ) );
57-
}
5850
}
5951

6052

@@ -133,21 +125,11 @@ public function validate_data( $data, $schema_name ) {
133125
public function load_schema( $schema_name ) {
134126
$schema_file = $this->schema_path . $schema_name . '.schema.json';
135127

136-
if ( ! file_exists( $schema_file ) ) {
137-
return null;
138-
}
139-
140-
if ( ! function_exists( 'WP_Filesystem' ) ) {
141-
require_once ABSPATH . 'wp-admin/includes/file.php';
142-
}
143-
WP_Filesystem();
144-
global $wp_filesystem;
145-
146-
if ( null === $wp_filesystem ) {
128+
if ( ! file_exists( $schema_file ) || ! is_readable( $schema_file ) ) {
147129
return null;
148130
}
149131

150-
$schema_content = $wp_filesystem->get_contents( $schema_file );
132+
$schema_content = file_get_contents( $schema_file );
151133
if ( false === $schema_content ) {
152134
return null;
153135
}
@@ -175,21 +157,6 @@ public function validate_required_schemas() {
175157
return true;
176158
}
177159

178-
/**
179-
* Display admin notice when required schemas are not available.
180-
*
181-
* @since 6.6.0
182-
*/
183-
public function show_schema_error() {
184-
?>
185-
<div class="notice notice-error is-dismissible">
186-
<p>
187-
<strong><?php esc_html_e( 'Secure Custom Fields Error:', 'secure-custom-fields' ); ?></strong>
188-
<?php esc_html_e( 'Required schema files are missing. Schema validation will not be available. Please ensure all schema files are present in the plugin directory.', 'secure-custom-fields' ); ?>
189-
</p>
190-
</div>
191-
<?php
192-
}
193160
/**
194161
* Gets the validation errors from the last validation attempt.
195162
*
@@ -275,17 +242,12 @@ public function validate_json( $json_string, $schema_name ) {
275242
public function validate_file( $file_path, $schema_name ) {
276243
$this->clear_validation_errors();
277244

278-
if ( ! file_exists( $file_path ) ) {
245+
if ( ! file_exists( $file_path ) || ! is_readable( $file_path ) ) {
279246
$this->add_validation_error( 'file', 'File does not exist: ' . $file_path );
280247
return false;
281248
}
282249

283-
if ( ! function_exists( 'WP_Filesystem' ) ) {
284-
require_once ABSPATH . 'wp-admin/includes/file.php';
285-
}
286-
WP_Filesystem();
287-
global $wp_filesystem;
288-
$json_content = $wp_filesystem->get_contents( $file_path );
250+
$json_content = file_get_contents( $file_path );
289251

290252
if ( false === $json_content ) {
291253
$this->add_validation_error( 'file', 'Could not read file: ' . $file_path );

0 commit comments

Comments
 (0)