'off', // off | coming_soon | maintenance.
'bypass_roles' => array( 'administrator', 'editor' ),
'headline' => __( 'Coming soon', 'launchvik' ),
'message' => __( "We're working on something new. Check back soon.", 'launchvik' ),
'logo_url' => '',
'bg_image' => '', // Optional photo behind the holding page.
'bg_overlay' => 40, // How strongly bg_color covers the image, 0–90 (%).
'bg_color' => '#111827',
'text_color' => '#f9fafb',
'accent_color' => '#6366f1',
'launch_date' => '', // Y-m-d\TH:i in the site's timezone.
'show_countdown' => 0,
'auto_launch' => 0, // Turn the mode off automatically at the launch date.
'launch_email' => 1, // Email the site admin when auto-launch happens.
'show_login_link' => 1,
'retry_after_hours' => 1,
'block_rest' => 1,
'excluded_paths' => '',
);
// Add-ons (LaunchVik Pro) add their own settings here.
return apply_filters( 'launchvik_defaults', $defaults );
}
/**
* Current settings merged with defaults.
*/
function launchvik_get_settings() {
$saved = get_option( LAUNCHVIK_OPTION, array() );
return wp_parse_args( is_array( $saved ) ? $saved : array(), launchvik_defaults() );
}
/**
* Whether coming-soon or maintenance mode is on.
*/
function launchvik_is_active() {
$s = launchvik_get_settings();
return in_array( $s['mode'], array( 'coming_soon', 'maintenance' ), true );
}
/**
* All preview links, keyed by link id.
*/
function launchvik_get_links() {
$links = get_option( LAUNCHVIK_LINKS_OPTION, array() );
return is_array( $links ) ? $links : array();
}
/**
* Whether a link exists and has not expired.
*/
function launchvik_link_is_valid( $link ) {
return is_array( $link )
&& ! empty( $link['token'] )
&& ( empty( $link['expires'] ) || (int) $link['expires'] > time() );
}
/**
* Find a link id by its token. Returns '' when not found.
*/
function launchvik_find_link_id_by_token( $token ) {
if ( ! is_string( $token ) || '' === $token ) {
return '';
}
foreach ( launchvik_get_links() as $id => $link ) {
if ( ! empty( $link['token'] ) && hash_equals( (string) $link['token'], $token ) ) {
return (string) $id;
}
}
return '';
}
/**
* Shareable URL for a token.
*/
function launchvik_preview_url( $token ) {
return add_query_arg( LAUNCHVIK_QUERY_VAR, rawurlencode( $token ), home_url( '/' ) );
}
/**
* Signed cookie value: "|". Revoking the link invalidates it.
*/
function launchvik_cookie_value( $link_id, $token ) {
return $link_id . '|' . hash_hmac( 'sha256', $link_id . '|' . $token, wp_salt( 'auth' ) );
}
/**
* Launch date as a Unix timestamp (0 when unset or invalid).
*/
function launchvik_launch_timestamp( $settings ) {
if ( empty( $settings['launch_date'] ) ) {
return 0;
}
$dt = date_create_immutable_from_format( 'Y-m-d\TH:i', $settings['launch_date'], wp_timezone() );
return $dt ? $dt->getTimestamp() : 0;
}
/**
* Timestamp of a scheduled automatic launch, or 0 when none is pending.
*/
function launchvik_scheduled_launch( $settings = null ) {
$s = $settings ? $settings : launchvik_get_settings();
if ( empty( $s['auto_launch'] ) || ! in_array( $s['mode'], array( 'coming_soon', 'maintenance' ), true ) ) {
return 0;
}
return launchvik_launch_timestamp( $s );
}
/**
* Runs early on every request: once the launch date has passed, open the site.
* Checking on each request (instead of relying only on WP-Cron) means the very
* first visitor after the launch time already sees the live site.
*/
function launchvik_maybe_auto_launch() {
$s = launchvik_get_settings();
$ts = launchvik_scheduled_launch( $s );
if ( ! $ts || $ts > time() ) {
return;
}
// add_option() fails if the row exists, so only one request does the launch.
if ( ! add_option( 'launchvik_launch_lock', time(), '', false ) ) {
if ( (int) get_option( 'launchvik_launch_lock' ) < time() - MINUTE_IN_SECONDS ) {
delete_option( 'launchvik_launch_lock' ); // Clear a lock left by a crashed request.
}
return;
}
$previous_mode = $s['mode'];
$s['mode'] = 'off';
$s['auto_launch'] = 0; // So switching the mode back on later doesn't instantly relaunch.
update_option( LAUNCHVIK_OPTION, $s );
update_option(
'launchvik_auto_launched',
array(
'time' => time(),
'mode' => $previous_mode,
),
false
);
delete_option( 'launchvik_launch_lock' );
if ( ! empty( $s['launch_email'] ) ) {
$site = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
wp_mail(
get_option( 'admin_email' ),
/* translators: %s: site name */
sprintf( __( '[%s] Your site is now live', 'launchvik' ), $site ),
sprintf(
/* translators: 1: mode name, 2: date and time, 3: site URL */
__( "LaunchVik turned off %1\$s mode at %2\$s, as scheduled.\n\nYour site is now open to everyone:\n%3\$s", 'launchvik' ),
'maintenance' === $previous_mode ? __( 'Maintenance', 'launchvik' ) : __( 'Coming soon', 'launchvik' ),
wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ),
home_url( '/' )
)
);
}
do_action( 'launchvik_auto_launched', $previous_mode );
}
/**
* Version string for a plugin asset. Includes the file's modified time so
* browsers fetch the new file as soon as it changes.
*/
function launchvik_asset_version( $relative_path ) {
$file = LAUNCHVIK_DIR . $relative_path;
return LAUNCHVIK_VERSION . ( file_exists( $file ) ? '.' . filemtime( $file ) : '' );
}
/**
* Removes all plugin data when the plugin is deleted from the Plugins screen:
* settings, preview links and all client feedback. Runs on Freemius's
* after_uninstall action (Freemius asks plugins not to use uninstall.php).
*/
function launchvik_uninstall() {
// The free and Pro copies share their settings. If the other copy is still
// installed (say, the free one is deleted after upgrading), keep the data.
foreach ( array( 'launchvik/launchvik.php', 'launchvik-pro/launchvik.php' ) as $basename ) {
if ( plugin_basename( LAUNCHVIK_FILE ) !== $basename && file_exists( WP_PLUGIN_DIR . '/' . $basename ) ) {
return;
}
}
if ( is_multisite() ) {
foreach ( get_sites( array( 'fields' => 'ids' ) ) as $site_id ) {
switch_to_blog( $site_id );
launchvik_uninstall_site();
restore_current_blog();
}
} else {
launchvik_uninstall_site();
}
}
/**
* Removes the plugin's data from the current site.
*/
function launchvik_uninstall_site() {
global $wpdb;
foreach ( array( 'launchvik_settings', 'launchvik_preview_links', 'launchvik_auto_launched', 'launchvik_launch_lock' ) as $option ) {
delete_option( $option );
}
// Feedback comments (post type launchvik_feedback) and their meta.
// phpcs:disable WordPress.DB.DirectDatabaseQuery
$wpdb->query(
"DELETE pm FROM {$wpdb->postmeta} pm INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id WHERE p.post_type = 'launchvik_feedback'"
);
$wpdb->query( "DELETE FROM {$wpdb->posts} WHERE post_type = 'launchvik_feedback'" );
// phpcs:enable
}