Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
cd27da7
Use installation strings when installing (#182)
costdev Jul 23, 2025
6787ed4
refactor get_update_data, will need filename from Metadata
afragen Jul 23, 2025
6d0405c
just use namespace, not functions
afragen Jul 23, 2025
6ab3dee
use namespace not functions
afragen Jul 23, 2025
7f4fc4d
move some functions to FAIR\Packages
afragen Jul 23, 2025
f6e2e7d
move function to FAIR\Packages
afragen Jul 24, 2025
7b501f1
Updater::$did already exists
afragen Jul 24, 2025
9ae222f
return WP_Error
afragen Jul 24, 2025
dceae83
remove extra steps
afragen Jul 24, 2025
2c7b1e5
new function get_hashed_filename
afragen Jul 24, 2025
89e1a19
use get_hashed_filename() here
afragen Jul 24, 2025
f987b2b
Merge branch 'refactor-get-update-data' into ajaxify_the_install_button
afragen Jul 24, 2025
15fc951
fix docblock
afragen Jul 24, 2025
c013c65
get DID and slug for renaming, use get_update_data()
afragen Jul 24, 2025
5b4ebd6
fix renaming on install
afragen Jul 24, 2025
8124652
just get metadata from DID
afragen Jul 24, 2025
fceb978
get metadata then release
afragen Jul 24, 2025
d862307
Merge branch 'refactor-get-update-data' into ajaxify_the_install_button
afragen Jul 24, 2025
cd66e0a
fix default
afragen Jul 25, 2025
ce10b3a
use variable for repetition
afragen Jul 25, 2025
0a89a8e
Merge branch 'refactor-get-update-data' into ajaxify_the_install_button
afragen Jul 25, 2025
bc7c916
null coalescing shit
afragen Jul 25, 2025
98da209
Make sure we skip if not AJAX
afragen Jul 25, 2025
34d861d
run upgrader_source_selection during Packages\Admin\bootstrap
afragen Jul 25, 2025
d7b60c3
original comment
afragen Jul 25, 2025
a5ce83c
add filters where needed and cleanup rest
afragen Jul 25, 2025
a1c7ea3
set $_POST['slug'] to hashed slug
afragen Jul 25, 2025
437caeb
cleanup aisle 3
afragen Jul 25, 2025
f7142a0
more info
afragen Jul 25, 2025
39e1c5b
typo
afragen Jul 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions inc/packages/admin/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
const TAB_DIRECT = 'fair_direct';
const ACTION_INSTALL = 'fair-install-plugin';
const ACTION_INSTALL_NONCE = 'fair-install-plugin';
const ACTION_INSTALL_DID = 'fair-install-did';

/**
* Bootstrap.
Expand All @@ -29,10 +30,12 @@ function bootstrap() {

add_filter( 'install_plugins_tabs', __NAMESPACE__ . '\\add_direct_tab' );
add_filter( 'plugins_api', __NAMESPACE__ . '\\handle_did_during_ajax', 10, 3 );
add_filter( 'upgrader_pre_download', 'FAIR\\Packages\\upgrader_pre_download', 10, 1 );
add_action( 'install_plugins_' . TAB_DIRECT, __NAMESPACE__ . '\\render_tab_direct' );
add_action( 'load-plugin-install.php', __NAMESPACE__ . '\\load_plugin_install' );
add_action( 'install_plugins_pre_plugin-information', __NAMESPACE__ . '\\maybe_hijack_plugin_info', 0 );
add_action( 'update-custom_' . ACTION_INSTALL, __NAMESPACE__ . '\\handle_direct_install' );
add_action( 'wp_ajax_check_plugin_dependencies', __NAMESPACE__ . '\\set_slug_to_hashed' );
}

/**
Expand All @@ -51,7 +54,7 @@ function add_direct_tab( $tabs ) {
*
* @param mixed $result The result of the plugins_api call.
* @param string $action The action being performed.
* @param array $args The arguments passed to the plugins_api call.
* @param object $args The arguments passed to the plugins_api call.
* @return mixed
*/
function handle_did_during_ajax( $result, $action, $args ) {
Expand All @@ -69,14 +72,11 @@ function handle_did_during_ajax( $result, $action, $args ) {
return $result;
}

$release = Updater\get_latest_release_from_did( $did );
if ( is_wp_error( $release ) ) {
return $release;
}
wp_cache_set( ACTION_INSTALL_DID, $did );
Updater\add_package_to_release_cache( $did );
add_filter( 'http_request_args', 'FAIR\\Updater\\maybe_add_accept_header', 20, 2 );

return (object) [
'download_link' => $release->artifacts->package[0]->url,
];
return (object) Packages\get_update_data( $did );
}

/**
Expand Down Expand Up @@ -235,11 +235,34 @@ function handle_direct_install() {
}

$skin = new WP_Upgrader_Skin();
$res = Packages\install_plugin( $id, $skin, $version );
var_dump( $res ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_dump
Packages\install_plugin( $id, $skin, $version );
exit;
}

/**
* Set slug to hashed slug from escaped slug-did.
*
* Needed for check_plugin_dependencies_during_ajax().
*
* @return void
*/
function set_slug_to_hashed() : void {
// phpcs:disable HM.Security.NonceVerification.Missing
if ( ! isset( $_POST['slug'] ) ) {
return;
}

$escaped_slug = sanitize_text_field( wp_unslash( $_POST['slug'] ) );
$did = 'did:' . explode( '-did:', str_replace( '--', ':', $escaped_slug ), 2 )[1];
if ( ! preg_match( '/^did:(web|plc):.+$/', $did ) ) {
return;
}

// Reset to proper hashed slug.
$_POST['slug'] = explode( '-did--', $escaped_slug, 2 )[0] . '-' . Packages\get_did_hash( $did );
// phpcs:enable
}

/**
* Hijack embedded info page.
*
Expand Down
8 changes: 8 additions & 0 deletions inc/packages/class-metadatadocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class MetadataDocument {
*/
public $slug;

/**
* File name.
*
* @var string
*/
public $filename;

/**
* License.
*
Expand Down Expand Up @@ -124,6 +131,7 @@ public static function from_data( stdClass $data ) {
$optional = [
'name',
'slug',
'filename',
'description',
'keywords',
'sections',
Expand Down
72 changes: 45 additions & 27 deletions inc/packages/class-upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ protected function run_install( string $destination, $options ) {
$artifact = pick_artifact_by_lang( $this->release->artifacts->package );

add_package_to_release_cache( $this->package->id );
add_filter( 'upgrader_pre_download', 'FAIR\\Updater\\upgrader_pre_download', 10, 1 );

// Download the package.
$path = $this->download_package( $artifact->url, false, $options['hook_extra'] );
Expand Down Expand Up @@ -442,7 +441,7 @@ public function install_theme( $clear_cache, $overwrite ) {
*/
public function install( MetadataDocument $package, ReleaseDocument $release, $clear_cache = true, $overwrite = false ) {
$this->init();
// $this->install_strings();
$this->install_strings();

$this->package = $package;
$this->release = $release;
Expand All @@ -458,6 +457,50 @@ public function install( MetadataDocument $package, ReleaseDocument $release, $c
return new WP_Error( 'fair.packages.upgrader.install.invalid_type', 'Invalid package type.' );
}
}

/**
* Retrieves the hashed path to the file that contains the plugin info.
*
* This isn't used internally in the class, but is called by the skins.
*
* @since WordPress 2.8.0
*
* @return string|false The full path to the main plugin file, or false.
*/
public function plugin_info() {
if ( ! isset( $this->package ) ) {
return false;
}

return get_hashed_filename( $this->package );
}

/**
* Gets the WP_Theme object for a theme.
*
* @since WordPress 2.8.0
* @since WordPress 3.0.0 The `$theme` argument was added.
*
* @param string $theme The directory name of the theme. This is optional, and if not supplied,
* the directory name from the last result will be used.
* @return WP_Theme|false The theme's info object, or false `$theme` is not supplied
* and the last result isn't set.
*/
public function theme_info( $theme = null ) {
if ( empty( $theme ) ) {
if ( ! empty( $this->result['destination_name'] ) ) {
$theme = $this->result['destination_name'];
} else {
return false;
}
}

$theme = wp_get_theme( $theme );
$theme->cache_delete();

return $theme;
}

/**
* Checks that the source package contains a valid plugin.
*
Expand Down Expand Up @@ -663,29 +706,4 @@ protected function validate_theme( $dir ) {
$this->new_theme_data = $info;
}

/**
* Renames a package's directory when it doesn't match the slug.
*
* This is commonly required for packages from Git hosts.
*
* @param string $source Path of $source.
* @param string $remote_source Path of $remote_source.
*
* @return string
*/
public function rename_source_selection( string $source, string $remote_source ) {
global $wp_filesystem;

if ( str_contains( $source, get_did_hash( $this->package->id ) ) && basename( $source ) === $this->package->slug ) {
return $source;
}

$new_source = trailingslashit( $remote_source ) . $this->package->slug . '-' . get_did_hash( $this->package->id );

if ( trailingslashit( strtolower( $source ) ) !== trailingslashit( strtolower( $new_source ) ) ) {
$wp_filesystem->move( $source, $new_source, true );
}

return trailingslashit( $new_source );
}
}
Loading