|
13 | 13 | /**
|
14 | 14 | * WordPressVIPMinimum_Sniffs_Files_IncludingFileSniff.
|
15 | 15 | *
|
16 |
| - * Checks that __DIR__, dirname( __FILE__ ) or plugin_dir_path( __FILE__ ) |
17 |
| - * is used when including or requiring files. |
| 16 | + * Checks for custom variables, functions and constants, and external URLs used in file inclusion. |
18 | 17 | *
|
19 | 18 | * @package VIPCS\WordPressVIPMinimum
|
20 | 19 | */
|
@@ -55,6 +54,17 @@ class IncludingFileSniff extends AbstractFunctionRestrictionsSniff {
|
55 | 54 | 'WP_PLUGIN_DIR',
|
56 | 55 | ];
|
57 | 56 |
|
| 57 | + /** |
| 58 | + * List of keywords allowed for use in custom constants. |
| 59 | + * Note: Customizing this property will overwrite current default values. |
| 60 | + * |
| 61 | + * @var array |
| 62 | + */ |
| 63 | + public $allowedKeywords = [ |
| 64 | + 'PATH', |
| 65 | + 'DIR', |
| 66 | + ]; |
| 67 | + |
58 | 68 | /**
|
59 | 69 | * Functions used for modify slashes.
|
60 | 70 | *
|
@@ -122,6 +132,11 @@ public function process_token( $stackPtr ) {
|
122 | 132 | return;
|
123 | 133 | }
|
124 | 134 |
|
| 135 | + if ( $this->has_custom_path( $this->tokens[ $nextToken ]['content'] ) === true ) { |
| 136 | + // The construct is using a constant with an allowed keyword. |
| 137 | + return; |
| 138 | + } |
| 139 | + |
125 | 140 | if ( array_key_exists( $this->tokens[ $nextToken ]['content'], $this->restrictedConstants ) === true ) {
|
126 | 141 | // The construct is using one of the restricted constants.
|
127 | 142 | $message = '`%s` constant might not be defined or available. Use `%s()` instead.';
|
@@ -172,4 +187,21 @@ public function process_token( $stackPtr ) {
|
172 | 187 | $message = 'Absolute include path must be used. Use `get_template_directory()`, `get_stylesheet_directory()` or `plugin_dir_path()`.';
|
173 | 188 | $this->phpcsFile->addError( $message, $nextToken, 'NotAbsolutePath' );
|
174 | 189 | }
|
| 190 | + |
| 191 | + /** |
| 192 | + * Check if a content string contains a keyword in custom paths. |
| 193 | + * |
| 194 | + * @param string $content Content string. |
| 195 | + * |
| 196 | + * @return bool True if the string partially matches a keyword in $allowedCustomKeywords, false otherwise. |
| 197 | + */ |
| 198 | + private function has_custom_path( $content ) { |
| 199 | + foreach ( $this->allowedKeywords as $keyword ) { |
| 200 | + if ( strpos( $content, $keyword ) !== false ) { |
| 201 | + return true; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + return false; |
| 206 | + } |
175 | 207 | }
|
0 commit comments