diff --git a/functions.php b/functions.php index da37ecd..05b5daf 100644 --- a/functions.php +++ b/functions.php @@ -1,46 +1,60 @@ is_404 = false; +add_filter( + 'template_redirect', + function () { + global $wp_query; + + if ( file_exists( DOCS_FILE_PATH ) ) { + add_filter( + 'wp_title', + function ( $title ) { + $title = 'Tide Docs'; + $fileparts = explode( '/', get_current_url() ); + $page = end( $fileparts ); + $new_title = ucwords( str_replace( '-', ' ', $page ) ); + + if ( $new_title ) { + if ( 'Gcp' === $new_title ) { + $new_title = 'Google Cloud Platform'; + } elseif ( 'Aws' === $new_title ) { + $new_title = 'Amazon Web Services'; + } elseif ( 'Search' === $new_title ) { + $new_title = 'API Search'; + } elseif ( 'Api' === $new_title ) { + $new_title = 'API'; + } elseif ( 'Phpcs Server' === $new_title ) { + $new_title = 'PHPCS Server'; + } + + $title = "$title — $new_title"; + } + + return $title; + }, + 10 + ); + status_header( 200 ); + $wp_query->is_404 = false; + } } -} ); +); /** * Filter to override a 404. @@ -87,25 +108,30 @@ * The links are actually 404 errors, the pages don't exist really. We're faking status 200 * We just use the routes to pass in info on what file we want to include within index.php. */ -add_filter( 'body_class', function( $classes ) { - global $wp_query; +add_filter( + 'body_class', + function ( $classes ) { + global $wp_query; - if ( strpos( DOCS_FILE_PATH, '404.html' ) !== false && file_exists( DOCS_FILE_PATH ) ) { - $classes[] = 'error404'; - } + if ( strpos( DOCS_FILE_PATH, '404.html' ) !== false && file_exists( DOCS_FILE_PATH ) ) { + $classes[] = 'error404'; + } - if ( strpos( DOCS_FILE_PATH, 'search.html' ) !== false && file_exists( DOCS_FILE_PATH ) ) { - $classes[] = 'is-search'; - } + if ( strpos( DOCS_FILE_PATH, 'search.html' ) !== false && file_exists( DOCS_FILE_PATH ) ) { + $classes[] = 'is-search'; + } - return $classes; -} ); + return $classes; + } +); /** * Add GA tracking code to the HEAD. */ -add_action( 'wp_head', function() { - if ( strpos( get_home_url(), 'wptide.org' ) !== false ) { ?> +add_action( + 'wp_head', + function () { + if ( strpos( get_home_url(), 'wptide.org' ) !== false ) { ?> @@ -116,5 +142,8 @@ function gtag(){dataLayer.push(arguments);} gtag('config', 'UA-135952573-1'); - isDir(); - } ); + // Filter the iterator to include only directories. + $all_dirs = array_filter( + iterator_to_array( $dir_iterator ), + function ( $file ) { + return $file->isDir(); + } + ); - $relative_dirs = array_map( function ($file) { - return str_replace( DOCS_PATH . '/', '', $file ); - }, array_keys( $all_dirs ) ); + // Convert absolute paths to relative paths by removing the DOCS_PATH prefix. + $relative_dirs = array_map( + function ( $file ) { + return str_replace( DOCS_PATH . '/', '', $file ); + }, + array_keys( $all_dirs ) + ); return str_replace( '//', '/', $relative_dirs ); -} \ No newline at end of file +} diff --git a/functions/docpress-get-file-path.php b/functions/docpress-get-file-path.php index 57bf2a9..c10710a 100644 --- a/functions/docpress-get-file-path.php +++ b/functions/docpress-get-file-path.php @@ -1,12 +1,25 @@ isFile() && 'html' === pathinfo($file, PATHINFO_EXTENSION); - } ); - - $relative_paths = array_map( function ($file) { - $path = str_replace( DOCS_PATH . '/', '', $file ); - if ( $path ) { - return $path; + $all_files = array_filter( + iterator_to_array( $file_iterator ), + function ( $file ) { + return $file->isFile() && 'html' === pathinfo( $file, PATHINFO_EXTENSION ); } - }, array_keys( $all_files ) ); + ); + + $relative_paths = array_map( + function ( $file ) { + $path = str_replace( DOCS_PATH . '/', '', $file ); + if ( $path ) { + return $path; + } + }, + array_keys( $all_files ) + ); - $href_match = array_map( function ($file) { - return 'href="'. $file .'"'; - }, $relative_paths); + $href_match = array_map( + function ( $file ) { + return 'href="' . $file . '"'; + }, + $relative_paths + ); - $href_replace = array_map( function ($file) { - return 'href="'. site_url() . '/' . str_replace(array('/index.html', 'index.html', '.html', '/index'), '', $file) .'"'; - }, $relative_paths ); + $href_replace = array_map( + function ( $file ) { + return 'href="' . site_url() . '/' . str_replace( array( '/index.html', 'index.html', '.html', '/index' ), '', $file ) . '"'; + }, + $relative_paths + ); $parts = explode( '/', get_current_url() ); $files = array( @@ -29,23 +47,23 @@ function docpress_make_absolute_urls ($file_contents) { 'local-development', ); - // Links - $content = str_replace($href_match, $href_replace, $file_contents); // replace all relative href file matches with absolute urls and remove .html & index + // Links. + $content = str_replace( $href_match, $href_replace, $file_contents ); // replace all relative href file matches with absolute urls and remove .html & index if ( in_array( end( $parts ), $files, true ) ) { - $content = preg_replace('#href="(?!\/|https?:\/\/|mailto:|\#)(.*)"#im', 'href="'. site_url() .'/$1"', $content); // replace relative links inside root level files to other root level files + $content = preg_replace( '#href="(?!\/|https?:\/\/|mailto:|\#)(.*)"#im', 'href="' . site_url() . '/$1"', $content ); // replace relative links inside root level files to other root level files } - $content = preg_replace('#href="(?:\.\.\/)(.+)"#im', 'href="'. site_url() .'/$1"', $content); // replace ../ - $content = preg_replace('#href="(.+)(?:(\/.*)\.html)"#im', 'href="$1$2"', $content); // replace .html links - $content = preg_replace('#href="(.+)(\/index)"#im', 'href="$1"', $content); // replace /index links - $content = preg_replace('#href="(?!mailto:)([^\/\#]+)(\#.+)*"#im', 'href="'. get_current_url() .'/$1"', $content); // replace sibling links - $content = preg_replace('#href="' . site_url() . '(?:\/\/)([^"]*)"#im', 'href="' . site_url() . '/$1"', $content); // replace path//file links + $content = preg_replace( '#href="(?:\.\.\/)(.+)"#im', 'href="' . site_url() . '/$1"', $content ); // replace ../ + $content = preg_replace( '#href="(.+)(?:(\/.*)\.html)"#im', 'href="$1$2"', $content ); // replace .html links + $content = preg_replace( '#href="(.+)(\/index)"#im', 'href="$1"', $content ); // replace /index links + $content = preg_replace( '#href="(?!mailto:)([^\/\#]+)(\#.+)*"#im', 'href="' . get_current_url() . '/$1"', $content ); // replace sibling links + $content = preg_replace( '#href="' . site_url() . '(?:\/\/)([^"]*)"#im', 'href="' . site_url() . '/$1"', $content ); // replace path//file links - // Images - $content = preg_replace('#src="(?!https?:\/\/)(?!data:)(.+)"#im', 'src="'. DOCS_URI .'/$1"', $content); // fix relative images - $content = preg_replace('#src="(.+)(?:\.\.\/)(.+)"#im', 'src="$1$2"', $content); // replace ../ + // Images. + $content = preg_replace( '#src="(?!https?:\/\/)(?!data:)(.+)"#im', 'src="' . DOCS_URI . '/$1"', $content ); // fix relative images + $content = preg_replace( '#src="(.+)(?:\.\.\/)(.+)"#im', 'src="$1$2"', $content ); // replace ../ // @todo Find a better way to fix the invalid URL coming from `docpress`. - $content = preg_replace('/trunk\/wordpress.org\/public\//', 'trunk/wordpress.org/public_html/', $content ); + $content = preg_replace( '/trunk\/wordpress.org\/public\//', 'trunk/wordpress.org/public_html/', $content ); return $content; -} \ No newline at end of file +} diff --git a/functions/enqueue-scripts.php b/functions/enqueue-scripts.php index 4eaaf74..01a3599 100644 --- a/functions/enqueue-scripts.php +++ b/functions/enqueue-scripts.php @@ -1,5 +1,11 @@