Skip to content

Commit

Permalink
fix functions + refactor main file + refactor mu plugin + refactor mi…
Browse files Browse the repository at this point in the history
…gration script + update doc
  • Loading branch information
froger-me committed Jan 16, 2024
1 parent 20622d6 commit c1a00c3
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 202 deletions.
2 changes: 1 addition & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function wppus_get_local_package_path( $package_slug ) {
function wppus_browse_licenses( $browse_query ) {
$api = WPPUS_License_API::get_instance();

return $api->browse( $$browse_query );
return $api->browse( $browse_query );
}
}

Expand Down
8 changes: 4 additions & 4 deletions integration/docs/misc.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ WP Packages Update Server provides an API and offers a series of functions, acti
* [wppus\_fire\_webhook](#wppus_fire_webhook)
* [Actions](#actions)
* [wppus\_no\_api\_includes](#wppus_no_api_includes)
* [wppus\_no\_license\_api\_includes](#wppus_no_license_api_includes)
* [wppus\_no\_priority\_api\_includes](#wppus_no_priority_api_includes)
* [wppus\_remote\_sources\_options\_updated](#wppus_remote_sources_options_updated)
* [Filters](#filters)
* [wppus\_is\_api\_request](#wppus_is_api_request)
Expand Down Expand Up @@ -584,14 +584,14 @@ do_action( 'wppus_no_api_includes' );
Fired when the plugin is including files and the current request is not made by a remote client interacting with any of the plugin's API.

___
### wppus_no_license_api_includes
### wppus_no_priority_api_includes

```php
do_action( 'wppus_no_license_api_includes' );
do_action( 'wppus_no_priority_api_includes' );
```

**Description**
Fired when the plugin is including files and the current request is not made by a client plugin or theme interacting with the plugin's license API.
Fired when the plugin is including files and the current request is not made by a client plugin or theme interacting with the plugin's high priority API (typically the license API).

___
### wppus_remote_sources_options_updated
Expand Down
93 changes: 44 additions & 49 deletions lib/wp-update-migrate/class-wp-update-migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
* WordPress plugins and themes update path library.
*
* @author Alexandre Froger
* @version 1.2.2
* @see https://github.com/froger-me/wp-update-migrate
* @copyright Alexandre Froger - https://www.froger.me
* @version 1.5
*/

/*================================================================================================ */
Expand All @@ -31,42 +29,41 @@
* - each update function returns (bool) true in case of success, a WP_Error object otherwise
**/

/** For plugins **/
// require_once plugin_dir_path( __FILE__ ) . 'lib/wp-update-migrate/class-wp-update-migrate.php';
//
// $hook = 'plugins_loaded';
// phpcs:disable
// require_once __DIR__ . '/lib/wp-update-migrate/class-wp-update-migrate.php';

/** For themes **/
// require_once trailingslashit( get_stylesheet_directory() ) . 'lib/wp-update-migrate/class-wp-update-migrate.php';
//
// $hook = 'after_setup_theme';

// add_action( $hook, function() {
// $update_migrate = WP_Update_Migrate::get_instance( __FILE__, 'example_prefix' );

// if ( false === $update_migrate->get_result() ) {
///**
// * @todo
// * Execute your own logic here in case the update failed.
// *
// * if ( false !== has_action( 'example_action', 'example_function' ) ) {
// * remove_action( 'example_action', 'example_function', 10 );
// * }
// **/
// }

// if ( true === $update_migrate->get_result() ) {
///**
// * @todo
// * Execute your own logic here in case an update was applied successfully.
// *
// * if ( false === has_action( 'example_action', 'example_function' ) ) {
// * add_action( 'example_action', 'example_function', 10 );
// * }
// **/
// }

// }, PHP_INT_MIN );
// add_action(
// 0 === strpos( __DIR__, WP_PLUGIN_DIR ) ? 'plugins_loaded' : 'after_setup_theme',
// function () {
// $update_migrate = WP_Update_Migrate::get_instance( __FILE__, 'example_prefix' );

// if ( false === $update_migrate->get_result() ) {
// /**
// * @todo
// * Execute your own logic here in case the update failed.
// *
// * if ( false !== has_action( 'example_action', 'example_function' ) ) {
// * remove_action( 'example_action', 'example_function', 10 );
// * }
// **/
// }

// if ( true === $update_migrate->get_result() ) {
// /**
// * @todo
// * Execute your own logic here in case an update was applied successfully.
// *
// * if ( false === has_action( 'example_action', 'example_function' ) ) {
// * add_action( 'example_action', 'example_function', 10 );
// * }
// **/
// }
// },
// PHP_INT_MIN + 100
// );
// phpcs:enable

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

Expand All @@ -78,7 +75,7 @@

class WP_Update_Migrate {

const VERSION = '1.2.0';
const VERSION = '1.5.0';

protected $failed_update_info;
protected $success_update_info;
Expand Down Expand Up @@ -117,9 +114,8 @@ private function __construct( $package_handle, $package_prefix ) {

if ( ! is_null( $this->package_type ) ) {
$current_recorded_version = get_option( $package_prefix . '_' . $this->package_type . '_version' );
$is_version_current = version_compare( $current_recorded_version, $latest_version, '>=' );

if ( ! $is_version_current ) {
if ( ! version_compare( $current_recorded_version, $latest_version, '>=' ) ) {
$i10n_path = trailingslashit( basename( $this->package_dir ) ) . 'lib/wp-update-migrate/languages';

if ( 'plugin' === $this->package_type ) {
Expand All @@ -146,22 +142,19 @@ public static function get_instance( $package_handle, $package_prefix ) {
$cache = wp_cache_get( $package_prefix, 'wp-update-migrate' );

if ( ! $cache ) {

self::$instance = new self( $package_handle, $package_prefix );
}

return self::$instance;
}

public function get_result() {

return $this->update_result;
}

public function update_failed_notice() {
$class = 'notice notice-error is-dismissible';
$message = '<p>' . $this->failed_update_info . '</p>';

// translators: %1$s is the package type
$message .= '<p>' . sprintf( __( 'The %1$s may not have any effect until the issues are resolved.', 'wp-update-migrate' ), $this->package_type ) . '</p>';

Expand Down Expand Up @@ -224,7 +217,7 @@ protected function update() {
}
}

if ( true === $result ) {
if ( $result && ! is_wp_error( $result ) ) {

if ( ! $this->update_file_exists_for_version( $this->to_version ) ) {
$result = $this->update_package_version( $this->to_version );
Expand All @@ -234,16 +227,21 @@ protected function update() {
if ( true !== $result ) {
$this->update_result = false;
$result = $this->handle_error( $result );

$notice_hooks = apply_filters( 'wpum_update_failure_extra_notice_hooks', array( array( $this, 'update_failed_notice' ) ) );
$notice_hooks = apply_filters(
'wpum_update_failure_extra_notice_hooks',
array( array( $this, 'update_failed_notice' ) )
);
} else {
$this->update_result = true;

if ( ! empty( $update_path ) ) {
$this->handle_success( sprintf( __( '<br/>All updates have been applied successfully.', 'wp-update-migrate' ), $this->to_version ) );
}

$notice_hooks = apply_filters( 'wpum_update_success_extra_notice_hooks', array( array( $this, 'update_success_notice' ) ) );
$notice_hooks = apply_filters(
'wpum_update_success_extra_notice_hooks',
array( array( $this, 'update_success_notice' ) )
);
}

foreach ( $notice_hooks as $hook ) {
Expand Down Expand Up @@ -294,7 +292,6 @@ protected function do_update( $version ) {

return true;
} else {

return $this->handle_error( $result );
}
}
Expand All @@ -311,12 +308,10 @@ protected function init_package_type() {
}

protected function update_file_exists_for_version( $version ) {

return file_exists( $this->package_dir . 'updates' . DIRECTORY_SEPARATOR . $version . '.php' );
}

protected function get_update_file_path_for_version( $version ) {

return $this->package_dir . 'updates' . DIRECTORY_SEPARATOR . $version . '.php';
}

Expand Down
Loading

0 comments on commit c1a00c3

Please sign in to comment.