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 7 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
34 changes: 33 additions & 1 deletion admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,36 @@ function render_page() {
}
}
}
}
}

/**
* Jump to the requested line when opening the file editor.
*/
add_action(
'admin_enqueue_scripts',
function ( $hook_suffix ) {
if ( ! isset( $_GET['line'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return;
}
$line = (int) $_GET['line']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

if ( 'plugin-editor.php' === $hook_suffix ) {
wp_add_inline_script(
'wp-theme-plugin-editor',
sprintf(
'
(
function( originalInitCodeEditor ) {
wp.themePluginEditor.initCodeEditor = function init() {
originalInitCodeEditor.apply( this, arguments );
this.instance.codemirror.doc.setCursor( %d - 1 );
};
}
)( wp.themePluginEditor.initCodeEditor );
',
wp_json_encode( $line )
)
);
}
}
);
EvanHerman marked this conversation as resolved.
Show resolved Hide resolved
30 changes: 28 additions & 2 deletions checks/phpcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,42 @@ protected function phpcs_result_to_warnings( $result ) {
}

$source_code = esc_html( trim( file( $this->path . '/' . $filename )[ $message['line'] - 1 ] ) );
$edit_link = '';
EvanHerman marked this conversation as resolved.
Show resolved Hide resolved

if ( ! defined( 'DISALLOW_FILE_EDIT' ) || ! DISALLOW_FILE_EDIT ) {
$plugin_data = get_plugins( '/' . $this->slug );

$edit_link = sprintf(
'<a href="%1$s" title="%2$s" aria-label="%2$s" target="_blank">%3$s</a>',
esc_url(
add_query_arg(
[
'plugin' => rawurlencode( $this->slug . '/' . array_key_first( $plugin_data ) ),
'file' => rawurlencode( $this->slug . '/' . $filename ),
'line' => rawurlencode( $message['line'] ),
],
admin_url( 'plugin-editor.php' )
)
),
sprintf(
/* translators: %1$s is the path to a plugin file. */
esc_attr__( 'View %1$s in the plugin file editor.', 'plugin-check' ),
$this->slug . '/' . $filename
),
esc_html__( 'View in code editor', 'plugin-check' )
);
}
EvanHerman marked this conversation as resolved.
Show resolved Hide resolved

$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><code>{$source_code}</code></pre>"
"<pre><code>{$source_code}</code></pre>",
$edit_link
)
);
}
Expand Down