robotstxt-documentation-mar.../robotstxt-documentation-markdown-functions.php
2026-01-30 18:51:40 +00:00

135 lines
3.2 KiB
PHP

<?php
/**
* General Helper Functions
*
* @package RobotsTxt\DocumentationMarkdown
* @since 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Encrypt GitHub token for storage
*
* @since 1.0.0
*
* @param string $token Plain token.
* @return string Encrypted token.
*/
function robotstxt_docmd_encrypt_token( $token ) {
if ( empty( $token ) ) {
return '';
}
// Use WordPress salts for encryption key.
$key = wp_salt( 'auth' );
$iv_length = openssl_cipher_iv_length( 'aes-256-cbc' );
$iv = openssl_random_pseudo_bytes( $iv_length );
$encrypted = openssl_encrypt( $token, 'aes-256-cbc', $key, 0, $iv );
return base64_encode( $iv . $encrypted ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
}
/**
* Decrypt GitHub token from storage
*
* @since 1.0.0
*
* @param string $encrypted_token Encrypted token.
* @return string Plain token.
*/
function robotstxt_docmd_decrypt_token( $encrypted_token ) {
if ( empty( $encrypted_token ) ) {
return '';
}
$key = wp_salt( 'auth' );
$iv_length = openssl_cipher_iv_length( 'aes-256-cbc' );
$decoded = base64_decode( $encrypted_token ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
$iv = substr( $decoded, 0, $iv_length );
$encrypted = substr( $decoded, $iv_length );
return openssl_decrypt( $encrypted, 'aes-256-cbc', $key, 0, $iv );
}
/**
* Generate title from file path
*
* Converts "api-documentation.md" to "API Documentation"
*
* @since 1.0.0
*
* @param string $file_path File path.
* @return string Generated title.
*/
function robotstxt_docmd_generate_title_from_path( $file_path ) {
// Get filename without extension.
$filename = basename( $file_path );
$title = preg_replace( '/\.(md|markdown)$/i', '', $filename );
// Replace separators with spaces.
$title = str_replace( array( '-', '_' ), ' ', $title );
// Capitalize words.
$title = ucwords( $title );
// Handle common abbreviations.
$abbreviations = array(
'Api' => 'API',
'Faq' => 'FAQ',
'Url' => 'URL',
'Html' => 'HTML',
'Css' => 'CSS',
'Php' => 'PHP',
'Json' => 'JSON',
'Xml' => 'XML',
'Sql' => 'SQL',
);
foreach ( $abbreviations as $search => $replace ) {
$title = preg_replace( '/\b' . $search . '\b/', $replace, $title );
}
return $title;
}
/**
* Get status badge HTML
*
* @since 1.0.0
*
* @param string $status Status (never, pending, success, error).
* @return string Badge HTML.
*/
function robotstxt_docmd_get_status_badge( $status ) {
$badges = array(
'never' => array(
'class' => 'status-never',
'label' => __( 'Never Synced', 'robotstxt-documentation-markdown' ),
),
'pending' => array(
'class' => 'status-pending',
'label' => __( 'Syncing...', 'robotstxt-documentation-markdown' ),
),
'success' => array(
'class' => 'status-success',
'label' => __( 'Success', 'robotstxt-documentation-markdown' ),
),
'error' => array(
'class' => 'status-error',
'label' => __( 'Error', 'robotstxt-documentation-markdown' ),
),
);
$badge = $badges[ $status ] ?? $badges['never'];
return sprintf(
'<span class="status-badge %s">%s</span>',
esc_attr( $badge['class'] ),
esc_html( $badge['label'] )
);
}