Changeset 5533
- Timestamp:
- 06/01/2017 08:46:01 PM (7 years ago)
- Location:
- sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types/wc-post-types.php
r5529 r5533 57 57 add_filter( 'the_content', array( $this, 'add_session_info_to_speaker_posts' ) ); 58 58 59 // REST API60 add_action( 'init', array( $this, 'expose_public_post_meta' ) );61 add_filter( 'rest_prepare_wcb_speaker', array( $this, 'link_speaker_to_sessions' ), 10, 2 );62 add_filter( 'rest_prepare_wcb_session', array( $this, 'link_session_to_speakers' ), 10, 2 );63 64 59 add_filter( 'dashboard_glance_items', array( $this, 'glance_items' ) ); 65 60 add_filter( 'option_default_comment_status', array( $this, 'default_comment_ping_status' ) ); 66 61 add_filter( 'option_default_ping_status', array( $this, 'default_comment_ping_status' ) ); 62 63 add_action( 'init', array( $this, 'rest_init' ), 9 ); 67 64 } 68 65 … … 77 74 register_setting( 'wcb_sponsor_options', 'wcb_sponsor_level_order', array( $this, 'validate_sponsor_options' ) ); 78 75 add_action( 'pre_get_posts', array( $this, 'admin_pre_get_posts' ) ); 76 } 77 78 /** 79 * Runs during init, because rest_api_init is too late. 80 */ 81 function rest_init() { 82 require_once( 'inc/rest-api.php' ); 79 83 } 80 84 … … 1398 1402 1399 1403 return $content . $sessions_html; 1400 }1401 1402 /**1403 * Add non-sensitive meta fields to the speaker/session REST API endpoints1404 *1405 * if we ever want to register meta for purposes other than exposing it in the API, then this function will1406 * probably need to be re-thought and re-factored.1407 */1408 public function expose_public_post_meta() {1409 $public_session_fields = array(1410 '_wcpt_session_time' => array(1411 'type' => 'integer',1412 'single' => true,1413 ),1414 1415 '_wcpt_session_type' => array(1416 'single' => true,1417 ),1418 1419 '_wcpt_session_slides' => array(1420 'single' => true,1421 ),1422 1423 '_wcpt_session_video' => array(1424 'single' => true,1425 ),1426 );1427 1428 wcorg_register_meta_only_on_endpoint( 'post', $public_session_fields, '/wp-json/wp/v2/sessions/' );1429 1430 $public_sponsor_fields = array(1431 '_wcpt_sponsor_website' => array(1432 'single' => true,1433 )1434 );1435 1436 wcorg_register_meta_only_on_endpoint( 'post', $public_sponsor_fields, '/wp-json/wp/v2/sponsors/' );1437 }1438 1439 /**1440 * Link all sessions to the speaker in the `speakers` API endpoint1441 *1442 * This allows clients to request a speaker and get all their sessions embedded in the response, avoiding1443 * extra HTTP requests1444 *1445 * @param WP_REST_Response $response1446 * @param WP_Post $post1447 *1448 * @return WP_REST_Response1449 */1450 function link_speaker_to_sessions( $response, $post ) {1451 $sessions = get_posts( array(1452 'post_type' => 'wcb_session',1453 'posts_per_page' => 100,1454 'fields' => 'ids',1455 1456 'meta_query' => array(1457 array(1458 'key' => '_wcpt_speaker_id',1459 'value' => $post->ID,1460 ),1461 ),1462 ) );1463 1464 foreach( $sessions as $session_id ) {1465 $response->add_link(1466 'sessions',1467 get_rest_url( null, "/wp/v2/sessions/$session_id" ),1468 array( 'embeddable' => true )1469 );1470 }1471 1472 return $response;1473 }1474 1475 /**1476 * Link all speakers to the session in the `sessions` API endpoint1477 *1478 * This allows clients to request a session and get all its speakers embedded in the response, avoiding extra1479 * HTTP requests1480 *1481 * @param WP_REST_Response $response1482 * @param WP_Post $post1483 *1484 * @return WP_REST_Response1485 */1486 function link_session_to_speakers( $response, $post ) {1487 $speaker_ids = get_post_meta( $post->ID, '_wcpt_speaker_id', false );1488 1489 foreach( $speaker_ids as $speaker_id ) {1490 $response->add_link(1491 'speakers',1492 get_rest_url( null, "/wp/v2/speakers/$speaker_id" ),1493 array( 'embeddable' => true )1494 );1495 }1496 1497 return $response;1498 1404 } 1499 1405
Note: See TracChangeset
for help on using the changeset viewer.