From 555c797fe43acf65464710e9ed368c4aa35b8056 Mon Sep 17 00:00:00 2001 From: Niclas Norin Date: Mon, 25 Sep 2023 17:09:59 +0200 Subject: [PATCH 01/10] Feat: Upgrade init Added functions migrateAcfFieldValueToNewField, migrateBlockFieldValueToNewField --- source/php/App.php | 4 +- source/php/Module/Divider/Divider.php | 2 +- source/php/Upgrade.php | 462 ++++++++++++++++++++++++++ 3 files changed, 466 insertions(+), 2 deletions(-) create mode 100644 source/php/Upgrade.php diff --git a/source/php/App.php b/source/php/App.php index f8a8831c5..f8979debb 100644 --- a/source/php/App.php +++ b/source/php/App.php @@ -28,7 +28,8 @@ public function __construct() }); $this->setupAdminBar(); - + + new Upgrade(); new Ajax(); new Options\General(); new Options\Archives(); @@ -52,6 +53,7 @@ public function __construct() }); } + /** * Update modified date on related post when module is saved * @return boolean True if update(s) where made, otherwise false. diff --git a/source/php/Module/Divider/Divider.php b/source/php/Module/Divider/Divider.php index e0f5599de..bb818d832 100644 --- a/source/php/Module/Divider/Divider.php +++ b/source/php/Module/Divider/Divider.php @@ -21,7 +21,7 @@ public function data() : array $fields = $this->getFields(); //Asign to view names - $data['title'] = !empty($fields['divider_title']) ? $fields['divider_title'] : false; + $data['title'] = !empty($this->ID) ? $this->data['post_title'] : (!empty($this->data['postTitle']) ? $this->data['postTitle'] : false); $data['titleVariant'] = !empty($fields['divider_title_variant']) ? $fields['divider_title_variant'] : 'h2'; //Send to view diff --git a/source/php/Upgrade.php b/source/php/Upgrade.php new file mode 100644 index 000000000..a5fb8161a --- /dev/null +++ b/source/php/Upgrade.php @@ -0,0 +1,462 @@ +After upgrade'; + echo '
';
+        print_r(get_theme_mods());
+        echo '
'; + } + + /** + * Enable to print stuff you need. + * + * @return void + */ + public function debugPre() + { + echo '

Before upgrade

'; + echo '
';
+        print_r(get_theme_mods());
+        echo '
'; + } + + /** + * Reset db version, in order to run all scripts from the beginning. + * + * @return void + */ + public function reset() + { + update_option($this->dbVersionKey, 1); + } + + /** + * Upgrade database, + * when you want to upgrade database, + * create a new function and increase + * $this->dbVersion. + * + * Method inspiration from WordPress Core. + * + * @return boolean + */ + private function v_1($db): bool + { + global $wpdb; + + $this->migrateBlockFieldValueToNewField('divider_title', 'custom_block_title', 'divider'); + + $posts = $wpdb->get_results( + "SELECT p.* + FROM $wpdb->posts p + JOIN $wpdb->postmeta pm ON p.ID = pm.post_id + WHERE pm.meta_key = 'divider_title'" + ); + + if (!empty($posts)) { + foreach ($posts as &$post) { + if (!empty($post->post_type) && $post->post_type === $postType) { + $oldField = get_field('divider_title', $post->ID); + + if (!empty($oldField) && is_string($oldField)) { + wp_update_post([ + 'ID' => $post->ID, + 'post_title' => $oldField + ]); + } + } + } + } + + return true; //Return false to keep running this each time! + } + + /** + * Post: Extract a field value and adds it to another field + * + * @param string $oldFieldName The field to extract the data from + * @param string $newField The field to add the data to + * @param string $postType The name of the post type containing the field + */ + private function migrateAcfFieldValueToNewField(string $oldFieldName, string $newFieldName, string $postType) { + $args = array( + 'post_type' => $postType, + 'meta_query' => array( + array( + 'key' => $oldFieldName, + 'compare' => 'EXISTS' + ), + ), + ); + + $posts = get_posts($args); + + if (!empty($posts)) { + foreach ($posts as &$post) { + $oldFieldValue = get_field($oldFieldName, $post->ID); + update_field($newFieldName, $oldField, $post->ID); + delete_field($oldFieldName, $post->ID); + } + } + } + + /** + * Block: Extract a field value and adds it to another field. + * + * @param string $oldFieldName The field to extract the data from + * @param string $newField The field to add the data to + * @param string $blockName Name of the block + */ + private function migrateBlockFieldValueToNewField($oldFieldName, $newFieldName, $blockName = '') { + global $wpdb; + + $pages = $wpdb->get_results( + "SELECT * + FROM $wpdb->posts + WHERE post_content LIKE '%$oldFieldName%'" + ); + + if (!empty($pages)) { + foreach ($pages as &$page) { + if ($page->post_type !== 'customize_changeset' && $page->post_type !== 'revision') { + $blocks = parse_blocks($page->post_content); + + if (!empty($blocks) && !empty($page->ID)) { + foreach ($blocks as &$block) { + if ($block['blockName'] === "acf/{$blockName}") { + if (isset($block['attrs']['data'][$oldFieldName])) { + $block['attrs']['data'][$newFieldName] = $block['attrs']['data'][$oldFieldName]; + unset($block['attrs']['data'][$oldFieldName]); + } + } + } + + $serializedBlocks = serialize_blocks($blocks); + + wp_update_post([ + 'ID' => $page->ID, + 'post_content' => $serializedBlocks + ]); + } + } + } + } + } + + /** + * Get all post types + * + * @return array + */ + private function getAllPostTypes() + { + $postTypes = array(); + foreach (get_post_types() as $key => $postType) { + $args = get_post_type_object($postType); + + if (!$args->public || $args->name === 'page') { + continue; + } + + $postTypes[$postType] = $args; + } + + $postTypes['author'] = (object) array( + 'name' => 'author', + 'label' => __('Author'), + 'has_archive' => true, + 'is_author_archive' => true + ); + + return $postTypes; + } + + + /** + * Move and clean out the old theme mod + * + * @param string $oldKey + * @param string $newKey + * @return bool + */ + private function migrateThemeMod($oldKey, $newKey, $subkey = null) + { + if ($oldValue = get_theme_mod($oldKey)) { + if ($subkey && isset($oldValue[$subkey])) { + return $this->setAssociativeThemeMod($newKey, $oldValue[$subkey]); + } elseif (is_null($subkey)) { + return $this->setAssociativeThemeMod($newKey, $oldValue); + } + } + return false; + } + + /** + * Logs error message + * + * @param string $message Error message + * + */ + private function logError(string $message) + { + error_log($message); + } + + /** + * Migrates an ACF option to a theme mod. + * + * @param string $option The option key which is being migrated. + * @param string $themeMod [Optional] The theme mod key to which the + * option is being migrated. If not provided, it will take the value of $option. + * + */ + private function migrateACFOptionToThemeMod(string $option, string $themeMod) + { + $errorMessage = "Failed to migrate ACF option \"$option\" to theme mod \"$themeMod\""; + + if ( + !function_exists('get_field') || + empty($value = get_field($option, 'option', false)) || + !set_theme_mod($themeMod, $value) + ) { + $this->logError($errorMessage); + return; + } + + delete_field($option, 'option'); + } + + /** + * Migrates an ACF option image id to a theme mod url. + * + * @param string $option The option key which is being migrated. + * @param string $themeMod [Optional] The theme mod key to which the option is + * being migrated. If not provided, it will take the value of $option. + * + */ + private function migrateACFOptionImageIdToThemeModUrl(string $option, string $themeMod) + { + $errorMessage = "Failed to migrate ACF option \"$option\" to theme mod \"$themeMod\""; + + if (!function_exists('get_field')) { + $this->logError($errorMessage); + return; + } + + $value = get_field($option, 'option', false); + + if (empty($value = get_field($option, 'option', false)) || !is_int(absint($value))) { + $this->logError($errorMessage); + return; + } + + $attachmentUrl = wp_get_attachment_url($value); + + if ($attachmentUrl === false || !set_theme_mod($themeMod, $attachmentUrl)) { + $this->logError($errorMessage); + return; + } + + delete_field($option, 'option'); + } + + /** + * A simple wrapper around set_theme_mod() in order to set a single property value of an associative array setting. + * Key should include a dot in order to target a property. + * eg. color_palette.primary will target array('primary' => VALUE). + * + * Does not support nested values eg settings.property.nested_value_1.nested_value_2 etc + * + * @param string $key + * @param string $value + * @param bool $castToArray this will transform existing values which are not arrays to empty arrays when true + * @return bool True if the value was updated, false otherwise. + */ + protected function setAssociativeThemeMod($key, $value, $castToArray = false) + { + $parsedString = explode('.', $key); + $key = $parsedString[0] ?? ''; + $property = $parsedString[1] ?? ''; + + if (empty($parsedString) || empty($key)) { + return false; + } + + if (!empty($property)) { + $associativeArr = get_theme_mod($key, []); + $associativeArr = is_array($associativeArr) || $castToArray !== true ? $associativeArr : []; + + if (!is_array($associativeArr)) { + $errorMessage = "Failed to migrate setting (" . $key . "." . $property . "). + The specified setting already exists and is not an associative array."; + $this->logError($errorMessage); + return false; + } + + $associativeArr[$property] = $value; + $value = $associativeArr; + } + + return set_theme_mod($key, $value); + } + + /** + * Deletes a theme mod + * + * @param string $key + * @return bool + */ + private function deleteThemeMod($key) + { + return remove_theme_mod($key); + } + + /** + * Undocumented function + * + * @param [type] $color + * @param boolean $opacity + * @return void + */ + private function hex2rgba($color, $opacity = false) + { + $default = 'rgb(0,0,0)'; + + //Return default if no color provided + if (empty($color)) { + return $default; + } + + //Sanitize $color if "#" is provided + if ($color[0] == '#') { + $color = substr($color, 1); + } + + //Check if color has 6 or 3 characters and get values + if (strlen($color) == 6) { + $hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] ); + } elseif (strlen($color) == 3) { + $hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] ); + } else { + return $default; + } + + //Convert hexadec to rgb + $rgb = array_map('hexdec', $hex); + + //Check if opacity is set(rgba or rgb) + if ($opacity) { + if (abs($opacity) > 1) { + $opacity = 1.0; + } + $output = 'rgba(' . implode(",", $rgb) . ',' . $opacity . ')'; + } else { + $output = 'rgb(' . implode(",", $rgb) . ')'; + } + + //Return rgb(a) color string + return $output; + } + + /** + * Run upgrade functions + * + * @return void + */ + public function initUpgrade() + { + $currentDbVersion = is_numeric(get_option($this->dbVersionKey)) ? (int) get_option($this->dbVersionKey) : 1; + + if ($this->dbVersion != $currentDbVersion) { + if (!is_numeric($this->dbVersion)) { + wp_die(__('To be installed database version must be a number.', 'municipio')); + } + + if (!is_numeric($currentDbVersion)) { + $this->logError(__('Current database version must be a number.', 'municipio')); + } + + if ($currentDbVersion > $this->dbVersion) { + $this->logError( + __( + 'Database cannot be lower than currently installed (cannot downgrade).', + 'municipio' + ) + ); + } + + //Fetch global wpdb object, save to $db + $this->globalToLocal('wpdb', 'db'); + + //Run upgrade(s) + while ($currentDbVersion <= $this->dbVersion) { + $currentDbVersion++; + $funcName = 'v_' . (string) $currentDbVersion; + if (method_exists($this, $funcName)) { + if ($this->{$funcName}($this->db)) { + update_option($this->dbVersionKey, (int) $currentDbVersion); + wp_cache_flush(); + } + } + } + } + } + + /** + * Creates a local copy of the global instance + * The target var should be defined in class header as private or public + * @param string $global The name of global varable that should be made local + * @param string $local Handle the global with the name of this string locally + * @return void + */ + private function globalToLocal($global, $local = null) + { + global $$global; + + if (is_null($$global)) { + return false; + } + + if (is_null($local)) { + $this->$global = $$global; + } else { + $this->$local = $$global; + } + + return true; + } +} From 8d836252b0674b00bb48f00908969e4def002b34 Mon Sep 17 00:00:00 2001 From: Niclas Norin Date: Thu, 28 Sep 2023 15:36:34 +0200 Subject: [PATCH 02/10] Feat: Adding functions to migrate modules and acf fields --- source/php/Upgrade.php | 157 +++++++++++++++++++++++++++-------------- 1 file changed, 105 insertions(+), 52 deletions(-) diff --git a/source/php/Upgrade.php b/source/php/Upgrade.php index a5fb8161a..26398b08c 100644 --- a/source/php/Upgrade.php +++ b/source/php/Upgrade.php @@ -77,25 +77,26 @@ public function reset() private function v_1($db): bool { global $wpdb; + + $this->migrateBlockFieldsValueToNewFields('divider', ['divider_title' => 'custom_block_title']); - $this->migrateBlockFieldValueToNewField('divider_title', 'custom_block_title', 'divider'); - - $posts = $wpdb->get_results( - "SELECT p.* - FROM $wpdb->posts p - JOIN $wpdb->postmeta pm ON p.ID = pm.post_id - WHERE pm.meta_key = 'divider_title'" + /* Removing divider acf title field and adding it as post_title */ + $args = array( + 'post_type' => 'mod-divider', ); + + $dividers = get_posts($args); - if (!empty($posts)) { - foreach ($posts as &$post) { - if (!empty($post->post_type) && $post->post_type === $postType) { - $oldField = get_field('divider_title', $post->ID); - - if (!empty($oldField) && is_string($oldField)) { + if (!empty($dividers)) { + foreach ($dividers as &$divider) { + if (!empty($divider->post_type) && $divider->post_type === $postType) { + $dividerTitleField = get_field('divider_title', $divider->ID); + delete_field('divider_title', $divider->ID); + + if (!empty($dividerTitleField) && is_string($dividerTitleField)) { wp_update_post([ - 'ID' => $post->ID, - 'post_title' => $oldField + 'ID' => $divider->ID, + 'post_title' => $dividerTitleField ]); } } @@ -108,28 +109,47 @@ private function v_1($db): bool /** * Post: Extract a field value and adds it to another field * - * @param string $oldFieldName The field to extract the data from - * @param string $newField The field to add the data to - * @param string $postType The name of the post type containing the field + * @param string $moduleName The name of the post type containing the field + * @param array $fields Fields is an array with the old name of the field being a key and the value being the new name of the field + * @param string|false $newBlockName renames the block to a different block. */ - private function migrateAcfFieldValueToNewField(string $oldFieldName, string $newFieldName, string $postType) { + private function migrateAcfFieldsValueToNewFields(string $moduleName, array $fields, $newModuleName = false) + { $args = array( - 'post_type' => $postType, - 'meta_query' => array( - array( - 'key' => $oldFieldName, - 'compare' => 'EXISTS' - ), - ), + 'post_type' => $moduleName, ); - $posts = get_posts($args); + $modules = get_posts($args); - if (!empty($posts)) { - foreach ($posts as &$post) { - $oldFieldValue = get_field($oldFieldName, $post->ID); - update_field($newFieldName, $oldField, $post->ID); - delete_field($oldFieldName, $post->ID); + if (!empty($modules) && is_array($modules)) { + foreach ($modules as &$module) { + $this->migrateModuleFields($fields, $module->ID); + + if (!empty($newModuleName)) { + wp_update_post([ + 'ID' => $module->ID, + 'post_type' => $newModuleName + ]); + } + } + } + } + + /** + * Post: Extract a field value and adds it to another field + * + * @param array $fields Fields is an array with the old name of the field being a key and the value being the new name of the field + * @param int $id Id of the post + */ + private function migrateModuleFields(array $fields, int $id) + { + if (!empty($fields) && is_array($fields)) { + foreach ($fields as $oldFieldName => $newFieldName) { + $oldFieldValue = get_field($oldFieldName, $id); + if (!empty($oldFieldValue)) { + update_field($newFieldName, $oldFieldValue, $id); + } + delete_field($oldFieldName, $id); } } } @@ -137,44 +157,77 @@ private function migrateAcfFieldValueToNewField(string $oldFieldName, string $ne /** * Block: Extract a field value and adds it to another field. * - * @param string $oldFieldName The field to extract the data from - * @param string $newField The field to add the data to * @param string $blockName Name of the block + * @param array $fields Fields is an array with the old name of the field being a key and the value being the new name of the field + * @param string|false $newBlockName renames the block to a different block. */ - private function migrateBlockFieldValueToNewField($oldFieldName, $newFieldName, $blockName = '') { - global $wpdb; - - $pages = $wpdb->get_results( - "SELECT * - FROM $wpdb->posts - WHERE post_content LIKE '%$oldFieldName%'" - ); + private function migrateBlockFieldsValueToNewFields(string $blockName = '', array $fields = [], $newBlockName = false) + { + $pages = $this->getPagesFromBlockName($blockName); - if (!empty($pages)) { + if (!empty($pages) && is_array($pages) && !empty($fields) && is_array($fields)) { foreach ($pages as &$page) { if ($page->post_type !== 'customize_changeset' && $page->post_type !== 'revision') { $blocks = parse_blocks($page->post_content); - + if (!empty($blocks) && !empty($page->ID)) { foreach ($blocks as &$block) { - if ($block['blockName'] === "acf/{$blockName}") { - if (isset($block['attrs']['data'][$oldFieldName])) { - $block['attrs']['data'][$newFieldName] = $block['attrs']['data'][$oldFieldName]; - unset($block['attrs']['data'][$oldFieldName]); + if (!empty($block['blockName']) && $block['blockName'] === $blockName && !empty($block['attrs']['data'])) { + $block['attrs']['data'] = $this->migrateBlockFields($fields, $block['attrs']['data']); + + if (!empty($newModuleName)) { + $block['blockName'] = $newModuleName; + $block['attrs']['name'] = $newBlockName; } } } - - $serializedBlocks = serialize_blocks($blocks); + + $serializedBlocks = serialize_blocks($blocks); wp_update_post([ 'ID' => $page->ID, 'post_content' => $serializedBlocks - ]); + ]); } } } - } + } + } + + /** + * Block: Extract a field value and adds it to another field. + * + * @param array $fields Fields is an array with the old name of the field being a key and the value being the new name of the field + * @param array $blockData All the data of the block (the acf fields attached to the block) + */ + private function migrateBlockFields(array $fields = [], array $blockData) + { + if (!empty($fields) && is_array($fields)) { + foreach ($fields as $oldFieldName => $newFieldName) { + if (isset($blockData[$oldFieldName])) { + $blockData[$newFieldName] = $blockData[$oldFieldName]; + unset($blockData[$oldFieldName]); + } + } + } + + return $blockData; + } + + /** + * Gets all pages that has a specific block attached + * + * @param string $blockName Name of the block + */ + private function getPagesFromBlockName(string $blockName = '') { + global $wpdb; + $pages = $wpdb->get_results( + "SELECT * + FROM $wpdb->posts + WHERE post_content LIKE '%{$blockName}%'" + ); + + return $pages; } /** From 3d2c0c7f148733b7cc3ef1f79122de114c5e1dd6 Mon Sep 17 00:00:00 2001 From: Niclas Norin Date: Fri, 29 Sep 2023 13:19:42 +0200 Subject: [PATCH 03/10] Fix: Upgrade docs --- source/php/Upgrade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/php/Upgrade.php b/source/php/Upgrade.php index 26398b08c..10746d477 100644 --- a/source/php/Upgrade.php +++ b/source/php/Upgrade.php @@ -107,7 +107,7 @@ private function v_1($db): bool } /** - * Post: Extract a field value and adds it to another field + * Module: Extract a field value and adds it to another field * * @param string $moduleName The name of the post type containing the field * @param array $fields Fields is an array with the old name of the field being a key and the value being the new name of the field From 4ac746c1f7739e507ce25b7cafefc507008692dd Mon Sep 17 00:00:00 2001 From: NiclasNorin Date: Tue, 3 Oct 2023 08:49:07 +0200 Subject: [PATCH 04/10] Feat: Upgrade db key --- source/php/Upgrade.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/source/php/Upgrade.php b/source/php/Upgrade.php index 10746d477..e47c231a3 100644 --- a/source/php/Upgrade.php +++ b/source/php/Upgrade.php @@ -10,7 +10,7 @@ class Upgrade { private $dbVersion = 1; //The db version we want to achive - private $dbVersionKey = 'municipio_db_version'; + private $dbVersionKey = 'modularity_db_version'; private $db; /** @@ -452,7 +452,11 @@ private function hex2rgba($color, $opacity = false) */ public function initUpgrade() { - $currentDbVersion = is_numeric(get_option($this->dbVersionKey)) ? (int) get_option($this->dbVersionKey) : 1; + if (empty(get_option($this->dbVersionKey))) { + update_option($this->dbVersionKey, 0); + } + + $currentDbVersion = is_numeric(get_option($this->dbVersionKey)) ? (int) get_option($this->dbVersionKey) : 0; if ($this->dbVersion != $currentDbVersion) { if (!is_numeric($this->dbVersion)) { @@ -471,10 +475,10 @@ public function initUpgrade() ) ); } - + //Fetch global wpdb object, save to $db $this->globalToLocal('wpdb', 'db'); - + //Run upgrade(s) while ($currentDbVersion <= $this->dbVersion) { $currentDbVersion++; From 3b137cf722cb7e0f2b7a758b936740c8142a4631 Mon Sep 17 00:00:00 2001 From: NiclasNorin Date: Wed, 4 Oct 2023 08:45:38 +0200 Subject: [PATCH 05/10] Fix: No need to check post type, also setting hide title to false --- source/php/Upgrade.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/source/php/Upgrade.php b/source/php/Upgrade.php index e47c231a3..4a03ab379 100644 --- a/source/php/Upgrade.php +++ b/source/php/Upgrade.php @@ -87,18 +87,18 @@ private function v_1($db): bool $dividers = get_posts($args); + if (!empty($dividers)) { foreach ($dividers as &$divider) { - if (!empty($divider->post_type) && $divider->post_type === $postType) { - $dividerTitleField = get_field('divider_title', $divider->ID); - delete_field('divider_title', $divider->ID); - - if (!empty($dividerTitleField) && is_string($dividerTitleField)) { - wp_update_post([ - 'ID' => $divider->ID, - 'post_title' => $dividerTitleField - ]); - } + $dividerTitleField = get_field('divider_title', $divider->ID); + delete_field('divider_title', $divider->ID); + + if (!empty($dividerTitleField) && is_string($dividerTitleField)) { + update_post_meta($divider->ID, 'modularity-module-hide-title', false); + wp_update_post([ + 'ID' => $divider->ID, + 'post_title' => $dividerTitleField + ]); } } } @@ -458,6 +458,8 @@ public function initUpgrade() $currentDbVersion = is_numeric(get_option($this->dbVersionKey)) ? (int) get_option($this->dbVersionKey) : 0; + $currentDbVersion = 0; + if ($this->dbVersion != $currentDbVersion) { if (!is_numeric($this->dbVersion)) { wp_die(__('To be installed database version must be a number.', 'municipio')); From 8ff6a5ab2cf32da94606882f6862489b1dc4d8a9 Mon Sep 17 00:00:00 2001 From: NiclasNorin Date: Wed, 4 Oct 2023 12:35:28 +0200 Subject: [PATCH 06/10] Fix: Correct values and names --- source/php/Upgrade.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/source/php/Upgrade.php b/source/php/Upgrade.php index 4a03ab379..c2350436b 100644 --- a/source/php/Upgrade.php +++ b/source/php/Upgrade.php @@ -78,7 +78,7 @@ private function v_1($db): bool { global $wpdb; - $this->migrateBlockFieldsValueToNewFields('divider', ['divider_title' => 'custom_block_title']); + $this->migrateBlockFieldsValueToNewFields('acf/divider', ['divider_title' => 'custom_block_title']); /* Removing divider acf title field and adding it as post_title */ $args = array( @@ -175,8 +175,8 @@ private function migrateBlockFieldsValueToNewFields(string $blockName = '', arra if (!empty($block['blockName']) && $block['blockName'] === $blockName && !empty($block['attrs']['data'])) { $block['attrs']['data'] = $this->migrateBlockFields($fields, $block['attrs']['data']); - if (!empty($newModuleName)) { - $block['blockName'] = $newModuleName; + if (!empty($newBlockName)) { + $block['blockName'] = $newBlockName; $block['attrs']['name'] = $newBlockName; } } @@ -458,8 +458,6 @@ public function initUpgrade() $currentDbVersion = is_numeric(get_option($this->dbVersionKey)) ? (int) get_option($this->dbVersionKey) : 0; - $currentDbVersion = 0; - if ($this->dbVersion != $currentDbVersion) { if (!is_numeric($this->dbVersion)) { wp_die(__('To be installed database version must be a number.', 'municipio')); From 248369ffb24eac83564e69628cb6d293693f4978 Mon Sep 17 00:00:00 2001 From: Niclas Norin Date: Wed, 4 Oct 2023 14:44:21 +0200 Subject: [PATCH 07/10] Fix: Removing divider code --- source/php/Upgrade.php | 28 +--------------------------- 1 file changed, 1 insertion(+), 27 deletions(-) diff --git a/source/php/Upgrade.php b/source/php/Upgrade.php index c2350436b..ef8190c82 100644 --- a/source/php/Upgrade.php +++ b/source/php/Upgrade.php @@ -9,7 +9,7 @@ */ class Upgrade { - private $dbVersion = 1; //The db version we want to achive + private $dbVersion = 0; //The db version we want to achive private $dbVersionKey = 'modularity_db_version'; private $db; @@ -76,32 +76,6 @@ public function reset() */ private function v_1($db): bool { - global $wpdb; - - $this->migrateBlockFieldsValueToNewFields('acf/divider', ['divider_title' => 'custom_block_title']); - - /* Removing divider acf title field and adding it as post_title */ - $args = array( - 'post_type' => 'mod-divider', - ); - - $dividers = get_posts($args); - - - if (!empty($dividers)) { - foreach ($dividers as &$divider) { - $dividerTitleField = get_field('divider_title', $divider->ID); - delete_field('divider_title', $divider->ID); - - if (!empty($dividerTitleField) && is_string($dividerTitleField)) { - update_post_meta($divider->ID, 'modularity-module-hide-title', false); - wp_update_post([ - 'ID' => $divider->ID, - 'post_title' => $dividerTitleField - ]); - } - } - } return true; //Return false to keep running this each time! } From 1cd59fd343a2422d487260393d69452035180b8d Mon Sep 17 00:00:00 2001 From: Niclas Norin Date: Wed, 4 Oct 2023 14:52:37 +0200 Subject: [PATCH 08/10] Fix: Reseting divider --- source/php/Module/Divider/Divider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/php/Module/Divider/Divider.php b/source/php/Module/Divider/Divider.php index bb818d832..e0f5599de 100644 --- a/source/php/Module/Divider/Divider.php +++ b/source/php/Module/Divider/Divider.php @@ -21,7 +21,7 @@ public function data() : array $fields = $this->getFields(); //Asign to view names - $data['title'] = !empty($this->ID) ? $this->data['post_title'] : (!empty($this->data['postTitle']) ? $this->data['postTitle'] : false); + $data['title'] = !empty($fields['divider_title']) ? $fields['divider_title'] : false; $data['titleVariant'] = !empty($fields['divider_title_variant']) ? $fields['divider_title_variant'] : 'h2'; //Send to view From eb2fae7b01a2b5d08b7bce6ee55739583576afe5 Mon Sep 17 00:00:00 2001 From: Niclas Norin Date: Wed, 4 Oct 2023 15:12:24 +0200 Subject: [PATCH 09/10] Fix: Get all posts -1 --- source/php/Upgrade.php | 1 + 1 file changed, 1 insertion(+) diff --git a/source/php/Upgrade.php b/source/php/Upgrade.php index ef8190c82..03ecb63e3 100644 --- a/source/php/Upgrade.php +++ b/source/php/Upgrade.php @@ -91,6 +91,7 @@ private function migrateAcfFieldsValueToNewFields(string $moduleName, array $fie { $args = array( 'post_type' => $moduleName, + 'numberposts' => -1 ); $modules = get_posts($args); From 3e16b2cdf53a5cd40d70a5f019c309a7c39021a7 Mon Sep 17 00:00:00 2001 From: Niclas Norin Date: Wed, 4 Oct 2023 15:12:38 +0200 Subject: [PATCH 10/10] Fix: Also change revisions --- source/php/Upgrade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/php/Upgrade.php b/source/php/Upgrade.php index 03ecb63e3..c39d22ef4 100644 --- a/source/php/Upgrade.php +++ b/source/php/Upgrade.php @@ -142,7 +142,7 @@ private function migrateBlockFieldsValueToNewFields(string $blockName = '', arra if (!empty($pages) && is_array($pages) && !empty($fields) && is_array($fields)) { foreach ($pages as &$page) { - if ($page->post_type !== 'customize_changeset' && $page->post_type !== 'revision') { + if ($page->post_type !== 'customize_changeset') { $blocks = parse_blocks($page->post_content); if (!empty($blocks) && !empty($page->ID)) {