Tribe__Events__Linked_Posts::saved_linked_post_dropdown( string $post_type, mixed $current = null )
Helper function for displaying dropdowns for linked post types
Contents
Parameters
- $post_type
-
(string) (Required) Post type to display dropdown for.
- $current
-
(mixed) (Optional) The current saved linked post item.
Default value: null
Source
File: src/Tribe/Linked_Posts.php
public function saved_linked_post_dropdown( $post_type, $current = null ) {
$post_type_object = get_post_type_object( $post_type );
$linked_post_type_container = $this->get_post_type_container( $post_type );
$linked_post_type_id_field = $this->get_post_type_id_field_index( $post_type );
$name = "{$linked_post_type_container}[{$linked_post_type_id_field}][]";
$my_linked_post_ids = array();
$current_user = wp_get_current_user();
$can_edit_others_posts = current_user_can( $post_type_object->cap->edit_others_posts );
$plural_name = $this->linked_post_types[ $post_type ]['name'];
$singular_name = ! empty( $this->linked_post_types[ $post_type ]['singular_name'] ) ? $this->linked_post_types[ $post_type ]['singular_name'] : $plural_name;
$singular_name_lowercase = ! empty( $this->linked_post_types[ $post_type ]['singular_name_lowercase'] ) ? $this->linked_post_types[ $post_type ]['singular_name_lowercase'] : $singular_name;
$options = (object) array(
'owned' => array(
'text' => sprintf( esc_html__( 'My %s', 'the-events-calendar' ), $plural_name ),
'children' => array(),
),
'available' => array(
'text' => sprintf( esc_html__( 'Available %s', 'the-events-calendar' ), $plural_name ),
'children' => array(),
),
);
// backwards compatibility with old organizer filter
if ( Tribe__Events__Organizer::POSTTYPE === $post_type ) {
/**
* Filters the linked organizer dropdown optgroup label that holds organizers that have
* been created by that user
*
* @deprecated 4.2
*
* @param string $my_optgroup_name Label of the optgroup for the "My Organizers" section
*/
$options->owned['text'] = apply_filters( 'tribe_events_saved_organizers_dropdown_my_optgroup', $options->owned['text'] );
/**
* Filters the linked organizer dropdown optgroup label for saved organizers
*
* @deprecated 4.2
*
* @param string $my_optgroup_name Label of the optgroup for the "Available Organizers" section
*/
$options->available['text'] = apply_filters( 'tribe_events_saved_organizers_dropdown_optgroup', $options->available['text'] );
}
/**
* Filters the linked post dropdown optgroup label that holds organizers that have
* been created by that user
*
* @since 4.2
*
* @param string $my_optgroup_name Label of the optgroup for the "My X" section
* @param string $post_type Post type of the linked post
*/
$options->owned['text'] = apply_filters( 'tribe_events_saved_linked_post_dropdown_my_optgroup', $options->owned['text'], $post_type );
/**
* Filters the linked post dropdown optgroup label that holds all published posts of the given type
*
* @since 4.2
*
* @param string $my_optgroup_name Label of the optgroup for the "Available X" section
* @param string $post_type Post type of the linked post
*/
$options->available['text'] = apply_filters( 'tribe_events_saved_linked_post_dropdown_optgroup', $options->available['text'], $post_type );
add_filter( 'tribe_events_return_all_linked_posts_if_none', '__return_true' );
$my_linked_posts = $this->get_linked_post_info(
$post_type,
array(
'post_status' => array(
'publish',
'draft',
'private',
'pending',
),
'author' => $current_user->ID,
)
);
if ( ! empty( $my_linked_posts ) ) {
foreach ( $my_linked_posts as $my_linked_post ) {
$my_linked_post_ids[] = $my_linked_post->ID;
$new_child = array(
'id' => $my_linked_post->ID,
'text' => wp_kses( get_the_title( $my_linked_post->ID ), array() ),
);
$edit_link = get_edit_post_link( $my_linked_post );
if ( ! empty( $edit_link ) ) {
$new_child['edit'] = $edit_link;
}
$options->owned['children'][] = $new_child;
}
}
if ( $can_edit_others_posts ) {
$linked_posts = $this->get_linked_post_info(
$post_type,
array(
'post_status' => array(
'publish',
'draft',
'private',
'pending',
),
'post__not_in' => $my_linked_post_ids,
)
);
} else {
$linked_posts = $this->get_linked_post_info(
$post_type,
array(
'post_status' => 'publish',
'post__not_in' => $my_linked_post_ids,
)
);
}
remove_filter( 'tribe_events_return_all_linked_posts_if_none', '__return_true' );
if ( $linked_posts ) {
foreach ( $linked_posts as $linked_post ) {
$new_child = array(
'id' => $linked_post->ID,
'text' => wp_kses( get_the_title( $linked_post->ID ), array() ),
);
$edit_link = get_edit_post_link( $linked_post );
if ( ! empty( $edit_link ) ) {
$new_child['edit'] = $edit_link;
}
$options->available['children'][] = $new_child;
}
}
// Clean Both Options
$options->owned['children'] = array_filter( $options->owned['children'] );
$options->available['children'] = array_filter( $options->available['children'] );
// When Owned is empty, we only use Available
if ( empty( $options->owned['children'] ) ) {
$data = $options->available['children'];
// When Available is empty, we only use Owned
} elseif ( empty( $options->available['children'] ) ) {
$data = $options->owned['children'];
// If we have both we make it an array
} else {
$data = array_values( (array) $options );
}
$user_can_create = ( ! empty( $post_type_object->cap->create_posts ) && current_user_can( $post_type_object->cap->create_posts ) );
$allowed_creation = ( ! empty( $this->linked_post_types[ $post_type ]['allow_creation'] ) && $this->linked_post_types[ $post_type ]['allow_creation'] );
/**
* Controls whether the UI to create new linked posts should be displayed.
*
* @since 4.5.7
*
* @param bool $enabled
* @param string $post_type
* @param Tribe__Events__Linked_Posts
*/
$creation_enabled = apply_filters( 'tribe_events_linked_posts_dropdown_enable_creation', $user_can_create && $allowed_creation, $post_type, $this );
// Get the label to use in placeholder attrs.
$label = $this->get_create_or_find_labels( $post_type, $creation_enabled );
if ( $linked_posts || $my_linked_posts ) {
echo '<input
type="hidden"
class="tribe-dropdown linked-post-dropdown"
name="' . esc_attr( $name ) . '"
id="saved_' . esc_attr( $post_type ) . '"
data-placeholder="' . $label . '"
data-search-placeholder="' . $label . '" ' .
( $creation_enabled ?
'data-freeform
data-sticky-search
data-create-choice-template="' . __( 'Create: <b><%= term %></b>', 'the-events-calendar' ) . '" data-allow-html ' : '' ) .
'data-options="' . esc_attr( json_encode( $data ) ) . '"' .
( empty( $current ) ? '' : ' value="' . esc_attr( $current ) . '"' ) .
'>';
} else {
echo '<p class="nosaved">' . sprintf( esc_attr__( 'No saved %s exists.', 'the-events-calendar' ), $singular_name_lowercase ) . '</p>';
printf( '<input type="hidden" name="%s" value="%d"/>', esc_attr( $name ), 0 );
}
}