@@ -70,10 +70,15 @@ public function p2p_sources( $sources = array() ) {
7070 $ options = array ();
7171 $ connexions = P2P_Connection_Type_Factory::get_all_instances ();
7272 foreach ( $ connexions as $ connexion ) {
73- $ from_ptype = get_post_type_object ( $ connexion ->side ['from ' ]->first_post_type () );
74- $ to_ptype = get_post_type_object ( $ connexion ->side ['to ' ]->first_post_type () );
75- $ 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 );
76- $ 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 );
73+ $ from_ptype = $ this ->parse_side ( $ connexion ->side ['from ' ] );
74+ $ to_ptype = $ this ->parse_side ( $ connexion ->side ['to ' ] );
75+ $ options [ sprintf ( 'p2p/%s/%s ' , $ connexion ->name , $ from_ptype ->name ) ] = sprintf ( "[%s → %s] %s " , $ from_ptype ->singular_name , $ to_ptype ->singular_name , $ from_ptype ->singular_name );
76+ $ options [ sprintf ( 'p2p/%s/%s ' , $ connexion ->name , $ to_ptype ->name ) ] = sprintf ( "[%s → %s] %s " , $ from_ptype ->singular_name , $ to_ptype ->singular_name , $ to_ptype ->singular_name );
77+ }
78+
79+ // don't add source if no options available
80+ if ( empty ( $ options ) ) {
81+ return $ sources ;
7782 }
7883
7984 $ sources ['p2p ' ] = array (
@@ -100,14 +105,19 @@ public function p2pmetas_sources( $sources = array() ) {
100105 continue ;
101106 }
102107
103- $ 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 () );
108+ $ from_ptype = $ this -> parse_side ( $ connexion ->side ['from ' ] );
109+ $ to_ptype = $ this -> parse_side ( $ connexion ->side ['to ' ] );
105110 foreach ( $ connexion ->fields as $ field_name => $ field_options ) {
106111 $ field_title = ! empty ( $ field_options ['title ' ] ) ? $ field_options ['title ' ] : $ field_name ;
107- $ 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 );
112+ $ options [ sprintf ( 'p2pmeta/%s/%s ' , $ connexion ->name , $ field_name ) ] = sprintf ( "[%s → %s] %s " , $ from_ptype ->singular_name , $ to_ptype ->singular_name , $ field_title );
108113 }
109114 }
110115
116+ // don't add source if no options available
117+ if ( empty ( $ options ) ) {
118+ return $ sources ;
119+ }
120+
111121 $ sources ['p2p_meta ' ] = array (
112122 'label ' => __ ( 'Posts 2 Posts Meta ' , 'facetwp-p2p ' ),
113123 'choices ' => $ options ,
@@ -199,7 +209,7 @@ public function p2pmetas_indexer( $bypass, $defaults ) {
199209 return true ;
200210 }
201211
202- $ p2p_column = $ connexion_type ->side ['from ' ]-> first_post_type ( ) === $ post_ptype ? 'p2p_from ' : 'p2p_to ' ;
212+ $ p2p_column = $ this -> get_post_type ( $ connexion_type ->side ['from ' ] ) === $ post_ptype ? 'p2p_from ' : 'p2p_to ' ;
203213
204214 $ p2p_ids = $ wpdb ->get_col (
205215 $ wpdb ->prepare (
@@ -362,23 +372,60 @@ protected function get_facet_source_for_p2p_connection( $p2p_id, $direction = fa
362372 return sprintf (
363373 'p2p/%s/%s ' ,
364374 $ connexion ->p2p_type ,
365- $ connexion_type ->side [ $ direction ]-> first_post_type ( )
375+ $ this -> get_post_type ( $ connexion_type ->side [ $ direction ] )
366376 );
367377 }
368378
369379 return array (
370380 'from ' => sprintf (
371381 'p2p/%s/%s ' ,
372382 $ connexion ->p2p_type ,
373- $ connexion_type ->side ['from ' ]-> first_post_type ( )
383+ $ this -> get_post_type ( $ connexion_type ->side ['from ' ] )
374384 ),
375385 'to ' => sprintf (
376386 'p2p/%s/%s ' ,
377387 $ connexion ->p2p_type ,
378- $ connexion_type ->side ['to ' ]-> first_post_type ( )
388+ $ this -> get_post_type ( $ connexion_type ->side ['to ' ] )
379389 )
380390 );
381391 }
392+
393+ /**
394+ * Get post type for a P2P_Side.
395+ *
396+ * Handle special case for P2P_Side_User.
397+ *
398+ * @param P2P_Side $side
399+ *
400+ * @return string
401+ */
402+ protected function get_post_type ( $ side ) {
403+ return ( is_a ( $ side , 'P2P_Side_User ' ) ) ? 'user ' : $ side ->first_post_type ();
404+ }
405+
406+ /**
407+ * Prepare data for a P2P_Side.
408+ *
409+ * @param P2P_Side $side
410+ *
411+ * @return object
412+ */
413+ protected function parse_side ( $ side ) {
414+ $ data = [];
415+
416+ $ type = $ this ->get_post_type ( $ side );
417+
418+ if ( 'user ' === $ type ) {
419+ $ data ['name ' ] = 'user ' ;
420+ $ data ['singular_name ' ] = 'User ' ;
421+ } else {
422+ $ ptype = get_post_type_object ( $ type );
423+ $ data ['name ' ] = $ ptype ->name ;
424+ $ data ['singular_name ' ] = $ ptype ->labels ->singular_name ;
425+ }
426+
427+ return (object ) $ data ;
428+ }
382429}
383430
384431/**
@@ -425,4 +472,4 @@ function FWP_P2P_init() {
425472 FWP_P2P ();
426473}
427474
428- add_action ( 'plugins_loaded ' , 'FWP_P2P_init ' );
475+ add_action ( 'plugins_loaded ' , 'FWP_P2P_init ' );
0 commit comments