'ninetytwo_notify_unsubscribe', 'token' => rawurlencode( $token ), ), admin_url( 'admin-post.php' ) ); return apply_filters( 'ninetytwo_notify_unsubscribe_url', $url, $token ); } /** * Handles a click on an unsubscribe link. Marks the matching subscriber * inactive (status = 0) rather than deleting the row, consistent with how * `status` is used to control notification eligibility elsewhere. */ function ninetytwo_notify_handle_unsubscribe_request() { global $wpdb; $table_name = $wpdb->prefix . 'ninetytwo_notify_subscribers'; // The unsubscribe token itself is the authentication mechanism for this // public, unauthenticated link (sent only inside notification emails), so // a WordPress nonce — which is tied to a signed-in session and expires in // ~24h — isn't the applicable protection here. // phpcs:ignore WordPress.Security.NonceVerification.Recommended $token = isset( $_GET['token'] ) ? sanitize_text_field( wp_unslash( $_GET['token'] ) ) : ''; if ( empty( $token ) ) { ninetytwo_notify_render_unsubscribe_notice( false ); } $subscriber = $wpdb->get_row( $wpdb->prepare( 'SELECT id, email FROM %i WHERE unsubscribe_token = %s', $table_name, $token ), ARRAY_A ); if ( ! $subscriber ) { ninetytwo_notify_render_unsubscribe_notice( false ); } $wpdb->update( $table_name, array( 'status' => 0 ), array( 'id' => $subscriber['id'] ), array( '%d' ), array( '%d' ) ); ninetytwo_notify_render_unsubscribe_notice( true ); } add_action( 'admin_post_nopriv_ninetytwo_notify_unsubscribe', 'ninetytwo_notify_handle_unsubscribe_request' ); add_action( 'admin_post_ninetytwo_notify_unsubscribe', 'ninetytwo_notify_handle_unsubscribe_request' ); /** * Prints a minimal, branded confirmation page and stops execution. * * @param bool $success Whether the unsubscribe request was valid. */ function ninetytwo_notify_render_unsubscribe_notice( $success ) { $store_name = get_bloginfo( 'name' ); if ( $success ) { $message = sprintf( /* translators: %s: store name */ esc_html__( 'You have been unsubscribed from product availability emails from %s.', '92-notify-for-woocommerce' ), esc_html( $store_name ) ); $title = __( 'Unsubscribed', '92-notify-for-woocommerce' ); } else { $message = esc_html__( 'This unsubscribe link is invalid or has already been used.', '92-notify-for-woocommerce' ); $title = __( 'Invalid Link', '92-notify-for-woocommerce' ); } wp_die( wp_kses_post( '
' . $message . '
' . esc_html__( 'Return to site', '92-notify-for-woocommerce' ) . '
' ), esc_html( $title ), array( 'response' => $success ? 200 : 400 ) ); }