meta_box_name, $metaBoxModel->getName()); self::updateBoxPostMeta($boxNames['newKey'], $boxNames['oldKey']); } } /** * @param string $newKey * @param string $oldKey * * @return void * @throws \Exception */ private static function updateBoxPostMeta(string $newKey, string $oldKey): void { global $wpdb; if ($newKey !== $oldKey) { $sql = "UPDATE `{$wpdb->prefix}commentmeta` cm SET cm.meta_key=REPLACE(cm.meta_key, %s, %s) WHERE cm.meta_key LIKE %s"; ACPT_Lite_DB::executeQueryOrThrowException($sql, [$oldKey, $newKey, $oldKey.'%']); } } /** * @param MetaFieldModel $fieldModel * @param string|null $find * * @return void * @throws \Exception */ public static function syncField(MetaFieldModel $fieldModel, ?string $find = null): void { if ($fieldModel->getParentId() !== null) { self::updateNestedPostMetaWhenFieldNameChanges($fieldModel); } elseif ($fieldModel->getBlockId() !== null) { } else { self::updatePostMetaWhenFieldNameChanges($fieldModel); } } /** * @param MetaFieldModel $fieldModel * * @return void * @throws \Exception */ private static function updateNestedPostMetaWhenFieldNameChanges(MetaFieldModel $fieldModel): void { if ($oldField = self::getMetaFieldData($fieldModel)) { if ($parentField = self::getParentMetaFieldData($fieldModel)) { global $wpdb; // old parent key $parentBoxName = $parentField->meta_box_name; $parentFieldName = $parentField->field_name; $parentKey = Strings::toDBFormat($parentBoxName).'_'.Strings::toDBFormat($parentFieldName); // old key $fieldName = $oldField->field_name; $oldKey = Strings::toDBFormat($fieldName); // new key $newKey = Strings::toDBFormat($fieldModel->getName()); $query = " SELECT * FROM `{$wpdb->prefix}commentmeta` cm WHERE cm.meta_key=%s "; $results = ACPT_Lite_DB::getResults($query, [$parentKey], 7200, null, true); foreach ($results as $result) { $oldMetaValue = Serializer::unserialize($result->meta_value); // key update if ($newKey !== $oldKey) { $oldMetaValue[$newKey] = $oldMetaValue[$oldKey]; unset($oldMetaValue[$oldKey]); $metaValue = serialize($oldMetaValue); $sql = " UPDATE `{$wpdb->prefix}commentmeta` cm SET cm.meta_key=%s WHERE cm.meta_key=%s "; ACPT_Lite_DB::executeQueryOrThrowException($sql, [$metaValue, $parentKey]); } } } } } /** * @param string $parentKey * @param string $oldKey * @param string $newKey * * @return void * @throws \Exception */ private static function updateNestedPostMetaData(string $parentKey, string $oldKey, string $newKey): void { global $wpdb; $query = " SELECT * FROM `{$wpdb->prefix}commentmeta` cm WHERE cm.meta_key=%s "; $results = ACPT_Lite_DB::getResults($query, [$parentKey], 7200, null, true); foreach ($results as $result) { $oldMetaValue = Serializer::unserialize($result->meta_value); // key update if ($newKey !== $oldKey) { // blocks if (isset($oldMetaValue['blocks']) && is_array($oldMetaValue['blocks'])) { foreach ($oldMetaValue['blocks'] as $blockIndex => $block) { foreach ($block as $blockName => $blockValues) { foreach ($blockValues as $fieldName => $fieldValues) { if ($fieldName === $oldKey) { $oldMetaValue['blocks'][$blockIndex][$blockName][$newKey] = $oldMetaValue['blocks'][$blockIndex][$blockName][$oldKey]; unset($oldMetaValue['blocks'][$blockIndex][$blockName][$oldKey]); } } } } } else { $oldMetaValue[$newKey] = $oldMetaValue[$oldKey]; unset($oldMetaValue[$oldKey]); } $metaValue = serialize($oldMetaValue); $sql = " UPDATE `{$wpdb->prefix}commentmeta` cm SET cm.meta_key=%s WHERE cm.meta_key=%s "; ACPT_Lite_DB::executeQueryOrThrowException($sql, [$metaValue, $parentKey]); } } } /** * @param MetaFieldModel $fieldModel * * @return void * @throws \Exception */ private static function updatePostMetaWhenFieldNameChanges(MetaFieldModel $fieldModel): void { if ($oldField = self::getMetaFieldData($fieldModel)) { // old key $boxName = $oldField->meta_box_name; $fieldName = $oldField->field_name; $oldKey = Strings::toDBFormat($boxName).'_'.Strings::toDBFormat($fieldName); // new key $newKey = $fieldModel->getDbName(); self::updateFieldPostMeta($newKey, $oldKey); } } /** * @param string $newKey * @param string $oldKey * * @return void * @throws \Exception */ private static function updateFieldPostMeta(string $newKey, string $oldKey): void { global $wpdb; if ($newKey !== $oldKey) { $rename = self::renameFieldKeys('cm.meta_key', $oldKey, $newKey); $sql = " UPDATE `{$wpdb->prefix}commentmeta` cm SET {$rename['set']} WHERE {$rename['where']} "; ACPT_Lite_DB::executeQueryOrThrowException($sql, $rename['args']); } } }