Number of rows changed per step. */ function acscf_migrate_legacy_names() { global $wpdb; $changed = array( 'settings' => 0, 'ai_settings' => 0, 'forms' => 0, 'entries' => 0, 'meta' => 0, 'content' => 0, ); // 1. Settings. Copied, never moved, so 2.0.1 keeps working after a rollback. if ( false === get_option( 'acscf_settings', false ) ) { $legacy = get_option( 'acs_cf_settings', false ); if ( is_array( $legacy ) ) { update_option( 'acscf_settings', $legacy ); $changed['settings'] = 1; } } /* * 2. AI settings. Only the three keys the plugin still owns are carried * over. The provider, the model and the stored API key from the versions * that talked to a provider directly are deliberately dropped: 2.0.2 asks * WordPress core for the model instead, and must not keep a key around. */ if ( false === get_option( 'acscf_ai_settings', false ) ) { $legacy_ai = get_option( 'acs_cf_ai_settings', false ); if ( is_array( $legacy_ai ) ) { $carried = array_intersect_key( $legacy_ai, array( 'tone' => '', 'sender_name' => '', 'auto_draft' => '', ) ); update_option( 'acscf_ai_settings', $carried ); $changed['ai_settings'] = 1; } } // 3. Post types. Both are private to this plugin, so a straight rename is safe. $type_map = array( 'acs_form' => 'acscf_form', 'acs_cf_entry' => 'acscf_entry', ); foreach ( $type_map as $old_type => $new_type ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- One time upgrade routine. There is no API that renames a post type in bulk, and the affected caches are cleared below. $ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s", $old_type ) ); if ( empty( $ids ) ) { continue; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- One time upgrade routine, see above. $wpdb->update( $wpdb->posts, array( 'post_type' => $new_type ), array( 'post_type' => $old_type ), array( '%s' ), array( '%s' ) ); foreach ( $ids as $post_id ) { clean_post_cache( (int) $post_id ); } $key = ( 'acs_form' === $old_type ) ? 'forms' : 'entries'; $changed[ $key ] = count( $ids ); } // 4. Meta keys. "_acs_cf_status" becomes "_acscf_status", and so on. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- One time upgrade routine. Renaming meta keys one row at a time would mean a query per entry per key; the post cache is cleared below. $meta_post_ids = $wpdb->get_col( "SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_key LIKE '\\_acs\\_cf\\_%'" ); if ( ! empty( $meta_post_ids ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- One time upgrade routine, see above. $rows = $wpdb->query( "UPDATE {$wpdb->postmeta} SET meta_key = CONCAT('_acscf_', SUBSTRING(meta_key, 9)) WHERE meta_key LIKE '\\_acs\\_cf\\_%'" ); foreach ( $meta_post_ids as $post_id ) { clean_post_cache( (int) $post_id ); } $changed['meta'] = (int) $rows; } // 5. Embedded forms. The shortcode and the block both changed name. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- One time upgrade routine. Post content is searched by substring, which WP_Query cannot express; the post cache is cleared below. $content_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_content LIKE '%[acs_form%' OR post_content LIKE '%wp:acs-cf/form-block%'" ); if ( ! empty( $content_ids ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- One time upgrade routine, see above. $wpdb->query( "UPDATE {$wpdb->posts} SET post_content = REPLACE( post_content, '[acs_form', '[acscf_form' ) WHERE post_content LIKE '%[acs_form%'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- One time upgrade routine, see above. $wpdb->query( "UPDATE {$wpdb->posts} SET post_content = REPLACE( post_content, 'wp:acs-cf/form-block', 'wp:acs-contact-form/form' ) WHERE post_content LIKE '%wp:acs-cf/form-block%'" ); foreach ( $content_ids as $post_id ) { clean_post_cache( (int) $post_id ); } $changed['content'] = count( $content_ids ); } // 6. The daily reminder was scheduled under the old hook name. $legacy_event = wp_next_scheduled( 'acs_cf_stale_alert' ); if ( $legacy_event ) { wp_clear_scheduled_hook( 'acs_cf_stale_alert' ); } acscf_schedule_stale_alert(); return $changed; } /** * Decide whether the migration still has to run, and run it. * * @return void */ function acscf_maybe_upgrade() { if ( get_option( 'acscf_db_version' ) === ACSCF_DB_VERSION ) { return; } $changed = acscf_migrate_legacy_names(); update_option( 'acscf_db_version', ACSCF_DB_VERSION ); update_option( 'acscf_version', ACSCF_VERSION ); if ( array_sum( $changed ) > 0 ) { update_option( 'acscf_migration_notice', $changed ); } } add_action( 'admin_init', 'acscf_maybe_upgrade' ); /** * Tell the site owner, once, that the rename went through. * * @return void */ function acscf_migration_notice() { if ( ! current_user_can( acscf_manage_capability() ) ) { return; } $changed = get_option( 'acscf_migration_notice', false ); if ( ! is_array( $changed ) ) { return; } delete_option( 'acscf_migration_notice' ); $forms = isset( $changed['forms'] ) ? (int) $changed['forms'] : 0; $entries = isset( $changed['entries'] ) ? (int) $changed['entries'] : 0; $content = isset( $changed['content'] ) ? (int) $changed['content'] : 0; ?>