Skip to content

Instantly share code, notes, and snippets.

@trepmal
Last active June 3, 2025 21:44
Show Gist options
  • Save trepmal/7fa4df42bf6ddaa8901bd6d859f9588f to your computer and use it in GitHub Desktop.
Save trepmal/7fa4df42bf6ddaa8901bd6d859f9588f to your computer and use it in GitHub Desktop.
<?php
if ( isset( $argv[1] ) ) {
$query = $argv[1];
} else {
$allowed_tokens = [
];
if ( ! in_array( $_POST['token'], $allowed_tokens ) ) {
die;
}
$query = trim( $_POST['text'] );
}
function vipdocs_fetch( $search_query ) {
$url = 'https://docs.wpvip.com/wp-json/wp/v2/search?search=' . rawurlencode( $search_query );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 2 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$buffer = curl_exec( $ch );
curl_close( $ch );
$json = json_decode( $buffer );
if ( is_null( $json ) ) {
no_results( true );
}
return $json;
}
function vipdocs_format_results( $json ) {
if ( empty( $json ) ) {
no_results();
}
foreach ( $json as $match ) {
$output[] = sprintf( '<%s|%s> %s', $match->url, $match->title, $match->subtype );
}
return implode( PHP_EOL, $output );
}
function no_results( $error = false ) {
$response = [
"response_type" => "in_channel",
"blocks" => [
[
"type" => "section",
"text" => [
"type" => "mrkdwn",
"text" => $error ? "Error" : "No results"
]
],
[
"type" => "context",
"elements" => [
[
"type" => "mrkdwn",
"text" => sprintf( '<%s|Source code>', 'https://gist.github.com/trepmal/7fa4df42bf6ddaa8901bd6d859f9588f' )
],
[
"type" => "plain_text",
"text" => ':heart: @trepmal'
]
]
],
]
];
header( 'Content-Type: application/json' );
die( json_encode( $response ) );
}
$response = [
"response_type" => "in_channel",
"blocks" => [
[
"type" => "section",
"text" => [
"type" => "mrkdwn",
"text" => vipdocs_format_results( vipdocs_fetch( $query ) )
]
],
[
"type" => "context",
"elements" => [
[
"type" => "mrkdwn",
"text" => sprintf( 'https://docs.wpvip.com?s=%s', rawurlencode( $query ) )
],
[
"type" => "mrkdwn",
"text" => sprintf( '<%s|Source code>', 'https://gist.github.com/trepmal/7fa4df42bf6ddaa8901bd6d859f9588f' )
],
[
"type" => "plain_text",
"text" => ':heart: @trepmal'
]
]
],
]
];
header( 'Content-Type: application/json' );
die( json_encode( $response ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment