Gists

2 months ago
<?php
/** shows prev link while on page 1 and next link while on last page
 **/
add_filter( 'facetwp_facet_html', function( $output, $params ) {
    if ( 'standard_pager' == $params['facet']['name'] ) { // change 'standard_pager' to name of your pager facet
        $page = FWP()->facet->pager_args['page'];
        $total = FWP()->facet->pager_args['total_pages'];
        if ( 2 > $page ) { // if this is page 1
            // add the prev page link
            $label = facetwp_i18n( $params['facet']['prev_label'] );
            $class = 'facetwp-page prev active';
            $data = ' data-page="1"';
            $html = '<a class="' . $class . '"' . $data . '>' . $label . '</a>';
            $output = str_replace( '<div class="facetwp-pager">', '<div class="facetwp-pager">' . $html, $output );           
        } elseif ( $page == $total) { // if this is last page
            // add the next page link
            $label = facetwp_i18n( $params['facet']['next_label'] );
            $class = 'facetwp-page next active';
            $data = ' data-page="' . $page .'"';
            $html = '<a class="' . $class . '"' . $data . '>' . $label . '</a>';
            $output = str_replace( '</div>', $html . '</div>', $output ); // 
        }
    }
    return $output;
}, 10, 2 );
  
3 months ago
<?php
// Also see: https://gist.facetwp.com/gist/fake-facet-choices/

add_filter( 'facetwp_facet_html', function( $output, $params ) {

  if ( 'event_month' == $params['facet']['name'] ) { // adjust name of facet

    $values = (array) $params['values'];
    $selected_values = (array) $params['selected_values'];

    for ( $x = 0; $x < 12; $x ++ ) {
      $time = strtotime( '+' . $x . ' months', strtotime( date( 'Y-M' . '-01' ) ) );
      $key = date( 'Y-m', $time );
      $name = date( 'F', $time );
      $months[ $key ] = $name;
    }

    if ( empty( $selected_values ) ) {
      $fake_output = '<div class="facetwp-radio checked" data-value="">All</div>';
    } else {
      $fake_output = '<div class="facetwp-radio" data-value="">All</div>';
    }

    foreach ( $months as $fake => $fake_display ) {
      $key = array_search( $fake, array_column( $values, 'facet_value' ) );
      if ( ! empty( $key ) || $key === 0 ) {
        $selected = in_array( $values[ $key ]['facet_value'], $selected_values ) ? ' checked' : '';
        $fake_output .= '<div class="facetwp-radio' . $selected . '" data-value="' . $fake . '"><span class="facetwp-display-value">' . $values[ $key ]['facet_display_value'] . '</span></div>';
      } else {
        $fake_output .= '<div class="facetwp-radio disabled" data-value="' . $fake . '"><span class="facetwp-display-value">' . $fake_display . '</span></div>';
      }
    }
    $output = $fake_output;
  }
  return $output;

}, 10, 2 );
3 months ago
<?php

/** Add fake facet choices as ghosts - for example if you need to display terms that
 * don't have any posts in them yet, and you want them to appear as ghost choices.
 * This example is for basic non-hierarchal checkbox, but
 * could be adapted for Radio facets.
 * Note: allowing users to make non-existant selections can have 
 * weird side-effects.
  */
add_filter( 'facetwp_facet_html', function( $output, $params ) {

    if ( 'categories' == $params['facet']['name'] ) { // adjust name of facet

        /** Array of facet_value (the technical value of the choice), facetwp_display_value (the display value of the choice).
         * Could be added manually as below, or with some custom code 
         * to find the values that you need from terms or other sources.
         */
        $fake_facets = [ 'fake1' => 'Fake 1', 'fake2' => 'Another Fake' ];

        $fake_output = '';
        foreach ( $fake_facets as $fake => $fake_display ) {
            $fake_output .= '<div class="facetwp-checkbox disabled" data-value="' . $fake . '">' . $fake_display . ' <span class="facetwp-counter">(0)</span></div>';
        }

        $output = $output . $fake_output;

    }

    return $output;
}, 10, 2);
5 years ago
<?php
/**
* Custom functions that deal with various plugin integrations of WP Job Manager.
*
* @package Listable
*/

/**
 * ======  FacetWP - https://facetwp.com/  ======
 */

/* ------- UTILITY FUNCTIONS --------- */

/*
 * Retrieve all defined facets in the the settings
 */
