Skip to content

Commit

Permalink
update integrations - use json
Browse files Browse the repository at this point in the history
  • Loading branch information
froger-me committed Jan 9, 2024
1 parent b42d9b3 commit f86abfb
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 80 deletions.
24 changes: 14 additions & 10 deletions integration/dummy-plugin/dummy-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,27 @@
/* ================================================================================================ */

/**
* Selectively uncomment the sections below to enable updates with WP Packages Update Server.
* Uncomment the section below to enable updates with WP Packages Update Server.
*
* 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
* - $prefix_updater => Replace "prefix" in this variable's name with a unique plugin prefix
*
* @see https://github.com/froger-me/wp-package-updater
* Before deploying the plugin or theme, make sure to change the following values in wppus.json:
* - server => The URL of the server where WP Packages Update Server is installed ; required
* - requireLicense => Whether the package requires a license ; true or false ; optional
*
* Also change $prefix_updater below - replace "prefix" in this variable's name with a unique prefix
*
**/

/** Enable updates**/
/* phpcs:ignore Squiz.PHP.CommentedOutCode.Found
require_once plugin_dir_path( __FILE__ ) . 'lib/wp-package-updater/class-wp-package-updater.php';
/** Enable plugin updates**/
// $prefix_updater = new WP_Package_Updater(
// wp_normalize_path( __FILE__ ),
// wp_normalize_path( plugin_dir_path( __FILE__ ) )
// );
$prefix_updater = new WP_Package_Updater(
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 @@ -24,28 +24,33 @@
* where appropriate to enable updates with WP Packages Update Server.
*
* WARNING - READ FIRST:
* Before deploying the plugin or theme, make sure to change the following value
* - 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
**/
*
* Before deploying the plugin or theme, make sure to change the following values in wppus.json:
* - server => The URL of the server where WP Packages Update Server is installed ; required
* - requireLicense => Whether the package requires a license ; true or false ; optional
*
* Also change $prefix_updater below - replace "prefix" in this variable's name with a unique prefix
*
/** Uncomment for plugin updates **/
// require_once plugin_dir_path( __FILE__ ) . 'lib/wp-package-updater/class-wp-package-updater.php';
/** Use for plugin updates **/
/* phpcs:ignore Squiz.PHP.CommentedOutCode.Found
require_once plugin_dir_path( __FILE__ ) . 'lib/wp-package-updater/class-wp-package-updater.php';
/** Enable plugin updates **/
// $prefix_updater = new WP_Package_Updater(
// wp_normalize_path( __FILE__ ),
// wp_normalize_path( plugin_dir_path( __FILE__ ) ),
// );
$prefix_updater = new WP_Package_Updater(
wp_normalize_path( __FILE__ ),
wp_normalize_path( plugin_dir_path( __FILE__ ) )
);
*/

/** Uncomment for theme updates **/
// require_once get_stylesheet_directory() . '/lib/wp-package-updater/class-wp-package-updater.php';
/** Use for theme updates **/
/* phpcs:ignore Squiz.PHP.CommentedOutCode.Found
require_once plugin_dir_path( __FILE__ ) . 'lib/wp-package-updater/class-wp-package-updater.php';
/** Enable theme updates **/
// $prefix_updater = new WP_Package_Updater(
// wp_normalize_path( __FILE__ ),
// get_stylesheet_directory(),
// );
$prefix_updater = new WP_Package_Updater(
wp_normalize_path( __FILE__ ),
get_stylesheet_directory()
);
*/

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

Expand All @@ -63,7 +68,8 @@ class WP_Package_Updater {
private $type;
private $use_license;
private $package_id;
private $json_options;

private static $json_options;

public function __construct( $package_file_path, $package_path ) {
global $wp_filesystem;
Expand All @@ -86,7 +92,7 @@ public function __construct( $package_file_path, $package_path ) {
}

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

$this->set_type();

Expand Down Expand Up @@ -144,6 +150,9 @@ public function __construct( $package_file_path, $package_path ) {
add_action( 'admin_enqueue_scripts', array( $this, 'add_admin_scripts' ), 99, 1 );
add_action( 'admin_notices', array( $this, 'show_license_error_notice' ), 10, 0 );
add_action( 'init', array( $this, 'load_textdomain' ), 10, 0 );
add_action( 'upgrader_process_complete', array( $this, 'upgrader_process_complete' ), 10, 2 );

add_filter( 'upgrader_pre_install', array( $this, 'upgrader_pre_install' ), 10, 2 );
}
}

Expand Down Expand Up @@ -382,7 +391,7 @@ public function set_license_error_notice_content( $package_info, $result ) { //
}

public function show_license_error_notice() {
$error = get_option( 'licenseError' );
$error = $this->get_option( 'licenseError' );

if ( $error ) {
$class = 'license-error license-error-' . $this->package_slug . ' notice notice-error is-dismissible';
Expand All @@ -391,6 +400,53 @@ public function show_license_error_notice() {
}
}

public function upgrader_process_complete( $upgrader_object, $options ) {

if ( 'update' === $options['action'] ) {

if ( 'plugin' === $options['type'] && isset( $options['plugins'] ) && is_array( $options['plugins'] ) ) {

foreach ( $options['plugins'] as $plugin ) {

if ( $plugin === $this->package_id ) {
$this->restore_wppus_options();
}
}
}

if ( 'theme' === $options['type'] && isset( $options['themes'] ) && is_array( $options['themes'] ) ) {

foreach ( $options['themes'] as $theme ) {

if ( $theme === $this->package_slug ) {
$this->restore_wppus_options();
}
}
}
}
}

public function upgrader_pre_install( $_true, $hook_extra ) {

if ( isset( $hook_extra['plugin'] ) ) {
$plugin_to_update = $hook_extra['plugin'];

if ( $plugin_to_update === $this->package_id ) {
$this->save_wppus_options();
}
}

if ( isset( $hook_extra['theme'] ) ) {
$theme_to_update = $hook_extra['theme'];

if ( $theme_to_update === $this->package_slug ) {
$this->save_wppus_options();
}
}

return $_true;
}

// Misc. -------------------------------------------------------

public function locate_template( $template_name, $load = false, $required_once = true ) {
Expand Down Expand Up @@ -568,6 +624,50 @@ protected function delete_option( $option ) {
}
}

protected function save_wppus_options() {
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 );
}
}

update_option( 'wppus_' . $this->package_slug . '_options', self::$json_options );
}

