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

43305 backend performance #735

Merged
merged 3 commits into from
Feb 20, 2024
Merged
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
9 changes: 7 additions & 2 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@

$pAdminViewController = new AdminViewController();
$pDetailViewPostSaveController = $pDI->get(DetailViewPostSaveController::class);

add_action('save_post', [$pDetailViewPostSaveController, 'onSavePost']);
add_action('wp_trash_post', [$pDetailViewPostSaveController, 'onMoveTrash']);
if (is_admin() && isset($_GET['post']) && isset($_GET['action']) && $_GET['action'] === 'edit') {
return $pDI;
}

$pDI->get(ScriptLoaderRegistrator::class)->generate();

add_action('plugins_loaded', function() use ($pDI) {
Expand All @@ -99,8 +106,6 @@
add_action('admin_enqueue_scripts', [$pAdminViewController, 'enqueue_ajax']);
add_action('admin_enqueue_scripts', [$pAdminViewController, 'enqueue_css']);
add_action('admin_enqueue_scripts', [$pAdminViewController, 'enqueueExtraJs']);
add_action('save_post', [$pDetailViewPostSaveController, 'onSavePost']);
add_action('wp_trash_post', [$pDetailViewPostSaveController, 'onMoveTrash']);
add_action('oo_cache_cleanup', function() use ($pDI) {
$pDI->get(CacheHandler::class)->clean();
});
Expand Down
98 changes: 84 additions & 14 deletions plugin/Field/CustomLabel/CustomLabelRead.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,80 @@ public function __construct(wpdb $pWPDB)

/**
* @param int $formId
* @param Field $pField
* @return CustomLabelModelField
* @param array $pFields
* @param string $currentLocale
* @param string $pCustomsLabelConfigurationField
* @param string $pTranslateLabelConfigurationField
* @return array
*/
public function readCustomLabelsField(int $formId, Field $pField, $pCustomsLabelConfigurationField, $pTranslateLabelConfigurationField): CustomLabelModelField
public function getCustomLabelsFieldsForAdmin(int $formId, array $pFields, string $currentLocale, string $pCustomsLabelConfigurationField, string $pTranslateLabelConfigurationField): array
{
$query = $this->createBaseQuery($formId, $pField, $pCustomsLabelConfigurationField, $pTranslateLabelConfigurationField);
$rows = $this->_pWPDB->get_results($query, OBJECT);
$pDataModel = new CustomLabelModelField($formId, $pField);
$pDataModels = [];
$customLabelRows = $this->readCustomLabelsField($formId, $pFields, $pCustomsLabelConfigurationField, $pTranslateLabelConfigurationField);
$rowsByFieldName = $this->groupCustomLabelRowsByFieldName($customLabelRows);

foreach ($pFields as $pField) {
$pDataModel = new CustomLabelModelField($formId, $pField);
if (isset($rowsByFieldName[$pField->getName()])) {
foreach ($rowsByFieldName[$pField->getName()] as $pRow) {
$pDataModel->addValueByLocale($pRow->locale, $pRow->value);
}
}

foreach ($rows as $pRow) {
$pDataModel->addValueByLocale($pRow->locale, $pRow->value);
$valuesByLocale = $this->getLocalizedValues($pDataModel, $currentLocale);
$pDataModels[$pField->getName()] = $valuesByLocale;
}

return $pDataModel;
return $pDataModels;
}

/**
* @param CustomLabelModelField $pDataModel
* @param string $currentLocale
* @return array
*/
private function getLocalizedValues(CustomLabelModelField $pDataModel, string $currentLocale): array
{
$valuesByLocale = $pDataModel->getValuesByLocale();

if (isset($valuesByLocale[$currentLocale])) {
$valuesByLocale['native'] = $valuesByLocale[$currentLocale];
unset($valuesByLocale[$currentLocale]);
}

return $valuesByLocale;
}

/**
* @param array $rows
* @return array
*/
private function groupCustomLabelRowsByFieldName(array $rows): array
{
$customLabelGroup = [];
foreach ($rows as $row) {
$customLabelGroup[$row->fieldname][] = $row;
}

return $customLabelGroup;
}

/**
* @param int $formId
* @param array $pFields
* @param string $pCustomsLabelConfigurationField
* @param string $pTranslateLabelConfigurationField
* @return array
*/
public function readCustomLabelsField(int $formId, array $pFields, string $pCustomsLabelConfigurationField, string $pTranslateLabelConfigurationField): array
{
$pFieldNames = array_map(function ($field) {
return $field->getName();
}, $pFields);

$query = $this->createBaseQuery($formId, $pFieldNames, $pCustomsLabelConfigurationField, $pTranslateLabelConfigurationField);

return $this->_pWPDB->get_results($query, OBJECT);
}

/**
Expand All @@ -72,7 +132,7 @@ public function readCustomLabelsField(int $formId, Field $pField, $pCustomsLabel
* @param $current_lang
* @return array
*/
public function readCustomLabelByFormIdAndFieldName(int $formId, $field, $current_lang, $pCustomsLabelConfigurationField, $pTranslateLabelConfigurationField): array
public function readCustomLabelByFormIdAndFieldName(int $formId, $field, $current_lang, string $pCustomsLabelConfigurationField, string $pTranslateLabelConfigurationField): array
{
$query = $this->createCustomsLabelsByFormIdQuery($formId, $field, $current_lang, $pCustomsLabelConfigurationField, $pTranslateLabelConfigurationField);
return $this->_pWPDB->get_results($query, OBJECT);
Expand All @@ -81,21 +141,31 @@ public function readCustomLabelByFormIdAndFieldName(int $formId, $field, $curren

/**
* @param int $formId
* @param Field $pField
* @param array $pFieldNames
* @param string $pCustomsLabelConfigurationField
* @param string $pTranslateLabelConfigurationField
* @return string
*/
private function createBaseQuery(int $formId, Field $pField, $pCustomsLabelConfigurationField, $pTranslateLabelConfigurationField): string
private function createBaseQuery(int $formId, array $pFieldNames, string $pCustomsLabelConfigurationField, string $pTranslateLabelConfigurationField): string
{
$prefix = $this->_pWPDB->prefix;

$names = array_map(function ($item) {
return "'" . esc_sql($item) . "'";
}, $pFieldNames);
$names = implode(',', $names);

$query = "SELECT {$prefix}$pCustomsLabelConfigurationField.customs_labels_id,"
. "{$prefix}$pTranslateLabelConfigurationField.locale,\n"
. "{$prefix}$pTranslateLabelConfigurationField.value\n"
. "{$prefix}$pTranslateLabelConfigurationField.value,\n"
. "{$prefix}$pCustomsLabelConfigurationField.fieldname\n"
. "FROM {$prefix}$pCustomsLabelConfigurationField\n"
. "INNER JOIN {$prefix}$pTranslateLabelConfigurationField\n"
. "ON {$prefix}$pCustomsLabelConfigurationField.customs_labels_id = "
. " {$prefix}$pTranslateLabelConfigurationField.input_id\n"
. "WHERE BINARY {$prefix}$pCustomsLabelConfigurationField.fieldname = '" . esc_sql($pField->getName()) . "' AND\n"
. "WHERE {$prefix}$pCustomsLabelConfigurationField.fieldname IN (" . $names . ") AND\n"
. " {$prefix}$pCustomsLabelConfigurationField.form_id = " . esc_sql($formId);

return $query;
}

Expand Down
19 changes: 6 additions & 13 deletions plugin/Gui/AdminPageEstateListSettingsBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,20 +202,13 @@ private function readCustomLabels(): array
/** @var CustomLabelRead $pCustomLabelRead*/
$pCustomLabelRead = $this->getContainer()->get(CustomLabelRead::class);
$pLanguage = $this->getContainer()->get(Language::class);
$currentLocale = $pLanguage->getLocale();

foreach ($this->buildFieldsCollectionForCurrentEstate()->getAllFields() as $pField) {
$pCustomLabelModel = $pCustomLabelRead->readCustomLabelsField
((int)$this->getListViewId(), $pField, RecordManager::TABLENAME_FIELDCONFIG_ESTATE_CUSTOMS_LABELS, RecordManager::TABLENAME_FIELDCONFIG_ESTATE_TRANSLATED_LABELS);
$valuesByLocale = $pCustomLabelModel->getValuesByLocale();

$currentLocale = $pLanguage->getLocale();

if (isset($valuesByLocale[$currentLocale])) {
$valuesByLocale['native'] = $valuesByLocale[$currentLocale];
unset($valuesByLocale[$currentLocale]);
}
$result[$pField->getName()] = $valuesByLocale;
}
foreach (array_chunk($this->buildFieldsCollectionForCurrentEstate()->getAllFields(), 100) as $pField) {
$pCustomLabelModel = $pCustomLabelRead->getCustomLabelsFieldsForAdmin
((int)$this->getListViewId(), $pField, $currentLocale, RecordManager::TABLENAME_FIELDCONFIG_ESTATE_CUSTOMS_LABELS, RecordManager::TABLENAME_FIELDCONFIG_ESTATE_TRANSLATED_LABELS);
if (count($pCustomLabelModel)) $result = array_merge($result, $pCustomLabelModel);
}

return $result;
}
Expand Down
16 changes: 5 additions & 11 deletions plugin/Gui/AdminPageFormSettingsBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,19 +399,13 @@ private function readCustomLabels(): array
/** @var CustomLabelRead $pCustomLabelRead*/
$pCustomLabelRead = $this->getContainer()->get(CustomLabelRead::class);
$pLanguage = $this->getContainer()->get(Language::class);
$currentLocale = $pLanguage->getLocale();

foreach ($this->buildFieldsCollectionForCurrentForm()->getAllFields() as $pField) {
$pCustomLabelModel = $pCustomLabelRead->readCustomLabelsField
((int)$this->getListViewId(), $pField, RecordManager::TABLENAME_FIELDCONFIG_FORM_CUSTOMS_LABELS, RecordManager::TABLENAME_FIELDCONFIG_FORM_TRANSLATED_LABELS);

$valuesByLocale = $pCustomLabelModel->getValuesByLocale();
$currentLocale = $pLanguage->getLocale();
foreach (array_chunk($this->buildFieldsCollectionForCurrentForm()->getAllFields(), 100) as $pField) {
$pCustomLabelModel = $pCustomLabelRead->getCustomLabelsFieldsForAdmin
((int)$this->getListViewId(), $pField, $currentLocale, RecordManager::TABLENAME_FIELDCONFIG_FORM_CUSTOMS_LABELS, RecordManager::TABLENAME_FIELDCONFIG_FORM_TRANSLATED_LABELS);

if (isset($valuesByLocale[$currentLocale])) {
$valuesByLocale['native'] = $valuesByLocale[$currentLocale];
unset($valuesByLocale[$currentLocale]);
}
$result[$pField->getName()] = $valuesByLocale;
if (count($pCustomLabelModel)) $result = array_merge($result, $pCustomLabelModel);
}

return $result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,9 @@ public function callbackValueInputModelAvailableOptions(InputModelBase $pInputMo

public function createSortableFieldList($module, $htmlType, bool $isShow = true): InputModelDB
{
$pSortableFieldsList = parent::createSortableFieldList($module, $htmlType);
$pSortableFieldsList = $this->getInputModelDBFactory()->create(
InputModelDBFactory::INPUT_FIELD_CONFIG, null, true);
$pSortableFieldsList->setHtmlType($htmlType);
if (! $isShow) {
return $pSortableFieldsList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ public function generate(string $pageSlug, $listViewId = null): FormModel
*/
public function createSortableFieldList($module, $htmlType, bool $isShow = true): InputModelDB
{
$pSortableFieldsList = parent::createSortableFieldList($module, $htmlType, false);
$pSortableFieldsList = $this->getInputModelDBFactory()->create(
InputModelDBFactory::INPUT_FIELD_CONFIG, null, true);
$pSortableFieldsList->setHtmlType($htmlType);
$pFieldsCollection = $this->getFieldsCollection();
$fieldNames = [];

Expand Down
56 changes: 26 additions & 30 deletions tests/TestClassCustomlabelRead.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,56 +66,52 @@ public function prepare()
* @dataProvider dataProviderField
* @param int $formId
* @param array $rows
* @param CustomLabelModelField $pReference
*
*/

public function testReadCustomLabelsField(int $formId, array $rows, CustomLabelModelField $pReference)
public function testReadCustomLabelsField(int $formId, array $rows)
{
$this->_pWPDBMock->expects($this->once())->method('get_results')->will($this->returnValue($rows));
$pField = new Field('testField', 'testModule');
$pResult = $this->_pSubject->readCustomLabelsField($formId, [$pField],'oo_plugin_fieldconfig_form_customs_labels','oo_plugin_fieldconfig_form_translated_labels');

$pResult = $this->_pSubject->readCustomLabelsField($formId, $pField,'oo_plugin_fieldconfig_form_customs_labels','oo_plugin_fieldconfig_form_translated_labels');
$this->assertInstanceOf(CustomLabelModelField::class, $pResult);
$this->assertEquals($pReference, $pResult);
$this->assertEquals($rows, $pResult);
}

/**
*
* @return Generator
* @return array
*
*/

public function dataProviderField(): Generator
public function dataProviderField()
{
$pField = new Field('testField', 'testModule');
$rows = [
[123, ['customs_labels_id' => 1337, 'locale' => 'de_DE', 'value' => 'Deutschland', 'fieldname' => 'testField']]
];

$rows = [];
$pReference1 = new CustomLabelModelField(12, $pField);
yield [12, $rows, $pReference1];
$pReference2 = new CustomLabelModelField(13, $pField);
$pReference2->addValueByLocale('de_DE', 'Deutschland');
$pReference2->addValueByLocale('en_US', 'United States');
$pReference2->addValueByLocale('fr_BE', 'Belgique');
return $rows;
}

$rows = [
(object)[
'customs_labels_id' => 1337,
'locale' => 'de_DE',
'value' => 'Deutschland',
],
/**
*
*/
public function testGetCustomLabelsFieldsForAdmin()
{
$pField = new Field('testField', 'testModule');
$row = [
(object)[
'customs_labels_id' => 1338,
'defaults_id' => '13',
'value' => 'Spider Man',
'fieldname' => 'testField',
'type' => 'text',
'locale' => 'en_US',
'value' => 'United States',
],
(object)[
'customs_labels_id' => 1339,
'locale' => 'fr_BE',
'value' => 'Belgique',
],
];
yield [13, $rows, $pReference2];
}

$this->_pWPDBMock->expects($this->once())->method('get_results')->will($this->returnValue($row));
$result = $this->_pSubject->getCustomLabelsFieldsForAdmin(13, [$pField], 'en_US', 'oo_plugin_fieldconfig_form_customs_labels','oo_plugin_fieldconfig_form_translated_labels');

$this->assertEquals(['testField' => ['native' => 'Spider Man']], $result);
}
}
Loading