-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from ucsc/h42-plugin-updates
plugin updates for lots of new H42 work
- Loading branch information
Showing
7 changed files
with
381 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.