You've already forked wp-fedistream
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0627dd0db7 | |||
| df3b8a7ec2 | |||
| d1597aa854 | |||
| 12f7d8f7b3 | |||
| bdc11d8769 | |||
| 35ad390aeb | |||
| b592e45d58 | |||
| a41eddbc49 | |||
| eb85870909 | |||
| 6988e49287 | |||
| 166a5e6f7c | |||
| fedab21c2a |
108
CHANGELOG.md
108
CHANGELOG.md
@@ -7,6 +7,104 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
## [0.5.1] - 2026-02-02
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- **Localhost license bypass** - License check is automatically bypassed on local development environments
|
||||||
|
- Detects `localhost`, `127.0.0.1`, `::1`
|
||||||
|
- Detects common local TLDs: `.local`, `.test`, `.localhost`, `.dev.local`
|
||||||
|
- Allows full plugin functionality without license on development sites
|
||||||
|
|
||||||
|
## [0.5.0] - 2026-02-02
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- **Prometheus Metrics Integration** - Expose FediStream metrics for monitoring
|
||||||
|
- Content metrics: `fedistream_content_total` (by type/status), `fedistream_genres_total`, `fedistream_moods_total`
|
||||||
|
- Engagement metrics: `fedistream_plays_total`, `fedistream_plays_today`, `fedistream_favorites_total`, `fedistream_local_follows_total`, `fedistream_listening_history_entries`
|
||||||
|
- User metrics: `fedistream_users_with_library`, `fedistream_users_following_artists`, `fedistream_notifications_total`, `fedistream_notifications_pending`
|
||||||
|
- WooCommerce metrics (conditional): `fedistream_purchases_total`, `fedistream_customers_total`, `fedistream_products_total`
|
||||||
|
- ActivityPub metrics (conditional): `fedistream_activitypub_followers_total`, `fedistream_activitypub_followers_by_artist`, `fedistream_activitypub_reactions_total`
|
||||||
|
- New setting in Integrations tab to enable/disable Prometheus metrics
|
||||||
|
- Requires WP Prometheus plugin to be active
|
||||||
|
|
||||||
|
## [0.4.9] - 2026-02-02
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- **Reverted nuclear option** - Restored conditional the_content filter usage
|
||||||
|
- `get_post_data()` now uses the_content filter only when NOT in shortcode context, NOT at depth > 1, and NOT loading page template
|
||||||
|
- All other protections remain in place (render depth, page template loading flag, main template lock, shortcode context)
|
||||||
|
- Memory leak investigation to be continued later
|
||||||
|
|
||||||
|
## [0.4.8] - 2026-02-02
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- **Nuclear option: NEVER apply the_content filter** - Completely removed the_content filter usage (reverted in 0.4.9)
|
||||||
|
- `get_post_data()` now ALWAYS strips shortcodes and uses raw content
|
||||||
|
- NEVER calls `apply_filters('the_content', ...)` or `get_the_excerpt()`
|
||||||
|
- FediStream posts don't need shortcode processing in their content anyway
|
||||||
|
- This guarantees no recursion through WordPress hook system
|
||||||
|
|
||||||
|
## [0.4.7] - 2026-02-02
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- **Hard main template rendering lock** - Added additional protection at Plugin::render() level
|
||||||
|
- Added `$rendering_main_template` flag that completely blocks any other render calls while main template is rendering
|
||||||
|
- Reduced MAX_RENDER_DEPTH from 5 to 2 (allows one level of {% include %} but prevents deeper recursion)
|
||||||
|
- template-wrapper.php now passes `is_main_template = true` to enable the hard lock
|
||||||
|
- Any render attempt during main template rendering is immediately blocked
|
||||||
|
|
||||||
|
## [0.4.6] - 2026-02-02
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- **Page template loading lock** - Block ALL shortcode rendering during page template loading
|
||||||
|
- Added `$loading_page_template` flag in TemplateLoader
|
||||||
|
- template-wrapper.php now sets this flag before loading theme header/footer
|
||||||
|
- Shortcodes::render_template() checks this flag and returns early if set
|
||||||
|
- This prevents any recursion triggered by theme components, widgets, or other plugins during page template loading
|
||||||
|
- Main template rendering still works (uses Plugin::render() directly, not through Shortcodes)
|
||||||
|
|
||||||
|
## [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
|
## [0.4.1] - 2026-02-02
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
@@ -207,7 +305,15 @@ Initial release of WP FediStream - a WordPress plugin for streaming music over A
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
[Unreleased]: https://src.bundespruefstelle.ch/magdev/wp-fedistream/compare/v0.4.1...HEAD
|
[Unreleased]: https://src.bundespruefstelle.ch/magdev/wp-fedistream/compare/v0.4.9...HEAD
|
||||||
|
[0.4.9]: https://src.bundespruefstelle.ch/magdev/wp-fedistream/compare/v0.4.8...v0.4.9
|
||||||
|
[0.4.8]: https://src.bundespruefstelle.ch/magdev/wp-fedistream/compare/v0.4.7...v0.4.8
|
||||||
|
[0.4.7]: https://src.bundespruefstelle.ch/magdev/wp-fedistream/compare/v0.4.6...v0.4.7
|
||||||
|
[0.4.6]: https://src.bundespruefstelle.ch/magdev/wp-fedistream/compare/v0.4.5...v0.4.6
|
||||||
|
[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.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.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.3.0]: https://src.bundespruefstelle.ch/magdev/wp-fedistream/compare/v0.2.0...v0.3.0
|
||||||
|
|||||||
55
CLAUDE.md
55
CLAUDE.md
@@ -552,3 +552,58 @@ wp-fedistream/
|
|||||||
- Uses Gitea API directly for release creation (not GitHub Actions)
|
- Uses Gitea API directly for release creation (not GitHub Actions)
|
||||||
- Submodule uses relative URL for CI compatibility
|
- Submodule uses relative URL for CI compatibility
|
||||||
- Composer symlinks from `lib/wc-licensed-product-client` to vendor
|
- Composer symlinks from `lib/wc-licensed-product-client` to vendor
|
||||||
|
|
||||||
|
### 2026-02-02 - Memory Leak Investigation v0.4.1 through v0.4.9
|
||||||
|
|
||||||
|
**Summary:** Investigated memory exhaustion issue when WP FediStream is used with WP Prometheus plugin. Multiple fix attempts were made but the root cause remains unresolved.
|
||||||
|
|
||||||
|
**Problem:**
|
||||||
|
|
||||||
|
- PHP Fatal error: Allowed memory size exhausted (1GB)
|
||||||
|
- Error locations varied: Twig StagingExtension.php, Environment.php, ExtensionSet.php, WordPress class-wp-hook.php
|
||||||
|
- Only occurs when WP Prometheus plugin is also active
|
||||||
|
- Suspected infinite recursion through WordPress hook system
|
||||||
|
|
||||||
|
**Fix Attempts (v0.4.1 - v0.4.8):**
|
||||||
|
|
||||||
|
1. **v0.4.1** - Added recursion depth tracking in `get_post_data()`, skip `the_content` filter at depth > 1
|
||||||
|
2. **v0.4.2** - Added `$in_shortcode_context` flag, all shortcode render methods enter context before data loading
|
||||||
|
3. **v0.4.3** - Changed boolean to counter (`$shortcode_context_depth`), added context to `template-wrapper.php`
|
||||||
|
4. **v0.4.4** - Fixed `get_the_excerpt()` which internally triggers `the_content` filter
|
||||||
|
5. **v0.4.5** - Added render depth tracking in `Plugin::render()`, strip shortcodes from content
|
||||||
|
6. **v0.4.6** - Added `$loading_page_template` flag to block shortcode rendering during page template loading
|
||||||
|
7. **v0.4.7** - Added `$rendering_main_template` hard lock in `Plugin::render()`, reduced MAX_RENDER_DEPTH to 2
|
||||||
|
8. **v0.4.8** - Nuclear option: ALWAYS skip `the_content` filter (didn't work, reverted)
|
||||||
|
|
||||||
|
**v0.4.9 - Current State:**
|
||||||
|
|
||||||
|
- Reverted nuclear option
|
||||||
|
- Kept all other protections in place
|
||||||
|
- Issue documented in README.md as known incompatibility
|
||||||
|
|
||||||
|
**Files Modified:**
|
||||||
|
|
||||||
|
- `includes/Frontend/TemplateLoader.php` - Multiple recursion protection mechanisms
|
||||||
|
- `includes/Frontend/Shortcodes.php` - Shortcode context entry, page template loading check
|
||||||
|
- `includes/Frontend/template-wrapper.php` - Page template loading flag, main template render flag
|
||||||
|
- `includes/Plugin.php` - Render depth tracking, main template rendering lock
|
||||||
|
- `README.md` - Added Known Issues section
|
||||||
|
|
||||||
|
**Protection Mechanisms in Place:**
|
||||||
|
|
||||||
|
1. `$recursion_depth` counter in `get_post_data()` (max 3)
|
||||||
|
2. `$shortcode_context_depth` counter for nested shortcodes
|
||||||
|
3. `$loading_page_template` flag blocks shortcode rendering during page load
|
||||||
|
4. `$rendering_main_template` flag in `Plugin::render()` blocks parallel renders
|
||||||
|
5. `MAX_RENDER_DEPTH = 2` in `Plugin::render()`
|
||||||
|
6. Skip `the_content` and `get_the_excerpt()` when in protected context
|
||||||
|
7. Strip shortcodes from content when skipping content filter
|
||||||
|
|
||||||
|
**Key Learnings:**
|
||||||
|
|
||||||
|
- `get_the_excerpt()` internally calls `apply_filters('the_content', ...)` when generating auto-excerpts
|
||||||
|
- `the_content` filter triggers `do_shortcode()` which can cause recursive shortcode processing
|
||||||
|
- WordPress hook system (class-wp-hook.php) can itself be the recursion point
|
||||||
|
- The interaction between FediStream and WP Prometheus appears to be at a fundamental WordPress level
|
||||||
|
|
||||||
|
**Status:** UNRESOLVED - Documented as known incompatibility, investigation to continue later
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Stream music over ActivityPub - Build your own music streaming platform for Musicians and Labels.
|
Stream music over ActivityPub - Build your own music streaming platform for Musicians and Labels.
|
||||||
|
|
||||||
[](CHANGELOG.md)
|
[](CHANGELOG.md)
|
||||||
[](https://php.net)
|
[](https://php.net)
|
||||||
[](https://wordpress.org)
|
[](https://wordpress.org)
|
||||||
[](https://www.gnu.org/licenses/gpl-2.0.html)
|
[](https://www.gnu.org/licenses/gpl-2.0.html)
|
||||||
|
|||||||
@@ -81,6 +81,9 @@ class Shortcodes {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function render_artist( array $atts ): 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(
|
$atts = shortcode_atts(
|
||||||
array(
|
array(
|
||||||
'id' => 0,
|
'id' => 0,
|
||||||
@@ -95,6 +98,7 @@ class Shortcodes {
|
|||||||
|
|
||||||
$post = $this->get_post( $atts, 'fedistream_artist' );
|
$post = $this->get_post( $atts, 'fedistream_artist' );
|
||||||
if ( ! $post ) {
|
if ( ! $post ) {
|
||||||
|
TemplateLoader::exit_shortcode_context();
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,6 +123,9 @@ class Shortcodes {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function render_album( array $atts ): 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(
|
$atts = shortcode_atts(
|
||||||
array(
|
array(
|
||||||
'id' => 0,
|
'id' => 0,
|
||||||
@@ -132,6 +139,7 @@ class Shortcodes {
|
|||||||
|
|
||||||
$post = $this->get_post( $atts, 'fedistream_album' );
|
$post = $this->get_post( $atts, 'fedistream_album' );
|
||||||
if ( ! $post ) {
|
if ( ! $post ) {
|
||||||
|
TemplateLoader::exit_shortcode_context();
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,6 +163,9 @@ class Shortcodes {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function render_track( array $atts ): 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(
|
$atts = shortcode_atts(
|
||||||
array(
|
array(
|
||||||
'id' => 0,
|
'id' => 0,
|
||||||
@@ -168,6 +179,7 @@ class Shortcodes {
|
|||||||
|
|
||||||
$post = $this->get_post( $atts, 'fedistream_track' );
|
$post = $this->get_post( $atts, 'fedistream_track' );
|
||||||
if ( ! $post ) {
|
if ( ! $post ) {
|
||||||
|
TemplateLoader::exit_shortcode_context();
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,6 +203,9 @@ class Shortcodes {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function render_playlist( array $atts ): 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(
|
$atts = shortcode_atts(
|
||||||
array(
|
array(
|
||||||
'id' => 0,
|
'id' => 0,
|
||||||
@@ -204,6 +219,7 @@ class Shortcodes {
|
|||||||
|
|
||||||
$post = $this->get_post( $atts, 'fedistream_playlist' );
|
$post = $this->get_post( $atts, 'fedistream_playlist' );
|
||||||
if ( ! $post ) {
|
if ( ! $post ) {
|
||||||
|
TemplateLoader::exit_shortcode_context();
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,6 +243,9 @@ class Shortcodes {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function render_latest_releases( array $atts ): 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(
|
$atts = shortcode_atts(
|
||||||
array(
|
array(
|
||||||
'count' => 6,
|
'count' => 6,
|
||||||
@@ -292,6 +311,9 @@ class Shortcodes {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function render_popular_tracks( array $atts ): 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(
|
$atts = shortcode_atts(
|
||||||
array(
|
array(
|
||||||
'count' => 10,
|
'count' => 10,
|
||||||
@@ -359,6 +381,9 @@ class Shortcodes {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function render_artists_grid( array $atts ): 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(
|
$atts = shortcode_atts(
|
||||||
array(
|
array(
|
||||||
'count' => 12,
|
'count' => 12,
|
||||||
@@ -426,6 +451,9 @@ class Shortcodes {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function render_player( array $atts ): 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(
|
$atts = shortcode_atts(
|
||||||
array(
|
array(
|
||||||
'track' => 0,
|
'track' => 0,
|
||||||
@@ -471,6 +499,7 @@ class Shortcodes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ( empty( $tracks ) ) {
|
if ( empty( $tracks ) ) {
|
||||||
|
TemplateLoader::exit_shortcode_context();
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -523,18 +552,31 @@ class Shortcodes {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function render_template( string $template, array $context ): string {
|
private function render_template( string $template, array $context ): string {
|
||||||
|
// Block shortcode rendering while loading page template to prevent recursion.
|
||||||
|
// This catches any shortcodes triggered by theme header/footer, widgets, etc.
|
||||||
|
if ( TemplateLoader::is_loading_page_template() ) {
|
||||||
|
return '<!-- FediStream: shortcode blocked during page template loading -->';
|
||||||
|
}
|
||||||
|
|
||||||
// Check for unlicensed mode.
|
// Check for unlicensed mode.
|
||||||
if ( $this->unlicensed_mode ) {
|
if ( $this->unlicensed_mode ) {
|
||||||
return $this->get_unlicensed_message();
|
return $this->get_unlicensed_message();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Enter shortcode context to prevent recursive shortcode processing.
|
||||||
|
TemplateLoader::enter_shortcode_context();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return $this->plugin->render( $template, $context );
|
$result = $this->plugin->render( $template, $context );
|
||||||
} catch ( \Exception $e ) {
|
} catch ( \Exception $e ) {
|
||||||
|
TemplateLoader::exit_shortcode_context();
|
||||||
if ( WP_DEBUG ) {
|
if ( WP_DEBUG ) {
|
||||||
return '<p class="fedistream-error">' . esc_html( $e->getMessage() ) . '</p>';
|
return '<p class="fedistream-error">' . esc_html( $e->getMessage() ) . '</p>';
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TemplateLoader::exit_shortcode_context();
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,82 @@ class TemplateLoader {
|
|||||||
*/
|
*/
|
||||||
private const MAX_RECURSION_DEPTH = 3;
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flag indicating we're currently loading a FediStream page template.
|
||||||
|
* This completely blocks any nested FediStream shortcode rendering.
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private static bool $loading_page_template = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enter page template loading mode.
|
||||||
|
* This blocks ALL shortcode rendering during page template loading.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function enter_page_template_loading(): void {
|
||||||
|
self::$loading_page_template = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exit page template loading mode.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function exit_page_template_loading(): void {
|
||||||
|
self::$loading_page_template = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if we're loading a page template.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function is_loading_page_template(): bool {
|
||||||
|
return self::$loading_page_template;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
@@ -213,14 +289,40 @@ class TemplateLoader {
|
|||||||
// Track recursion to prevent infinite loops from shortcodes in content.
|
// Track recursion to prevent infinite loops from shortcodes in content.
|
||||||
++self::$recursion_depth;
|
++self::$recursion_depth;
|
||||||
|
|
||||||
// At depth > 1, skip the_content filter to prevent shortcode recursion.
|
// Skip the_content filter if:
|
||||||
$is_nested = self::$recursion_depth > 1;
|
// 1. We're in a shortcode context (prevents recursive shortcode processing)
|
||||||
|
// 2. We're at depth > 1 (nested data loading)
|
||||||
|
// 3. We're loading a page template
|
||||||
|
$skip_content_filter = self::$shortcode_context_depth > 0
|
||||||
|
|| self::$recursion_depth > 1
|
||||||
|
|| self::$loading_page_template;
|
||||||
|
|
||||||
|
// When skipping content filter, 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, 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(
|
$data = array(
|
||||||
'id' => $post->ID,
|
'id' => $post->ID,
|
||||||
'title' => get_the_title( $post ),
|
'title' => get_the_title( $post ),
|
||||||
'content' => $is_nested ? wp_kses_post( $post->post_content ) : apply_filters( 'the_content', $post->post_content ),
|
'content' => $content,
|
||||||
'excerpt' => get_the_excerpt( $post ),
|
'excerpt' => $excerpt,
|
||||||
'permalink' => get_permalink( $post ),
|
'permalink' => get_permalink( $post ),
|
||||||
'thumbnail' => get_the_post_thumbnail_url( $post->ID, 'large' ),
|
'thumbnail' => get_the_post_thumbnail_url( $post->ID, 'large' ),
|
||||||
'date' => get_the_date( '', $post ),
|
'date' => get_the_date( '', $post ),
|
||||||
|
|||||||
@@ -13,6 +13,12 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||||||
use WP_FediStream\Plugin;
|
use WP_FediStream\Plugin;
|
||||||
use WP_FediStream\Frontend\TemplateLoader;
|
use WP_FediStream\Frontend\TemplateLoader;
|
||||||
|
|
||||||
|
// Enter page template loading mode - this completely blocks nested FediStream rendering.
|
||||||
|
TemplateLoader::enter_page_template_loading();
|
||||||
|
|
||||||
|
// Also enter shortcode context to prevent recursive shortcode processing in post content.
|
||||||
|
TemplateLoader::enter_shortcode_context();
|
||||||
|
|
||||||
// Get template context.
|
// Get template context.
|
||||||
$context = TemplateLoader::get_context();
|
$context = TemplateLoader::get_context();
|
||||||
|
|
||||||
@@ -54,7 +60,8 @@ get_header();
|
|||||||
if ( $template_name ) {
|
if ( $template_name ) {
|
||||||
try {
|
try {
|
||||||
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||||
echo $plugin->render( $template_name, $context );
|
// Pass true for is_main_template to set the hard rendering lock.
|
||||||
|
echo $plugin->render( $template_name, $context, true );
|
||||||
} catch ( \Exception $e ) {
|
} catch ( \Exception $e ) {
|
||||||
if ( WP_DEBUG ) {
|
if ( WP_DEBUG ) {
|
||||||
echo '<div class="fedistream-error">';
|
echo '<div class="fedistream-error">';
|
||||||
@@ -75,4 +82,8 @@ get_header();
|
|||||||
</main>
|
</main>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
// Exit shortcode context and page template loading mode.
|
||||||
|
TemplateLoader::exit_shortcode_context();
|
||||||
|
TemplateLoader::exit_page_template_loading();
|
||||||
|
|
||||||
get_footer();
|
get_footer();
|
||||||
|
|||||||
@@ -347,6 +347,7 @@ class Installer {
|
|||||||
$defaults = array(
|
$defaults = array(
|
||||||
'wp_fedistream_enable_activitypub' => 1,
|
'wp_fedistream_enable_activitypub' => 1,
|
||||||
'wp_fedistream_enable_woocommerce' => 0,
|
'wp_fedistream_enable_woocommerce' => 0,
|
||||||
|
'wp_fedistream_enable_prometheus' => 0,
|
||||||
'wp_fedistream_audio_formats' => array( 'mp3', 'wav', 'flac', 'ogg' ),
|
'wp_fedistream_audio_formats' => array( 'mp3', 'wav', 'flac', 'ogg' ),
|
||||||
'wp_fedistream_max_upload_size' => 50, // MB
|
'wp_fedistream_max_upload_size' => 50, // MB
|
||||||
'wp_fedistream_default_license' => 'all-rights-reserved',
|
'wp_fedistream_default_license' => 'all-rights-reserved',
|
||||||
|
|||||||
@@ -421,15 +421,50 @@ final class Manager {
|
|||||||
/**
|
/**
|
||||||
* Check if the license is currently valid.
|
* Check if the license is currently valid.
|
||||||
*
|
*
|
||||||
* Uses cached status for performance.
|
* Uses cached status for performance. Bypasses license check on localhost.
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function is_license_valid(): bool {
|
public static function is_license_valid(): bool {
|
||||||
|
// Bypass license check on localhost for development.
|
||||||
|
if ( self::is_localhost() ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
$status = get_option( self::OPTION_LICENSE_STATUS, 'unchecked' );
|
$status = get_option( self::OPTION_LICENSE_STATUS, 'unchecked' );
|
||||||
return 'valid' === $status;
|
return 'valid' === $status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the current site is running on localhost.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function is_localhost(): bool {
|
||||||
|
$host = wp_parse_url( home_url(), PHP_URL_HOST );
|
||||||
|
|
||||||
|
// Common localhost identifiers.
|
||||||
|
$localhost_hosts = array(
|
||||||
|
'localhost',
|
||||||
|
'127.0.0.1',
|
||||||
|
'::1',
|
||||||
|
);
|
||||||
|
|
||||||
|
if ( in_array( $host, $localhost_hosts, true ) ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Common local development TLDs.
|
||||||
|
$local_tlds = array( '.local', '.test', '.localhost', '.dev.local' );
|
||||||
|
foreach ( $local_tlds as $tld ) {
|
||||||
|
if ( str_ends_with( $host, $tld ) ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the license key.
|
* Get the license key.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ use WP_FediStream\Frontend\TemplateLoader;
|
|||||||
use WP_FediStream\Frontend\Widgets;
|
use WP_FediStream\Frontend\Widgets;
|
||||||
use WP_FediStream\PostTypes\Artist;
|
use WP_FediStream\PostTypes\Artist;
|
||||||
use WP_FediStream\WooCommerce\Integration as WooCommerceIntegration;
|
use WP_FediStream\WooCommerce\Integration as WooCommerceIntegration;
|
||||||
|
use WP_FediStream\Prometheus\Integration as PrometheusIntegration;
|
||||||
use WP_FediStream\WooCommerce\DigitalDelivery;
|
use WP_FediStream\WooCommerce\DigitalDelivery;
|
||||||
use WP_FediStream\WooCommerce\StreamingAccess;
|
use WP_FediStream\WooCommerce\StreamingAccess;
|
||||||
use WP_FediStream\PostTypes\Album;
|
use WP_FediStream\PostTypes\Album;
|
||||||
@@ -55,6 +56,29 @@ final class Plugin {
|
|||||||
*/
|
*/
|
||||||
private ?\Twig\Environment $twig = null;
|
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.
|
||||||
|
* Set to 2 to allow one level of nested includes but prevent deeper recursion.
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private const MAX_RENDER_DEPTH = 2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flag to track if we're currently rendering the main page template.
|
||||||
|
* This is a hard lock that prevents ANY other rendering.
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private static bool $rendering_main_template = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Post type instances.
|
* Post type instances.
|
||||||
*
|
*
|
||||||
@@ -184,6 +208,11 @@ final class Plugin {
|
|||||||
new StreamingAccess();
|
new StreamingAccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize Prometheus integration.
|
||||||
|
if ( get_option( 'wp_fedistream_enable_prometheus', 0 ) && $this->is_prometheus_active() ) {
|
||||||
|
new PrometheusIntegration();
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize user library and notifications.
|
// Initialize user library and notifications.
|
||||||
new UserLibrary();
|
new UserLibrary();
|
||||||
new LibraryPage();
|
new LibraryPage();
|
||||||
@@ -425,6 +454,7 @@ final class Plugin {
|
|||||||
// Get current settings.
|
// Get current settings.
|
||||||
$enable_activitypub = get_option( 'wp_fedistream_enable_activitypub', 1 );
|
$enable_activitypub = get_option( 'wp_fedistream_enable_activitypub', 1 );
|
||||||
$enable_woocommerce = get_option( 'wp_fedistream_enable_woocommerce', 0 );
|
$enable_woocommerce = get_option( 'wp_fedistream_enable_woocommerce', 0 );
|
||||||
|
$enable_prometheus = get_option( 'wp_fedistream_enable_prometheus', 0 );
|
||||||
$max_upload_size = get_option( 'wp_fedistream_max_upload_size', 50 );
|
$max_upload_size = get_option( 'wp_fedistream_max_upload_size', 50 );
|
||||||
$default_license = get_option( 'wp_fedistream_default_license', 'all-rights-reserved' );
|
$default_license = get_option( 'wp_fedistream_default_license', 'all-rights-reserved' );
|
||||||
|
|
||||||
@@ -463,7 +493,7 @@ final class Plugin {
|
|||||||
$this->render_settings_tab( $max_upload_size, $default_license );
|
$this->render_settings_tab( $max_upload_size, $default_license );
|
||||||
break;
|
break;
|
||||||
case 'integrations':
|
case 'integrations':
|
||||||
$this->render_integrations_tab( $enable_activitypub, $enable_woocommerce );
|
$this->render_integrations_tab( $enable_activitypub, $enable_woocommerce, $enable_prometheus );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
@@ -502,6 +532,7 @@ final class Plugin {
|
|||||||
case 'integrations':
|
case 'integrations':
|
||||||
update_option( 'wp_fedistream_enable_activitypub', isset( $_POST['enable_activitypub'] ) ? 1 : 0 );
|
update_option( 'wp_fedistream_enable_activitypub', isset( $_POST['enable_activitypub'] ) ? 1 : 0 );
|
||||||
update_option( 'wp_fedistream_enable_woocommerce', isset( $_POST['enable_woocommerce'] ) ? 1 : 0 );
|
update_option( 'wp_fedistream_enable_woocommerce', isset( $_POST['enable_woocommerce'] ) ? 1 : 0 );
|
||||||
|
update_option( 'wp_fedistream_enable_prometheus', isset( $_POST['enable_prometheus'] ) ? 1 : 0 );
|
||||||
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Integration settings saved.', 'wp-fedistream' ) . '</p></div>';
|
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Integration settings saved.', 'wp-fedistream' ) . '</p></div>';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -696,9 +727,10 @@ final class Plugin {
|
|||||||
*
|
*
|
||||||
* @param int $enable_activitypub Whether ActivityPub is enabled.
|
* @param int $enable_activitypub Whether ActivityPub is enabled.
|
||||||
* @param int $enable_woocommerce Whether WooCommerce integration is enabled.
|
* @param int $enable_woocommerce Whether WooCommerce integration is enabled.
|
||||||
|
* @param int $enable_prometheus Whether Prometheus metrics are enabled.
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function render_integrations_tab( int $enable_activitypub, int $enable_woocommerce ): void {
|
private function render_integrations_tab( int $enable_activitypub, int $enable_woocommerce, int $enable_prometheus ): void {
|
||||||
?>
|
?>
|
||||||
<form method="post" action="">
|
<form method="post" action="">
|
||||||
<?php wp_nonce_field( 'fedistream_save_settings', 'fedistream_settings_nonce' ); ?>
|
<?php wp_nonce_field( 'fedistream_save_settings', 'fedistream_settings_nonce' ); ?>
|
||||||
@@ -737,6 +769,23 @@ final class Plugin {
|
|||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th scope="row"><?php esc_html_e( 'Prometheus Metrics', 'wp-fedistream' ); ?></th>
|
||||||
|
<td>
|
||||||
|
<label>
|
||||||
|
<input type="checkbox" name="enable_prometheus" value="1" <?php checked( $enable_prometheus, 1 ); ?> <?php disabled( ! $this->is_prometheus_active() ); ?>>
|
||||||
|
<?php esc_html_e( 'Enable Prometheus metrics', 'wp-fedistream' ); ?>
|
||||||
|
</label>
|
||||||
|
<?php if ( ! $this->is_prometheus_active() ) : ?>
|
||||||
|
<p class="description" style="color: #d63638;">
|
||||||
|
<span class="dashicons dashicons-dismiss" style="font-size: 16px; width: 16px; height: 16px; vertical-align: text-bottom;"></span>
|
||||||
|
<?php esc_html_e( 'WP Prometheus plugin is not installed or active.', 'wp-fedistream' ); ?>
|
||||||
|
</p>
|
||||||
|
<?php else : ?>
|
||||||
|
<p class="description"><?php esc_html_e( 'Expose FediStream metrics for Prometheus monitoring.', 'wp-fedistream' ); ?></p>
|
||||||
|
<?php endif; ?>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<?php submit_button(); ?>
|
<?php submit_button(); ?>
|
||||||
@@ -842,8 +891,35 @@ final class Plugin {
|
|||||||
* @param array $context Template context variables.
|
* @param array $context Template context variables.
|
||||||
* @return string Rendered template.
|
* @return string Rendered template.
|
||||||
*/
|
*/
|
||||||
public function render( string $template, array $context = array() ): string {
|
public function render( string $template, array $context = array(), bool $is_main_template = false ): string {
|
||||||
return $this->twig->render( $template . '.twig', $context );
|
// If we're already rendering the main template, block any other renders.
|
||||||
|
if ( self::$rendering_main_template && ! $is_main_template ) {
|
||||||
|
return '<!-- FediStream: blocked during main template render -->';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prevent infinite recursion in Twig rendering.
|
||||||
|
if ( self::$render_depth >= self::MAX_RENDER_DEPTH ) {
|
||||||
|
return '<!-- FediStream: render depth exceeded -->';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set main template lock if this is the main template.
|
||||||
|
$was_main = self::$rendering_main_template;
|
||||||
|
if ( $is_main_template ) {
|
||||||
|
self::$rendering_main_template = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
++self::$render_depth;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$result = $this->twig->render( $template . '.twig', $context );
|
||||||
|
} finally {
|
||||||
|
--self::$render_depth;
|
||||||
|
if ( $is_main_template ) {
|
||||||
|
self::$rendering_main_template = $was_main;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -883,4 +959,13 @@ final class Plugin {
|
|||||||
public function is_activitypub_active(): bool {
|
public function is_activitypub_active(): bool {
|
||||||
return class_exists( 'Activitypub\Activitypub' );
|
return class_exists( 'Activitypub\Activitypub' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if WP Prometheus plugin is active.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function is_prometheus_active(): bool {
|
||||||
|
return defined( 'WP_PROMETHEUS_VERSION' ) || class_exists( 'WP_Prometheus\Plugin' );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
422
includes/Prometheus/Integration.php
Normal file
422
includes/Prometheus/Integration.php
Normal file
@@ -0,0 +1,422 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Prometheus Integration.
|
||||||
|
*
|
||||||
|
* @package WP_FediStream
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace WP_FediStream\Prometheus;
|
||||||
|
|
||||||
|
// Prevent direct file access.
|
||||||
|
if ( ! defined( 'ABSPATH' ) ) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main Prometheus integration class.
|
||||||
|
*
|
||||||
|
* Exposes FediStream metrics for Prometheus monitoring via the
|
||||||
|
* wp_prometheus_collect_metrics action hook.
|
||||||
|
*/
|
||||||
|
class Integration {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether WP Prometheus is active.
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private bool $prometheus_active = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*/
|
||||||
|
public function __construct() {
|
||||||
|
// Check WP Prometheus immediately since we're instantiated during plugins_loaded.
|
||||||
|
$this->check_prometheus();
|
||||||
|
|
||||||
|
// If plugins_loaded hasn't fully completed, hook init at priority 20.
|
||||||
|
// Otherwise, run init directly.
|
||||||
|
if ( ! did_action( 'plugins_loaded' ) || doing_action( 'plugins_loaded' ) ) {
|
||||||
|
add_action( 'plugins_loaded', array( $this, 'init' ), 20 );
|
||||||
|
} else {
|
||||||
|
$this->init();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if WP Prometheus is active.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function check_prometheus(): void {
|
||||||
|
$this->prometheus_active = defined( 'WP_PROMETHEUS_VERSION' )
|
||||||
|
|| class_exists( 'WP_Prometheus\Plugin' );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize Prometheus integration.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function init(): void {
|
||||||
|
if ( ! $this->prometheus_active ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register metrics collector.
|
||||||
|
add_action( 'wp_prometheus_collect_metrics', array( $this, 'collect_metrics' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if Prometheus is active.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function is_active(): bool {
|
||||||
|
return $this->prometheus_active;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collect FediStream metrics.
|
||||||
|
*
|
||||||
|
* @param object $collector The Prometheus collector.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function collect_metrics( $collector ): void {
|
||||||
|
// Register and collect all metric categories.
|
||||||
|
$this->collect_content_metrics( $collector );
|
||||||
|
$this->collect_engagement_metrics( $collector );
|
||||||
|
$this->collect_user_metrics( $collector );
|
||||||
|
|
||||||
|
if ( $this->is_woocommerce_enabled() ) {
|
||||||
|
$this->collect_woocommerce_metrics( $collector );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( $this->is_activitypub_enabled() ) {
|
||||||
|
$this->collect_activitypub_metrics( $collector );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collect content metrics.
|
||||||
|
*
|
||||||
|
* @param object $collector The Prometheus collector.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function collect_content_metrics( $collector ): void {
|
||||||
|
// Content count by type and status.
|
||||||
|
$content_gauge = $collector->register_gauge(
|
||||||
|
'fedistream_content_total',
|
||||||
|
'Total FediStream content count',
|
||||||
|
array( 'type', 'status' )
|
||||||
|
);
|
||||||
|
|
||||||
|
$post_types = array(
|
||||||
|
'fedistream_artist',
|
||||||
|
'fedistream_album',
|
||||||
|
'fedistream_track',
|
||||||
|
'fedistream_playlist',
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ( $post_types as $post_type ) {
|
||||||
|
$type_label = str_replace( 'fedistream_', '', $post_type );
|
||||||
|
$counts = wp_count_posts( $post_type );
|
||||||
|
|
||||||
|
foreach ( array( 'publish', 'draft', 'pending' ) as $status ) {
|
||||||
|
$count = isset( $counts->$status ) ? (int) $counts->$status : 0;
|
||||||
|
$content_gauge->set( $count, array( $type_label, $status ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Genre count.
|
||||||
|
$genres_gauge = $collector->register_gauge(
|
||||||
|
'fedistream_genres_total',
|
||||||
|
'Total number of genres',
|
||||||
|
array()
|
||||||
|
);
|
||||||
|
$genre_count = wp_count_terms( array( 'taxonomy' => 'fedistream_genre' ) );
|
||||||
|
$genres_gauge->set( is_wp_error( $genre_count ) ? 0 : (int) $genre_count, array() );
|
||||||
|
|
||||||
|
// Mood count.
|
||||||
|
$moods_gauge = $collector->register_gauge(
|
||||||
|
'fedistream_moods_total',
|
||||||
|
'Total number of moods',
|
||||||
|
array()
|
||||||
|
);
|
||||||
|
$mood_count = wp_count_terms( array( 'taxonomy' => 'fedistream_mood' ) );
|
||||||
|
$moods_gauge->set( is_wp_error( $mood_count ) ? 0 : (int) $mood_count, array() );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collect engagement metrics.
|
||||||
|
*
|
||||||
|
* @param object $collector The Prometheus collector.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function collect_engagement_metrics( $collector ): void {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$plays_table = $wpdb->prefix . 'fedistream_plays';
|
||||||
|
$favorites_table = $wpdb->prefix . 'fedistream_favorites';
|
||||||
|
$follows_table = $wpdb->prefix . 'fedistream_user_follows';
|
||||||
|
$history_table = $wpdb->prefix . 'fedistream_listening_history';
|
||||||
|
|
||||||
|
// Total plays.
|
||||||
|
$plays_gauge = $collector->register_gauge(
|
||||||
|
'fedistream_plays_total',
|
||||||
|
'Total track plays',
|
||||||
|
array()
|
||||||
|
);
|
||||||
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||||
|
$total_plays = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$plays_table}" );
|
||||||
|
$plays_gauge->set( $total_plays, array() );
|
||||||
|
|
||||||
|
// Plays today.
|
||||||
|
$plays_today_gauge = $collector->register_gauge(
|
||||||
|
'fedistream_plays_today',
|
||||||
|
'Track plays today',
|
||||||
|
array()
|
||||||
|
);
|
||||||
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||||
|
$plays_today = (int) $wpdb->get_var(
|
||||||
|
"SELECT COUNT(*) FROM {$plays_table} WHERE DATE(played_at) = CURDATE()"
|
||||||
|
);
|
||||||
|
$plays_today_gauge->set( $plays_today, array() );
|
||||||
|
|
||||||
|
// Favorites by type.
|
||||||
|
$favorites_gauge = $collector->register_gauge(
|
||||||
|
'fedistream_favorites_total',
|
||||||
|
'Total favorites by content type',
|
||||||
|
array( 'type' )
|
||||||
|
);
|
||||||
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||||
|
$favorites_by_type = $wpdb->get_results(
|
||||||
|
"SELECT content_type, COUNT(*) as count FROM {$favorites_table} GROUP BY content_type"
|
||||||
|
);
|
||||||
|
foreach ( $favorites_by_type as $row ) {
|
||||||
|
$favorites_gauge->set( (int) $row->count, array( $row->content_type ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Local follows.
|
||||||
|
$follows_gauge = $collector->register_gauge(
|
||||||
|
'fedistream_local_follows_total',
|
||||||
|
'Total local artist follows',
|
||||||
|
array()
|
||||||
|
);
|
||||||
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||||
|
$total_follows = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$follows_table}" );
|
||||||
|
$follows_gauge->set( $total_follows, array() );
|
||||||
|
|
||||||
|
// Listening history entries.
|
||||||
|
$history_gauge = $collector->register_gauge(
|
||||||
|
'fedistream_listening_history_entries',
|
||||||
|
'Total listening history entries',
|
||||||
|
array()
|
||||||
|
);
|
||||||
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||||
|
$history_count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$history_table}" );
|
||||||
|
$history_gauge->set( $history_count, array() );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collect user metrics.
|
||||||
|
*
|
||||||
|
* @param object $collector The Prometheus collector.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function collect_user_metrics( $collector ): void {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$favorites_table = $wpdb->prefix . 'fedistream_favorites';
|
||||||
|
$follows_table = $wpdb->prefix . 'fedistream_user_follows';
|
||||||
|
$notifications_table = $wpdb->prefix . 'fedistream_notifications';
|
||||||
|
|
||||||
|
// Users with library (favorites).
|
||||||
|
$users_library_gauge = $collector->register_gauge(
|
||||||
|
'fedistream_users_with_library',
|
||||||
|
'Users who have favorites',
|
||||||
|
array()
|
||||||
|
);
|
||||||
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||||
|
$users_with_library = (int) $wpdb->get_var(
|
||||||
|
"SELECT COUNT(DISTINCT user_id) FROM {$favorites_table}"
|
||||||
|
);
|
||||||
|
$users_library_gauge->set( $users_with_library, array() );
|
||||||
|
|
||||||
|
// Users following artists.
|
||||||
|
$users_following_gauge = $collector->register_gauge(
|
||||||
|
'fedistream_users_following_artists',
|
||||||
|
'Users following at least one artist',
|
||||||
|
array()
|
||||||
|
);
|
||||||
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||||
|
$users_following = (int) $wpdb->get_var(
|
||||||
|
"SELECT COUNT(DISTINCT user_id) FROM {$follows_table}"
|
||||||
|
);
|
||||||
|
$users_following_gauge->set( $users_following, array() );
|
||||||
|
|
||||||
|
// Notifications by type and status.
|
||||||
|
$notifications_gauge = $collector->register_gauge(
|
||||||
|
'fedistream_notifications_total',
|
||||||
|
'Notifications by type and status',
|
||||||
|
array( 'type', 'status' )
|
||||||
|
);
|
||||||
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||||
|
$notifications = $wpdb->get_results(
|
||||||
|
"SELECT type, is_read, COUNT(*) as count FROM {$notifications_table} GROUP BY type, is_read"
|
||||||
|
);
|
||||||
|
foreach ( $notifications as $row ) {
|
||||||
|
$status = $row->is_read ? 'read' : 'unread';
|
||||||
|
$notifications_gauge->set( (int) $row->count, array( $row->type, $status ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pending notifications (simple gauge).
|
||||||
|
$pending_gauge = $collector->register_gauge(
|
||||||
|
'fedistream_notifications_pending',
|
||||||
|
'Total unread notifications',
|
||||||
|
array()
|
||||||
|
);
|
||||||
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||||
|
$pending = (int) $wpdb->get_var(
|
||||||
|
"SELECT COUNT(*) FROM {$notifications_table} WHERE is_read = 0"
|
||||||
|
);
|
||||||
|
$pending_gauge->set( $pending, array() );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collect WooCommerce metrics.
|
||||||
|
*
|
||||||
|
* @param object $collector The Prometheus collector.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function collect_woocommerce_metrics( $collector ): void {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$purchases_table = $wpdb->prefix . 'fedistream_purchases';
|
||||||
|
|
||||||
|
// Purchases by type.
|
||||||
|
$purchases_gauge = $collector->register_gauge(
|
||||||
|
'fedistream_purchases_total',
|
||||||
|
'Total purchases by content type',
|
||||||
|
array( 'type' )
|
||||||
|
);
|
||||||
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||||
|
$purchases = $wpdb->get_results(
|
||||||
|
"SELECT content_type, COUNT(*) as count FROM {$purchases_table} GROUP BY content_type"
|
||||||
|
);
|
||||||
|
foreach ( $purchases as $row ) {
|
||||||
|
$purchases_gauge->set( (int) $row->count, array( $row->content_type ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unique customers.
|
||||||
|
$customers_gauge = $collector->register_gauge(
|
||||||
|
'fedistream_customers_total',
|
||||||
|
'Unique customers with purchases',
|
||||||
|
array()
|
||||||
|
);
|
||||||
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||||
|
$customers = (int) $wpdb->get_var(
|
||||||
|
"SELECT COUNT(DISTINCT user_id) FROM {$purchases_table}"
|
||||||
|
);
|
||||||
|
$customers_gauge->set( $customers, array() );
|
||||||
|
|
||||||
|
// WooCommerce products count (FediStream types).
|
||||||
|
if ( function_exists( 'wc_get_products' ) ) {
|
||||||
|
$products_gauge = $collector->register_gauge(
|
||||||
|
'fedistream_products_total',
|
||||||
|
'FediStream products in WooCommerce',
|
||||||
|
array( 'type' )
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ( array( 'fedistream_album', 'fedistream_track' ) as $type ) {
|
||||||
|
$products = wc_get_products(
|
||||||
|
array(
|
||||||
|
'type' => $type,
|
||||||
|
'limit' => -1,
|
||||||
|
'return' => 'ids',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$products_gauge->set( count( $products ), array( $type ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collect ActivityPub metrics.
|
||||||
|
*
|
||||||
|
* @param object $collector The Prometheus collector.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function collect_activitypub_metrics( $collector ): void {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$followers_table = $wpdb->prefix . 'fedistream_followers';
|
||||||
|
$reactions_table = $wpdb->prefix . 'fedistream_reactions';
|
||||||
|
|
||||||
|
// Total ActivityPub followers.
|
||||||
|
$followers_gauge = $collector->register_gauge(
|
||||||
|
'fedistream_activitypub_followers_total',
|
||||||
|
'Total ActivityPub followers across all artists',
|
||||||
|
array()
|
||||||
|
);
|
||||||
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||||
|
$total_followers = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$followers_table}" );
|
||||||
|
$followers_gauge->set( $total_followers, array() );
|
||||||
|
|
||||||
|
// Followers by artist (top 10 to avoid cardinality explosion).
|
||||||
|
$followers_by_artist_gauge = $collector->register_gauge(
|
||||||
|
'fedistream_activitypub_followers_by_artist',
|
||||||
|
'Followers per artist',
|
||||||
|
array( 'artist_id', 'artist_name' )
|
||||||
|
);
|
||||||
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||||
|
$followers_by_artist = $wpdb->get_results(
|
||||||
|
"SELECT artist_id, COUNT(*) as count FROM {$followers_table} GROUP BY artist_id ORDER BY count DESC LIMIT 10"
|
||||||
|
);
|
||||||
|
foreach ( $followers_by_artist as $row ) {
|
||||||
|
$artist = get_post( $row->artist_id );
|
||||||
|
$artist_name = $artist ? $artist->post_title : 'Unknown';
|
||||||
|
$followers_by_artist_gauge->set( (int) $row->count, array( $row->artist_id, $artist_name ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reactions by type (if table exists).
|
||||||
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||||
|
$table_exists = $wpdb->get_var(
|
||||||
|
$wpdb->prepare( 'SHOW TABLES LIKE %s', $reactions_table )
|
||||||
|
);
|
||||||
|
|
||||||
|
if ( $table_exists ) {
|
||||||
|
$reactions_gauge = $collector->register_gauge(
|
||||||
|
'fedistream_activitypub_reactions_total',
|
||||||
|
'Fediverse reactions by type',
|
||||||
|
array( 'type' )
|
||||||
|
);
|
||||||
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||||
|
$reactions = $wpdb->get_results(
|
||||||
|
"SELECT reaction_type, COUNT(*) as count FROM {$reactions_table} GROUP BY reaction_type"
|
||||||
|
);
|
||||||
|
foreach ( $reactions as $row ) {
|
||||||
|
$reactions_gauge->set( (int) $row->count, array( $row->reaction_type ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if WooCommerce integration is enabled.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function is_woocommerce_enabled(): bool {
|
||||||
|
return get_option( 'wp_fedistream_enable_woocommerce', 0 )
|
||||||
|
&& class_exists( 'WooCommerce' );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if ActivityPub integration is enabled.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function is_activitypub_enabled(): bool {
|
||||||
|
return (bool) get_option( 'wp_fedistream_enable_activitypub', 1 );
|
||||||
|
}
|
||||||
|
}
|
||||||
1
includes/Prometheus/index.php
Normal file
1
includes/Prometheus/index.php
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<?php // Silence is golden.
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
* Plugin Name: WP FediStream
|
* Plugin Name: WP FediStream
|
||||||
* Plugin URI: https://src.bundespruefstelle.ch/magdev/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.
|
* Description: Stream music over ActivityPub - Build your own music streaming platform for Musicians and Labels.
|
||||||
* Version: 0.4.1
|
* Version: 0.5.1
|
||||||
* Requires at least: 6.4
|
* Requires at least: 6.4
|
||||||
* Requires PHP: 8.3
|
* Requires PHP: 8.3
|
||||||
* Author: Marco Graetsch
|
* Author: Marco Graetsch
|
||||||
@@ -26,7 +26,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
define( 'WP_FEDISTREAM_VERSION', '0.4.1' );
|
define( 'WP_FEDISTREAM_VERSION', '0.5.1' );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plugin file path.
|
* Plugin file path.
|
||||||
|
|||||||
Reference in New Issue
Block a user