Skip to content

Instantly share code, notes, and snippets.

@alexstandiford
Last active March 29, 2019 13:44
Show Gist options
  • Save alexstandiford/3abf6a3c639de5a66e2dcd821dabda6e to your computer and use it in GitHub Desktop.
Save alexstandiford/3abf6a3c639de5a66e2dcd821dabda6e to your computer and use it in GitHub Desktop.
Testing script to compare proposed new method of getting non-affiliate, and affiliate users.
<?php
add_filter( 'wp_is_large_network', '__return_false' );
/**
* Calculates the time needed to run the specified function.
* @param $function string The callback to run.
* @param $args array Array of arguments to pass.
* @param int $number_of_times int The number of times this function should run.
* @return mixed
*/
function get_time_to_run_function( $function, $args, $number_of_times = 1 ) {
set_time_limit( 0 );
ini_set( 'memory_limit', '4096M' );
$benchmarks = array();
$start = microtime( true );
$i = 0;
while( $i < $number_of_times ){
$result = call_user_func_array( $function, $args );
$benchmarks[] = array( 'time' => microtime( true ) - $start, 'result' => $result );
$i ++;
}
return $benchmarks;
}
/**
* Dummy test version of affwp_search_users. Modified to work in this test scenario.
* @param $term
* @param string $status
* @return array
*/
function affwp_test_search_users( $term, $status = 'none' ) {
$search_query = htmlentities2( trim( $term ) );
/**
* Fires immediately prior to an AffiliateWP user search query.
*
* @param string $search_query The user search query.
*/
do_action( 'affwp_pre_search_users', $search_query );
$args = array(
'search_columns' => array( 'user_login', 'display_name', 'user_email' ),
);
// Add search string to args.
$args['search'] = '*' . mb_strtolower( $search_query ) . '*';
// Get users matching search.
$found_users = get_users( $args );
$status = isset( $status ) ? mb_strtolower( htmlentities2( trim( $status ) ) ) : 'bypass';
$user_list = array();
if ( $found_users ) {
foreach ( $found_users as $user ) {
$label = empty( $user->user_email ) ? $user->user_login : "{$user->user_login} ({$user->user_email})";
if ( 'bypass' !== $status ) {
switch ( $status ) {
case 'none':
if ( ! affiliate_wp()->affiliates->get_by( 'user_id', $user->ID ) ) {
$user_list[] = array(
'label' => $label,
'value' => $user->user_login,
'user_id' => $user->ID,
);
}
break;
case 'any':
if ( affiliate_wp()->affiliates->get_by( 'user_id', $user->ID ) ) {
$user_list[] = array(
'label' => $label,
'value' => $user->user_login,
'user_id' => $user->ID,
);
}
break;
default:
$affiliate = affiliate_wp()->affiliates->get_by( 'user_id', $user->ID );
if ( $affiliate && $status == $affiliate->status ) {
$user_list[] = array(
'label' => $label,
'value' => $user->user_login,
'user_id' => $user->ID,
);
}
}
} else {
$user_list[] = array(
'label' => $label,
'value' => $user->user_login,
'user_id' => $user->ID,
);
}
}
}
return $user_list;
}
add_action( 'pre_user_query', function( $query ) {
if ( ! isset( $query->query_vars['affwp_is_affiliate'] ) ) {
return;
}
global $wpdb;
if ( $query->query_vars['affwp_is_affiliate'] ) {
$where = ' AND ID IN (SELECT user_id FROM ' . $wpdb->prefix . 'affiliate_wp_affiliates)';
} else {
$where = ' AND ID NOT IN (SELECT user_id FROM ' . $wpdb->prefix . 'affiliate_wp_affiliates)';
}
$query->query_where .= $where;
} );
get_header();
$current_method_benchmarks = get_time_to_run_function( 'affwp_test_search_users', array( '*', 'any' ),10 );
$proposed_method_benchmarks = get_time_to_run_function( 'get_users', array(
array(
'affwp_is_affiliate' => true,
'search' => '***',
),
),10 );
?>
<h2>Current Method</h2>
<ul>
<?php
foreach ( $current_method_benchmarks as $key => $benchmark ):
?>
<li>Benchmark <?= $key + 1 ?></li>
<ul>
<li>
This completed <?= $benchmark['time'] ?> after the start
</li>
<li>
Result: <?= count( $benchmark['result'] ) ?>
</li>
</ul>
<?php
endforeach;
?>
</ul>
<h2>Proposed Method</h2>
<ul>
<?php
foreach ( $proposed_method_benchmarks as $key => $benchmark ):
?>
<li>Benchmark <?= $key + 1 ?></li>
<ul>
<li>
This completed <?= $benchmark['time'] ?> after the start
</li>
<li>
Result: <?= count( $benchmark['result'] ) ?>
</li>
</ul>
<?php
endforeach;
?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment