Gists

3 years ago
<?php

add_filter( 'facetwp_query_args', function( $query_args, $class ) {
    if ( 'gluten_free' == $class->ajax_params['template'] && 'gluten-free-everywhere' == $class->http_params['uri'] ) {
        $query_args['tax_query'] = array(
            array(
              'taxonomy' => 'category', // change 'category' to different taxonomy if needed
              'field'    => 'slug',
              'terms'    => array( 'entertainment', 'travel', 'breakfast-box', 'lunchbox', 'gluten-free-guides' ) // array of term slugs
            )
        );
    }
    return $query_args;
}, 10, 2 );
4 months ago
<?php

add_filter( 'facetwp_query_args', function( $query_args, $class ) {

  if ( $class->ajax_params['paged'] !== 1  ) {
      $query_args['posts_per_page'] = 2;
  }
    
  return $query_args;

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

/**
 * [facetwp template="publications" project="housing"]
 *
 * Look for a "project" shortcode attribute. If one exists,
 * update the query to narrow results by that project
 */
add_filter( 'facetwp_query_args', function( $query_args, $class ) {
    foreach ( FWP()->display->shortcode_atts as $atts ) {
        if ( isset( $atts['template'] ) && isset( $atts['project'] ) ) {

            // Narrow results by the given project
            $query_args['tax_query'] = [
                [
                    'taxonomy' => 'project',
                    'field' => 'slug',
                    'terms' => $atts['project']
                ]
            ];
            break;
        }
    }
    return $query_args;
}, 10, 2 );
2 years ago
<?php

// add to your (child) theme's functions.php
// this unregisters the proximity facet's "post ID re-sorting" hook

add_filter( 'facetwp_query_args', function( $query_args ) {
    remove_filter( 'facetwp_filtered_post_ids', [ FWP()->helper->facet_types['proximity'], 'sort_by_distance' ] );
    return $query_args;
} );
5 years ago
<?php

add_filter( 'facetwp_query_args', function( $args, $class ) {
    $per_page = (int) $args['posts_per_page'] ?? get_option( 'posts_per_page', 10 );
    $args['posts_per_page'] = min( $per_page, 100 ); // maximum of 100 results
    return $args;
}, 999, 2 );
10 months ago
<?php
/** adds the post_id to a variable passed during refresh **/
add_action( 'wp_footer', function() {
    if ( is_singular() ) : ?>
        <script>
        (function($) {
            $(document).on('facetwp-refresh', function() {
                FWP_HTTP.post_id = <?php echo get_the_ID(); ?>;
            });
        })(jQuery);
        </script>
    <?php endif;
}, 100 );

/** access post_id in query args of facetwp listing template settings
 ** if using a visual query you'll first need to convert that to Dev mode
 ** advanced query args **/
add_filter( 'facetwp_query_args', function( $query_args, $class ) {
    if ( 'my_template_name' == $class->ajax_params['template'] ) {
        $post_id = isset( $class->http_params['post_id'] ) ? $class->http_params['post_id'] : get_the_id();
        // OR if that doesn't work as expected, use: $post_id = isset( FWP()->facet->http_params[ 'post_id' ] ) ? FWP()->facet->http_params[ 'post_id' ] : get_the_id();
    }
    return $query_args;
}, 10, 2 );
6 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
/*
Plugin Name: FacetWP - Polylang
Description: Polylang support for FacetWP
Version: 1.2.2
Author: FacetWP, LLC
Author URI: https://facetwp.com/
GitHub URI: facetwp/facetwp-polylang
*/

defined( 'ABSPATH' ) or exit;

class FWP_Polylang
{

    function __construct() {
        add_action( 'init' , array( $this, 'init' ) );
    }


    /**
     * Intialize
     */
    function init() {

        if ( function_exists( 'FWP' ) && function_exists( 'pll_register_string' ) ) {
            add_action( 'wp_footer', array( $this, 'wp_footer' ), 30 );
            add_action( 'admin_init', array( $this, 'register_strings' ) );
            add_action( 'facetwp_refresh', array( $this, 'set_langcode' ) );

            add_filter( 'facetwp_query_args', array( $this, 'facetwp_query_args' ), 10, 2 );
            add_filter( 'facetwp_indexer_query_args', array( $this, 'facetwp_indexer_query_args' ) );
            add_filter( 'facetwp_render_params', array( $this, 'support_preloader' ) );
            add_filter( 'facetwp_i18n', array( $this, 'facetwp_i18n' ) );
        }
        else {
            add_action( 'admin_notices', array( $this, 'admin_notice' ) );
        }
    }


    /**
     * Put the language into FWP_HTTP
     */
    function wp_footer() {
        if ( function_exists( 'pll_current_language' ) ) {
            $lang = pll_current_language();
            echo "<script>if ('undefined' != typeof FWP_HTTP) FWP_HTTP.lang = '$lang';</script>";
        }
    }


    /**
     * On ajax refreshes, set the langcode
     */
    function set_langcode() {
        if ( isset( FWP()->facet->http_params['lang'] ) ) {
            $_GET['lang'] = FWP()->facet->http_params['lang'];
        }
    }


    /**
     * Support FacetWP preloading (3.0.4+)
     */
    function support_preloader( $params ) {
        if ( isset( $params['is_preload'] ) && function_exists( 'pll_current_language' ) ) {
            $params['http_params']['lang'] = pll_current_language();
        }

        return $params;
    }


    /**
     * Query posts for the current language
     */
    function facetwp_query_args( $args, $class ) {
        if ( isset( $class->http_params['lang'] ) ) {
            $args['lang'] = $class->http_params['lang'];
        }

        return $args;
    }


    /**
     * Index all languages
     */
    function facetwp_indexer_query_args( $args ) {
        $args['lang'] = ''; // query posts in all languages
        return $args;
    }


    /**
     * Warn the user that Polylang is inactive
     */
    function admin_notice() {
?>
    <div class="error">
        <p><code>FacetWP - Polylang</code> requires the <a href="<?php echo admin_url(); ?>plugin-install.php?tab=search&s=polylang">Polylang</a> plugin. Please install and/or activate it.</p>
    </div>
<?php
    }


    /**
     * Register dynamic strings
     */
    function register_strings() {
        $facets = FWP()->helper->get_facets();
        $whitelist = array( 'label', 'label_any', 'placeholder' );

        if ( ! empty( $facets ) ) {
            foreach ( $facets as $facet ) {
                foreach ( $whitelist as $k ) {
                    if ( ! empty( $facet[ $k ] ) ) {
                        pll_register_string( 'FacetWP', $facet[ $k ] );
                    }
                }
            }
        }
    }


    /**
     * Handle string translations
     */
    function facetwp_i18n( $string ) {
        $lang = pll_current_language();
        $default = pll_default_language();

        if ( isset( FWP()->facet->http_params['lang'] ) ) {
            $lang = FWP()->facet->http_params['lang'];
        }

        if ( $lang != $default ) {
            return pll_translate_string( $string, $lang );
        }

        return $string;
    }
}


$fwp_polylang = new FWP_Polylang();
7 years ago
<?php
/*
Plugin Name: FacetWP - Polylang
Description: Polylang support for FacetWP
Version: 1.2.1
Author: FacetWP, LLC
Author URI: https://facetwp.com/
GitHub URI: facetwp/facetwp-polylang
*/

defined( 'ABSPATH' ) or exit;

class FWP_Polylang
{

    function __construct() {
        add_action( 'init' , array( $this, 'init' ) );
    }


    /**
     * Intialize
     */
    function init() {

        if ( function_exists( 'FWP' ) && function_exists( 'pll_register_string' ) ) {
            add_action( 'wp_footer', array( $this, 'wp_footer' ), 30 );
            add_action( 'admin_init', array( $this, 'register_strings' ) );

            add_filter( 'facetwp_query_args', array( $this, 'facetwp_query_args' ), 10, 2 );
            add_filter( 'facetwp_indexer_query_args', array( $this, 'facetwp_indexer_query_args' ) );
            add_filter( 'facetwp_render_params', array( $this, 'support_preloader' ) );
            add_filter( 'facetwp_i18n', array( $this, 'facetwp_i18n' ) );
        }
        else {
            add_action( 'admin_notices', array( $this, 'admin_notice' ) );
        }
    }


    /**
     * Put the language into FWP_HTTP
     */
    function wp_footer() {
        if ( function_exists( 'pll_current_language' ) ) {
            $lang = pll_current_language();
            echo "<script>if ('undefined' != typeof FWP_HTTP) FWP_HTTP.lang = '$lang';</script>";
        }
    }


    /**
     * Support FacetWP's preloader (3.0.4+)
     */
    function support_preloader( $params ) {
        if ( isset( $params['is_preload'] ) && function_exists( 'pll_current_language' ) ) {
            $params['http_params']['lang'] = pll_current_language();
        }

        return $params;
    }


    /**
     * Query posts for the current language
     */
    function facetwp_query_args( $args, $class ) {
        if ( isset( $class->http_params['lang'] ) ) {
            $args['lang'] = $class->http_params['lang'];
        }

        return $args;
    }


    /**
     * Index all languages
     */
    function facetwp_indexer_query_args( $args ) {
        $args['lang'] = ''; // query posts in all languages
        return $args;
    }


    /**
     * Warn the user that Polylang is inactive
     */
    function admin_notice() {
?>
    <div class="error">
        <p><code>FacetWP - Polylang</code> requires the <a href="<?php echo admin_url(); ?>plugin-install.php?tab=search&s=polylang">Polylang</a> plugin. Please install and/or activate it.</p>
    </div>
<?php
    }


    /**
     * Register dynamic strings
     */
    function register_strings() {
        $facets = FWP()->helper->get_facets();
        $whitelist = array( 'label', 'label_any', 'placeholder' );

        if ( ! empty( $facets ) ) {
            foreach ( $facets as $facet ) {
                foreach ( $whitelist as $k ) {
                    if ( ! empty( $facet[ $k ] ) ) {
                        pll_register_string( 'FacetWP', $facet[ $k ] );
                    }
                }
            }
        }
    }


    /**
     * Handle string translations
     */
    function facetwp_i18n( $string ) {
        $lang = pll_current_language();
        $default = pll_default_language();

        if ( isset( FWP()->facet->http_params['lang'] ) ) {
            $lang = FWP()->facet->http_params['lang'];
        }

        if ( $lang != $default ) {
            return pll_translate_string( $string, $lang );
        }

        return $string;
    }
}


$fwp_polylang = new FWP_Polylang();
7 years ago
<?php

add_filter( 'facetwp_query_args', function( $query_args, $class ) {
    if ( 0 < count( $class->facets['distance']['selected_values'] ) ) {
        $query_args['orderby'] = 'post__in';
        $query_args['order'] = 'ASC';
    }
    return $query_args;
}, 10, 2 );