-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclass-mb-relationships-facetwp.php
146 lines (124 loc) · 3.42 KB
/
class-mb-relationships-facetwp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* FacetWP integration
*/
class MB_Relationships_FacetWP extends FacetWP_Facet {
const FACET_TYPE = 'mb_relationships';
/**
* Facet label.
*
* @var string
* @since 1.0.0
*/
public $label;
/**
* FacetWP Indexer.
*
* @var FacetWP_Indexer
*/
protected $indexer;
/**
* Construct the class.
*
* @since 1.12.0
*/
public function __construct() {
$this->label = __( 'MB Relationships', 'meta-box-facetwp-integrator' );
// Add all registered relationships as FacetWP sources.
add_filter( 'facetwp_facet_sources', [ $this, 'facet_sources' ] );
// Hook into indexer.
add_filter( 'facetwp_indexer_post_facet', [ $this, 'facetwp_indexer_post_facet' ], 10, 2 );
}
/**
* Add all registerd relationships as facet sources.
*
* @since 1.12.0
*
* @param array $sources FacetWP sources.
*
* @return array
*/
public function facet_sources( $sources ) {
$choices = [];
$relationships = MB_Relationships_API::get_all_relationships();
foreach ( $relationships as $relationship ) {
$choices[ self::FACET_TYPE . '/' . $relationship->id ] = $relationship->id;
}
if ( ! empty( $choices ) ) {
$sources[ self::FACET_TYPE ] = array(
'label' => __( 'MB Relationships', 'meta-box-facetwp-integrator' ),
'choices' => $choices,
'weight' => 7,
);
}
return $sources;
}
/**
* Index MB relationships.
*
* @since 1.12.0
*
* @param bool $bypass Bypass default indexing.
* @param array $params Extra helper data.
*
* @return array
*/
public function facetwp_indexer_post_facet( $bypass, $params ) {
if ( ! isset( $params['facet']['source'] ) || self::FACET_TYPE . '/' !== substr( $params['facet']['source'], 0, 17 ) ) {
return $bypass;
}
$this->indexer = FWP()->indexer;
$relationship_id = substr( $params['facet']['source'], 17 );
$connected_objects = MB_Relationships_API::get_connected([
'id' => $relationship_id,
'from' => $params['defaults']['post_id'],
]);
// If no related objects, stop processing.
if ( empty( $connected_objects ) ) {
return true;
}
foreach ( $connected_objects as $connected_object ) {
$this->index_field_value( $connected_object, $params['defaults'] );
}
return $bypass;
}
/**
* Manually index a relationship value.
*
* @since 1.12.0
*
* @param WP_Post|WP_Term|WP_User $connected_object Connected object.
* @param array $params Extra helper data.
*
* @return void
*/
protected function index_field_value( $connected_object, $params ) {
switch ( get_class( $connected_object ) ) {
case WP_Term::class:
$params['facet_value'] = $connected_object->term_id;
$params['facet_display_value'] = $connected_object->name;
break;
case WP_User::class:
$params['facet_value'] = $connected_object->ID;
$params['facet_display_value'] = $connected_object->display_name;
break;
case WP_Post::class:
default:
$params['facet_value'] = $connected_object->ID;
$params['facet_display_value'] = get_the_title( $connected_object );
break;
}
/**
* Filters the FacetWP data for a connected object.
*
* @since 1.12.0
*
* @param array $params FacetWP object params.
* @param mixed $connected_object Connected object.
*
* @return array
*/
$params = apply_filters( 'mb_relationships_facet_index_value', $params, $connected_object );
$this->indexer->index_row( $params );
}
}