[ 'name' => 'Porta Potty Rentals', 'template' => 'template-sub-porta-potty-rentals.html' ], ]; private static $template_cache = []; private static $instance = null; const MAX_URLS_PER_SITEMAP = 15000; public function __construct() { self::$instance = $this; add_action('init', [$this, 'load_cities'], 5); add_action('init', [$this, 'add_rewrite_rules'], 20); add_action('init', [$this, 'add_sitemap_rewrites']); add_filter('query_vars', [$this, 'add_query_vars']); add_action('template_redirect', [$this, 'handle_template_redirect']); } public static function get_instance() { return self::$instance; } public function load_cities() { $file_path = plugin_dir_path(__FILE__) . $this->cities_file; if (file_exists($file_path)) { $json = file_get_contents($file_path); $data = json_decode($json, true); if (is_array($data)) { foreach ($data as $city) { if (isset($city['slug'], $city['name'], $city['state'])) { $slug = strtolower($city['slug']); $this->cities[$slug] = [ 'name' => $city['name'], 'state' => strtoupper($city['state']), 'full' => $city['name'] . ', ' . strtoupper($city['state']) ]; } } } } } public function add_rewrite_rules() { // City home → dumpster-rentals-los-angeles/ add_rewrite_rule( '^dumpster-rentals-([^/]+)/?$', 'index.php?dumpster_city=$matches[1]', 'top' ); // Sub-service → porta-potty-rentals-los-angeles/ etc. $sub_slugs = array_keys($this->sub_services); $escaped = array_map('preg_quote', $sub_slugs); $pattern = '^(' . implode('|', $escaped) . ')-([^/]+)/?$'; add_rewrite_rule( $pattern, 'index.php?dumpster_sub_service=$matches[1]&dumpster_city=$matches[2]', 'top' ); } public function add_sitemap_rewrites() { add_rewrite_rule('^sitemap-dumpster-index\.xml$', 'index.php?dumpster_sitemap=index', 'top'); add_rewrite_rule('^sitemap-dumpster-([0-9]+)\.xml$', 'index.php?dumpster_sitemap=$matches[1]', 'top'); } public function add_query_vars($vars) { $vars[] = 'dumpster_city'; $vars[] = 'dumpster_sub_service'; $vars[] = 'dumpster_sitemap'; return $vars; } public function handle_template_redirect() { $sitemap = get_query_var('dumpster_sitemap'); if ($sitemap !== '') { if ($sitemap === 'index') { $this->render_sitemap_index(); } else { $page = intval($sitemap); if ($page > 0) $this->render_sitemap_page($page); } exit; } $city_slug = get_query_var('dumpster_city'); $sub_slug = get_query_var('dumpster_sub_service'); $city_slug = strtolower($city_slug); if ($city_slug && isset($this->cities[$city_slug])) { if ($sub_slug && array_key_exists($sub_slug, $this->sub_services)) { $this->render_sub_service_page($city_slug, $sub_slug); } elseif (!$sub_slug) { $this->render_city_home_page($city_slug); } else { $this->set_404(); } exit; } } private function set_404() { global $wp_query; $wp_query->set_404(); status_header(404); include get_404_template(); exit; } private function render_city_home_page($city_lower) { $city = $this->cities[$city_lower]; $city_full = $city['full']; $templates = ['template-city1.html', 'template-city2.html', 'template-city3.html', 'template-city4.html']; $chosen = $templates[array_rand($templates)]; $content = $this->load_template($chosen); $content = str_replace( ['William Paterson University Of New Jersey, NJ', 'William Paterson University Of New Jersey', 'NJ'], [esc_html($city_full), esc_html($city['name']), esc_html($city['state'])], $content ); $content = str_replace('Porta Potty Rentals', 'Dumpster Rentals', $content); $content .= $this->get_sub_services_list($city_lower); $content .= $this->get_random_cities_footer(); echo $content; } private function render_sub_service_page($city_lower, $sub_slug) { $city = $this->cities[$city_lower]; $city_full = $city['full']; $service = $this->sub_services[$sub_slug]; $content = $this->load_template($service['template']); $content = str_replace( ['William Paterson University Of New Jersey, NJ', 'William Paterson University Of New Jersey', 'NJ', '#ServiceName'], [esc_html($city_full), esc_html($city['name']), esc_html($city['state']), esc_html($service['name'])], $content ); $content .= $this->get_sub_services_list($city_lower); $content .= $this->get_random_cities_footer(); echo $content; } private function load_template($filename) { if (!isset(self::$template_cache[$filename])) { $file_path = plugin_dir_path(__FILE__) . $filename; if (!file_exists($file_path)) { wp_die("Template missing: $filename"); } self::$template_cache[$filename] = file_get_contents($file_path); } return self::$template_cache[$filename]; } private function get_sub_services_list($city_lower) { $list = '

Our Waste Management Services in ' . esc_html($this->cities[$city_lower]['full']) . '

'; foreach ($this->sub_services as $slug => $service) { $link = home_url("/{$slug}-{$city_lower}/"); $list .= '' . esc_html($service['name']) . ''; } $list .= '
'; return $list; } private function get_random_cities_footer() { $all_slugs = array_keys($this->cities); shuffle($all_slugs); $selected = array_slice($all_slugs, 0, 50); $footer = '

We Serve These Areas

'; foreach ($selected as $slug) { $name = $this->cities[$slug]['full']; $link = home_url("/dumpster-rentals-{$slug}/"); $footer .= 'Dumpster Rentals ' . esc_html($name) . ''; } $footer .= '
'; return $footer; } private function render_sitemap_index() { header('Content-Type: application/xml; charset=UTF-8'); echo ''; echo ''; $total_urls = count($this->cities) * (1 + count($this->sub_services)); $pages = ceil($total_urls / self::MAX_URLS_PER_SITEMAP); for ($i = 1; $i <= $pages; $i++) { echo '' . esc_url(home_url("/sitemap-dumpster-{$i}.xml")) . '' . current_time('c') . ''; } echo ''; exit; } private function render_sitemap_page($page) { header('Content-Type: application/xml; charset=UTF-8'); echo ''; echo ''; $cities = array_keys($this->cities); $sub_services = array_keys($this->sub_services); $start = ($page - 1) * self::MAX_URLS_PER_SITEMAP; $end = $start + self::MAX_URLS_PER_SITEMAP; $current = 0; // City pages first foreach ($cities as $city_slug) { if ($current >= $end) break; if ($current >= $start) { echo '' . esc_url(home_url("/dumpster-rentals-{$city_slug}/")) . '' . current_time('c') . 'weekly0.8'; } $current++; } // Sub-service pages foreach ($cities as $city_slug) { foreach ($sub_services as $sub_slug) { if ($current >= $end) break 2; if ($current >= $start) { echo '' . esc_url(home_url("/{$sub_slug}-{$city_slug}/")) . '' . current_time('c') . 'weekly0.6'; } $current++; } } echo ''; exit; } public function get_random_links($count = 300) { $count = min(intval($count), 1000); $slugs = array_keys($this->cities); $sub_slugs = array_keys($this->sub_services); if (empty($slugs)) return ''; $output = '
'; $used = []; for ($i = 0; $i < $count; $i++) { if (mt_rand(0, 3) === 0) { // ~25% main page $city_slug = $slugs[array_rand($slugs)]; $url = home_url("/dumpster-rentals-{$city_slug}/"); $text = "Dumpster Rentals " . $this->cities[$city_slug]['full']; } else { $city_slug = $slugs[array_rand($slugs)]; $sub_slug = $sub_slugs[array_rand($sub_slugs)]; $url = home_url("/{$sub_slug}-{$city_slug}/"); $text = $this->sub_services[$sub_slug]['name'] . " " . $this->cities[$city_slug]['full']; } if (in_array($url, $used)) { $i--; continue; } $used[] = $url; $output .= '' . esc_html($text) . ''; } $output .= '
'; return $output; } } // Shortcode add_shortcode('random_dumpster_links', function($atts) { $atts = shortcode_atts(['count' => 300], $atts); $instance = DumpsterRentalsDynamicPages::get_instance(); return $instance ? $instance->get_random_links($atts['count']) : ''; }); new DumpsterRentalsDynamicPages(); register_activation_hook(__FILE__, 'flush_rewrite_rules'); register_deactivation_hook(__FILE__, 'flush_rewrite_rules');

Our Waste Management Services in William Paterson University Of New Jersey, NJ

Dumpster Rental

We Serve These Areas

Porta Potty Rentals Folsom, PAPorta Potty Rentals Hopewell, VAPorta Potty Rentals St. Clair Shores, MIPorta Potty Rentals Maytown, PAPorta Potty Rentals Fort Washakie, WYPorta Potty Rentals Albany, ORPorta Potty Rentals New Hope, PAPorta Potty Rentals Zephyrhills North, FLPorta Potty Rentals Marks, MSPorta Potty Rentals Hasley Canyon, CAPorta Potty Rentals Barnum Island, NYPorta Potty Rentals Kearny, AZPorta Potty Rentals Short Pump, VAPorta Potty Rentals Edwards, COPorta Potty Rentals Tunica Resorts, MSPorta Potty Rentals St. George, SCPorta Potty Rentals Montgomery, TXPorta Potty Rentals Winsted, MNPorta Potty Rentals Mascotte, FLPorta Potty Rentals Gonzales, LAPorta Potty Rentals Van Buren, MOPorta Potty Rentals Mundys Corner, PAPorta Potty Rentals Trophy Club, TXPorta Potty Rentals Timberline-Fernwood, AZPorta Potty Rentals Walls, MSPorta Potty Rentals Mcleansboro, ILPorta Potty Rentals New Middletown, OHPorta Potty Rentals Zebulon, NCPorta Potty Rentals Belle Rose, LAPorta Potty Rentals Kingsford, MIPorta Potty Rentals Maple Heights-Lake Desire, WAPorta Potty Rentals Lovelock, NVPorta Potty Rentals Sebree, KYPorta Potty Rentals Walla Walla, WAPorta Potty Rentals El Rancho, NMPorta Potty Rentals Holcomb, KSPorta Potty Rentals Beulah, NDPorta Potty Rentals Mount Holly Springs, PAPorta Potty Rentals West Columbia, SCPorta Potty Rentals Speedway, INPorta Potty Rentals Lake Bluff, ILPorta Potty Rentals Bonham, TXPorta Potty Rentals West Easton, PAPorta Potty Rentals Flemington, PAPorta Potty Rentals Merion Station, PAPorta Potty Rentals Cumberland Center, MEPorta Potty Rentals Shelley, IDPorta Potty Rentals Archer Lodge, NCPorta Potty Rentals Germantown Hills, ILPorta Potty Rentals East Valley, NV