Created
June 21, 2017 00:54
-
-
Save bekarice/7a61a09eb6a52f02ab29c9ec2d426cce to your computer and use it in GitHub Desktop.
Provides a plugin to test whether a server will allow background job processing or not for WooComm import / export plugins.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Background Processing Test for Import / Export | |
* Plugin URI: http://skyverge.com/ | |
* Description: Tests whether or not the server can connect to itself to process jobs asynchronously. | |
* Author: SkyVerge | |
* Author URI: http://www.skyverge.com/ | |
* Version: 1.0.0 | |
* Text Domain: sv-background | |
* | |
* Copyright: (c) 2017 SkyVerge, Inc. (info@skyverge.com) | |
* | |
* License: GNU General Public License v3.0 | |
* License URI: http://www.gnu.org/licenses/gpl-3.0.html | |
* | |
* @package Background-Processing-Test | |
* @author SkyVerge | |
* @category Admin | |
* @copyright Copyright (c) 2017, SkyVerge, Inc. | |
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 | |
*/ | |
defined( 'ABSPATH' ) or exit; | |
// fire it up! | |
add_action( 'plugins_loaded', 'sv_background_processing_test' ); | |
/** | |
* Main plugin class | |
* | |
* @since 1.0.0 | |
*/ | |
class SV_Background_Processing_Test { | |
/** @var SV_Background_Processing_Test single instance of this plugin */ | |
protected static $instance; | |
/** @var string $message the message based on the result of the tool */ | |
protected $message; | |
/** | |
* Initialize the class. | |
* | |
* @since 1.0.0 | |
*/ | |
public function __construct() { | |
// add test content for the request | |
add_action( 'init', array( $this, 'init' ), 25 ); | |
if ( is_admin() ) { | |
// add plugin links | |
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_plugin_links' ) ); | |
// add zeh tool! | |
add_filter( 'woocommerce_debug_tools', array( $this, 'add_debug_tool' ) ); | |
// translate the success message | |
add_filter( 'gettext', array( $this, 'translate_success_message' ), 10, 3 ); | |
} | |
} | |
/** Plugin methods ***************************************/ | |
/** | |
* Initialize the test | |
* | |
* @since 1.0.0 | |
*/ | |
public function init() { | |
// super-simplistic test response for checking whether loopback connections are enabled on server | |
if ( ! empty( $_REQUEST['sv_wc_test_loopback_connections'] ) ) { | |
echo '[TEST_LOOPBACK]'; | |
exit; | |
} | |
} | |
/** | |
* Adds loopback connection testing tool to WC system status -> tools page. | |
* | |
* @since 1.0.0 | |
* | |
* @param array $tools system status tools | |
* @return array | |
*/ | |
public function add_debug_tool( $tools ) { | |
$tools['sv_wc_test_loopback_request'] = array( | |
'name' => __( 'Background Processing Test', 'sv-background' ), | |
'button' => __( 'Run Test', 'sv-background' ), | |
'desc' => __( 'This tool will test whether your server is capable of processing background jobs for WooCommerce imports or exports.', 'sv-background' ), | |
'callback' => array( $this, 'test_loopback_connection' ), | |
); | |
return $tools; | |
} | |
/** | |
* Tests whether loopback connections work by firing a GET request to the server itself | |
* | |
* @since 1.0.0 | |
* | |
* @return bool whether the request was successful or not | |
*/ | |
function test_loopback_connection() { | |
$url = add_query_arg( 'sv_wc_test_loopback_connections', 1, home_url( '/' ) ); | |
$result = wp_remote_get( $url ); | |
$success = is_array( $result ) && ! empty( $result['body'] ) && '[TEST_LOOPBACK]' === $result['body']; | |
if ( ! $success && is_admin() ) { | |
$this->message = esc_html__( 'Could not connect. Please ask your hosting company to ensure your server has loopback connections enabled.', 'sv-background' ); | |
} elseif ( $success && is_admin() ) { | |
$this->message = esc_html__( 'Success! You should be able to process background jobs.', 'sv-background' ); | |
} | |
return $success; | |
} | |
/** | |
* Translate the tool success message. | |
* | |
* This could possibly be removed in favor of returning the message string in `test_loopback_connection()` | |
* when WC 3.1 is common. | |
* | |
* @since 1.0.0 | |
* | |
* @param string $translated the text to output | |
* @param string $original the original text | |
* @param string $domain the textdomain | |
* @return string updated text | |
*/ | |
public function translate_success_message( $translated, $original, $domain ) { | |
if ( 'woocommerce' === $domain && ( 'Tool ran.' === $original || 'There was an error calling %s' === $original ) ) { | |
$translated = $this->message; | |
} | |
return $translated; | |
} | |
/** Helper methods ***************************************/ | |
/** | |
* Main SV_Background_Processing_Test Instance, ensures only one instance is/can be loaded | |
* | |
* @since 1.0.0 | |
* @see sv_background_processing_test() | |
* @return SV_Background_Processing_Test | |
*/ | |
public static function instance() { | |
if ( is_null( self::$instance ) ) { | |
self::$instance = new self(); | |
} | |
return self::$instance; | |
} | |
/** | |
* Adds plugin page links | |
* | |
* @since 1.0.0 | |
* @param array $links all plugin links | |
* @return array $links all plugin links + our custom links (i.e., "Settings") | |
*/ | |
public function add_plugin_links( $links ) { | |
$plugin_links = array( | |
'<a href="' . admin_url( 'admin.php?page=wc-status&tab=tools' ) . '">' . __( 'View Tools', 'sv-background' ) . '</a>', | |
); | |
return array_merge( $plugin_links, $links ); | |
} | |
} | |
/** | |
* Returns the One True Instance of SV_Background_Processing_Test | |
* | |
* @since 1.0.0 | |
* @return SV_Background_Processing_Test | |
*/ | |
function sv_background_processing_test() { | |
return SV_Background_Processing_Test::instance(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment