Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Modular
/**
* Create Shortcode for displaying all saved items.
*
* @since 1.0.0
*/
public function register_saved_shortcode() {
// Get our options
$options = get_option( $this->plugin_name . '-settings' );
// Default Values
$post_types = array();
$saved_items = array();
$text_no_saved = '';
// Get our text when there are no saved items.
if ( ! empty( $options['text-no-saved'] ) ) {
$text_no_saved = $options['text-no-saved'];
}
// Check if user is logged in and get his saved items.
if ( is_user_logged_in() ) {
$saved_items = get_user_meta( get_current_user_id(), 'toptal_saved_items', true );
} else {
$saved_items = $this->toptal_get_cookie( $this->get_unique_cookie_name() );
}
// Wrap our content
$html_to_return = '<div class="toptal-saved-items">';
// Check if there are any saved post types as well as selected post types
if ( ! empty( $options['post-types'] ) && ! empty( $saved_items ) ) {
$post_types = $options['post-types'];
// Let's create our query
$saved_args = array(
'post_type' => $post_types,
'posts_per_page' => -1,
'post__in' => $saved_items
);
$saved_query = new WP_Query( $saved_args );
if ( $saved_query->have_posts() ) : while ( $saved_query->have_posts() ) : $saved_query->the_post();
// Our structure for individual saved items.
$html_to_return .= '<div id="toptal-saved-' . get_the_ID() . '" class="toptal-saved-item">';
// Show our save/unsave button.
$html_to_return .= $this->show_save_button( get_the_ID() );
// Check if this item has featured image, and defined class.
if ( has_post_thumbnail() ) {
$inner_class = 'toptal-saved-dp-table';
} else {
$inner_class = 'toptal-saved-dp-table toptal-saved-no-thumbnail';
}
$inner_html_to_return = '<div class="' . esc_attr( $inner_class ) . '">';
// Let's check if our class has featured image, and then show it.
if ( $inner_class == 'toptal-saved-dp-table' ) {
$inner_html_to_return .= '<div class="toptal-saved-dp-table-cell cell-thumbnail">';
$inner_html_to_return .= '<a href="' . esc_url( get_permalink() ) . '">';
$item_image = wp_get_attachment_image( get_post_thumbnail_id( get_the_ID() ), 'thumbnail' );
$inner_html_to_return .= $item_image;
$inner_html_to_return .= '</a>';
$inner_html_to_return .= '</div>';
}
$inner_html_to_return .= '<div class="toptal-saved-dp-table-cell cell-content">';
$inner_html_to_return .= '<div class="toptal-saved-item-title">';
$inner_html_to_return .= '<a href="' . esc_url( get_permalink() ) . '">';
$inner_html_to_return .= get_the_title();
$inner_html_to_return .= '</a>';
$inner_html_to_return .= '</div>';
if ( has_excerpt() ) {
$inner_html_to_return .= '<div class="toptal-saved-item-excerpt">' . get_the_excerpt() . '</div>';
}
$inner_html_to_return .= '</div>';
$inner_html_to_return .= '</div>';
$html_to_return .= apply_filters( 'toptal_saved_item_html', $inner_html_to_return );
$html_to_return .= '</div>';
endwhile; wp_reset_postdata(); endif;
} else {
$html_to_return .= '<div class="toptal-saved-nothing">' . esc_html( $text_no_saved ) . '</div>';
}
$html_to_return .= '</div>';
return $html_to_return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.