Skip to content

Commit ae35e41

Browse files
committed
add new filters to modify facetwp params before inserting them in the index table
1 parent 51bace4 commit ae35e41

File tree

1 file changed

+50
-25
lines changed

1 file changed

+50
-25
lines changed

facetwp-p2p.php

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,19 @@ public function init() {
6767
* @return array
6868
*/
6969
public function p2p_sources( $sources = array() ) {
70-
$options = array();
70+
$options = array();
7171
$connexions = P2P_Connection_Type_Factory::get_all_instances();
72-
foreach( $connexions as $connexion ) {
72+
foreach ( $connexions as $connexion ) {
7373
$from_ptype = get_post_type_object( $connexion->side['from']->first_post_type() );
7474
$to_ptype = get_post_type_object( $connexion->side['to']->first_post_type() );
7575
$options[ sprintf( 'p2p/%s/%s', $connexion->name, $from_ptype->name ) ] = sprintf( "[%s → %s] %s", $from_ptype->labels->singular_name, $to_ptype->labels->singular_name, $from_ptype->labels->singular_name );
7676
$options[ sprintf( 'p2p/%s/%s', $connexion->name, $to_ptype->name ) ] = sprintf( "[%s → %s] %s", $from_ptype->labels->singular_name, $to_ptype->labels->singular_name, $to_ptype->labels->singular_name );
7777
}
7878

7979
$sources['p2p'] = array(
80-
'label' => __( 'Posts 2 Posts', 'facetwp-p2p' ),
80+
'label' => __( 'Posts 2 Posts', 'facetwp-p2p' ),
8181
'choices' => $options,
82-
'weight' => 40,
82+
'weight' => 40,
8383
);
8484

8585
return $sources;
@@ -93,25 +93,25 @@ public function p2p_sources( $sources = array() ) {
9393
* @return array
9494
*/
9595
public function p2pmetas_sources( $sources = array() ) {
96-
$options = array();
96+
$options = array();
9797
$connexions = P2P_Connection_Type_Factory::get_all_instances();
98-
foreach( $connexions as $connexion ) {
98+
foreach ( $connexions as $connexion ) {
9999
if ( empty( $connexion->fields ) ) {
100100
continue;
101101
}
102102

103103
$from_ptype = get_post_type_object( $connexion->side['from']->first_post_type() );
104-
$to_ptype = get_post_type_object( $connexion->side['to']->first_post_type() );
104+
$to_ptype = get_post_type_object( $connexion->side['to']->first_post_type() );
105105
foreach ( $connexion->fields as $field_name => $field_options ) {
106106
$field_title = ! empty( $field_options['title'] ) ? $field_options['title'] : $field_name;
107107
$options[ sprintf( 'p2pmeta/%s/%s', $connexion->name, $field_name ) ] = sprintf( "[%s → %s] %s", $from_ptype->labels->singular_name, $to_ptype->labels->singular_name, $field_title );
108108
}
109109
}
110110

111111
$sources['p2p_meta'] = array(
112-
'label' => __( 'Posts 2 Posts Meta', 'facetwp-p2p' ),
112+
'label' => __( 'Posts 2 Posts Meta', 'facetwp-p2p' ),
113113
'choices' => $options,
114-
'weight' => 50,
114+
'weight' => 50,
115115
);
116116

117117
return $sources;
@@ -133,27 +133,37 @@ public function p2p_indexer( $bypass, $defaults ) {
133133
return $bypass;
134134
}
135135

136-
$connexion = $source[1];
136+
$connexion = $source[1];
137137
$connexion_side = $source[2];
138-
$post_ptype = get_post_type( (int) $params['post_id'] );
138+
$post_ptype = get_post_type( (int) $params['post_id'] );
139139

140140
if ( $post_ptype !== $connexion_side ) {
141141
return true;
142142
}
143143

144144
$connected = get_posts( array(
145-
'connected_type' => $connexion,
146-
'connected_items' => (int) $params['post_id'],
147-
'nopaging' => true,
145+
'connected_type' => $connexion,
146+
'connected_items' => (int) $params['post_id'],
147+
'nopaging' => true,
148148
'suppress_filters' => false
149149
) );
150150

151151
//Index each connected posts
152-
foreach( $connected as $p ) {
152+
foreach ( $connected as $p ) {
153153
$new_params = wp_parse_args( array(
154-
'facet_value' => $p->ID,
154+
'facet_value' => $p->ID,
155155
'facet_display_value' => $p->post_title,
156156
), $params );
157+
158+
/**
159+
* Filters values send to the indexer.
160+
*
161+
* @since 2.1.0
162+
*
163+
* @param array $new_params Associative array of parameters.
164+
* @param string $connexion P2P connexion's name.
165+
*/
166+
$new_params = apply_filters( 'facetp2p_p2p_index_params', $new_params, $connexion );
157167
FWP()->indexer->insert( $new_params );
158168
}
159169

@@ -179,9 +189,9 @@ public function p2pmetas_indexer( $bypass, $defaults ) {
179189
return $bypass;
180190
}
181191

182-
$connexion = $source[1];
192+
$connexion = $source[1];
183193
$field_name = $source[2];
184-
$post_id = (int) $params['post_id'];
194+
$post_id = (int) $params['post_id'];
185195
$post_ptype = get_post_type( $post_id );
186196

187197
$connexion_type = P2P_Connection_Type_Factory::get_instance( $connexion );
@@ -207,10 +217,23 @@ public function p2pmetas_indexer( $bypass, $defaults ) {
207217

208218
foreach ( $meta_value as $value ) {
209219
$new_params = wp_parse_args( array(
210-
'facet_value' => FWP()->helper->safe_value( $value ),
220+
'facet_value' => FWP()->helper->safe_value( $value ),
211221
'facet_display_value' => $value,
212-
'parent_id' => $p2p_id,
222+
'parent_id' => $p2p_id,
213223
), $params );
224+
225+
/**
226+
* Filters values send to the indexer.
227+
*
228+
* @since 2.1.0
229+
*
230+
* @param array $new_params Associative array of parameters.
231+
* @param int $p2p_id P2P connexion's id.
232+
* @param string $field_name P2P connexion's meta name.
233+
* @param P2P_Connection_Type $connexion_type P2P connexion's object.
234+
* @param string $p2p_column Current P2P connexion side.
235+
*/
236+
$new_params = apply_filters( 'facetp2p_p2pmeta_index_params', $new_params, (int) $p2p_id, $field_name, $connexion_type, $p2p_column );
214237
FWP()->indexer->insert( $new_params );
215238
}
216239
}
@@ -227,7 +250,7 @@ public function p2pmetas_indexer( $bypass, $defaults ) {
227250
*/
228251
public function p2p_created_connection( $p2p_id ) {
229252
$connexion = p2p_get_connection( $p2p_id );
230-
$sources = $this->get_facet_source_for_p2p_connection( $connexion );
253+
$sources = $this->get_facet_source_for_p2p_connection( $connexion );
231254
if ( is_wp_error( $sources ) ) {
232255
return false;
233256
}
@@ -257,7 +280,7 @@ public function p2p_delete_connections( $p2p_ids ) {
257280
foreach ( $p2p_ids as $p2p_id ) {
258281

259282
$connexion = p2p_get_connection( $p2p_id );
260-
$sources = $this->get_facet_source_for_p2p_connection( $connexion );
283+
$sources = $this->get_facet_source_for_p2p_connection( $connexion );
261284
if ( is_wp_error( $sources ) ) {
262285
continue;
263286
}
@@ -286,7 +309,7 @@ public function p2pmeta_delete_connections( $p2p_ids ) {
286309

287310
foreach ( $p2p_ids as $p2p_id ) {
288311

289-
$connexion = p2p_get_connection( $p2p_id );
312+
$connexion = p2p_get_connection( $p2p_id );
290313
$connexion_type = P2P_Connection_Type_Factory::get_instance( $connexion->p2p_type );
291314
if ( ! $connexion_type || empty( $connexion_type->fields ) ) {
292315
continue;
@@ -326,7 +349,7 @@ protected function get_facet_source_for_p2p_connection( $p2p_id, $direction = fa
326349
);
327350
}
328351

329-
$connexion = ( isset( $p2p_id->p2p_type ) ) ? $p2p_id : p2p_get_connection( $p2p_id );
352+
$connexion = ( isset( $p2p_id->p2p_type ) ) ? $p2p_id : p2p_get_connection( $p2p_id );
330353
$connexion_type = P2P_Connection_Type_Factory::get_instance( $connexion->p2p_type );
331354
if ( ! $connexion_type ) {
332355
return new WP_Error(
@@ -349,7 +372,7 @@ protected function get_facet_source_for_p2p_connection( $p2p_id, $direction = fa
349372
$connexion->p2p_type,
350373
$connexion_type->side['from']->first_post_type()
351374
),
352-
'to' => sprintf(
375+
'to' => sprintf(
353376
'p2p/%s/%s',
354377
$connexion->p2p_type,
355378
$connexion_type->side['to']->first_post_type()
@@ -395,9 +418,11 @@ function FWP_P2P_init() {
395418
|| version_compare( FACETWP_VERSION, '2.0.4', '<' )
396419
) {
397420
add_action( 'admin_notices', 'FWP_P2P_notice' );
421+
398422
return;
399423
}
400424

401425
FWP_P2P();
402426
}
427+
403428
add_action( 'plugins_loaded', 'FWP_P2P_init' );

0 commit comments

Comments
 (0)