true, 'archive.php' => true, 'home.php' => true, 'index.php' => true, 'page.php' => true, 'search.php' => true, 'single.php' => true, 'singular.php' => true, // Plain secondary template file names. 'attachment.php' => true, 'author.php' => true, 'category.php' => true, 'date.php' => true, 'embed.php' => true, 'front-page.php' => true, 'single-post.php' => true, 'tag.php' => true, 'taxonomy.php' => true, // Plain partial and miscellaneous template file names. 'comments.php' => true, 'footer.php' => true, 'header.php' => true, 'sidebar.php' => true, // Top-leve mime types. 'application.php' => true, 'audio.php' => true, 'example.php' => true, 'font.php' => true, 'image.php' => true, 'message.php' => true, 'model.php' => true, 'multipart.php' => true, 'text.php' => true, 'video.php' => true, ]; /** * Check that defined global variables are prefixed. * * This overloads the parent method to allow for non-prefixed variables to be declared * in template files as those are included from within a function and therefore would be * local to that function. * * @since 0.2.0 * @since 0.2.1 Added $in_list parameter as introduced in WPCS 2.2.0. * * @param int $stackPtr The position of the current token in the stack. * @param bool $in_list Whether or not this is a variable in a list assignment. * Defaults to false. * * @return int|void Integer stack pointer to skip forward or void to continue * normal file processing. */ protected function process_variable_assignment( $stackPtr, $in_list = false ) { // Usage of `strip_quotes` is to ensure `stdin_path` passed by IDEs does not include quotes. $file = $this->strip_quotes( $this->phpcsFile->getFileName() ); $fileName = basename( $file ); if ( \defined( '\PHP_CODESNIFFER_IN_TESTS' ) ) { $fileName = str_replace( '.inc', '.php', $fileName ); } // Don't process in case of a template file or folder. if ( isset( $this->simple_theme_template_file_names[ $fileName ] ) === true ) { return; } if ( preg_match( self::COMPLEX_THEME_TEMPLATE_NAME_REGEX, $fileName ) === 1 ) { return; } if ( $this->is_from_allowed_folder( $file ) ) { return; } // Not a typical template file name, defer to the prefix checking in the parent sniff. return parent::process_variable_assignment( $stackPtr, $in_list ); } /** * Checks if the given file path is located in the $allowed_folders array. * * @since 0.2.0 * * @param string $path Full path of the sniffed file. * @return boolean */ private function is_from_allowed_folder( $path ) { if ( empty( $this->allowed_folders ) || ! is_array( $this->allowed_folders ) ) { return false; } foreach ( $this->allowed_folders as $folder ) { if ( strrpos( $path, DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR ) !== false ) { return true; } } return false; } }