PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.9.2
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.9.2
1.1.10 1.1.9 1.1.8 1.1.7 1.1.6 1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.1 1.0.0 0.9.8 0.9.7 0.9.6 0.9.4 0.9.5 0.9.3 0.9.2 0.9.1 0.9.0 0.8.9 0.8.8 0.8.7 All 34 releases
desktop-mode / includes / migrations.php

migrations.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 0.9.2, at includes/migrations.php

164 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Desktop Mode — one-time data migrations.
4 *
5 * A tiny, option-versioned migration runner modeled on the lazy schema
6 * installer in `includes/desktop-files/schema.php`: a stored option holds
7 * the highest migration version that has run; on every admin load we
8 * compare it against {@see DESKTOP_MODE_MIGRATION_VERSION} and run any
9 * pending migrations exactly once. Guarded so it is a cheap no-op after
10 * the first successful pass.
11 *
12 * @package WPDesktopMode
13 */
14
15 defined( 'ABSPATH' ) || exit;
16
17 /**
18 * Highest migration version shipped by the plugin.
19 *
20 * Bump this (and add a matching branch in
21 * {@see desktop_mode_run_pending_migrations}) whenever a new one-time
22 * migration is needed.
23 *
24 * - 1: native list windows flipped from opt-out (default ON) to opt-in
25 * Beta (default OFF). Clears the five `native*Enabled` flags from every
26 * user who had them persisted so the whole install reverts to opt-in.
27 * - 2: post & taxonomy-term AI analysis was removed (the copilot now only
28 * analyzes comments for spam, and the assistant finds content via native
29 * WordPress search). Unschedules any queued `desktop_mode_ai_analyze_post`
30 * / `desktop_mode_ai_analyze_term` cron events left over from prior versions.
31 *
32 * @since 0.10.0
33 */
34 const DESKTOP_MODE_MIGRATION_VERSION = 2;
35
36 /** Option storing the highest migration version that has run. autoload=no. */
37 const DESKTOP_MODE_MIGRATION_OPTION = 'desktop_mode_migration_version';
38
39 /**
40 * Runs any pending migrations, then records the new high-water mark.
41 *
42 * Idempotent: bails immediately when the stored version is already at
43 * or above the shipped version, so it is safe to fire on every request.
44 *
45 * @since 0.10.0
46 *
47 * @return void
48 */
49 function desktop_mode_maybe_run_migrations() {
50 $installed = (int) get_option( DESKTOP_MODE_MIGRATION_OPTION, 0 );
51 if ( $installed >= DESKTOP_MODE_MIGRATION_VERSION ) {
52 return;
53 }
54
55 desktop_mode_run_pending_migrations( $installed );
56
57 update_option( DESKTOP_MODE_MIGRATION_OPTION, DESKTOP_MODE_MIGRATION_VERSION, false );
58 }
59 add_action( 'admin_init', 'desktop_mode_maybe_run_migrations' );
60
61 /**
62 * Dispatches each migration whose version is newer than what has run.
63 *
64 * @since 0.10.0
65 *
66 * @param int $from The highest migration version already applied.
67 * @return void
68 */
69 function desktop_mode_run_pending_migrations( $from ) {
70 $from = (int) $from;
71
72 if ( $from < 1 ) {
73 desktop_mode_migrate_os_settings_optin();
74 }
75
76 if ( $from < 2 ) {
77 desktop_mode_migrate_unschedule_post_term_ai();
78 }
79 }
80
81 /**
82 * Migration 1 — reset the native list windows to opt-in.
83 *
84 * The native Posts/Pages/Users/Plugins/Comments windows used to default
85 * ON (opt-out). The shell persists the whole OS-settings object on every
86 * change, so most active users already have these flags stored as `true`
87 * and would keep the native UI even after the default flips. This clears
88 * the five flags from every user who has the meta, leaving the rest of
89 * their settings (wallpaper, accent, dock order, …) untouched. On the
90 * next read the cleared keys fall back to the new `false` default, so the
91 * whole install lands on opt-in and users re-enable each window from
92 * OS Settings → Features → Beta features.
93 *
94 * Only users who actually have the meta are queried — fresh accounts and
95 * users who never touched OS Settings are skipped entirely.
96 *
97 * @since 0.10.0
98 *
99 * @return void
100 */
101 function desktop_mode_migrate_os_settings_optin() {
102 $flags = array(
103 'nativePostsEnabled',
104 'nativePagesEnabled',
105 'nativeUsersEnabled',
106 'nativePluginsEnabled',
107 'nativeCommentsEnabled',
108 );
109
110 $user_ids = get_users(
111 array(
112 'fields' => 'ID',
113 'meta_key' => DESKTOP_MODE_OS_SETTINGS_META_KEY, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- one-time migration; the key is indexed in usermeta and the scan is guarded to run once.
114 'meta_compare' => 'EXISTS',
115 )
116 );
117
118 foreach ( $user_ids as $user_id ) {
119 $raw = get_user_meta( (int) $user_id, DESKTOP_MODE_OS_SETTINGS_META_KEY, true );
120 if ( ! is_array( $raw ) ) {
121 continue;
122 }
123
124 $changed = false;
125 foreach ( $flags as $flag ) {
126 if ( array_key_exists( $flag, $raw ) ) {
127 unset( $raw[ $flag ] );
128 $changed = true;
129 }
130 }
131
132 if ( ! $changed ) {
133 continue;
134 }
135
136 // Re-save through the canonical sanitizer so the cleared flags are
137 // backfilled with the new `false` default and the rest of the
138 // settings array is normalized exactly as a client write would be.
139 desktop_mode_save_os_settings( (int) $user_id, $raw );
140 }
141 }
142
143 /**
144 * Migration 2 — unschedule leftover post/term AI analysis jobs.
145 *
146 * Post and taxonomy-term analysis was removed: the copilot now only
147 * analyzes comments (for the spam score), and the AI assistant finds
148 * content with native WordPress keyword search. Their cron callbacks no
149 * longer exist, so any single-events still queued from a prior version
150 * would simply no-op — but we clear them so the cron array stays tidy and
151 * `wp cron event list` doesn't show orphaned hooks.
152 *
153 * Existing `_desktop_mode_ai_analysis` meta on posts/terms is left in place
154 * (hidden, harmless, and cheap to ignore).
155 *
156 * @since 0.11.0
157 *
158 * @return void
159 */
160 function desktop_mode_migrate_unschedule_post_term_ai() {
161 wp_unschedule_hook( 'desktop_mode_ai_analyze_post' );
162 wp_unschedule_hook( 'desktop_mode_ai_analyze_term' );
163 }
164