Skip to content

Commit

Permalink
Update logic for edit link column
Browse files Browse the repository at this point in the history
  • Loading branch information
mukeshpanchal27 committed Oct 26, 2023
1 parent 7f9f6e9 commit 5df8da4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
41 changes: 34 additions & 7 deletions assets/js/plugin-check-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,30 +355,56 @@
Date.now().toString( 36 ) +
Math.random().toString( 36 ).substr( 2 );

// Check if any errors or warnings have links.
const hasLinks =
hasLinksInResults( errors ) || hasLinksInResults( warnings );

// Render the file table.
resultsContainer.innerHTML += renderTemplate(
'plugin-check-results-table',
{ file, index }
{ file, index, hasLinks }
);
const resultsTable = document.getElementById(
'plugin-check__results-body-' + index
);

// Render results to the table.
renderResultRows( 'ERROR', errors, resultsTable );
renderResultRows( 'WARNING', warnings, resultsTable );
renderResultRows( 'ERROR', errors, resultsTable, hasLinks );
renderResultRows( 'WARNING', warnings, resultsTable, hasLinks );
}

/**
* Renders a result row onto the file table.
* Checks if there are any links in the results object.
*
* @since n.e.x.t
*
* @param {string} type The result type. Either ERROR or WARNING.
* @param {Object} results The results object.
* @param {Object} table The HTML table to append a result row to.
* @return {boolean} True if there are links, false otherwise.
*/
function hasLinksInResults( results ) {
for ( const line in results ) {
for ( const column in results[ line ] ) {
for ( let i = 0; i < results[ line ][ column ].length; i++ ) {
if ( results[ line ][ column ][ i ].link ) {
return true;
}
}
}
}
return false;
}

/**
* Renders a result row onto the file table.
*
* @since n.e.x.t
*
* @param {string} type The result type. Either ERROR or WARNING.
* @param {Object} results The results object.
* @param {Object} table The HTML table to append a result row to.
* @param {boolean} hasLinks Whether any result has links.
*/
function renderResultRows( type, results, table ) {
function renderResultRows( type, results, table, hasLinks ) {
// Loop over each result by the line, column and messages.
for ( const line in results ) {
for ( const column in results[ line ] ) {
Expand All @@ -396,6 +422,7 @@
message,
code,
link,
hasLinks,
}
);
}
Expand Down
7 changes: 5 additions & 2 deletions templates/results-row.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
<td>
{{data.message}}
</td>
<# if ( data.link ) { #>
<# if ( data.hasLinks ) { #>
<td>
<a href="{{data.link}}" aria-label="<?php esc_attr_e( 'View file in the plugin file editor.', 'plugin-check' ); ?>" target="_blank"><?php esc_html_e( 'View in code editor', 'plugin-check' ); ?></a>
<a href="{{data.link}}"<?php echo ( ! has_filter( 'wp_plugin_check_validation_error_source_file_editor_url_template' ) ) ? ' target="_blank"' : '' ?>>
<?php esc_html_e( 'View in code editor', 'plugin-check' ); ?>
<span class="screen-reader-text"><?php esc_html_e( '(opens in a new tab)', 'plugin-check' ); ?></span>
</a>
</td>
<# } #>
</tr>
Expand Down
7 changes: 6 additions & 1 deletion templates/results-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
<td>
<?php esc_html_e( 'Code', 'plugin-check' ); ?>
</td>
<td colspan="2">
<td>
<?php esc_html_e( 'Message', 'plugin-check' ); ?>
</td>
<# if ( data.hasLinks ) { #>
<td>
<?php esc_html_e( 'Edit Link', 'plugin-check' ); ?>
</td>
<# } #>
</tr>
</thead>
<tbody id="plugin-check__results-body-{{data.index}}"></tbody>
Expand Down

0 comments on commit 5df8da4

Please sign in to comment.