From f35f54537c8119f4247a8f46d23f9e8526eea1a4 Mon Sep 17 00:00:00 2001 From: Bruce Atkinson Date: Wed, 2 Jan 2019 15:00:41 +0200 Subject: [PATCH 1/3] = 2.2.9 - December 29, 2018 = * Add auto-update feature --- README.md | 4 +- changelog.txt | 3 + paygate-tools.php | 2 +- paygate.php | 72 ++++++- paygate_gf_class.php | 2 +- updater.class.php | 491 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 568 insertions(+), 6 deletions(-) create mode 100755 changelog.txt mode change 100644 => 100755 paygate-tools.php mode change 100644 => 100755 paygate.php mode change 100644 => 100755 paygate_gf_class.php create mode 100755 updater.class.php diff --git a/README.md b/README.md index 522997b..7e122de 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ # PayWeb_Gravity_Forms -## DPO PayGate Gravity Forms plugin v2.2.8 for Gravity Forms v2.2.5.21, WordPress v4.9.2 +## DPO PayGate Gravity Forms plugin v2.2.9 for Gravity Forms v2.2.5.21, WordPress v4.9.2 This is the DPO PayGate PayWeb3 plugin for Gravity Forms. Please feel free to contact the DPO PayGate support team at support@paygate.co.za should you require any assistance. ## Installation -Please navigate to the [releases page](https://github.com/PayGate/PayWeb_Gravity_Forms/releases), download the latest release (v1.0.1) and unzip. You will them be able to follow the integration guide PDF which is included in the zip. +Please navigate to the [releases page](https://github.com/PayGate/PayWeb_Gravity_Forms/releases), download the latest release (v2.2.9) and unzip. You will them be able to follow the integration guide PDF which is included in the zip. ## Collaboration diff --git a/changelog.txt b/changelog.txt new file mode 100755 index 0000000..38d1b91 --- /dev/null +++ b/changelog.txt @@ -0,0 +1,3 @@ += 2.2.9 - December 29, 2018 = + +* Add auto-update feature \ No newline at end of file diff --git a/paygate-tools.php b/paygate-tools.php old mode 100644 new mode 100755 index e416a78..0b7b7f6 --- a/paygate-tools.php +++ b/paygate-tools.php @@ -3,7 +3,7 @@ * Copyright (c) 2018 PayGate (Pty) Ltd * * Author: App Inlet (Pty) Ltd - * + * * Released under the GNU General Public License */ class PayGate diff --git a/paygate.php b/paygate.php old mode 100644 new mode 100755 index 516f08b..7b98d3d --- a/paygate.php +++ b/paygate.php @@ -2,9 +2,9 @@ /* Plugin Name: Gravity Forms PayGate Add-On -Plugin URI: http://www.gravityforms.com +Plugin URI: https://github.com/PayGate/PayWeb_Gravity_Forms Description: Integrates Gravity Forms with PayGate, a South African payment gateway. -Version: 2.2.8 +Version: 2.2.9 Author: PayGate (Pty) Ltd Author URI: https://www.paygate.co.za/ Developer: App Inlet (Pty) Ltd @@ -49,6 +49,37 @@ public static function load() require_once 'paygate_gf_class.php'; GFAddOn::register( 'PayGateGF' ); + + /** + * Auto updates from GIT + * + * @since 2.2.9 + * + */ + + require_once 'updater.class.php'; + + if ( is_admin() ) { + // note the use of is_admin() to double check that this is happening in the admin + + $config = array( + 'slug' => plugin_basename( __FILE__ ), + 'proper_folder_name' => 'gravity-forms-paygate-plugin', + 'api_url' => 'https://api.github.com/repos/PayGate/PayWeb_Gravity_Forms', + 'raw_url' => 'https://raw.github.com/PayGate/PayWeb_Gravity_Forms/master', + 'github_url' => 'https://github.com/PayGate/PayWeb_Gravity_Forms', + 'zip_url' => 'https://github.com/PayGate/PayWeb_Gravity_Forms/archive/master.zip', + 'homepage' => 'https://github.com/PayGate/PayWeb_Gravity_Forms', + 'sslverify' => true, + 'requires' => '4.0', + 'tested' => '4.9.8', + 'readme' => 'README.md', + 'access_token' => '', + ); + + new WP_GitHub_Updater( $config ); + + } } } @@ -147,3 +178,40 @@ function GF_encryption( $string, $action = 'e' ) return $output; } + +add_action( 'plugins_loaded', 'plugin_auto_updater', 0 ); + +function plugin_auto_updater() +{ + /** + * Auto updates from GIT + * + * @since 2.2.9 + * + */ + + require_once 'updater.class.php'; + + if ( is_admin() ) { + // note the use of is_admin() to double check that this is happening in the admin + + $config = array( + 'slug' => plugin_basename( __FILE__ ), + 'proper_folder_name' => 'gravity-forms-paygate-plugin', + 'api_url' => 'https://api.github.com/repos/PayGate/PayWeb_Gravity_Forms', + 'raw_url' => 'https://raw.github.com/PayGate/PayWeb_Gravity_Forms/master', + 'github_url' => 'https://github.com/PayGate/PayWeb_Gravity_Forms', + 'zip_url' => 'https://github.com/PayGate/PayWeb_Gravity_Forms/archive/master.zip', + 'homepage' => 'https://github.com/PayGate/PayWeb_Gravity_Forms', + 'sslverify' => true, + 'requires' => '4.0', + 'tested' => '4.9.8', + 'readme' => 'README.md', + 'access_token' => '', + ); + + new WP_GitHub_Updater( $config ); + + } + +} diff --git a/paygate_gf_class.php b/paygate_gf_class.php old mode 100644 new mode 100755 index 98015e0..04a55d3 --- a/paygate_gf_class.php +++ b/paygate_gf_class.php @@ -3,7 +3,7 @@ * Copyright (c) 2018 PayGate (Pty) Ltd * * Author: App Inlet (Pty) Ltd - * + * * Released under the GNU General Public License */ add_action( 'parse_request', array( "PayGateGF", "notify_handler" ) ); diff --git a/updater.class.php b/updater.class.php new file mode 100755 index 0000000..b3bd16d --- /dev/null +++ b/updater.class.php @@ -0,0 +1,491 @@ + + * @link http://jkudish.com + * @package WP_GitHub_Updater + * @license http://www.gnu.org/copyleft/gpl.html GNU Public License + * @copyright Copyright (c) 2011-2013, Joachim Kudish + * + * GNU General Public License, Free Software Foundation + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +class WP_GitHub_Updater +{ + + /** + * GitHub Updater version + */ + const VERSION = 1.6; + + /** + * @var $config the config for the updater + * @access public + */ + public $config; + + /** + * @var $missing_config any config that is missing from the initialization of this instance + * @access public + */ + public $missing_config; + + /** + * @var $github_data temporiraly store the data fetched from GitHub, allows us to only load the data once per class instance + * @access private + */ + private $github_data; + + /** + * Class Constructor + * + * @since 1.0 + * @param array $config the configuration required for the updater to work + * @see has_minimum_config() + * @return void + */ + public function __construct( $config = array() ) + { + + $defaults = array( + 'slug' => plugin_basename( __FILE__ ), + 'proper_folder_name' => dirname( plugin_basename( __FILE__ ) ), + 'sslverify' => true, + 'access_token' => '', + ); + + $this->config = wp_parse_args( $config, $defaults ); + + // if the minimum config isn't set, issue a warning and bail + if ( !$this->has_minimum_config() ) { + $message = 'The GitHub Updater was initialized without the minimum required configuration, please check the config in your plugin. The following params are missing: '; + $message .= implode( ',', $this->missing_config ); + _doing_it_wrong( __CLASS__, $message, self::VERSION ); + return; + } + + $this->set_defaults(); + + add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'api_check' ) ); + + // Hook into the plugin details screen + add_filter( 'plugins_api', array( $this, 'get_plugin_info' ), 10, 3 ); + add_filter( 'upgrader_post_install', array( $this, 'upgrader_post_install' ), 10, 3 ); + + // set timeout + add_filter( 'http_request_timeout', array( $this, 'http_request_timeout' ) ); + + // set sslverify for zip download + add_filter( 'http_request_args', array( $this, 'http_request_sslverify' ), 10, 2 ); + } + + public function has_minimum_config() + { + + $this->missing_config = array(); + + $required_config_params = array( + 'api_url', + 'raw_url', + 'github_url', + 'zip_url', + 'requires', + 'tested', + 'readme', + ); + + foreach ( $required_config_params as $required_param ) { + if ( empty( $this->config[$required_param] ) ) { + $this->missing_config[] = $required_param; + } + + } + + return ( empty( $this->missing_config ) ); + } + + /** + * Check wether or not the transients need to be overruled and API needs to be called for every single page load + * + * @return bool overrule or not + */ + public function overrule_transients() + { + return ( defined( 'WP_GITHUB_FORCE_UPDATE' ) && WP_GITHUB_FORCE_UPDATE ); + } + + /** + * Set defaults + * + * @since 1.2 + * @return void + */ + public function set_defaults() + { + if ( !empty( $this->config['access_token'] ) ) { + + // See Downloading a zipball (private repo) https://help.github.com/articles/downloading-files-from-the-command-line + extract( parse_url( $this->config['zip_url'] ) ); // $scheme, $host, $path + + $zip_url = $scheme . '://api.github.com/repos' . $path; + $zip_url = add_query_arg( array( 'access_token' => $this->config['access_token'] ), $zip_url ); + + $this->config['zip_url'] = $zip_url; + } + + if ( !isset( $this->config['new_version'] ) ) { + $this->config['new_version'] = $this->get_new_version(); + } + + if ( !isset( $this->config['last_updated'] ) ) { + $this->config['last_updated'] = $this->get_date(); + } + + if ( !isset( $this->config['description'] ) ) { + $this->config['description'] = $this->get_description(); + } + + if ( !isset( $this->config['changelog'] ) ) { + $this->config['changelog'] = $this->get_changelog(); + } + + $plugin_data = $this->get_plugin_data(); + if ( !isset( $this->config['plugin_name'] ) ) { + $this->config['plugin_name'] = $plugin_data['Name']; + } + + if ( !isset( $this->config['version'] ) ) { + $this->config['version'] = $plugin_data['Version']; + } + + if ( !isset( $this->config['author'] ) ) { + $this->config['author'] = $plugin_data['Author']; + } + + if ( !isset( $this->config['homepage'] ) ) { + $this->config['homepage'] = $plugin_data['PluginURI']; + } + + if ( !isset( $this->config['readme'] ) ) { + $this->config['readme'] = 'README.md'; + } + + } + + /** + * Callback fn for the http_request_timeout filter + * + * @since 1.0 + * @return int timeout value + */ + public function http_request_timeout() + { + return 2; + } + + /** + * Callback fn for the http_request_args filter + * + * @param unknown $args + * @param unknown $url + * + * @return mixed + */ + public function http_request_sslverify( $args, $url ) + { + if ( $this->config['zip_url'] == $url ) { + $args['sslverify'] = $this->config['sslverify']; + } + + return $args; + } + + /** + * Get New Version from GitHub + * + * @since 1.0 + * @return int $version the version number + */ + public function get_new_version() + { + $version = get_site_transient( md5( $this->config['slug'] ) . '_new_version' ); + + if ( $this->overrule_transients() || ( !isset( $version ) || !$version || '' == $version ) ) { + + $raw_response = $this->remote_get( trailingslashit( $this->config['raw_url'] ) . basename( $this->config['slug'] ) ); + + if ( is_wp_error( $raw_response ) ) { + $version = false; + } + + if ( is_array( $raw_response ) ) { + if ( !empty( $raw_response['body'] ) ) { + preg_match( '/.*Version\:\s*(.*)$/mi', $raw_response['body'], $matches ); + } + + } + + if ( empty( $matches[1] ) ) { + $version = false; + } else { + $version = $matches[1]; + } + + // back compat for older readme version handling + // only done when there is no version found in file name + if ( false === $version ) { + $raw_response = $this->remote_get( trailingslashit( $this->config['raw_url'] ) . $this->config['readme'] ); + + if ( is_wp_error( $raw_response ) ) { + return $version; + } + + preg_match( '#^\s*`*~Current Version\:\s*([^~]*)~#im', $raw_response['body'], $__version ); + + if ( isset( $__version[1] ) ) { + $version_readme = $__version[1]; + if ( -1 == version_compare( $version, $version_readme ) ) { + $version = $version_readme; + } + + } + } + + // refresh every 6 hours + if ( false !== $version ) { + set_site_transient( md5( $this->config['slug'] ) . '_new_version', $version, 60 * 60 * 6 ); + } + + } + + return $version; + } + + /** + * Interact with GitHub + * + * @param string $query + * + * @since 1.6 + * @return mixed + */ + public function remote_get( $query ) + { + if ( !empty( $this->config['access_token'] ) ) { + $query = add_query_arg( array( 'access_token' => $this->config['access_token'] ), $query ); + } + + $raw_response = wp_remote_get( $query, array( + 'sslverify' => $this->config['sslverify'], + ) ); + + return $raw_response; + } + + /** + * Get GitHub Data from the specified repository + * + * @since 1.0 + * @return array $github_data the data + */ + public function get_github_data() + { + if ( isset( $this->github_data ) && !empty( $this->github_data ) ) { + $github_data = $this->github_data; + } else { + $github_data = get_site_transient( md5( $this->config['slug'] ) . '_github_data' ); + + if ( $this->overrule_transients() || ( !isset( $github_data ) || !$github_data || '' == $github_data ) ) { + $github_data = $this->remote_get( $this->config['api_url'] ); + + if ( is_wp_error( $github_data ) ) { + return false; + } + + $github_data = json_decode( $github_data['body'] ); + + // refresh every 6 hours + set_site_transient( md5( $this->config['slug'] ) . '_github_data', $github_data, 60 * 60 * 6 ); + } + + // Store the data in this class instance for future calls + $this->github_data = $github_data; + } + + return $github_data; + } + + /** + * Get update date + * + * @since 1.0 + * @return string $date the date + */ + public function get_date() + { + $_date = $this->get_github_data(); + return ( !empty( $_date->updated_at ) ) ? date( 'Y-m-d', strtotime( $_date->updated_at ) ) : false; + } + + /** + * Get plugin description + * + * @since 1.0 + * @return string $description the description + */ + public function get_description() + { + $_description = $this->get_github_data(); + return ( !empty( $_description->description ) ) ? $_description->description : false; + } + + /** + * Get plugin changelog + * + * @since 1.0 + * @return string $_changelog the changelog + */ + public function get_changelog() + { + $_changelog = $this->remote_get( $this->config['raw_url'] . '/changelog.txt' ); + $_changelog = nl2br( $_changelog['body'] ); + // return + return ( !empty( $_changelog ) ? $_changelog : 'Could not get changelog from server.' ); + } + + /** + * Get Plugin data + * + * @since 1.0 + * @return object $data the data + */ + public function get_plugin_data() + { + include_once ABSPATH . '/wp-admin/includes/plugin.php'; + $data = get_plugin_data( WP_PLUGIN_DIR . '/' . $this->config['slug'] ); + return $data; + } + + /** + * Hook into the plugin update check and connect to GitHub + * + * @since 1.0 + * @param object $transient the plugin data transient + * @return object $transient updated plugin data transient + */ + public function api_check( $transient ) + { + + // Check if the transient contains the 'checked' information + // If not, just return its value without hacking it + if ( empty( $transient->checked ) ) { + return $transient; + } + + // check the version and decide if it's new + $update = version_compare( $this->config['new_version'], $this->config['version'] ); + + if ( 1 === $update ) { + $response = new stdClass; + $response->new_version = $this->config['new_version']; + $response->slug = $this->config['proper_folder_name']; + $response->url = add_query_arg( array( 'access_token' => $this->config['access_token'] ), $this->config['github_url'] ); + $response->package = $this->config['zip_url']; + + // If response is false, don't alter the transient + if ( false !== $response ) { + $transient->response[$this->config['slug']] = $response; + } + + } + + return $transient; + } + + /** + * Get Plugin info + * + * @since 1.0 + * @param bool $false always false + * @param string $action the API function being performed + * @param object $args plugin arguments + * @return object $response the plugin info + */ + public function get_plugin_info( $false, $action, $response ) + { + + // Check if this call API is for the right plugin + if ( !isset( $response->slug ) || $response->slug != $this->config['proper_folder_name'] ) { + return false; + } + + $res = new stdClass(); + $res->name = $this->config['plugin_name']; + $res->slug = $this->config['slug']; + $res->version = $this->config['new_version']; + $res->author = $this->config['author']; + $res->homepage = $this->config['homepage']; + $res->requires = $this->config['requires']; + $res->tested = $this->config['tested']; + $res->downloaded = 0; + $res->last_updated = $this->config['last_updated']; + $res->sections = array( + 'description' => $this->config['description'], + 'changelog' => $this->config['changelog'], + ); + $res->download_link = $this->config['zip_url']; + + return $res; + } + + /** + * Upgrader/Updater + * Move & activate the plugin, echo the update message + * + * @since 1.0 + * @param boolean $true always true + * @param mixed $hook_extra not used + * @param array $result the result of the move + * @return array $result the result of the move + */ + public function upgrader_post_install( $true, $hook_extra, $result ) + { + + global $wp_filesystem; + + // Move & Activate + $proper_destination = WP_PLUGIN_DIR . '/' . $this->config['proper_folder_name']; + $wp_filesystem->move( $result['destination'], $proper_destination ); + $result['destination'] = $proper_destination; + $activate = activate_plugin( WP_PLUGIN_DIR . '/' . $this->config['slug'] ); + + // Output the update message + $fail = __( 'The plugin has been updated, but could not be reactivated. Please reactivate it manually.', 'github_plugin_updater' ); + $success = __( 'Plugin reactivated successfully.', 'github_plugin_updater' ); + echo is_wp_error( $activate ) ? $fail : $success; + return $result; + + } +} From ac705f31a834399456148d4654ba001e00c15d9c Mon Sep 17 00:00:00 2001 From: Bruce Atkinson Date: Wed, 2 Jan 2019 15:03:05 +0200 Subject: [PATCH 2/3] Tested with Gravity Forms v2.4.4 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7e122de..3202548 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # PayWeb_Gravity_Forms -## DPO PayGate Gravity Forms plugin v2.2.9 for Gravity Forms v2.2.5.21, WordPress v4.9.2 +## DPO PayGate Gravity Forms plugin v2.2.9 for Gravity Forms v2.4.4, WordPress v4.9.2 This is the DPO PayGate PayWeb3 plugin for Gravity Forms. Please feel free to contact the DPO PayGate support team at support@paygate.co.za should you require any assistance. From e3efaabb0efe0e6cb2bd8b007ad76039bc04b837 Mon Sep 17 00:00:00 2001 From: Bruce Atkinson Date: Wed, 2 Jan 2019 15:14:55 +0200 Subject: [PATCH 3/3] Remove extra updater function --- paygate.php | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/paygate.php b/paygate.php index 7b98d3d..ecbf867 100755 --- a/paygate.php +++ b/paygate.php @@ -49,37 +49,6 @@ public static function load() require_once 'paygate_gf_class.php'; GFAddOn::register( 'PayGateGF' ); - - /** - * Auto updates from GIT - * - * @since 2.2.9 - * - */ - - require_once 'updater.class.php'; - - if ( is_admin() ) { - // note the use of is_admin() to double check that this is happening in the admin - - $config = array( - 'slug' => plugin_basename( __FILE__ ), - 'proper_folder_name' => 'gravity-forms-paygate-plugin', - 'api_url' => 'https://api.github.com/repos/PayGate/PayWeb_Gravity_Forms', - 'raw_url' => 'https://raw.github.com/PayGate/PayWeb_Gravity_Forms/master', - 'github_url' => 'https://github.com/PayGate/PayWeb_Gravity_Forms', - 'zip_url' => 'https://github.com/PayGate/PayWeb_Gravity_Forms/archive/master.zip', - 'homepage' => 'https://github.com/PayGate/PayWeb_Gravity_Forms', - 'sslverify' => true, - 'requires' => '4.0', - 'tested' => '4.9.8', - 'readme' => 'README.md', - 'access_token' => '', - ); - - new WP_GitHub_Updater( $config ); - - } } }