PageRenderTime 79ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/web xuanthuc.com php/web xuanthuc.com/wp-content/plugins/posts-to-posts/admin/box.php

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