Home / Admin / GravityForms_DIR_AD_RAPI – Expose Gravity Forms ACF Fields to REST API
Duplicate Snippet

Embed Snippet on Your Site

GravityForms_DIR_AD_RAPI – Expose Gravity Forms ACF Fields to REST API

* Description: Makes Gravity Forms data accessible via Application Password authentication
* Location: Run Everywhere
* Priority: 15

ismail daugherty PRO
<10
Code Preview
php
<?php
/**
 * WPCode Snippet: Expose Gravity Forms ACF Fields to REST API
 * Description: Makes Gravity Forms data accessible via Application Password authentication
 * Location: Run Everywhere
 * Priority: 15
 */
defined( 'ABSPATH' ) || exit;
/**
 * Register Gravity Forms ACF meta fields in REST API
 */
add_action( 'rest_api_init', function() {
    
    $gf_fields = array(
        // Form Basic Info
        'form_id'              => 'integer',
        'form_status'          => 'string',
        'entry_count'          => 'integer',
        'field_count'          => 'integer',
        
        // Feed Counts
        'feed_count_total'     => 'integer',
        'feed_count_active'    => 'integer',
        'feed_count_inactive'  => 'integer',
        'feeds_list'           => 'array',
        
        // Form Settings
        'form_settings'        => 'object',
        
        // Notifications & Confirmations
        'notification_count'   => 'integer',
        'confirmation_count'   => 'integer',
        'notifications_content' => 'string',
        'confirmations_content' => 'string',
        
        // Link Fields
        'gf_form_edit_link'    => 'string',
        'gf_form_entries_link' => 'string',
        
        // Sync Timestamps
        'last_sync'            => 'string',
        
        // Field Details
        'field_details_list'      => 'string',
        'field_details_last_sync' => 'string',
    );
    
    // Register Gravity Forms fields
    foreach ( $gf_fields as $field_name => $field_type ) {
        register_rest_field( 'gf_form_feeds', $field_name, array(
            'get_callback' => function( $object ) use ( $field_name, $field_type ) {
                $value = get_post_meta( $object['id'], $field_name, true );
                
                if ( $value === '' || $value === false ) {
                    return null;
                }
                
                switch ( $field_type ) {
                    case 'integer':
                        return intval( $value );
                    case 'boolean':
                        return (bool) $value;
                    case 'array':
                        return is_array( $value ) ? $value : array();
                    case 'object':
                        return is_array( $value ) || is_object( $value ) ? $value : new stdClass();
                    default:
                        return $value;
                }
            },
            'update_callback' => function( $value, $object ) use ( $field_name ) {
                return update_post_meta( $object->ID, $field_name, $value );
            },
            'schema' => array(
                'description' => ucwords( str_replace( '_', ' ', $field_name ) ),
                'type'        => $field_type,
                'context'     => array( 'view', 'edit' ),
            ),
        ) );
    }
    
} );
/**
 * Enable REST API for Gravity Forms CPT
 */
add_filter( 'register_post_type_args', function( $args, $post_type ) {
    
    if ( $post_type === 'gf_form_feeds' ) {
        $args['show_in_rest'] = true;
        $args['rest_base'] = 'gf-forms-feeds';
        $args['rest_controller_class'] = 'WP_REST_Posts_Controller';
    }
    
    return $args;
}, 10, 2 );
/**
 * Security: Block access unless using Application Password
 */
add_filter( 'rest_pre_dispatch', function( $result, $server, $request ) {
    
    $route = $request->get_route();
    if ( strpos( $route, '/wp/v2/gf-forms-feeds' ) === false && 
         strpos( $route, '/gf-forms-feeds/v1' ) === false ) {
        return $result;
    }
    
    $user = wp_get_current_user();
    
    if ( ! $user || ! $user->exists() ) {
        return new WP_Error(
            'rest_forbidden',
            __( 'Authentication required. Please use Application Password credentials.' ),
            array( 'status' => 401 )
        );
    }
    
    $auth_header = $request->get_header( 'authorization' );
    
    if ( empty( $auth_header ) ) {
        return new WP_Error(
            'rest_forbidden',
            __( 'Application Password authentication required. Browser login not allowed.' ),
            array( 'status' => 403 )
        );
    }
    
    return $result;
    
}, 10, 3 );
/**
 * Debug endpoint
 */
add_action( 'rest_api_init', function() {
    register_rest_route( 'gf-forms-feeds/v1', '/test', array(
        'methods'  => 'GET',
        'callback' => function() {
            
            $posts = get_posts( array(
                'post_type' => 'gf_form_feeds',
                'numberposts' => 1,
            ) );
            
            if ( empty( $posts ) ) {
                return new WP_REST_Response( array(
                    'status' => 'error',
                    'message' => 'No GF posts found. Run sync first.',
                ), 404 );
            }
            
            $post = $posts[0];
            
            return new WP_REST_Response( array(
                'status' => 'success',
                'message' => 'Gravity Forms REST API is working!',
                'post_info' => array(
                    'id' => $post->ID,
                    'title' => $post->post_title,
                ),
                'total_fields_available' => 18,
            ), 200 );
        },
        'permission_callback' => '__return_true',
    ) );
} );

Comments

Add a Comment