function listable_get_all_facets() {
	return FWP()->helper->get_facets();
}

function listable_get_facets_by_area( $area = 'front_page_hero' ) {
	$facets = array();

	$listable_facets = (array) json_decode( get_option( 'listable_facets_config' ), true ) ;

	if ( isset( $listable_facets[ $area ] ) ) {
		$facets = $listable_facets[ $area ];
	}

	return apply_filters( 'listable_get_facets_by_area', $facets, $area );
}

/*
 * Return the markup for facets
 *
 * @param array $facets
 */
function listable_get_display_facets( $facets ) {
	$output = '';
	if ( ! empty( $facets ) ) {
		foreach ( $facets as $facet ) {
			$facet_name = is_array( $facet ) ? $facet['name'] : $facet;
			$output .= facetwp_display( 'facet', $facet_name );
		}
	}

	return $output;
}

/*
 * Echo the markup for the given facets
 *
 * @param array $facets
 */
function listable_display_facets( $facets ) {
	echo listable_get_display_facets( $facets );
}

/*
 * Filter the html of each facet and add labels in front
 */
function listable_facetwp_facet_html( $html, $params ) {
	if ( isset( $params['facet'] ) && isset( $params['facet']['label'] ) ) {
		$html = '<label class="facetwp-filter-title">' . facetwp_i18n( $params['facet']['label'] ) . '</label><div class="facet-wrapper">' . $html . '</div>';
	}

	return $html;
}

add_filter( 'facetwp_facet_html', 'listable_facetwp_facet_html', 10, 2 );

/**
 * Automatically add a Listings template to FacetWP since we so desperately need it
 *
 * @param array $templates
 *
 * @return array
 */
function listable_register_listings_template( $templates ) {
	$templates[] = array(
		'label'     => 'Listings',
		'name'      => 'listings',
		'query'     => '',
		'template'  => ''
	);
	return $templates;
}
add_filter( 'facetwp_templates', 'listable_register_listings_template' );

/*
 * Enable WP archive detection for FacetWP templates
 * Requires FacetWP 2.4.1
 */
add_filter( 'facetwp_template_use_archive', '__return_true' );

/*
 * Filter the FacetWP query when using the "listings" template in FacetWP
 */
function listable_facetwp_query_args( $query_args, $facet ) {
	if ( 'listings' != $facet->template[ 'name' ] ) {
		return $query_args;
	}

	if ( '' == $query_args ) {
		$query_args = array();
	}

	// Prevent "Undefined index" error for search facets
	$search = '';
	if ( ! empty( $facet->http_params[ 'get' ][ 's' ] ) ) {
		$search = $facet->http_params[ 'get' ][ 's' ];
	}

	$defaults = array(
		'post_type' => 'job_listing',
		'post_status' => 'publish',
		's' => $search,
	);

	$query_args = wp_parse_args( $query_args, $defaults );

	return $query_args;
}
add_filter( 'facetwp_query_args', 'listable_facetwp_query_args', 10, 2 );

/*
 * Output the loop for the listings when using the "listings" template in FacetWP
 * This is used to load the listings via AJAX
 */
function listable_facetwp_template_html( $output, $class ) {
	global $post;

	//on frontpage we generally hide facet results, excepting the case when the page template requires a listing archive
	if ( '' == $class->http_params['uri'] && ! has_shortcode( $post->post_content, 'jobs' ) ) {
		return $output;
	}

	if ( 'listings' != $class->template[ 'name' ] || is_single() ) {
		return $output;
	}

	$query = $class->query;

	ob_start();

	echo '<div class="grid list job_listings">';
	if ( $query->have_posts() ) {
		while ( $query->have_posts() ) {
			$query->the_post();
			get_template_part( 'job_manager/content', 'job_listing' );
		}
		wp_reset_postdata();
	} else {
		get_template_part( 'job_manager/content', 'no-jobs-found' );
	}
	echo '</div>';
	$output = ob_get_clean();

	return $output;
}
add_filter( 'facetwp_template_html', 'listable_facetwp_template_html', 10, 2 );

