Skip to content

Instantly share code, notes, and snippets.

@tripflex
Last active August 23, 2017 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tripflex/ea06e94ee77cb7cc476da87bb548497e to your computer and use it in GitHub Desktop.
Save tripflex/ea06e94ee77cb7cc476da87bb548497e to your computer and use it in GitHub Desktop.
FacetWP support for WP Job Manager serialized fields (not needed if using Field Editor 1.4.7 or newer)
<?php
add_filter( 'facetwp_indexer_post_facet', 'fwp_index_wpjmfe_serialized', 15, 2 );
/**
* Filter FacetWP indexing to index WP Job Manager fields
*
* Some WP Job Manager fields are saved as serialized arrays, and due to this, we need to
* make sure and unserialize that data to make it indexable. This will also index normal
* WP Job Manager fields that are not serialized.
*
* This method uses the `facetwp_indexer_post_facet` filter that runs before FacetWP attempts to
* get the data itself. If we return the $falsey variable (which is FALSE by default), FacetWP
* will attempt to pull the data itself, if we return TRUE, it will not, and will move on to
* the next Facet in the loop.
*
*
* @since @@version
*
* @param $falsey boolean Normally FALSE to allow FacetWP to handle pulling data to index
* @param $config array Array of configuration data, includes the `defaults` and `facet` keys.
*
* @return boolean Return TRUE to prevent FacetWP from pulling data to index, FALSE to allow it to
*/
function fwp_index_wpjmfe_serialized( $falsey, $config ){
$params = $config['defaults'];
$post_type = isset( $params['post_id'] ) ? get_post_type( $params['post_id'] ) : 'unknown';
$wpjm_post_types = array( 'job_listing', 'resume' );
/**
* Exit if user already has custom function, or if the post type is not a supported post type
*/
if( function_exists( 'fwp_index_wpjmfe' ) || ! in_array( $post_type, $wpjm_post_types )) return $falsey;
$fields = WP_Job_Manager_Field_Editor_Fields::get_instance();
$all_fields = $fields->get_fields();
/**
* Get FacetWP Indexer Class Object
*/
$indexer = function_exists( 'FWP' ) && FWP()->indexer instanceof FacetWP_Indexer ? FWP()->indexer : FALSE;
/**
* Exit if error getting fields, indexer class object, or not custom field facet source.
* WP Job Manager custom field sources will always have the prepended underscore for the meta key.
*/
if( empty( $all_fields ) || empty( $indexer ) || strpos( $params['facet_source'], 'cf/_' ) === FALSE ) return $falsey;
/**
* Get meta key, without prepended underscore
*/
$cf_name = str_replace( 'cf/_', '', $params['facet_source'] );
foreach( $all_fields as $field_group => $fields ) {
/**
* Meta key not found in this field group
*/
if( ! array_key_exists( $cf_name, $fields ) ) continue;
$values = maybe_unserialize( get_post_meta( $params['post_id'], "_{$cf_name}", TRUE ) );
settype( $values, 'array' );
/**
* This is our meta key, but we don't have any values
*/
if( empty( $values ) || empty( $values[0] ) ) return TRUE;
/**
* Add values to FacetWP Index Table
*/
foreach( $values as $val ) {
$params['facet_value'] = $val;
$params['facet_display_value'] = $val;
/**
* Pass params through core FacetWP filter just in case
*/
$params = apply_filters( 'facetwp_index_row', $params, FWP()->indexer );
if( ! empty( $params ) ) $indexer->insert( $params );
}
/**
* Prevent FacetWP from trying to insert, since we already have
*/
return TRUE;
}
/**
* Return false to let FacetWP handle it
*/
return $falsey;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment