| 1 |
<?php |
| 2 |
/** |
| 3 |
* Content Control Upgrades |
| 4 |
* |
| 5 |
* @package ContentControl\Plugin |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace ContentControl\Plugin; |
| 9 |
|
| 10 |
use ContentControl\Base\Container; |
| 11 |
|
| 12 |
/** |
| 13 |
* Upgrader class. |
| 14 |
* |
| 15 |
* NOTE: For wordpress.org admins: This is only used if: |
| 16 |
* - The user explicitly entered a license key. |
| 17 |
* - They further opened a window to our site allowing the user to authorize the connection & installation of the pro upgrade. |
| 18 |
* |
| 19 |
* @package ContentControl |
| 20 |
*/ |
| 21 |
class Upgrader { |
| 22 |
|
| 23 |
/** |
| 24 |
* Container. |
| 25 |
* |
| 26 |
* @var Container |
| 27 |
*/ |
| 28 |
private $c; |
| 29 |
|
| 30 |
/** |
| 31 |
* Initialize license management. |
| 32 |
* |
| 33 |
* @param Container $c Container. |
| 34 |
*/ |
| 35 |
public function __construct( $c ) { |
| 36 |
$this->c = $c; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Maybe load functions & classes required for upgrade. |
| 41 |
* |
| 42 |
* Purely here due to prevent possible random errors. |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public function maybe_load_required_files() { |
| 47 |
if ( ! function_exists( 'request_filesystem_credentials' ) ) { |
| 48 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! function_exists( 'get_plugin_data' ) ) { |
| 52 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Log a message to the debug log if enabled. |
| 58 |
* |
| 59 |
* Here to prevent constant conditional checks for the debug mode. |
| 60 |
* |
| 61 |
* @param string $message Message. |
| 62 |
* @param string $type Type. |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function debug_log( $message, $type = 'INFO' ) { |
| 67 |
if ( defined( 'CONTENT_CONTROL_UPGRADE_DEBUG_LOGGING' ) && CONTENT_CONTROL_UPGRADE_DEBUG_LOGGING ) { |
| 68 |
$this->c->get( 'logging' )->log( "Plugin\Upgrader.$type: $message" ); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get credentials for the current request. |
| 74 |
* |
| 75 |
* @return bool |
| 76 |
*/ |
| 77 |
public function get_fs_creds() { |
| 78 |
// Prepare variables. |
| 79 |
$url = esc_url_raw( |
| 80 |
add_query_arg( |
| 81 |
[ |
| 82 |
'page' => 'content-control-settings', |
| 83 |
'view' => 'upgrade', |
| 84 |
], |
| 85 |
admin_url( 'options-general.php' ) |
| 86 |
) |
| 87 |
); |
| 88 |
|
| 89 |
$creds = request_filesystem_credentials( $url, '', false, '', null ); |
| 90 |
|
| 91 |
if ( false === $creds || ! WP_Filesystem( $creds ) ) { |
| 92 |
$this->debug_log( 'Unable to get filesystem credentials.', 'ERROR' ); |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
return (bool) $creds; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Activate a plugin. |
| 101 |
* |
| 102 |
* @param string $plugin_basename The plugin basename. |
| 103 |
* @return bool|\WP_Error |
| 104 |
*/ |
| 105 |
public function activate_plugin( $plugin_basename ) { |
| 106 |
if ( empty( $plugin_basename ) ) { |
| 107 |
return new \WP_Error( 'content_control_plugin_basename', __( 'Plugin basename empty.', 'content-control' ) ); |
| 108 |
} |
| 109 |
|
| 110 |
// Activate the plugin silently. |
| 111 |
$activated = activate_plugin( $plugin_basename, '', false, true ); |
| 112 |
|
| 113 |
if ( is_wp_error( $activated ) ) { |
| 114 |
$this->debug_log( 'Plugin failed to activate: ' . $activated->get_error_message() ); |
| 115 |
return $activated; |
| 116 |
} |
| 117 |
|
| 118 |
$this->debug_log( 'Plugin activated: ' . $plugin_basename ); |
| 119 |
|
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Install a plugin from file. |
| 125 |
* |
| 126 |
* @param string $file The plugin file. |
| 127 |
* |
| 128 |
* @return bool|\WP_Error |
| 129 |
*/ |
| 130 |
public function install_plugin( $file ) { |
| 131 |
// Load required files. |
| 132 |
$this->maybe_load_required_files(); |
| 133 |
|
| 134 |
// Check for file system permissions. |
| 135 |
if ( false === $this->get_fs_creds() ) { |
| 136 |
return new \WP_Error( 'content_control_fs_creds', __( 'Unable to get filesystem credentials.', 'content-control' ) ); |
| 137 |
} |
| 138 |
|
| 139 |
// Do not allow WordPress to search/download translations, as this will break JS output. |
| 140 |
remove_action( 'upgrader_process_complete', [ 'Language_Pack_Upgrader', 'async_upgrade' ], 20 ); |
| 141 |
|
| 142 |
$installer = new \ContentControl\Installers\PluginSilentUpgrader( new \ContentControl\Installers\Install_Skin() ); |
| 143 |
|
| 144 |
// 1. Check if the plugin exists already, if so upgrade it. |
| 145 |
|
| 146 |
// Error check. |
| 147 |
if ( ! method_exists( $installer, 'install' ) ) { |
| 148 |
return new \WP_Error( 'content_control_upgrader', __( 'Upgrader missing install method.', 'content-control' ) ); |
| 149 |
} |
| 150 |
|
| 151 |
$this->debug_log( 'Installing plugin from file: ' . $file ); |
| 152 |
|
| 153 |
$plugin = $installer->install( $file, [ |
| 154 |
'overwrite_package' => true, |
| 155 |
] ); |
| 156 |
|
| 157 |
if ( is_wp_error( $plugin ) && 'folder_exists' === $plugin->get_error_code() ) { |
| 158 |
$this->debug_log( 'Plugin already exists, upgrading instead.' ); |
| 159 |
|
| 160 |
// Plugin already exists, upgrade it. |
| 161 |
$plugin_basename = $installer->plugin_info(); |
| 162 |
|
| 163 |
// Filter get_site_transient( 'update_plugins' ) to replace $plugin_basename->package with $file. |
| 164 |
add_filter( 'pre_site_transient_update_plugins', function ( $current ) use ( $file, $plugin_basename ) { |
| 165 |
if ( isset( $current->response[ $plugin_basename ] ) ) { |
| 166 |
$current->response[ $plugin_basename ]->package = $file; |
| 167 |
} else { |
| 168 |
$current->response[ $plugin_basename ] = (object) [ |
| 169 |
'id' => '0', |
| 170 |
'slug' => $plugin_basename, |
| 171 |
'new_version' => '0', |
| 172 |
'url' => '', |
| 173 |
'package' => $file, |
| 174 |
]; |
| 175 |
} |
| 176 |
|
| 177 |
$this->debug_log( 'Filtering update_plugins transient to replace package with file.' ); |
| 178 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r |
| 179 |
$this->debug_log( 'Current: ' . print_r( $current->response[ $plugin_basename ], true ) ); |
| 180 |
return $current; |
| 181 |
}, 10, 1 ); |
| 182 |
|
| 183 |
$this->debug_log( 'Upgrading plugin.' ); |
| 184 |
$upgraded = $installer->upgrade( $file ); |
| 185 |
|
| 186 |
if ( is_wp_error( $upgraded ) ) { |
| 187 |
$this->debug_log( 'Error upgrading plugin: ' . $upgraded->get_error_message(), 'ERROR' ); |
| 188 |
return $upgraded; |
| 189 |
} |
| 190 |
|
| 191 |
return $upgraded; |
| 192 |
} |
| 193 |
|
| 194 |
if ( is_wp_error( $plugin ) ) { |
| 195 |
$this->debug_log( 'Error installing plugin: ' . $plugin->get_error_message(), 'ERROR' ); |
| 196 |
return $plugin; |
| 197 |
} |
| 198 |
|
| 199 |
// Flush the cache and return the newly installed plugin basename. |
| 200 |
wp_cache_flush(); |
| 201 |
|
| 202 |
$plugin_basename = $installer->plugin_info(); |
| 203 |
|
| 204 |
$this->debug_log( 'Plugin installed: ' . $plugin_basename ); |
| 205 |
|
| 206 |
return $this->activate_plugin( $plugin_basename ); |
| 207 |
} |
| 208 |
} |
| 209 |
|