protected function restore_wppus_options() {
$wppus_options = get_option( 'wppus_' . $this->package_slug . '_options' );

if ( $wppus_options ) {
global $wp_filesystem;

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

WP_Filesystem();
}

$server = $this->get_option( 'server' );
$wppus_options['server'] = $server;
$wppus_json = wp_json_encode(
$wppus_options,
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
);

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

protected function do_query_license( $query_type ) {

if ( ! wp_verify_nonce( $_REQUEST['nonce'], 'license_nonce' ) ) {
Expand Down Expand Up @@ -702,7 +802,7 @@ protected function handle_license_errors( $license_data, $query_type = null ) {
}

protected function get_license_form() {
$license = get_option( 'licenseKey' );
$license = $this->get_option( 'licenseKey' );

ob_start();

Expand Down
5 changes: 3 additions & 2 deletions integration/dummy-plugin/wppus.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"server": "https:\/\/server.domain.tld\/"
}
"server": "https:\/\/server.domain.tld\/",
"requireLicense": true
}
38 changes: 16 additions & 22 deletions integration/dummy-theme/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,27 @@
/* ================================================================================================ */

/**
* Selectively uncomment the sections below to enable updates with WP Packages Update Server.
* Uncomment the section below to enable updates with WP Packages Update Server.
*
* 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
* - $prefix_updater => Replace "prefix" in this variable's name with a unique theme prefix
*
* @see https://github.com/froger-me/wp-package-updater
* Before deploying the plugin or theme, make sure to change the following values in wppus.json:
* - server => The URL of the server where WP Packages Update Server is installed ; required
* - requireLicense => Whether the package requires a license ; true or false ; optional
*
* Also change $prefix_updater below - replace "prefix" in this variable's name with a unique prefix
*
**/

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(),
// 'Public API Authentication Key',
// true
// );

/** Enable theme updates without license check **/
// $prefix_updater = new WP_Package_Updater(
// 'https://your-update-server.com',
// wp_normalize_path( __FILE__ ),
// get_stylesheet_directory()
// );
/** Enable updates**/
/* phpcs:ignore Squiz.PHP.CommentedOutCode.Found
require_once plugin_dir_path( __FILE__ ) . 'lib/wp-package-updater/class-wp-package-updater.php';
$prefix_updater = new WP_Package_Updater(
wp_normalize_path( __FILE__ ),
get_stylesheet_directory()
);
*/

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

Expand Down
Loading

0 comments on commit f86abfb

Please sign in to comment.