Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Disable REST API when not logged in */
- add_filter( 'rest_authentication_errors', 'chuck_disable_rest_endpoints' );
- function chuck_disable_rest_endpoints( $access ) {
- if( ! is_user_logged_in() ) {
- return new WP_Error( 'rest_cannot_access', __( 'Only authenticated users can access the REST API.', 'disable-json-api' ), array( 'status' => rest_authorization_required_code() ) );
- }
- return $access;
- }
- /* Client to Voyage Metabox Relationship */
- add_action( 'mb_relationships_init', 'wvm_client_to_voyage_connection' );
- function wvm_client_to_voyage_connection() {
- MB_Relationships_API::register( [
- 'id' => 'client-to-voyage-relationship',
- 'from' => [
- 'object_type' => 'user',
- 'meta_box' => [
- 'title' => 'My Voyage(s)',
- ],
- 'field' => array(
- 'max_clone' => -1,
- 'query_args' => array(
- 'role__in' => array( 'client' )
- )
- )
- ],
- 'to' => [
- 'object_type' => 'post',
- 'post_type' => 'voyage',
- 'meta_box' => [
- 'title' => 'Client',
- ],
- ],
- ] );
- }
- /* Shortcode to show upcoming voyages for a client */
- function fdm_my_voyages_shortcode() {
- if ( is_user_logged_in() ) {
- $voyages = MB_Relationships_API::get_connected( [
- 'id' => 'client-to-voyage-relationship',
- 'from' => get_current_user_id(),
- ] );
- if ( ! $voyages ){
- return 'No trips assigned.';
- }
- $output = '';
- $output .= '<div class="client-voyages">';
- $i = 0;
- foreach( $voyages as $voyage){
- $voyageID = $voyage->ID;
- $url = site_url( 'wp-json/wp/v2/voyage/' ) . $voyageID;
- $response = wp_remote_get( $url );
- // Exit if error.
- if ( is_wp_error( $response ) ) {
- return;
- }
- $posts = json_decode( wp_remote_retrieve_body( $response ) );
- $startDate = $posts->meta_box->trip_start_date;
- $startDate = date("M d", strtotime($startDate));
- $endeDate = $posts->meta_box->trip_end_date;
- $endeDate = date("M d", strtotime($endeDate));
- $voyageLink = $posts->link;
- $output .= '<a class="client-voyage-link" href="'.$voyageLink.'">';
- $output .= '<div class="client-voyage">'.$voyage->post_title.'</div>';
- $output .= '<div class="client-voyage-dates">'.$startDate.' - '.$endeDate.'</div>';
- $output .= '</a>';
- }
- $output .= '</div>';
- return $output;
- } else {
- $output .= 'No Access!';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement