PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.4
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.4
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / classes / Plugin / Upgrader.php

Upgrader.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 2.0.4, at classes/Plugin/Upgrader.php

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