Making WordPress.org


Ignore:
Timestamp:
05/09/2024 08:33:51 AM (2 years ago)
Author:
amieiro
Message:

Translate: Sync "Translation Events" from GitHub

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-events/includes/event/event-repository.php

    r13529 r13683  
    2121        }
    2222
     23        /**
     24         * Get or create the parent post for the year.
     25         *
     26         * @return int|WP_Error
     27         */
     28        private function get_year_post_id( string $year ) {
     29                $year_post = get_page_by_path( $year, OBJECT, self::POST_TYPE );
     30                if ( ! $year_post ) {
     31                        return wp_insert_post(
     32                                array(
     33                                        'post_type'    => self::POST_TYPE,
     34                                        'post_title'   => $year,
     35                                        'post_name'    => $year,
     36                                        'post_status'  => 'publish',
     37                                        'post_content' => '',
     38                                )
     39                        );
     40                }
     41                return $year_post->ID;
     42        }
     43
    2344        public function insert_event( Event $event ) {
     45                $post_parent = $this->get_year_post_id( $event->start()->utc()->format( 'Y' ) );
     46                if ( is_wp_error( $post_parent ) ) {
     47                        return $post_parent;
     48                }
     49
    2450                $event_id_or_error = wp_insert_post(
    2551                        array(
     
    2955                                'post_content' => $event->description(),
    3056                                'post_status'  => $event->status(),
     57                                'post_parent'  => $post_parent,
    3158                        )
    3259                );
     
    4168
    4269        public function update_event( Event $event ) {
     70                $post_parent = $this->get_year_post_id( $event->start()->utc()->format( 'Y' ) );
     71                if ( is_wp_error( $post_parent ) ) {
     72                        return $post_parent;
     73                }
    4374                $event_id_or_error = wp_update_post(
    4475                        array(
     
    4879                                'post_content' => $event->description(),
    4980                                'post_status'  => $event->status(),
     81                                'post_parent'  => $post_parent,
    5082                        )
    5183                );
     
    5890        }
    5991
    60         public function delete_event( Event $event ) {
     92        public function trash_event( Event $event ) {
    6193                $result = wp_trash_post( $event->id() );
    6294                if ( ! $result ) {
    6395                        return false;
    6496                }
     97                return $event;
     98        }
     99
     100        public function delete_event( Event $event ) {
     101                $result = wp_delete_post( $event->id(), true );
     102                if ( ! $result ) {
     103                        return false;
     104                }
     105
     106                // Delete attendees.
     107                $attendees = $this->attendee_repository->get_attendees( $event->id() );
     108                foreach ( $attendees as $attendee ) {
     109                        $this->attendee_repository->remove_attendee( $event->id(), $attendee->user_id() );
     110                }
     111
     112                // Delete stats.
     113                global $wpdb, $gp_table_prefix;
     114                // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
     115                // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
     116                $wpdb->delete(
     117                        "{$gp_table_prefix}event_actions",
     118                        array( 'event_id' => $event->id() ),
     119                        array( '%d' ),
     120                );
     121                // phpcs:enable
     122
    65123                return $event;
    66124        }
     
    153211        }
    154212
     213        public function get_trashed_events( int $page = - 1, int $page_size = - 1 ): Events_Query_Result {
     214                return $this->execute_events_query(
     215                        $page,
     216                        $page_size,
     217                        array(
     218                                'post_status' => 'trash',
     219                        )
     220                );
     221        }
     222
     223        public function get_current_events_for_user( int $user_id, int $page = -1, int $page_size = -1 ): Events_Query_Result {
     224                $now = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) );
     225
     226                // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_query
     227                // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_key
     228                return $this->execute_events_query(
     229                        $page,
     230                        $page_size,
     231                        array(
     232                                'meta_query' => array(
     233                                        array(
     234                                                'key'     => '_event_start',
     235                                                'value'   => $now->format( 'Y-m-d H:i:s' ),
     236                                                'compare' => '<=',
     237                                                'type'    => 'DATETIME',
     238                                        ),
     239                                        array(
     240                                                'key'     => '_event_end',
     241                                                'value'   => $now->format( 'Y-m-d H:i:s' ),
     242                                                'compare' => '>=',
     243                                                'type'    => 'DATETIME',
     244                                        ),
     245                                ),
     246                                'meta_key'   => '_event_start',
     247                                'orderby'    => 'meta_value',
     248                                'order'      => 'ASC',
     249                        ),
     250                        $this->attendee_repository->get_events_for_user( $user_id ),
     251                );
     252                // phpcs:enable
     253        }
     254
    155255        public function get_current_and_upcoming_events_for_user( int $user_id, int $page = -1, int $page_size = -1 ): Events_Query_Result {
    156256                $now = new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) );
     
    414514
    415515        private function update_event_meta( Event $event ) {
     516                $hosts     = $this->attendee_repository->get_hosts( $event->id() );
     517                $hosts_ids = array_map(
     518                        function ( $host ) {
     519                                return $host->user_id();
     520                        },
     521                        $hosts
     522                );
     523                $hosts_ids = implode( ', ', $hosts_ids );
    416524                update_post_meta( $event->id(), '_event_start', $event->start()->utc()->format( 'Y-m-d H:i:s' ) );
    417525                update_post_meta( $event->id(), '_event_end', $event->end()->utc()->format( 'Y-m-d H:i:s' ) );
    418526                update_post_meta( $event->id(), '_event_timezone', $event->timezone()->getName() );
     527                update_post_meta( $event->id(), '_hosts', $hosts_ids );
    419528        }
    420529}
Note: See TracChangeset for help on using the changeset viewer.