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 9 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 @@ -71,4 +71,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' );
27 changes: 25 additions & 2 deletions checks/phpcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,39 @@ protected function phpcs_result_to_warnings( $result ) {
}

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

$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' ),
EvanHerman marked this conversation as resolved.
Show resolved Hide resolved
$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><code>{$source_code}</code></pre>"
"<pre><code>{$source_code}</code></pre>",
$edit_link
)
);
}
Expand Down