function listable_fix_geolocation_indexing( $params, $class ) {
	//first the user must select as source the geolocation_lat
	if ( 'cf/geolocation_lat' == $params['facet_source'] ) {
		$lat = $params['facet_value'];

		if ( ! empty( $lat ) ) {
			$lat = get_post( $params[ 'post_id' ] )->geolocation_lat;
			$lng = get_post( $params[ 'post_id' ] )->geolocation_long;

			//save the latitude in the facet value
			$params['facet_value'] = $lat;
			//save the longitude in the facet display value
			$params['facet_display_value'] = $lng;
		}
	}

	return $params;
}
add_filter( 'facetwp_index_row', 'listable_fix_geolocation_indexing', 10, 2 );

function listable_fwp_index_wpjm_product_prices( $params, $class ) {
	if ( 'cf/_products' == $params['facet_source'] ) {
		$product_ids = (array) maybe_unserialize( $params['facet_value'] );
		foreach ( $product_ids as $id ) {
			$product = wc_get_product( $id );
			if ( is_object( $product ) ) {
				$price = $product->get_price();
				$params['facet_value'] = $price;
				$params['facet_display_value'] = $price;
				$class->insert( $params );
			}
		}
		return false; // skip default
	}
	return $params;
}
add_filter( 'facetwp_index_row', 'listable_fwp_index_wpjm_product_prices', 10, 2 );

/* ==== THE CUSTOM DRAG&DROP INTERFACE === */

function listable_fwp_admin_scripts() {
	if ( ! isset( $_GET['page'] ) || $_GET['page'] !== 'job-manager-settings') {
		return;
	}

	wp_enqueue_style( 'fwp_job_manager_admin_css', get_template_directory_uri() . '/assets/css/admin/fwp-settings.css', array( 'job_manager_admin_css' ) );

	wp_register_script( 'fwp_sortable_js',get_template_directory_uri() . '/assets/js/admin/facetwp/sortable.js', array(), null, true );
	wp_enqueue_script( 'fwp_job_manager_admin_js',get_template_directory_uri() . '/assets/js/admin/facetwp/fwp.js', array( 'job_manager_admin_js', 'fwp_sortable_js' ), null, true );
}
add_action( 'admin_enqueue_scripts', 'listable_fwp_admin_scripts', 11 );

