This site is currently private.

'; } /** * Returns the default notification email subject. */ function lswitch_get_default_email_subject() { return sprintf( /* translators: %s: Site name */ __( 'Important message from %s', 'last-switch' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) ); } /** * Returns the default notification email body with auto-filled site URL and passcode. */ function lswitch_get_default_email_body() { $passcode = (string) get_option( 'lswitch_passcode', '' ); $pass_display = ! empty( $passcode ) ? $passcode : __( '[Your Unlock Passcode]', 'last-switch' ); $lines = array( __( 'Hello,', 'last-switch' ), __( 'You are receiving this automated message because I have not logged into my website for the configured period, and I chose you as someone I deeply trust.', 'last-switch' ), __( 'Thank you so much for everything you have meant to me and for all the kindness you have shown along the way.', 'last-switch' ), __( 'If you have not heard from me recently, please try reaching out to me first. If I am unreachable, please visit my website below, where I have left a final message and archive for you:', 'last-switch' ), sprintf( /* translators: %s: Site URL */ __( 'Website URL: %s', 'last-switch' ), home_url( '/' ) ), sprintf( /* translators: %s: Unlock passcode */ __( 'Unlock Passcode: %s', 'last-switch' ), $pass_display ), __( 'Please take good care of yourself, and thank you again for being part of my life.', 'last-switch' ), __( 'With gratitude,', 'last-switch' ), ); return implode( "\n\n", $lines ); } /** * Retrieves the site-specific secret salt. */ function lswitch_get_salt() { $salt = get_option( 'lswitch_secret_salt' ); if ( ! $salt ) { $salt = wp_generate_password( 64, true, true ); update_option( 'lswitch_secret_salt', $salt ); } return $salt; } /** * Retrieves the current plugin settings. */ function lswitch_get_settings() { $active_mode = (string) get_option( 'lswitch_active_mode', 'public' ); if ( ! in_array( $active_mode, array( 'public', 'private' ), true ) ) { $active_mode = 'public'; } $action = (string) get_option( 'lswitch_expiration_action', 'passcode' ); if ( ! in_array( $action, array( 'passcode', 'testament_only' ), true ) ) { $action = 'passcode'; } $status_code = (int) get_option( 'lswitch_status_code', 403 ); if ( ! in_array( $status_code, array( 200, 403, 404 ), true ) ) { $status_code = 403; } return array( 'active_mode' => $active_mode, 'days' => (int) get_option( 'lswitch_inactive_days', 10 ), 'action' => $action, 'testament_page_id' => absint( get_option( 'lswitch_testament_page_id', 0 ) ), 'passcode' => (string) get_option( 'lswitch_passcode', '' ), 'status_code' => $status_code, 'custom_html' => (string) get_option( 'lswitch_custom_html', lswitch_get_default_html() ), 'notify_email' => (string) get_option( 'lswitch_notify_email', '' ), 'notify_subject' => (string) get_option( 'lswitch_notify_subject', '' ), 'notify_body' => (string) get_option( 'lswitch_notify_body', '' ), ); } /** * Checks whether the configured administrator inactivity period has expired. */ function lswitch_is_expired() { $last_login = (int) get_option( 'lswitch_admin_last_login' ); if ( ! $last_login ) { $last_login = time(); update_option( 'lswitch_admin_last_login', $last_login ); } $settings = lswitch_get_settings(); $threshold_seconds = $settings['days'] * DAY_IN_SECONDS; return ( time() - $last_login ) > $threshold_seconds; } // ========================================================================= // 3. EMAIL NOTIFICATIONS // ========================================================================= /** * Sends the configured notification email once when the inactivity threshold expires. */ function lswitch_maybe_send_notification_email( $force_test = false ) { if ( ! $force_test ) { if ( ! lswitch_is_expired() ) { return false; } if ( (int) get_option( 'lswitch_email_sent', 0 ) === 1 ) { return false; } } $settings = lswitch_get_settings(); $raw_emails = explode( ',', $settings['notify_email'] ); $recipients = array(); foreach ( $raw_emails as $raw_email ) { $clean_email = sanitize_email( trim( $raw_email ) ); if ( ! empty( $clean_email ) && is_email( $clean_email ) ) { $recipients[] = $clean_email; } } if ( empty( $recipients ) ) { update_option( 'lswitch_last_mail_status', 'Error: Invalid or empty recipient email address.' ); return false; } $subject = ! empty( $settings['notify_subject'] ) ? $settings['notify_subject'] : lswitch_get_default_email_subject(); $body = ! empty( $settings['notify_body'] ) ? $settings['notify_body'] : lswitch_get_default_email_body(); if ( $force_test ) { $subject = '[TEST] ' . $subject; } if ( ! $force_test ) { update_option( 'lswitch_email_sent', 1 ); } $sent = wp_mail( $recipients, $subject, $body ); if ( $sent ) { update_option( 'lswitch_last_mail_status', 'Success: wp_mail() accepted the email at ' . wp_date( 'Y-m-d H:i:s' ) . ' to ' . implode( ', ', $recipients ) ); return true; } else { if ( ! $force_test ) { update_option( 'lswitch_email_sent', 0 ); } update_option( 'lswitch_last_mail_status', 'Failed: wp_mail() returned false at ' . wp_date( 'Y-m-d H:i:s' ) . '. Check server mail configuration or use an SMTP plugin.' ); return false; } } add_action( 'lswitch_check_inactivity_event', 'lswitch_maybe_send_notification_email' ); /** * Handles manual test email button submission from the settings page. */ function lswitch_handle_test_email_action() { if ( ! isset( $_POST['lswitch_send_test_email'], $_POST['lswitch_test_email_nonce'] ) ) { return; } if ( ! current_user_can( 'manage_options' ) ) { return; } if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['lswitch_test_email_nonce'] ) ), 'lswitch_test_email_action' ) ) { return; } $result = lswitch_maybe_send_notification_email( true ); $status = $result ? 'sent' : 'failed'; wp_safe_redirect( add_query_arg( array( 'page' => 'last-switch', 'lswitch_test_email' => $status, ), admin_url( 'options-general.php' ) ) ); exit; } add_action( 'admin_init', 'lswitch_handle_test_email_action' ); // ========================================================================= // 4. ADMIN SETTINGS REGISTRATION & UI // ========================================================================= /** * Sanitizes the custom HTML input. */ function lswitch_sanitize_custom_html( $html ) { if ( ! current_user_can( 'unfiltered_html' ) ) { return wp_kses_post( $html ); } return (string) $html; } /** * Registers plugin settings with the WordPress Settings API. */ function lswitch_register_settings() { register_setting( 'lswitch_settings_group', 'lswitch_active_mode', array( 'type' => 'string', 'sanitize_callback' => function ( $val ) { return ( 'private' === $val ) ? 'private' : 'public'; }, 'default' => 'public', ) ); register_setting( 'lswitch_settings_group', 'lswitch_inactive_days', array( 'type' => 'integer', 'sanitize_callback' => 'absint', 'default' => 10, ) ); register_setting( 'lswitch_settings_group', 'lswitch_expiration_action', array( 'type' => 'string', 'sanitize_callback' => function ( $val ) { return ( 'testament_only' === $val ) ? 'testament_only' : 'passcode'; }, 'default' => 'passcode', ) ); register_setting( 'lswitch_settings_group', 'lswitch_testament_page_id', array( 'type' => 'integer', 'sanitize_callback' => 'absint', 'default' => 0, ) ); register_setting( 'lswitch_settings_group', 'lswitch_passcode', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => '', ) ); register_setting( 'lswitch_settings_group', 'lswitch_status_code', array( 'type' => 'integer', 'sanitize_callback' => function ( $val ) { $code = intval( $val ); return in_array( $code, array( 200, 403, 404 ), true ) ? $code : 403; }, 'default' => 403, ) ); register_setting( 'lswitch_settings_group', 'lswitch_custom_html', array( 'type' => 'string', 'sanitize_callback' => function ( $val ) { if ( is_string( $val ) && 0 === strpos( $val, 'B64:' ) ) { $decoded = base64_decode( substr( $val, 4 ), true ); $val = ( false !== $decoded ) ? $decoded : ''; } return lswitch_sanitize_custom_html( $val ); }, 'default' => lswitch_get_default_html(), ) ); register_setting( 'lswitch_settings_group', 'lswitch_notify_email', array( 'type' => 'string', 'sanitize_callback' => function ( $val ) { $parts = explode( ',', (string) $val ); $cleaned = array(); foreach ( $parts as $part ) { $email = sanitize_email( trim( $part ) ); if ( ! empty( $email ) ) { $cleaned[] = $email; } } return implode( ', ', $cleaned ); }, 'default' => '', ) ); register_setting( 'lswitch_settings_group', 'lswitch_notify_subject', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => '', ) ); register_setting( 'lswitch_settings_group', 'lswitch_notify_body', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_textarea_field', 'default' => '', ) ); } add_action( 'admin_init', 'lswitch_register_settings' ); /** * Adds the plugin settings page to the admin menu. */ function lswitch_add_admin_menu() { add_options_page( __( 'Last Switch Settings', 'last-switch' ), __( 'Last Switch', 'last-switch' ), 'manage_options', 'last-switch', 'lswitch_render_settings_page' ); } add_action( 'admin_menu', 'lswitch_add_admin_menu' ); /** * Enqueues inline JavaScript for the Last Switch settings page. * * @param string $hook_suffix Current admin page hook suffix. */ function lswitch_enqueue_admin_scripts( $hook_suffix ) { if ( 'settings_page_last-switch' !== $hook_suffix ) { return; } wp_register_script( 'lswitch-admin', false, array(), '1.0.0', true ); wp_enqueue_script( 'lswitch-admin' ); $inline_js = <<<'JS' (function() { var modeRadios = document.querySelectorAll('input[name="lswitch_active_mode"]'); var privateRows = document.querySelectorAll('.lswitch-private-only-row'); var actionRadios = document.querySelectorAll('input[name="lswitch_expiration_action"]'); var passRow = document.getElementById('lswitch-passcode-row'); function updateVisibility() { var selectedMode = document.querySelector('input[name="lswitch_active_mode"]:checked'); if (selectedMode) { var isPublic = (selectedMode.value === 'public'); privateRows.forEach(function(row) { row.style.opacity = isPublic ? '0.45' : '1'; }); } var selectedAction = document.querySelector('input[name="lswitch_expiration_action"]:checked'); if (selectedAction && passRow) { passRow.style.opacity = (selectedAction.value === 'testament_only') ? '0.45' : '1'; } } modeRadios.forEach(function(r) { r.addEventListener('change', updateVisibility); }); actionRadios.forEach(function(r) { r.addEventListener('change', updateVisibility); }); updateVisibility(); var form = document.getElementById('lswitch-settings-form'); if (form) { form.addEventListener('submit', function() { var rawHtml = document.getElementById('lswitch_custom_html').value; var bytes = new TextEncoder().encode(rawHtml); var binString = Array.from(bytes, function(byte) { return String.fromCodePoint(byte); }).join(''); document.getElementById('lswitch_custom_html_hidden').value = 'B64:' + btoa(binString); }); } })(); JS; wp_add_inline_script( 'lswitch-admin', $inline_js ); } add_action( 'admin_enqueue_scripts', 'lswitch_enqueue_admin_scripts' ); /** * Renders the plugin settings page. */ function lswitch_render_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $settings = lswitch_get_settings(); $last_login = get_option( 'lswitch_admin_last_login' ); $email_sent = (int) get_option( 'lswitch_email_sent', 0 ); $last_mail_status = (string) get_option( 'lswitch_last_mail_status', '' ); $display_subject = ! empty( $settings['notify_subject'] ) ? $settings['notify_subject'] : lswitch_get_default_email_subject(); $display_body = ! empty( $settings['notify_body'] ) ? $settings['notify_body'] : lswitch_get_default_email_body(); // Retrieve all pages including published, private, and draft statuses. $pages = get_pages( array( 'post_status' => array( 'publish', 'private', 'draft' ), 'sort_column' => 'post_title', 'sort_order' => 'ASC', ) ); // Retrieve the URL of the bundled explanatory diagram image. $image_url = plugins_url( 'assets/how-it-works.webp', __FILE__ ); ?>

