Home / Admin / WpPages_DIR_AA_CPT – WordPress Pages Sync CPT Registration
Duplicate Snippet

Embed Snippet on Your Site

WpPages_DIR_AA_CPT – WordPress Pages Sync CPT Registration

* Description: Registers custom post type for WordPress pages data with full REST API support
* Location: Run Everywhere
* Priority: 0
*
* Purpose: Create a mirror/snapshot of WordPress pages for safe Airtable/WhaleSync integration
* without exposing actual pages to bidirectional sync vulnerabilities
*

ismail daugherty PRO
<10
Code Preview
php
<?php
/**
 * WPCode Snippet: WordPress Pages Sync CPT Registration
 * Description: Registers custom post type for WordPress pages data with full REST API support
 * Location: Run Everywhere
 * Priority: 0
 * 
 * Purpose: Create a mirror/snapshot of WordPress pages for safe Airtable/WhaleSync integration
 * without exposing actual pages to bidirectional sync vulnerabilities
 */
defined( 'ABSPATH' ) || exit;
add_action( 'init', function() {
    $labels = [
        'name'                     => 'WP Pages Sync',
        'singular_name'            => 'Page Sync',
        'menu_name'                => 'WP Pages Sync',
        'all_items'                => 'All Pages',
        'add_new'                  => 'Add New',
        'add_new_item'             => 'Add New Page Sync',
        'edit_item'                => 'Edit Page Sync',
        'new_item'                 => 'New Page Sync',
        'view_item'                => 'View Page Details',
        'search_items'             => 'Search Pages',
        'not_found'                => 'No pages found',
        'not_found_in_trash'       => 'No pages found in Trash',
        'parent_item_colon'        => 'Parent Page:',
        'item_updated'             => 'Page sync updated',
        'item_published'           => 'Page sync published',
        'item_reverted_to_draft'   => 'Page sync reverted to draft',
    ];
    
    register_post_type( 'wp_pages_sync', [
        'labels'              => $labels,
        'description'         => 'WordPress pages data mirror with comprehensive content analysis including Gutenberg blocks, HTML, shortcodes, WP Fusion protection, and access restrictions',
        'public'              => false,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'menu_position'       => 31,
        'menu_icon'           => 'dashicons-admin-page',
        
        // REST API support - CRITICAL for WhaleSync/Airtable
        'show_in_rest'        => true,
        'rest_base'           => 'wp-pages-sync',
        'rest_controller_class' => 'WP_REST_Posts_Controller',
        
        // Security
        'capability_type'     => 'post',
        'map_meta_cap'        => true,
        
        // Features - MUST include 'custom-fields' for meta exposure
        'supports'            => [ 'title', 'custom-fields', 'revisions', 'page-attributes' ],
        'hierarchical'        => true,  // Support parent/child like pages
        'has_archive'         => false,
        'rewrite'             => false,
        'query_var'           => true,
        'exclude_from_search' => true,
        'can_export'          => true,
        'delete_with_user'    => false,
        
        // Admin columns
        'show_in_admin_bar'   => false,
    ] );
    
    // Flush rewrite rules on activation (only once)
    if ( get_option( 'wp_pages_sync_flush_rewrite_rules' ) !== 'done' ) {
        flush_rewrite_rules();
        update_option( 'wp_pages_sync_flush_rewrite_rules', 'done' );
    }
    
}, 0 ); // Priority 0 to register early
/**
 * Add custom admin columns for better overview
 */
add_filter( 'manage_wp_pages_sync_posts_columns', function( $columns ) {
    // Remove default columns
    unset( $columns['date'] );
    
    // Add custom columns
    $new_columns = [
        'cb'                => $columns['cb'],
        'title'             => 'Page Title',
        'page_id'           => 'Page ID',
        'page_status'       => 'Status',
        'page_template'     => 'Template',
        'blocks_count'      => 'Blocks',
        'special_content'   => 'Special Content',
        'wp_fusion'         => 'Protected',
        'last_sync'         => 'Last Synced',
    ];
    
    return $new_columns;
} );
/**
 * Populate custom admin columns
 */
