Skip to content

Commit

Permalink
Generic WIP ; next is to check YahnisElsts\PluginUpdateChecker\v5p3\U…
Browse files Browse the repository at this point in the history
…pdateChecker->requestMetadata does not point to a valid metadata file. HTTP response code is 404 (expected: 200)
  • Loading branch information
froger-me committed Jan 7, 2024
1 parent 7b9255f commit 886ffe6
Show file tree
Hide file tree
Showing 25 changed files with 493 additions and 163 deletions.
11 changes: 11 additions & 0 deletions inc/class-wp-packages-update-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __construct( $init_hooks = false ) {
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ), 10, 1 );
add_action( 'admin_menu', array( $this, 'admin_menu' ), 5, 0 );
add_action( 'admin_menu', array( $this, 'admin_menu_help' ), 99, 0 );
add_action( 'action_scheduler_failed_execution', array( $this, 'action_scheduler_failed_execution' ), 10, 3 );

add_filter( 'wppus_admin_scripts', array( $this, 'wppus_admin_scripts' ), 10, 1 );
add_filter( 'wppus_admin_styles', array( $this, 'wppus_admin_styles' ), 10, 1 );
Expand All @@ -36,6 +37,16 @@ public function __construct( $init_hooks = false ) {
* Public methods
*******************************************************************/

public function action_scheduler_failed_execution( $action_id, Exception $exception, $context = '' ) {
php_log(
array(
'action_id' => $action_id,
'exception' => $exception,
'context' => $context,
)
);
}

// WordPress hooks ---------------------------------------------

public static function activate() {
Expand Down
38 changes: 24 additions & 14 deletions inc/class-wppus-cloud-storage-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ public function wppus_submitted_package_config( $config ) {
$config,
array(
'wppus_use_cloud_storage' => array(
'value' => filter_input( INPUT_POST, 'wppus_use_cloud_storage', FILTER_VALIDATE_BOOLEAN ),
'display_name' => __( 'Use Cloud Storage', 'wppus' ),
'condition' => 'boolean',
'value' => filter_input( INPUT_POST, 'wppus_use_cloud_storage', FILTER_VALIDATE_BOOLEAN ),
'display_name' => __( 'Use Cloud Storage', 'wppus' ),
'failure_display_message' => __( 'Something went wrong', 'wppus' ),
'condition' => 'boolean',
),
'wppus_cloud_storage_access_key' => array(
'value' => filter_input( INPUT_POST, 'wppus_cloud_storage_access_key', FILTER_SANITIZE_FULL_SPECIAL_CHARS ),
Expand Down Expand Up @@ -157,8 +158,6 @@ public function wppus_submitted_package_config( $config ) {

public function wppus_package_option_update( $condition, $option_name, $option_info, $options ) {

php_log();

if ( 'use-cloud-storage' === $option_info['condition'] ) {

if (
Expand All @@ -182,6 +181,8 @@ public function wppus_package_option_update( $condition, $option_name, $option_i
if ( ! $condition ) {
update_option( 'wppus_use_cloud_storage', false );
}
} else {
$condition = true;
}

return $condition;
Expand Down Expand Up @@ -430,7 +431,7 @@ public function wppus_check_remote_package_update_local_meta( $local_meta, $loca
$wp_filesystem->is_file( $filename ) &&
$wp_filesystem->is_readable( $filename )
) {
$local_meta = WshWordPressPackageParser::parsePackage( $filename, true );
$local_meta = WshWordPressPackageParser_Extended::parsePackage( $filename, true );
}
} catch ( PhpS3Exception $e ) {
php_log( $e );
Expand Down Expand Up @@ -741,13 +742,24 @@ public function wppus_package_info( $package_info, $slug ) {
);

if ( $result ) {
$package = Wpup_Package_Extended::fromArchive( $filename, $slug, $cache );
$cache_value = $package->getMetadata();
$package = false;

$cache->set( $cache_key, $cache_value, Wpup_ZipMetadataParser::$cacheTime ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
try {
$package = Wpup_Package_Extended::fromArchive( $filename, $slug, $cache );
} catch ( Wpup_InvalidPackageException $e ) {
php_log( $e );

$cleanup = true;
}

if ( $package ) {
$package_info = $cache_value;
$package_info = $package->getMetadata();

$cache->set(
$cache_key,
$package_info,
Wpup_ZipMetadataParser::$cacheTime // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
);
}
}
} else {
Expand All @@ -756,10 +768,8 @@ public function wppus_package_info( $package_info, $slug ) {

if ( $package_info ) {

if ( $package_info['details_url'] ) {
$package_info['type'] = 'theme';
} else {
$package_info['type'] = 'plugin';
if ( ! isset( $package_info['type'] ) ) {
$package_info['type'] = 'unknown';
}

$package_info['file_name'] = $package_info['slug'] . '.zip';
Expand Down
4 changes: 2 additions & 2 deletions inc/class-wppus-data-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class WPPUS_Data_Manager {
public function __construct( $init_hooks = false ) {

if ( $init_hooks ) {
add_action( 'init', array( $this, 'init' ), 10, 0 );
add_action( 'action_scheduler_init', array( $this, 'action_scheduler_init' ), 10, 0 );
}
}

Expand All @@ -34,7 +34,7 @@ public function __construct( $init_hooks = false ) {

// WordPress hooks ---------------------------------------------

public function init() {
public function action_scheduler_init() {
self::register_cleanup_events();
self::register_cleanup_schedules();
}
Expand Down
4 changes: 2 additions & 2 deletions inc/class-wppus-license-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct( $init_hooks = false ) {
if ( $use_licenses ) {
$this->license_server = new WPPUS_License_Server();

add_action( 'init', array( $this, 'init' ), 10, 0 );
add_action( 'action_scheduler_init', array( $this, 'action_scheduler_init' ), 10, 0 );
add_action( 'wppus_packages_table_cell', array( $this, 'wppus_packages_table_cell' ), 10, 4 );

add_filter( 'wppus_packages_table_columns', array( $this, 'wppus_packages_table_columns' ), 10, 1 );
Expand Down Expand Up @@ -102,7 +102,7 @@ public static function deactivate() {
do_action( 'wppus_cleared_license_schedule' );
}

public function init() {
public function action_scheduler_init() {
$hook = 'wppus_expire_licenses';

if ( ! as_has_scheduled_action( $hook ) ) {
Expand Down
5 changes: 3 additions & 2 deletions inc/class-wppus-license-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -775,9 +775,10 @@ protected function validate_license_payload( $license, $partial = false ) {
if (
! ( $partial && ! isset( $license['package_type'] ) ) &&
'plugin' !== $license['package_type'] &&
'theme' !== $license['package_type']
'theme' !== $license['package_type'] &&
'generic' !== $license['package_type']
) {
$errors['invalid_package_type'] = __( 'The package type is required and must be "plugin" or "theme".', 'wppus' );
$errors['invalid_package_type'] = __( 'The package type is required and must be "generic", "plugin" or "theme".', 'wppus' );
}

if (
Expand Down
2 changes: 2 additions & 0 deletions inc/class-wppus-license-update-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ protected function verify_license_exists( $license_key ) {
$result->product_ref = $result->package_slug . '/functions.php';
} elseif ( 'plugin' === $result->package_type ) {
$result->product_ref = $result->package_slug . '/' . $result->package_slug . '.php';
} elseif ( 'generic' === $result->package_type ) {
$result->product_ref = $result->package_slug . '/wppus.json';
}
}

Expand Down
4 changes: 2 additions & 2 deletions inc/class-wppus-nonce.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function deactivate() {

public static function uninstall() {}

public static function wp() {
public static function action_scheduler_init() {
$d = new DateTime( 'now', new DateTimeZone( wp_timezone_string() ) );

if ( ! as_has_scheduled_action( 'wppus_nonce_cleanup' ) ) {
Expand Down Expand Up @@ -172,7 +172,7 @@ public static function register() {

if ( ! self::is_doing_api_request() ) {
add_action( 'init', array( get_class(), 'add_endpoints' ), 10, 0 );
add_action( 'wp', array( get_class(), 'wp' ) );
add_action( 'action_scheduler_init', array( get_class(), 'action_scheduler_init' ) );
add_action( 'wppus_nonce_cleanup', array( get_class(), 'wppus_nonce_cleanup' ) );
}

Expand Down
23 changes: 4 additions & 19 deletions inc/class-wppus-package-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,7 @@ public function prime_package_from_remote() {
Wppus_Update_Server::unlock_update_from_remote( $slug );

$api = WPPUS_Update_API::get_instance();
$result = $api->download_remote_package( $slug, 'Theme', true );

if ( ! $result ) {
Wppus_Update_Server::unlock_update_from_remote( $slug );

$result = $api->download_remote_package( $slug, 'Plugin', true );
}
$result = $api->download_remote_package( $slug, null, true );
} else {
$error = new WP_Error(
__METHOD__,
Expand Down Expand Up @@ -580,10 +574,8 @@ public function get_package_info( $slug ) {
if ( $package ) {
$package_info = $package->getMetadata();

if ( isset( $package_info['details_url'] ) ) {
$package_info['type'] = 'theme';
} else {
$package_info['type'] = 'plugin';
if ( ! isset( $package_info['type'] ) ) {
$package_info['type'] = 'unknown';
}

$package_info['file_name'] = $package_info['slug'] . '.zip';
Expand Down Expand Up @@ -658,14 +650,7 @@ public function get_batch_package_info( $search = false ) {
);

if ( $include ) {
$packages[ $meta['slug'] ] = $meta;

if ( isset( $meta['details_url'] ) ) {
$packages[ $meta['slug'] ]['type'] = 'theme';
} else {
$packages[ $meta['slug'] ]['type'] = 'plugin';
}

$packages[ $meta['slug'] ] = $meta;
$packages[ $meta['slug'] ]['file_name'] = $meta['slug'] . '.zip';
$packages[ $meta['slug'] ]['file_size'] = $package->getFileSize();
$packages[ $meta['slug'] ]['file_last_modified'] = $package->getLastModified();
Expand Down
52 changes: 7 additions & 45 deletions inc/class-wppus-remote-sources-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ public function __construct( $init_hooks = false ) {
if ( $init_hooks ) {

if ( get_option( 'wppus_use_remote_repository' ) ) {
add_action( 'init', array( $this, 'register_remote_check_scheduled_hooks' ), 10, 0 );
add_action( 'action_scheduler_init', array( $this, 'register_remote_check_scheduled_hooks' ), 10, 0 );
} else {
add_action( 'init', array( $this, 'clear_remote_check_scheduled_hooks' ), 10, 0 );
}

add_action( 'wp_ajax_wppus_force_clean', array( $this, 'force_clean' ), 10, 0 );
add_action( 'wp_ajax_wppus_force_register', array( $this, 'force_register' ), 10, 0 );
add_action( 'wp_ajax_wppus_remote_repository_test', array( $this, 'remote_repository_test' ), 10, 0 );
add_action( 'admin_menu', array( $this, 'admin_menu' ), 15, 0 );
add_filter( 'wppus_admin_tab_links', array( $this, 'wppus_admin_tab_links' ), 15, 1 );
Expand Down Expand Up @@ -104,37 +103,6 @@ public function wppus_admin_tab_states( $states, $page ) {
return $states;
}

public function force_register() {
$result = false;
$type = false;

if ( isset( $_REQUEST['nonce'] ) && wp_verify_nonce( $_REQUEST['nonce'], 'wppus_plugin_options' ) ) {
$type = filter_input( INPUT_POST, 'type', FILTER_SANITIZE_FULL_SPECIAL_CHARS );

if ( 'schedules' === $type ) {
$result = self::register_schedules();
}
}

if ( $result && $type ) {
wp_send_json_success();
} elseif ( 'schedules' === $type ) {
$error = new WP_Error(
__METHOD__,
__( 'Error - check the packages directory is readable and not empty', 'wppus' )
);

wp_send_json_error( $error );
} else {
$error = new WP_Error(
__METHOD__,
__( 'Error - an unknown error has occured', 'wppus' ) . ': type = "' . $type . '" ; result = "' . $result . '"'
);

wp_send_json_error( $error );
}
}

public function force_clean() {
$result = false;
$type = false;
Expand All @@ -144,6 +112,12 @@ public function force_clean() {

if ( 'schedules' === $type ) {
$result = $this->clear_remote_check_scheduled_hooks();

if ( $result ) {
$this->reschedule_remote_check_recurring_events(
get_option( 'wppus_remote_repository_check_frequency', 'daily' )
);
}
}
}

Expand Down Expand Up @@ -284,18 +258,6 @@ public static function clear_schedules() {
return $manager->clear_remote_check_scheduled_hooks();
}

public static function register_schedules() {
$manager = new self();
$result = false;

if ( apply_filters( 'wppus_use_recurring_schedule', true ) ) {
$frequency = get_option( 'wppus_remote_repository_check_frequency', 'daily' );
$result = $manager->reschedule_remote_check_recurring_events( $frequency );
}

return $result;
}

public function reschedule_remote_check_recurring_events( $frequency ) {

if ( WPPUS_Update_API::is_doing_api_request() ) {
Expand Down
Loading

0 comments on commit 886ffe6

Please sign in to comment.