'POST', 'callback' => __NAMESPACE__ . '\handle_webhook', ) ); } add_action( 'rest_api_init', __NAMESPACE__ . '\register_webhook_endpoint' ); /** * Handle a request to the Airstory webhook. * * The payload should be delivered via an HTTP POST request, with the following structure: * * { * identifier: XXX, * project: 'pXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', * document: 'dXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX' * } * * @param WP_REST_Request $request The WP REST API request object. */ function handle_webhook( WP_REST_Request $request ) { $user_id = $request->get_param( 'identifier' ); $project = $request->get_param( 'project' ); $document = $request->get_param( 'document' ); // Establish an API connection, using the Airstory token of the connection owner. $api = new Airstory\API; $api->set_token( Credentials\get_token( $user_id ) ); // Determine if there's a current post that matches. $post_id = Core\get_current_draft( $project, $document ); if ( $post_id ) { $post_id = Core\update_document( $api, $project, $document, $post_id ); } else { $post_id = Core\create_document( $api, $project, $document, $user_id ); } // Return early if create_document() gave us a WP_Error object. if ( is_wp_error( $post_id ) ) { return $post_id; } // Since get_edit_post_link() depends on permission checks, we'll construct the link manually. $edit_path = add_query_arg( array( 'post' => $post_id, 'action' => 'edit', ), '/post.php' ); return array( 'project' => $project, 'document' => $document, 'post_id' => $post_id, 'edit_url' => admin_url( $edit_path ), ); }