add_action( 'manage_wp_pages_sync_posts_custom_column', function( $column, $post_id ) {
    switch ( $column ) {
        case 'page_id':
            $page_id = get_post_meta( $post_id, 'page_id', true );
            if ( $page_id ) {
                $edit_link = admin_url( 'post.php?post=' . $page_id . '&action=edit' );
                echo '<a href="' . esc_url( $edit_link ) . '" target="_blank">' . $page_id . '</a>';
            } else {
                echo '—';
            }
            break;
            
        case 'page_status':
            $status = get_post_meta( $post_id, 'page_status', true );
            if ( $status === 'publish' ) {
                echo '<span style="color: green;">●</span> Published';
            } elseif ( $status === 'draft' ) {
                echo '<span style="color: orange;">●</span> Draft';
            } elseif ( $status === 'private' ) {
                echo '<span style="color: blue;">●</span> Private';
            } else {
                echo ucfirst( $status );
            }
            break;
            
        case 'page_template':
            $template = get_post_meta( $post_id, 'page_template', true );
            if ( $template && $template !== 'default' ) {
                echo basename( $template, '.php' );
            } else {
                echo 'Default';
            }
            break;
            
        case 'blocks_count':
            $count = get_post_meta( $post_id, 'blocks_count', true );
            echo $count ? $count : '0';
            break;
            
        case 'special_content':
            $badges = [];
            
            // GravityView
            if ( get_post_meta( $post_id, 'has_gravityview_blocks', true ) ) {
                $gv_count = get_post_meta( $post_id, 'gravityview_blocks_count', true );
                $badges[] = '<span style="background: #7c3aed; color: white; padding: 2px 6px; border-radius: 3px; font-size: 10px; font-weight: bold;" title="GravityView Blocks">GV×' . $gv_count . '</span>';
            }
            
            // Gravity Forms
            if ( get_post_meta( $post_id, 'has_gravity_forms_blocks', true ) ) {
                $gf_count = get_post_meta( $post_id, 'gravity_forms_blocks_count', true );
                $badges[] = '<span style="background: #f97316; color: white; padding: 2px 6px; border-radius: 3px; font-size: 10px; font-weight: bold;" title="Gravity Forms">GF×' . $gf_count . '</span>';
            }
            
            // LifterLMS
            if ( get_post_meta( $post_id, 'has_lifterlms_blocks', true ) ) {
                $lms_count = get_post_meta( $post_id, 'lifterlms_blocks_count', true );
                $badges[] = '<span style="background: #2563eb; color: white; padding: 2px 6px; border-radius: 3px; font-size: 10px; font-weight: bold;" title="LifterLMS Blocks">LMS×' . $lms_count . '</span>';
            }
            
            // iFrames
            if ( get_post_meta( $post_id, 'has_iframes', true ) ) {
                $iframe_count = get_post_meta( $post_id, 'iframes_count', true );
                $badges[] = '<span style="background: #059669; color: white; padding: 2px 6px; border-radius: 3px; font-size: 10px; font-weight: bold;" title="iFrames (Google Docs/Sheets)">IFRAME×' . $iframe_count . '</span>';
            }
            
            // Shortcodes
            if ( get_post_meta( $post_id, 'has_shortcodes', true ) ) {
                $sc_count = get_post_meta( $post_id, 'shortcodes_count', true );
                $badges[] = '<span style="background: #8b5cf6; color: white; padding: 2px 6px; border-radius: 3px; font-size: 10px; font-weight: bold;" title="Shortcodes">SC×' . $sc_count . '</span>';
            }
            
            // HTML blocks
            if ( get_post_meta( $post_id, 'has_html_blocks', true ) ) {
                $html_count = get_post_meta( $post_id, 'html_blocks_count', true );
                $badges[] = '<span style="background: #64748b; color: white; padding: 2px 6px; border-radius: 3px; font-size: 10px; font-weight: bold;" title="HTML Blocks">HTML×' . $html_count . '</span>';
            }
            
            if ( ! empty( $badges ) ) {
                echo implode( ' ', $badges );
            } else {
                echo '<span style="color: #999;">—</span>';
            }
            break;
            
        case 'wp_fusion':
            $protected = get_post_meta( $post_id, 'wp_fusion_protected', true );
            if ( $protected ) {
                $tags_count = get_post_meta( $post_id, 'wp_fusion_tags_count', true );
                echo '<span style="color: red;">🔒</span>';
                if ( $tags_count ) {
                    echo ' <span style="font-size: 10px; color: #666;">(' . $tags_count . ')</span>';
                }
            } else {
                echo '<span style="color: #999;">—</span>';
            }
            break;
            
        case 'last_sync':
            $last_sync = get_post_meta( $post_id, 'last_sync', true );
            if ( $last_sync ) {
                echo human_time_diff( strtotime( $last_sync ), current_time( 'timestamp' ) ) . ' ago';
            } else {
                echo '—';
            }
            break;
    }
}, 10, 2 );
/**
 * Make columns sortable
 */
