From df61ade3d31085584d2bf714f54eaf66edab8624 Mon Sep 17 00:00:00 2001 From: Evan Herman Date: Thu, 21 Sep 2023 19:13:11 -0400 Subject: [PATCH 01/19] Add a view in code editor link to the PHPCS warnings/errors --- admin/admin.php | 31 ++++++++++++++++++++++++++++++- checks/phpcs.php | 22 ++++++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/admin/admin.php b/admin/admin.php index caac190b7..5e05e7bbf 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -71,4 +71,33 @@ function render_page() { } } } -} \ No newline at end of file +} + +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 || 'theme-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 ) + ) + ); + } + } +); \ No newline at end of file diff --git a/checks/phpcs.php b/checks/phpcs.php index 4a18ff883..f12e9251b 100644 --- a/checks/phpcs.php +++ b/checks/phpcs.php @@ -110,16 +110,34 @@ 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 ); + $view_link = ''; + + if ( ! defined( 'DISALLOW_FILE_EDIT' ) || ! DISALLOW_FILE_EDIT ) { + $view_link = sprintf( + '%2$s', + 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' ) + ), + esc_html__( 'View in code editor', 'plugin-check' ) + ); + } $return[] = new $notice_class( $message['source'], sprintf( - '%s Line %d of file %s.
%s.
%s', + '%s Line %d of file %s.
%s.
%s%s', "{$message['source']}", $message['line'], $filename, rtrim( $message['message'], '.' ), - "
{$source_code}
" + "
{$source_code}
", + $view_link ) ); } From d697fbdeb6f75e787a36e155df6371cc44b2e7ba Mon Sep 17 00:00:00 2001 From: Evan Herman Date: Thu, 21 Sep 2023 19:19:37 -0400 Subject: [PATCH 02/19] Update var name --- checks/phpcs.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/checks/phpcs.php b/checks/phpcs.php index f12e9251b..b3b4c7edc 100644 --- a/checks/phpcs.php +++ b/checks/phpcs.php @@ -111,10 +111,10 @@ 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 ); - $view_link = ''; + $edit_link = ''; if ( ! defined( 'DISALLOW_FILE_EDIT' ) || ! DISALLOW_FILE_EDIT ) { - $view_link = sprintf( + $edit_link = sprintf( '%2$s', add_query_arg( [ @@ -137,7 +137,7 @@ protected function phpcs_result_to_warnings( $result ) { $filename, rtrim( $message['message'], '.' ), "
{$source_code}
", - $view_link + $edit_link ) ); } From 820b0ca0b1529a9a5e1354ee411ee0a22f45489e Mon Sep 17 00:00:00 2001 From: Evan Herman Date: Thu, 21 Sep 2023 19:33:17 -0400 Subject: [PATCH 03/19] Add alt and aria-label to the edit link --- checks/phpcs.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/checks/phpcs.php b/checks/phpcs.php index b3b4c7edc..2a80560c1 100644 --- a/checks/phpcs.php +++ b/checks/phpcs.php @@ -115,14 +115,21 @@ protected function phpcs_result_to_warnings( $result ) { if ( ! defined( 'DISALLOW_FILE_EDIT' ) || ! DISALLOW_FILE_EDIT ) { $edit_link = sprintf( - '%2$s', - 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' ) + '%3$s', + 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' ) ); From 40f31d1cdb4fee49c700c9d7ff7e65d251fd60dd Mon Sep 17 00:00:00 2001 From: Evan Herman Date: Thu, 21 Sep 2023 19:34:06 -0400 Subject: [PATCH 04/19] Remove theme reference --- admin/admin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/admin.php b/admin/admin.php index 5e05e7bbf..16c2553c9 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -81,7 +81,7 @@ function ( $hook_suffix ) { } $line = (int) $_GET['line']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended - if ( 'plugin-editor.php' === $hook_suffix || 'theme-editor.php' === $hook_suffix ) { + if ( 'plugin-editor.php' === $hook_suffix ) { wp_add_inline_script( 'wp-theme-plugin-editor', sprintf( From ec2632bb8d4d87c872e7be3565ff5d6a6aad9e7f Mon Sep 17 00:00:00 2001 From: Evan Herman Date: Thu, 21 Sep 2023 19:38:28 -0400 Subject: [PATCH 05/19] Update docblock --- admin/admin.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/admin/admin.php b/admin/admin.php index 16c2553c9..5cb6aaadd 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -73,6 +73,9 @@ function render_page() { } } +/** + * Jump to the requested line when opening the file editor. + */ add_action( 'admin_enqueue_scripts', function ( $hook_suffix ) { From 6ac270b1f0ef22ccd4f4e953eb6da89551109e72 Mon Sep 17 00:00:00 2001 From: Evan Herman Date: Thu, 21 Sep 2023 19:38:43 -0400 Subject: [PATCH 06/19] Empty line at end of file --- admin/admin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/admin.php b/admin/admin.php index 5cb6aaadd..b087512c8 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -103,4 +103,4 @@ function( originalInitCodeEditor ) { ); } } -); \ No newline at end of file +); From 51601fe6e8472fb8d4ddaa5604d7e7b865079e48 Mon Sep 17 00:00:00 2001 From: Evan Herman Date: Thu, 21 Sep 2023 19:40:21 -0400 Subject: [PATCH 07/19] Move get_plugins call inside of conditional --- checks/phpcs.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/checks/phpcs.php b/checks/phpcs.php index 2a80560c1..dffebc2d2 100644 --- a/checks/phpcs.php +++ b/checks/phpcs.php @@ -110,10 +110,11 @@ 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 = ''; if ( ! defined( 'DISALLOW_FILE_EDIT' ) || ! DISALLOW_FILE_EDIT ) { + $plugin_data = get_plugins( '/' . $this->slug ); + $edit_link = sprintf( '%3$s', esc_url( From 57c9e54078a6d928eaa45d0a1d30a884ce52acf6 Mon Sep 17 00:00:00 2001 From: Evan Herman Date: Thu, 21 Sep 2023 20:01:41 -0400 Subject: [PATCH 08/19] Update admin/admin.php based on suggestion Co-authored-by: Gustavo Bordoni --- admin/admin.php | 64 ++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/admin/admin.php b/admin/admin.php index b087512c8..1663ce85e 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -74,33 +74,43 @@ function render_page() { } /** - * Jump to the requested line when opening the file editor. + * 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 */ -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 +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 ) { - 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 ) - ) - ); - } + 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 ) + ) + ); +} + +/** + * Jump to the requested line when opening the file editor. + */ +add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\jump_to_line_code_editor' ); From 79748002422e3ca7416d5f5173cd27b52d5d4e89 Mon Sep 17 00:00:00 2001 From: Evan Herman Date: Thu, 21 Sep 2023 20:13:24 -0400 Subject: [PATCH 09/19] Remove DISALLOW_FILE_EDIT condition --- checks/phpcs.php | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/checks/phpcs.php b/checks/phpcs.php index dffebc2d2..89709b792 100644 --- a/checks/phpcs.php +++ b/checks/phpcs.php @@ -110,31 +110,28 @@ 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 = ''; - if ( ! defined( 'DISALLOW_FILE_EDIT' ) || ! DISALLOW_FILE_EDIT ) { - $plugin_data = get_plugins( '/' . $this->slug ); - - $edit_link = sprintf( - '%3$s', - 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' ) - ); - } + $edit_link = sprintf( + '%3$s', + 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' ) + ); $return[] = new $notice_class( $message['source'], From f74bb10d17d15dc69cafb2be0548ef8260b13f2b Mon Sep 17 00:00:00 2001 From: Evan Herman Date: Fri, 22 Sep 2023 09:25:10 -0400 Subject: [PATCH 10/19] Remove duplicate var --- checks/phpcs.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/checks/phpcs.php b/checks/phpcs.php index 89709b792..db3144989 100644 --- a/checks/phpcs.php +++ b/checks/phpcs.php @@ -111,9 +111,7 @@ 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 = ''; - - $edit_link = sprintf( + $edit_link = sprintf( '%3$s', esc_url( add_query_arg( From 477c0aeeb77567d765aacfb3a75e1f8ada195b53 Mon Sep 17 00:00:00 2001 From: Evan Herman Date: Fri, 22 Sep 2023 09:25:48 -0400 Subject: [PATCH 11/19] use %s --- checks/phpcs.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/checks/phpcs.php b/checks/phpcs.php index db3144989..2623de218 100644 --- a/checks/phpcs.php +++ b/checks/phpcs.php @@ -124,8 +124,8 @@ protected function phpcs_result_to_warnings( $result ) { ) ), sprintf( - /* translators: %1$s is the path to a plugin file. */ - esc_attr__( 'View %1$s in the plugin file editor.', 'plugin-check' ), + /* 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' ) From 32954e1598608ecc449b6b179670b9f9b123481b Mon Sep 17 00:00:00 2001 From: Evan Herman Date: Fri, 22 Sep 2023 09:27:04 -0400 Subject: [PATCH 12/19] Add changelog entry --- readme.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/readme.txt b/readme.txt index af45232a2..7e4a43304 100644 --- a/readme.txt +++ b/readme.txt @@ -42,6 +42,10 @@ This plugin checker is not perfect, and never will be. It is only a tool to help == Changelog == += [0.3.0] 2023-09-? = + +* Added - 'View in code editor' link beneath each PHPCS error or warning. + = [0.2.0] 2023-09-18 = * Feature - Enable modification of the PHP Binary path used by the plugin with `PLUGIN_CHECK_PHP_BIN` constant. From ea3650739d4510e6f5d908d25e41b1dbea44571f Mon Sep 17 00:00:00 2001 From: Evan Herman Date: Fri, 22 Sep 2023 10:30:09 -0400 Subject: [PATCH 13/19] Introduce a way to open files in an external editor --- checks/phpcs.php | 113 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 102 insertions(+), 11 deletions(-) diff --git a/checks/phpcs.php b/checks/phpcs.php index 2623de218..21231ff29 100644 --- a/checks/phpcs.php +++ b/checks/phpcs.php @@ -110,19 +110,9 @@ 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 = sprintf( '%3$s', - 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' ) - ) - ), + $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' ), @@ -148,4 +138,105 @@ protected function phpcs_result_to_warnings( $result ) { 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( 'pcp_validation_error_source_file_editor_url_template', function () { + * return 'phpstorm://open?file={{file}}&line={{line}}'; + * } ); + * + * # VSCode + * add_filter( 'pcp_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( 'pcp_validation_error_source_file_editor_url_template', null ); + + // Supply the file path to the editor template. + if ( null !== $editor_url_template && false !== strpos( $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( 'pcp_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_first_key( $plugin_data ) ), + 'file' => rawurlencode( $this->slug . '/' . $filename ), + 'line' => rawurlencode( $line ), + ], + admin_url( 'plugin-editor.php' ) + ) + ); + } + + return $edit_url; + } } \ No newline at end of file From a4e5d563c4b85c9a1175b5bcfcf3a8441a40ab3e Mon Sep 17 00:00:00 2001 From: Evan Herman Date: Fri, 22 Sep 2023 10:30:52 -0400 Subject: [PATCH 14/19] Fix typo --- checks/phpcs.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/checks/phpcs.php b/checks/phpcs.php index 21231ff29..37447e9df 100644 --- a/checks/phpcs.php +++ b/checks/phpcs.php @@ -228,7 +228,7 @@ private function get_file_editor_url( $filename, $line ) { return esc_url( add_query_arg( [ - 'plugin' => rawurlencode( $this->slug . '/' . array_first_key( $plugin_data ) ), + 'plugin' => rawurlencode( $this->slug . '/' . array_key_first( $plugin_data ) ), 'file' => rawurlencode( $this->slug . '/' . $filename ), 'line' => rawurlencode( $line ), ], From bc41ab25dc1236571c078f5a2276a2f3154240b9 Mon Sep 17 00:00:00 2001 From: Evan Herman Date: Fri, 22 Sep 2023 10:34:35 -0400 Subject: [PATCH 15/19] Fix comment --- checks/phpcs.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/checks/phpcs.php b/checks/phpcs.php index 37447e9df..6e16ed65d 100644 --- a/checks/phpcs.php +++ b/checks/phpcs.php @@ -167,12 +167,12 @@ private function get_file_editor_url( $filename, $line ) { * 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 + * # PhpStorm * add_filter( 'pcp_validation_error_source_file_editor_url_template', function () { * return 'phpstorm://open?file={{file}}&line={{line}}'; * } ); * - * # VSCode + * # VS Code * add_filter( 'pcp_validation_error_source_file_editor_url_template', function () { * return 'vscode://file/{{file}}:{{line}}'; * } ); From 1e66025fc040b8c75cd7a40f8622ff21618efbac Mon Sep 17 00:00:00 2001 From: Evan Herman Date: Fri, 22 Sep 2023 11:19:49 -0400 Subject: [PATCH 16/19] Update filter name --- checks/phpcs.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/checks/phpcs.php b/checks/phpcs.php index 6e16ed65d..1c6d02e6c 100644 --- a/checks/phpcs.php +++ b/checks/phpcs.php @@ -168,12 +168,12 @@ private function get_file_editor_url( $filename, $line ) { * open in an IDE, the following filters can be used: * * # PhpStorm - * add_filter( 'pcp_validation_error_source_file_editor_url_template', function () { + * add_filter( 'plugin_check_validation_error_source_file_editor_url_template', function () { * return 'phpstorm://open?file={{file}}&line={{line}}'; * } ); * * # VS Code - * add_filter( 'pcp_validation_error_source_file_editor_url_template', function () { + * add_filter( 'plugin_check_validation_error_source_file_editor_url_template', function () { * return 'vscode://file/{{file}}:{{line}}'; * } ); * @@ -183,7 +183,7 @@ private function get_file_editor_url( $filename, $line ) { * * @param string|null $editor_url_template Editor URL template. */ - $editor_url_template = apply_filters( 'pcp_validation_error_source_file_editor_url_template', null ); + $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 && false !== strpos( $editor_url_template, '{{file}}' ) ) { @@ -203,7 +203,7 @@ private function get_file_editor_url( $filename, $line ) { * @param string|null $editor_url_template Editor URL template. * @param array $source Source information. */ - $file_path = apply_filters( 'pcp_validation_error_source_file_path', $file_path, array( $this->slug, $filename, $line) ); + $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( [ From d72c91dd0823b5d0162972e6f5f26d73088547b4 Mon Sep 17 00:00:00 2001 From: Evan Herman Date: Fri, 22 Sep 2023 11:43:51 -0400 Subject: [PATCH 17/19] Add missing editlink var --- checks/phpcs.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/checks/phpcs.php b/checks/phpcs.php index 89e299621..c70967b26 100644 --- a/checks/phpcs.php +++ b/checks/phpcs.php @@ -129,7 +129,8 @@ protected function phpcs_result_to_warnings( $result ) { $message['line'], $filename, rtrim( $message['message'], '.' ), - "
{$source_code}
" + "
{$source_code}
", + $edit_link ) ); } From a193c229a05a972c3d7ce125336736cecb416425 Mon Sep 17 00:00:00 2001 From: Gustavo Bordoni Date: Fri, 22 Sep 2023 15:15:04 -0400 Subject: [PATCH 18/19] Include Capabilities Co-authored-by: Felix Arntz --- checks/phpcs.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/checks/phpcs.php b/checks/phpcs.php index c70967b26..a34329295 100644 --- a/checks/phpcs.php +++ b/checks/phpcs.php @@ -110,16 +110,18 @@ protected function phpcs_result_to_warnings( $result ) { } $source_code = esc_html( trim( file( $this->path . '/' . $filename )[ $message['line'] - 1 ] ) ); - $edit_link = sprintf( - '%3$s', - $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' ) - ); + if ( current_user_can( 'edit_plugins' ) { + $edit_link = sprintf( + '%3$s', + $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'], From c74de6ad45f02b368527caf9f44eabf7d59c5026 Mon Sep 17 00:00:00 2001 From: Gustavo Bordoni Date: Fri, 22 Sep 2023 15:15:21 -0400 Subject: [PATCH 19/19] Simplify to use `str_contains` Co-authored-by: Felix Arntz --- checks/phpcs.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/checks/phpcs.php b/checks/phpcs.php index a34329295..4ba5fc13d 100644 --- a/checks/phpcs.php +++ b/checks/phpcs.php @@ -188,7 +188,7 @@ private function get_file_editor_url( $filename, $line ) { $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 && false !== strpos( $editor_url_template, '{{file}}' ) ) { + 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;