Skip to content

Commit

Permalink
Merge pull request #5 from ucsc/h42-plugin-updates
Browse files Browse the repository at this point in the history
plugin updates for lots of new H42 work
  • Loading branch information
daggerhart authored Mar 26, 2020
2 parents cb5aa8b + 1c18f88 commit bdd1ed1
Show file tree
Hide file tree
Showing 7 changed files with 381 additions and 3 deletions.
51 changes: 51 additions & 0 deletions lib/external-urls.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* Mechanisms for WordPress posts that represent urls on other websites.
*/

/**
* Alter the permalink for custom post type posts that are configured as external.
*
* @link https://codex.wordpress.org/Plugin_API/Filter_Reference/post_type_link
*
* @param string $link The post's permalink.
* @param WP_Post $post The post in question.
* @param bool $leavename Whether to keep the post name.
* @param bool $sample Is it a sample permalink.
*/
function ucsc_pbsci_external_url_alter_permalink( $link, $post, $leavename, $sample) {
if ( $sample ) {
return $link;
}

$is_external = get_field( 'external_url_switch', $post->ID );
$external_link = get_field( 'external_url', $post->ID );

if ( !is_admin() && $is_external && $external_link && filter_var($external_link, FILTER_VALIDATE_URL) !== FALSE ) {
$link = $external_link;
}

return $link;
}
add_filter('post_type_link', 'ucsc_pbsci_external_url_alter_permalink', 20, 4);

/**
* Redirect non-logged-in users that visit a post that is configured to be external.
*
* @link https://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect
*/
function ucsc_pbsci_external_url_redirect() {
if ( is_singular() && !is_user_logged_in() ) {
$post = get_post();

$is_external = get_field( 'external_url_switch', $post->ID );
$external_link = get_field( 'external_url', $post->ID );

if ( $is_external && $external_link && filter_var($external_link, FILTER_VALIDATE_URL) !== FALSE ) {
wp_redirect( $external_link );
exit;
}
}
}
add_action( 'template_redirect', 'ucsc_pbsci_external_url_redirect' );
124 changes: 124 additions & 0 deletions lib/functions/fields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

/**
* Dynamically provide the post type value options to ACF fields.
*
* @link https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
*/
function ucsc_post_type_field_options( $field ) {
$field['choices'] = [];

// Get all the desirable post types
$types = get_post_types( [
'public' => true,
'_builtin' => true,
], 'objects' );
$types += get_post_types( [
'public' => true,
'_builtin' => false,
'publicly_queryable' => true,
], 'objects' );

foreach ( $types as $type ) {
$field['choices'][ $type->name ] = $type->label;
}

return $field;
}
add_filter('acf/load_field/name=cta_visibility_post_type', 'ucsc_post_type_field_options');
add_filter('acf/load_field/name=fc_query_post_type', 'ucsc_post_type_field_options');

/**
* Dynamically provide the taxonomy value options to ACF fields.
*
* @link https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
*/
function ucsc_taxonomy_field_options( $field ) {
$field['choices'] = [];

// Get all the desirable post types
$types = get_taxonomies( [
'show_ui' => true,
'_builtin' => false,
], 'objects' );

foreach ( $types as $type ) {
$field['choices'][ $type->name ] = $type->label;
}

return $field;
}
add_filter('acf/load_field/name=filter_taxonomy', 'ucsc_taxonomy_field_options');

/**
* Dynamically provide
*
* @link https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
*/
function ucsc_choices_fields_as_field_options( $field ) {
$field['choices'] = [];
$choices_fields = ucsc_acf_get_choices_fields();

if ( isset( $choices_fields[ $field['name'] ] ) ) {
unset( $choices_fields[ $field['name'] ] );
}

foreach ( $choices_fields as $_field ) {
$field['choices'][ $_field['name'] ] = $_field['label'];
}

return $field;
}
add_filter('acf/load_field/name=filter_field', 'ucsc_choices_fields_as_field_options');

