PluginProbe
Plugin Check (PCP) / 1.5.0
Plugin Check (PCP) v1.5.0
2.1.0 trunk 0.1 0.2.0 0.2.1 0.2.2 0.2.3 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.3.0 1.3.1 1.4.0 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 2.0.0 ci-artifacts
plugin-check / includes / Traits / File_Editor_URL.php

File_Editor_URL.php in Plugin Check (PCP) 1.5.0, at includes/Traits/File_Editor_URL.php

127 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Trait WordPress\Plugin_Check\Traits\File_Editor_URL
4 *
5 * @package plugin-check
6 */
7
8 namespace WordPress\Plugin_Check\Traits;
9
10 use WordPress\Plugin_Check\Checker\Check_Result;
11
12 /**
13 * Trait for file editor URL.
14 *
15 * @since 1.0.0
16 */
17 trait File_Editor_URL {
18
19 /**
20 * Gets the URL for opening the plugin file in an external editor.
21 *
22 * @since 1.0.0
23 *
24 * @param Check_Result $result The check result to amend, including the plugin context to check.
25 * @param string $filename Error file name.
26 * @param int $line Optional. Line number of error. Default 0 (no specific line).
27 * @return string|null File editor URL or null if not available.
28 */
29 protected function get_file_editor_url( Check_Result $result, $filename, $line = 0 ) {
30
31 $edit_url = null;
32
33 $plugin_path = $result->plugin()->path( '/' );
34 $plugin_slug = $result->plugin()->slug();
35 $filename = str_replace( $plugin_path, '', $filename );
36 /**
37 * Filters the template for the URL for linking to an external editor to open a file for editing.
38 *
39 * Users of IDEs that support opening files in via web protocols can use this filter to override
40 * the edit link to result in their editor opening rather than the plugin editor.
41 *
42 * The initial filtered value is null, requiring extension plugins to supply the URL template
43 * string themselves. If no template string is provided, links to the plugin editors will
44 * be provided if available. For example, for an extension plugin to cause file edit links to
45 * open in an IDE, the following filters can be used:
46 *
47 * # PhpStorm
48 * add_filter( 'wp_plugin_check_validation_error_source_file_editor_url_template', function () {
49 * return 'phpstorm://open?file={{file}}&line={{line}}';
50 * } );
51 *
52 * # VS Code
53 * add_filter( 'wp_plugin_check_validation_error_source_file_editor_url_template', function () {
54 * return 'vscode://file/{{file}}:{{line}}';
55 * } );
56 *
57 * For a template to be considered, the string '{{file}}' must be present in the filtered value.
58 *
59 * @since 1.0.0
60 *
61 * @param string|null $editor_url_template Editor URL template. default null.
62 */
63 $editor_url_template = apply_filters( 'wp_plugin_check_validation_error_source_file_editor_url_template', null );
64
65 // Supply the file path to the editor template.
66 if ( is_string( $editor_url_template ) && str_contains( $editor_url_template, '{{file}}' ) ) {
67 $file_path = WP_PLUGIN_DIR . '/' . $plugin_slug;
68 if ( $plugin_slug !== $filename ) {
69 $file_path .= '/' . $filename;
70 }
71
72 if ( file_exists( $file_path ) ) {
73 /**
74 * Filters the file path to be opened in an external editor for a given PHPCS error source.
75 *
76 * This is useful to map the file path from inside of a Docker container or VM to the host machine.
77 *
78 * @since 1.0.0
79 *
80 * @param string|null $editor_url_template Editor URL template.
81 * @param array $source Source information.
82 */
83 $file_path = apply_filters( 'wp_plugin_check_validation_error_source_file_path', $file_path, array( $plugin_slug, $filename, $line ) );
84 if ( $file_path ) {
85 $edit_url = str_replace(
86 array(
87 '{{file}}',
88 '{{line}}',
89 ),
90 array(
91 rawurlencode( $file_path ),
92 $line,
93 ),
94 $editor_url_template
95 );
96 }
97 }
98 }
99
100 // Fall back to using the plugin editor if no external editor is offered.
101 if ( ! $edit_url && current_user_can( 'edit_plugins' ) ) {
102 $file = '';
103
104 if ( $result->plugin()->is_single_file_plugin() ) {
105 $file = $filename;
106 } elseif ( $result->plugin()->is_file_editable( $filename ) ) {
107 $file = $plugin_slug . '/' . $filename;
108 }
109
110 if ( ! empty( $file ) ) {
111 $query_args = array(
112 'plugin' => rawurlencode( $result->plugin()->basename() ),
113 'file' => rawurlencode( $file ),
114 );
115 if ( $line ) {
116 $query_args['line'] = $line;
117 }
118 return add_query_arg(
119 $query_args,
120 admin_url( 'plugin-editor.php' )
121 );
122 }
123 }
124 return $edit_url;
125 }
126 }
127