get_bookmarks( $user_id ); // Normalize bookmark. // Back-compat: older bookmarks only stored question/answer. $type = isset( $bookmark['type'] ) ? sanitize_key( $bookmark['type'] ) : ''; if ( '' === $type ) { $type = ( ! empty( $bookmark['answer'] ) || ! empty( $bookmark['question'] ) ) ? 'answer' : 'link'; } $new = array( 'ts' => isset( $bookmark['ts'] ) ? absint( $bookmark['ts'] ) : time(), 'type' => $type, 'screen_id' => isset( $bookmark['screen_id'] ) ? sanitize_text_field( $bookmark['screen_id'] ) : '', // Answer-type bookmark fields. 'question' => isset( $bookmark['question'] ) ? sanitize_textarea_field( $bookmark['question'] ) : '', 'answer' => isset( $bookmark['answer'] ) ? wp_kses_post( $bookmark['answer'] ) : '', // Generic link bookmark fields. 'title' => isset( $bookmark['title'] ) ? sanitize_text_field( $bookmark['title'] ) : '', 'url' => isset( $bookmark['url'] ) ? esc_url_raw( $bookmark['url'] ) : '', ); // If an answer bookmark doesn't have an explicit title, use question. if ( 'answer' === $type && '' === $new['title'] && '' !== $new['question'] ) { $new['title'] = wp_trim_words( $new['question'], 12, '…' ); } // Prepend newest. array_unshift( $items, $new ); // Cap stored bookmarks. if ( count( $items ) > 50 ) { $items = array_slice( $items, 0, 50 ); } update_user_meta( $user_id, self::USERMETA_KEY, $items ); return true; } public function delete_bookmark( $user_id, $index ) { $user_id = (int) $user_id; $index = absint( $index ); $items = $this->get_bookmarks( $user_id ); if ( ! isset( $items[ $index ] ) ) { return false; } unset( $items[ $index ] ); $items = array_values( $items ); update_user_meta( $user_id, self::USERMETA_KEY, $items ); return true; } }