From 1c18f889890a4861bfb9b07173b25fc8b60bc134 Mon Sep 17 00:00:00 2001 From: Jonathan Daggerhart Date: Thu, 26 Mar 2020 14:12:57 -0400 Subject: [PATCH] plugin updates for lots of new H42 work --- lib/external-urls.php | 51 ++++++++++++++ lib/functions/fields.php | 124 +++++++++++++++++++++++++++++++++++ lib/functions/general.php | 123 +++++++++++++++++++++++++++++++++- lib/functions/post-types.php | 46 +++++++++++++ lib/functions/shortcodes.php | 2 +- lib/functions/taxonomies.php | 34 ++++++++++ plugin.php | 4 +- 7 files changed, 381 insertions(+), 3 deletions(-) create mode 100644 lib/external-urls.php create mode 100644 lib/functions/fields.php diff --git a/lib/external-urls.php b/lib/external-urls.php new file mode 100644 index 0000000..e7ed42c --- /dev/null +++ b/lib/external-urls.php @@ -0,0 +1,51 @@ +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' ); \ No newline at end of file diff --git a/lib/functions/fields.php b/lib/functions/fields.php new file mode 100644 index 0000000..55927aa --- /dev/null +++ b/lib/functions/fields.php @@ -0,0 +1,124 @@ + 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; +} \ No newline at end of file diff --git a/lib/functions/general.php b/lib/functions/general.php index 4979dba..74d260f 100644 --- a/lib/functions/general.php +++ b/lib/functions/general.php @@ -208,4 +208,125 @@ function ucsc_pbsci_link_filter($link, $post) } return $link; } -add_filter('post_link', 'ucsc_pbsci_link_filter', 10, 2); \ No newline at end of file +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; +} diff --git a/lib/functions/post-types.php b/lib/functions/post-types.php index ad4ba2c..c9a0858 100644 --- a/lib/functions/post-types.php +++ b/lib/functions/post-types.php @@ -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 diff --git a/lib/functions/shortcodes.php b/lib/functions/shortcodes.php index e814d7c..3153a0e 100644 --- a/lib/functions/shortcodes.php +++ b/lib/functions/shortcodes.php @@ -163,4 +163,4 @@ function resources_shortcode(){ return $resourceList; } } -add_shortcode('resources-shortcode','resources_shortcode'); \ No newline at end of file +add_shortcode('resources-shortcode','resources_shortcode'); diff --git a/lib/functions/taxonomies.php b/lib/functions/taxonomies.php index c449455..7e81c61 100644 --- a/lib/functions/taxonomies.php +++ b/lib/functions/taxonomies.php @@ -408,6 +408,40 @@ function ucsc_support_science_interest() } add_action('init', 'ucsc_support_science_interest'); +/** + * Create a 'Unit Category' taxonomy + * Attached to 'department' Custom Post Type + * @since 1.2.0 + * @link http://codex.wordpress.org/Function_Reference/register_taxonomy + */ +function ucsc_unit_category() +{ + $labels = array( + 'name' => 'Unit Categories', + 'singular_name' => 'Unit Category', + 'search_items' => 'Search Unit Categories', + 'all_items' => 'All Unit Categories', + 'edit_item' => 'Edit Unit Category', + 'update_item' => 'Update Unit Category', + 'add_new_item' => 'Add Unit Category', + 'new_item_name' => 'New Unit Category', + 'menu_name' => 'Unit Categories' + ); + + register_taxonomy( + 'unit-category', + array('department'), + array( + 'hierarchical' => true, + 'labels' => $labels, + 'show_ui' => true, + 'query_var' => true, + 'rewrite' => array('slug' => 'unit'), + ) + ); +} +add_action('init', 'ucsc_unit_category'); + /** * Prevent adding additional terms to Featured taxonomy * diff --git a/plugin.php b/plugin.php index d1b385f..38926dd 100644 --- a/plugin.php +++ b/plugin.php @@ -3,7 +3,7 @@ * Plugin Name: UCSC Science/PBSci Custom Functionality * Plugin URI: https://github.com/ucsc/pbsci-core-functionality-plugin * Description: Contains custom functionality. Theme independent. - * Version: 1.1.2 + * Version: 1.2.0 * Author: Jason Chafin, Senior Developer, UC Santa Cruz Communications & Marketing * Author URI: https://github.com/Herm71 * License: GPL2 @@ -22,6 +22,8 @@ define('UCSC_PLUG_URL', plugin_dir_url(__FILE__)); //Include Customization files: +require_once( UCSC_DIR . '/lib/external-urls.php' ); +require_once( UCSC_DIR . '/lib/functions/fields.php' ); // Post Types require_once( UCSC_DIR . '/lib/functions/post-types.php' );