Home / Admin / DIR_RAPI_WPCODE – REST API Enhancements for WPCode Snippets Sync
Duplicate Snippet

Embed Snippet on Your Site

DIR_RAPI_WPCODE – REST API Enhancements for WPCode Snippets Sync

* WPCode Snippet: REST API Enhancements for WPCode Snippets Sync
* Description: Exposes wpcode_snippets_sync CPT fields via REST API for WhaleSync
* Location: Run Everywhere
* Priority: 15
*
* INSTALL ORDER: #4 - Install last, after sync system is set up
*
* IMPORTANT: This exposes the SHADOW CPT (wpcode_snippets_sync), NOT the actual wpcode CPT
* ✅ UPDATED: Now exposes snippet_code_file attachment field with download URL
*
* IMPORTANT: This exposes the SHADOW CPT (wpcode_snippets_sync), NOT the actual wpcode CPT

ismail daugherty PRO
<10
Code Preview
php
<?php
/**
 * WPCode Snippet: REST API Enhancements for WPCode Snippets Sync
 * Description: Exposes wpcode_snippets_sync CPT fields via REST API for WhaleSync
 * Location: Run Everywhere
 * Priority: 15
 * 
 * INSTALL ORDER: #4 - Install last, after sync system is set up
 * 
 * IMPORTANT: This exposes the SHADOW CPT (wpcode_snippets_sync), NOT the actual wpcode CPT
 * ✅ UPDATED: Now exposes snippet_code_file attachment field with download URL
 */
defined( 'ABSPATH' ) || exit;
/**
 * Register all ACF fields in REST API for wpcode_snippets_sync
 */
add_action( 'rest_api_init', function() {
    
    // Core identifier fields
    $core_fields = array(
        'original_wpcode_id',
        'kic_snippet_id',
        'snippet_active',
        'last_sync_time',
    );
    
    foreach ( $core_fields as $field_name ) {
        register_rest_field( 'wpcode_snippets_sync', $field_name, array(
            'get_callback' => function( $object ) use ( $field_name ) {
                $value = get_field( $field_name, $object['id'] );
                
                // Handle boolean fields
                if ( in_array( $field_name, array( 'snippet_active' ) ) ) {
                    return (bool) $value;
                }
                
                // Return as string for WhaleSync compatibility
                return strval( $value );
            },
            'schema' => array(
                'description' => sprintf( 'Field: %s', $field_name ),
                'type'        => in_array( $field_name, array( 'snippet_active' ) ) ? 'boolean' : 'string',
                'context'     => array( 'view', 'edit' ),
            ),
        ) );
    }
    
    // Code and type fields
    $code_fields = array(
        'snippet_title',
        'snippet_code',
        'snippet_type',
        'snippet_location',
        'snippet_priority',
    );
    
    foreach ( $code_fields as $field_name ) {
        register_rest_field( 'wpcode_snippets_sync', $field_name, array(
            'get_callback' => function( $object ) use ( $field_name ) {
                $value = get_field( $field_name, $object['id'] );
                
                if ( 'snippet_priority' === $field_name ) {
                    return (int) $value;
                }
                
                return $value;
            },
            'schema' => array(
                'description' => sprintf( 'Field: %s', $field_name ),
                'type'        => 'snippet_priority' === $field_name ? 'integer' : 'string',
                'context'     => array( 'view', 'edit' ),
            ),
        ) );
    }
    
    // ==========================================
    // ✅ NEW: Code File Attachment
    // ==========================================
    register_rest_field( 'wpcode_snippets_sync', 'snippet_code_file', array(
        'get_callback' => function( $object ) {
            $file = get_field( 'snippet_code_file', $object['id'] );
            
            // ACF file field returns array with: ID, url, filename, filesize, etc.
            if ( $file && is_array( $file ) ) {
                return array(
                    'id'       => $file['ID'],
                    'url'      => $file['url'],
                    'filename' => $file['filename'],
                    'filesize' => $file['filesize'],
                    'type'     => $file['type'],
                    'subtype'  => $file['subtype'],
                    'title'    => $file['title'],
                );
            }
            
            return null;
        },
        'schema' => array(
            'description' => 'Downloadable code file attachment',
            'type'        => 'object',
            'context'     => array( 'view', 'edit' ),
            'properties'  => array(
                'id'       => array( 'type' => 'integer' ),
                'url'      => array( 'type' => 'string', 'format' => 'uri' ),
                'filename' => array( 'type' => 'string' ),
                'filesize' => array( 'type' => 'integer' ),
                'type'     => array( 'type' => 'string' ),
                'subtype'  => array( 'type' => 'string' ),
                'title'    => array( 'type' => 'string' ),
            ),
        ),
    ) );
    
    // Targeting fields
    $targeting_fields = array(
        'snippet_auto_insert',
        'snippet_device_type',
        'snippet_schedule_start',
        'snippet_schedule_end',
    );
    
    foreach ( $targeting_fields as $field_name ) {
        register_rest_field( 'wpcode_snippets_sync', $field_name, array(
            'get_callback' => function( $object ) use ( $field_name ) {
                $value = get_field( $field_name, $object['id'] );
                
                if ( 'snippet_auto_insert' === $field_name ) {
                    return (bool) $value;
                }
                
                return $value;
            },
            'schema' => array(
                'description' => sprintf( 'Field: %s', $field_name ),
                'type'        => 'snippet_auto_insert' === $field_name ? 'boolean' : 'string',
                'context'     => array( 'view', 'edit' ),
            ),
        ) );
    }
    
    // Metadata fields
    $metadata_fields = array(
        'snippet_tags',
        'snippet_description',
        'snippet_dependencies',
        'snippet_created_date',
        'snippet_modified_date',
        'snippet_author',
        'snippet_version',
    );
    
    foreach ( $metadata_fields as $field_name ) {
        register_rest_field( 'wpcode_snippets_sync', $field_name, array(
            'get_callback' => function( $object ) use ( $field_name ) {
                return get_field( $field_name, $object['id'] );
            },
            'schema' => array(
                'description' => sprintf( 'Field: %s', $field_name ),
                'type'        => 'string',
                'context'     => array( 'view', 'edit' ),
            ),
        ) );
    }
    
    // Computed fields
    register_rest_field( 'wpcode_snippets_sync', 'snippet_stats', array(
        'get_callback' => function( $object ) {
            $code = get_field( 'snippet_code', $object['id'] );
            $schedule_start = get_field( 'snippet_schedule_start', $object['id'] );
            $code_file = get_field( 'snippet_code_file', $object['id'] );
            
            return array(
                'code_length'       => strlen( $code ),
                'is_scheduled'      => ! empty( $schedule_start ),
                'last_synced'       => get_field( 'last_sync_time', $object['id'] ),
                'has_code_file'     => ! empty( $code_file ),
            );
        },
        'schema' => array(
            'description' => 'Computed snippet statistics',
            'type'        => 'object',
            'context'     => array( 'view', 'edit' ),
        ),
    ) );
    
} );
/**
 * Allow filtering by snippet type in REST API
 */
