where( 'status', '=', 'active' ) ->get(); if ( empty( $active_ad_spaces ) ) { // No active ad spaces found. return; } // Build page content. $page_content = self::build_adspace_content( $active_ad_spaces ); // Check if page already exists. $existing_page = get_page_by_path( 'adfuz-demo-homepage' ); if ( $existing_page ) { // Update existing page. $page_id = $existing_page->ID; wp_update_post( array( 'ID' => $page_id, 'post_content' => $page_content, ) ); } else { // Create new page. $page_id = wp_insert_post( array( 'post_title' => 'AdFuz Demo Homepage', 'post_name' => 'adfuz-demo-homepage', 'post_content' => $page_content, 'post_status' => 'publish', 'post_type' => 'page', ) ); } if ( $page_id && ! is_wp_error( $page_id ) ) { // Add the custom meta to identify this as our demo page. update_post_meta( $page_id, 'adfuz-demo-content', 'manual' ); update_post_meta( $page_id, 'adfuz_created_by', 'automated_setup' ); update_post_meta( $page_id, 'adfuz_creation_timestamp', current_time( 'timestamp' ) ); } } /** * Build the page content with all ad spaces */ private static function build_adspace_content( $ad_spaces ) { $content = ''; foreach ( $ad_spaces as $ad_space ) { $ad_space_id = Common::get( 'id', $ad_space ); $ad_space_title = Common::get( 'title', $ad_space ); // Add Ad Space Title as heading (using block format). $content .= '' . "\n"; $content .= '

' . esc_html( $ad_space_title ) . '

' . "\n"; $content .= '' . "\n\n"; // Add the ad space block with the ID. $content .= '' . "\n\n"; // Add separator block $content .= '' . "\n"; $content .= '
' . "\n"; $content .= '' . "\n\n"; } return $content; } /** * Set the created page as homepage */ private static function set_homepage() { $page = get_page_by_path( 'adfuz-demo-homepage' ); if ( $page ) { // Check if this is indeed our demo page with the correct meta. $is_demo_page = get_post_meta( $page->ID, 'adfuz-demo-content', true ); if ( 'manual' === $is_demo_page ) { // Set as front page. update_option( 'page_on_front', $page->ID ); update_option( 'show_on_front', 'page' ); } } } /** * Optional: Helper method to remove/clean up the demo page and reset homepage */ public static function cleanup_adfuz_demo() { $page = get_page_by_path( 'adfuz-demo-homepage' ); if ( $page ) { $is_demo_page = get_post_meta( $page->ID, 'adfuz-demo-content', true ); if ( 'manual' === $is_demo_page ) { // Reset homepage to default (blog posts) if this page is currently the homepage. $current_front_page_id = get_option( 'page_on_front' ); if ( $current_front_page_id == $page->ID ) { update_option( 'show_on_front', 'posts' ); update_option( 'page_on_front', 0 ); } // Delete the page. wp_delete_post( $page->ID, true ); } } } }