Browse: Home / Snippets /

Notifications: Overwrite default front-end UI

Contents


Snippet #

add_action( 'plugins_loaded', function() {
	remove_filter( 'learndash_notifications_email_content', [ 'LD_Notifications_Subscription_Manager', 'subscription_notice' ], 99 );

	remove_action( 'template_redirect', [ 'LD_Notifications_Subscription_Manager', 'redirect_to_login_page' ], 10 );

	remove_action( 'template_include', [ 'LD_Notifications_Subscription_Manager', 'subscription_page' ], 10 );

	remove_filter( 'document_title_parts', [ 'LD_Notifications_Subscription_Manager', 'subscription_page_title' ], 10 );

	remove_action( 'init', [ 'LD_Notifications_Subscription_Manager', 'save_subscriptions' ], 10 );
  
} );

add_filter( 'learndash_notifications_email_content', function( $content, $notification_id ) {
    $content .= "\r\n\r\n";

    $content .= '<p class="subscription-manager-notice" style="font-size: 12px; font-style: italic;">';
        $content .= sprintf( __( 'Don\'t want to receive this email anymore? <a href="%s">Click here</a> to manage your notification emails subscription.', 'learndash-notifications' ), home_url( '/notifications-subscription/' ) );
    $content .= '</p>';

    return $content;
}, 99, 2 );

add_action( 'template_redirect', function() {
    $query_vars = LD_Notifications_Subscription_Manager::get_query_vars();

    if ( ! is_user_logged_in() && array_search( 'notifications-subscription', $query_vars, true ) !== false ) {
        $login_page = wp_login_url( home_url( '/notifications-subscription/' ) );

        wp_safe_redirect( $login_page );
        exit();
    }
} );

