Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a 'View in code editor' link to the PHPCS warnings/errors #262

Merged
merged 21 commits into from
Sep 22, 2023
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
44 changes: 43 additions & 1 deletion admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,46 @@ function render_page() {
}
}
}
}
}

/**
* Includes JS to jump to a specific line in the code editor.
*
* @since 0.2.1
*
* @param string $hook_suffix Which admin page we're on.
*
* @return void
*/
function jump_to_line_code_editor( $hook_suffix ) {
$line = (int) ( $_GET['line'] ?? 0 ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! $line ) {
return;
}

if ( 'plugin-editor.php' !== $hook_suffix ) {
return;
}

wp_add_inline_script(
'wp-theme-plugin-editor',
sprintf(
'
(
( originalInitCodeEditor ) => {
wp.themePluginEditor.initCodeEditor = function() {
originalInitCodeEditor.apply( this, arguments );
this.instance.codemirror.doc.setCursor( %d - 1 );
};
}
)( wp.themePluginEditor.initCodeEditor );
',
wp_json_encode( $line )
)
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

/**
* Jump to the requested line when opening the file editor.
*/
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\jump_to_line_code_editor' );
118 changes: 116 additions & 2 deletions checks/phpcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,135 @@ protected function phpcs_result_to_warnings( $result ) {
}

$source_code = esc_html( trim( file( $this->path . '/' . $filename )[ $message['line'] - 1 ] ) );
if ( current_user_can( 'edit_plugins' ) {
$edit_link = sprintf(
'<a href="%1$s" title="%2$s" aria-label="%2$s" target="_blank">%3$s</a>',
$this->get_file_editor_url( $filename, $message['line'] ),
sprintf(
/* translators: %s is the path to a plugin file. */
esc_attr__( 'View %s in the plugin file editor.', 'plugin-check' ),
$this->slug . '/' . $filename
),
esc_html__( 'View in code editor', 'plugin-check' )
);
}

$return[] = new $notice_class(
$message['source'],
sprintf(
'%s Line %d of file %s.<br>%s.<br>%s',
'%s Line %d of file %s.<br>%s.<br>%s%s',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be translated at some point.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that is a great catch too, I will handle that in a separate PR.

"<strong>{$message['source']}</strong>",
$message['line'],
$filename,
rtrim( $message['message'], '.' ),
"<pre class='wp-plugin-check-code'><code>{$source_code}</code></pre>"
"<pre class='wp-plugin-check-code'><code>{$source_code}</code></pre>",
$edit_link
)
);
}
} );

return $return;
}

/**
* Get the URL for opening the plugin file in an external editor.
*
* @since 0.2.1
*
* @param array $filename Source of PHPCS error.
* @param array $line Line number of PHPCS error.
*
* @return string|null File editor URL or null if not available.
*/
private function get_file_editor_url( $filename, $line ) {
if ( ! isset( $filename, $line ) ) {
return null;
}

$edit_url = null;

/**
* 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 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 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:
*
* # PhpStorm
* add_filter( 'plugin_check_validation_error_source_file_editor_url_template', function () {
* return 'phpstorm://open?file={{file}}&line={{line}}';
* } );
*
* # VS Code
* add_filter( 'plugin_check_validation_error_source_file_editor_url_template', function () {
* return 'vscode://file/{{file}}:{{line}}';
* } );
*
* For a template to be considered, the string '{{file}}' must be present in the filtered value.
*
* @since 0.2.1
*
* @param string|null $editor_url_template Editor URL template.
*/
$editor_url_template = apply_filters( 'plugin_check_validation_error_source_file_editor_url_template', null );

// Supply the file path to the editor template.
if ( null !== $editor_url_template && str_contains( $editor_url_template, '{{file}}' ) ) {
$file_path = WP_PLUGIN_DIR . '/' . $this->slug;
if ( $this->slug !== $filename ) {
$file_path .= '/' . $filename;
}

if ( $file_path && 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.
*
* @since 0.2.1
*
* @param string|null $editor_url_template Editor URL template.
* @param array $source Source information.
*/
$file_path = apply_filters( 'plugin_check_validation_error_source_file_path', $file_path, array( $this->slug, $filename, $line) );
if ( $file_path ) {
$edit_url = str_replace(
[
'{{file}}',
'{{line}}',
],
[
rawurlencode( $file_path ),
rawurlencode( $line ),
],
$editor_url_template
);
}

}
}

// Fall back to using the plugin editor if no external editor is offered.
if ( ! $edit_url ) {
$plugin_data = get_plugins( '/' . $this->slug );

return esc_url(
add_query_arg(
[
'plugin' => rawurlencode( $this->slug . '/' . array_key_first( $plugin_data ) ),
'file' => rawurlencode( $this->slug . '/' . $filename ),
'line' => rawurlencode( $line ),
],
admin_url( 'plugin-editor.php' )
)
);
}

return $edit_url;
}
}
3 changes: 2 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ This plugin checker is not perfect, and never will be. It is only a tool to help
* Fix - Ensure `readme.txt` has priority over `readme.md` when both are present. Props @bordoni, @afragen [#258](https://github.com/10up/plugin-check/pull/258)
* Fix - Ensure that the PHPCS check runs even when the PHPCS binary is not executable. Props @bordoni, @shawn-digitalpoint, @mrfoxtalbot [#254](https://github.com/10up/plugin-check/pull/254)
* Fix - Readme changes and typos. Props @aaronjorbin. [#261](https://github.com/10up/plugin-check/pull/261)
* Fix - Long lines of code with PHPCS check no longer expand over the size of the notice. Props @bordoni @felixarntz. [#263](https://github.com/10up/plugin-check/pull/263)
* Added - 'View in code editor' link beneath each PHPCS error or warning. Props @EvanHerman, @westonruter, @felixarntz, @mukeshpanchal27 [#262](https://github.com/10up/plugin-check/pull/262)
* Fix - Long lines of code with PHPCS check no longer expand over the size of the notice. Props @bordoni, @felixarntz. [#263](https://github.com/10up/plugin-check/pull/263)

= [0.2.0] 2023-09-18 =

Expand Down
Loading