From 262582373468667d4572208136daaf3071450bcb Mon Sep 17 00:00:00 2001 From: Gerhard Potgieter Date: Thu, 16 Aug 2018 14:09:39 +0200 Subject: [PATCH] Add fallback to wp.org plugin info check if the changelog check fails, this is for free plugins hosted on .org --- ...class-wc-rest-system-status-controller.php | 62 ++++++++++++++----- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/includes/api/class-wc-rest-system-status-controller.php b/includes/api/class-wc-rest-system-status-controller.php index 7ab23db7ae9..c828ff4248e 100644 --- a/includes/api/class-wc-rest-system-status-controller.php +++ b/includes/api/class-wc-rest-system-status-controller.php @@ -796,21 +796,53 @@ class WC_REST_System_Status_Controller extends WC_REST_Controller { $version_data = get_transient( md5( $plugin ) . '_version_data' ); if ( false === $version_data ) { $changelog = wp_safe_remote_get( 'http://dzv365zjfbd8v.cloudfront.net/changelogs/' . $dirname . '/changelog.txt' ); - $cl_lines = explode( "\n", wp_remote_retrieve_body( $changelog ) ); - if ( ! empty( $cl_lines ) ) { - foreach ( $cl_lines as $line_num => $cl_line ) { - if ( preg_match( '/^[0-9]/', $cl_line ) ) { - $date = str_replace( '.', '-', trim( substr( $cl_line, 0, strpos( $cl_line, '-' ) ) ) ); - $version = preg_replace( '~[^0-9,.]~', '', stristr( $cl_line, 'version' ) ); - $update = trim( str_replace( '*', '', $cl_lines[ $line_num + 1 ] ) ); - $version_data = array( - 'date' => $date, - 'version' => $version, - 'update' => $update, - 'changelog' => $changelog, - ); - set_transient( md5( $plugin ) . '_version_data', $version_data, DAY_IN_SECONDS ); - break; + if ( '200' === wp_remote_retrieve_response_code( $changelog ) ) { + $cl_lines = explode( "\n", wp_remote_retrieve_body( $changelog ) ); + if ( ! empty( $cl_lines ) ) { + foreach ( $cl_lines as $line_num => $cl_line ) { + if ( preg_match( '/^[0-9]/', $cl_line ) ) { + $date = str_replace( '.', '-', trim( substr( $cl_line, 0, strpos( $cl_line, '-' ) ) ) ); + $version = preg_replace( '~[^0-9,.]~', '', stristr( $cl_line, 'version' ) ); + $update = trim( str_replace( '*', '', $cl_lines[ $line_num + 1 ] ) ); + $version_data = array( + 'date' => $date, + 'version' => $version, + 'update' => $update, + 'changelog' => $changelog, + ); + set_transient( md5( $plugin ) . '_version_data', $version_data, DAY_IN_SECONDS ); + break; + } + } + } + } else { + $args = (object) array( + 'slug' => $dirname, + ); + $request = array( + 'action' => 'plugin_information', + 'request' => wp_json_encode( $args ), + ); + $plugin_info = wp_safe_remote_post( 'http://api.wordpress.org/plugins/info/1.0/', array( 'body' => $request ) ); + if ( '200' === wp_remote_retrieve_response_code( $plugin_info ) ) { + $body = wp_remote_retrieve_body( $plugin_info ); + $cl_lines = explode( "\n", $body->changelog ); + if ( ! empty( $cl_lines ) ) { + foreach ( $cl_lines as $line_num => $cl_line ) { + if ( preg_match( '/^[0-9]/', $cl_line ) ) { + $date = $body->last_updated; + $version = $body->version; + $update = trim( str_replace( '*', '', $cl_lines[ $line_num + 1 ] ) ); + $version_data = array( + 'date' => $date, + 'version' => $version, + 'update' => $update, + 'changelog' => $body->changelog, + ); + set_transient( md5( $plugin ) . '_version_data', $version_data, DAY_IN_SECONDS ); + break; + } + } } } }