plugin = $plugin; $this->settings = $settings; $this->cache = $cache; } protected function moveViewAndCardMetaToPostContentJson(): void { $queryArgs = [ 'post_type' => [AcfViews::NAME, AcfCards::NAME,], 'post_status' => ['publish', 'draft', 'trash',], 'posts_per_page' => -1, ]; $myPosts = new WP_Query($queryArgs); $myPosts = $myPosts->get_posts(); global $wpdb; foreach ($myPosts as $myPost) { $postId = $myPost->ID; $data = AcfViews::NAME === $myPost->post_type ? $this->cache->getAcfViewData($postId) : $this->cache->getAcfCardData($postId); $data->load($myPost->ID); $data->saveToPostContent(); $wpdb->delete($wpdb->prefix . 'postmeta', [ 'post_id' => $postId, ]); } } protected function moveOptionsToSettings(): void { $license = (string)get_option(Options::PREFIX . 'license', ''); $licenceExpiration = (string)get_option(Options::PREFIX . 'license_expiration', ''); $demoImport = (array)get_option(Options::PREFIX . 'demo_import', []); $this->settings->setLicense($license); $this->settings->setLicenseExpiration($licenceExpiration); $this->settings->setDemoImport($demoImport); $this->settings->save(); //// delete_option(Options::PREFIX . 'license'); delete_option(Options::PREFIX . 'license_expiration'); delete_option(Options::PREFIX . 'demo_import'); } protected function fixMultipleSlashesInPostContentJson(): void { global $wpdb; // don't use 'get_post($id)->post_content' / 'wp_update_post()' // to avoid the kses issue https://core.trac.wordpress.org/ticket/38715 $myPosts = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE post_type IN (%s,%s) AND post_content != ''", AcfViews::NAME, AcfCards::NAME ) ); foreach ($myPosts as $myPost) { $content = str_replace('\\\\\\', '\\', $myPost->post_content); $wpdb->update($wpdb->posts, ['post_content' => $content], ['ID' => $myPost->ID]); } } protected function firstRun(): bool { // skip upgrading as hook won't be fired and data is not available if (! $this->plugin->isAcfPluginAvailable()) { return false; } add_action('acf/init', function () { $this->moveViewAndCardMetaToPostContentJson(); $this->moveOptionsToSettings(); }); return true; } public function upgrade(): void { $previousVersion = $this->settings->getVersion(); // a) 1.5.10 and lower didn't have the version in the DB // b) the very firstRun (unfortunately can't avoid it) if (! $previousVersion) { // skip if upgrading isn't possible ATM if (! $this->firstRun()) { return; } } if ('1.6.0' === $previousVersion) { $this->fixMultipleSlashesInPostContentJson(); } $this->settings->setVersion($this->plugin->getVersion()); $this->settings->save(); } public function setHooks(): void { // don't use 'upgrader_process_complete' hook, as user can update the plugin manually by FTP $dbVersion = $this->settings->getVersion(); $codeVersion = $this->plugin->getVersion(); // run upgrade if version in the DB is different from the code version if ($dbVersion !== $codeVersion) { // only at this hook can be sure that other plugin's functions are available add_action('plugins_loaded', [$this, 'upgrade']); } } }