Advertisement
keygrip

Metabox and REST API

Aug 16th, 2022
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.31 KB | Source Code | 0 0
  1. /* Disable REST API when not logged in */
  2. add_filter( 'rest_authentication_errors', 'chuck_disable_rest_endpoints' );
  3. function chuck_disable_rest_endpoints( $access ) {
  4.     if( ! is_user_logged_in() ) {
  5.         return new WP_Error( 'rest_cannot_access', __( 'Only authenticated users can access the REST API.', 'disable-json-api' ), array( 'status' => rest_authorization_required_code() ) );
  6.     }
  7.   return $access;
  8. }
  9.  
  10.  
  11. /* Client to Voyage Metabox Relationship */
  12. add_action( 'mb_relationships_init', 'wvm_client_to_voyage_connection' );
  13. function wvm_client_to_voyage_connection() {
  14.     MB_Relationships_API::register( [
  15.         'id'   => 'client-to-voyage-relationship',
  16.         'from' => [
  17.             'object_type' => 'user',
  18.             'meta_box'    => [
  19.                 'title' => 'My Voyage(s)',
  20.             ],
  21.             'field' => array(
  22.                 'max_clone' => -1,
  23.                 'query_args' => array(
  24.                     'role__in' => array( 'client' )
  25.                 )
  26.             )
  27.         ],
  28.         'to'   => [
  29.             'object_type' => 'post',
  30.             'post_type'   => 'voyage',
  31.             'meta_box'    => [
  32.                 'title' => 'Client',
  33.             ],
  34.         ],
  35.     ] );
  36. }
  37.  
  38. /* Shortcode to show upcoming voyages for a client */
  39. function fdm_my_voyages_shortcode() {
  40.     if ( is_user_logged_in() ) {
  41.  
  42.         $voyages = MB_Relationships_API::get_connected( [
  43.             'id'   => 'client-to-voyage-relationship',
  44.             'from' => get_current_user_id(),
  45.         ] );
  46.  
  47.         if ( ! $voyages ){
  48.             return 'No trips assigned.';
  49.         }
  50.         $output = '';
  51.         $output .= '<div class="client-voyages">';
  52.         $i = 0;
  53.         foreach( $voyages as $voyage){
  54.             $voyageID = $voyage->ID;
  55.             $url = site_url( 'wp-json/wp/v2/voyage/' ) . $voyageID;
  56.             $response = wp_remote_get( $url );
  57.        
  58.             // Exit if error.
  59.             if ( is_wp_error( $response ) ) {
  60.                 return;
  61.             }
  62.             $posts = json_decode( wp_remote_retrieve_body( $response ) );
  63.             $startDate = $posts->meta_box->trip_start_date;
  64.             $startDate = date("M d", strtotime($startDate));  
  65.             $endeDate = $posts->meta_box->trip_end_date;
  66.             $endeDate = date("M d", strtotime($endeDate));
  67.             $voyageLink = $posts->link;
  68.            
  69.             $output .= '<a class="client-voyage-link" href="'.$voyageLink.'">';
  70.             $output .= '<div class="client-voyage">'.$voyage->post_title.'</div>';
  71.             $output .= '<div class="client-voyage-dates">'.$startDate.' - '.$endeDate.'</div>';
  72.             $output .= '</a>';
  73.         }
  74.         $output .= '</div>';
  75.         return $output;
  76.     } else {
  77.         $output .= 'No Access!';
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement