Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jetpack CRM: Fix hundreds of unnedded db queries #40711

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

CRM: Fix bug which caused duplicated queries to run.
49 changes: 39 additions & 10 deletions projects/plugins/crm/includes/ZeroBSCRM.DAL3.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@
*/
private $cache = array();

/**
* This is a temporary fix for issue #3504, until we don't change how we load settings this should help to avoid slowing down websites.
*
* @var array
*/
private static $mitigation_cache_for_issue_3504 = array();

// ===============================================================================
// =========== SUB DAL LAYERS ==================================================
// These hold sub-objects, e.g. contact
Expand Down Expand Up @@ -1633,6 +1640,9 @@

if ( !empty( $key ) ){

if ( isset( self::$mitigation_cache_for_issue_3504[ $key ] ) ) {
return self::$mitigation_cache_for_issue_3504[ $key ];
}
return $this->getSetting(array(

'key' => $key,
Expand Down Expand Up @@ -1708,6 +1718,10 @@
); foreach ($defaultArgs as $argK => $argV){ $$argK = $argV; if (is_array($args) && isset($args[$argK])) { if (is_array($args[$argK])){ $newData = $$argK; if (!is_array($newData)) $newData = array(); foreach ($args[$argK] as $subK => $subV){ $newData[$subK] = $subV; }$$argK = $newData;} else { $$argK = $args[$argK]; } } }
#} =========== / LOAD ARGS =============

if ( ! $fullDetails && isset( self::$mitigation_cache_for_issue_3504[ $key ] ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase, VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
return self::$mitigation_cache_for_issue_3504[ $key ]; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable

Check failure on line 1722 in projects/plugins/crm/includes/ZeroBSCRM.DAL3.php

View workflow job for this annotation

GitHub Actions / Static analysis

TypeError PhanTypeMismatchDimFetchNullable When fetching an array index from a value of type array, found an array index of type null=, but expected the index to be of the non-nullable type int|string

Check failure on line 1722 in projects/plugins/crm/includes/ZeroBSCRM.DAL3.php

View workflow job for this annotation

GitHub Actions / Static analysis

TypeError PhanTypeMismatchDimFetchNullable When fetching an array index from a value of type array|null[]|string[], found an array index of type null=, but expected the index to be of the non-nullable type int|string
}

#} Check key
if (!empty($key)){

Expand Down Expand Up @@ -1794,6 +1808,9 @@

$setting = $this->tidy_settingSingular($potentialRes);

// We only update the mitigation cache when $fullDetails is false.
self::$mitigation_cache_for_issue_3504[ $key ] = $setting; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable

// cache (commonly retrieved)
$this->update_cache_var( 'setting_' . $key, $setting );

Expand All @@ -1805,6 +1822,11 @@

} // / if ID

if ( ! $fullDetails ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase, VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
// We only update the mitigation cache when $fullDetails is false.
self::$mitigation_cache_for_issue_3504[ $key ] = $default; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
}

return $default;

}
Expand All @@ -1818,6 +1840,11 @@
* @return array of settings lines
*/
public function getSettings($args=array()){
// If we have already loaded something in our cache, return it.
// In this function we won't care if the values have changed or not because when they become invalid due to other functions, these functions should clear the cache.
if ( ! empty( self::$mitigation_cache_for_issue_3504 ) ) {
return self::$mitigation_cache_for_issue_3504;
}

#} ============ LOAD ARGS =============
$defaultArgs = array(
Expand Down Expand Up @@ -1889,20 +1916,19 @@

#} Has results, tidy + return
foreach ($potentialRes as $resDataLine) {
// We are only caching the singular setting (i.e. the value), because it is good enough.
self::$mitigation_cache_for_issue_3504[ $resDataLine->zbsset_key ] = $this->tidy_settingSingular( $resDataLine ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase

// DEBUG echo $resDataLine->zbsset_key.' = ';

if ($fullDetails){
// tidy
$resArr = $this->tidy_setting($resDataLine);
$res[$resArr['key']] = $resArr;
} else
$res[$resDataLine->zbsset_key] = $this->tidy_settingSingular($resDataLine);

if ( $fullDetails ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase, VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
$resArr = $this->tidy_setting( $resDataLine ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
$res[ $resArr['key'] ] = $resArr; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
} else {
$res[ $resDataLine->zbsset_key ] = $this->tidy_settingSingular( $resDataLine ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
}
}
}

return $res;
return $res;
}

/**
Expand Down Expand Up @@ -2005,6 +2031,9 @@
); foreach ($defaultArgs as $argK => $argV){ $$argK = $argV; if (is_array($args) && isset($args[$argK])) { if (is_array($args[$argK])){ $newData = $$argK; if (!is_array($newData)) $newData = array(); foreach ($args[$argK] as $subK => $subV){ $newData[$subK] = $subV; }$$argK = $newData;} else { $$argK = $args[$argK]; } } }
#} =========== / LOAD ARGS ============

// Invalidates our whole cache.
self::$mitigation_cache_for_issue_3504 = array();

#} ========== CHECK FIELDS ============

$id = (int)$id;
Expand Down
Loading