Skip to content

Commit

Permalink
Add index for the database if it does not exist, 1.2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
ronilaukkarinen committed Mar 14, 2024
1 parent 37bb5ae commit 9bfbd20
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
### 1.2.6: 2023-02-21
### 1.2.7: 2024-03-14

* Add index for the database if it does not exist

### 1.2.6: 2024-02-21

* Unit test fixes
* Bump tested WordPress version up to 6.4.3
* More fixes for db query in record_consent

### 1.2.5: 2023-02-20
### 1.2.5: 2024-02-20

* Add unit tests
* Support PHP 8.3
* Fix phpcs errors
* Fix db query in record_consent

### 1.2.4: 2023-02-12
### 1.2.4: 2024-02-12

* Change changelog format
* Fix bunch of typos
Expand Down
2 changes: 1 addition & 1 deletion air-cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Air cookie
* Plugin URI: https://github.com/digitoimistodude/air-cookie
* Description: Simple cookie banner and management.
* Version: 1.2.6
* Version: 1.2.7
* Author: Digitoimisto Dude Oy
* Author URI: https://www.dude.fi
* Requires at least: 5.5
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"plugin"
],
"license": "GPL-3.0+",
"version": "1.2.6",
"version": "1.2.7",
"authors": [
{
"name": "Timi Wahalahti",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "air-cookie",
"version": "1.2.6",
"version": "1.2.7",
"description": "Simple cookie banner and management.",
"main": "air-cookie.php",
"dependencies": {},
Expand Down
4 changes: 3 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

<!-- Set a description for this ruleset. -->
<description>A custom set of code standard rules to check for WordPress themes.</description>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/plugin-update-checker/*</exclude-pattern>

<!-- Include the WordPress ruleset, with exclusions. -->
<rule ref="WordPress">
Expand Down Expand Up @@ -80,6 +82,6 @@
<exclude name="Universal.WhiteSpace.PrecisionAlignment.Found" />
<exclude name="Universal.Arrays.DisallowShortArraySyntax.Found" />
<exclude name="Modernize.FunctionCalls.Dirname.FileConstant" />

<exclude name="WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase" />
</rule>
</ruleset>
15 changes: 11 additions & 4 deletions rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Author: Timi Wahalahti
* @Date: 2021-09-07 16:56:00
* @Last Modified by: Roni Laukkarinen
* @Last Modified time: 2024-02-20 17:17:04
* @Last Modified time: 2024-03-14 13:38:00
* @package air-cookie
*/

Expand Down Expand Up @@ -48,9 +48,16 @@ function record_consent( $request ) {
return;
}

// Try if the user consent for this revision and levels has been already recorded
// Test query: SELECT id FROM wp_air_cookie WHERE visitor_id = 'f284009a-ace9-42e7-a5ef-42b065a9184c' AND cookie_revision = '2412150750' AND cookie_value = 'a:4:{i:0;s:9:"necessary";i:1;s:10:"functional";i:2;s:9:"analytics";i:3;s:6:"embeds";}'
$exists = $wpdb->get_row( $wpdb->prepare( "SELECT id FROM {$table_name} WHERE visitor_id = %s AND cookie_revision = %s AND cookie_value = %s", $data->visitorid, $data->revision, $cookie_value ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders.QuotedSimplePlaceholder, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
// Check if the index exists
$index_exists = $wpdb->get_row( "SHOW INDEX FROM {$table_name} WHERE Key_name = 'idx_id_revision_value'" ); // phpcs:ignore

// CREATE INDEX idx_id_revision_value ON wp_air_cookie (visitor_id, cookie_revision, cookie_value);
if ( null === $index_exists ) {
$wpdb->query( "CREATE INDEX idx_id_revision_value ON {$table_name} (visitor_id, cookie_revision, cookie_value);" ); // phpcs:ignore
}

// Check if the user consent for this revision and levels has been already recorded
$exists = $wpdb->get_row( $wpdb->prepare( "SELECT id FROM {$table_name} WHERE visitor_id = %s AND cookie_revision = %s AND cookie_value = %s", $data->visitorid, $data->revision, $cookie_value ) ); // phpcs:ignore

// Bail if the consent has been already recorded
if ( null !== $exists ) {
Expand Down

0 comments on commit 9bfbd20

Please sign in to comment.