Gists

3 months ago
<?php

add_filter( 'facetwp_i18n', function( $string ) {

    // Get the current site's locale
    $lang = get_locale();
    
    $translations = [
        'fr_FR' => [
            'All categories' => 'Toutes les catégories',
        ],
    ];
 
    if ( isset( $translations[ $lang ][ $string ] ) ) {
        return $translations[ $lang ][ $string ];
    }

    return $string;

});
2 months ago
<?php
/** change 'Default sort' to the text of your Default label setting of your search facet, be sure to match exactly including case
 ** https://facetwp.com/help-center/facets/facet-types/sort/#available-options
 ** change 'my_search_facet' to the name of your search facet (2 places)
 ** change 'Relevance' to whatever you want the default label to be when using a search facet
 **/
add_filter( 'facetwp_i18n', function( $text ) {
    if ( 'Default sort' == $text && isset( FWP()->facet->facets['my_search_facet']['selected_values'] ) && !empty( FWP()->facet->facets['search']['selected_values'] )  ) {
        return 'Relevance';
    }
    return $text;
});
3 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 years ago
<?php

add_filter( 'facetwp_i18n', function( $string ) {
    if ( isset( FWP()->facet->http_params['lang'] ) ) {
        $lang = FWP()->facet->http_params['lang'];

        // Original = Spanish
        // Destination = English (en)
        $translations = [];
        $translations['en']['Mostrando [lower] - [upper] de [total] resultados'] = 'Showing [lower] - [upper] of [total] results';
        $translations['en']['1 resultado'] = '1 result';
        $translations['en']['No hay resultados'] = 'No results';

        if ( isset( $translations[ $lang ][ $string ] ) ) {
            return $translations[ $lang ][ $string ];
        }
    }

    return $string;
});
4 years ago
<?php
/** checks WPML current language if FWP()->facet->http_params['lang'] is not set
 **/
add_filter( 'facetwp_i18n', function( $string ) {
    
    if ( isset( FWP()->facet->http_params['lang'] ) ) {
        $lang = FWP()->facet->http_params['lang'];
    else {
	    $lang = apply_filters( 'wpml_current_language', null );
    }
 
    $translations = [];
    $translations['es']['Products'] = 'Productos';
    $translations['es']['Location'] = 'Ubicación';

    if ( isset( $translations[ $lang ][ $string ] ) ) {
	    return $translations[ $lang ][ $string ];
    }
 
    return $string;
}, 11 );
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' );
9 months ago
<?php
/**
 ** Modify text of "Enable map filtering" and "Reset" buttons.
 ** These button texts can be changed with a setting since v1.0.4 of the Map add-on.
 ** The texts can also be translated with the facetwp_i18n hook.
 **/

add_filter( 'gettext', function( $translated_text, $untranslated_text, $domain ) {
  if ( 'facetwp-map-facet' == $domain ) {
    if ( $translated_text == 'Enable map filtering' ) {
      $translated_text = 'Use map as filter';
    }
    if ( $translated_text == 'Reset' ) {
      $translated_text = 'Disable map as filter';
    }
  }  
  return $translated_text;
}, 10, 3 );
6 years ago
<?php
/*
Plugin Name: FacetWP - WPML
Description: WPML support for FacetWP
Version: 1.3.0
Author: FacetWP, LLC
Author URI: https://facetwp.com/
GitHub URI: facetwp/facetwp-wpml
*/

defined( 'ABSPATH' ) or exit;

class FWP_WPML
{

    public $default_language;
    public $current_language;


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


    /**
     * Initialize
     */
    function init() {
        if ( defined( 'ICL_SITEPRESS_VERSION' ) && function_exists( 'FWP' ) ) {
            add_action( 'wp_footer', array( $this, 'wp_footer' ), 30 );
            add_action( 'facetwp_refresh', array( $this, 'set_ajax_langauge' ) );
            add_filter( 'facetwp_render_params', array( $this, 'support_preloader' ) );
            add_filter( 'facetwp_indexer_query_args', array( $this, 'indexer_query_args' ) );
            add_action( 'facetwp_indexer_post', array( $this, 'set_post_language_code' ) );

            // Setup properties
            $this->default_language = apply_filters( 'wpml_default_language', null );
            $this->current_language = apply_filters( 'wpml_current_language', null );

            // Require WPML String Translation
            if ( function_exists( 'icl_register_string' ) ) {
                add_action( 'admin_init', array( $this, 'register_strings' ) );
                add_filter( 'facetwp_i18n', array( $this, 'facetwp_i18n' ) );
            }
        }
    }


    /**
     * Put the language into FWP_HTTP
     */
    function wp_footer() {
        $lang = $this->current_language;
        echo "<script>var FWP_HTTP = FWP_HTTP || {}; FWP_HTTP.lang = '$lang';</script>";
    }


    /**
     * Support FacetWP preloading (3.0.4+)
     */
    function support_preloader( $params ) {
        if ( isset( $params['is_preload'] ) ) {
            $params['http_params']['lang'] = $this->current_language;
        }

        return $params;
    }


    /**
     * Set the correct language for ajax requests
     */
    function set_ajax_language() {
        $http = FWP()->facet->http_params;
        if ( isset( $http['lang'] ) && $http['lang'] !== $this->default_language ) {
            do_action( 'wpml_switch_language', $http['lang'] );
        }
    }


    /**
     * Index all languages
     */
    function indexer_query_args( $args ) {
        if ( function_exists( 'is_checkout' ) && is_checkout() ) {
            return $args;
        }

        if ( -1 === $args['posts_per_page'] ) {
            do_action( 'wpml_switch_language', 'all' );
        }

        $args['suppress_filters'] = true; // query posts in all languages
        return $args;
    }


    /**
     * Find a post's language code
     */
    function get_post_language_code( $post_id ) {
        global $wpdb;

        $query = $wpdb->prepare( "SELECT language_code FROM {$wpdb->prefix}icl_translations WHERE element_id = %d", $post_id );
        return $wpdb->get_var( $query );
    }


    /**
     * Set the indexer language code
     */
    function set_post_language_code( $params ) {
        $language_code = $this->get_post_language_code( $params['post_id'] );
        do_action( 'wpml_switch_language', $language_code );
    }


    /**
     * 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 ] ) ) {
                        do_action( 'wpml_register_single_string', 'FacetWP', $facet[ $k ], $facet[ $k ] );
                    }
                }
            }
        }
    }


    /**
     * Handle string translations
     */
    function facetwp_i18n( $string ) {
        $lang = $this->current_language;
        $default = $this->default_language;

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

        if ( $lang != $default ) {
            return apply_filters( 'wpml_translate_single_string', $string, 'FacetWP', $string, $lang );
        }

        return $string;
    }
}


$fwp_wpml = new FWP_WPML();
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();
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 );