Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 82 additions & 27 deletions includes/Traits/File_Editor_URL.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,62 +25,117 @@ trait File_Editor_URL {
* @param string $filename Error file name.
* @param int $line Optional. Line number of error. Default 0 (no specific line).
* @return string|null File editor URL or null if not available.
*
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
protected function get_file_editor_url( Check_Result $result, $filename, $line = 0 ) {

$edit_url = null;
$line = (int) $line;

$plugin_path = $result->plugin()->path( '/' );
$plugin_slug = $result->plugin()->slug();
$filename = str_replace( $plugin_path, '', $filename );

$file_path = WP_PLUGIN_DIR . '/' . $plugin_slug;
if ( $plugin_slug !== $filename ) {
$file_path .= '/' . $filename;
}

$source = array(
'file' => $file_path,
'line' => $line,
'plugin' => $plugin_slug,
'filename' => $filename,
);

/**
* Filters the template for the URL for linking to an external editor to open a file for editing.
* Filters the URL for linking to an external editor to open a file for editing.
*
* Users of IDEs that support opening files via web protocols can use this filter to
* override the edit link so it opens in their editor rather than the plugin editor.
*
* Users of IDEs that support opening files in via web protocols can use this filter to override
* the edit link to result in their editor opening rather than the plugin editor.
* The initial filtered value is null, requiring extension plugins to supply the URL
* themselves. If no URL is provided, links to the plugin editor are used if available.
* Returning a string that contains `{{file}}` or `{{line}}` placeholders causes them
* to be substituted with the raw filesystem path and the integer line number
* respectively. Returning a string without placeholders uses it verbatim.
*
* The initial filtered value is null, requiring extension plugins to supply the URL template
* string themselves. If no template string is provided, links to the plugin editors will
* be provided if available. For example, for an extension plugin to cause file edit links to
* open in an IDE, the following filters can be used:
* For example, to cause file edit links to open in an IDE:
*
* # PhpStorm
* add_filter( 'wp_plugin_check_validation_error_source_file_editor_url_template', function () {
* return 'phpstorm://open?file={{file}}&line={{line}}';
* } );
* add_filter( 'wp_plugin_check_validation_error_source_url', function ( $url, $source ) {
* return 'phpstorm://open?file=' . rawurlencode( $source['file'] ) . '&line=' . (int) $source['line'];
* }, 10, 2 );
*
* # VS Code
* add_filter( 'wp_plugin_check_validation_error_source_file_editor_url_template', function () {
* # VS Code (using placeholders)
* add_filter( 'wp_plugin_check_validation_error_source_url', function ( $url ) {
* return 'vscode://file/{{file}}:{{line}}';
* } );
*
* For a template to be considered, the string '{{file}}' must be present in the filtered value.
* @since 2.1.0
*
* @since 1.0.0
*
* @param string|null $editor_url_template Editor URL template. default null.
* @param string|null $url Editor URL. Default null.
* @param array $source Source information: file, line, plugin, filename.
*/
$editor_url_template = apply_filters( 'wp_plugin_check_validation_error_source_file_editor_url_template', null );
$url = apply_filters( 'wp_plugin_check_validation_error_source_url', null, $source );

// Supply the file path to the editor template.
if ( is_string( $editor_url_template ) && str_contains( $editor_url_template, '{{file}}' ) ) {
$file_path = WP_PLUGIN_DIR . '/' . $plugin_slug;
if ( $plugin_slug !== $filename ) {
$file_path .= '/' . $filename;
}
if ( is_string( $url ) && '' !== $url && file_exists( $file_path ) ) {
$edit_url = str_replace(
array(
'{{file}}',
'{{line}}',
),
array(
$file_path,
$line,
),
$url
);
}

if ( file_exists( $file_path ) ) {
// Backward compatibility for the two-filter chain that this single filter replaces.
if ( ! $edit_url && has_filter( 'wp_plugin_check_validation_error_source_file_editor_url_template' ) ) {
/**
* Filters the template for the URL for linking to an external editor to open a file for editing.
*
* Users of IDEs that support opening files via web protocols can use this filter to override
* the edit link to result in their editor opening rather than the plugin editor.
*
* @deprecated 2.1.0 Use the `wp_plugin_check_validation_error_source_url` filter instead.
*
* @since 1.0.0
*
* @param string|null $editor_url_template Editor URL template. Default null.
*/
$editor_url_template = apply_filters_deprecated(
'wp_plugin_check_validation_error_source_file_editor_url_template',
array( null ),
'2.1.0',
'wp_plugin_check_validation_error_source_url'
);

// Supply the file path to the editor template.
if ( is_string( $editor_url_template ) && str_contains( $editor_url_template, '{{file}}' ) && file_exists( $file_path ) ) {
/**
* Filters the file path to be opened in an external editor for a given PHPCS error source.
*
* This is useful to map the file path from inside of a Docker container or VM to the host machine.
*
* @deprecated 2.1.0 Use the `wp_plugin_check_validation_error_source_url` filter instead.
*
* @since 1.0.0
*
* @param string|null $editor_url_template Editor URL template.
* @param array $source Source information.
* @param string|null $file_path File path to be opened in the external editor.
* @param array $source Source information.
*/
$file_path = apply_filters( 'wp_plugin_check_validation_error_source_file_path', $file_path, array( $plugin_slug, $filename, $line ) );
$file_path = apply_filters_deprecated(
'wp_plugin_check_validation_error_source_file_path',
array( $file_path, array( $plugin_slug, $filename, $line ) ),
'2.1.0',
'wp_plugin_check_validation_error_source_url'
);
if ( $file_path ) {
$edit_url = str_replace(
array(
Expand Down
Loading
Loading