PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/the-events-calendar/src/Tribe/Linked_Posts/Chooser_Meta_Box.php

https://gitlab.com/ezgonzalez/integral
PHP | 306 lines | 162 code | 45 blank | 99 comment | 26 complexity | 09b42b2a91ac91c45f721958ce18b863 MD5 | raw file
  1. <?php
  2. /**
  3. * Class Tribe__Events__Linked_Posts__Chooser_Meta_Box
  4. *
  5. * Handles the Organizer section inside the Events meta box
  6. */
  7. class Tribe__Events__Linked_Posts__Chooser_Meta_Box {
  8. /**
  9. * @var WP_Post
  10. */
  11. protected $event;
  12. /**
  13. * @var Tribe__Events__Main
  14. */
  15. protected $tribe;
  16. /**
  17. * @var Tribe__Events__Linked_Posts
  18. */
  19. protected $linked_posts;
  20. /**
  21. * @var string
  22. */
  23. protected $post_type;
  24. /**
  25. * @var string
  26. */
  27. protected $singular_name;
  28. public function __construct( $event = null, $post_type = null ) {
  29. $this->tribe = Tribe__Events__Main::instance();
  30. $this->linked_posts = Tribe__Events__Linked_Posts::instance();
  31. $this->post_type = $post_type;
  32. $this->singular_name = $this->linked_posts->linked_post_types[ $this->post_type ]['singular_name'];
  33. $this->singular_name_lowercase = $this->linked_posts->linked_post_types[ $this->post_type ]['singular_name_lowercase'];
  34. $this->get_event( $event );
  35. add_action( 'wp', array( $this, 'sticky_form_data' ), 50 ); // Later than events-admin.js itself is enqueued
  36. }
  37. /**
  38. * Work with the specifed event object or else use a placeholder if we are in
  39. * the middle of creating a new event.
  40. *
  41. * @param null $event
  42. */
  43. protected function get_event( $event = null ) {
  44. if ( is_null( $event ) ) {
  45. $event = $GLOBALS['post'];
  46. }
  47. if ( is_numeric( $event ) ) {
  48. $event = WP_Post::get_instance( $event );
  49. }
  50. if ( $event instanceof stdClass || is_array( $event ) ) {
  51. $event = new WP_Post( (object) $event );
  52. }
  53. if ( ! $event instanceof WP_Post ) {
  54. $event = new WP_Post( (object) array( 'ID' => 0 ) );
  55. }
  56. $this->event = $event;
  57. }
  58. /**
  59. * Render the organizer chooser section for the events meta box
  60. *
  61. */
  62. public function render() {
  63. $this->render_dropdowns();
  64. $this->render_add_post_button();
  65. /**
  66. * Make this Template filterable, used for Community Facing templates
  67. *
  68. * @var string $file_path
  69. */
  70. include apply_filters( 'tribe_events_multiple_linked_post_template', $this->tribe->pluginPath . 'src/admin-views/linked-post-meta-box.php' );
  71. }
  72. /**
  73. * displays the saved organizer dropdown in the event metabox
  74. * Used to be a PRO only feature, but as of 3.0, it is part of Core.
  75. *
  76. */
  77. public function render_dropdowns() {
  78. $post_id = $this->event->ID;
  79. $current_linked_posts = get_post_meta( $post_id, $this->linked_posts->get_meta_key( $this->post_type ), false );
  80. if ( $this->use_default_post( $current_linked_posts ) ) {
  81. /**
  82. * Filters the default selected post for the linked post
  83. *
  84. * @param array $default Default post array
  85. * @param string $post_type Linked post post type
  86. */
  87. $current_linked_posts = apply_filters( 'tribe_events_linked_post_default', array(), $this->post_type );
  88. }
  89. /**
  90. * Filters the default selected post for the linked post
  91. *
  92. * @param array $current_linked_posts Array of currently linked posts
  93. * @param string $post_type Linked post post type
  94. */
  95. $current_linked_posts = (array) apply_filters( 'tribe_display_event_linked_post_dropdown_id', $current_linked_posts, $this->post_type );
  96. /* if the user can't create organizers, then remove any empty values
  97. from the $current_organizers array. This prevents the automatic
  98. selection of an organizer every time the event is edited. */
  99. $linked_post_pto = get_post_type_object( $this->post_type );
  100. if ( ! current_user_can( $linked_post_pto->cap->create_posts ) ) {
  101. $current_linked_posts = array_filter( $current_linked_posts );
  102. }
  103. ?><script type="text/template" id="tmpl-tribe-select-<?php echo esc_attr( $this->post_type ); ?>"><?php $this->single_post_dropdown( 0 ); ?></script><?php
  104. $current_linked_posts = $this->maybe_parse_candidate_linked_posts( $current_linked_posts );
  105. $i = 0;
  106. $num_records = count( $current_linked_posts );
  107. do {
  108. echo '<tbody>';
  109. $this->single_post_dropdown( isset( $current_linked_posts[ $i ] ) ? $current_linked_posts[ $i ] : 0 );
  110. echo '</tbody>';
  111. $i++;
  112. } while ( $i < $num_records );
  113. }
  114. /**
  115. * Render a single row of the organizers table
  116. *
  117. * @param int $organizer_id
  118. *
  119. */
  120. protected function single_post_dropdown( $linked_post_id ) {
  121. $linked_post_type_container = $this->linked_posts->get_post_type_container( $this->post_type );
  122. $linked_post_type_id_field = $this->linked_posts->get_post_type_id_field_index( $this->post_type );
  123. ?>
  124. <tr class="saved-linked-post">
  125. <td style="width:170px"><?php
  126. $this->move_handle();
  127. ?><label data-l10n-create-<?php echo esc_attr( $this->post_type ); ?>="<?php printf( esc_attr__( 'Create New %s', 'the-events-calendar' ), $this->singular_name ); ?>"><?php printf( esc_html__( 'Use Saved %s:', 'the-events-calendar' ), $this->singular_name ); ?></label>
  128. </td>
  129. <td><?php
  130. $this->linked_posts->saved_linked_post_dropdown( $this->post_type, $linked_post_id );
  131. $this->edit_post_link( $linked_post_id );
  132. if ( ! empty( $this->linked_posts->linked_post_types[ $this->post_type ]['allow_multiple'] ) ) {
  133. $this->delete_handle();
  134. }
  135. ?></td>
  136. </tr>
  137. <?php
  138. }
  139. /**
  140. * Render a link to edit the organizer post
  141. *
  142. * @param int $organizer_id
  143. *
  144. */
  145. protected function edit_post_link( $linked_post_id ) {
  146. $linked_post_pto = get_post_type_object( $this->post_type );
  147. if (
  148. empty( $linked_post_pto->cap->create_posts )
  149. || ! current_user_can( $linked_post_pto->cap->create_posts )
  150. ) {
  151. return;
  152. }
  153. ?>
  154. <div class="edit-linked-post-link"><a
  155. <?php if ( empty( $linked_post_id ) ) { ?> style="display:none;"<?php } ?>
  156. data-admin-url="<?php echo esc_url( admin_url( 'post.php?action=edit&post=' ) ); ?>"
  157. href="<?php echo esc_url( admin_url( sprintf( 'post.php?action=edit&post=%s', $linked_post_id ) ) ); ?>"
  158. target="_blank"><?php printf( esc_html__( 'Edit %s', 'the-events-calendar' ), esc_html( $this->singular_name ) ); ?></a>
  159. </div>
  160. <?php
  161. }
  162. /**
  163. * Determine if the event can use the default setting
  164. *
  165. * @param array $current_organizers
  166. *
  167. * @return bool
  168. */
  169. protected function use_default_post( $current_posts ) {
  170. if ( ! empty( $current_posts ) ) {
  171. return false; // the event already has organizers
  172. }
  173. if ( ! empty( $this->event->ID ) && get_post_status( $this->event->ID ) != 'auto-draft' ) {
  174. return false; // the event has already been saved
  175. }
  176. if ( is_admin() ) {
  177. return Tribe__Admin__Helpers::instance()->is_action( 'add' );
  178. } else {
  179. return true; // a front-end submission form (e.g., community)
  180. }
  181. }
  182. /**
  183. * Renders the "Add Another Organizer" button
  184. *
  185. */
  186. protected function render_add_post_button() {
  187. if ( empty( $this->linked_posts->linked_post_types[ $this->post_type ]['allow_multiple'] ) ) {
  188. return;
  189. }
  190. ?>
  191. <tfoot>
  192. <tr>
  193. <td colspan="2"><a class="tribe-add-post" href="#"><?php echo esc_html( sprintf( __( 'Add another %s', 'the-events-calendar' ), $this->singular_name_lowercase ) ); ?></a></td>
  194. </tr>
  195. </tfoot>
  196. <?php
  197. }
  198. /**
  199. * Renders the handle for sorting organizers
  200. *
  201. */
  202. protected function move_handle() {
  203. echo '<span class="dashicons dashicons-screenoptions move-linked-post-group"></span>';
  204. }
  205. /**
  206. * Renders the handle for deleting an organizer
  207. *
  208. */
  209. protected function delete_handle() {
  210. echo '<a class="dashicons dashicons-trash delete-linked-post-group" href="#"></a>';
  211. }
  212. /**
  213. * Supply previously submitted organizer field values to the events-admin.js
  214. * script in order to provide them with sticky qualities.
  215. *
  216. * This *must* run later than the action:priority used to enqueue
  217. * events-admin.js.
  218. */
  219. public function sticky_form_data() {
  220. $submitted_data = array();
  221. $linked_posts = Tribe__Events__Linked_Posts::instance();
  222. $container = $linked_posts->get_post_type_container( $this->post_type );
  223. if ( empty( $_POST[ $container ] ) || ! is_array( $_POST[ $container ] ) ) {
  224. return;
  225. }
  226. foreach ( $_POST[ $container ] as $field => $set_of_values ) {
  227. if ( ! is_array( $set_of_values ) ) {
  228. continue;
  229. }
  230. foreach ( $set_of_values as $index => $value ) {
  231. if ( ! isset( $submitted_data[ $index ] ) ) {
  232. $submitted_data[ $index ] = array();
  233. }
  234. $submitted_data[ $index ][ $field ] = esc_attr( $value );
  235. }
  236. }
  237. wp_localize_script( 'tribe-events-admin', 'tribe_sticky_' . $this->post_type . '_fields', $submitted_data );
  238. }
  239. /**
  240. * @param $current_linked_posts
  241. *
  242. * @return mixed
  243. */
  244. private function maybe_parse_candidate_linked_posts( array $current_linked_posts = array() ) {
  245. $linked_post_type_container = $this->linked_posts->get_post_type_container( $this->post_type );
  246. // filter out any non-truthy values
  247. $current_linked_posts = array_filter( $current_linked_posts );
  248. $has_no_current_linked_posts = empty( $current_linked_posts );
  249. $submitted_data_contains_candidate_linked_posts = ! empty( $_POST[ $linked_post_type_container ] );
  250. if ( $has_no_current_linked_posts && $submitted_data_contains_candidate_linked_posts ) {
  251. $candidate_linked_posts = $_POST[ $linked_post_type_container ];
  252. $linked_post_type_id_field = $this->linked_posts->get_post_type_id_field_index( $this->post_type );
  253. if ( ! empty( $candidate_linked_posts[ $linked_post_type_id_field ] ) ) {
  254. $candidate_linked_posts = $candidate_linked_posts[ $linked_post_type_id_field ];
  255. return $candidate_linked_posts;
  256. }
  257. return $current_linked_posts;
  258. }
  259. return $current_linked_posts;
  260. }
  261. }