add_filter( 'rest_wpcode_snippets_sync_query', function( $args, $request ) {
    
    // Filter by snippet type
    if ( ! empty( $request['snippet_type'] ) ) {
        $args['meta_query'] = isset( $args['meta_query'] ) ? $args['meta_query'] : array();
        $args['meta_query'][] = array(
            'key'   => 'snippet_type',
            'value' => sanitize_text_field( $request['snippet_type'] ),
        );
    }
    
    // Filter by active status
    if ( isset( $request['active'] ) ) {
        if ( ! isset( $args['meta_query'] ) ) {
            $args['meta_query'] = array();
        }
        $args['meta_query'][] = array(
            'key'   => 'snippet_active',
            'value' => $request['active'] ? '1' : '0',
        );
    }
    
    // Filter by device type
    if ( ! empty( $request['device_type'] ) ) {
        if ( ! isset( $args['meta_query'] ) ) {
            $args['meta_query'] = array();
        }
        $args['meta_query'][] = array(
            'key'   => 'snippet_device_type',
            'value' => sanitize_text_field( $request['device_type'] ),
        );
    }
    
    return $args;
}, 10, 2 );
/**
 * Add custom query parameters to REST API schema
 */
add_filter( 'rest_wpcode_snippets_sync_collection_params', function( $params ) {
    
    $params['snippet_type'] = array(
        'description' => 'Filter snippets by type (php, html, css, js, text)',
        'type'        => 'string',
        'enum'        => array( 'php', 'html', 'css', 'js', 'text' ),
    );
    
    $params['active'] = array(
        'description' => 'Filter snippets by active status',
        'type'        => 'boolean',
    );
    
    $params['device_type'] = array(
        'description' => 'Filter snippets by device type',
        'type'        => 'string',
        'enum'        => array( 'all', 'mobile', 'desktop' ),
    );
    
    return $params;
} );
/**
 * Modify REST response to include helpful data
 */
add_filter( 'rest_prepare_wpcode_snippets_sync', function( $response, $post, $request ) {
    $data = $response->get_data();
    
    // Add source information
    $data['_source_info'] = array(
        'cpt'          => 'wpcode_snippets_sync',
        'original_cpt' => 'wpcode',
        'sync_type'    => 'one-way-mirror',
        'read_only'    => true,
    );
    
    $response->set_data( $data );
    return $response;
}, 10, 3 );
/**
 * Register all meta fields for REST API exposure
 */
$meta_fields_to_register = array(
    'original_wpcode_id'     => 'string', // Changed from integer to string for WhaleSync
    'kic_snippet_id'         => 'string',
    'snippet_active'         => 'boolean',
    'last_sync_time'         => 'string',
    'snippet_title'          => 'string',
    'snippet_code'           => 'string',
    'snippet_code_file'      => 'integer', // Attachment ID
    'snippet_type'           => 'string',
    'snippet_location'       => 'string',
    'snippet_priority'       => 'integer',
    'snippet_auto_insert'    => 'boolean',
    'snippet_device_type'    => 'string',
    'snippet_schedule_start' => 'string',
    'snippet_schedule_end'   => 'string',
    'snippet_tags'           => 'string',
    'snippet_description'    => 'string',
    'snippet_dependencies'   => 'string',
    'snippet_created_date'   => 'string',
    'snippet_modified_date'  => 'string',
    'snippet_author'         => 'string',
    'snippet_version'        => 'string',
);
foreach ( $meta_fields_to_register as $meta_key => $type ) {
    register_post_meta( 'wpcode_snippets_sync', $meta_key, array(
        'show_in_rest' => true,
        'single'       => true,
        'type'         => $type,
    ) );
}

Comments

Add a Comment