Skip to content

Instantly share code, notes, and snippets.

@joshco
Last active March 24, 2021 13:37
Show Gist options
  • Save joshco/089944027b2c04090978524fdc6b77c5 to your computer and use it in GitHub Desktop.
Save joshco/089944027b2c04090978524fdc6b77c5 to your computer and use it in GitHub Desktop.
TablePress AutoImport Patch

Overview

This patch adds:

  • a function to wrap the WP calls to get a URL based source and sets a 30sec timeout.
  • More detailed status information in the plugin's WP Dashboard pane on why an autoupdate failed.
  • Logging information, and a logging function, which outputs debug information when WP_DEBUG is true to help troubleshoot.

Failure case (with a timeout)

Debug Log

root@1f1feb9dd9b1:/web# wp --allow-root eval 'do_action("tablepress_table_auto_import_hook");'
TablePress: Importing Table ID 6 Type url Format csv Source http://foo.loopback.site/mysheet
TablePress: Couldn't retrieve http://foo.loopback.site/mysheet cURL error 28: Operation timed out
after 31001 milliseconds with 0 bytes received
TablePress: Set Status Message: <strong>Failed</strong> @ 2019-11-15 17:48:12 <em>cURL error 28:
Operation timed out after 31001 milliseconds with 0 bytes received</em>

WP Dashboard

image

Success Case

Debug Log

root@1f1feb9dd9b1:/web# wp --allow-root eval 'do_action("tablepress_table_auto_import_hook");'
TablePress: Importing Table ID 6 Type url Format csv Source http://foobar.site/mysheet
TablePress: Set Status Message: Success @ 2019-11-15 17:45:52

WP Dashboard

image

Index: wp-content/plugins/tablepress-table-auto-update/tablepress-table-auto-update.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- wp-content/plugins/tablepress-table-auto-update/tablepress-table-auto-update.php (date 1573826959000)
+++ wp-content/plugins/tablepress-table-auto-update/tablepress-table-auto-update.php (date 1573869984299)
@@ -75,6 +75,36 @@
return $schedules;
}
+ /**
+ * Log Function
+ *
+ * @param string|Object $log Message to log,
+ * @param string $prefix Optional prefix
+ */
+ public static function _log ( $log, $prefix="" ) {
+
+ $log_message=array("TablePress: ");
+
+ if ($prefix) {
+ array_push($log_message, $prefix . " ");
+ }
+
+ switch(true) {
+
+ case ( is_wp_error($log)):
+ array_push($log_message, $log->get_error_message());
+ break;
+
+ case ( is_array( $log ) || is_object( $log ) ):
+ array_push($log_message,( print_r( $log, true ) ));
+ break;
+
+ default:
+ array_push($log_message, $log);
+ }
+ error_log(join("",$log_message));
+ }
+
/**
* Every 15 minutes: Loop through the list of tables, import them from their given source,
* and replace the existing data with the new data.
@@ -112,12 +142,88 @@
continue;
}
+ self::_log(
+ join(" ",
+ array(
+ "Importing Table ID",
+ $table_id,
+ "Type",
+ $table_config['source_type'],
+ "Format",
+ $table_config['source_format'],
+ "Source",
+ $table_config['source']
+ )
+ )
+ );
+
$result = self::_import_table( $table_id, $table_config['source_type'], $table_config['source_format'], $table_config['source'] );
- $tables[ $table_id ]['last_auto_import'] = ( ( false !== $result ) ? 'Success' : '<strong>Failed</strong>' ). ' @ ' . current_time( 'mysql' );
+
+ switch(true) {
+ case ($result === false):
+ $success=false;
+ $reason="";
+ break;
+
+ case (is_wp_error($result)):
+ $success=false;
+ $reason=join("",
+ array(
+ "<em>",
+ $result->get_error_message(),
+ "</em>"
+ )
+ );
+ break;
+
+ default:
+ $success=true;
+ $reason="";
+
+ }
+ $import_message = join(" ",
+ array(
+ (false !== $success) ? 'Success' : '<strong>Failed</strong>',
+ "@",
+ current_time('mysql'),
+ $reason
+ )
+ );
+
+ $tables[ $table_id ]['last_auto_import'] = $import_message;
+ self::_log($import_message,"Set Status Message:");
+
}
$auto_import_config->update( $tables );
}
+ /**
+ * Get a URL based source's data
+ * Wraps WP functions to set timeout, and return WP_Error information
+ *
+ * @param string $uri
+ * @return bool|string|WP_Error
+ */
+ public static function _get_url_source( $uri ) {
+ $parsed_url = @parse_url( $uri );
+
+ if ( ! $parsed_url || ! is_array( $parsed_url ) ) {
+ return false;
+ }
+
+ $options = array();
+ $options['timeout'] = 30;
+
+ $response = wp_safe_remote_get( $uri, $options );
+
+ if ( is_wp_error( $response ) ) {
+ self::_log($response, "Couldn't retrieve " . $uri);
+ return $response;
+ }
+
+ return wp_remote_retrieve_body( $response );
+ }
+
/**
* Update a single table from the given source.
*
@@ -139,7 +245,7 @@
if ( 'http://' === $source ) {
return false;
}
- $import_data = wp_remote_fopen( $source );
+ $import_data = self::_get_url_source( $source );
break;
case 'server':
if ( ABSPATH === $source ) {
@@ -154,6 +260,10 @@
return false;
}
+ if ( is_wp_error($import_data)) {
+ return $import_data;
+ }
+
if ( empty( $import_data ) ) {
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment