PluginProbe
WooCommerce / 11.1.0
WooCommerce v11.1.0
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / src / Internal / Admin / RemoteFreeExtensions / ProcessCoreProfilerPluginInstallOptions.php
ProcessCoreProfilerPluginInstallOptions.php
176 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare( strict_types = 1 );
4
5 namespace Automattic\WooCommerce\Internal\Admin\RemoteFreeExtensions;
6
7 use WC_Logger_Interface;
8
9 /**
10 * Process install options for plugins.
11 */
12 class ProcessCoreProfilerPluginInstallOptions {
13 /**
14 * List of plugins.
15 *
16 * @var array List of plugins
17 */
18 private array $plugins;
19
20 /**
21 * Plugin slug.
22 *
23 * @var string Plugin slug
24 */
25 private string $slug;
26
27 /**
28 * Logger instance.
29 *
30 * @var WC_Logger_Interface Logger instance
31 */
32 private WC_Logger_Interface $logger;
33
34 private const DISALLOWED_OPTIONS = array(
35 'siteurl', // The URL to your WordPress installation.
36 'home', // The home URL of the site.
37 'admin_email', // Administrator email address.
38 'wp_user_roles', // Serialized roles and capabilities.
39 'active_plugins', // List of active plugins.
40 'template', // The current theme template.
41 'stylesheet', // The current theme stylesheet.
42 'default_role', // Default role for new users.
43 'ftp_hostname', // FTP server hostname.
44 'ftp_username', // FTP server username.
45 'ftp_password', // FTP server password.
46 'ftp_port', // FTP server port.
47 'ftp_ssl', // Whether to use FTP over SSL.
48 'ftp_pasv', // Whether to use passive FTP.
49 'rewrite_rules', // URL rewrite rules.
50 'permalink_structure', // Structure of permalinks.
51 'cron', // Scheduled tasks (WP-Cron jobs).
52 'upload_path', // Filesystem path for uploads.
53 'upload_url_path', // URL path for uploads.
54 'mailserver_url', // Mail server hostname.
55 'mailserver_login', // Mail server login.
56 'mailserver_pass', // Mail server password.
57 'mailserver_port', // Mail server port.
58 );
59
60 /**
61 * Constructor.
62 *
63 * @param array $plugins List of plugins.
64 * @param string $slug Plugin slug.
65 * @param WC_Logger_Interface|null $logger Logger instance.
66 */
67 public function __construct( array $plugins, string $slug, ?WC_Logger_Interface $logger = null ) {
68 $this->plugins = $plugins;
69 $this->slug = $slug;
70 $this->logger = $logger ?? wc_get_logger();
71 }
72
73 /**
74 * Retrieve install options for a plugin.
75 *
76 * @param string $plugin_slug Plugin slug.
77 * @return array|null Install options or null if not found.
78 */
79 public function get_install_options( string $plugin_slug ): ?array {
80 foreach ( $this->plugins as $plugin ) {
81 if ( $this->matches_plugin_slug( $plugin, $plugin_slug ) ) {
82 return $plugin->install_options ?? null;
83 }
84 }
85 return null;
86 }
87
88 /**
89 * Process install options based on a filtering function.
90 */
91 public function process_install_options() {
92 $install_options = $this->get_install_options( $this->slug );
93 if ( ! $install_options ) {
94 return;
95 }
96
97 foreach ( $install_options as $install_option ) {
98 $this->add_install_option( $install_option );
99 }
100 }
101
102 /**
103 * Updates an install option in the WordPress database.
104 *
105 * @param object $install_option Install option object.
106 */
107 protected function add_install_option( object $install_option ) {
108 $default_options = array(
109 'force_array' => false,
110 'autoload' => false,
111 );
112
113 $options = isset( $install_option->options )
114 ? (object) $install_option->options
115 : new \stdClass();
116
117 foreach ( $default_options as $key => $value ) {
118 if ( ! isset( $options->$key ) ) {
119 $options->$key = $value;
120 }
121 }
122
123 if ( $options->force_array ) {
124 $install_option->value = json_decode( wp_json_encode( $install_option->value ), true );
125 // In case of JSON error, return early.
126 if ( json_last_error() !== JSON_ERROR_NONE ) {
127 $this->logger && $this->logger->error( 'Failed to decode JSON for install option value for ' . $install_option->name . ': ' . json_last_error_msg() );
128 return;
129 }
130 }
131
132 $autoload = null;
133
134 if ( isset( $options->autoload ) ) {
135 if ( 'yes' === $options->autoload ) {
136 $autoload = true;
137 } elseif ( 'no' === $options->autoload ) {
138 $autoload = false;
139 } elseif ( true === $options->autoload || false === $options->autoload ) {
140 $autoload = $options->autoload;
141 }
142 }
143
144 $this->add_option( $install_option->name, $install_option->value, $autoload );
145 }
146
147 /**
148 * Updates an option in the WordPress database.
149 *
150 * @param string $name Option name.
151 * @param mixed $value Option value.
152 * @param string $autoload Autoload option.
153 *
154 * @return void
155 */
156 protected function add_option( string $name, $value, $autoload = null ) {
157 if ( in_array( $name, self::DISALLOWED_OPTIONS, true ) ) {
158 $this->logger && $this->logger->error( 'Disallowed option: ' . $name );
159 return;
160 }
161
162 add_option( $name, $value, '', $autoload );
163 }
164
165 /**
166 * Checks if the given plugin matches the provided slug.
167 *
168 * @param object $plugin Plugin object.
169 * @param string $plugin_slug Plugin slug.
170 * @return bool True if it matches, false otherwise.
171 */
172 private function matches_plugin_slug( object $plugin, string $plugin_slug ): bool {
173 return explode( ':', $plugin->key )[0] === $plugin_slug;
174 }
175 }
176