Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/preference filter #1509

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

mmcachran
Copy link
Contributor

@mmcachran mmcachran commented Oct 2, 2019

Description of the Change

Allow a preference query string to be added to the search request path to ensure search results have the same sorting. This avoids duplicate results when custom sorting is used.

Benefits

Allows an easier way for a preference query string to be added to the request path.

Verification Process

Use the following example filter to ensure the unique ID is the same throughout pagination requests.

/**
 * Gets a unique string for the current user.
 *
 * @param string $null            The current user key value or null.
 * @param string $path            The current request path.
 * @param string $name            The index name.
 * @param string $type            The index type.
 * @param array  $query           Prepared ES query.
 * @param array  $query_args      WP Query args.
 * @return string                 Unique string for the user.
 */
function get_unique_user_key( $null, string $path, string $name, string $type, array $query, array $query_args ): string {
	// Bail early if the user is logged in and use their user ID.
	if ( \is_user_logged_in() ) {
		return md5( get_current_user_id() );
	}

	// Parameters for the key.
	$key_parameters = [
		'REMOTE_ADDR',
		'HTTP_CLIENT_IP',
		'HTTP_X_FORWARDED_FOR',
		'HTTP_USER_AGENT',
	];

	// Holds the unique key for the user.
	$key = '';

	// Loop through parameters to build the unique key.
	foreach ( $key_parameters as $parameter ) {
		// Get the server variable value.
		$value = filter_input( INPUT_SERVER, $parameter, FILTER_SANITIZE_ENCODED );

		// Skip if parameter isn't set.
		if ( empty( $value ) ) {
			continue;
		}

		// Add it to our unique key.
		$key .= sanitize_text_field( wp_unslash( $value ) );
	}

	// Bail early if we have a unique key.
	if ( ! empty( $key ) ) {
		return md5( $key );
	}

	// Create a session and return the ID if we haven't created a unique key.
	if ( ! session_id() ) {
		session_start();
	}

	return session_id();
}
add_filter( 'ep_preference', 'get_unique_user_key', 10, 6 );

Checklist:

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests passed.

@felipeelia
Copy link
Member

@dustinrue can you please check if this would bring any performance penalty?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants