LLMS_Helper_Upgrader::install_addon( string|obj $addon_or_id, string $action = 'install' )
Install an add-on from LifterLMS.com
Parameters Parameters
- $addon_or_id
-
(string|obj) (Required) ID for the add-on or an instance of the LLMS_Add_On.
- $action
-
(string) (Optional) Installation type [install|update].
Default value: 'install'
Return Return
(WP_Error|true)
Source Source
File: libraries/lifterlms-helper/includes/class-llms-helper-upgrader.php
public function install_addon( $addon_or_id, $action = 'install' ) {
// Setup the addon.
$addon = is_a( $addon_or_id, 'LLMS_Add_On' ) ? $addon_or_id : llms_get_add_on( $addon_or_id );
if ( ! $addon ) {
return new WP_Error( 'invalid_addon', __( 'Invalid add-on ID.', 'lifterlms' ) );
}
if ( ! in_array( $action, array( 'install', 'update' ), true ) ) {
return new WP_Error( 'invalid_action', __( 'Invalid action.', 'lifterlms' ) );
}
if ( ! $addon->is_installable() ) {
return new WP_Error( 'not_installable', __( 'Add-on cannot be installable.', 'lifterlms' ) );
}
// Make sure it's not already installed.
if ( 'install' === $action && $addon->is_installed() ) {
// Translators: %s = Add-on name.
return new WP_Error( 'installed', sprintf( __( '%s is already installed', 'lifterlms' ), $addon->get( 'title' ) ) );
}
// Get download info via llms.com api.
$dl_info = $addon->get_download_info();
if ( is_wp_error( $dl_info ) ) {
return $dl_info;
}
if ( ! isset( $dl_info['data']['url'] ) ) {
return new WP_Error( 'no_url', __( 'An error occured while attempting to retrieve add-on download information. Please try again.', 'lifterlms' ) );
}
require_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
WP_Filesystem();
$skin = new Automatic_Upgrader_Skin();
if ( 'plugin' === $addon->get_type() ) {
$upgrader = new Plugin_Upgrader( $skin );
} elseif ( 'theme' === $addon->get_type() ) {
$upgrader = new Theme_Upgrader( $skin );
} else {
return new WP_Error( 'inconceivable', __( 'The requested action is not possible.', 'lifterlms' ) );
}
if ( 'install' === $action ) {
remove_filter( 'upgrader_package_options', array( $this, 'upgrader_package_options' ) );
$result = $upgrader->install( $dl_info['data']['url'] );
add_filter( 'upgrader_package_options', array( $this, 'upgrader_package_options' ) );
} elseif ( 'update' === $action ) {
$result = $upgrader->upgrade( $addon->get( 'update_file' ) );
}
if ( is_wp_error( $result ) ) {
return $result;
} elseif ( is_wp_error( $skin->result ) ) {
return $skin->result;
} elseif ( is_null( $result ) ) {
return new WP_Error( 'filesystem', __( 'Unable to connect to the filesystem. Please confirm your credentials.', 'lifterlms' ) );
}
return true;
}
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
| Version | Description |
|---|---|
| 3.4.0 | Use core textdomain. |
| 3.2.0 | Use strict comparison for in_array(). |
| 3.0.0 | Introduced. |