Last active
June 8, 2020 06:38
-
-
Save arvedsen/a447ed16e9a3489f393f7ce3c8b6e434 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Plugin Name: My PMPro Directory Widget | |
* Description: Add widget to Member Directory page to filter results. | |
*/ | |
class My_PMPro_Directory_Widget extends WP_Widget { | |
/** | |
* Sets up the widget | |
*/ | |
public function __construct() { | |
parent::__construct( | |
'my_pmpro_directory_widget', | |
'My PMPro Directory Widget', | |
array( 'description' => 'Filter the PMPro Member Directory' ) | |
); | |
} | |
/** | |
* Code that runs on the frontend. | |
* | |
* Modify the content in the <li> tags to | |
* create filter inputs in the sidebar | |
*/ | |
public function widget( $args, $instance ) { | |
// If we're not on a page with a PMPro directory, return. | |
global $post; | |
if ( ! is_a( $post, 'WP_Post' ) || ! has_shortcode( $post->post_content, 'pmpro_member_directory' ) ) { | |
return; | |
} | |
?> | |
<aside id="my_pmpro_directory_widget" class="widget my_pmpro_directory_widget"> | |
<h3 class="widget-title">Filtrering</h3> | |
<form> | |
<ul> | |
<li> | |
<strong>Landsdel:</strong><br/> | |
<?php | |
// Set up values to filter for. | |
$province = array( | |
"nordjylland"=>"Nordjylland", | |
"midtjylland"=>"Midtjylland", | |
"soenderjylland"=>"Sønderjylland", | |
"fyn"=>"Fyn", "sjaelland"=>"Sjælland", | |
"storkoebenhavn"=>"Storkøbenhavn", | |
"bornholm"=>"Bornholm", | |
); | |
foreach ( $province as $key => $value ) { | |
// Check if this value should default to be checked. | |
$checked_modifier = ''; | |
if ( ! empty( $_REQUEST['province'] ) && in_array( $key, $_REQUEST['province'] ) ) { | |
$checked_modifier = ' checked'; | |
} | |
// Add checkbox. | |
echo '<input type="checkbox" name="province[]" value="' . $key . '"' . $checked_modifier . '><label> ' . $value . '</label><br/>'; | |
} | |
?> | |
</li> | |
<li> | |
<strong>Køn:</strong><br/> | |
<?php | |
// Set up values to filter for. | |
$gender = array( | |
"male"=>"Dreng", | |
"female"=>"Pige", | |
); | |
foreach ( $gender as $key => $value ) { | |
// Check if this value should default to be checked. | |
$checked_modifier = ''; | |
if ( ! empty( $_REQUEST['gender'] ) && in_array( $key, $_REQUEST['gender'] ) ) { | |
$checked_modifier = ' checked'; | |
} | |
// Add checkbox. | |
echo '<input type="checkbox" name="gender[]" value="' . $key . '"' . $checked_modifier . '><label> ' . $value . '</label><br/>'; | |
} | |
?> | |
</li> | |
<li> | |
<strong>Kategori:</strong><br/> | |
<?php | |
// Set up values to filter for. | |
$category = array( | |
"model"=>"Model", "statist"=>"Statist", "actor_unex"=>"Børneskuespiller (uøvet)", "actor_ex"=>"Børnesskuespiller (øvet)" | |
); | |
foreach ( $category as $key => $value ) { | |
// Check if this value should default to be checked. | |
$checked_modifier = ''; | |
if ( ! empty( $_REQUEST['category'] ) && in_array( $key, $_REQUEST['category'] ) ) { | |
$checked_modifier = ' checked'; | |
} | |
// Add checkbox. | |
echo '<input type="checkbox" name="category[]" value="' . $key . '"' . $checked_modifier . '><label> ' . $value . '</label><br/>'; | |
} | |
?> | |
</li> | |
<li> | |
<strong>Alder:</strong><br/> | |
<?php | |
// Set up values to filter for. | |
$age = array( | |
"0 years"=>"0 år", "1"=>"1 år", "2"=>"2 år", "3"=>"3 år", "4"=>"4 år", "5"=>"5 år", "6"=>"6 år", "7"=>"7 år", "8"=>"8 år", "9"=>"9 år", "10"=>"10 år", "11"=>"11 år", "12"=>"12 år", "13"=>"13 år", "14"=>"14 år" | |
); | |
foreach ( $age as $key => $value ) { | |
// Check if this value should default to be checked. | |
$checked_modifier = ''; | |
if ( ! empty( $_REQUEST['age'] ) && in_array( $key, $_REQUEST['age'] ) ) { | |
$checked_modifier = ' checked'; | |
} | |
// Add checkbox. | |
echo '<input type="checkbox" name="age[]" value="' . $key . '"' . $checked_modifier . '><label> ' . $value . '</label><br/>'; | |
} | |
?> | |
</li> | |
<li> | |
<strong>Tøjstørrelse:</strong><br/> | |
<?php | |
// Set up values to filter for. | |
$clothing_size = array( | |
"60-65"=>"60-65", "65-70"=>"65-70", "70-75"=>"70-75", "75-80"=>"75-80", "80-85"=>"80-85", "85-90"=>"85-90", "90-96"=>"90-96", "96-104"=>"96-104", "104-110"=>"104-110", "110-116"=>"110-116", "116-122"=>"116-122", "122-128"=>"122-128", "128-137"=>"128-137", "137-147"=>"137-147", "147-158"=>"147-158", "158-170"=>"158-170", "170-172"=>"170-172", "172-174"=>"172-174" | |
); | |
foreach ( $clothing_size as $key => $value ) { | |
// Check if this value should default to be checked. | |
$checked_modifier = ''; | |
if ( ! empty( $_REQUEST['clothing_size'] ) && in_array( $key, $_REQUEST['clothing_size'] ) ) { | |
$checked_modifier = ' checked'; | |
} | |
// Add checkbox. | |
echo '<input type="checkbox" name="clothing_size[]" value="' . $key . '"' . $checked_modifier . '><label> ' . $value . '</label><br/>'; | |
} | |
?> | |
</li> | |
<li> | |
<strong>Skostørrelse:</strong><br/> | |
<?php | |
// Set up values to filter for. | |
$shoe_size = array( | |
"14-16"=>"14-16", "16-18"=>"16-18", "18-20"=>"18-20", "20-22"=>"20-22", "22-24"=>"22-24", "24-26"=>"24-26", "26-28"=>"26-28", "28-30"=>"28-30", "30-33"=>"30-33", "33-35"=>"33-35", "35-37"=>"35-37", "37-39"=>"37-39", "39-41"=>"39-41", "41-43"=>"41-43", "43-45"=>"43-45" | |
); | |
foreach ( $shoe_size as $key => $value ) { | |
// Check if this value should default to be checked. | |
$checked_modifier = ''; | |
if ( ! empty( $_REQUEST['shoe_size'] ) && in_array( $key, $_REQUEST['shoe_size'] ) ) { | |
$checked_modifier = ' checked'; | |
} | |
// Add checkbox. | |
echo '<input type="checkbox" name="shoe_size[]" value="' . $key . '"' . $checked_modifier . '><label> ' . $value . '</label><br/>'; | |
} | |
?> | |
</li> | |
<li> | |
<strong>Højde:</strong><br/> | |
<?php | |
// Set up values to filter for. | |
$height = array( | |
"47-57"=>"47-57", "57-68"=>"57-68", "68-75"=>"68-75", "75-84"=>"75-84", "84-90"=>"84-90", "90-95"=>"90-95", "95-105"=>"95-105", "105-113"=>"105-113", "113-121"=>"113-121", "121-128"=>"121-128", "128-135"=>"128-135", "135-144"=>"135-144", "144-150"=>"144-150", "150-156"=>"150-156", "156-162"=>"156-162", "162-171"=>"162-171", "171-178"=>"171-178", "178-185"=>"178-185", "185-194"=>"185-194" | |
); | |
foreach ( $height as $key => $value ) { | |
// Check if this value should default to be checked. | |
$checked_modifier = ''; | |
if ( ! empty( $_REQUEST['height'] ) && in_array( $key, $_REQUEST['height'] ) ) { | |
$checked_modifier = ' checked'; | |
} | |
// Add checkbox. | |
echo '<input type="checkbox" name="height[]" value="' . $key . '"' . $checked_modifier . '><label> ' . $value . '</label><br/>'; | |
} | |
?> | |
</li> | |
<li> | |
<strong>Udseende:</strong><br/> | |
<?php | |
// Set up values to filter for. | |
$nationality = array( | |
"afrikansk"=>"Afrikansk", "asiatisk"=>"Asiatisk", "indisk"=>"Indisk", "mellenoestisk"=>"Mellemøstligt", "mulat"=>"Mulat", "skandinavisk"=>"Skandinavisk", "sydamerikansk"=>"Sydamerikansk", "sydeuropaeisk"=>"Sydeuropæisk", "oesteuropaisk"=>"Østeuropæisk" | |
); | |
foreach ( $nationality as $key => $value ) { | |
// Check if this value should default to be checked. | |
$checked_modifier = ''; | |
if ( ! empty( $_REQUEST['nationality'] ) && in_array( $key, $_REQUEST['nationality'] ) ) { | |
$checked_modifier = ' checked'; | |
} | |
// Add checkbox. | |
echo '<input type="checkbox" name="nationality[]" value="' . $key . '"' . $checked_modifier . '><label> ' . $value . '</label><br/>'; | |
} | |
?> | |
</li> | |
<li> | |
<strong>Hårfarve:</strong><br/> | |
<?php | |
// Set up values to filter for. | |
$colors = array( | |
'blond' => 'Blond', | |
'brown' => 'Brun', | |
'red' => 'Rød', | |
'black' => 'Sort', | |
); | |
foreach ( $colors as $key => $value ) { | |
// Check if this value should default to be checked. | |
$checked_modifier = ''; | |
if ( ! empty( $_REQUEST['hair_color'] ) && in_array( $key, $_REQUEST['hair_color'] ) ) { | |
$checked_modifier = ' checked'; | |
} | |
// Add checkbox. | |
echo '<input type="checkbox" name="hair_color[]" value="' . $key . '"' . $checked_modifier . '><label> ' . $value . '</label><br/>'; | |
} | |
?> | |
</li> | |
<li> | |
<strong>Øjenfarve:</strong><br/> | |
<?php | |
// Set up values to filter for. | |
$eye_color = array( | |
"blue"=>"Blå", "gren"=>"Grønne", "blue_green"=>"Blå/grønne", "brown"=>"Brune" | |
); | |
foreach ( $eye_color as $key => $value ) { | |
// Check if this value should default to be checked. | |
$checked_modifier = ''; | |
if ( ! empty( $_REQUEST['eye_color'] ) && in_array( $key, $_REQUEST['eye_color'] ) ) { | |
$checked_modifier = ' checked'; | |
} | |
// Add checkbox. | |
echo '<input type="checkbox" name="eye_color[]" value="' . $key . '"' . $checked_modifier . '><label> ' . $value . '</label><br/>'; | |
} | |
?> | |
</li> | |
<li><input type="submit" value="Filtrer"></li> | |
<li><input type="button" onclick="window.location.href = 'https://kidsmanagement.dk/katalog';" value="Nulstil"/></li> | |
</ul> | |
</form> | |
</aside> | |
<?php | |
} | |
} | |
/** | |
* Check $_REQUEST for parameters from the widget. Add to SQL query. | |
*/ | |
function my_pmpro_directory_widget_filter_sql_parts( $sql_parts, $levels, $s, $pn, $limit, $start, $end, $order_by, $order ) { | |
global $wpdb; | |
// Filter results based on province if a province is selected. | |
if ( ! empty( $_REQUEST['province'] ) && is_array( $_REQUEST['province'] ) ) { | |
$sql_parts['JOIN'] .= " LEFT JOIN $wpdb->usermeta um_province ON um_province.meta_key = 'province' AND u.ID = um_province.user_id "; | |
$sql_parts['WHERE'] .= " AND um_province.meta_value in ('" . implode( "','", $_REQUEST['province'] ) . "') "; | |
} | |
// Filter results based on gender if a gender is selected. | |
if ( ! empty( $_REQUEST['gender'] ) && is_array( $_REQUEST['gender'] ) ) { | |
$sql_parts['JOIN'] .= " LEFT JOIN $wpdb->usermeta um_gender ON um_gender.meta_key = 'gender' AND u.ID = um_gender.user_id "; | |
$sql_parts['WHERE'] .= " AND um_gender.meta_value in ('" . implode( "','", $_REQUEST['gender'] ) . "') "; | |
} | |
// Filter results based on category if a category is selected. | |
if ( ! empty( $_REQUEST['category'] ) && is_array( $_REQUEST['category'] ) ) { | |
$sql_parts['JOIN'] .= " LEFT JOIN $wpdb->usermeta um_category ON um_category.meta_key = 'category' AND u.ID = um_category.user_id "; | |
$sql_parts['WHERE'] .= " AND ( "; | |
foreach( $_REQUEST['category'] as $category ) { | |
$sql_parts['WHERE'] .= "um_category.meta_value LIKE '%" . esc_sql( $category ) . "%' OR "; | |
} | |
$sql_parts['WHERE'] = rtrim( $sql_parts['WHERE'], 'OR ' ); // Remove the last stray OR. | |
$sql_parts['WHERE'] .= " )"; | |
} | |
// Filter results based on birthday if a birthday is selected. | |
if ( ! empty( $_REQUEST['age'] ) && is_array( $_REQUEST['age'] ) ) { | |
$sql_parts['JOIN'] .= " LEFT JOIN $wpdb->usermeta um_age ON um_age.meta_key = 'age' AND u.ID = um_age.user_id "; | |
$sql_parts['WHERE'] .= " AND um_age.meta_value in ('" . implode( "','", $_REQUEST['age'] ) . "') "; | |
} | |
// Filter results based on clothing size if a clothing size is selected. | |
if ( ! empty( $_REQUEST['clothing_size'] ) && is_array( $_REQUEST['clothing_size'] ) ) { | |
$sql_parts['JOIN'] .= " LEFT JOIN $wpdb->usermeta um_clothing_size ON um_clothing_size.meta_key = 'clothing_size' AND u.ID = um_clothing_size.user_id "; | |
$sql_parts['WHERE'] .= " AND um_clothing_size.meta_value in ('" . implode( "','", $_REQUEST['clothing_size'] ) . "') "; | |
} | |
// Filter results based on shoe size if a shoe size is selected. | |
if ( ! empty( $_REQUEST['shoe_size'] ) && is_array( $_REQUEST['shoe_size'] ) ) { | |
$sql_parts['JOIN'] .= " LEFT JOIN $wpdb->usermeta um_shoe_size ON um_shoe_size.meta_key = 'shoe_size' AND u.ID = um_shoe_size.user_id "; | |
$sql_parts['WHERE'] .= " AND um_shoe_size.meta_value in ('" . implode( "','", $_REQUEST['shoe_size'] ) . "') "; | |
} | |
// Filter results based on height if a height is selected. | |
if ( ! empty( $_REQUEST['height'] ) && is_array( $_REQUEST['height'] ) ) { | |
$sql_parts['JOIN'] .= " LEFT JOIN $wpdb->usermeta um_height ON um_height.meta_key = 'height' AND u.ID = um_height.user_id "; | |
$sql_parts['WHERE'] .= " AND um_height.meta_value in ('" . implode( "','", $_REQUEST['height'] ) . "') "; | |
} | |
// Filter results based on nationality if a nationality is selected. | |
if ( ! empty( $_REQUEST['nationality'] ) && is_array( $_REQUEST['nationality'] ) ) { | |
$sql_parts['JOIN'] .= " LEFT JOIN $wpdb->usermeta um_nationality ON um_nationality.meta_key = 'nationality' AND u.ID = um_nationality.user_id "; | |
$sql_parts['WHERE'] .= " AND um_nationality.meta_value in ('" . implode( "','", $_REQUEST['nationality'] ) . "') "; | |
} | |
// Filter results based on hair color if a color is selected. | |
if ( ! empty( $_REQUEST['hair_color'] ) && is_array( $_REQUEST['hair_color'] ) ) { | |
$sql_parts['JOIN'] .= " LEFT JOIN $wpdb->usermeta um_hair_color ON um_hair_color.meta_key = 'hair_color' AND u.ID = um_hair_color.user_id "; | |
$sql_parts['WHERE'] .= " AND um_hair_color.meta_value in ('" . implode( "','", $_REQUEST['hair_color'] ) . "') "; | |
} | |
// Filter results based on eye color if a color is selected. | |
if ( ! empty( $_REQUEST['eye_color'] ) && is_array( $_REQUEST['eye_color'] ) ) { | |
$sql_parts['JOIN'] .= " LEFT JOIN $wpdb->usermeta um_eye_color ON um_eye_color.meta_key = 'eye_color' AND u.ID = um_eye_color.user_id "; | |
$sql_parts['WHERE'] .= " AND um_eye_color.meta_value in ('" . implode( "','", $_REQUEST['eye_color'] ) . "') "; | |
} | |
// Filter results based on max weight if a max weight was inputted. | |
/*if ( ! empty( $_REQUEST['max_weight'] ) && is_numeric( $_REQUEST['max_weight'] ) ) { | |
$join_weight = true; // We will JOIN this later, but we don't want to JOIN it twice. | |
$sql_parts['WHERE'] .= ' AND um_dog_weight.meta_value <= ' . $_REQUEST['max_weight'] . ' '; | |
} | |
// Filter results based on min weight if a min weight was inputted. | |
if ( ! empty( $_REQUEST['min_weight'] ) && is_numeric( $_REQUEST['min_weight'] ) ) { | |
$join_weight = true; // We will JOIN this later, but we don't want to JOIN it twice. | |
$sql_parts['WHERE'] .= ' AND um_dog_weight.meta_value >= ' . $_REQUEST['min_weight'] . ' '; | |
} | |
// Make sure to get the dog weight in the SQL query if we use that in a WHERE clause. | |
if ( ! empty( $join_weight ) ) { | |
$sql_parts['JOIN'] .= " LEFT JOIN $wpdb->usermeta um_dog_weight ON um_dog_weight.meta_key = 'dog_weight' AND u.ID = um_dog_weight.user_id "; | |
}*/ | |
return $sql_parts; | |
} | |
add_filter( 'pmpro_member_directory_sql_parts', 'my_pmpro_directory_widget_filter_sql_parts', 10, 9 ); | |
/** | |
* Registers widget. | |
*/ | |
function my_pmpro_register_directory_widget() { | |
register_widget( 'My_PMPro_Directory_Widget' ); | |
} | |
add_action( 'widgets_init', 'my_pmpro_register_directory_widget' ); | |
/* END filter member directory */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment