PodsUpgrade_2_1_0.php
133 lines
| 1 | <?php |
| 2 | |
| 3 | // Don't load directly. |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | die( '-1' ); |
| 6 | } |
| 7 | |
| 8 | /** |
| 9 | * @package Pods\Upgrade |
| 10 | */ |
| 11 | class PodsUpgrade_2_1_0 extends PodsUpgrade { |
| 12 | |
| 13 | /** |
| 14 | * @var string |
| 15 | */ |
| 16 | protected $version = '2.1.0'; |
| 17 | |
| 18 | /** |
| 19 | * |
| 20 | */ |
| 21 | public function __construct() { |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @return array|bool|int|mixed|null|void |
| 26 | */ |
| 27 | public function prepare_relationships() { |
| 28 | $relationship_fields = $this->api->load_fields( [ 'type' => 'pick' ] ); |
| 29 | |
| 30 | $count = 0; |
| 31 | |
| 32 | if ( ! empty( $relationship_fields ) ) { |
| 33 | $count = count( $relationship_fields ); |
| 34 | } |
| 35 | |
| 36 | return $count; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @return string |
| 41 | */ |
| 42 | public function migrate_relationships() { |
| 43 | if ( true === $this->check_progress( __FUNCTION__ ) ) { |
| 44 | return '1'; |
| 45 | } |
| 46 | |
| 47 | $migration_limit = (int) apply_filters( 'pods_upgrade_item_limit', 1500 ); |
| 48 | $migration_limit = max( $migration_limit, 100 ); |
| 49 | |
| 50 | $last_id = (int) $this->check_progress( __FUNCTION__ ); |
| 51 | |
| 52 | $relationship_fields = $this->api->load_fields( [ 'type' => [ 'pick', 'file' ] ] ); |
| 53 | |
| 54 | foreach ( $relationship_fields as $field ) { |
| 55 | $pod = $this->api->load_pod( [ 'pod' => $field['pod'] ] ); |
| 56 | |
| 57 | // Only target pods that are meta-enabled |
| 58 | if ( ! in_array( $pod['type'], [ 'post_type', 'media', 'user', 'comment' ], true ) ) { |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | // Only target multi-select relationships |
| 63 | $single_multi = pods_v( $field['type'] . '_format_type', $field['options'], 'single' ); |
| 64 | |
| 65 | if ( 'multi' === $single_multi ) { |
| 66 | $limit = (int) pods_v( $field['type'] . '_limit', $field['options'], 0 ); |
| 67 | } else { |
| 68 | $limit = 1; |
| 69 | } |
| 70 | |
| 71 | if ( $limit <= 1 ) { |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | // Get and loop through relationship meta |
| 76 | $sql = ' |
| 77 | |
| 78 | '; |
| 79 | // if serialized (or array), save as individual meta items and save new order meta key |
| 80 | }//end foreach |
| 81 | |
| 82 | $last_id = true; |
| 83 | |
| 84 | $rel = []; |
| 85 | |
| 86 | $this->update_progress( __FUNCTION__, $last_id ); |
| 87 | |
| 88 | if ( count( $rel ) === $migration_limit ) { |
| 89 | return '-2'; |
| 90 | } else { |
| 91 | return '1'; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @return string |
| 97 | */ |
| 98 | public function migrate_cleanup() { |
| 99 | $this->upgraded(); |
| 100 | |
| 101 | $this->api->cache_flush_pods(); |
| 102 | |
| 103 | return '1'; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * |
| 108 | */ |
| 109 | public function restart() { |
| 110 | $upgraded = get_option( 'pods_framework_upgraded' ); |
| 111 | |
| 112 | if ( empty( $upgraded ) || ! is_array( $upgraded ) ) { |
| 113 | $upgraded = []; |
| 114 | } |
| 115 | |
| 116 | delete_option( 'pods_framework_upgrade_' . str_replace( '.', '_', $this->version ) ); |
| 117 | |
| 118 | if ( in_array( $this->version, $upgraded, true ) ) { |
| 119 | // @codingStandardsIgnoreLine |
| 120 | unset( $upgraded[ array_search( $this->version, $upgraded ) ] ); |
| 121 | } |
| 122 | |
| 123 | update_option( 'pods_framework_upgraded', $upgraded ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * |
| 128 | */ |
| 129 | public function cleanup() { |
| 130 | $this->restart(); |
| 131 | } |
| 132 | } |
| 133 |