add_filter( 'template_include', function( $template ) {
    $query_vars = LD_Notifications_Subscription_Manager::get_query_vars();

    if ( is_user_logged_in() && array_search( 'notifications-subscription', $query_vars, true ) !== false ) {
        http_response_code( 200 );
        ob_start();

        add_action( 'wp_footer', function() {
            ?>
            <script type="text/javascript">
                jQuery( document ).ready( function( $ ) {
                    $( '.select-all' ).on( 'click', function() {
                        if ( $( this ) .is( ':checked' ) ) {
                            $( 'input[type="checkbox"]' ).prop( 'checked', true );
                        } else {
                            $( 'input[type="checkbox"]' ).prop( 'checked', false );
                        }
                    } );
                });
            </script>
            <?php
        } );

        get_header();

        $triggers      = learndash_notifications_get_triggers();
        $subscriptions = get_user_meta( get_current_user_id(), 'learndash_notifications_subscription', true );
        ?>

        <style scoped="scoped">
            .primary {
                margin-bottom: 30px;
                max-width: 600px;
                margin: 0 auto;
            }

            h1 {
                text-align: center;
                margin-bottom: 40px;
            }

            .message {
                color: #fff;
                padding: 5px 15px;
                margin-bottom: 20px;
            }

            .message.success {
                background-color: green;
            }

            .message.fail {
                background-color: red;
            }

            .triggers {
                display: flex;
                flex-direction: column;
            }

            .item {
                display: flex;
                flex-direction: row;
            }

            .item.alternate {
                background-color: #f5f5f5;
            }

            .item.submit {
                margin-top: 20px;
            }

            .header {
                font-weight: bold;
            }

            .child {
                width: 80%;
                padding: 5px 10px;
            }

            .child.cb {
                width: 200px;
                text-align: center; 
            }

            @media screen {
                
            }
        </style>

        <main class="learndash-notifications primary">
            <h1><?php _e( 'Notifications Subscription', 'learndash-notifications' ) ?></h1>
            <?php
            if ( isset( $_GET['message'] ) ) {
                switch ( $_GET['message'] ) {
                    case 'success':
                        $class = 'success';
                        $message = __( 'Your notification settings has been successfully saved.', 'learndash-notifications' );
                        break;

                    case 'fail':
                        $class = 'fail';
                        $message = __( 'There is something wrong. Please try again later.', 'learndash-notifications' );
                        break;
                    
                    default:
                        $message = false;
                        break;
                }

                if ( $message ) {
                    ?>
                    <div class="message <?php echo esc_attr( $class ); ?>"><?php echo $message; ?></div>
                    <?php
                }
            }
            ?>
            <div class="triggers">
                <form action="" method="POST">
                    <input type="hidden" name="action" value="learndash_notifications_subscription">
                    <input type="hidden" name="user_id" value="<?php echo esc_attr( get_current_user_id() ) ?>">
                    <?php wp_nonce_field( 'learndash_notifications_subscription', 'ld_nonce' ) ?>
                    <div class="item">
                        <div class="header child"><?php _e( 'Triggers', 'learndash-notifications' ) ?></div>
                        <div class="header cb child">
                            <div>
                                <?php _e( 'Enabled', 'learndash-notifications' ) ?>
                            </div>
                            <div class="input">
                                <input type="checkbox" name="select-all" class="select-all" title="<?php esc_attr_e( 'Select All', 'learndash-notifications' ) ?>">
                            </div>
                        </div>
                    </div>
                    <?php $count = 0; ?>
                    <?php foreach ( $triggers as $key => $label ) : ?>
                        <?php $count++; ?>
                        <?php $checked = ! isset( $subscriptions[ $key ] ) || $subscriptions[ $key ] ? 'checked="checked"' : ''; ?>
                        <div class="item <?php if ( $count % 2 ) echo 'alternate'; ?>">
                            <div class="label child"><?php echo esc_textarea( $label ) ?></div>
                            <div class="cb child"><input type="checkbox" name="<?php echo esc_attr( $key ) ?>" value="1" <?php echo $checked ?>></div>
                        </div>
                    <?php endforeach; ?>
                    <div class="item submit">
                        <div class="child"></div>
                        <div class="child cb">
                            <input class="submit" type="submit" value="<?php esc_attr_e( 'Save Changes', 'learndash-notifications' ) ?>">
                        </div>
                    </div>
                </form>
            </div>
        </main>

        <?php
        get_footer();

        echo ob_get_clean();

        return '';
    } else {
        return $template;
    }
} );

add_filter( 'document_title_parts', function( $title ) {
    $query_vars = LD_Notifications_Subscription_Manager::get_query_vars();

    if ( array_search( 'notifications-subscription', $query_vars, true ) !== false ) {
        $title['title'] = __( 'Notifications Subscription' );
    }

    return $title;
} );

add_action( 'init', function() {
    if ( ! isset( $_POST['action'] ) || $_POST['action'] !== 'learndash_notifications_subscription' ) {
        return;
    }

    if ( ! wp_verify_nonce( $_POST['ld_nonce'], 'learndash_notifications_subscription' ) ) {
        return;
    }

    $user_id = intval( $_POST['user_id'] );

    if ( empty( $user_id ) ) {
        return;
    }

    $triggers = learndash_notifications_get_triggers();
    $subscriptions = [];
    foreach ( $triggers as $key => $label ) {
        if ( isset( $_POST[ $key ] ) && $_POST[ $key ] == 1 ) {
            $subscriptions[ $key ] = 1;
        } else {
            $subscriptions[ $key ] = 0;
        }
    }

    $update = update_user_meta( $user_id, 'learndash_notifications_subscription', $subscriptions );

    if ( $update ) {
        $redirect_url = add_query_arg( [ 'message' => 'success' ], home_url( 'notifications-subscription/' ) );
    } else {
        $redirect_url = add_query_arg( [ 'message' => 'fail' ], home_url( 'notifications-subscription/' ) );
    }

    wp_redirect( $redirect_url );
    exit();
} );