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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /**
  3. * A metabox in wp-admin
  4. */
  5. class P2P_Box {
  6. private $ctype;
  7. private $args;
  8. private $columns;
  9. private static $enqueued_scripts = false;
  10. private static $admin_box_qv = array(
  11. 'update_post_term_cache' => false,
  12. 'update_post_meta_cache' => false,
  13. 'post_status' => 'any',
  14. );
  15. function __construct( $args, $columns, $ctype ) {
  16. $this->args = $args;
  17. $this->columns = $columns;
  18. $this->ctype = $ctype;
  19. $this->labels = $this->ctype->get( 'opposite', 'labels' );
  20. }
  21. public function init_scripts() {
  22. if ( self::$enqueued_scripts )
  23. return;
  24. wp_enqueue_style( 'p2p-box', plugins_url( 'box.css', __FILE__ ),
  25. array(), P2P_PLUGIN_VERSION );
  26. wp_register_script( 'mustache', plugins_url( 'mustache.js', __FILE__ ),
  27. array(), '0.7.2', true );
  28. wp_enqueue_script( 'p2p-box', plugins_url( 'box.js', __FILE__ ),
  29. array( 'backbone', 'mustache' ), P2P_PLUGIN_VERSION, true );
  30. wp_localize_script( 'p2p-box', 'P2PAdminL10n', array(
  31. 'nonce' => wp_create_nonce( P2P_BOX_NONCE ),
  32. 'spinner' => admin_url( 'images/wpspin_light.gif' ),
  33. 'deleteConfirmMessage' => __( 'Are you sure you want to delete all connections?', P2P_TEXTDOMAIN ),
  34. ) );
  35. self::$enqueued_scripts = true;
  36. add_action( 'admin_footer', array( __CLASS__, 'add_templates' ) );
  37. }
  38. static function add_templates() {
  39. self::add_template( 'tab-list' );
  40. self::add_template( 'table-row' );
  41. }
  42. private static function add_template( $slug ) {
  43. echo html( 'script', array(
  44. 'type' => 'text/html',
  45. 'id' => "p2p-template-$slug"
  46. ), file_get_contents( dirname( __FILE__ ) . "/templates/$slug.html" ) );
  47. }
  48. function render( $item ) {
  49. $extra_qv = array_merge( self::$admin_box_qv, array(
  50. 'p2p:context' => 'admin_box',
  51. 'p2p:per_page' => -1
  52. ) );
  53. $this->connected_items = $this->ctype->get_connected( $item, $extra_qv, 'abstract' )->items;
  54. $data = array(
  55. 'attributes' => $this->render_data_attributes(),
  56. 'connections' => $this->render_connections_table( $item ),
  57. 'create-connections' => $this->render_create_connections( $item ),
  58. 'help' => isset( $this->labels->help ) ? $this->labels->help : ''
  59. );
  60. echo P2P_Mustache::render( 'box', $data );
  61. }
  62. protected function render_data_attributes() {
  63. $data_attr = array(
  64. 'p2p_type' => $this->ctype->name,
  65. 'duplicate_connections' => $this->ctype->duplicate_connections,
  66. 'cardinality' => $this->ctype->get( 'opposite', 'cardinality' ),
  67. 'direction' => $this->ctype->get_direction()
  68. );
  69. $data_attr_str = array();
  70. foreach ( $data_attr as $key => $value )
  71. $data_attr_str[] = "data-$key='" . $value . "'";
  72. return implode( ' ', $data_attr_str );
  73. }
  74. protected function render_connections_table( $item ) {
  75. $data = array();
  76. if ( empty( $this->connected_items ) )
  77. $data['hide'] = 'style="display:none"';
  78. $tbody = array();
  79. foreach ( $this->connected_items as $item ) {
  80. $tbody[] = $this->connection_row( $item->p2p_id, $item );
  81. }
  82. $data['tbody'] = $tbody;
  83. foreach ( $this->columns as $key => $field ) {
  84. $data['thead'][] = array(
  85. 'column' => $key,
  86. 'title' => $field->get_title()
  87. );
  88. }
  89. return $data;
  90. }
  91. protected function render_create_connections( $item ) {
  92. $data = array(
  93. 'label' => $this->labels->create,
  94. );
  95. if ( 'one' == $this->ctype->get( 'opposite', 'cardinality' ) ) {
  96. if ( !empty( $this->connected_items ) )
  97. $data['hide'] = 'style="display:none"';
  98. }
  99. // Search tab
  100. $tab_content = P2P_Mustache::render( 'tab-search', array(
  101. 'placeholder' => $this->labels->search_items,
  102. ) );
  103. $data['tabs'][] = array(
  104. 'tab-id' => 'search',
  105. 'tab-title' => __( 'Search', P2P_TEXTDOMAIN ),
  106. 'is-active' => array(true),
  107. 'tab-content' => $tab_content
  108. );
  109. // "Create post" tab
  110. if ( $this->can_create_post() ) {
  111. $tab_content = P2P_Mustache::render( 'tab-create-post', array(
  112. 'title' => $this->labels->add_new_item
  113. ) );
  114. $data['tabs'][] = array(
  115. 'tab-id' => 'create-post',
  116. 'tab-title' => $this->labels->new_item,
  117. 'tab-content' => $tab_content
  118. );
  119. }
  120. $data['show-tab-headers'] = count( $data['tabs'] ) > 1 ? array(true) : false;
  121. return $data;
  122. }
  123. protected function connection_row( $p2p_id, $item, $render = false ) {
  124. $item->title = apply_filters( 'p2p_connected_title', $item->get_title(), $item->get_object(), $this->ctype );
  125. $data = array();
  126. foreach ( $this->columns as $key => $field ) {
  127. $data['columns'][] = array(
  128. 'column' => $key,
  129. 'content' => $field->render( $p2p_id, $item )
  130. );
  131. }
  132. if ( !$render )
  133. return $data;
  134. return P2P_Mustache::render( 'table-row', $data );
  135. }
  136. protected function candidate_row( $item ) {
  137. $title = apply_filters( 'p2p_candidate_title', $item->get_title(), $item->get_object(), $this->ctype );
  138. $title_data = array_merge( $this->columns['title']->get_data( $item ), array(
  139. 'title' => $title,
  140. 'item-id' => $item->get_id(),
  141. ) );
  142. $data = array();
  143. $data['columns'][] = array(
  144. 'column' => 'create',
  145. 'content' => P2P_Mustache::render( 'column-create', $title_data )
  146. );
  147. return $data;
  148. }
  149. protected function candidate_rows( $current_post_id, $page = 1, $search = '' ) {
  150. $extra_qv = array_merge( self::$admin_box_qv, array(
  151. 'p2p:context' => 'admin_box_candidates',
  152. 'p2p:search' => $search,
  153. 'p2p:page' => $page,
  154. 'p2p:per_page' => 5
  155. ) );
  156. $candidate = $this->ctype->get_connectable( $current_post_id, $extra_qv, 'abstract' );
  157. if ( empty( $candidate->items ) ) {
  158. return html( 'div class="p2p-notice"', $this->labels->not_found );
  159. }
  160. $data = array();
  161. foreach ( $candidate->items as $item ) {
  162. $data['rows'][] = $this->candidate_row( $item );
  163. }
  164. if ( $candidate->total_pages > 1 ) {
  165. $data['navigation'] = array(
  166. 'current-page' => number_format_i18n( $candidate->current_page ),
  167. 'total-pages' => number_format_i18n( $candidate->total_pages ),
  168. 'total-pages-raw' => $candidate->total_pages,
  169. 'prev-inactive' => ( 1 == $candidate->current_page ) ? 'inactive' : '',
  170. 'next-inactive' => ( $candidate->total_pages == $candidate->current_page ) ? 'inactive' : '',
  171. 'prev-label' => __( 'previous', P2P_TEXTDOMAIN ),
  172. 'next-label' => __( 'next', P2P_TEXTDOMAIN ),
  173. 'of-label' => __( 'of', P2P_TEXTDOMAIN ),
  174. );
  175. }
  176. return $data;
  177. }
  178. // Ajax handlers
  179. public function ajax_create_post() {
  180. if ( !$this->can_create_post() )
  181. die( -1 );
  182. $args = array(
  183. 'post_title' => $_POST['post_title'],
  184. 'post_author' => get_current_user_id(),
  185. 'post_type' => $this->ctype->get( 'opposite', 'side' )->first_post_type()
  186. );
  187. $from = absint( $_POST['from'] );
  188. $args = apply_filters( 'p2p_new_post_args', $args, $this->ctype, $from );
  189. $this->safe_connect( wp_insert_post( $args ) );
  190. }
  191. public function ajax_connect() {
  192. $this->safe_connect( $_POST['to'] );
  193. }
  194. private function safe_connect( $to ) {
  195. $from = absint( $_POST['from'] );
  196. $to = absint( $to );
  197. if ( !$from || !$to )
  198. die(-1);
  199. $p2p_id = $this->ctype->connect( $from, $to );
  200. self::maybe_send_error( $p2p_id );
  201. $item = $this->ctype->get( 'opposite','side')->item_recognize( $to );
  202. $out = array(
  203. 'row' => $this->connection_row( $p2p_id, $item, true )
  204. );
  205. die( json_encode( $out ) );
  206. }
  207. public function ajax_disconnect() {
  208. p2p_delete_connection( $_POST['p2p_id'] );
  209. $this->refresh_candidates();
  210. }
  211. public function ajax_clear_connections() {
  212. $r = $this->ctype->disconnect( $_POST['from'], 'any' );
  213. self::maybe_send_error( $r );
  214. $this->refresh_candidates();
  215. }
  216. protected static function maybe_send_error( $r ) {
  217. if ( !is_wp_error( $r ) )
  218. return;
  219. $out = array(
  220. 'error' => $r->get_error_message()
  221. );
  222. die( json_encode( $out ) );
  223. }
  224. public function ajax_search() {
  225. $this->refresh_candidates();
  226. }
  227. private function refresh_candidates() {
  228. die( json_encode( $this->candidate_rows(
  229. $_REQUEST['from'], $_REQUEST['paged'], $_REQUEST['s'] ) ) );
  230. }
  231. protected function can_create_post() {
  232. if ( !$this->args->can_create_post )
  233. return false;
  234. $side = $this->ctype->get( 'opposite', 'side' );
  235. return $side->can_create_item();
  236. }
  237. }