Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 103 additions & 74 deletions functions.php
Original file line number Diff line number Diff line change
@@ -1,111 +1,137 @@
<?php
/**
* WP Tide functions.php file
*/

// Define paths
// Define paths.
define( 'DOCS_URI', get_stylesheet_directory_uri() . '/_docpress' );
define( 'DOCS_PATH', dirname(__FILE__) . '/_docpress' );
define( 'DOCS_PATH', __DIR__ . '/_docpress' );

// Require theme functions
require_once( 'functions/enqueue-styles.php' );
require_once( 'functions/enqueue-scripts.php' );
// Require theme functions.
require_once 'functions/enqueue-styles.php';
require_once 'functions/enqueue-scripts.php';

// Helpers
require_once( 'functions/get-current-url.php' );
// Helpers.
require_once 'functions/get-current-url.php';

// Docpress
require_once( 'functions/docpress-get-dirs.php' );
require_once( 'functions/docpress-get-file-path.php' );
require_once( 'functions/docpress-make-absolute-urls.php' );
// Docpress.
require_once 'functions/docpress-get-dirs.php';
require_once 'functions/docpress-get-file-path.php';
require_once 'functions/docpress-make-absolute-urls.php';

define( 'DOCS_FILE_PATH', docpress_get_file_path() );

// Enables themes to manage the document title tag
add_action( 'after_setup_theme', function() {
add_theme_support( 'title-tag' );
} );
// Enables themes to manage the document title tag.
add_action(
'after_setup_theme',
function () {
add_theme_support( 'title-tag' );
}
);

// Remove unused scripts & styles.
add_action('init', function() {
global $pagenow;

if ( 'wp-login.php' !== $pagenow && ! is_admin() ) {
wp_deregister_script( 'wp-embed' );
wp_deregister_script( 'jquery' );
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
add_action(
'init',
function () {
global $pagenow;

if ( 'wp-login.php' !== $pagenow && ! is_admin() ) {
wp_deregister_script( 'wp-embed' );
wp_deregister_script( 'jquery' );
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
}
}
} );
);

// Block 301 redirect.
add_filter( 'redirect_canonical', function( $redirect_url, $requested_url ) {
if ( strpos( $requested_url, 'api/tide/v1' ) === false ) {
return '';
}
}, 10, 2 );
add_filter(
'redirect_canonical',
function ( $redirect_url, $requested_url ) {
if ( strpos( $requested_url, 'api/tide/v1' ) === false ) {
return '';
}
},
10,
2
);

/**
* Filter to override a 404.
*
* 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( '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';
} else if ( 'Aws' === $new_title ) {
$new_title = 'Amazon Web Services';
} else if ( 'Search' === $new_title ) {
$new_title = 'API Search';
} else if ( 'Api' === $new_title ) {
$new_title = 'API';
} else if ( 'Phpcs Server' === $new_title ) {
$new_title = 'PHPCS Server';
}

$title = "$title — $new_title";
}

return $title;
}, 10 );
status_header( 200 );
$wp_query->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.
*
* 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 ) { ?>

<!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-135952573-1"></script>
Expand All @@ -116,5 +142,8 @@ function gtag(){dataLayer.push(arguments);}
gtag('config', 'UA-135952573-1');
</script>

<?php }
}, 0 );
<?php
}
},
0
);
34 changes: 25 additions & 9 deletions functions/docpress-get-dirs.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
<?php

function docpress_get_dirs () {
$dir_iterator = new RecursiveDirectoryIterator(DOCS_PATH);
/**
* Retrieve a list of relative directory paths from the DOCS_PATH directory.
*
* @return array List of relative directory paths.
*/
function docpress_get_dirs() {
// Create a directory iterator for the DOCS_PATH directory.
$dir_iterator = new RecursiveDirectoryIterator( DOCS_PATH );

// Get the current URL's relative path (not used in this function currently).
$relative_url = str_replace( site_url( '/' ), '', get_current_url() );

$all_dirs = array_filter( iterator_to_array($dir_iterator), function ( $file ) {
return $file->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 );
}
}
25 changes: 19 additions & 6 deletions functions/docpress-get-file-path.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
<?php

function docpress_get_file_path () {
/**
* Get the full file path of the requested documentation page.
*
* @return string Full path to the documentation HTML file.
*/
function docpress_get_file_path() {
// Get the current URL path relative to the site root.
$relative_url = str_replace( site_url( '/' ), '', get_current_url() );
$filename = ( '' === $relative_url) ? 'index'
: in_array($relative_url, docpress_get_dirs()) ? $relative_url .'/index'
: $relative_url;

$file = DOCS_PATH .'/'. $filename .'.html';
// Determine the filename based on the relative URL:
// - If the URL is empty, use 'index'
// - If it matches a directory, append '/index'
// - Otherwise, use the URL as-is
$filename = ( '' === $relative_url )
? 'index'
: ( in_array( $relative_url, docpress_get_dirs(), true )
? $relative_url . '/index'
: $relative_url );

$file = DOCS_PATH . '/' . $filename . '.html';

return str_replace( '//', '/', $file );
}
}
78 changes: 48 additions & 30 deletions functions/docpress-make-absolute-urls.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
<?php

function docpress_make_absolute_urls ($file_contents) {
$file_iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator(DOCS_PATH . '/') );
/**
* Convert relative URLs in the given HTML content to absolute URLs.
*
* @param string $file_contents The HTML content with potential relative URLs.
* @return string The modified HTML content with absolute URLs.
*/
function docpress_make_absolute_urls( $file_contents ) {
$file_iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( DOCS_PATH . '/' ) );

$all_files = array_filter( iterator_to_array($file_iterator), function ($file) {
return $file->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(
Expand All @@ -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;
}
}
Loading