/**
* 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 [];
}

$choices_fields = [];
$posts = get_posts( [
'posts_per_page' => -1,
'post_type' => 'acf-field',
'orderby' => 'post_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) {
$choices_fields[ $post->post_excerpt ] = [
'ID' => $post->ID,
'name' => $post->post_excerpt,
'label' => $post->post_title,
'choices' => maybe_unserialize( $post->post_content )['choices'],
];
}

return $choices_fields;
}
123 changes: 122 additions & 1 deletion lib/functions/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,125 @@ function ucsc_pbsci_link_filter($link, $post)
}
return $link;
}
add_filter('post_link', 'ucsc_pbsci_link_filter', 10, 2);
add_filter('post_link', 'ucsc_pbsci_link_filter', 10, 2);

/**
* Search for CTAs with visibility options that match the current WP route.
*
* @return array
*/
function ucsc_cta_get_visible_ctas() {
// Simple caching mechanism so this function can be run multiple times
// without adding load.
static $ctas = [];

if ( !empty( $ctas ) ) {
return $ctas;
}

// Search for CTAs visible to the current route.
if ( is_singular() || is_archive() || is_search() ) {
$id = get_the_ID();
$post_type = get_post_type();

$query = new WP_Query( [
'post_type' => 'cta',
'ignore_sticky_posts' => true,
'meta_query' => [
'relation' => 'AND',
[
'key' => 'cta_fields_cta_switch',
'value' => '1',
],
[
'relation' => 'OR',
[
'key' => 'cta_visibility_post_type',
'value' => $post_type,
'compare' => 'LIKE',
],
[
'key' => 'cta_visibility_page',
'value' => "\"{$id}\"",
'compare' => 'LIKE',
],
[
'key' => 'cta_visibility_global',
'value' => 1,
],
]
]
] );

if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$ctas[ get_the_ID() ] = get_post();
}
wp_reset_query();
}
}

return $ctas;
}
add_action( 'wp', 'ucsc_cta_get_visible_ctas' );

/**
* Utility function to get a list of all published departments, grouped by
* their unit-category.
*
* @return array
*/
function ucsc_get_departments_by_category() {
$departments = [];

$posts = get_posts( [
'post_type' => 'department',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'ignore_sticky_posts' => true,
] );

foreach ($posts as $post) {
$post_terms = get_the_terms( $post, 'unit-category' );

if ( !empty($post_terms) ) {
$post->unit_category = $post_terms[0];
$departments[ $post_terms[0]->name ][] = $post;
}
}

return $departments;
}

/**
* Get a set of arrays for all taxonomy term objects for a given set of 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;
}

return $grouped;
}
46 changes: 46 additions & 0 deletions lib/functions/post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,52 @@ function ucsc_register_suppport_science_post_type()
}
add_action('init', 'ucsc_register_suppport_science_post_type');

/**
* Register CTA Post Type
*
* @link https://codex.wordpress.org/Function_Reference/register_post_type
*/
function ucsc_register_cta_post_type()
{
$labels = array(
'name' => 'CTA',
'singular_name' => 'CTA',
'add_new' => 'Add New CTA',
'add_new_item' => 'Add New CTA',
'edit_item' => 'Edit CTA',
'new_item' => 'New CTA',
'view_item' => 'View CTA',
'search_items' => 'Search CTAs',
'not_found' => 'No CTA found',
'not_found_in_trash' => 'No CTA found in trash',
'parent_item_colon' => '',
'menu_name' => 'CTAs'
);

$args = array(
'labels' => $labels,
'public' => true,
'exclude_from_search' => true,
'publicly_queryable' => false,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array(
'with_front' => false,
'slug' => 'cta',
),
'capability_type' => 'page',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
//'menu_icon' => 'megaphone',
'show_in_rest' => false,
'supports' => array('title')
);

register_post_type('cta', $args);
}
add_action('init', 'ucsc_register_cta_post_type');

/**
* Flush permalink rewrite on activation
Expand Down
2 changes: 1 addition & 1 deletion lib/functions/shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,4 @@ function resources_shortcode(){
return $resourceList;
}
}
add_shortcode('resources-shortcode','resources_shortcode');
add_shortcode('resources-shortcode','resources_shortcode');
Loading

0 comments on commit bdd1ed1

Please sign in to comment.