function listable_wpjm_facets_drag_drop_interface ( $option, $attrs, $value, $placeholder ) {
	$current_values = json_decode( $value, true );
	$all_facets = FWP()->helper->get_facets();

	<div class="available_block">

		<h2><?php esc_html_e( 'Facets Advanced Filtering', 'listable' ); ?></h2>
		<p><?php esc_html_e( 'Add filtering fields to your site and allow your visitors to better search through the listings', 'listable' ); ?></p>

		<div class="sortable_block">
			<h3><?php esc_html_e( 'Available Facets', 'listable' ); ?></h3>
			<p><em><?php esc_html_e( 'Drag and drop the facets you'd like to add into the right side boxes', 'listable' ); ?></em></p>
			<ul id="facets_list" class="facets">
				<?php listable_admin_show_facets_items( $all_facets ); ?>
			</ul>
		</div>
	</div>

	<div class="facets-config">
		<input type="hidden" id="setting-listable_facets_config" name="listable_facets_config" value='<?php echo json_encode( $current_values ); ?>'>
		<div class="sortable_block">
			<h3><?php esc_html_e( 'Listing Archive', 'listable' ); ?></h3>
			<p><em><?php esc_html_e( 'This area is where most of your facets should go (except the ones already shown in the Navigation Bar)', 'listable' ); ?></em></p>

			<ul id="listings_archive_visible" class="facets">
				<?php
				if ( ! empty( $current_values['listings_archive_visible'] ) ) {
					listable_admin_show_facets_items( $current_values['listings_archive_visible'] );
				} ?>
			</ul>

			<div class="secondary_blocks">
				<p><em><?php esc_html_e( 'Facets dragged here will be hidden under an "More Filters" button', 'listable' ); ?></em></p>
				<ul id="listings_archive_hidden" class="facets">
					<?php
					if ( ! empty( $current_values['listings_archive_hidden'] ) ) {
						listable_admin_show_facets_items( $current_values['listings_archive_hidden'] );
					} ?>
				</ul>
			</div>
		</div>

		<div class="sortable_block">
			<h3><?php esc_html_e( 'Navigation Bar', 'listable' ); ?></h3>
			<p><em><?php esc_html_e( 'Site-wide available facets. Choose wisely a maximum of two of the most essential filters -- the rest of them go to the Listings Archive section.', 'listable' ); ?></em></p>

			<ul id="navigation_bar" class="facets">
				<?php
				if ( ! empty( $current_values['navigation_bar'] ) ) {
					listable_admin_show_facets_items( $current_values['navigation_bar'] );
				} ?>
			</ul>
		</div>
		<div class="sortable_block">
			<h3><?php esc_html_e( 'Front Page Hero', 'listable' ); ?></h3>
			<p><em><?php esc_html_e( 'Considering that Navigation Bar facets will not be shown on the Front Pagem feel free to use a similar configuration.', 'listable' ); ?></em></p>

			<ul id="front_page_hero" class="facets">
				<?php
				if ( ! empty( $current_values['front_page_hero'] ) ) {
					listable_admin_show_facets_items( $current_values['front_page_hero'] );
				} ?>
			</ul>
		</div>
	</div>
	<?php
}
add_action( 'wp_job_manager_admin_field_fwp_drag_and_drop', 'listable_wpjm_facets_drag_drop_interface', 10, 4 );

function listable_admin_show_facets_items( $facetwp_settings = array() ) {

	if ( empty( $facetwp_settings ) ) {
		return;
	}

	foreach ( $facetwp_settings as $facet ) {
		if ( empty( $facet ) ) {
			continue;
		}

		if ( ! is_array( $facet ) ) {
			$facet = FWP()->helper->get_facet_by_name( $facet );
		}
		?>
		<li data-facet="<?php echo $facet['name']; ?>">
			<span class="title"><?php echo $facet['label']; ?></span>
			<span class="type"><?php echo $facet['type']; ?></span>
			<span class="facet-remove">x</span>
		</li>
	<?php }
}

// disable google-maps api load in facetwp ... we will do in in theme
add_filter( 'facetwp_proximity_load_js', '__return_false' );
6 years ago
<?php

add_filter( 'facetwp_facet_html', function( $output, $params ) {
    $output = str_replace( ' <span class="facetwp-counter">', '<span class="facetwp-counter">', $output );
    return $output;
}, 10, 2 );
6 years ago
<?php

add_filter( 'facetwp_facet_html', function( $html, $args ) {
    if ( 'langdurige_opsluiting' == $args['facet']['name'] ) {
        $html = htmlspecialchars_decode( $html );
    }
    return $html;
}, 10, 2 );
1 year ago
<?php

/** removes the parentheses () from facet counts. Could also be used to replace with alternate bracketing or other output **/

add_filter( 'facetwp_facet_html', function( $output, $params ) {
	if ( 'my_facet_name' == $params['facet']['name'] ) {
		$output = preg_replace( '/\(|\)/','', $output );
	}
	return $output;
}, 10, 2 );
6 years ago
<?php

/** replace the "Date" or "Start Date" or "End Date" text in the datepicker
 ** Other langauges may have different text to replace as well
 **/
add_filter( 'facetwp_facet_html', function( $output, $params ) {
	if ( 'my_date_facet' == $params['facet']['name'] ) { // change 'my_date_facet' to the name of your date picker facet
		$output = str_replace( 'placeholder="Date"', 'placeholder="mm/dd/yyyy"', $output );
	}
	return $output;
}, 10, 2 );
6 years ago
<?php
/** simple example of find and replace values in the output of a facet's html
 ** Be careful that this doesn't replace unwanted values, you may need to be more
 ** specific, such as including part of the surround html - '>Posts' to '>Articles' makes
 ** sure you target the word right after the processing <div> tag.
 **/

add_filter( 'facetwp_facet_html', function( $output, $params ) {
	if ( 'post_type' == $params['facet']['name'] ) {
		$current_values = array( 'Posts', 'Products' );
		$replace_values = array( 'Articles', 'For Sale' );
		$output = str_replace( $current_values, $replace_values, $output );
	}
	return $output;
}, 10, 2 );
6 years ago
<?php

/*
 * Filter the html of each facet and add labels in front
 */
function listable_facetwp_facet_html( $html, $params ) {
	if ( isset( $params['facet'] ) && isset( $params['facet']['label'] ) ) {
		$html = '<label class="facetwp-filter-title">' . facetwp_i18n( $params['facet']['label'] ) . '</label><div class="facet-wrapper">' . $html . '</div>';
	}

	return $html;
}

add_filter( 'facetwp_facet_html', 'listable_facetwp_facet_html', 10, 2 );