PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.6.4
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.6.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.6.4, at classes/Plugin/Upgrader.php

210 lines 5.7 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 * 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. Sanity check to ensure install method exists.
147 // @phpstan-ignore-next-line .
148 if ( ! method_exists( $installer, 'install' ) ) {
149 return new \WP_Error( 'content_control_upgrader', __( 'Upgrader missing install method.', 'content-control' ) );
150 }
151
152 $this->debug_log( 'Installing plugin from file: ' . $file );
153
154 $plugin = $installer->install( $file, [
155 'overwrite_package' => true,
156 ] );
157
158 if ( is_wp_error( $plugin ) && 'folder_exists' === $plugin->get_error_code() ) {
159 $this->debug_log( 'Plugin already exists, upgrading instead.' );
160
161 // Plugin already exists, upgrade it.
162 $plugin_basename = $installer->plugin_info();
163
164 // Filter get_site_transient( 'update_plugins' ) to replace $plugin_basename->package with $file.
165 add_filter( 'pre_site_transient_update_plugins', function ( $current ) use ( $file, $plugin_basename ) {
166 if ( isset( $current->response[ $plugin_basename ] ) ) {
167 $current->response[ $plugin_basename ]->package = $file;
168 } else {
169 $current->response[ $plugin_basename ] = (object) [
170 'id' => '0',
171 'slug' => $plugin_basename,
172 'new_version' => '0',
173 'url' => '',
174 'package' => $file,
175 ];
176 }
177
178 $this->debug_log( 'Filtering update_plugins transient to replace package with file.' );
179 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
180 $this->debug_log( 'Current: ' . print_r( $current->response[ $plugin_basename ], true ) );
181 return $current;
182 }, 10, 1 );
183
184 $this->debug_log( 'Upgrading plugin.' );
185 $upgraded = $installer->upgrade( $file );
186
187 if ( is_wp_error( $upgraded ) ) {
188 $this->debug_log( 'Error upgrading plugin: ' . $upgraded->get_error_message(), 'ERROR' );
189 return $upgraded;
190 }
191
192 return $upgraded;
193 }
194
195 if ( is_wp_error( $plugin ) ) {
196 $this->debug_log( 'Error installing plugin: ' . $plugin->get_error_message(), 'ERROR' );
197 return $plugin;
198 }
199
200 // Flush the cache and return the newly installed plugin basename.
201 wp_cache_flush();
202
203 $plugin_basename = $installer->plugin_info();
204
205 $this->debug_log( 'Plugin installed: ' . $plugin_basename );
206
207 return $this->activate_plugin( $plugin_basename );
208 }
209 }
210