Skip to content

Commit

Permalink
Add support for dynamic changelogs
Browse files Browse the repository at this point in the history
See epiphyt/impressum-plus/#131
  • Loading branch information
MatzeKitt committed Apr 18, 2024
1 parent 394f3ea commit fe44f37
Showing 1 changed file with 70 additions and 1 deletion.
71 changes: 70 additions & 1 deletion includes/Wpup/Epiphyt_Server.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ protected function checkAuthorization( $request ) {
*/
protected function filterMetadata( $meta, $request ) {
$meta = parent::filterMetadata( $meta, $request );
$locale = $request->query['locale'] ?? 'en_US';
$meta = $this->get_changelog( $meta, $locale );

$email = $request->param( 'license_email' );
$home_url = $request->param( 'platform' );
Expand Down Expand Up @@ -140,14 +142,81 @@ private function get_api_data( $data = [] ) {
}

foreach ( $responses as $response ) {
if ( is_object( $response ) && is_array( $response->activations ) ) {
if ( is_object( $response ) && isset( $response->activations ) && is_array( $response->activations ) ) {
return $response;
}
}

return reset( $responses );
}

/**
* Get
*
* @param array $meta
* @param string $locale
* @return array
*/
private function get_changelog( array $meta, string $locale ): array {
$urls = [
'impressum' => [
'default' => 'https://impressum.plus/en/documentation/',
'german' => 'https://impressum.plus/dokumentation/',
],
'form-block-pro' => [
'default' => 'https://formblock.pro/en/documentation/',
'german' => 'https://formblock.pro/dokumentation/',
],
];

if ( empty( $urls[ $meta['slug'] ] ) ) {
$meta['sections'] = [
'changelog' => '',
];

return $meta;
}

switch ( $locale ) {
case 'de_AT':
case 'de_DE':
case 'de_DE_formal':
case 'de_CH':
case 'de_CH_formal':
$url = $urls[ $meta['slug'] ]['german'];
break;
default:
$url = $urls[ $meta['slug'] ]['default'];
break;
}

$html = $this->get_single_api_data( $url );
$use_internal_errors = \libxml_use_internal_errors( true );
$changelog = '';
$dom = new DOMDocument();
$dom->loadHTML( $html, \LIBXML_HTML_NOIMPLIED | \LIBXML_HTML_NODEFDTD );
$group = $dom->getElementById( 'changelog-group' );

if ( $group ) {
/** @var \DOMNode $childNode */
foreach ( $group->firstElementChild->childNodes as $childNode ) {
if ( \method_exists( $childNode, 'removeAttribute' ) ) {
$childNode->removeAttribute( 'id' );
}

$changelog .= $dom->saveHTML( $childNode );
}
}

\libxml_use_internal_errors( $use_internal_errors );

$meta['sections'] = [
'changelog' => \trim( $changelog ),
];

return $meta;
}

/**
* Use cURL to get data from any URL.
*
Expand Down

0 comments on commit fe44f37

Please sign in to comment.