Compare commits
5
Commits
04201a66f8
...
v0.4.5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eb85870909 | ||
|
|
6988e49287 | ||
|
|
166a5e6f7c | ||
|
|
fedab21c2a | ||
|
|
eaefcff9c9 |
No files matched your search
+58
-1
@@ -7,6 +7,58 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [0.4.5] - 2026-02-02
|
||||
|
||||
### Fixed
|
||||
|
||||
- **Multi-layer recursion protection** - Added additional safeguards against infinite Twig rendering
|
||||
- Added render depth tracking in `Plugin::render()` with max depth of 5
|
||||
- Strip shortcodes from content when in shortcode context (prevents any later `do_shortcode()` calls from triggering recursion)
|
||||
- This addresses the Twig StagingExtension.php recursion error
|
||||
|
||||
## [0.4.4] - 2026-02-02
|
||||
|
||||
### Fixed
|
||||
|
||||
- **Fix excerpt-triggered recursion** - `get_the_excerpt()` internally calls `the_content` filter when generating auto-excerpts
|
||||
- When in shortcode context, now uses raw `$post->post_excerpt` or generates simple excerpt with `wp_trim_words()` instead
|
||||
- This was the remaining recursion path causing memory exhaustion in `class-wp-hook.php`
|
||||
|
||||
## [0.4.3] - 2026-02-02
|
||||
|
||||
### Fixed
|
||||
|
||||
- **Further memory leak fix** - v0.4.2 fix was still incomplete
|
||||
- Changed `$in_shortcode_context` boolean to `$shortcode_context_depth` counter to properly handle nested shortcodes
|
||||
- Added shortcode context protection to `template-wrapper.php` for single page views
|
||||
- This fixes the remaining recursion path where `the_content` filter was still being applied when viewing single FediStream posts (artists, albums, tracks, playlists)
|
||||
|
||||
## [0.4.2] - 2026-02-02
|
||||
|
||||
### Fixed
|
||||
|
||||
- **Complete fix for memory leak** - v0.4.1 fix was incomplete
|
||||
- Added `$in_shortcode_context` flag to TemplateLoader to track when we're rendering shortcodes
|
||||
- All shortcode render methods now call `enter_shortcode_context()` before loading data
|
||||
- When in shortcode context, `the_content` filter is always skipped to prevent recursive shortcode processing
|
||||
- This prevents infinite recursion when post content contains FediStream shortcodes
|
||||
|
||||
## [0.4.1] - 2026-02-02
|
||||
|
||||
### Fixed
|
||||
|
||||
- **Critical memory leak** causing "Allowed memory size exhausted" errors in Twig's StagingExtension
|
||||
- Root cause: `apply_filters('the_content')` in `get_post_data()` triggered shortcode processing, causing infinite recursion when post content contained FediStream shortcodes
|
||||
- Added recursion depth tracking with `MAX_RECURSION_DEPTH = 3` to prevent runaway nesting
|
||||
- Nested items now skip `the_content` filter, using `wp_kses_post()` instead
|
||||
- Nested data loading (albums within artists, tracks within albums) is now properly bounded
|
||||
|
||||
### Changed
|
||||
|
||||
- Made `get_artist_data()`, `get_album_data()`, `get_track_data()`, and `get_playlist_data()` public methods in TemplateLoader (previously private but called externally)
|
||||
- These methods now accept both `int` post IDs and `WP_Post` objects for flexibility
|
||||
- Added `$load_nested` parameter to control whether nested items are fully loaded or just counted
|
||||
|
||||
## [0.4.0] - 2026-01-29
|
||||
|
||||
### Added
|
||||
@@ -191,7 +243,12 @@ Initial release of WP FediStream - a WordPress plugin for streaming music over A
|
||||
|
||||
---
|
||||
|
||||
[Unreleased]: https://src.bundespruefstelle.ch/magdev/wp-fedistream/compare/v0.4.0...HEAD
|
||||
[Unreleased]: https://src.bundespruefstelle.ch/magdev/wp-fedistream/compare/v0.4.5...HEAD
|
||||
[0.4.5]: https://src.bundespruefstelle.ch/magdev/wp-fedistream/compare/v0.4.4...v0.4.5
|
||||
[0.4.4]: https://src.bundespruefstelle.ch/magdev/wp-fedistream/compare/v0.4.3...v0.4.4
|
||||
[0.4.3]: https://src.bundespruefstelle.ch/magdev/wp-fedistream/compare/v0.4.2...v0.4.3
|
||||
[0.4.2]: https://src.bundespruefstelle.ch/magdev/wp-fedistream/compare/v0.4.1...v0.4.2
|
||||
[0.4.1]: https://src.bundespruefstelle.ch/magdev/wp-fedistream/compare/v0.4.0...v0.4.1
|
||||
[0.4.0]: https://src.bundespruefstelle.ch/magdev/wp-fedistream/compare/v0.3.0...v0.4.0
|
||||
[0.3.0]: https://src.bundespruefstelle.ch/magdev/wp-fedistream/compare/v0.2.0...v0.3.0
|
||||
[0.2.0]: https://src.bundespruefstelle.ch/magdev/wp-fedistream/compare/v0.1.1...v0.2.0
|
||||
|
||||
@@ -81,6 +81,9 @@ class Shortcodes {
|
||||
* @return string
|
||||
*/
|
||||
public function render_artist( array $atts ): string {
|
||||
// Enter shortcode context to prevent recursive shortcode processing during data loading.
|
||||
TemplateLoader::enter_shortcode_context();
|
||||
|
||||
$atts = shortcode_atts(
|
||||
array(
|
||||
'id' => 0,
|
||||
@@ -95,6 +98,7 @@ class Shortcodes {
|
||||
|
||||
$post = $this->get_post( $atts, 'fedistream_artist' );
|
||||
if ( ! $post ) {
|
||||
TemplateLoader::exit_shortcode_context();
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -119,6 +123,9 @@ class Shortcodes {
|
||||
* @return string
|
||||
*/
|
||||
public function render_album( array $atts ): string {
|
||||
// Enter shortcode context to prevent recursive shortcode processing during data loading.
|
||||
TemplateLoader::enter_shortcode_context();
|
||||
|
||||
$atts = shortcode_atts(
|
||||
array(
|
||||
'id' => 0,
|
||||
@@ -132,6 +139,7 @@ class Shortcodes {
|
||||
|
||||
$post = $this->get_post( $atts, 'fedistream_album' );
|
||||
if ( ! $post ) {
|
||||
TemplateLoader::exit_shortcode_context();
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -155,6 +163,9 @@ class Shortcodes {
|
||||
* @return string
|
||||
*/
|
||||
public function render_track( array $atts ): string {
|
||||
// Enter shortcode context to prevent recursive shortcode processing during data loading.
|
||||
TemplateLoader::enter_shortcode_context();
|
||||
|
||||
$atts = shortcode_atts(
|
||||
array(
|
||||
'id' => 0,
|
||||
@@ -168,6 +179,7 @@ class Shortcodes {
|
||||
|
||||
$post = $this->get_post( $atts, 'fedistream_track' );
|
||||
if ( ! $post ) {
|
||||
TemplateLoader::exit_shortcode_context();
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -191,6 +203,9 @@ class Shortcodes {
|
||||
* @return string
|
||||
*/
|
||||
public function render_playlist( array $atts ): string {
|
||||
// Enter shortcode context to prevent recursive shortcode processing during data loading.
|
||||
TemplateLoader::enter_shortcode_context();
|
||||
|
||||
$atts = shortcode_atts(
|
||||
array(
|
||||
'id' => 0,
|
||||
@@ -204,6 +219,7 @@ class Shortcodes {
|
||||
|
||||
$post = $this->get_post( $atts, 'fedistream_playlist' );
|
||||
if ( ! $post ) {
|
||||
TemplateLoader::exit_shortcode_context();
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -227,6 +243,9 @@ class Shortcodes {
|
||||
* @return string
|
||||
*/
|
||||
public function render_latest_releases( array $atts ): string {
|
||||
// Enter shortcode context to prevent recursive shortcode processing during data loading.
|
||||
TemplateLoader::enter_shortcode_context();
|
||||
|
||||
$atts = shortcode_atts(
|
||||
array(
|
||||
'count' => 6,
|
||||
@@ -292,6 +311,9 @@ class Shortcodes {
|
||||
* @return string
|
||||
*/
|
||||
public function render_popular_tracks( array $atts ): string {
|
||||
// Enter shortcode context to prevent recursive shortcode processing during data loading.
|
||||
TemplateLoader::enter_shortcode_context();
|
||||
|
||||
$atts = shortcode_atts(
|
||||
array(
|
||||
'count' => 10,
|
||||
@@ -359,6 +381,9 @@ class Shortcodes {
|
||||
* @return string
|
||||
*/
|
||||
public function render_artists_grid( array $atts ): string {
|
||||
// Enter shortcode context to prevent recursive shortcode processing during data loading.
|
||||
TemplateLoader::enter_shortcode_context();
|
||||
|
||||
$atts = shortcode_atts(
|
||||
array(
|
||||
'count' => 12,
|
||||
@@ -426,6 +451,9 @@ class Shortcodes {
|
||||
* @return string
|
||||
*/
|
||||
public function render_player( array $atts ): string {
|
||||
// Enter shortcode context to prevent recursive shortcode processing during data loading.
|
||||
TemplateLoader::enter_shortcode_context();
|
||||
|
||||
$atts = shortcode_atts(
|
||||
array(
|
||||
'track' => 0,
|
||||
@@ -471,6 +499,7 @@ class Shortcodes {
|
||||
}
|
||||
|
||||
if ( empty( $tracks ) ) {
|
||||
TemplateLoader::exit_shortcode_context();
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -528,13 +557,20 @@ class Shortcodes {
|
||||
return $this->get_unlicensed_message();
|
||||
}
|
||||
|
||||
// Enter shortcode context to prevent recursive shortcode processing.
|
||||
TemplateLoader::enter_shortcode_context();
|
||||
|
||||
try {
|
||||
return $this->plugin->render( $template, $context );
|
||||
$result = $this->plugin->render( $template, $context );
|
||||
} catch ( \Exception $e ) {
|
||||
TemplateLoader::exit_shortcode_context();
|
||||
if ( WP_DEBUG ) {
|
||||
return '<p class="fedistream-error">' . esc_html( $e->getMessage() ) . '</p>';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
TemplateLoader::exit_shortcode_context();
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,60 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
*/
|
||||
class TemplateLoader {
|
||||
|
||||
/**
|
||||
* Recursion depth for get_post_data calls.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private static int $recursion_depth = 0;
|
||||
|
||||
/**
|
||||
* Maximum allowed recursion depth.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private const MAX_RECURSION_DEPTH = 3;
|
||||
|
||||
/**
|
||||
* Shortcode rendering context depth counter.
|
||||
* When > 0, the_content filter is skipped to prevent recursive shortcode processing.
|
||||
* Using a counter instead of boolean to handle nested shortcodes properly.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private static int $shortcode_context_depth = 0;
|
||||
|
||||
/**
|
||||
* Enter shortcode rendering context.
|
||||
* Call this before rendering shortcode content to prevent recursive shortcode processing.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function enter_shortcode_context(): void {
|
||||
++self::$shortcode_context_depth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Exit shortcode rendering context.
|
||||
* Call this after shortcode rendering is complete.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function exit_shortcode_context(): void {
|
||||
if ( self::$shortcode_context_depth > 0 ) {
|
||||
--self::$shortcode_context_depth;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we're in a shortcode rendering context.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_in_shortcode_context(): bool {
|
||||
return self::$shortcode_context_depth > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
@@ -191,34 +245,66 @@ class TemplateLoader {
|
||||
/**
|
||||
* Get post data for template.
|
||||
*
|
||||
* @param \WP_Post $post Post object.
|
||||
* @param \WP_Post $post Post object.
|
||||
* @param bool $skip_nested Whether to skip loading nested items (albums, tracks, etc.).
|
||||
* @return array Post data.
|
||||
*/
|
||||
public static function get_post_data( \WP_Post $post ): array {
|
||||
public static function get_post_data( \WP_Post $post, bool $skip_nested = false ): array {
|
||||
// Track recursion to prevent infinite loops from shortcodes in content.
|
||||
++self::$recursion_depth;
|
||||
|
||||
// Skip the_content filter if:
|
||||
// 1. We're in a shortcode context (prevents recursive shortcode processing)
|
||||
// 2. We're at depth > 1 (nested data loading)
|
||||
$skip_content_filter = self::$shortcode_context_depth > 0 || self::$recursion_depth > 1;
|
||||
|
||||
// When skipping content filter, also use raw excerpt to avoid get_the_excerpt()
|
||||
// triggering the_content filter internally when generating auto-excerpts.
|
||||
if ( $skip_content_filter ) {
|
||||
$excerpt = $post->post_excerpt;
|
||||
if ( empty( $excerpt ) ) {
|
||||
// Generate a simple excerpt without triggering the_content filter.
|
||||
$excerpt = wp_trim_words( wp_strip_all_tags( $post->post_content ), 55, '…' );
|
||||
}
|
||||
} else {
|
||||
$excerpt = get_the_excerpt( $post );
|
||||
}
|
||||
|
||||
// When skipping content filter, also strip shortcodes to prevent them from
|
||||
// being processed by anything else that might call do_shortcode on the output.
|
||||
if ( $skip_content_filter ) {
|
||||
$content = strip_shortcodes( $post->post_content );
|
||||
$content = wp_kses_post( $content );
|
||||
} else {
|
||||
$content = apply_filters( 'the_content', $post->post_content );
|
||||
}
|
||||
|
||||
$data = array(
|
||||
'id' => $post->ID,
|
||||
'title' => get_the_title( $post ),
|
||||
'content' => apply_filters( 'the_content', $post->post_content ),
|
||||
'excerpt' => get_the_excerpt( $post ),
|
||||
'content' => $content,
|
||||
'excerpt' => $excerpt,
|
||||
'permalink' => get_permalink( $post ),
|
||||
'thumbnail' => get_the_post_thumbnail_url( $post->ID, 'large' ),
|
||||
'date' => get_the_date( '', $post ),
|
||||
'author' => get_the_author_meta( 'display_name', $post->post_author ),
|
||||
);
|
||||
|
||||
// Add post type specific data.
|
||||
// Add post type specific data (skip nested items if at max depth).
|
||||
$load_nested = ! $skip_nested && self::$recursion_depth < self::MAX_RECURSION_DEPTH;
|
||||
|
||||
switch ( $post->post_type ) {
|
||||
case 'fedistream_artist':
|
||||
$data = array_merge( $data, self::get_artist_data( $post->ID ) );
|
||||
$data = array_merge( $data, self::get_artist_data( $post->ID, $load_nested ) );
|
||||
break;
|
||||
case 'fedistream_album':
|
||||
$data = array_merge( $data, self::get_album_data( $post->ID ) );
|
||||
$data = array_merge( $data, self::get_album_data( $post->ID, $load_nested ) );
|
||||
break;
|
||||
case 'fedistream_track':
|
||||
$data = array_merge( $data, self::get_track_data( $post->ID ) );
|
||||
break;
|
||||
case 'fedistream_playlist':
|
||||
$data = array_merge( $data, self::get_playlist_data( $post->ID ) );
|
||||
$data = array_merge( $data, self::get_playlist_data( $post->ID, $load_nested ) );
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -226,16 +312,23 @@ class TemplateLoader {
|
||||
$data['genres'] = self::get_terms( $post->ID, 'fedistream_genre' );
|
||||
$data['moods'] = self::get_terms( $post->ID, 'fedistream_mood' );
|
||||
|
||||
--self::$recursion_depth;
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get artist-specific data.
|
||||
*
|
||||
* @param int $post_id Post ID.
|
||||
* @param int|\WP_Post $post_id Post ID or WP_Post object.
|
||||
* @param bool $load_nested Whether to load nested albums.
|
||||
* @return array Artist data.
|
||||
*/
|
||||
private static function get_artist_data( int $post_id ): array {
|
||||
public static function get_artist_data( int|\WP_Post $post_id, bool $load_nested = true ): array {
|
||||
// Support both post ID and WP_Post object.
|
||||
if ( $post_id instanceof \WP_Post ) {
|
||||
$post_id = $post_id->ID;
|
||||
}
|
||||
$type = get_post_meta( $post_id, '_fedistream_artist_type', true ) ?: 'solo';
|
||||
$types = array(
|
||||
'solo' => __( 'Solo Artist', 'wp-fedistream' ),
|
||||
@@ -244,23 +337,48 @@ class TemplateLoader {
|
||||
'collective' => __( 'Collective', 'wp-fedistream' ),
|
||||
);
|
||||
|
||||
$albums = get_posts(
|
||||
array(
|
||||
'post_type' => 'fedistream_album',
|
||||
'posts_per_page' => -1,
|
||||
'post_status' => 'publish',
|
||||
'meta_key' => '_fedistream_album_artist',
|
||||
'meta_value' => $post_id,
|
||||
'orderby' => 'meta_value',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => '_fedistream_album_release_date',
|
||||
'compare' => 'EXISTS',
|
||||
$albums = array();
|
||||
$album_count = 0;
|
||||
|
||||
if ( $load_nested ) {
|
||||
$album_posts = get_posts(
|
||||
array(
|
||||
'post_type' => 'fedistream_album',
|
||||
'posts_per_page' => -1,
|
||||
'post_status' => 'publish',
|
||||
'meta_key' => '_fedistream_album_artist',
|
||||
'meta_value' => $post_id,
|
||||
'orderby' => 'meta_value',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => '_fedistream_album_release_date',
|
||||
'compare' => 'EXISTS',
|
||||
),
|
||||
),
|
||||
),
|
||||
'order' => 'DESC',
|
||||
)
|
||||
);
|
||||
'order' => 'DESC',
|
||||
)
|
||||
);
|
||||
$album_count = count( $album_posts );
|
||||
$albums = array_map(
|
||||
function ( $album ) {
|
||||
return self::get_post_data( $album, true ); // Skip further nesting.
|
||||
},
|
||||
$album_posts
|
||||
);
|
||||
} else {
|
||||
// Just get the count without loading full data.
|
||||
$album_count = (int) get_posts(
|
||||
array(
|
||||
'post_type' => 'fedistream_album',
|
||||
'posts_per_page' => -1,
|
||||
'post_status' => 'publish',
|
||||
'meta_key' => '_fedistream_album_artist',
|
||||
'meta_value' => $post_id,
|
||||
'fields' => 'ids',
|
||||
)
|
||||
);
|
||||
$album_count = is_array( $album_count ) ? count( $album_count ) : 0;
|
||||
}
|
||||
|
||||
return array(
|
||||
'artist_type' => $type,
|
||||
@@ -270,18 +388,23 @@ class TemplateLoader {
|
||||
'website' => get_post_meta( $post_id, '_fedistream_artist_website', true ),
|
||||
'social_links' => get_post_meta( $post_id, '_fedistream_artist_social_links', true ) ?: array(),
|
||||
'members' => get_post_meta( $post_id, '_fedistream_artist_members', true ) ?: array(),
|
||||
'albums' => array_map( array( __CLASS__, 'get_post_data' ), $albums ),
|
||||
'album_count' => count( $albums ),
|
||||
'albums' => $albums,
|
||||
'album_count' => $album_count,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get album-specific data.
|
||||
*
|
||||
* @param int $post_id Post ID.
|
||||
* @param int|\WP_Post $post_id Post ID or WP_Post object.
|
||||
* @param bool $load_nested Whether to load nested tracks.
|
||||
* @return array Album data.
|
||||
*/
|
||||
private static function get_album_data( int $post_id ): array {
|
||||
public static function get_album_data( int|\WP_Post $post_id, bool $load_nested = true ): array {
|
||||
// Support both post ID and WP_Post object.
|
||||
if ( $post_id instanceof \WP_Post ) {
|
||||
$post_id = $post_id->ID;
|
||||
}
|
||||
$type = get_post_meta( $post_id, '_fedistream_album_type', true ) ?: 'album';
|
||||
$types = array(
|
||||
'album' => __( 'Album', 'wp-fedistream' ),
|
||||
@@ -293,24 +416,49 @@ class TemplateLoader {
|
||||
);
|
||||
$artist_id = get_post_meta( $post_id, '_fedistream_album_artist', true );
|
||||
|
||||
$tracks = get_posts(
|
||||
array(
|
||||
'post_type' => 'fedistream_track',
|
||||
'posts_per_page' => -1,
|
||||
'post_status' => 'publish',
|
||||
'meta_key' => '_fedistream_track_album',
|
||||
'meta_value' => $post_id,
|
||||
'orderby' => 'meta_value_num',
|
||||
'meta_query' => array(
|
||||
'relation' => 'AND',
|
||||
array(
|
||||
'key' => '_fedistream_track_number',
|
||||
'compare' => 'EXISTS',
|
||||
$tracks = array();
|
||||
$total_tracks = 0;
|
||||
|
||||
if ( $load_nested ) {
|
||||
$track_posts = get_posts(
|
||||
array(
|
||||
'post_type' => 'fedistream_track',
|
||||
'posts_per_page' => -1,
|
||||
'post_status' => 'publish',
|
||||
'meta_key' => '_fedistream_track_album',
|
||||
'meta_value' => $post_id,
|
||||
'orderby' => 'meta_value_num',
|
||||
'meta_query' => array(
|
||||
'relation' => 'AND',
|
||||
array(
|
||||
'key' => '_fedistream_track_number',
|
||||
'compare' => 'EXISTS',
|
||||
),
|
||||
),
|
||||
),
|
||||
'order' => 'ASC',
|
||||
)
|
||||
);
|
||||
'order' => 'ASC',
|
||||
)
|
||||
);
|
||||
$total_tracks = count( $track_posts );
|
||||
$tracks = array_map(
|
||||
function ( $track ) {
|
||||
return self::get_post_data( $track, true ); // Skip further nesting.
|
||||
},
|
||||
$track_posts
|
||||
);
|
||||
} else {
|
||||
// Just get the count without loading full data.
|
||||
$track_ids = get_posts(
|
||||
array(
|
||||
'post_type' => 'fedistream_track',
|
||||
'posts_per_page' => -1,
|
||||
'post_status' => 'publish',
|
||||
'meta_key' => '_fedistream_track_album',
|
||||
'meta_value' => $post_id,
|
||||
'fields' => 'ids',
|
||||
)
|
||||
);
|
||||
$total_tracks = is_array( $track_ids ) ? count( $track_ids ) : 0;
|
||||
}
|
||||
|
||||
return array(
|
||||
'album_type' => $type,
|
||||
@@ -322,19 +470,23 @@ class TemplateLoader {
|
||||
'artist_url' => $artist_id ? get_permalink( $artist_id ) : '',
|
||||
'upc' => get_post_meta( $post_id, '_fedistream_album_upc', true ),
|
||||
'catalog_number' => get_post_meta( $post_id, '_fedistream_album_catalog_number', true ),
|
||||
'total_tracks' => count( $tracks ),
|
||||
'total_tracks' => $total_tracks,
|
||||
'total_duration' => (int) get_post_meta( $post_id, '_fedistream_album_total_duration', true ),
|
||||
'tracks' => array_map( array( __CLASS__, 'get_post_data' ), $tracks ),
|
||||
'tracks' => $tracks,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get track-specific data.
|
||||
*
|
||||
* @param int $post_id Post ID.
|
||||
* @param int|\WP_Post $post_id Post ID or WP_Post object.
|
||||
* @return array Track data.
|
||||
*/
|
||||
private static function get_track_data( int $post_id ): array {
|
||||
public static function get_track_data( int|\WP_Post $post_id ): array {
|
||||
// Support both post ID and WP_Post object.
|
||||
if ( $post_id instanceof \WP_Post ) {
|
||||
$post_id = $post_id->ID;
|
||||
}
|
||||
$album_id = get_post_meta( $post_id, '_fedistream_track_album', true );
|
||||
$audio_file = get_post_meta( $post_id, '_fedistream_track_audio_file', true );
|
||||
$artists = get_post_meta( $post_id, '_fedistream_track_artists', true ) ?: array();
|
||||
@@ -374,16 +526,21 @@ class TemplateLoader {
|
||||
/**
|
||||
* Get playlist-specific data.
|
||||
*
|
||||
* @param int $post_id Post ID.
|
||||
* @param int|\WP_Post $post_id Post ID or WP_Post object.
|
||||
* @param bool $load_nested Whether to load nested tracks.
|
||||
* @return array Playlist data.
|
||||
*/
|
||||
private static function get_playlist_data( int $post_id ): array {
|
||||
public static function get_playlist_data( int|\WP_Post $post_id, bool $load_nested = true ): array {
|
||||
// Support both post ID and WP_Post object.
|
||||
if ( $post_id instanceof \WP_Post ) {
|
||||
$post_id = $post_id->ID;
|
||||
}
|
||||
global $wpdb;
|
||||
|
||||
$table = $wpdb->prefix . 'fedistream_playlist_tracks';
|
||||
$table = $wpdb->prefix . 'fedistream_playlist_tracks';
|
||||
$duration = (int) get_post_meta( $post_id, '_fedistream_playlist_total_duration', true );
|
||||
|
||||
// Get tracks.
|
||||
// Get track IDs.
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||
$track_ids = $wpdb->get_col(
|
||||
$wpdb->prepare(
|
||||
@@ -392,11 +549,15 @@ class TemplateLoader {
|
||||
)
|
||||
);
|
||||
|
||||
$tracks = array();
|
||||
foreach ( $track_ids as $track_id ) {
|
||||
$track = get_post( $track_id );
|
||||
if ( $track && 'publish' === $track->post_status ) {
|
||||
$tracks[] = self::get_post_data( $track );
|
||||
$tracks = array();
|
||||
$track_count = count( $track_ids );
|
||||
|
||||
if ( $load_nested && ! empty( $track_ids ) ) {
|
||||
foreach ( $track_ids as $track_id ) {
|
||||
$track = get_post( $track_id );
|
||||
if ( $track && 'publish' === $track->post_status ) {
|
||||
$tracks[] = self::get_post_data( $track, true ); // Skip further nesting.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -404,7 +565,7 @@ class TemplateLoader {
|
||||
'visibility' => get_post_meta( $post_id, '_fedistream_playlist_visibility', true ) ?: 'public',
|
||||
'collaborative' => (bool) get_post_meta( $post_id, '_fedistream_playlist_collaborative', true ),
|
||||
'federated' => (bool) get_post_meta( $post_id, '_fedistream_playlist_federated', true ),
|
||||
'track_count' => count( $tracks ),
|
||||
'track_count' => $load_nested ? count( $tracks ) : $track_count,
|
||||
'total_duration' => $duration,
|
||||
'duration_formatted' => $duration >= 3600
|
||||
? sprintf( '%d:%02d:%02d', floor( $duration / 3600 ), floor( ( $duration % 3600 ) / 60 ), $duration % 60 )
|
||||
|
||||
@@ -13,6 +13,9 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
use WP_FediStream\Plugin;
|
||||
use WP_FediStream\Frontend\TemplateLoader;
|
||||
|
||||
// Enter shortcode context to prevent recursive shortcode processing in post content.
|
||||
TemplateLoader::enter_shortcode_context();
|
||||
|
||||
// Get template context.
|
||||
$context = TemplateLoader::get_context();
|
||||
|
||||
@@ -75,4 +78,7 @@ get_header();
|
||||
</main>
|
||||
|
||||
<?php
|
||||
// Exit shortcode context.
|
||||
TemplateLoader::exit_shortcode_context();
|
||||
|
||||
get_footer();
|
||||
+28
-1
@@ -55,6 +55,20 @@ final class Plugin {
|
||||
*/
|
||||
private ?\Twig\Environment $twig = null;
|
||||
|
||||
/**
|
||||
* Current Twig render depth to prevent infinite recursion.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private static int $render_depth = 0;
|
||||
|
||||
/**
|
||||
* Maximum allowed Twig render depth.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private const MAX_RENDER_DEPTH = 5;
|
||||
|
||||
/**
|
||||
* Post type instances.
|
||||
*
|
||||
@@ -843,7 +857,20 @@ final class Plugin {
|
||||
* @return string Rendered template.
|
||||
*/
|
||||
public function render( string $template, array $context = array() ): string {
|
||||
return $this->twig->render( $template . '.twig', $context );
|
||||
// Prevent infinite recursion in Twig rendering.
|
||||
if ( self::$render_depth >= self::MAX_RENDER_DEPTH ) {
|
||||
return '<!-- FediStream: render depth exceeded -->';
|
||||
}
|
||||
|
||||
++self::$render_depth;
|
||||
|
||||
try {
|
||||
$result = $this->twig->render( $template . '.twig', $context );
|
||||
} finally {
|
||||
--self::$render_depth;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+2
-2
@@ -3,7 +3,7 @@
|
||||
* Plugin Name: WP FediStream
|
||||
* Plugin URI: https://src.bundespruefstelle.ch/magdev/wp-fedistream
|
||||
* Description: Stream music over ActivityPub - Build your own music streaming platform for Musicians and Labels.
|
||||
* Version: 0.4.0
|
||||
* Version: 0.4.5
|
||||
* Requires at least: 6.4
|
||||
* Requires PHP: 8.3
|
||||
* Author: Marco Graetsch
|
||||
@@ -26,7 +26,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
define( 'WP_FEDISTREAM_VERSION', '0.4.0' );
|
||||
define( 'WP_FEDISTREAM_VERSION', '0.4.5' );
|
||||
|
||||
/**
|
||||
* Plugin file path.
|
||||
|
||||
Reference in new issue
Block a user