Last Switch - How It Works





$rest_status ) ); } add_filter( 'rest_authentication_errors', 'lswitch_block_rest_api' ); /** * Renders the full-screen testament page and optional passcode unlock form. */ function lswitch_render_testament_screen( $settings, $show_passcode_form ) { $testament_post = null; if ( $settings['testament_page_id'] > 0 ) { $candidate = get_post( $settings['testament_page_id'] ); if ( $candidate && 'page' === $candidate->post_type && 'trash' !== $candidate->post_status ) { $testament_post = $candidate; } } if ( $testament_post ) { status_header( 200 ); } else { status_header( $settings['status_code'] ); } nocache_headers(); header( 'Content-Type: text/html; charset=utf-8' ); $has_fixed_bar = ( $testament_post && $show_passcode_form ); wp_enqueue_style( 'wp-block-library' ); ?> style="color-scheme: dark;"> <?php if ( $testament_post && ! empty( $testament_post->post_title ) ) { echo esc_html( $testament_post->post_title . ' | ' . get_bloginfo( 'name' ) ); } else { echo esc_html( get_bloginfo( 'name' ) ); } ?>
post_title ) ) : ?>

post_title ); ?>

post_content ); $rendered_content = wptexturize( $rendered_content ); $rendered_content = wpautop( $rendered_content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Rendered content from trusted admin's selected testament page. echo $rendered_content; ?>