add_filter( 'manage_edit-wp_pages_sync_sortable_columns', function( $columns ) {
    $columns['page_id'] = 'page_id';
    $columns['page_status'] = 'page_status';
    $columns['blocks_count'] = 'blocks_count';
    $columns['last_sync'] = 'last_sync';
    return $columns;
} );
/**
 * Handle sorting
 */
add_action( 'pre_get_posts', function( $query ) {
    if ( ! is_admin() || ! $query->is_main_query() ) {
        return;
    }
    
    if ( $query->get( 'post_type' ) !== 'wp_pages_sync' ) {
        return;
    }
    
    $orderby = $query->get( 'orderby' );
    
    switch ( $orderby ) {
        case 'page_id':
            $query->set( 'meta_key', 'page_id' );
            $query->set( 'orderby', 'meta_value_num' );
            break;
        case 'page_status':
            $query->set( 'meta_key', 'page_status' );
            $query->set( 'orderby', 'meta_value' );
            break;
        case 'blocks_count':
            $query->set( 'meta_key', 'blocks_count' );
            $query->set( 'orderby', 'meta_value_num' );
            break;
        case 'last_sync':
            $query->set( 'meta_key', 'last_sync' );
            $query->set( 'orderby', 'meta_value' );
            break;
    }
} );
/**
 * Add helpful admin notices
 */
add_action( 'admin_notices', function() {
    $screen = get_current_screen();
    
    if ( ! $screen || $screen->post_type !== 'wp_pages_sync' ) {
        return;
    }
    
    // Show notice if no pages synced yet
    $pages_count = wp_count_posts( 'wp_pages_sync' );
    if ( $pages_count->publish == 0 && $pages_count->draft == 0 ) {
        ?>
        <div class="notice notice-info">
            <p>
                <strong>ℹ️ No pages synced yet.</strong> 
                Use the sync button in the admin bar to sync your WordPress pages to this CPT.
            </p>
        </div>
        <?php
    }
} );
/**
 * Add meta box with quick info
 */
