first commit

This commit is contained in:
2023-09-12 21:41:04 +02:00
commit 3361a7f053
13284 changed files with 2116755 additions and 0 deletions

View File

@@ -0,0 +1,110 @@
<?php
namespace OTGS\Installer\Upgrade;
class AutoUpgrade {
/**
* @var \WP_Installer $installer
*/
private $installer;
/**
* @var \OTGS_Installer_Plugin_Finder
*/
private $installerPluginsFinder;
/**
* @var InstallerPlugins
*/
private $installerPlugins;
public function __construct( \WP_Installer $installer, \OTGS_Installer_Plugin_Finder $installerPluginsFinder, InstallerPlugins $installerPlugins ) {
$this->installer = $installer;
$this->installerPluginsFinder = $installerPluginsFinder;
$this->installerPlugins = $installerPlugins;
}
public function addHooks() {
add_filter( 'pre_update_site_option_auto_update_plugins', [
$this,
'modifyAutoUpdatePluginsOption',
], 10, 2 );
add_filter( 'plugin_auto_update_setting_html', [ $this, 'modifyAutoUpdateSettingHtml' ], 10, 3 );
}
public function modifyAutoUpdatePluginsOption( $value, $oldValue ) {
$enabled = array_diff( $value, $oldValue );
$disabled = array_diff( $oldValue, $value );
$pluginFile = reset( $enabled ) ?: reset( $disabled );
foreach ( $this->installerPlugins->getFilteredInstallerPlugins() as $repositoryId => $installedRepositoryPlugins ) {
$pluginData = $this->installerPlugins->getPluginData( $repositoryId, $pluginFile );
if ( $pluginData ) {
$pluginObj = $this->installerPluginsFinder->get_plugin( $pluginData['slug'], $repositoryId );
if ( ! $pluginObj || $this->installer->plugin_is_registered( $repositoryId, $pluginData['slug'] )
&& $pluginObj->get_external_repo() && $this->installer->plugin_is_registered( $pluginObj->get_external_repo(), $pluginData['slug'] ) ) {
continue;
}
$installedRepositoryPluginIds = array_map( function ( $plugin ) {
return $plugin['id'];
}, $installedRepositoryPlugins );
if ( array_intersect( $enabled, $installedRepositoryPluginIds ) ) {
$this->updateInstallerAutoUpdateSetting( $repositoryId, true );
$value = array_unique( array_merge( $value, $installedRepositoryPluginIds ) );
} elseif ( array_intersect( $disabled, $installedRepositoryPluginIds ) ) {
$this->updateInstallerAutoUpdateSetting( $repositoryId, false );
$value = array_diff( $value, $installedRepositoryPluginIds );
}
}
}
return $value;
}
public function modifyAutoUpdateSettingHtml( $html, $pluginFile ) {
foreach ( $this->installerPlugins->getFilteredInstallerPlugins() as $repositoryId => $installedRepositoryPlugins ) {
$pluginData = $this->installerPlugins->getPluginData( $repositoryId, $pluginFile );
if ( $pluginData ) {
$pluginObj = $this->installerPluginsFinder->get_plugin( $pluginData['slug'], $repositoryId );
if ( ! $this->installer->plugin_is_registered( $repositoryId, $pluginData['slug'] ) ) {
if ( ( ! $pluginObj || $pluginObj->has_fallback_on_wporg() || $pluginObj->get_external_repo() && $this->installer->plugin_is_registered( $pluginObj->get_external_repo(), $pluginData['slug'] ) )
|| $this->installer->plugin_is_registered( 'wpml', $pluginData['slug'] ) ) {
continue;
}
$html = $this->getRegisterMessage( $repositoryId );
}
}
}
return $html;
}
private function updateInstallerAutoUpdateSetting( $repositoryId, $value ) {
if ( ! isset( $this->installer->settings['repositories'][ $repositoryId ]['auto_update'] )
|| $this->installer->settings['repositories'][ $repositoryId ]['auto_update'] !== $value ) {
$this->installer->settings['repositories'][ $repositoryId ]['auto_update'] = $value;
$this->installer->save_settings();
}
}
/**
* @param string $repositoryId
*
* @return string
*/
private function getRegisterMessage( $repositoryId ) {
$url = $this->installer->menu_url() . '&repository=' . $repositoryId . '&action=register';
$linkText = __( 'Register', 'installer' );
$text = __( ' to use auto-updates', 'installer' );
return '<a href="' . esc_url( $url ) . '">' . esc_html( $linkText ) . '</a>' . esc_html( $text );
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace OTGS\Installer\Upgrade;
class InstallerPlugins {
/**
* @var \WP_Installer $installer
*/
private $installer;
/**
* @var array
*/
private $filteredInstallerPlugins;
public function __construct( \WP_Installer $installer, \OTGS_Installer_Plugin_Finder $installerPluginsFinder ) {
$this->installer = $installer;
$this->filteredInstallerPlugins = $this->filterInstallerPlugins($installerPluginsFinder);
}
/**
* @return array
*/
public function getFilteredInstallerPlugins() {
return $this->filteredInstallerPlugins;
}
/**
* @return array
*/
private function filterInstallerPlugins(\OTGS_Installer_Plugin_Finder $installerPluginsFinder) {
$filteredInstallerPlugins = [];
foreach ( $installerPluginsFinder->get_otgs_installed_plugins_by_repository() as $repositoryId => $installedRepositoryPlugins ) {
foreach ( $installedRepositoryPlugins as $installedRepositoryPlugin ) {
$pluginObj = $installerPluginsFinder->get_plugin( $installedRepositoryPlugin['slug'], $repositoryId );
if ( !$pluginObj || $pluginObj->get_external_repo() && $this->installer->plugin_is_registered( $pluginObj->get_external_repo(), $installedRepositoryPlugin['slug'] ) ) {
continue;
}
$filteredInstallerPlugins[ $repositoryId ][] = $installedRepositoryPlugin;
}
}
return $filteredInstallerPlugins;
}
/**
* @param $repositoryId
* @param $pluginId
*
* @return array|null
*/
public function getPluginData( $repositoryId, $pluginId ) {
return current( array_filter( $this->filteredInstallerPlugins[ $repositoryId ], function ( $plugin ) use ( $pluginId ) {
return $plugin['id'] === $pluginId;
} ) );
}
}

View File

@@ -0,0 +1,165 @@
<?php
class OTGS_Installer_Upgrade_Response {
/**
* @var array
*/
private $plugins;
/**
* @var array
*/
private $repositories;
/**
* @var OTGS_Installer_Source_Factory
*/
private $source_factory;
/**
* @var OTGS_Installer_Package_Product_Finder
*/
private $product_finder;
public function __construct( array $plugins, OTGS_Installer_Repositories $repositories, OTGS_Installer_Source_Factory $source_factory, OTGS_Installer_Package_Product_Finder $product_finder ) {
$this->plugins = $plugins;
$this->repositories = $repositories;
$this->source_factory = $source_factory;
$this->product_finder = $product_finder;
}
public function add_hooks() {
if ( defined( 'DOING_AJAX' ) && isset( $_POST['action'] ) && 'installer_download_plugin' === $_POST['action'] ) {
add_filter( 'site_transient_update_plugins', array( $this, 'modify_upgrade_response' ) );
}
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'modify_upgrade_response' ) );
}
public function modify_upgrade_response( $update_plugins ) {
if ( isset( $update_plugins ) && is_object( $update_plugins ) ) {
foreach ( $this->plugins as $plugin ) {
$repository = $this->repositories->get( $plugin->get_repo() );
$subscription = $repository->get_subscription();
$response = (object) [
'id' => $plugin->get_id(),
'slug' => $plugin->get_slug(),
'plugin' => $plugin->get_id(),
'new_version' => $plugin->get_version(),
];
if ( $this->should_skip_upgrade_process( $plugin, $update_plugins, $repository ) ) {
$update_plugins->no_update[$plugin->get_id() ] = $response;
continue;
}
$response->upgrade_notice = '';
$response->url = $plugin->get_url();
$response->tested = $plugin->get_tested();
if ( $subscription->get_site_key() ) {
$response->package = $this->append_site_key_to_download_url(
$plugin->get_url(),
$subscription->get_site_key(),
$repository->get_id(),
WP_Installer()->get_installer_site_url()
);
}
$response = apply_filters( 'otgs_installer_upgrade_check_response', $response, $plugin->get_name(), $repository->get_id() );
$update_plugins->checked[ $plugin->get_id() ] = $plugin->get_installed_version();
$update_plugins->response[ $plugin->get_id() ] = $response;
}
}
return $update_plugins;
}
private function should_skip_upgrade_process( OTGS_Installer_Plugin $plugin, $update_plugins, OTGS_Installer_Repository $repository ) {
$has_wp_org_update = isset( $update_plugins->response[ $plugin->get_id() ] );
$subscription = $repository->get_subscription();
$needs_upgrade = $plugin->get_installed_version() && version_compare( $plugin->get_version(), $plugin->get_installed_version(), '>' ) || ! empty( $_POST['reset_to_channel'] );
if ( ! $needs_upgrade ) {
return true;
}
if ( ! $subscription || ! $subscription->is_valid() ) {
return true;
}
if ( isset( $update_plugins->response[ $plugin->get_id() ] ) ) {
return true;
}
if ( $this->is_plugin_registered_on_external_repo( $plugin ) ) {
return true;
}
$product = $this->product_finder->get_product_in_repository_by_subscription( $repository );
if ( ! $product || ! $product->is_plugin_registered( $plugin->get_slug() ) ) {
return true;
}
if ( $subscription && $this->should_fallback_under_wp_org_repo( $plugin, $subscription->get_site_key() ) && $has_wp_org_update ) {
return true;
}
return false;
}
private function is_plugin_registered_on_external_repo( OTGS_Installer_Plugin $plugin ) {
$repository = $this->repositories->get( $plugin->get_external_repo() );
if ( $repository ) {
$product = $this->product_finder->get_product_in_repository_by_subscription( $repository );
if ( $product && $plugin->get_external_repo() && $product->is_plugin_registered( $plugin->get_slug() ) ) {
return true;
}
}
return false;
}
private function append_site_key_to_download_url( $url, $key, $repository_id, $site_url ) {
$url_params['site_key'] = $key;
$url_params['site_url'] = $site_url;
$url = add_query_arg( $url_params, $url );
if ( 'wpml' === $repository_id ) {
$url = add_query_arg( array(
'using_icl' => function_exists( 'wpml_site_uses_icl' ) && wpml_site_uses_icl(),
'wpml_version' => defined( 'ICL_SITEPRESS_VERSION' ) ? ICL_SITEPRESS_VERSION : ''
), $url );
}
return $url;
}
private function get_extra_url_parameters( $source ) {
$parameters = array();
if ( $source ) {
foreach ( $source as $key => $val ) {
$parameters[ $key ] = $val;
}
}
$parameters['installer_version'] = WP_INSTALLER_VERSION;
$parameters['theme'] = wp_get_theme()->get( 'Name' );
$parameters['site_name'] = get_bloginfo( 'name' );
return $parameters;
}
private function should_fallback_under_wp_org_repo( OTGS_Installer_Plugin $plugin, $site_key ) {
return ( $plugin->is_free_on_wporg() || $plugin->has_fallback_on_wporg() && $plugin->has_fallback_on_wporg() && ! $site_key ) && $plugin->get_channel() === WP_Installer_Channels::CHANNEL_PRODUCTION;
}
}