Skip to content

Add fix for PHP Fatal error: Uncaught TypeError: explode(): Argument #2 ($string) must be of type string #3

@OussamaKhoubran

Description

@OussamaKhoubran
public function sanitize_settings(array $settings): array
	{
		// If the restore default settings checkbox is checked, restore the default settings.
		if (isset($settings['restore_default_settings']['restore_default_settings'])) {
			return array();
		}

		// Sanitize the general settings.
		$settings['general_settings']['disable_wp_frontend'] = isset($settings['general_settings']['disable_wp_frontend']);

		// Sanitize the path whitelist. Take in the string from the textarea and split it into an array by new line. Make sure the array items are sanitized.
		// $settings['exception_settings']['path_whitelist'] = array_map('sanitize_text_field', explode("\n", $settings['exception_settings']['path_whitelist']));
		// Sanitize the path whitelist: allow either a string (from textarea) or an array (already exploded).
		$raw = $settings['exception_settings']['path_whitelist'] ?? '';

		// If it’s a string, explode on newline; if it’s already an array, leave it.
		if (is_array($raw)) {
			$lines = $raw;
		} else {
			// normalize CRLF to LF then split
			$lines = preg_split("/\r\n|\n|\r/", (string) $raw);
		}

		// Trim out any empty lines and sanitize each entry
		$settings['exception_settings']['path_whitelist'] = array_values(array_filter(
			array_map('sanitize_text_field', $lines),
			// remove blank entries
			fn($line) => (string) $line !== ''
		));

		return $settings;
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions