| @@ -5,8 +5,9 @@ | ||
| 5 | 5 | exit; |
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | use ABlocks\Admin\Settings; |
| 9 | +use ABlocks\Helper; | |
| 9 | 10 | |
| 10 | 11 | class Migration { |
| 11 | 12 | |
| 12 | 13 | public static function init() { |
| @@ -16,8 +17,11 @@ | ||
| 16 | 17 | } |
| 17 | 18 | |
| 18 | 19 | public function run_migration() { |
| 19 | 20 | $this->migrate_font_stack(); |
| 21 | + // Must run BEFORE Settings::save_settings() so the raw option still | |
| 22 | + // shows which keys were never persisted (old "missing = enabled" sites). | |
| 23 | + $this->migrate_integration_block_defaults(); | |
| 20 | 24 | |
| 21 | 25 | $ablocks_version = get_option( 'ablocks_version' ); |
| 22 | 26 | // Save Version Number, flash role management and save permalink |
| 23 | 27 | if ( ABLOCKS_VERSION !== $ablocks_version ) { |
| @@ -25,8 +29,81 @@ | ||
| 25 | 29 | $this->migrate_1_6_3( $ablocks_version ); |
| 26 | 30 | $this->migrate_1_8_0( $ablocks_version ); |
| 27 | 31 | update_option( 'ablocks_version', ABLOCKS_VERSION ); |
| 28 | 32 | } |
| 33 | + } | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Academy LMS, StoreEngine and ECM blocks used to default to enabled, and | |
| 37 | + * old versions only persisted the "off" state — a block without an entry in | |
| 38 | + * the visibility option was treated as enabled by the reader. The reader | |
| 39 | + * now treats missing keys as disabled, so an old sparse save would silently | |
| 40 | + * turn integration blocks off after upgrade. | |
| 41 | + * | |
| 42 | + * Once per integration: for every block without an explicit entry, write | |
| 43 | + * `true` when the companion plugin is active (preserve the old on-by- | |
| 44 | + * default behavior for sites that actually use the integration) and | |
| 45 | + * `false` otherwise (new installs and sites that never had the plugin). | |
| 46 | + * Blocks that already have an explicit value are the user's choice and are | |
| 47 | + * left alone. No post_content scan — this is O(number of blocks). | |
| 48 | + */ | |
| 49 | + public function migrate_integration_block_defaults() { | |
| 50 | + $integrations = [ | |
| 51 | + 'academy' => 'academy/academy.php', | |
| 52 | + 'storeengine' => 'storeengine/storeengine.php', | |
| 53 | + 'ecm' => 'easy-content-manager/easy-content-manager.php', | |
| 54 | + ]; | |
| 55 | + // _v2: an earlier version of this migration treated any `true` already | |
| 56 | + // in the option as a user choice and left stale installer defaults in | |
| 57 | + // place. Bump the tracker so sites that ran the old pass re-migrate. | |
| 58 | + $option_key = 'ablocks_integration_blocks_migrated_v2'; | |
| 59 | + $done = (array) get_option( $option_key, [] ); | |
| 60 | + if ( ! array_diff( array_keys( $integrations ), $done ) ) { | |
| 61 | + return; | |
| 62 | + } | |
| 63 | + | |
| 64 | + $raw = get_option( ABLOCKS_BLOCKS_VISIBILITY_SETTINGS_NAME ); | |
| 65 | + $saved_blocks = $raw ? (array) json_decode( $raw, true ) : []; | |
| 66 | + $block_names = array_keys( Settings\Blocks::get_default_data() ); | |
| 67 | + $changed = false; | |
| 68 | + | |
| 69 | + foreach ( $integrations as $prefix => $plugin_file ) { | |
| 70 | + if ( in_array( $prefix, $done, true ) ) { | |
| 71 | + continue; | |
| 72 | + } | |
| 73 | + $done[] = $prefix; | |
| 74 | + $plugin_active = Helper::is_plugin_active( $plugin_file ); | |
| 75 | + $names = array_filter( $block_names, function ( $name ) use ( $prefix ) { | |
| 76 | + return 0 === strpos( $name, $prefix . '-' ); | |
| 77 | + } ); | |
| 78 | + foreach ( $names as $name ) { | |
| 79 | + if ( $plugin_active ) { | |
| 80 | + // Preserve the user's choice when they have the companion | |
| 81 | + // plugin. Missing keys mean the old "missing = enabled" | |
| 82 | + // behavior, so write true to keep those blocks on. | |
| 83 | + if ( ! array_key_exists( $name, $saved_blocks ) ) { | |
| 84 | + $saved_blocks[ $name ] = true; | |
| 85 | + $changed = true; | |
| 86 | + } | |
| 87 | + continue; | |
| 88 | + } | |
| 89 | + // Companion plugin not active → the blocks cannot render. | |
| 90 | + // Force-disable, including any `true` left behind by the old | |
| 91 | + // installer which saved on-by-default values into the option. | |
| 92 | + if ( ! array_key_exists( $name, $saved_blocks ) || $saved_blocks[ $name ] ) { | |
| 93 | + $saved_blocks[ $name ] = false; | |
| 94 | + $changed = true; | |
| 95 | + } | |
| 96 | + } | |
| 97 | + } | |
| 98 | + | |
| 99 | + if ( $changed ) { | |
| 100 | + update_option( ABLOCKS_BLOCKS_VISIBILITY_SETTINGS_NAME, wp_json_encode( $saved_blocks ) ); | |
| 101 | + // Block classes read this global when they're constructed later in the request. | |
| 102 | + $existing = isset( $GLOBALS['ablocks_blocks'] ) ? (array) $GLOBALS['ablocks_blocks'] : []; | |
| 103 | + $GLOBALS['ablocks_blocks'] = (object) array_merge( $existing, $saved_blocks ); | |
| 104 | + } | |
| 105 | + update_option( $option_key, $done ); | |
| 29 | 106 | } |
| 30 | 107 | |
| 31 | 108 | /** |
| 32 | 109 | * font-family declarations are now emitted as full stacks (quoted family + |