| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
|
| add_action( 'rest_api_init', function() {
|
|
|
| $wp_pages_fields = array(
|
|
|
| 'wp_page_edit_link' => 'string',
|
| 'wp_page_view_link' => 'string',
|
|
|
|
|
| 'page_url' => 'string',
|
| 'page_slug' => 'string',
|
|
|
|
|
| 'page_id' => 'integer',
|
| 'page_status' => 'string',
|
| 'page_template' => 'string',
|
| 'page_parent_id' => 'integer',
|
| 'page_order' => 'integer',
|
|
|
|
|
| 'page_author_id' => 'integer',
|
| 'page_author_name' => 'string',
|
| 'page_author_email' => 'string',
|
| 'page_author_role' => 'string',
|
|
|
|
|
| 'page_date_created' => 'string',
|
| 'page_date_modified' => 'string',
|
| 'page_date_published' => 'string',
|
|
|
|
|
| 'page_visibility' => 'string',
|
| 'page_password' => 'string',
|
| 'comment_status' => 'string',
|
|
|
|
|
| '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_url' => 'string',
|
| 'featured_image_id' => 'integer',
|
|
|
|
|
| 'custom_taxonomies' => 'string',
|
|
|
|
|
| 'theme_settings' => 'string',
|
|
|
|
|
| 'last_sync' => 'string',
|
|
|
|
|
| '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',
|
|
|
|
|
| 'has_gravityview_blocks' => 'boolean',
|
| 'gravityview_blocks_details' => 'string',
|
|
|
|
|
| 'has_gravity_forms_blocks' => 'boolean',
|
| 'gravity_forms_blocks_details' => 'string',
|
|
|
|
|
| 'has_lifterlms_blocks' => 'boolean',
|
| 'lifterlms_blocks_details' => 'string',
|
|
|
|
|
| 'has_iframes' => 'boolean',
|
| 'iframes_details' => 'string',
|
|
|
|
|
| 'wp_fusion_protected' => 'boolean',
|
| 'wp_fusion_tags_count' => 'integer',
|
| 'wp_fusion_details' => 'string',
|
|
|
|
|
| 'has_shortcodes' => 'boolean',
|
| 'shortcodes_details' => 'string',
|
|
|
|
|
| 'has_html_blocks' => 'boolean',
|
| 'html_blocks_details' => 'string',
|
|
|
|
|
| 'block_analysis' => 'string',
|
|
|
|
|
| 'page_content' => 'string',
|
| 'page_excerpt' => 'string',
|
| );
|
|
|
|
|
| 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' ),
|
| ),
|
| ) );
|
| }
|
|
|
| } );
|
|
|
|
|
|
|
|
|
| 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 );
|
|
|
|
|
|
|
|
|
| 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 );
|
|
|
|
|
|
|
|
|
| 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];
|
|
|
|
|
| $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',
|
| ) );
|
| } );
|
|
|
|
|
|
|
|
|
| add_action( 'rest_api_init', function() {
|
| error_log( 'WP_PAGES_SYNC_REST_API: All fields registered successfully' );
|
| }, 999 );
|
| |
| |
Comments