Exemple #1
0
 /**
  * Index CMB2 field data
  *
  * @param bool $return
  * @param array $params
  *
  * @return bool
  */
 public function indexer_post_facet($return, $params)
 {
     $defaults = $params['defaults'];
     $facet = $params['facet'];
     /**
      * Filter to enable debugging.
      *
      * @since 1.0.0
      *
      * @param bool $debugging Whether to enable debugging. Defaults to false./
      */
     if (apply_filters('facetwp_cmb2_debugging', false)) {
         error_log("Param info: " . print_r($params, true) . PHP_EOL, 3, WP_CONTENT_DIR . '/facet.log');
     }
     // Split up the facet source
     $source = explode('/', $facet['source']);
     // Maybe return early. Includes class check, just in case
     if ('cmb2' !== $source[0] || count($source) < 3 || !class_exists('CMB2_boxes', false)) {
         return $return;
     }
     // Initial var setup
     $metabox_id = $source[1];
     $field_id = $source[2];
     // Make sure we can retrieve the Metabox
     $cmb = CMB2_Boxes::get($metabox_id);
     if (!$cmb) {
         return $return;
     }
     // Make sure the field can be retrieved.
     $field = $cmb->get_field($field_id);
     if (!$field) {
         return $return;
     }
     $field_type = $field->type();
     /**
      * Filter the CMB2 field types that do not need to be indexed by FacetWP.
      *
      * @since 1.0.0
      *
      * @param array $fields Array of field types that do not need to be indexed.
      */
     $skip_index = apply_filters('facetwp_cmb2_skip_index', array('title', 'group'));
     if (in_array($field->type(), $skip_index)) {
         return true;
     }
     /**
      * Filter to skip indexing text fields.
      *
      * By default, skip indexing text fields, because data is likely to be unique. This filter provides access
      * to the field type, so that more granular control can be achieved.
      *
      * @since 1.0.0
      *
      * @param bool   $skip       Whether to skip indexing text fields. Default: true.
      * @param string $field_type The field type.
      */
     $skip_index_text = apply_filters('facetwp_cmb2_skip_index_text', true, $field_type);
     if (false !== strpos($field->type(), 'text') && $skip_index_text) {
         return false;
     }
     /**
      * Filter to skip indexing WYSIWYG fields.
      *
      * Similar to text fields, skip indexing by default because data is likely to be unique.
      *
      * @since 1.0.0
      *
      * @param bool $skip Whether to skip indexing WYSIWYG fields.
      */
     $skip_index_wysiwyg = apply_filters('facetwp_cmb2_skip_index_wysiwyg', true);
     if ('wysiwyg' == $field_type && $skip_index_wysiwyg) {
         return false;
     }
     // Checkboxes are either on or off. Only index the "on" value.
     if ('checkbox' == $field_type) {
         if ('on' == $field->value()) {
             $this->index_field($field, $defaults);
         } else {
             return true;
         }
     }
     /**
      * Filter whether to do the default indexing.
      *
      * @since 1.0.0
      *
      * @param bool                     $index    Whether to use the default indexing.
      * @param CMB2_Field               $field    The CMB2_Field object.
      * @param array                    $defaults The array of defaults.
      * @param FacetWP_Integration_CMB2 $obj      The current class object.
      */
     $default_index = apply_filters('facetwp_cmb2_default_index', true, $field, $defaults, $this);
     if ($default_index) {
         $this->index_field_values($field, $defaults);
     }
     return true;
 }
Exemple #2
0
/**
 * Retrieve a CMB2 instance by the metabox ID
 * @since  2.0.0
 * @param  mixed  $meta_box    Metabox ID or Metabox config array
 * @param  int    $object_id   Object ID
 * @param  string $object_type Type of object being saved. (e.g., post, user, comment, or options-page).
 *                             Defaults to metabox object type.
 * @return CMB2 object
 */
function cmb2_get_metabox($meta_box, $object_id = 0, $object_type = '')
{
    if ($meta_box instanceof CMB2) {
        return $meta_box;
    }
    if (is_string($meta_box)) {
        $cmb = CMB2_Boxes::get($meta_box);
    } else {
        // See if we already have an instance of this metabox
        $cmb = CMB2_Boxes::get($meta_box['id']);
        // If not, we'll initate a new metabox
        $cmb = $cmb ? $cmb : new CMB2($meta_box, $object_id);
    }
    if ($cmb && $object_id) {
        $cmb->object_id($object_id);
    }
    if ($cmb && $object_type) {
        $cmb->object_type($object_type);
    }
    return $cmb;
}
 /**
  * Allows three methods of adding metaboxes:
  *
  * 1) Injected boxes are added to the boxes array
  * 2) Add additional boxes (or boxes if none were injected) the usual way within this function
  * 3) If array is still empty and getboxes === true, call CMB2_Boxes::get_all();
  *
  * @param string $id @since 1.1.0
  *
  * @return array|\CMB2[]
  *
  * @since 1.1.0 allow skipping of call to CMB2_Boxes::get_all();
  *              - ok to pass CMB2 metabox ID or CMB2 object; previously only objects allowed
  * @since 1.0.0
  */
 private function cmb2_metaboxes($id)
 {
     // add any injected metaboxes
     $boxes = self::$props[$id]['boxes'];
     // if boxes is not empty, check to see if they're CMB2 objects, or strings
     if (!empty($boxes)) {
         foreach ($boxes as $key => $box) {
             if (!is_object($box)) {
                 $boxes[$key] = CMB2_Boxes::get($box);
             }
         }
     }
     // if $boxes is still empty and getboxes is true, try grabbing boxes from CMB2
     $boxes = empty($boxes) && self::$props[$id]['getboxes'] === TRUE ? CMB2_Boxes::get_all() : $boxes;
     return $boxes;
 }