Gists

by mgibbs189 - 12 months ago

<?php

add_filter( 'facetwp_indexer_query_args', function( $args ) {
    $args['post_type'] = (array) get_post_types();
    $args['post_type'][] = 'wprm_recipe';
    return $args;
});

by djrmom - 1 year ago

<?php
/** inherit adds attachment indexing **/
add_filter( 'facetwp_indexer_query_args', function( $args ) {
    $args['post_status'] = array( 'publish', 'inherit' );
    return $args;
});

/** post_mime_type is the column in the posts table to add **/
add_filter( 'facetwp_facet_sources', function( $sources ) {
    $sources['posts']['choices']['post_mime_type'] = 'Media Type';
    return $sources;
});

/** modifies string for mine type
 ** example image/png becomes image
 **/
add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'post_mime_type' == $params['facet_source'] ) {
		  $value = $params['facet_display_value'];
		  $value = substr( $value, 0, strpos( $value, '/' ) );
		  $params['facet_value'] = $params['facet_display_value'] = $value;
    }
    return $params;
}, 10, 2 );
by mgibbs189 - 1 year 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();

by mgibbs189 - 1 year 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();

by mgibbs189 - 2 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();

by mgibbs189 - 3 years ago

<?php

// Force indexing of product variations
function fwp_index_product_variations( $args ) {
    $post_types = get_post_types();
    $post_types[] = 'product_variation';
    $args['post_type'] = $post_types;
    return $args;
}
add_filter( 'facetwp_indexer_query_args', 'fwp_index_product_variations' );


// Index color field for the parent product
function fwp_index_color( $params, $class ) {
    if ( 'cf/_ax_color_field' == $params['facet_source'] ) {
        $variation = get_post( $params['post_id'] );
        $parent_product_id = (int) $variation->post_parent;
        $params['post_id'] = $parent_product_id;
    }
    return $params;
}
add_filter( 'facetwp_index_row', 'fwp_index_color', 10, 2 );
by mgibbs189 - 3 years ago

<?php

function fwp_index_resumes( $args ) {
    $args['post_type'] = (array) get_post_types();
    $args['post_type'][] = 'resume';
    return $args;
}
add_filter( 'facetwp_indexer_query_args', 'fwp_index_resumes' );

by mgibbs189 - 4 years ago

<?php
/*
Plugin Name: Site Customizations
Description: Site Customizations
Version: 1.0
Author: Matt Gibbs
Author URI: https://facetwp.com/
*/

function fwp_index_attachments( $args ) {
    $args['post_status'] = array( 'publish', 'inherit' );
    return $args;
}

add_filter( 'facetwp_indexer_query_args', 'fwp_index_attachments' );