diff --git a/workchronicles/workchronicles.php b/workchronicles/workchronicles.php index f15c88c..2e15165 100644 --- a/workchronicles/workchronicles.php +++ b/workchronicles/workchronicles.php @@ -12,50 +12,51 @@ use Shaarli\Plugin\PluginManager; function workchronicles_url(string $url): string { - echo "
DEBUG: Checking URL {$url}
"; + if (!empty($GLOBALS['WC_DEBUG'])) { echo "
DEBUG: Checking URL {$url}
"; } $pattern = '~^https?://(?:www\.)?(?:workchronicles\.com|workchronicles\.substack\.com)/\S+~i'; if (preg_match($pattern, $url, $m)) { - echo "
DEBUG: Matched workchronicles URL {$m[0]}
"; + if (!empty($GLOBALS['WC_DEBUG'])) { echo "
DEBUG: Matched workchronicles URL {$m[0]}
"; } return $m[0]; } - echo "
DEBUG: Not a workchronicles URL
"; + if (!empty($GLOBALS['WC_DEBUG'])) { echo "
DEBUG: Not a workchronicles URL
"; } return ''; } function hook_workchronicles_render_linklist(array $data): array { - echo "
DEBUG: hook_workchronicles_render_linklist start
"; + $GLOBALS['WC_DEBUG'] = !empty($data['is_admin']) || !empty($data['is_loggedin']); + if (!empty($GLOBALS['WC_DEBUG'])) { echo "
DEBUG: hook_workchronicles_render_linklist start
"; } if (empty($data['links']) || !is_array($data['links'])) { - echo "
DEBUG: Aucun lien à traiter
"; + if (!empty($GLOBALS['WC_DEBUG'])) { echo "
DEBUG: Aucun lien à traiter
"; } return $data; } foreach ($data['links'] as &$link) { $url = $link['url'] ?? ''; - echo "
DEBUG: Processing link {$url}
"; + if (!empty($GLOBALS['WC_DEBUG'])) { echo "
DEBUG: Processing link {$url}
"; } $page = workchronicles_url($url); if ('' === $page) { - echo "
DEBUG: Skipping non-workchronicles
"; + if (!empty($GLOBALS['WC_DEBUG'])) { echo "
DEBUG: Skipping non-workchronicles
"; } continue; } - echo "
DEBUG: Fetching page content from {$page}
"; + if (!empty($GLOBALS['WC_DEBUG'])) { echo "
DEBUG: Fetching page content from {$page}
"; } list($headers, $content) = get_http_response($page); if (empty($content)) { - echo "
DEBUG: Empty content
"; + if (!empty($GLOBALS['WC_DEBUG'])) { echo "
DEBUG: Empty content
"; } continue; } - echo "
DEBUG: Page content length: " . strlen($content) . "
"; + if (!empty($GLOBALS['WC_DEBUG'])) { echo "
DEBUG: Page content length: " . strlen($content) . "
"; } $imgUrl = ''; if (preg_match('/DEBUG: Found og:image {$imgUrl}"; + if (!empty($GLOBALS['WC_DEBUG'])) { echo "
DEBUG: Found og:image {$imgUrl}
"; } } if ('' === $imgUrl && preg_match('#]*>.*?]+src="([^"]+)"#is', $content, $m2)) { $imgUrl = $m2[1]; - echo "
DEBUG: Fallback found 
img src {$imgUrl}
"; + if (!empty($GLOBALS['WC_DEBUG'])) { echo "
DEBUG: Fallback found 
img src {$imgUrl}
"; } } if ('' !== $imgUrl) { @@ -68,25 +69,26 @@ function hook_workchronicles_render_linklist(array $data): array } else { $link['description'] = $html; } - echo "
DEBUG: Injected thumbnail into description
"; + if (!empty($GLOBALS['WC_DEBUG'])) { echo "
DEBUG: Injected thumbnail into description
"; } } else { - echo "
DEBUG: No image found (og:image nor 
img)
"; + if (!empty($GLOBALS['WC_DEBUG'])) { echo "
DEBUG: No image found (og:image nor 
img)
"; } } } unset($link); - echo "
DEBUG: hook_workchronicles_render_linklist end
"; + if (!empty($GLOBALS['WC_DEBUG'])) { echo "
DEBUG: hook_workchronicles_render_linklist end
"; } return $data; } function hook_workchronicles_render_includes(array $data): array { - echo "
DEBUG: hook_workchronicles_render_includes
"; + $GLOBALS['WC_DEBUG'] = !empty($data['is_admin']) || !empty($data['is_loggedin']); + if (!empty($GLOBALS['WC_DEBUG'])) { echo "
DEBUG: hook_workchronicles_render_includes
"; } $css = ''; $data['includes'][] = $css; - echo "
DEBUG: Injected CSS
"; + if (!empty($GLOBALS['WC_DEBUG'])) { echo "
DEBUG: Injected CSS
"; } return $data; }