From 0f737972c1cc969ec4fa2e7294f5c2f9bbcc67dd Mon Sep 17 00:00:00 2001 From: Remco Tolsma <869674+remcotolsma@users.noreply.github.com> Date: Fri, 21 Oct 2022 14:58:20 +0200 Subject: [PATCH] Working on releaser. --- .github/scripts/README.md | 4 +- .github/scripts/release.php | 76 +++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/.github/scripts/README.md b/.github/scripts/README.md index 51a9a0a..abe4445 100644 --- a/.github/scripts/README.md +++ b/.github/scripts/README.md @@ -5,9 +5,11 @@ > **Note** > WooCommerce Helper is part of WooCommerce as of version 3.1. Purchase, connect, download products, and activate keys in one place with ease. Read more at Managing WooCommerce.com subscriptions. -http://woodojo.s3.amazonaws.com/downloads/woothemes-updater/woothemes-updater.zip +- https://woocommerce.com/document/woocommerce-helper/ +- http://woodojo.s3.amazonaws.com/downloads/woothemes-updater/woothemes-updater.zip ## WooCommerce API - https://woocommerce.com/wc-api/product-key-api - https://woocommerce.com/wc-api/product-key-api?request=check +- https://woocommerce.com/wp-json/helper/1.0 diff --git a/.github/scripts/release.php b/.github/scripts/release.php index 5c933c8..99b3923 100644 --- a/.github/scripts/release.php +++ b/.github/scripts/release.php @@ -13,3 +13,79 @@ function run( $command, &$result_code = null ) { return $last_line; } + +/** + * WooCommerce Helper API authentication. + * + * @link https://github.com/woocommerce/woocommerce/blob/ca91250b2e17e88c902e460135c0531b3f632d90/plugins/woocommerce/includes/admin/helper/class-wc-helper-api.php#L67-L118 + */ +$access_token = getenv( 'WOOCOMMERCE_HELPER_ACCESS_TOKEN' ); +$access_token_secret = getenv( 'WOOCOMMERCE_HELPER_ACCESS_TOKEN_SECRET' ); + +if ( empty( $access_token ) ) { + echo 'WooCommerce Helper API acces token not defined in `WOOCOMMERCE_HELPER_ACCESS_TOKEN` environment variable.'; + + exit( 1 ); +} + +if ( empty( $access_token_secret ) ) { + echo 'WooCommerce Helper API acces token secret not defined in `WOOCOMMERCE_HELPER_ACCESS_TOKEN_SECRET` environment variable.'; + + exit( 1 ); +} + +// Subscriptions. +$url = 'https://woocommerce.com/wp-json/helper/1.0/subscriptions'; + +$data = array( + 'host' => parse_url( $url, PHP_URL_HOST ), + 'request_uri' => parse_url( $url, PHP_URL_PATH ), + 'method' => 'GET', +); + +$signature = hash_hmac( 'sha256', json_encode( $data ), $access_token_secret ); + +$url .= '?' . http_build_query( + [ + 'token' => $access_token, + 'signature' => $signature, + ] +); + +$command = "curl -X GET '$url' -H 'Authorization: Bearer $access_token' -H 'X-Woo-Signature: $signature';"; + +run( $command ); + +// Check +$payload = [ + 27147 => [ + 'product_id' => 27147, + 'file_id' => '', + ], +]; + +ksort( $payload ); + +$body = json_encode( array( 'products' => $payload ) ); + +$url = 'https://woocommerce.com/wp-json/helper/1.0/update-check'; + +$data = array( + 'host' => parse_url( $url, PHP_URL_HOST ), + 'request_uri' => parse_url( $url, PHP_URL_PATH ), + 'method' => 'POST', + 'body' => $body, +); + +$signature = hash_hmac( 'sha256', json_encode( $data ), $access_token_secret ); + +$url .= '?' . http_build_query( + [ + 'token' => $access_token, + 'signature' => $signature, + ] +); + +$command = "curl -d '$body' -X POST '$url' -H 'Authorization: Bearer $access_token' -H 'X-Woo-Signature: $signature';"; + +run( $command );