-
Notifications
You must be signed in to change notification settings - Fork 54
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
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
df61ade
Add a view in code editor link to the PHPCS warnings/errors
EvanHerman d697fbd
Update var name
EvanHerman 820b0ca
Add alt and aria-label to the edit link
EvanHerman 40f31d1
Remove theme reference
EvanHerman ec2632b
Update docblock
EvanHerman 6ac270b
Empty line at end of file
EvanHerman 51601fe
Move get_plugins call inside of conditional
EvanHerman 57c9e54
Update admin/admin.php based on suggestion
EvanHerman 7974800
Remove DISALLOW_FILE_EDIT condition
EvanHerman f74bb10
Remove duplicate var
EvanHerman 477c0ae
use %s
EvanHerman 32954e1
Add changelog entry
EvanHerman 85fff97
Merge branch 'legacy-plugin' into legacy-plugin
EvanHerman ea36507
Introduce a way to open files in an external editor
EvanHerman a4e5d56
Fix typo
EvanHerman bc41ab2
Fix comment
EvanHerman 1e66025
Update filter name
EvanHerman c9f2e76
Merge branch 'legacy-plugin' into legacy-plugin
EvanHerman d72c91d
Add missing editlink var
EvanHerman a193c22
Include Capabilities
bordoni c74de6a
Simplify to use `str_contains`
bordoni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will need to be translated at some point. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is awesome 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inline after script goodness! 😄
Source: https://github.com/ampproject/amp-wp/blob/956c8d298764e4b22b936347d7c8b591b2fadfc2/includes/validation/class-amp-validation-error-taxonomy.php#L1034-L1062