add_action( 'add_meta_boxes', function() {
    add_meta_box(
        'wp_pages_sync_info',
        '📊 Sync Information',
        function( $post ) {
            $page_id = get_post_meta( $post->ID, 'page_id', true );
            $last_sync = get_post_meta( $post->ID, 'last_sync', true );
            $blocks_count = get_post_meta( $post->ID, 'blocks_count', true );
            
            // Special content detections
            $has_gv = get_post_meta( $post->ID, 'has_gravityview_blocks', true );
            $has_gf = get_post_meta( $post->ID, 'has_gravity_forms_blocks', true );
            $has_lms = get_post_meta( $post->ID, 'has_lifterlms_blocks', true );
            $has_iframe = get_post_meta( $post->ID, 'has_iframes', true );
            $has_shortcodes = get_post_meta( $post->ID, 'has_shortcodes', true );
            $has_html = get_post_meta( $post->ID, 'has_html_blocks', true );
            $wp_fusion = get_post_meta( $post->ID, 'wp_fusion_protected', true );
            
            ?>
            <div style="padding: 10px;">
                <p><strong>Original Page ID:</strong> <?php echo $page_id ? $page_id : 'N/A'; ?></p>
                <?php if ( $page_id ) : ?>
                    <p><strong>Edit Original:</strong> 
                        <a href="<?php echo admin_url( 'post.php?post=' . $page_id . '&action=edit' ); ?>" target="_blank">
                            Edit Page
                        </a>
                    </p>
                    <p><strong>View Live:</strong> 
                        <a href="<?php echo get_permalink( $page_id ); ?>" target="_blank">
                            View Page
                        </a>
                    </p>
                <?php endif; ?>
                
                <hr style="margin: 15px 0;">
                
                <p><strong>Total Blocks:</strong> <?php echo $blocks_count ? $blocks_count : '0'; ?></p>
                
                <p><strong>Special Content Detected:</strong></p>
                <ul style="margin: 5px 0 10px 20px;">
                    <?php if ( $has_gv ) : ?>
                        <li>🟣 GravityView Blocks (<?php echo get_post_meta( $post->ID, 'gravityview_blocks_count', true ); ?>)</li>
                    <?php endif; ?>
                    
                    <?php if ( $has_gf ) : ?>
                        <li>🟠 Gravity Forms Blocks (<?php echo get_post_meta( $post->ID, 'gravity_forms_blocks_count', true ); ?>)</li>
                    <?php endif; ?>
                    
                    <?php if ( $has_lms ) : ?>
                        <li>🔵 LifterLMS Blocks (<?php echo get_post_meta( $post->ID, 'lifterlms_blocks_count', true ); ?>)</li>
                    <?php endif; ?>
                    
                    <?php if ( $has_iframe ) : ?>
                        <li>🟢 iFrames/Embeds (<?php echo get_post_meta( $post->ID, 'iframes_count', true ); ?>)</li>
                    <?php endif; ?>
                    
                    <?php if ( $has_shortcodes ) : ?>
                        <li>🟣 Shortcodes (<?php echo get_post_meta( $post->ID, 'shortcodes_count', true ); ?>)</li>
                    <?php endif; ?>
                    
                    <?php if ( $has_html ) : ?>
                        <li>⚪ HTML Blocks (<?php echo get_post_meta( $post->ID, 'html_blocks_count', true ); ?>)</li>
                    <?php endif; ?>
                    
                    <?php if ( ! $has_gv && ! $has_gf && ! $has_lms && ! $has_iframe && ! $has_shortcodes && ! $has_html ) : ?>
                        <li style="color: #999;">None detected</li>
                    <?php endif; ?>
                </ul>
                
                <p><strong>WP Fusion Protection:</strong> 
                    <?php 
                    if ( $wp_fusion ) {
                        echo '🔒 Protected';
                        $tags_count = get_post_meta( $post->ID, 'wp_fusion_tags_count', true );
                        if ( $tags_count ) {
                            echo ' (' . $tags_count . ' tags)';
                        }
                    } else {
                        echo 'Public';
                    }
                    ?>
                </p>
                
                <hr style="margin: 15px 0;">
                
                <p><strong>Last Synced:</strong> 
                    <?php 
                    if ( $last_sync ) {
                        echo date( 'F j, Y g:i a', strtotime( $last_sync ) );
                        echo ' (' . human_time_diff( strtotime( $last_sync ), current_time( 'timestamp' ) ) . ' ago)';
                    } else {
                        echo 'Never';
                    }
                    ?>
                </p>
                
                <p style="color: #666; font-size: 11px; margin-top: 15px;">
                    <em>This is a read-only mirror of the original WordPress page. 
                    All edits should be made to the original page.</em>
                </p>
            </div>
            <?php
        },
        'wp_pages_sync',
        'side',
        'high'
    );
} );
/**
 * Prevent direct editing of synced posts (they should only be updated via sync)
 */
add_action( 'admin_notices', function() {
    $screen = get_current_screen();
    
    if ( ! $screen || $screen->id !== 'wp_pages_sync' || ! isset( $_GET['action'] ) || $_GET['action'] !== 'edit' ) {
        return;
    }
    
    ?>
    <div class="notice notice-warning">
        <p>
            <strong>⚠️ Read-Only:</strong> 
            This is a synced copy of a WordPress page. To make changes, edit the original page and re-sync.
        </p>
    </div>
    <?php
} );
/**
 * Add helpful links to admin bar
 */
add_action( 'admin_bar_menu', function( $wp_admin_bar ) {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }
    
    // Only show on wp_pages_sync pages
    $screen = get_current_screen();
    if ( $screen && $screen->post_type === 'wp_pages_sync' ) {
        $wp_admin_bar->add_node( [
            'id'    => 'wp_pages_sync_help',
            'title' => '❓ Help',
            'href'  => '#',
            'meta'  => [
                'title' => 'WP Pages Sync System',
                'onclick' => 'alert("WP Pages Sync System\n\nThis CPT mirrors WordPress pages for safe Airtable/WhaleSync integration.\n\nFeatures:\n• Comprehensive block analysis\n• HTML content preservation\n• WP Fusion protection detection\n• Shortcode identification\n• LifterLMS block detection\n\nUse the sync button in admin bar to update data."); return false;',
            ],
        ] );
    }
}, 100 );
/**
 * Log CPT registration for debugging
 */
add_action( 'init', function() {
    error_log( 'WP_PAGES_SYNC: CPT registered successfully' );
}, 999 );

Comments

Add a Comment