/** * Check how many posts needs checkbox update. * * @param type $field * @param type $action * @return boolean|int */ function wpcf_admin_fields_checkbox_migrate_empty_check($field, $action) { $field = wpcf_admin_fields_get_field($field); if (empty($field) || $field['type'] != 'checkbox') { 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 = ''; if ($action == 'do_not_save_check') { $meta_query = "(m.meta_key = '{$meta_key}' AND m.meta_value = '0')"; $posts = wpcf_admin_fields_get_posts_by_filter($filter, $meta_query); } else { if ($action == 'save_check') { $posts = wpcf_admin_fields_get_posts_by_filter_missing_meta($filter, $meta_key); } } $option = get_option('wpcf_checkbox_migration', array()); $cache_key = $action == 'do_not_save_check' ? 'do_not_save' : 'save'; $option[$cache_key] = $posts; update_option('wpcf_checkbox_migration', $option); return $posts; } return false; }
/** * Check how many posts needs checkbox update. * * @param type $field * @param type $action * @return boolean|int */ function wpcf_admin_fields_checkbox_migrate_empty_check($field, $action) { if ($field['type'] != 'checkbox') { return false; } if ($field['meta_type'] == 'postmeta') { $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 = ''; if ($action == 'do_not_save_check') { $meta_query = "(m.meta_key = '{$meta_key}' AND m.meta_value = '0')"; $posts = wpcf_admin_fields_get_posts_by_filter($filter, $meta_query); } else { if ($action == 'save_check') { $posts = wpcf_admin_fields_get_posts_by_filter_missing_meta($filter, $meta_key); } } $option = get_option('wpcf_checkbox_migration', array()); $cache_key = $action == 'do_not_save_check' ? 'do_not_save' : 'save'; $option[$cache_key] = $posts; update_option('wpcf_checkbox_migration', $option); return $posts; } } else { if ($field['meta_type'] == 'usermeta') { $option = get_option('wpcf_checkbox_migration_usermeta', array()); $cache_key = $action == 'do_not_save_check' ? 'do_not_save' : 'save'; if ($action == 'do_not_save_check') { $user_query = new WP_User_Query(array('meta_key' => $field['meta_key'], 'meta_value' => '0', 'meta_compare' => '=', 'fields' => 'ID')); $r = $user_query->results; } else { if ($action == 'save_check') { global $wpdb; $_query = "SELECT u.ID FROM {$wpdb->users} u WHERE NOT EXISTS (SELECT um.user_id FROM {$wpdb->usermeta} um WHERE u.ID = um.user_id AND um.meta_key = '%s')"; $r = $wpdb->get_col($wpdb->prepare($_query, $field['meta_key'])); } } $option[$field['meta_key']][$cache_key] = $r; update_option('wpcf_checkbox_migration_usermeta', $option); return $r; } } return false; }