Home / Admin / WpPages_DIR_AC_RAPI – Expose WordPress Pages Sync ACF Fields to REST API
Duplicate Snippet

Embed Snippet on Your Site

WpPages_DIR_AC_RAPI – Expose WordPress Pages Sync ACF Fields to REST API

* Description: Makes WordPress Pages data accessible via Application Password authentication
* Location: Run Everywhere
* Priority: 15
*/

ismail daugherty PRO
<10
Code Preview
php
<?php
/**
 * WPCode Snippet: Expose WordPress Pages Sync ACF Fields to REST API
 * Description: Makes WordPress Pages data accessible via Application Password authentication
 * Location: Run Everywhere
 * Priority: 15
 */
defined( 'ABSPATH' ) || exit;
/**
 * Register WordPress Pages Sync ACF meta fields in REST API
 */
add_action( 'rest_api_init', function() {
    
    $wp_pages_fields = array(
        // Quick Access Links
        'wp_page_edit_link'    => 'string',
        'wp_page_view_link'    => 'string',
        
        // URLs & Permalinks - CRITICAL
        'page_url'             => 'string',
        'page_slug'            => 'string',
        
        // Basic Page Info
        'page_id'              => 'integer',
        'page_status'          => 'string',
        'page_template'        => 'string',
        'page_parent_id'       => 'integer',
        'page_order'           => 'integer',
        
        // Author Information
        'page_author_id'       => 'integer',
        'page_author_name'     => 'string',
        'page_author_email'    => 'string',
        'page_author_role'     => 'string',
        
        // Date/Time Information
        'page_date_created'    => 'string',
        'page_date_modified'   => 'string',
        'page_date_published'  => 'string',
        
        // Access & Visibility
        'page_visibility'      => 'string',
        'page_password'        => 'string',
        'comment_status'       => 'string',
        
        // Block Counts
        'blocks_count'               => 'integer',
        'gravityview_blocks_count'   => 'integer',
        'gravity_forms_blocks_count' => 'integer',
        'lifterlms_blocks_count'     => 'integer',
        'iframes_count'              => 'integer',
        'shortcodes_count'           => 'integer',
        'html_blocks_count'          => 'integer',
        
        // Featured Image
        'featured_image_url'   => 'string',
        'featured_image_id'    => 'integer',
        
        // Custom Taxonomies
        'custom_taxonomies'    => 'string',
        
        // Theme Settings
        'theme_settings'       => 'string',
        
        // Last Sync
        'last_sync'            => 'string',
        
        // RankMath SEO Data
        'seo_enabled'          => 'boolean',
        'seo_title'            => 'string',
        'seo_description'      => 'string',
        'seo_focus_keyword'    => 'string',
        'seo_score'            => 'string',
        'seo_canonical_url'    => 'string',
        'og_title'             => 'string',
        'og_description'       => 'string',
        'og_image_url'         => 'string',
        'seo_robots'           => 'string',
        'seo_schema_type'      => 'string',
        'seo_complete_data'    => 'string',
        
        // GravityView Blocks
        'has_gravityview_blocks'      => 'boolean',
        'gravityview_blocks_details'  => 'string',
        
        // Gravity Forms Blocks
        'has_gravity_forms_blocks'       => 'boolean',
        'gravity_forms_blocks_details'   => 'string',
        
        // LifterLMS Blocks
        'has_lifterlms_blocks'    => 'boolean',
        'lifterlms_blocks_details' => 'string',
        
        // iFrames & Embeds
        'has_iframes'         => 'boolean',
        'iframes_details'     => 'string',
        
        // WP Fusion Protection
        'wp_fusion_protected'   => 'boolean',
        'wp_fusion_tags_count'  => 'integer',
        'wp_fusion_details'     => 'string',
        
        // Shortcodes
        'has_shortcodes'      => 'boolean',
        'shortcodes_details'  => 'string',
        
        // HTML Blocks
        'has_html_blocks'       => 'boolean',
        'html_blocks_details'   => 'string',
        
        // Block Analysis
        'block_analysis'      => 'string',
        
        // Page Content
        'page_content'        => 'string',
        'page_excerpt'        => 'string',
    );
    
    // Register WordPress Pages Sync fields
    foreach ( $wp_pages_fields as $field_name => $field_type ) {
        register_rest_field( 'wp_pages_sync', $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 WordPress Pages Sync CPT
 */
add_filter( 'register_post_type_args', function( $args, $post_type ) {
    
    if ( $post_type === 'wp_pages_sync' ) {
        $args['show_in_rest'] = true;
        $args['rest_base'] = 'wp-pages-sync';
        $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/wp-pages-sync' ) === false && 
         strpos( $route, '/wp-pages-sync/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( 'wp-pages-sync/v1', '/test', array(
        'methods'  => 'GET',
        'callback' => function() {
            
            $posts = get_posts( array(
                'post_type' => 'wp_pages_sync',
                'numberposts' => 1,
            ) );
            
            if ( empty( $posts ) ) {
                return new WP_REST_Response( array(
                    'status' => 'error',
                    'message' => 'No WordPress Pages Sync posts found. Run sync first.',
                ), 404 );
            }
            
            $post = $posts[0];
            
            // Get sample field data
            $sample_data = array(
                'page_url' => get_post_meta( $post->ID, 'page_url', true ),
                'page_id' => get_post_meta( $post->ID, 'page_id', true ),
                'page_status' => get_post_meta( $post->ID, 'page_status', true ),
                'has_gravityview_blocks' => get_post_meta( $post->ID, 'has_gravityview_blocks', true ),
                'has_gravity_forms_blocks' => get_post_meta( $post->ID, 'has_gravity_forms_blocks', true ),
                'wp_fusion_protected' => get_post_meta( $post->ID, 'wp_fusion_protected', true ),
            );
            
            return new WP_REST_Response( array(
                'status' => 'success',
                'message' => 'WordPress Pages Sync REST API is working!',
                'post_info' => array(
                    'id' => $post->ID,
                    'title' => $post->post_title,
                ),
                'total_fields_available' => 65,
                'sample_data' => $sample_data,
                'endpoints' => array(
                    'list_all' => home_url( '/wp-json/wp/v2/wp-pages-sync' ),
                    'single' => home_url( '/wp-json/wp/v2/wp-pages-sync/' . $post->ID ),
                    'test' => home_url( '/wp-json/wp-pages-sync/v1/test' ),
                ),
            ), 200 );
        },
        'permission_callback' => '__return_true',
    ) );
} );
/**
 * Log REST API registration
 */
add_action( 'rest_api_init', function() {
    error_log( 'WP_PAGES_SYNC_REST_API: All fields registered successfully' );
}, 999 );

Comments

Add a Comment