NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

side-post.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. class P2P_Side_Post extends P2P_Side {
  3. protected $item_type = 'P2P_Item_Post';
  4. function __construct( $query_vars ) {
  5. $this->query_vars = $query_vars;
  6. }
  7. public function get_object_type() {
  8. return 'post';
  9. }
  10. public function first_post_type() {
  11. return $this->query_vars['post_type'][0];
  12. }
  13. private function get_ptype() {
  14. $ptype = $this->first_post_type();
  15. $ptype_object = get_post_type_object( $ptype );
  16. if ( !$ptype_object ) {
  17. throw new P2P_Exception( "Can't find post type $ptype." );
  18. }
  19. return $ptype_object;
  20. }
  21. function get_base_qv( $q ) {
  22. if ( isset( $q['post_type'] ) && 'any' != $q['post_type'] ) {
  23. $common = array_intersect( $this->query_vars['post_type'], (array) $q['post_type'] );
  24. if ( !$common )
  25. unset( $q['post_type'] );
  26. }
  27. return array_merge( $this->query_vars, $q, array(
  28. 'suppress_filters' => false,
  29. 'ignore_sticky_posts' => true,
  30. ) );
  31. }
  32. function get_desc() {
  33. return implode( ', ', array_map( array( $this, 'post_type_label' ), $this->query_vars['post_type'] ) );
  34. }
  35. private function post_type_label( $post_type ) {
  36. $cpt = get_post_type_object( $post_type );
  37. return $cpt ? $cpt->label : $post_type;
  38. }
  39. function get_title() {
  40. return $this->get_labels()->name;
  41. }
  42. function get_labels() {
  43. try {
  44. $labels = $this->get_ptype()->labels;
  45. } catch ( P2P_Exception $e ) {
  46. trigger_error( $e->getMessage(), E_USER_WARNING );
  47. $labels = new stdClass;
  48. }
  49. return $labels;
  50. }
  51. function can_edit_connections() {
  52. try {
  53. return current_user_can( $this->get_ptype()->cap->edit_posts );
  54. } catch ( P2P_Exception $e ) {
  55. trigger_error( $e->getMessage(), E_USER_WARNING );
  56. return false;
  57. }
  58. }
  59. function can_create_item() {
  60. if ( count( $this->query_vars['post_type'] ) > 1 )
  61. return false;
  62. if ( count( $this->query_vars ) > 1 )
  63. return false;
  64. return true;
  65. }
  66. function translate_qv( $qv ) {
  67. $map = array(
  68. 'include' => 'post__in',
  69. 'exclude' => 'post__not_in',
  70. 'search' => 's',
  71. 'page' => 'paged',
  72. 'per_page' => 'posts_per_page'
  73. );
  74. foreach ( $map as $old => $new )
  75. if ( isset( $qv["p2p:$old"] ) )
  76. $qv[$new] = _p2p_pluck( $qv, "p2p:$old" );
  77. return $qv;
  78. }
  79. function do_query( $args ) {
  80. return new WP_Query( $args );
  81. }
  82. function capture_query( $args ) {
  83. $q = new WP_Query;
  84. $q->_p2p_capture = true;
  85. $q->query( $args );
  86. return $q->_p2p_sql;
  87. }
  88. function get_list( $wp_query ) {
  89. $list = new P2P_List( $wp_query->posts, $this->item_type );
  90. $list->current_page = max( 1, $wp_query->get('paged') );
  91. $list->total_pages = $wp_query->max_num_pages;
  92. return $list;
  93. }
  94. function is_indeterminate( $side ) {
  95. $common = array_intersect(
  96. $this->query_vars['post_type'],
  97. $side->query_vars['post_type']
  98. );
  99. return !empty( $common );
  100. }
  101. protected function recognize( $arg ) {
  102. if ( is_object( $arg ) && !isset( $arg->post_type ) )
  103. return false;
  104. $post = get_post( $arg );
  105. if ( !is_object( $post ) )
  106. return false;
  107. if ( !$this->recognize_post_type( $post->post_type ) )
  108. return false;
  109. return $post;
  110. }
  111. public function recognize_post_type( $post_type ) {
  112. if ( !post_type_exists( $post_type ) )
  113. return false;
  114. return in_array( $post_type, $this->query_vars['post_type'] );
  115. }
  116. }