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 .= '