NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. abstract class P2P_Factory {
  3. protected $key;
  4. protected $queue = array();
  5. function __construct() {
  6. add_action( 'p2p_registered_connection_type', array( $this, 'check_ctype' ), 10, 2 );
  7. }
  8. // Check if a newly registered connection type needs an item to be produced.
  9. function check_ctype( $ctype, $args ) {
  10. $sub_args = $this->expand_arg( $args );
  11. if ( !$sub_args['show'] )
  12. return false;
  13. $this->queue[ $ctype->name ] = (object) $sub_args;
  14. }
  15. // Collect sub-args from main connection type args and set defaults
  16. protected function expand_arg( $args ) {
  17. if ( isset( $args[ $this->key ] ) ) {
  18. $sub_args = $args[ $this->key ];
  19. if ( !is_array( $sub_args ) ) {
  20. $sub_args = array( 'show' => $sub_args );
  21. }
  22. } else {
  23. $sub_args = array( 'show' => false );
  24. }
  25. $sub_args = wp_parse_args( $sub_args, array(
  26. 'show' => 'any',
  27. ) );
  28. return $sub_args;
  29. }
  30. // Begin processing item queue for a particular screen.
  31. function add_items() {
  32. $screen = get_current_screen();
  33. $screen_map = array(
  34. 'edit' => 'post',
  35. 'users' => 'user'
  36. );
  37. if ( !isset( $screen_map[ $screen->base ] ) )
  38. return;
  39. $object_type = $screen_map[ $screen->base ];
  40. $this->filter( $object_type, $screen->post_type );
  41. }
  42. // Filter item queue based on object type.
  43. function filter( $object_type, $post_type ) {
  44. foreach ( $this->queue as $p2p_type => $args ) {
  45. $ctype = p2p_type( $p2p_type );
  46. $directions = self::determine_directions( $ctype, $object_type, $post_type, $args->show );
  47. $title = self::get_title( $directions, $ctype );
  48. foreach ( $directions as $direction ) {
  49. $key = ( 'to' == $direction ) ? 'to' : 'from';
  50. $directed = $ctype->set_direction( $direction );
  51. $this->add_item( $directed, $object_type, $post_type, $title[$key] );
  52. }
  53. }
  54. }
  55. // Produce an item and add it to the screen.
  56. abstract function add_item( $directed, $object_type, $post_type, $title );
  57. protected static function get_title( $directions, $ctype ) {
  58. $title = array(
  59. 'from' => $ctype->get_field( 'title', 'from' ),
  60. 'to' => $ctype->get_field( 'title', 'to' )
  61. );
  62. if ( count( $directions ) > 1 && $title['from'] == $title['to'] ) {
  63. $title['from'] .= __( ' (from)', P2P_TEXTDOMAIN );
  64. $title['to'] .= __( ' (to)', P2P_TEXTDOMAIN );
  65. }
  66. return $title;
  67. }
  68. protected static function determine_directions( $ctype, $object_type, $post_type, $show_ui ) {
  69. $direction = $ctype->direction_from_types( $object_type, $post_type );
  70. if ( !$direction )
  71. return array();
  72. return $ctype->strategy->directions_for_admin( $direction, $show_ui );
  73. }
  74. }