added company based filter and user meta
This commit is contained in:
51
includes/class-partnerexpo-core-user-fields.php
Normal file
51
includes/class-partnerexpo-core-user-fields.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Used to register custom user fields.
|
||||
*
|
||||
* @link https://github.com/Duskell
|
||||
* @since 1.1.0
|
||||
* @package Partnerexpo_Core
|
||||
* @subpackage Partnerexpo_Core/includes
|
||||
* @author Juhász Levente <juhasz.levente@rendszerepito.hu>
|
||||
*/
|
||||
class Partnerexpo_Core_User_Fields {
|
||||
public function activate_fields() {
|
||||
add_action( 'show_user_profile', [$this, 'add_custom_user_fields'] );
|
||||
add_action( 'edit_user_profile', [$this, 'add_custom_user_fields'] );
|
||||
add_action( 'personal_options_update', [$this, 'save_custom_user_fields'] );
|
||||
add_action( 'edit_user_profile_update', [$this, 'save_custom_user_fields'] );
|
||||
add_action( 'user_register', [$this, 'add_value_after_reg'] );
|
||||
}
|
||||
|
||||
public function add_custom_user_fields( $user ) {
|
||||
$company = get_user_meta( $user->ID, 'company', true );
|
||||
?>
|
||||
<h3>Additional Information</h3>
|
||||
<table class="form-table">
|
||||
<tr>
|
||||
<th><label for="company"><?php _e( 'Cég neve', 'partnerexpo-core' ) ?></label></th>
|
||||
<td>
|
||||
<input type="text" name="company" id="company" value="<?php echo esc_attr( $company ) ?>" class="regular-text" />
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function save_custom_user_fields( $user_id ) {
|
||||
if( ! isset( $_POST[ '_wpnonce' ] ) || ! wp_verify_nonce( $_POST[ '_wpnonce' ], 'update-user_' . $user_id ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if( ! current_user_can( 'edit_user', $user_id ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
update_user_meta( $user_id, 'company', sanitize_text_field( $_POST[ 'company' ] ) );
|
||||
}
|
||||
|
||||
public function add_value_after_reg( $user_id ) {
|
||||
update_user_meta( $user_id, 'company', sanitize_text_field( $_POST[ 'company' ] ?? '' ) );
|
||||
}
|
||||
}
|
||||
@@ -121,9 +121,19 @@ class Partnerexpo_Core {
|
||||
*/
|
||||
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-partnerexpo-core-public.php';
|
||||
|
||||
/**
|
||||
* Used to have relevance-based search results.
|
||||
*/
|
||||
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/dependency/class-query-w-relevance.php';
|
||||
|
||||
/**
|
||||
* The class responsible for registering custom user fields.
|
||||
*/
|
||||
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-partnerexpo-core-user-fields.php';
|
||||
|
||||
$this->loader = new Partnerexpo_Core_Loader();
|
||||
$user_fields = new Partnerexpo_Core_User_Fields();
|
||||
$user_fields->activate_fields();
|
||||
|
||||
}
|
||||
|
||||
@@ -209,7 +219,7 @@ class Partnerexpo_Core {
|
||||
'public' => true,
|
||||
'has_archive' => false,
|
||||
'show_in_rest' => true,
|
||||
'supports' => [ 'title', 'editor', 'thumbnail', 'excerpt' ],
|
||||
'supports' => [ 'title', 'editor', 'thumbnail', 'excerpt', 'author' ],
|
||||
'rewrite' => [
|
||||
'slug' => __( 'partnerek', 'partnerexpo-core' ),
|
||||
'pages' => false,
|
||||
|
||||
@@ -70,6 +70,15 @@ class Partnerexpo_Core_Public {
|
||||
$params['tags'] = str_replace(',', '+', $params['tags']);
|
||||
}
|
||||
|
||||
$companies = explode(',', $params['companies'] ?? '');
|
||||
|
||||
$authors = get_users([
|
||||
'meta_key' => 'company',
|
||||
'meta_value' => $companies,
|
||||
'meta_compare' => 'IN',
|
||||
'fields' => 'ID',
|
||||
]);
|
||||
|
||||
$args = [
|
||||
'post_type' => 'pexpo_partners',
|
||||
'posts_per_page' => $params['resultsPerPage'] ?? 10,
|
||||
@@ -79,6 +88,10 @@ class Partnerexpo_Core_Public {
|
||||
'pexpo_tags' => $params['tags'] ?? '',
|
||||
];
|
||||
|
||||
if (!empty($authors)) {
|
||||
$args['author__in'] = array_values($authors);
|
||||
}
|
||||
|
||||
if ( ! empty( $params['tags'] ) ) {
|
||||
$tag_string = $params['tags'];
|
||||
$operator = 'IN';
|
||||
|
||||
@@ -14,6 +14,7 @@ class PartnerExpoSearch {
|
||||
drawerBackdrop: document.getElementById("pexpo-core-drawerBackdrop"),
|
||||
filterFTag: document.getElementById("pexpo-core-tag-toggle"),
|
||||
filterTags: document.getElementById("pexpo-core-tags"),
|
||||
filterCompanies: document.getElementById("pexpo-core-companies"),
|
||||
qInput: document.getElementById("pexpo-core-q"),
|
||||
masonry: document.getElementById("pexpo-core-masonry"),
|
||||
measure: document.getElementById("pexpo-core-measure"),
|
||||
@@ -33,6 +34,7 @@ class PartnerExpoSearch {
|
||||
sort: "relevance",
|
||||
force_tags: false,
|
||||
tags: [],
|
||||
companies: [],
|
||||
resultsPerPage: 20,
|
||||
page: 1,
|
||||
},
|
||||
@@ -72,7 +74,6 @@ class PartnerExpoSearch {
|
||||
initMultiSelect() {
|
||||
if (typeof MultiSelect !== 'undefined') {
|
||||
new MultiSelect(this.els.filterTags, {
|
||||
placeholder: 'Címkék kiválasztása',
|
||||
search: true,
|
||||
selectAll: false,
|
||||
onSelect: (value) => {
|
||||
@@ -84,6 +85,19 @@ class PartnerExpoSearch {
|
||||
this.state.filters.tags = this.state.filters.tags.filter(tag => tag !== value);
|
||||
}
|
||||
});
|
||||
|
||||
new MultiSelect(this.els.filterCompanies, {
|
||||
search: true,
|
||||
selectAll: false,
|
||||
onSelect: (value) => {
|
||||
if (!this.state.filters.companies.includes(value)) {
|
||||
this.state.filters.companies.push(value);
|
||||
}
|
||||
},
|
||||
onUnselect: (value) => {
|
||||
this.state.filters.companies = this.state.filters.companies.filter(company => company !== value);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.warn("MultiSelect library not found.");
|
||||
}
|
||||
|
||||
@@ -15,8 +15,28 @@ $tags = get_terms([
|
||||
'hide_empty' => false,
|
||||
]);
|
||||
|
||||
?>
|
||||
$users = get_users([
|
||||
'meta_key' => 'company',
|
||||
'meta_compare' => 'EXISTS',
|
||||
]);
|
||||
|
||||
$companies = [];
|
||||
foreach ($users as $user) {
|
||||
$company = get_user_meta($user->ID, 'company', true);
|
||||
if (!empty($company)) {
|
||||
$companies[$company] = $company;
|
||||
}
|
||||
}
|
||||
|
||||
$authors = get_users([
|
||||
'meta_key' => 'company',
|
||||
'meta_value' => array_keys($companies) ?? [],
|
||||
'meta_compare' => 'IN',
|
||||
'fields' => 'ID',
|
||||
]);
|
||||
|
||||
|
||||
?>
|
||||
<div class="pexpo-core-root pexpo-core-shell" id="pexpo-core-shell">
|
||||
<div class="pexpo-core-topbar" id="pexpo-core-topbar">
|
||||
<div class="pexpo-core-filterWrap" id="pexpo-core-filterWrap">
|
||||
@@ -64,6 +84,12 @@ $tags = get_terms([
|
||||
<option value="<?php echo esc_attr($tag->slug); ?>"><?php echo esc_html($tag->name); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<label for="pexpo-core-companies"><?php echo esc_html__('Cégek', 'partnerexpo-core'); ?></label>
|
||||
<select id="pexpo-core-companies" data-placeholder="<?php echo esc_attr__('Cégek kiválasztása', 'partnerexpo-core'); ?>" data-search-text="<?php echo esc_attr__('Keresés...', 'partnerexpo-core'); ?>" multiple="multiple">
|
||||
<?php foreach ($companies as $key => $company) : ?>
|
||||
<option value="<?php echo esc_attr($key); ?>"><?php echo esc_html($company); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<button id="pexpo-core-filterApply"><?php echo esc_html__('Alkalmaz', 'partnerexpo-core'); ?></button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user