Siteβ―Builder
Editing:
BDO-Static-Cache.php
writable 0666
<?php /* Plugin Name: BDO StaticΒ Cache Description: Renders each **bdo_page** (or any post you whitelist) to flat HTML inside /home/bestdealon/public_html/ph/<10βdigitβphone>/*.html, with βhomeβ written as index.html and a miniβnav footer that links the pages together. Integrated with the PhoneβNumberβSlug network plugin. Version: 0.2.1 Author: BestDealOnΒ DevΒ Team Network: true */ if ( ! defined( 'ABSPATH' ) ) { exit; // direct access blocked } //βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ // 0. Constants β adjust if server paths ever change //βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ /* Absolute filesystem root where cached minisites live (NO trailing slash) */ const BDO_SC_ROOT_DIR = '/home/bestdealon/public_html/ph'; /* Public URL prefix that maps to the folder above (leading slash, no trailing) */ const BDO_SC_URL_BASE = '/ph'; /* Post type(s) we autoβcache. Extend with an array if you want more. */ const BDO_SC_CACHED_POST_TYPES = [ 'bdo_page' ]; /* Slug that represents the homepage (written as index.html) */ const BDO_SC_HOME_SLUG = 'home'; //βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ // 1. Helpers β phone digits, dir & filepath builders //βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ /** * Retrieve the 10βdigit phone number for this site. * Sourced from blogmeta set by the PhoneβNumberβSlug plugin, or derived from * the site path as a fallback. */ function bdo_sc_get_phone_digits(): string { $blog_id = get_current_blog_id(); // get_site_meta() alias works as get_blog_meta(). $phone = (string) get_site_meta( $blog_id, 'pns_phone', true ); if ( $phone && preg_match( '/^\d{10}$/', $phone ) ) { return $phone; } // Fallback β parse /##########/ from path $details = get_blog_details( $blog_id ); if ( $details && preg_match( '~/([0-9]{10})/~', $details->path, $m ) ) { return $m[1]; } return '0000000000'; // shouldnβt ever happen but keeps paths valid } /** Absolute dir for this siteβs static cache (ensures it exists). */ function bdo_sc_static_dir(): string { $dir = trailingslashit( BDO_SC_ROOT_DIR ) . bdo_sc_get_phone_digits(); if ( ! file_exists( $dir ) ) { wp_mkdir_p( $dir ); } return $dir; } /** Full file path for a given slug (home β index.html). */ function bdo_sc_static_path( string $slug ): string { $file = ( $slug === BDO_SC_HOME_SLUG || $slug === '' ) ? 'index.html' : $slug . '.html'; return bdo_sc_static_dir() . '/' . $file; } /** URL relative to the root domain for linking inside the minisite. */ function bdo_sc_static_url( string $slug ): string { $file = ( $slug === BDO_SC_HOME_SLUG || $slug === '' ) ? 'index.html' : $slug . '.html'; return trailingslashit( BDO_SC_URL_BASE ) . bdo_sc_get_phone_digits() . '/' . $file; } //βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ // 2. Core renderer β run on publish/update + AJAX regenerate //βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ /** * Main entry: generate (or delete) a static file when a cached post changes. */ function bdo_sc_maybe_generate_static( int $post_id ) { $post = get_post( $post_id ); if ( ! $post || $post->post_status !== 'publish' ) { return; } if ( ! in_array( $post->post_type, BDO_SC_CACHED_POST_TYPES, true ) ) { return; } $slug = $post->post_name; $title = $post->post_title; // Render the post content through WP filters (shortcodes, embeds, etc.) $content = apply_filters( 'the_content', $post->post_content ); // Mini footer nav β links to all published pages in this site $nav_items = []; $pages = get_posts([ 'post_type' => BDO_SC_CACHED_POST_TYPES, 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC', ]); foreach ( $pages as $_p ) { $_slug = $_p->post_name; $label = $_p->post_title ?: ucfirst( $_slug ); $url = bdo_sc_static_url( $_slug ); $nav_items[] = '<a href="' . esc_attr( $url ) . '">' . esc_html( $label ) . '</a>'; } $nav_html = $nav_items ? '<nav class="bdo-mini-nav">' . implode( ' | ', $nav_items ) . '</nav>' : ''; //------------------------------------------------------------------ // Header / Footer templates (swap out with real branding as needed) //------------------------------------------------------------------ $header = "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\" />\n<title>" . esc_html( $title ) . "</title>\n<meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">\n<style>body{font-family:Arial,Helvetica,sans-serif;max-width:800px;margin:0 auto;padding:20px;line-height:1.6;color:#333}img{max-width:100%;height:auto}.bdo-mini-nav{margin-top:40px;font-size:0.9em;text-align:center}</style>\n</head>\n<body>\n"; $footer = "{$nav_html}\n</body>\n</html>"; $static_html = $header . $content . $footer; // Write file $path = bdo_sc_static_path( $slug ); if ( file_put_contents( $path, $static_html ) !== false ) { error_log( '[BDO Static Cache] wrote ' . $path ); } else { error_log( '[BDO Static Cache] **FAILED** to write ' . $path ); } } add_action( 'save_post', 'bdo_sc_maybe_generate_static', 50 ); /** * Delete static file when a page is trashed or permanently deleted. */ function bdo_sc_cleanup_deleted( int $post_id ) { $post = get_post( $post_id ); if ( ! $post || ! in_array( $post->post_type, BDO_SC_CACHED_POST_TYPES, true ) ) { return; } $file = bdo_sc_static_path( $post->post_name ); if ( file_exists( $file ) ) { unlink( $file ); } } add_action( 'before_delete_post', 'bdo_sc_cleanup_deleted' ); //βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ // 3. Optional: admin "Regenerate" button on each bdo_page edit screen //βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ add_action( 'add_meta_boxes', function () { if ( ! current_user_can( 'manage_options' ) ) return; add_meta_box( 'bdo_sc_regen', 'Static Cache', 'bdo_sc_metabox', BDO_SC_CACHED_POST_TYPES, 'side', 'high' ); }); function bdo_sc_metabox( WP_Post $post ) { $file = bdo_sc_static_path( $post->post_name ); $cached = file_exists( $file ) ? date( 'Y-m-d H:i', filemtime( $file ) ) : 'β'; echo '<p>Cached file: <code>' . esc_html( basename( $file ) ) . '</code><br>Last built: ' . esc_html( $cached ) . '</p>'; wp_nonce_field( 'bdo_sc_regen_' . $post->ID ); echo '<p><input type="submit" class="button" name="bdo_sc_regen" value="Regenerate static HTML"></p>'; } add_action( 'save_post', function ( $post_id ) { if ( isset( $_POST['bdo_sc_regen'] ) && check_admin_referer( 'bdo_sc_regen_' . $post_id ) ) { bdo_sc_maybe_generate_static( $post_id ); } } ); // End of file.
Save changes
Create folder
writable 0777
Create
Cancel