wpml-upgrade-string-statuses.php 3.01 KB
Newer Older
John Punzalan's avatar
John Punzalan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
<?php

function update_string_statuses() {
    global $wpdb;

    $wpdb->update(
        $wpdb->prefix . 'icl_string_translations',
        array( 'status' => ICL_TM_COMPLETE, 'batch_id' => 0 ),
        array( 'batch_id' => -1, 'status' => 1 )
    );

    $wpdb->update(
        $wpdb->prefix . 'icl_string_translations',
        array( 'status' => ICL_TM_NEEDS_UPDATE, 'batch_id' => 0 ),
        array( 'batch_id' => -1, 'status' => 2 )
    );

    $wpdb->update(
        $wpdb->prefix . 'icl_string_translations',
        array( 'status' => ICL_TM_WAITING_FOR_TRANSLATOR, 'batch_id' => 0 ),
        array( 'batch_id' => -1, 'status' => 11 )
    );
    $sql = "ALTER TABLE `{$wpdb->prefix}icl_string_translations` CHANGE batch_id batch_id int DEFAULT 0 NOT NULL;";
    $wpdb->query( $sql );

}

function fix_icl_string_status() {
    global $wpdb;

    $wpdb->query(
        $wpdb->prepare(
            "UPDATE {$wpdb->prefix}icl_strings s
                                 SET s.status = %d
                                 WHERE EXISTS ( SELECT 1
                                                FROM {$wpdb->prefix}icl_string_translations st
                                                WHERE string_id = s.id AND st.status = %d )",
            ICL_TM_NEEDS_UPDATE,
            ICL_TM_NEEDS_UPDATE
        )
    );

    $wpdb->query(
        $wpdb->prepare(
            "UPDATE {$wpdb->prefix}icl_strings s
                                 SET s.status = %d
                                 WHERE  ( SELECT COUNT(string_id)
                                                FROM {$wpdb->prefix}icl_string_translations st
                                                WHERE st.string_id = s.id AND st.status = %d ) = (( SELECT COUNT(*)
                                                                                         FROM {$wpdb->prefix}icl_languages
                                                                                         WHERE active = 1) - 1)",
            ICL_TM_COMPLETE,
            ICL_TM_COMPLETE
        )
    );

    $wpdb->query(
        $wpdb->prepare(
            "UPDATE {$wpdb->prefix}icl_strings s
                                 SET s.status = %d
                                 WHERE  ( SELECT COUNT(string_id)
                                                FROM {$wpdb->prefix}icl_string_translations st
                                                WHERE st.string_id = s.id AND st.status = %d ) < (( SELECT COUNT(*)
                                                                                         FROM {$wpdb->prefix}icl_languages
                                                                                         WHERE active = 1) - 1)
                                                AND ( SELECT COUNT(string_id)
                                                      FROM {$wpdb->prefix}icl_string_translations st2
                                                      WHERE st2.string_id = s.id AND st2.status = %d ) > 0 ",
            2,
            ICL_TM_COMPLETE,
            ICL_TM_COMPLETE
        )
    );
}