[ 'disable_file_edit', 'disallow_file_mods', 'disable_script_concatenation', 'block_access_sensitive_files', 'block_directory_browsing', 'cors_header', 'hide_php_version', ], 'performance' => [ 'wp_memory_limit', 'wp_max_memory_limit', 'max_execution_time', 'autosave_interval', 'disable_wp_cron', ], 'posts' => [ 'empty_trash_days', 'number_posts_revisions', ], 'media' => [ 'upload_max_file_size', ], 'debug' => [ 'wp_debug', ], 'plugins' => [ 'plugin_download_enabled', ], ]; /** * Settings that write .htaccess on Apache but run in PHP for each site elsewhere, * so they only need locking where .htaccess is read. */ private const APACHE_LOCKED = [ 'security' => [ 'disable_xmlrpc', 'block_author_scans', 'block_specific_bots_enabled', 'block_specific_bots', 'block_specific_bots_custom', 'block_specific_countries_enabled', 'block_specific_countries', 'block_specific_countries_excluded_paths', ], ]; /** * Locked keys found while walking the fields, tab => keys, children included. * * @var array */ private array $locked_keys = []; public function __construct() { if( !is_multisite() ) { return; } add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ], 100 ); add_filter( 'adminease_validate_settings', [ $this, 'keep_locked_values' ], 30 ); } /** * Marks every locked field read-only and explains why under the parent field. * * @param array $fields The settings fields, by tab. * * @return array The fields with the locked ones disabled. */ public function adminease_settings_fields( array $fields ): array { $this->locked_keys = []; if( Utils::can_write_shared_files() ) { return $fields; } $locked = $this->get_locked_list(); foreach( $fields as $tab => $group ) { if( empty( $group['fields'] ) || !is_array( $group['fields'] ) ) { continue; } foreach( $group['fields'] as $index => $field ) { $key = self::get_field_key( $field, $tab ); if( '' === $key || !in_array( $key, $locked[ $tab ] ?? [], true ) ) { continue; } $fields[ $tab ]['fields'][ $index ] = $this->lock_field( $field, $tab, true ); } } return $fields; } /** * Puts the stored value back for every locked setting, whatever the request posted. * * @param mixed $sanitized_settings The settings about to be saved. * * @return mixed The settings, with locked values unchanged. */ public function keep_locked_values( $sanitized_settings ) { if( !is_array( $sanitized_settings ) || Utils::can_write_shared_files() ) { return $sanitized_settings; } if( empty( $this->locked_keys ) ) { Plugin::get_plugin_fields(); } $stored_settings = get_option( 'adminease_settings', [] ); if( !is_array( $stored_settings ) ) { $stored_settings = []; } foreach( $this->locked_keys as $tab => $keys ) { if( isset( $sanitized_settings[ $tab ] ) && !is_array( $sanitized_settings[ $tab ] ) ) { continue; } foreach( $keys as $key ) { if( isset( $stored_settings[ $tab ][ $key ] ) ) { $sanitized_settings[ $tab ][ $key ] = $stored_settings[ $tab ][ $key ]; } else { unset( $sanitized_settings[ $tab ][ $key ] ); } } } return $sanitized_settings; } /** * The locked keys for this server, by tab. * * @return array */ private function get_locked_list(): array { $locked = self::ALWAYS_LOCKED; if( Utils::is_apache_compatible() ) { foreach( self::APACHE_LOCKED as $tab => $keys ) { $locked[ $tab ] = array_merge( $locked[ $tab ] ?? [], $keys ); } } return $locked; } /** * Disables a field and all of its children, and records their keys. * * @param array $field The field definition. * @param string $tab The tab the field is stored under. * @param bool $is_parent Whether to add the explanation to this field. * * @return array The locked field. */ private function lock_field( array $field, string $tab, bool $is_parent ): array { $key = self::get_field_key( $field, $tab ); if( '' !== $key ) { $this->locked_keys[ $tab ][] = $key; } $field['attributes'] = is_array( $field['attributes'] ?? null ) ? $field['attributes'] : []; $field['attributes']['disabled'] = 'disabled'; if( $is_parent ) { $note = '' . esc_html__( 'This setting changes files every site on the network shares, so only a super admin can change it, from the main site.', 'adminease' ) . ''; $field['description'] = trim( (string) ( $field['description'] ?? '' ) . ' ' . $note ); } if( !empty( $field['child_fields'] ) && is_array( $field['child_fields'] ) ) { foreach( $field['child_fields'] as $index => $child_field ) { if( is_array( $child_field ) ) { $field['child_fields'][ $index ] = $this->lock_field( $child_field, $tab, false ); } } } return $field; } /** * Removes every setting that changes a shared file, children included, whatever the server. * * @param array $settings The settings, by tab. * * @return array The settings without the shared-file ones. */ public static function remove_shared_settings( array $settings ): array { foreach( self::get_shared_fields() as [ $tab, $field ] ) { $settings = self::remove_field_settings( $settings, $field, $tab ); } return $settings; } /** * The fields that change a shared file, whatever the server, as [ tab, field ] pairs. * * Kept for the request: Pro compares every site of a network with this list, and * building all the fields once per site is slow. * * @return array */ private static function get_shared_fields(): array { static $shared_fields = null; if( null !== $shared_fields ) { return $shared_fields; } $locked = self::ALWAYS_LOCKED; foreach( self::APACHE_LOCKED as $tab => $keys ) { $locked[ $tab ] = array_merge( $locked[ $tab ] ?? [], $keys ); } $shared_fields = []; foreach( Plugin::get_plugin_fields() as $tab => $group ) { if( empty( $group['fields'] ) || !is_array( $group['fields'] ) ) { continue; } foreach( $group['fields'] as $field ) { if( is_array( $field ) && in_array( self::get_field_key( $field, $tab ), $locked[ $tab ] ?? [], true ) ) { $shared_fields[] = [ (string) $tab, $field ]; } } } return $shared_fields; } /** * Removes a field's setting and those of its children. * * @param array $settings The settings, by tab. * @param array $field The field definition. * @param string $tab The tab the field is stored under. * * @return array The settings without the field. */ private static function remove_field_settings( array $settings, array $field, string $tab ): array { $key = self::get_field_key( $field, $tab ); if( '' !== $key && isset( $settings[ $tab ] ) && is_array( $settings[ $tab ] ) ) { unset( $settings[ $tab ][ $key ] ); } if( !empty( $field['child_fields'] ) && is_array( $field['child_fields'] ) ) { foreach( $field['child_fields'] as $child_field ) { if( is_array( $child_field ) ) { $settings = self::remove_field_settings( $settings, $child_field, $tab ); } } } return $settings; } /** * Reads the setting key from a field's name, adminease[tab][key]. * * @param array $field The field definition. * @param string $tab The tab the field must belong to. * * @return string The key, or '' when the field stores nothing under this tab. */ private static function get_field_key( array $field, string $tab ): string { $name = (string) ( $field['name'] ?? '' ); if( !preg_match( '/^adminease\[([a-z0-9_-]+)\]\[([a-z0-9_]+)\]/', $name, $matches ) || $matches[1] !== $tab ) { return ''; } return $matches[2]; } }