Skip to content

Commit 8d68000

Browse files
committed
Inline SQL queries where possible to avoid WPCS errors
1 parent 46b8802 commit 8d68000

File tree

6 files changed

+59
-60
lines changed

6 files changed

+59
-60
lines changed

php/class-list-table.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -835,12 +835,13 @@ private function fetch_shared_network_snippets() {
835835
} else {
836836

837837
$active_shared_snippets = get_option( 'active_shared_network_snippets', array() );
838+
$active_shared_snippet_format = implode( ',', array_fill( 0, count( $ids ), '%d' ) );
838839

839-
$sql = sprintf( "SELECT * FROM {$db->ms_table} WHERE id IN (%s)",
840-
implode( ',', array_fill( 0, count( $ids ), '%d' ) )
841-
);
842-
843-
$shared_snippets = $wpdb->get_results( $wpdb->prepare( $sql, $ids ), ARRAY_A );
840+
$shared_snippets = $wpdb->get_results( $wpdb->prepare( "
841+
SELECT * FROM $db->ms_table
842+
WHERE id IN ($active_shared_snippet_format)",
843+
$ids
844+
), ARRAY_A );
844845

845846
foreach ( $shared_snippets as $index => $snippet ) {
846847
$snippet = new Code_Snippet( $snippet );
@@ -1031,7 +1032,7 @@ private function usort_reorder_callback( $a, $b ) {
10311032

10321033
// sort ascending by default
10331034
$order = isset( $_REQUEST['order'] ) ? strtolower( sanitize_key( $_REQUEST['order'] ) ) : '';
1034-
if ( $order !== 'asc' && $order !== 'desc' ) {
1035+
if ( 'asc' !== $order && 'desc' !== $order ) {
10351036
$order = apply_filters( 'code_snippets/list_table/default_order', 'asc' );
10361037
}
10371038

php/class-upgrade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ private function migrate_scope_data( $table_name ) {
150150
);
151151

152152
foreach ( $scopes as $scope_number => $scope_name ) {
153-
$wpdb->query( sprintf(
154-
"UPDATE %s SET scope = '%s' WHERE scope = %d",
155-
$table_name, $scope_name, $scope_number
153+
$wpdb->query( $wpdb->prepare(
154+
"UPDATE $table_name SET scope = %s WHERE scope = %d",
155+
$scope_name, $scope_number
156156
) );
157157
}
158158
}

php/import-export.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ function _code_snippets_save_imported_snippets( $snippets, $multisite = null, $d
6161
/**f
6262
* Imports snippets from a JSON file
6363
*
64-
* @since 2.9.7
65-
*
66-
* @uses save_snippet() to add the snippets to the database
67-
*
6864
* @param string $file The path to the file to import
6965
* @param bool|null $multisite Import into network-wide table or site-wide table?
7066
* @param string $dup_action Action to take if duplicate snippets are detected. Can be 'skip', 'ignore', or 'replace'
7167
*
7268
* @return array|bool An array of imported snippet IDs on success, false on failure
69+
* @since 2.9.7
70+
*
71+
* @uses save_snippet() to add the snippets to the database
72+
*
7373
*/
7474
function import_snippets_json( $file, $multisite = null, $dup_action = 'ignore' ) {
7575

@@ -97,15 +97,15 @@ function import_snippets_json( $file, $multisite = null, $dup_action = 'ignore'
9797
/**
9898
* Imports snippets from an XML file
9999
*
100-
* @since 2.0
101-
*
102-
* @uses save_snippet() to add the snippets to the database
103-
*
104100
* @param string $file The path to the file to import
105101
* @param bool|null $multisite Import into network-wide table or site-wide table?
106102
* @param string $dup_action Action to take if duplicate snippets are detected. Can be 'skip', 'ignore', or 'replace'
107103
*
108104
* @return array|bool An array of imported snippet IDs on success, false on failure
105+
* @since 2.0
106+
*
107+
* @uses save_snippet() to add the snippets to the database
108+
*
109109
*/
110110
function import_snippets_xml( $file, $multisite = null, $dup_action = 'ignore' ) {
111111

@@ -172,19 +172,11 @@ function code_snippets_prepare_export( $format, $ids, $table_name = '', $mime_ty
172172
global $wpdb;
173173

174174
/* Fetch the snippets from the database */
175-
if ( '' === $table_name ) {
176-
$table_name = code_snippets()->db->get_table_name();
177-
}
178-
179175
if ( count( $ids ) ) {
176+
$table_name = '' === $table_name ? code_snippets()->db->get_table_name() : $table_name;
180177

181-
$sql = sprintf(
182-
'SELECT * FROM %s WHERE id IN (%s)', $table_name,
183-
implode( ',', array_fill( 0, count( $ids ), '%d' ) )
184-
);
185-
186-
$snippets = $wpdb->get_results( $wpdb->prepare( $sql, $ids ), ARRAY_A );
187-
178+
$sql_in_format = implode( ',', array_fill( 0, count( $ids ), '%d' ) );
179+
$snippets = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table_name WHERE id IN ($sql_in_format)", $ids ), ARRAY_A );
188180
} else {
189181
$snippets = array();
190182
}

php/snippet-ops.php

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function get_snippets( array $ids = array(), $multisite = null, array $args = ar
7171

7272
/* Build a query containing the specified IDs if there are any */
7373
if ( $ids_count > 1 ) {
74-
$sql .= sprintf( ' AND id IN (%s)', implode( ',', array_fill( 0, $ids_count, '%d' ) ) );
74+
$sql .= sprintf( ' AND id IN (%s)', implode( ',', array_fill( 0, $ids_count, '%d' ) ) );
7575
$sql_params = array_merge( $sql_params, array_values( $ids ) );
7676
}
7777

@@ -260,8 +260,7 @@ function activate_snippets( array $ids, $multisite = null ) {
260260

261261
/* Build a SQL query containing all the provided snippet IDs */
262262
$ids_format = implode( ',', array_fill( 0, count( $ids ), '%d' ) );
263-
$sql = sprintf( 'SELECT id, code FROM %s WHERE id IN (%s);', $table, $ids_format );
264-
$rows = $wpdb->get_results( $wpdb->prepare( $sql, $ids ) );
263+
$rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, code FROM $table WHERE id IN ($ids_format)", $ids ) );
265264

266265
if ( ! $rows ) {
267266
return array();
@@ -286,8 +285,7 @@ function activate_snippets( array $ids, $multisite = null ) {
286285

287286
/* Build a SQL query containing all the valid snippet IDs and activate the valid snippets */
288287
$ids_format = implode( ',', array_fill( 0, count( $valid_ids ), '%d' ) );
289-
$sql = sprintf( 'UPDATE %s SET active = 1 WHERE id IN (%s);', $table, $ids_format );
290-
$wpdb->query( $wpdb->prepare( $sql, $valid_ids ) );
288+
$wpdb->query( $wpdb->prepare( "UPDATE $table SET active = 1 WHERE id IN ($ids_format)", $valid_ids ) );
291289

292290
/* Remove snippet from shared network snippet list if it was Network Activated */
293291
if ( $table === $db->ms_table && $shared_network_snippets = get_site_option( 'shared_network_snippets', false ) ) {
@@ -495,14 +493,17 @@ function execute_active_snippets() {
495493
$db = code_snippets()->db;
496494

497495
$current_scope = is_admin() ? 'admin' : 'front-end';
498-
$queries = array();
499-
500-
$sql_format = "SELECT id, code, scope FROM %s WHERE scope IN ('global', 'single-use', %%s) ";
501-
$order = 'ORDER BY priority ASC, id ASC';
496+
$results = array();
502497

503498
/* Fetch snippets from site table */
504499
if ( $wpdb->get_var( "SHOW TABLES LIKE '$db->table'" ) === $db->table ) {
505-
$queries[ $db->table ] = $wpdb->prepare( sprintf( $sql_format, $db->table ) . 'AND active=1 ' . $order, $current_scope );
500+
$results[ $db->table ] = $wpdb->get_results( $wpdb->prepare( "
501+
SELECT id, code, scope
502+
FROM $db->table WHERE scope IN ('global', 'single-use', %s)
503+
AND active = 1
504+
ORDER BY priority, id",
505+
$current_scope
506+
), ARRAY_A );
506507
}
507508

508509
/* Fetch snippets from the network table */
@@ -515,23 +516,31 @@ function execute_active_snippets() {
515516
/* Build a list of "%d, %d, %d ..." for every active network shared snippet we have */
516517
$active_shared_ids_format = implode( ',', array_fill( 0, count( $active_shared_ids ), '%d' ) );
517518

518-
/* Include them in the query */
519-
$sql = sprintf( $sql_format, $db->ms_table ) . " AND (active=1 OR id IN ($active_shared_ids_format)) $order";
520-
521519
/* Add the scope number to the IDs array, so that it is the first variable in the query */
522520
array_unshift( $active_shared_ids, $current_scope );
523-
$queries[ $db->ms_table ] = $wpdb->prepare( $sql, $active_shared_ids );
521+
522+
$results[ $db->ms_table ] = $wpdb->get_results( $wpdb->prepare( "
523+
SELECT id, code, scope
524+
FROM $db->ms_table WHERE scope IN ('global', 'single-use', %s)
525+
AND (active = 1 OR id IN ($active_shared_ids_format))
526+
ORDER BY priority, id",
527+
$active_shared_ids
528+
), ARRAY_A );
529+
524530
array_shift( $active_shared_ids ); // remove it afterwards as we need this variable later
525531

526532
} else {
527-
$sql = sprintf( $sql_format, $db->ms_table ) . 'AND active=1 ' . $order;
528-
$queries[ $db->ms_table ] = $wpdb->prepare( $sql, $current_scope );
533+
$results[ $db->ms_table ] = $wpdb->get_results( $wpdb->prepare( "
534+
SELECT id, code, scope
535+
FROM $db->ms_table WHERE scope IN ('global', 'single-use', %s)
536+
AND active = 1
537+
ORDER BY priority, id",
538+
$current_scope
539+
), ARRAY_A );
529540
}
530541
}
531542

532-
foreach ( $queries as $table_name => $query ) {
533-
$active_snippets = $wpdb->get_results( $query, 'ARRAY_A' );
534-
543+
foreach ( $results as $table_name => $active_snippets ) {
535544
if ( ! is_array( $active_snippets ) ) {
536545
continue;
537546
}

php/views/edit.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ class="<?php echo implode( ' ', $classes ); ?>">
9595

9696
<div class="snippet-editor">
9797
<textarea id="snippet_code" name="snippet_code" rows="200" spellcheck="false"
98-
style="font-family: monospace; width: 100%;"><?php
99-
echo esc_textarea( $snippet->code );
100-
?></textarea>
98+
style="font-family: monospace; width: 100%;"><?php echo esc_textarea( $snippet->code ); ?></textarea>
10199

102100
<div class="snippet-editor-help">
103101

@@ -126,15 +124,15 @@ class="<?php echo implode( ' ', $classes ); ?>">
126124
<tr>
127125
<td><?php esc_html_e( 'Save changes', 'code-snippets' ); ?></td>
128126
<td>
129-
<kbd class="pc-key"><?php echo esc_html( $keys['Ctrl'] ); ?></kbd><kbd class="mac-key"><?php
130-
echo esc_html( $keys['Cmd'] ); ?></kbd>&hyphen;<kbd><?php echo esc_html( $keys['S'] ); ?></kbd>
127+
<kbd class="pc-key"><?php echo esc_html( $keys['Ctrl'] ); ?></kbd><kbd
128+
class="mac-key"><?php echo esc_html( $keys['Cmd'] ); ?></kbd>&hyphen;<kbd><?php echo esc_html( $keys['S'] ); ?></kbd>
131129
</td>
132130
</tr>
133131
<tr>
134132
<td><?php esc_html_e( 'Begin searching', 'code-snippets' ); ?></td>
135133
<td>
136-
<kbd class="pc-key"><?php echo esc_html( $keys['Ctrl'] ); ?></kbd><kbd class="mac-key"><?php
137-
echo esc_html( $keys['Cmd'] ); ?></kbd>&hyphen;<kbd><?php echo esc_html( $keys['F'] ); ?></kbd>
134+
<kbd class="pc-key"><?php echo esc_html( $keys['Ctrl'] ); ?></kbd><kbd
135+
class="mac-key"><?php echo esc_html( $keys['Cmd'] ); ?></kbd>&hyphen;<kbd><?php echo esc_html( $keys['F'] ); ?></kbd>
138136
</td>
139137
</tr>
140138
<tr>

phpcs.xml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,15 @@
3737
<exclude name="PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket" />
3838

3939
<!-- database table names should be interpolated -->
40-
<exclude name="WordPress.WP.PreparedSQL.NotPrepared" />
40+
<exclude name="WordPress.DB.PreparedSQL.InterpolatedNotPrepared" />
41+
42+
<!-- codesniffer does not seem to understand more complex queries -->
43+
<exclude name="WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare" />
44+
<exclude name="WordPress.DB.PreparedSQL.NotPrepared" />
4145

4246
<!-- class filenames do not include the namespace prefix -->
4347
<exclude name="WordPress.Files.FileName.InvalidClassFileName" />
4448

45-
<!-- this picks up a lot of false positives -->
46-
<exclude name="WordPress.DB.PreparedSQL.NotPrepared" />
47-
<exclude name="WordPress.DB.PreparedSQL.InterpolatedNotPrepared" />
48-
<exclude name="WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare" />
49-
5049
<!-- this disallows assignment inside conditional statements -->
5150
<exclude name="Squiz.PHP.DisallowMultipleAssignments.Found" />
5251

0 commit comments

Comments
 (0)