Making WordPress.org


Ignore:
Timestamp:
03/30/2017 10:48:32 PM (9 years ago)
Author:
iandunn
Message:

WordCamp Post Type: Link speaker and session endpoints together

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types/wc-post-types.php

    r5220 r5221  
    5656                add_filter( 'the_content', array( $this, 'add_video_info_to_session_posts' ) );
    5757                add_filter( 'the_content', array( $this, 'add_session_info_to_speaker_posts' ) );
     58
     59                // REST API
     60                add_filter( 'rest_prepare_wcb_speaker', array( $this, 'link_speaker_to_sessions' ), 10, 2 );
     61                add_filter( 'rest_prepare_wcb_session', array( $this, 'link_session_to_speakers' ), 10, 2 );
    5862
    5963                add_filter( 'dashboard_glance_items', array( $this, 'glance_items' ) );
     
    13931397
    13941398                return $content . $sessions_html;
     1399        }
     1400
     1401        /**
     1402         * Link all sessions to the speaker in the `speakers` API endpoint
     1403         *
     1404         * This allows clients to request a speaker and get all their sessions embedded in the response, avoiding
     1405         * extra HTTP requests
     1406         *
     1407         * @param WP_REST_Response $response
     1408         * @param WP_Post          $post
     1409         *
     1410         * @return WP_REST_Response
     1411         */
     1412        function link_speaker_to_sessions( $response, $post ) {
     1413                $sessions = get_posts( array(
     1414                        'post_type'      => 'wcb_session',
     1415                        'posts_per_page' => 100,
     1416                        'fields'         => 'ids',
     1417
     1418                        'meta_query' => array(
     1419                                array(
     1420                                        'key'   => '_wcpt_speaker_id',
     1421                                        'value' => $post->ID,
     1422                                ),
     1423                        ),
     1424                ) );
     1425
     1426                foreach( $sessions as $session_id ) {
     1427                        $response->add_link(
     1428                                'sessions',
     1429                                get_rest_url( null, "/wp/v2/sessions/$session_id" ),
     1430                                array( 'embeddable' => true )
     1431                        );
     1432                }
     1433
     1434                return $response;
     1435        }
     1436
     1437        /**
     1438         * Link all speakers to the session in the `sessions` API endpoint
     1439         *
     1440         * This allows clients to request a session and get all its speakers embedded in the response, avoiding extra
     1441         * HTTP requests
     1442         *
     1443         * @param WP_REST_Response $response
     1444         * @param WP_Post          $post
     1445         *
     1446         * @return WP_REST_Response
     1447         */
     1448        function link_session_to_speakers( $response, $post ) {
     1449                $speaker_ids = get_post_meta( $post->ID, '_wcpt_speaker_id', false );
     1450
     1451                foreach( $speaker_ids as $speaker_id ) {
     1452                        $response->add_link(
     1453                                'speakers',
     1454                                get_rest_url( null, "/wp/v2/speakers/$speaker_id" ),
     1455                                array( 'embeddable' => true )
     1456                        );
     1457                }
     1458
     1459                return $response;
    13951460        }
    13961461
Note: See TracChangeset for help on using the changeset viewer.