Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Show save button.
/**
* Show save button.
*
* @since 1.0.0
*/
public function show_save_button( $item_id = '' ) {
// Get our item ID
if ( empty( $item_id ) ) {
$item_id = get_queried_object_id();
}
// Get text for the button states
$options = get_option( $this->plugin_name . '-settings' );
$item_save_text = $options['text-save'];
$item_unsave_text = $options['text-unsave'];
// Get membership status option
$status = $this->get_user_status();
// Check if the user is logged in or not, and then check if the item is saved or not
if ( is_user_logged_in() ) {
// Get our saved items
$saved_items = get_user_meta( get_current_user_id(), 'toptal_saved_items', true );
// If nothing is saved make $saved_items variable an array
if ( empty( $saved_items ) ) {
$saved_items = array();
}
// Check if the current item is saved or not
if ( in_array( $item_id, $saved_items ) ) {
$is_saved = true;
} else {
$is_saved = false;
}
} else {
// Get our saved items
$saved_items = $this->toptal_get_cookie( $this->get_unique_cookie_name() );
// Check if the current item is saved or not
if ( in_array( $item_id, $saved_items ) ) {
$is_saved = true;
} else {
$is_saved = false;
}
}
if ( $status == 1 && is_user_logged_in() && is_singular() || $status == 0 && is_singular() ) {
// Depending on the item status (saved or not), display different things.
if ( $is_saved == false ) {
return '<a href="#" class="toptal-save-button" data-nonce="' . wp_create_nonce( 'toptal_save_nonce' ) . '" data-item-id="' . esc_attr( $item_id ) . '"><span class="toptal-save-icon"></span><span class="toptal-save-text">' . esc_html( $item_save_text ) . '</span></a>';
} else {
return '<a href="#" class="toptal-save-button saved" data-nonce="' . wp_create_nonce( 'toptal_save_nonce' ) . '" data-item-id="' . esc_attr( $item_id ) . '"><span class="toptal-save-icon"></span><span class="toptal-save-text">' . esc_html( $item_unsave_text ) . '</span></a>';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment