module-block-type-fields.php
3 years ago
module-block-type-upgrades.php
3 years ago
module-block-type.php
3 months ago
module-block-type-upgrades.php
163 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_module_block_type_upgrades')): |
| 8 | |
| 9 | class acfe_module_block_type_upgrades{ |
| 10 | |
| 11 | /** |
| 12 | * construct |
| 13 | */ |
| 14 | function __construct(){ |
| 15 | |
| 16 | add_action('acfe/do_upgrade', array($this, 'upgrade_0_8_9'), 30); |
| 17 | add_action('acfe/do_upgrade', array($this, 'upgrade_0_8_8'), 20); |
| 18 | add_action('acfe/do_upgrade', array($this, 'upgrade_0_8_5'), 10); |
| 19 | |
| 20 | } |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * upgrade_0_8_9 |
| 25 | * |
| 26 | * acfe/do_upgrade:30 |
| 27 | * |
| 28 | * @param $db_version |
| 29 | */ |
| 30 | function upgrade_0_8_9($db_version){ |
| 31 | |
| 32 | // check already done |
| 33 | if(acf_version_compare($db_version, '>=', '0.8.9')){ |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | // hook on init to load all WP components |
| 38 | // post types, post statuses 'acf-disabled' etc... |
| 39 | add_action('init', function(){ |
| 40 | |
| 41 | // get block types |
| 42 | $posts = get_posts(array( |
| 43 | 'post_type' => 'acfe-dbt', |
| 44 | 'posts_per_page' => -1, |
| 45 | 'fields' => 'ids', |
| 46 | 'post_status' => 'any', |
| 47 | )); |
| 48 | |
| 49 | $todo = array(); |
| 50 | |
| 51 | foreach($posts as $post_id){ |
| 52 | |
| 53 | if(acfe_is_module_v2_item($post_id)){ |
| 54 | $todo[] = $post_id; |
| 55 | } |
| 56 | |
| 57 | } |
| 58 | |
| 59 | if(!$todo){ |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | // get module |
| 64 | $module = acfe_get_module('block_type'); |
| 65 | |
| 66 | // loop |
| 67 | foreach($todo as $post_id){ |
| 68 | |
| 69 | $name = get_post_field('post_name', $post_id); |
| 70 | $settings = acfe_get_settings("modules.block_types.{$name}", array()); |
| 71 | |
| 72 | // db settings found |
| 73 | if($settings){ |
| 74 | |
| 75 | // generate item |
| 76 | $item = wp_parse_args($settings, array( |
| 77 | 'ID' => $post_id, |
| 78 | 'name' => $name, |
| 79 | )); |
| 80 | |
| 81 | // import item (update db) |
| 82 | $module->import_item($item); |
| 83 | |
| 84 | } |
| 85 | |
| 86 | } |
| 87 | |
| 88 | // log |
| 89 | acf_log('[ACF Extended] 0.8.9 Upgrade: Block Types'); |
| 90 | |
| 91 | }); |
| 92 | |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /** |
| 97 | * upgrade_0_8_8 |
| 98 | * |
| 99 | * acfe/do_upgrade:20 |
| 100 | * |
| 101 | * @param $db_version |
| 102 | */ |
| 103 | function upgrade_0_8_8($db_version){ |
| 104 | |
| 105 | // check already done |
| 106 | if(acf_version_compare($db_version, '>=', '0.8.8')){ |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | $old = acfe_get_settings('modules.dynamic_block_type.data', array()); |
| 111 | $new = acfe_get_settings('modules.block_types', array()); |
| 112 | |
| 113 | acfe_delete_settings('modules.dynamic_block_type'); |
| 114 | |
| 115 | // Check |
| 116 | if(empty($old)){ |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | // Log |
| 121 | acf_log('[ACF Extended] 0.8.8 Upgrade: Block Types'); |
| 122 | |
| 123 | // Update |
| 124 | acfe_update_settings('modules.block_types', array_merge($old, $new)); |
| 125 | |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /** |
| 130 | * upgrade_0_8_5 |
| 131 | * |
| 132 | * acfe/do_upgrade:10 |
| 133 | * |
| 134 | * @param $db_version |
| 135 | */ |
| 136 | function upgrade_0_8_5($db_version){ |
| 137 | |
| 138 | // check already done |
| 139 | if(acf_version_compare($db_version, '>=', '0.8.5')){ |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | $old = get_option('acfe_dynamic_block_types', array()); |
| 144 | $new = acfe_get_settings('modules.block_types', array()); |
| 145 | |
| 146 | delete_option('acfe_dynamic_block_types'); |
| 147 | |
| 148 | if(empty($old)){ |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | acf_log('[ACF Extended] 0.8.5 Upgrade: Block Types'); |
| 153 | |
| 154 | // Update |
| 155 | acfe_update_settings('modules.block_types', array_merge($old, $new)); |
| 156 | |
| 157 | } |
| 158 | |
| 159 | } |
| 160 | |
| 161 | acf_new_instance('acfe_module_block_type_upgrades'); |
| 162 | |
| 163 | endif; |