NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

box-factory.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. define( 'P2P_BOX_NONCE', 'p2p-box' );
  3. class P2P_Box_Factory extends P2P_Factory {
  4. protected $key = 'admin_box';
  5. function __construct() {
  6. parent::__construct();
  7. add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
  8. add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
  9. add_action( 'wp_ajax_p2p_box', array( $this, 'wp_ajax_p2p_box' ) );
  10. }
  11. function expand_arg( $args ) {
  12. $box_args = parent::expand_arg( $args );
  13. foreach ( array( 'can_create_post' ) as $key ) {
  14. if ( isset( $args[ $key ] ) ) {
  15. $box_args[ $key ] = _p2p_pluck( $args, $key );
  16. }
  17. }
  18. $box_args = wp_parse_args( $box_args, array(
  19. 'context' => 'side',
  20. 'priority' => 'default',
  21. 'can_create_post' => true
  22. ) );
  23. return $box_args;
  24. }
  25. function add_meta_boxes( $post_type ) {
  26. $this->filter( 'post', $post_type );
  27. }
  28. function add_item( $directed, $object_type, $post_type, $title ) {
  29. if ( !self::show_box( $directed, $GLOBALS['post'] ) )
  30. return;
  31. $box = $this->create_box( $directed );
  32. $box_args = $this->queue[ $directed->name ];
  33. add_meta_box(
  34. sprintf( 'p2p-%s-%s', $directed->get_direction(), $directed->name ),
  35. $title,
  36. array( $box, 'render' ),
  37. $post_type,
  38. $box_args->context,
  39. $box_args->priority
  40. );
  41. $box->init_scripts();
  42. }
  43. private static function show_box( $directed, $post ) {
  44. $show = $directed->get( 'opposite', 'side' )->can_edit_connections();
  45. return apply_filters( 'p2p_admin_box_show', $show, $directed, $post );
  46. }
  47. private function create_box( $directed ) {
  48. $box_args = $this->queue[ $directed->name ];
  49. $title_class = str_replace( 'P2P_Side_', 'P2P_Field_Title_',
  50. get_class( $directed->get( 'opposite', 'side' ) ) );
  51. $columns = array(
  52. 'delete' => new P2P_Field_Delete,
  53. 'title' => new $title_class( $directed->get( 'opposite', 'labels' )->singular_name ),
  54. );
  55. foreach ( $directed->fields as $key => $data ) {
  56. $columns[ 'meta-' . $key ] = new P2P_Field_Generic( $key, $data );
  57. }
  58. if ( $orderby_key = $directed->get_orderby_key() ) {
  59. $columns['order'] = new P2P_Field_Order( $orderby_key );
  60. }
  61. return new P2P_Box( $box_args, $columns, $directed );
  62. }
  63. /**
  64. * Collect metadata from all boxes.
  65. */
  66. function save_post( $post_id, $post ) {
  67. if ( 'revision' == $post->post_type || defined( 'DOING_AJAX' ) )
  68. return;
  69. if ( isset( $_POST['p2p_connections'] ) ) {
  70. // Loop through the hidden fields instead of through $_POST['p2p_meta'] because empty checkboxes send no data.
  71. foreach ( $_POST['p2p_connections'] as $p2p_id ) {
  72. $data = scbForms::get_value( array( 'p2p_meta', $p2p_id ), $_POST, array() );
  73. $connection = p2p_get_connection( $p2p_id );
  74. if ( ! $connection )
  75. continue;
  76. $fields = p2p_type( $connection->p2p_type )->fields;
  77. foreach ( $fields as $key => &$field ) {
  78. $field['name'] = $key;
  79. }
  80. $data = scbForms::validate_post_data( $fields, $data );
  81. scbForms::update_meta( $fields, $data, $p2p_id, 'p2p' );
  82. }
  83. }
  84. // Ordering
  85. if ( isset( $_POST['p2p_order'] ) ) {
  86. foreach ( $_POST['p2p_order'] as $key => $list ) {
  87. foreach ( $list as $i => $p2p_id ) {
  88. p2p_update_meta( $p2p_id, $key, $i );
  89. }
  90. }
  91. }
  92. }
  93. /**
  94. * Controller for all box ajax requests.
  95. */
  96. function wp_ajax_p2p_box() {
  97. check_ajax_referer( P2P_BOX_NONCE, 'nonce' );
  98. $ctype = p2p_type( $_REQUEST['p2p_type'] );
  99. if ( !$ctype || !isset( $this->queue[$ctype->name] ) )
  100. die(0);
  101. $directed = $ctype->set_direction( $_REQUEST['direction'] );
  102. if ( !$directed )
  103. die(0);
  104. $post = get_post( $_REQUEST['from'] );
  105. if ( !$post )
  106. die(0);
  107. if ( !self::show_box( $directed, $post ) )
  108. die(-1);
  109. $box = $this->create_box( $directed );
  110. $method = 'ajax_' . esc_attr( $_REQUEST['subaction'] );
  111. $box->$method();
  112. }
  113. }