Siteβ―Builder
Editing:
BDO-Page-Maker.php
writable 0666
<?php /* Plugin Name: BDO PageΒ Maker Description: Miniβsite builder for the BestDealOn Multisite network. Each siteβs slug is a 10βdigit phone number (set by the PhoneβNumberβSlug plugin). Published **bdo_page** posts are rendered to static HTML inside /ph/{phone}/, with βhomeβ saved as index.html. Version: 0.3.0 Author: BestDealOnΒ DevΒ Team Network: true */ if ( ! defined( 'ABSPATH' ) ) { exit; // safety first } // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ // 1. CONSTANTS // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ // Absolute filesystem base where static miniβsites live. *No* trailing slash. // e.g. /home/bestdealon/public_html/ph if ( ! defined( 'BDO_PM_STATIC_BASE' ) ) { define( 'BDO_PM_STATIC_BASE', '/home/bestdealon/public_html/ph' ); } // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ // 2. HELPERS // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ /** * Fetch the 10βdigit phone number that identifies this site. */ function bdo_pm_get_phone_digits() : string { $blog_id = get_current_blog_id(); // Preferred: blogmeta / siteβmeta (WP β₯5.1) if ( function_exists( 'get_blog_meta' ) ) { $digits = (string) get_blog_meta( $blog_id, 'pns_phone', true ); if ( $digits ) { return $digits; } } elseif ( function_exists( 'get_site_meta' ) ) { // fallback alias $digits = (string) get_site_meta( $blog_id, 'pns_phone', true ); if ( $digits ) { return $digits; } } // Fallback: try to glean from the subβdirectory URL itself. $path = trim( parse_url( home_url( '/' ), PHP_URL_PATH ), '/' ); if ( preg_match( '/^(\d{10})$/', $path, $m ) ) { return $m[1]; } return '0000000000'; // emergency placeholder so we never write outside BASE } /** * Directory for this siteβs static pages, e.g. β¦/ph/1234567890 */ function bdo_pm_static_dir() : string { return trailingslashit( BDO_PM_STATIC_BASE ) . bdo_pm_get_phone_digits(); } /** * Write static HTML file (slug.html or index.html). */ function bdo_pm_write_static( string $slug, string $html ) : bool { $dir = bdo_pm_static_dir(); if ( ! wp_mkdir_p( $dir ) ) { error_log( '[BDO_PM] mkdir failed β ' . $dir ); return false; } // "home" slug becomes index.html so default /ph/PHONE/ resolves. $file = trailingslashit( $dir ) . ( $slug === 'home' ? 'index' : $slug ) . '.html'; if ( file_put_contents( $file, $html ) === false ) { error_log( '[BDO_PM] write failed β ' . $file ); return false; } return true; } // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ // 3. CUSTOM POST TYPE β one βbdo_pageβ = one static page // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ function bdo_pm_register_cpt() { register_post_type( 'bdo_page', [ 'label' => 'MiniβSite Pages', 'public' => true, 'show_in_menu' => true, 'menu_icon' => 'dashicons-media-default', 'supports' => [ 'title', 'editor', 'thumbnail', 'excerpt', 'revisions' ], 'rewrite' => false, 'capability_type' => 'post', 'map_meta_cap' => true, 'show_in_rest' => true, ] ); } add_action( 'init', 'bdo_pm_register_cpt' ); // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ // 3.a OPTIONAL: force Classic (TinyMCE) editor for bdo_page add_filter( 'use_block_editor_for_post_type', function ( $use_block, $post_type ) { // Disable Gutenberg only for our custom post type β keep default for others return ( $post_type === 'bdo_page' ) ? false : $use_block; }, 10, 2 ); // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ // 4. STATIC RENDER on save/publish // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ function bdo_pm_render_static( int $post_id, WP_Post $post ) { if ( wp_is_post_revision( $post_id ) || $post->post_type !== 'bdo_page' || $post->post_status !== 'publish' ) { return; } $slug = $post->post_name ?: sanitize_title( $post->post_title ); $title = $post->post_title; // Render blocks/shortcodes. $body = apply_filters( 'the_content', $post->post_content ); $body = bdo_pm_expand_placeholders( $body ); // VERY minimal template β replace with your own. $html = '<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">' . '<title>' . esc_html( $title ) . '</title>' . '<style>body{font-family:system-ui,Arial,sans-serif;max-width:800px;margin:2rem auto;line-height:1.6;color:#333;padding:0 1rem;}img{max-width:100%;height:auto;}</style>' . '</head><body>' . $body . bdo_pm_footer_nav() . '</body></html>'; if ( bdo_pm_write_static( $slug, $html ) ) { update_post_meta( $post_id, '_bdo_pm_last_build', current_time( 'mysql' ) ); } } add_action( 'save_post_bdo_page', 'bdo_pm_render_static', 10, 2 ); /** * Mini footer nav linking all published bdo_page posts for this site. */ function bdo_pm_footer_nav() : string { $pages = get_posts( [ 'post_type' => 'bdo_page', 'post_status' => 'publish', 'numberposts' => -1 ] ); if ( ! $pages ) return ''; $digits = bdo_pm_get_phone_digits(); $base = '/ph/' . $digits . '/'; $out = '<nav style="margin-top:3rem;border-top:1px solid #ccc;padding-top:1rem;font-size:0.9rem;">'; foreach ( $pages as $p ) { $slug = $p->post_name ?: sanitize_title( $p->post_title ); $href = $base . ( $slug === 'home' ? '' : $slug . '.html' ); $out .= '<a style="margin-right:1rem;" href="' . esc_attr( $href ) . '">' . esc_html( $p->post_title ) . '</a>'; } return $out . '</nav>'; } /** * Replace placeholders like {{PHONE}}. */ function bdo_pm_expand_placeholders( string $html ) : string { $digits = bdo_pm_get_phone_digits(); $pretty = '('.substr($digits,0,3).') '.substr($digits,3,3).'-'.substr($digits,6); return str_replace( '{{PHONE}}', esc_html( $pretty ), $html ); } // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ // 5. ADMIN BUILDER screen (unchanged) // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ function bdo_pm_admin_menu() { add_submenu_page( 'edit.php?post_type=bdo_page', 'MiniβSite Builder', 'MiniβSite Builder', 'edit_posts', 'bdo_pm_builder', 'bdo_pm_render_builder' ); } add_action( 'admin_menu', 'bdo_pm_admin_menu' ); function bdo_pm_render_builder() { $required = [ 'home', 'about', 'services', 'contact' ]; $existing = get_posts( [ 'post_type' => 'bdo_page', 'numberposts' => -1, 'post_status' => [ 'publish', 'draft' ] ] ); $map = []; foreach ( $existing as $p ) $map[ $p->post_name ] = $p; echo '<div class="wrap"><h1>MiniβSite Builder</h1><table class="widefat"><thead><tr><th>Page</th><th>Status</th><th>Action</th></tr></thead><tbody>'; foreach ( $required as $slug ) { $page = $map[ $slug ] ?? null; echo '<tr>'; echo '<td>' . esc_html( ucfirst( $slug ) ) . '</td>'; if ( $page ) { $built = get_post_meta( $page->ID, '_bdo_pm_last_build', true ); echo '<td>Exists (' . esc_html( $page->post_status ) . ') | Last build: ' . esc_html( $built ?: 'β' ) . '</td>'; echo '<td><a class="button" href="' . esc_url( get_edit_post_link( $page->ID ) ) . '">Edit</a></td>'; } else { $url = admin_url( 'post-new.php?post_type=bdo_page&post_title=' . urlencode( ucfirst( $slug ) ) . '&post_name=' . $slug ); echo '<td><span style="color:#dc3232">Missing</span></td>'; echo '<td><a class="button button-primary" href="' . esc_url( $url ) . '">Create</a></td>'; } echo '</tr>'; } echo '</tbody></table></div>'; } // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ // 6. CLEANβUP β remove static HTML if the page is deleted // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ function bdo_pm_delete_static( int $post_id ) { $post = get_post( $post_id ); if ( $post->post_type !== 'bdo_page' ) return; $file = trailingslashit( bdo_pm_static_dir() ) . ( $post->post_name === 'home' ? 'index' : $post->post_name ) . '.html'; if ( file_exists( $file ) ) unlink( $file ); } add_action( 'before_delete_post', 'bdo_pm_delete_static' ); // End plugin ?>
Save changes
Create folder
writable 0777
Create
Cancel