Skip to content

Instantly share code, notes, and snippets.

@BrianHenryIE
Created January 17, 2020 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrianHenryIE/520724d8bc69aa058c1af89e16e8bf51 to your computer and use it in GitHub Desktop.
Save BrianHenryIE/520724d8bc69aa058c1af89e16e8bf51 to your computer and use it in GitHub Desktop.
<?php
class Maintenance_Redirect {
/**
* Exclude the Push Notifications for WordPress registration endpoint from redirection.
*
* @hooked wpjf3_matches
*
* @param array $wpjf3_matches List of reasons to disable redirection (not actually used anywhere, but array cannot be empty).
*
* @return array
*/
public function exclude_pnfw( $wpjf3_matches ) {
if ( ! isset( $_SERVER['REQUEST_URI'] ) ) {
return $wpjf3_matches;
}
$request_uri = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) );
if ( stristr( $request_uri, 'pnfw/register' ) ) {
$wpjf3_matches[] = '<!-- WPJF_MR: pnfw/register -->';
}
return $wpjf3_matches;
}
/**
* If a site visitor is outside the USA, do not redirect.
*
* @hooked wpjf3_matches
*
* @param array $wpjf3_matches List of reasons to disable redirection (non-empty array means DO NOT redirect, contents unused).
*
* @return array
*/
public function disable_redirect_outside_usa( $wpjf3_matches ) {
if ( function_exists( 'geoip_detect2_get_info_from_current_ip' ) ) {
$ip_info = geoip_detect2_get_info_from_current_ip();
if ( 'US' !== $ip_info->country ) {
$wpjf3_matches[] = '<!-- WPJF_MR: User outside USA -->';
}
}
return $wpjf3_matches;
}
/**
* If Maintenance Redirect plugin is not installed, add an admin notice with an install link.
* If it is installed but the plugin is not active, add an admin notice with an activate link.
* If it is active but not enabled, add an admin notice with a link to its settings page.
*
* @hooked admin_notices
*/
public function check_wpjf3_is_active() {
error_log( __FUNCTION__ );
$plugin = array(
'name' => 'Maintenance Redirect',
'slug' => 'jf3-maintenance-mode',
'path' => 'jf3-maintenance-mode/wpjf3_maintenance_redirect.php',
);
if ( ! file_exists( WP_PLUGIN_DIR . '/' . $plugin['path'] ) ) {
$css_class = 'notice notice-warning';
if ( ! isset( $plugin['url'] ) ) {
$message = 'Required plugin <b><i>' . $plugin['name'] . '</i></b> is missing. ';
} else {
$action = 'install-plugin';
$slug = $plugin['slug'];
$install_url = wp_nonce_url(
add_query_arg(
array(
'action' => $action,
'plugin' => $slug,
),
admin_url( 'update.php' )
),
$action . '_' . $slug
);
$message = 'Required plugin <i><a href="' . $plugin['url'] . '">' . $plugin['name'] . '</a></i> is missing. ';
$message .= '<b><a href="' . $install_url . '">Install now</a></b>.';
}
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $css_class ), $message );
} elseif ( is_plugin_inactive( $plugin['path'] ) ) {
$css_class = 'notice notice-warning';
$activate_url = wp_nonce_url( admin_url( 'plugins.php?action=activate&plugin=' . $plugin['path'] ), 'activate-plugin_' . $plugin['path'] );
if ( ! isset( $plugin['url'] ) ) {
$message = 'Required plugin <i>' . $plugin['name'] . '</i> is inactive. <b><a href="' . $activate_url . '">Activate Now</a></b>.';
} else {
$message = 'Required plugin <i><a href="' . $plugin['url'] . '">' . $plugin['name'] . '</a></i> is inactive. <a href="' . $activate_url . '">Activate Now</a>.';
}
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $css_class ), $message );
} elseif ( isset( $GLOBALS['my_wpjf3_maintenance_redirect'] ) ) {
/** @var \wpjf3_maintenance_redirect $my_wpjf3_maintenance_redirect */
$my_wpjf3_maintenance_redirect = $GLOBALS['my_wpjf3_maintenance_redirect'];
$wpjf3_mr_options = $my_wpjf3_maintenance_redirect->get_admin_options();
if ( $wpjf3_mr_options['enable_redirect'] !== 'YES' ) {
$css_class = 'notice notice-warning';
$settings_url = '/wp-admin/options-general.php?page=JF3_Maint_Redirect';
$message = 'Required plugin <i>Maintenance Redirect</i> is not enabled. <b><a href="' . $settings_url . '">Configure Now</a></b>.';
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $css_class ), $message );
}
}
}
/**
* If GeoIP Detection is not installed, add an admin notice with an install link.
* If it's installed but not active, add an admin notice with an activate link.
*
* @hooked admin_notices
*/
public function check_geoip_detect_is_active() {
$plugin = array(
'name' => 'GeoIP Detection',
'slug' => 'geoip-detect',
'path' => 'geoip-detect/geoip-detect.php',
);
if ( ! file_exists( WP_PLUGIN_DIR . '/' . $plugin['path'] ) ) {
$css_class = 'notice notice-warning';
if ( ! isset( $plugin['url'] ) ) {
$message = 'Required plugin <b><i>' . $plugin['name'] . '</i></b> is missing. ';
} else {
$action = 'install-plugin';
$slug = $plugin['slug'];
$install_url = wp_nonce_url(
add_query_arg(
array(
'action' => $action,
'plugin' => $slug,
),
admin_url( 'update.php' )
),
$action . '_' . $slug
);
$message = 'Required plugin <i><a href="' . $plugin['url'] . '">' . $plugin['name'] . '</a></i> is missing. ';
$message .= '<b><a href="' . $install_url . '">Install now</a></b>.';
}
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $css_class ), $message );
} elseif ( is_plugin_inactive( $plugin['path'] ) ) {
$css_class = 'notice notice-warning';
$activate_url = wp_nonce_url( admin_url( 'plugins.php?action=activate&plugin=' . $plugin['path'] ), 'activate-plugin_' . $plugin['path'] );
if ( ! isset( $plugin['url'] ) ) {
$message = 'Required plugin <i>' . $plugin['name'] . '</i> is inactive. <b><a href="' . $activate_url . '">Activate Now</a></b>.';
} else {
$message = 'Required plugin <i><a href="' . $plugin['url'] . '">' . $plugin['name'] . '</a></i> is inactive. <a href="' . $activate_url . '">Activate Now</a>.';
}
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $css_class ), $message );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment