Exemple #1
0
/**
 * Check how many posts needs checkboxes update.
 * 
 * @param type $field
 * @param type $action
 * @return boolean|int 
 */
function wpcf_admin_fields_checkboxes_migrate_empty_check($field, $action)
{
    $field = wpcf_admin_fields_get_field($field);
    if (empty($field) || $field['type'] != 'checkboxes' || empty($field['data']['options'])) {
        return false;
    }
    $filter = wpcf_admin_fields_get_filter_by_field($field['id']);
    if (!empty($filter)) {
        $posts = array();
        $meta_key = wpcf_types_get_meta_prefix($field) . $field['id'];
        $meta_query = '';
        // "wpcf-fields-checkboxes-option-1873650245";s:1:"1";
        if ($action == 'do_not_save_check') {
            $query = array();
            foreach ($field['data']['options'] as $option_id => $option_data) {
                $query[] = '\\"' . $option_id . '\\";s:1:\\"0\\";';
            }
            $meta_query = "(m.meta_key = '{$meta_key}' AND (m.meta_value LIKE '%%" . implode("%%' OR m.meta_value LIKE '%%", $query) . "%%'))";
            $posts = wpcf_admin_fields_get_posts_by_filter($filter, $meta_query);
        } else {
            if ($action == 'save_check') {
                $query = array();
                foreach ($field['data']['options'] as $option_id => $option_data) {
                    $query[] = '\\"' . $option_id . '\\";s:1:\\"0\\";';
                }
                $meta_query = "(m.meta_key = '{$meta_key}' AND (m.meta_value NOT LIKE '%%" . implode("%%' AND m.meta_value NOT LIKE '%%", $query) . "%%'))";
                $posts = wpcf_admin_fields_get_posts_by_filter($filter, $meta_query);
            }
        }
        $option = get_option('wpcf_checkboxes_migration', array());
        $cache_key = $action == 'do_not_save_check' ? 'do_not_save' : 'save';
        $option[$cache_key] = $posts;
        update_option('wpcf_checkboxes_migration', $option);
        return $posts;
    }
    return false;
}
Exemple #2
0
/**
 * Check how many posts needs checkboxes update.
 *
 * @global object $wpdb
 *
 * @param type $field
 * @param type $action
 * @return boolean|int
 */
function wpcf_admin_fields_checkboxes_migrate_empty_check($field, $action)
{
    if ($field['type'] != 'checkboxes' || empty($field['data']['options'])) {
        return false;
    }
    if ($field['meta_type'] == 'usermeta') {
        global $wpdb;
        if ($action == 'do_not_save_check') {
            $query = array();
            foreach ($field['data']['options'] as $option_id => $option_data) {
                $query[] = '\\"' . esc_sql($option_id) . '\\";i:0;';
            }
            $meta_query = "SELECT u.ID FROM {$wpdb->users} u\n                LEFT JOIN {$wpdb->usermeta} um ON u.ID = um.user_id\n                WHERE (um.meta_key = '%s' AND (um.meta_value LIKE '%%" . implode("%%' OR um.meta_value LIKE '%%", $query) . "%%'))";
        } else {
            if ($action == 'save_check') {
                $query = array();
                foreach ($field['data']['options'] as $option_id => $option_data) {
                    // Check only if missing
                    $query[] = '\\"' . esc_sql($option_id) . '\\"';
                }
                $meta_query = "SELECT u.ID FROM {$wpdb->users} u\n                LEFT JOIN {$wpdb->usermeta} um ON u.ID = um.user_id\n                WHERE (um.meta_key = '%s' AND (um.meta_value NOT LIKE '%%" . implode("%%' OR um.meta_value NOT LIKE '%%", $query) . "%%'))";
            }
        }
        $users = $wpdb->get_col($wpdb->prepare($meta_query, $field['meta_key']));
        $option = get_option('wpcf_checkboxes_migration_usermeta', array());
        $cache_key = $action == 'do_not_save_check' ? 'do_not_save' : 'save';
        $option[$field['meta_key']][$cache_key] = $users;
        update_option('wpcf_checkboxes_migration_usermeta', $option);
        return $users;
    }
    $filter = wpcf_admin_fields_get_filter_by_field($field['id']);
    if (!empty($filter)) {
        $posts = array();
        $meta_key = esc_sql(wpcf_types_get_meta_prefix($field) . $field['id']);
        $meta_query = '';
        // "wpcf-fields-checkboxes-option-1873650245";s:1:"1";
        if ($action == 'do_not_save_check') {
            $query = array();
            foreach ($field['data']['options'] as $option_id => $option_data) {
                $query[] = '\\"' . esc_sql($option_id) . '\\";i:0;';
            }
            $meta_query = "(m.meta_key = '{$meta_key}' AND (m.meta_value LIKE '%%" . implode("%%' OR m.meta_value LIKE '%%", $query) . "%%'))";
            $posts = wpcf_admin_fields_get_posts_by_filter($filter, $meta_query);
        } else {
            if ($action == 'save_check') {
                $query = array();
                foreach ($field['data']['options'] as $option_id => $option_data) {
                    // Check only if missing
                    $query[] = '\\"' . esc_sql($option_id) . '\\"';
                }
                $meta_query = "(m.meta_key = '{$meta_key}' AND (m.meta_value NOT LIKE '%%" . implode("%%' OR m.meta_value NOT LIKE '%%", $query) . "%%'))";
                $posts = wpcf_admin_fields_get_posts_by_filter($filter, $meta_query);
            }
        }
        $option = get_option('wpcf_checkboxes_migration', array());
        $cache_key = $action == 'do_not_save_check' ? 'do_not_save' : 'save';
        $option[$cache_key] = $posts;
        update_option('wpcf_checkboxes_migration', $option);
        return $posts;
    }
    return false;
}