Skip to content

Commit

Permalink
WIP - move all to wppus.json for server, licenseKey, licenseSignature…
Browse files Browse the repository at this point in the history
…, domain
  • Loading branch information
froger-me committed Jan 8, 2024
1 parent d21f813 commit 22c8c05
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 71 deletions.
4 changes: 2 additions & 2 deletions integration/dummy-generic/wppus-api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# replace https://server.domain.tld/ with the URL of the server where
# WP Packages Update Server is installed
url="https://server.domain.tld"
url=$(jq -r '.server' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json")

# define the package name
package_name="$(basename "$(cd "$(dirname "$0")"; pwd -P)")"
Expand All @@ -24,7 +24,7 @@ script_path="$(cd "$(dirname "$0")"; pwd -P)/$package_name/$script_name"
# define the update zip archive name
zip_name="$package_name.zip"
# define the current version of the package from the wppus.json file
version=$(jq -r '.Version' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json")
version=$(jq -r '.packageData.Version' "$(cd "$(dirname "$0")"; pwd -P)/wppus.json")
# define the domain
if [[ "$(uname)" == "Darwin" ]]; then
# macOS
Expand Down
16 changes: 9 additions & 7 deletions integration/dummy-generic/wppus.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"Name": "Dummy Generic Package",
"Version": "1.4.14",
"Homepage": "https:\/\/froger.me\/",
"Author": "Alexandre Froger",
"AuthorURI": "https:\/\/froger.me\/",
"Description": "Empty generic package to demonstrate the WP Package Updater.",
"PackageData": ["arbitrary data - can be left empty, but MUST be present."]
"server": "https:\/\/server.domain.tld\/",
"packageData": {
"Name": "Dummy Generic Package",
"Version": "1.4.14",
"Homepage": "https:\/\/froger.me\/",
"Author": "Alexandre Froger",
"AuthorURI": "https:\/\/froger.me\/",
"Description": "Empty generic package to demonstrate the WP Package Updater."
}
}
11 changes: 1 addition & 10 deletions integration/dummy-plugin/dummy-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,8 @@

require_once plugin_dir_path( __FILE__ ) . 'lib/wp-package-updater/class-wp-package-updater.php';

/** Enable plugin updates with license check **/
/** Enable plugin updates**/
// $prefix_updater = new WP_Package_Updater(
// 'https://your-update-server.com',
// wp_normalize_path( __FILE__ ),
// wp_normalize_path( plugin_dir_path( __FILE__ ) ),
// true
// );

/** Enable plugin updates without license check **/
// $prefix_updater = new WP_Package_Updater(
// 'https://your-update-server.com',
// wp_normalize_path( __FILE__ ),
// wp_normalize_path( plugin_dir_path( __FILE__ ) )
// );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,54 +25,33 @@
*
* WARNING - READ FIRST:
* Before deploying the plugin or theme, make sure to change the following value
* - https://your-update-server.com => The URL of the server where WP Packages Update Server is installed.
* - https://server.domain.tld => In wppus.json ; the URL of the server where WP Packages Update Server is installed.
* - $prefix_updater => Change this variable's name with your plugin or theme prefix
**/

/** Uncomment for plugin updates **/
// require_once plugin_dir_path( __FILE__ ) . 'lib/wp-package-updater/class-wp-package-updater.php';

/** Enable plugin updates with license check **/
/** Enable plugin updates **/
// $prefix_updater = new WP_Package_Updater(
// 'https://your-update-server.com',
// wp_normalize_path( __FILE__ ),
// wp_normalize_path( plugin_dir_path( __FILE__ ) ),
// true
// );

/** Enable plugin updates without license check **/
// $prefix_updater = new WP_Package_Updater(
// 'https://your-update-server.com',
// wp_normalize_path( __FILE__ ),
// wp_normalize_path( plugin_dir_path( __FILE__ ) ),
// false // Can be omitted, false by default
// );

/** Uncomment for theme updates **/
// require_once get_stylesheet_directory() . '/lib/wp-package-updater/class-wp-package-updater.php';

/** Enable theme updates with license check **/
// $prefix_updater = new WP_Package_Updater(
// 'https://your-update-server.com',
// wp_normalize_path( __FILE__ ),
// get_stylesheet_directory(),
// true
// );

/** Enable theme updates without license check **/
/** Enable theme updates **/
// $prefix_updater = new WP_Package_Updater(
// 'https://your-update-server.com',
// wp_normalize_path( __FILE__ ),
// get_stylesheet_directory(),
// false // Can be omitted, false by default
// );

/* ================================================================================================ */

if ( ! class_exists( 'WP_Package_Updater' ) ) {

class WP_Package_Updater {

const VERSION = '2.0';

private $license_server_url;
Expand All @@ -84,15 +63,31 @@ class WP_Package_Updater {
private $type;
private $use_license;
private $package_id;
private $json_options;

public function __construct( $package_file_path, $package_path ) {
global $wp_filesystem;

if ( ! isset( $wp_filesystem ) ) {
include_once ABSPATH . 'wp-admin/includes/file.php';

WP_Filesystem();
}

public function __construct(
$update_server_url,
$package_file_path,
$package_path,
$use_license = false
) {
$this->package_path = trailingslashit( $package_path );

if ( ! $wp_filesystem->exists( $package_path . 'wppus.json' ) ) {
throw new RuntimeException(
sprintf(
'The package updater cannot find the wppus.json file in "%s". ',
esc_html( htmlentities( $package_path ) )
)
);
}

$update_server_url = $this->get_option( 'server' );
$use_license = ! empty( $this->get_option( 'licenseKey' ) );

$this->set_type();

$package_path_parts = explode( '/', $package_path );
Expand All @@ -106,7 +101,6 @@ public function __construct(
$package_file_path_parts = explode( '/', $package_file_path );
$package_id_parts = array_slice( $package_file_path_parts, -2, 2 );
$package_id = implode( '/', $package_id_parts );

$this->package_id = $package_id;
$this->update_server_url = trailingslashit( $update_server_url ) . 'wppus-update-api/';
$this->package_slug = $package_slug;
Expand Down Expand Up @@ -489,6 +483,56 @@ protected static function get_theme_directory_name( $absolute_path ) {
return null;
}

protected function get_option( $option ) {
global $wp_filesystem;

if ( ! isset( $wp_filesystem ) ) {
include_once ABSPATH . 'wp-admin/includes/file.php';

WP_Filesystem();
}

if ( ! isset( self::$json_options ) ) {
$wppus_json = $wp_filesystem->get_contents( $this->package_path . 'wppus.json' );

if ( $wppus_json ) {
self::$json_options = json_decode( $wppus_json, true );
}
}

if ( isset( self::$json_options[ $option ] ) ) {
return self::$json_options[ $option ];
}

return '';
}

protected function update_option( $option, $value ) {
global $wp_filesystem;

if ( ! isset( $wp_filesystem ) ) {
include_once ABSPATH . 'wp-admin/includes/file.php';

WP_Filesystem();
}

if ( ! isset( self::$json_options ) ) {
$wppus_json = $wp_filesystem->get_contents( $this->package_path . 'wppus.json' );

if ( $wppus_json ) {
self::$json_options = json_decode( $wppus_json, true );
}
}

self::$json_options[ $option ] = $value;
$wppus_json = wp_json_encode(
self::$json_options,
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
);

$wp_filesystem->put_contents( $this->package_path . 'wppus.json', $wppus_json, FS_CHMOD_FILE );
}

protected function do_query_license( $query_type ) {

if ( ! wp_verify_nonce( $_REQUEST['nonce'], 'license_nonce' ) ) {
Expand Down
3 changes: 3 additions & 0 deletions integration/dummy-plugin/wppus.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"server": "https:\/\/server.domain.tld\/"
}
3 changes: 3 additions & 0 deletions integration/dummy-theme/wppus.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"server": "https:\/\/server.domain.tld\/"
}
7 changes: 1 addition & 6 deletions lib/proxy-update-checker/Proxuc/Generic/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
if (!class_exists(Update::class, false)):

class Update extends BaseUpdate {
protected static $extraFields = array('package_data');

public static function fromJson($json) {
php_log();
$instance = new self();

if (!parent::createFromJson($json, $instance)) {
Expand All @@ -21,16 +19,14 @@ public static function fromJson($json) {
}

public static function fromObject($object) {
php_log();
$update = new self();
$update->copyFields($object, $update);

return $update;
}

protected function validateMetadata($apiResponse) {
php_log();
$required = array('version', 'package_data');
$required = array('version');

foreach ($required as $key) {

Expand All @@ -45,7 +41,6 @@ protected function validateMetadata($apiResponse) {
}

protected function getPrefixedFilter($tag) {
php_log();
return parent::getPrefixedFilter($tag) . '_generic';
}
}
Expand Down
23 changes: 11 additions & 12 deletions lib/proxy-update-checker/Proxuc/Generic/UpdateChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,36 @@ protected function getUpdateListKey() {

public function requestUpdate() {
$api = $this->api;

$api->setLocalDirectory($this->Vcs_getAbsoluteDirectoryPath());

$update = new Update();
$update->slug = $this->slug;
$update->version = null;

//Figure out which reference (tag or branch) we'll use to get the latest version of the theme.
$updateSource = $api->chooseReference($this->branch);

if ($updateSource) {
$ref = $updateSource->name;
$update->download_url = $updateSource->downloadUrl;
} else {
return 'source_not_found';
}

//Get headers from the main stylesheet in this branch/tag. Its "Version" header and other metadata
//are what the WordPress install will actually see after upgrading, so they take precedence over releases/tags.
$file = $api->getRemoteFile('wppus.json', $ref);

if (!empty($file)) {
$remoteHeader = json_decode($file, true);
$update->version = Utils::findNotEmpty(array(
$remoteHeader['Version'],
Utils::get($updateSource, 'version'),
));
$fileContents = json_decode($file, true);

if (isset($fileContents['packageData']) && !empty($fileContents['packageData'])) {
$remoteHeader = $fileContents['packageData'];
$update->version = Utils::findNotEmpty(array(
$remoteHeader['Version'],
Utils::get($updateSource, 'version'),
));
}
}

if (empty($update->version)) {
//It looks like we didn't find a valid update after all.
$update = null;
}

Expand All @@ -82,8 +83,6 @@ protected function filterUpdateResult($update, $httpResult = null) {
protected function getNoUpdateItemFields() {
$fields = parent::getNoUpdateItemFields();

unset($fields['requires_php']);

return array_merge(
parent::getNoUpdateItemFields(),
array('generic' => $this->directoryName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class Wpup_ZipMetadataParser_Extended extends Wpup_ZipMetadataParser {
'RequiresPHP' => 'requires_php',
'Description' => 'description',
'DetailsURI' => 'details_url', //Only for themes
'PackageData' => 'package_data', //Only for generic
'Depends' => 'depends', // plugin-dependencies plugin
'Provides' => 'provides', // plugin-dependencies plugin
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ public static function parsePackage($packageFilename, $applyMarkdown = false){
//Generic info file?
if (empty($header) && ($extension === 'json') && (basename($fileName) === 'wppus.json')){
$fileContents = substr($zip->getFileContents($info), 0, 8*1024);
$header = json_decode($fileContents, true);
$fileContents = json_decode($fileContents, true);

if (!empty($header)){
if (!empty($fileContents) && isset($fileContents['packageData']) && !empty($fileContents['packageData'])){
$header = $fileContents['packageData'];
$genericFile = $fileName;
$type = 'generic';
}
Expand Down

0 comments on commit 22c8c05

Please sign in to comment.