Skip to content

Commit

Permalink
Merge pull request #6 from ucsc/remove-wpdb-queries
Browse files Browse the repository at this point in the history
Removing wpdb queries to meet campuspress requirements
  • Loading branch information
daggerhart authored Apr 15, 2020
2 parents bdd1ed1 + 42b8397 commit 5fe1c1d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 44 deletions.
35 changes: 9 additions & 26 deletions lib/functions/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,52 +73,35 @@ function ucsc_choices_fields_as_field_options( $field ) {

/**
* Helper function get all ACF fields that offer choices.
*
* @param array $field_names | ACF field names.
*
* @return array
*/
function ucsc_acf_get_choices_fields( $field_names = [] ) {
global $wpdb;

$sql = "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'acf-field' AND post_content LIKE '%\"choices\"%'";

// Field names are stored as post_excerpt.
if ( !empty( $field_names ) ) {
if ( !is_array( $field_names ) ) {
$field_names = [ $field_names ];
}
$field_names = esc_sql( $field_names );
$sql .= " AND post_excerpt IN ('" . implode("','", $field_names) . "')";
}

$field_ids = $wpdb->get_col( $sql, 0 );

if ( empty( $field_ids ) ) {
return [];
}

function ucsc_acf_get_choices_fields() {
$choices_fields = [];
$posts = get_posts( [
'posts_per_page' => -1,
'post_type' => 'acf-field',
'orderby' => 'post_title',
'orderby' => 'title',
'order' => 'ASC',
'suppress_filters' => true, // DO NOT allow WPML to modify the query
'cache_results' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'ignore_sticky_posts' => true,
'post__in' => $field_ids,
] );

foreach ($posts as $post) {
$config = maybe_unserialize( $post->post_content );
if ( !isset( $config['choices'] ) ) {
continue;
}
$choices_fields[ $post->post_excerpt ] = [
'ID' => $post->ID,
'name' => $post->post_excerpt,
'label' => $post->post_title,
'choices' => maybe_unserialize( $post->post_content )['choices'],
'choices' => $config['choices'],
];
}

return $choices_fields;
}
}
46 changes: 28 additions & 18 deletions lib/functions/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,32 +300,42 @@ function ucsc_get_departments_by_category() {
return $departments;
}

/**
* Get all taxonomies shown in the UI.
*
* @param string $output
*
* @return string[]|\WP_Taxonomy[]
*/
function ucsc_get_all_taxonomies( $output = 'objects') {
$taxonomies = get_taxonomies( [
'show_ui' => true,
'_builtin' => false,
], $output );
$taxonomies += get_taxonomies( [
'show_ui' => true,
'_builtin' => true,
], $output );

return $taxonomies;
}

/**
* Get a set of arrays for all taxonomy term objects for a given set of post ids.
*
*
* @param array $post_ids
*
* @return array
*/
function ucsc_get_all_terms_for_posts( $post_ids ) {
if ( !is_array( $post_ids ) ) {
$post_ids = [ $post_ids ];
}

// Sanitize here because we can't use prepare replacements for an IN() query.
array_walk( $post_ids, 'absint' );
$post_ids_string = implode( ',', $post_ids );

global $wpdb;
$sql = "SELECT r.object_id, t.name, t.slug, t.term_id, tt.taxonomy
FROM {$wpdb->term_relationships} as r
LEFT JOIN {$wpdb->term_taxonomy} as tt on tt.term_taxonomy_id = r.term_taxonomy_id
LEFT JOIN {$wpdb->terms} as t on t.term_id = tt.term_id
WHERE r.object_id IN ({$post_ids_string})
";
$results = $wpdb->get_results( $sql );

$grouped = [];
foreach ( $results as $result ) {
$grouped[ $result->object_id ][] = $result;
sort($post_ids);
$taxonomies = ucsc_get_all_taxonomies( 'names' );
$grouped = [];
foreach ($post_ids as $post_id) {
$grouped[ $post_id ] = wp_get_post_terms( $post_id, $taxonomies );
}

return $grouped;
Expand Down

0 comments on commit 5fe1c1d

Please sign in to comment.