Skip to content

Instantly share code, notes, and snippets.

@Ratko-Solaja
Created December 3, 2016 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ratko-Solaja/76f0e2619a3d6cdee094764b748e8958 to your computer and use it in GitHub Desktop.
Save Ratko-Solaja/76f0e2619a3d6cdee094764b748e8958 to your computer and use it in GitHub Desktop.
Create Shortcode for displaying all saved items.
/**
* 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';
}
$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' ) {
$html_to_return .= '<div class="toptal-saved-dp-table-cell cell-thumbnail">';
$html_to_return .= '<a href="' . esc_url( get_permalink() ) . '">';
$item_image = wp_get_attachment_image( get_post_thumbnail_id( get_the_ID() ), 'thumbnail' );
$html_to_return .= $item_image;
$html_to_return .= '</a>';
$html_to_return .= '</div>';
}
$html_to_return .= '<div class="toptal-saved-dp-table-cell cell-content">';
$html_to_return .= '<div class="toptal-saved-item-title">';
$html_to_return .= '<a href="' . esc_url( get_permalink() ) . '">';
$html_to_return .= get_the_title();
$html_to_return .= '</a>';
$html_to_return .= '</div>';
if ( has_excerpt() ) {
$html_to_return .= '<div class="toptal-saved-item-excerpt">' . get_the_excerpt() . '</div>';
}
$html_to_return .= '</div>';
$html_to_return .= '</div>';
$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