From dea2a70bb0237b1769e3793d3d2527ec4d53d35a Mon Sep 17 00:00:00 2001 From: Varun Kumar Dubey Date: Tue, 24 Nov 2026 09:10:25 +0530 Subject: [PATCH 1/3] Build: zip folder matches the WordPress.org slug, one-copy guard, folder lock CI --- .github/workflows/wporg-folder-lock.yml | 34 + bin/build-release.sh | 23 +- bp-activity-link-preview.php | 2102 ++++++++++++----------- 3 files changed, 1132 insertions(+), 1027 deletions(-) create mode 100644 .github/workflows/wporg-folder-lock.yml diff --git a/.github/workflows/wporg-folder-lock.yml b/.github/workflows/wporg-folder-lock.yml new file mode 100644 index 0000000..a470d1d --- /dev/null +++ b/.github/workflows/wporg-folder-lock.yml @@ -0,0 +1,34 @@ +name: WordPress.org folder lock + +# The plugin zip folder must equal the WordPress.org slug, so the store, GitHub and +# WordPress.org copies install into the same folder. Two folder names for one plugin +# means two copies on a site and a fatal error. Identical file in every Wbcom .org plugin. + +on: + push: + pull_request: + +permissions: + contents: read + +jobs: + lock: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Zip folder equals the WordPress.org slug + run: | + slug=$(sed -nE 's/^slug="?([a-z0-9-]+)"?.*/\1/p' bin/build-release.sh | head -1) + main=$(grep -lE '^[[:space:]]*\*?[[:space:]]*Plugin Name:' ./*.php | head -1) + main=${main#./} + if [ -z "$slug" ] || [ -z "$main" ]; then + echo "::error::bin/build-release.sh needs a 'slug=' line and the repo root a main plugin file"; exit 1 + fi + code=$(curl -s -o /dev/null -w '%{http_code}' "https://plugins.svn.wordpress.org/$slug/trunk/$main") + if [ "$code" != 200 ]; then + echo "::error::slug=$slug is not the WordPress.org folder for $main (HTTP $code). Set slug= in bin/build-release.sh to the WordPress.org slug."; exit 1 + fi + if ! grep -q 'WordPress.org folder lock' bin/build-release.sh; then + echo "::error::bin/build-release.sh is missing the standard WordPress.org folder lock block"; exit 1 + fi + echo "OK: zip folder '$slug' matches WordPress.org ($main)" diff --git a/bin/build-release.sh b/bin/build-release.sh index 0c173f8..3489449 100755 --- a/bin/build-release.sh +++ b/bin/build-release.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# Build the customer zip: dist/buddypress-activity-link-preview-.zip +# Build the customer zip: dist/activity-link-preview-for-buddypress-.zip (folder = WordPress.org slug) # # Usage: bash bin/build-release.sh [--allow-dirty] # @@ -12,8 +12,10 @@ # Exit codes: 1 generic, 2 dirty tree, 3 version mismatch, 4 zip leaks dev files, 5 docs lint. set -euo pipefail -SLUG="buddypress-activity-link-preview" # zip folder name (matches the released zips) +slug=activity-link-preview-for-buddypress # WordPress.org slug = zip folder. Locked by CI (wporg-folder-lock.yml). +SLUG="$slug" MAIN="bp-activity-link-preview.php" +main="$MAIN" ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" DIST_DIR="${ROOT}/dist" STAGE_DIR="${DIST_DIR}/${SLUG}" @@ -66,8 +68,21 @@ ZIP_PATH="${DIST_DIR}/${SLUG}-${VERSION}.zip" rm -rf "$STAGE_DIR" "$ZIP_PATH" mkdir -p "$STAGE_DIR" rsync -a --exclude-from="${ROOT}/.distignore" ./ "${STAGE_DIR}/" -( cd "$DIST_DIR" && zip -rqX "$ZIP_PATH" "${SLUG}/" ) +# Deflate every entry: a PHP file stored uncompressed trips the store's upload firewall. +( cd "$DIST_DIR" && python3 -I -c 'import os,sys,zipfile +out,top=sys.argv[1],sys.argv[2] +with zipfile.ZipFile(out,"w",zipfile.ZIP_DEFLATED,compresslevel=9) as z: + for r,d,f in os.walk(top): + d.sort() + for n in sorted(f): z.write(os.path.join(r,n))' "$ZIP_PATH" "$SLUG" ) rm -rf "$STAGE_DIR" +zip="$ZIP_PATH" +# --- WordPress.org folder lock (Wbcom standard - do not edit) ---------------------- +top=$(unzip -Z1 "$zip" | head -1 | cut -d/ -f1) +[ "$top" = "$slug" ] || { echo "Zip folder '$top' is not the WordPress.org slug '$slug'"; exit 1; } +unzip -Z1 "$zip" | grep -qx "$slug/$main" || { echo "Main file $slug/$main missing from zip"; exit 1; } +[ "$(LC_ALL=C grep -a -c '&2 exit 4 fi -for required in "$MAIN" readme.txt "languages/${SLUG}.pot" assets/js/bp-activity-link-preview.js assets/css/bp-activity-link-preview.css; do +for required in "$MAIN" readme.txt languages/buddypress-activity-link-preview.pot assets/js/bp-activity-link-preview.js assets/css/bp-activity-link-preview.css; do printf '%s\n' "$LISTING" | grep -qx "${SLUG}/${required}" || { echo "FAIL: zip is missing ${required}" >&2; exit 4; } done diff --git a/bp-activity-link-preview.php b/bp-activity-link-preview.php index 1cf4e93..92f22b1 100644 --- a/bp-activity-link-preview.php +++ b/bp-activity-link-preview.php @@ -25,123 +25,139 @@ exit; } +// One copy only: stop if this plugin is already loaded from another folder. +if ( defined( 'ACTIVITY_LINK_PREVIEW_FOR_BUDDYPRESS_LOADED' ) ) { + return; +} +define( 'ACTIVITY_LINK_PREVIEW_FOR_BUDDYPRESS_LOADED', __FILE__ ); + define( 'BP_ACTIVITY_LINK_PREVIEW_VERSION', '1.7.7' ); define( 'BP_ACTIVITY_LINK_PREVIEW_URL', plugin_dir_url( __FILE__ ) ); define( 'BP_ACTIVITY_LINK_PREVIEW_PATH', plugin_dir_path( __FILE__ ) ); -/** - * Load the plugin text domain for translation. - * - * WordPress only auto-loads language packs that exist on - * translate.wordpress.org; it never reads a plugin's own languages/ folder. - * This plugin ships its own translations, so the domain must be registered - * here. load_plugin_textdomain() checks the WordPress.org pack first and falls - * back to the bundled file, so both paths work. - * - * Runs on init: loading a text domain earlier triggers - * _load_textdomain_just_in_time on WordPress 6.7+. - * - * @since 1.7.4 - * @return void - */ -function bp_activity_link_preview_load_textdomain() { - load_plugin_textdomain( - 'buddypress-activity-link-preview', - false, - basename( BP_ACTIVITY_LINK_PREVIEW_PATH ) . '/languages/' - ); +if ( ! function_exists( 'bp_activity_link_preview_load_textdomain' ) ) { + /** + * Load the plugin text domain for translation. + * + * WordPress only auto-loads language packs that exist on + * translate.wordpress.org; it never reads a plugin's own languages/ folder. + * This plugin ships its own translations, so the domain must be registered + * here. load_plugin_textdomain() checks the WordPress.org pack first and falls + * back to the bundled file, so both paths work. + * + * Runs on init: loading a text domain earlier triggers + * _load_textdomain_just_in_time on WordPress 6.7+. + * + * @since 1.7.4 + * @return void + */ + function bp_activity_link_preview_load_textdomain() { + load_plugin_textdomain( + 'buddypress-activity-link-preview', + false, + basename( BP_ACTIVITY_LINK_PREVIEW_PATH ) . '/languages/' + ); + } } add_action( 'init', 'bp_activity_link_preview_load_textdomain' ); -/** - * Check if BuddyPress or BuddyBoss Platform is active. - * - * @since 1.7.1 - * @return bool True if either BuddyPress or BuddyBoss is active. - */ -function bp_activity_link_preview_is_bp_active() { - // Check for BuddyPress. - if ( class_exists( 'BuddyPress' ) ) { - return true; - } - // Check for BuddyBoss Platform. - if ( defined( 'BP_PLATFORM_VERSION' ) || class_exists( 'BuddyBoss_Platform' ) ) { - return true; +if ( ! function_exists( 'bp_activity_link_preview_is_bp_active' ) ) { + /** + * Check if BuddyPress or BuddyBoss Platform is active. + * + * @since 1.7.1 + * @return bool True if either BuddyPress or BuddyBoss is active. + */ + function bp_activity_link_preview_is_bp_active() { + // Check for BuddyPress. + if ( class_exists( 'BuddyPress' ) ) { + return true; + } + // Check for BuddyBoss Platform. + if ( defined( 'BP_PLATFORM_VERSION' ) || class_exists( 'BuddyBoss_Platform' ) ) { + return true; + } + return false; } - return false; } -/** - * Display admin notice if BuddyPress/BuddyBoss is not active. - * - * @since 1.7.1 - */ -function bp_activity_link_preview_admin_notice() { - ?> -
-

- tag. */ - esc_html__( '%s requires BuddyPress or BuddyBoss Platform to be installed and active.', 'buddypress-activity-link-preview' ), - '' . esc_html__( 'Activity Link Preview For BuddyPress', 'buddypress-activity-link-preview' ) . '' - ); - ?> -

-
- +
+

+ tag. */ + esc_html__( '%s requires BuddyPress or BuddyBoss Platform to be installed and active.', 'buddypress-activity-link-preview' ), + '' . esc_html__( 'Activity Link Preview For BuddyPress', 'buddypress-activity-link-preview' ) . '' + ); + ?> +

+
+ post_content, 'activity-listing' ); - } - +if ( ! function_exists( 'bp_activity_link_preview_should_load_assets' ) ) { /** - * Filter whether the link-preview assets load on the current page. - * - * Assets load on BuddyPress activity screens, groups, and singular pages - * whose content holds the [activity-listing] shortcode. Use this to load - * them on other custom pages (widgets, page builders) or unload them. + * Determine whether the current page renders a BuddyPress activity stream + * and therefore needs the link-preview assets (own CSS/JS + Twitter/Facebook SDKs). * * @since 1.7.4 - * @param bool $should_load Whether the current page is an activity context. + * @return bool True when assets should be enqueued. */ - return (bool) apply_filters( 'bp_activity_link_preview_load_assets', $should_load ); -} + function bp_activity_link_preview_should_load_assets() { + $should_load = function_exists( 'bp_is_activity_component' ) + && ( bp_is_activity_component() || bp_is_activity_directory() || bp_is_user_activity() || bp_is_group() ); + + // A page that embeds the activity posting form through Shortcodes for + // BuddyPress ([activity-listing allow_posting=true]) is not a BP screen. + if ( ! $should_load && is_singular() ) { + $post = get_post(); + $should_load = $post instanceof WP_Post && has_shortcode( $post->post_content, 'activity-listing' ); + } -/** Bp_activity_link_preview_enqueue_scripts */ -function bp_activity_link_preview_enqueue_scripts() { - // Do not load plugin assets (or third-party SDKs) sitewide - activity contexts only. - if ( ! bp_activity_link_preview_should_load_assets() ) { - return; + /** + * Filter whether the link-preview assets load on the current page. + * + * Assets load on BuddyPress activity screens, groups, and singular pages + * whose content holds the [activity-listing] shortcode. Use this to load + * them on other custom pages (widgets, page builders) or unload them. + * + * @since 1.7.4 + * @param bool $should_load Whether the current page is an activity context. + */ + return (bool) apply_filters( 'bp_activity_link_preview_load_assets', $should_load ); } - - bp_activity_link_preview_enqueue_assets(); } -/** - * Load the composer assets wherever a BuddyPress activity post form renders. - * - * Forms embedded outside BuddyPress screens (BuddyPress Post From Anywhere's - * shortcode, block or widget, page builders) fire BuddyPress's own - * bp_before_activity_post_form hook, so the preview follows the form instead - * of a list of page types. The scripts print in the footer, so enqueueing - * while the page body renders is in time. - * - * @since 1.7.7 - */ -function bp_activity_link_preview_enqueue_for_post_form() { - /** This filter is documented in bp_activity_link_preview_should_load_assets(). */ - if ( wp_script_is( 'bp-activity-link-preview-js' ) || ! apply_filters( 'bp_activity_link_preview_load_assets', true ) ) { - return; - } +if ( ! function_exists( 'bp_activity_link_preview_enqueue_scripts' ) ) { + /** Bp_activity_link_preview_enqueue_scripts */ + function bp_activity_link_preview_enqueue_scripts() { + // Do not load plugin assets (or third-party SDKs) sitewide - activity contexts only. + if ( ! bp_activity_link_preview_should_load_assets() ) { + return; + } - bp_activity_link_preview_enqueue_assets(); + bp_activity_link_preview_enqueue_assets(); + } } -/** - * Enqueue the composer styles and scripts and their localized data. - * - * @since 1.7.7 - */ -function bp_activity_link_preview_enqueue_assets() { - // The composer JS renders dashicon markup (close/prev/next buttons); dashicons - // is not guaranteed on the frontend (only loads with the admin bar), so enqueue it. - wp_enqueue_style( 'dashicons' ); - wp_enqueue_style( 'bp-activity-link-preview-css', BP_ACTIVITY_LINK_PREVIEW_URL . 'assets/css/bp-activity-link-preview.css', array(), BP_ACTIVITY_LINK_PREVIEW_VERSION, 'all' ); - wp_enqueue_script( 'twitter-js', 'https://platform.twitter.com/widgets.js', array( 'jquery' ), BP_ACTIVITY_LINK_PREVIEW_VERSION, true ); - wp_enqueue_script( 'facebook-js', 'https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v21.0', array( 'jquery' ), BP_ACTIVITY_LINK_PREVIEW_VERSION, true ); - wp_enqueue_script( 'bp-activity-link-preview-js', BP_ACTIVITY_LINK_PREVIEW_URL . 'assets/js/bp-activity-link-preview.js', array( 'jquery' ), BP_ACTIVITY_LINK_PREVIEW_VERSION, true ); - - // Detect if BuddyBoss is active with its own link preview (the JS stands - // down for BuddyBoss's native preview to avoid duplicate cards). - $buddyboss_active = function_exists( 'buddyboss_theme' ) || class_exists( 'BuddyBoss_Platform' ); - $buddyboss_link_preview_active = $buddyboss_active && function_exists( 'bp_is_activity_link_preview_active' ) && bp_is_activity_link_preview_active(); - - // Only the keys the frontend script actually reads are localized. Youzify - // stand-down is handled server-side in the render path, so no Youzify flags - // are passed to JS. - // - // The i18n array is the ONLY translatable home for the composer UI strings: - // the preview card is built entirely in JS, so any literal that lives only - // in bp-activity-link-preview.js is invisible to the POT scanner and can - // never be translated. Every key here has a matching bpalpText() read in - // the script; the English literal there is a fallback only. Keep the two - // in lockstep - a key read but not seeded renders English on all locales. - wp_localize_script( - 'bp-activity-link-preview-js', - 'bp_activity_link_preview', - array( - 'ajaxurl' => admin_url( 'admin-ajax.php' ), - 'nonce' => wp_create_nonce( 'bp_activity_link_preview_nonce' ), - 'buddyboss_link_preview_active' => $buddyboss_link_preview_active, - 'i18n' => array( - 'cancelPreview' => __( 'Cancel Preview', 'buddypress-activity-link-preview' ), - 'cancelPreviewImage' => __( 'Cancel Preview Image', 'buddypress-activity-link-preview' ), - 'previousImage' => __( 'Previous image', 'buddypress-activity-link-preview' ), - 'nextImage' => __( 'Next image', 'buddypress-activity-link-preview' ), - /* translators: 1: current image number. 2: total number of images. */ - 'imageCount' => __( 'Image %1$s of %2$s', 'buddypress-activity-link-preview' ), - ), - ) - ); -} +if ( ! function_exists( 'bp_activity_link_preview_enqueue_for_post_form' ) ) { + /** + * Load the composer assets wherever a BuddyPress activity post form renders. + * + * Forms embedded outside BuddyPress screens (BuddyPress Post From Anywhere's + * shortcode, block or widget, page builders) fire BuddyPress's own + * bp_before_activity_post_form hook, so the preview follows the form instead + * of a list of page types. The scripts print in the footer, so enqueueing + * while the page body renders is in time. + * + * @since 1.7.7 + */ + function bp_activity_link_preview_enqueue_for_post_form() { + /** This filter is documented in bp_activity_link_preview_should_load_assets(). */ + if ( wp_script_is( 'bp-activity-link-preview-js' ) || ! apply_filters( 'bp_activity_link_preview_load_assets', true ) ) { + return; + } -/** - * Remove BuddyBoss Platform's link preview filter when our plugin is active. - * BuddyBoss adds bp_activity_link_preview at priority 20 which causes duplicate previews. - * - * @since 1.7.1 - */ -function bp_activity_link_preview_disable_buddyboss_preview() { - // Check if BuddyBoss Platform is active. - if ( defined( 'BP_PLATFORM_VERSION' ) && function_exists( 'bp_activity_link_preview' ) ) { - remove_filter( 'bp_get_activity_content_body', 'bp_activity_link_preview', 20 ); + bp_activity_link_preview_enqueue_assets(); } } -/** Bp_activity_parse_url_preview */ -function bp_activity_parse_url_preview() { - // Check if user is logged in first. - if ( ! is_user_logged_in() ) { - wp_send_json_error( array( 'message' => __( 'You must be logged in to perform this action.', 'buddypress-activity-link-preview' ) ) ); +if ( ! function_exists( 'bp_activity_link_preview_enqueue_assets' ) ) { + /** + * Enqueue the composer styles and scripts and their localized data. + * + * @since 1.7.7 + */ + function bp_activity_link_preview_enqueue_assets() { + // The composer JS renders dashicon markup (close/prev/next buttons); dashicons + // is not guaranteed on the frontend (only loads with the admin bar), so enqueue it. + wp_enqueue_style( 'dashicons' ); + wp_enqueue_style( 'bp-activity-link-preview-css', BP_ACTIVITY_LINK_PREVIEW_URL . 'assets/css/bp-activity-link-preview.css', array(), BP_ACTIVITY_LINK_PREVIEW_VERSION, 'all' ); + wp_enqueue_script( 'twitter-js', 'https://platform.twitter.com/widgets.js', array( 'jquery' ), BP_ACTIVITY_LINK_PREVIEW_VERSION, true ); + wp_enqueue_script( 'facebook-js', 'https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v21.0', array( 'jquery' ), BP_ACTIVITY_LINK_PREVIEW_VERSION, true ); + wp_enqueue_script( 'bp-activity-link-preview-js', BP_ACTIVITY_LINK_PREVIEW_URL . 'assets/js/bp-activity-link-preview.js', array( 'jquery' ), BP_ACTIVITY_LINK_PREVIEW_VERSION, true ); + + // Detect if BuddyBoss is active with its own link preview (the JS stands + // down for BuddyBoss's native preview to avoid duplicate cards). + $buddyboss_active = function_exists( 'buddyboss_theme' ) || class_exists( 'BuddyBoss_Platform' ); + $buddyboss_link_preview_active = $buddyboss_active && function_exists( 'bp_is_activity_link_preview_active' ) && bp_is_activity_link_preview_active(); + + // Only the keys the frontend script actually reads are localized. Youzify + // stand-down is handled server-side in the render path, so no Youzify flags + // are passed to JS. + // + // The i18n array is the ONLY translatable home for the composer UI strings: + // the preview card is built entirely in JS, so any literal that lives only + // in bp-activity-link-preview.js is invisible to the POT scanner and can + // never be translated. Every key here has a matching bpalpText() read in + // the script; the English literal there is a fallback only. Keep the two + // in lockstep - a key read but not seeded renders English on all locales. + wp_localize_script( + 'bp-activity-link-preview-js', + 'bp_activity_link_preview', + array( + 'ajaxurl' => admin_url( 'admin-ajax.php' ), + 'nonce' => wp_create_nonce( 'bp_activity_link_preview_nonce' ), + 'buddyboss_link_preview_active' => $buddyboss_link_preview_active, + 'i18n' => array( + 'cancelPreview' => __( 'Cancel Preview', 'buddypress-activity-link-preview' ), + 'cancelPreviewImage' => __( 'Cancel Preview Image', 'buddypress-activity-link-preview' ), + 'previousImage' => __( 'Previous image', 'buddypress-activity-link-preview' ), + 'nextImage' => __( 'Next image', 'buddypress-activity-link-preview' ), + /* translators: 1: current image number. 2: total number of images. */ + 'imageCount' => __( 'Image %1$s of %2$s', 'buddypress-activity-link-preview' ), + ), + ) + ); } +} - // Verify nonce - required for security. - $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; - if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'bp_activity_link_preview_nonce' ) ) { - wp_send_json_error( array( 'message' => __( 'Security check failed.', 'buddypress-activity-link-preview' ) ) ); +if ( ! function_exists( 'bp_activity_link_preview_disable_buddyboss_preview' ) ) { + /** + * Remove BuddyBoss Platform's link preview filter when our plugin is active. + * BuddyBoss adds bp_activity_link_preview at priority 20 which causes duplicate previews. + * + * @since 1.7.1 + */ + function bp_activity_link_preview_disable_buddyboss_preview() { + // Check if BuddyBoss Platform is active. + if ( defined( 'BP_PLATFORM_VERSION' ) && function_exists( 'bp_activity_link_preview' ) ) { + remove_filter( 'bp_get_activity_content_body', 'bp_activity_link_preview', 20 ); + } } +} - // Get URL and comment ID (if it's a comment). - $url = ! empty( $_POST['url'] ) ? filter_var( wp_unslash( $_POST['url'] ), FILTER_VALIDATE_URL ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- URL is validated with FILTER_VALIDATE_URL. - $comment_id = ! empty( $_POST['comment_id'] ) ? sanitize_text_field( wp_unslash( $_POST['comment_id'] ) ) : ''; +if ( ! function_exists( 'bp_activity_parse_url_preview' ) ) { + /** Bp_activity_parse_url_preview */ + function bp_activity_parse_url_preview() { + // Check if user is logged in first. + if ( ! is_user_logged_in() ) { + wp_send_json_error( array( 'message' => __( 'You must be logged in to perform this action.', 'buddypress-activity-link-preview' ) ) ); + } - // Check if URL is validated. - if ( ! filter_var( $url, FILTER_VALIDATE_URL ) ) { - wp_send_json( array( 'error' => __( 'The URL you entered is not valid.', 'buddypress-activity-link-preview' ) ) ); - } + // Verify nonce - required for security. + $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; + if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'bp_activity_link_preview_nonce' ) ) { + wp_send_json_error( array( 'message' => __( 'Security check failed.', 'buddypress-activity-link-preview' ) ) ); + } - // Parse URL to get host. - $parsed_url = wp_parse_url( $url ); - $host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : ''; + // Get URL and comment ID (if it's a comment). + $url = ! empty( $_POST['url'] ) ? filter_var( wp_unslash( $_POST['url'] ), FILTER_VALIDATE_URL ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- URL is validated with FILTER_VALIDATE_URL. + $comment_id = ! empty( $_POST['comment_id'] ) ? sanitize_text_field( wp_unslash( $_POST['comment_id'] ) ) : ''; - // Block requests to private/internal IP ranges and localhost. - if ( bp_activity_link_preview_is_blocked_host( $host ) ) { - wp_send_json( array( 'error' => __( 'This URL cannot be previewed for security reasons.', 'buddypress-activity-link-preview' ) ) ); - } + // Check if URL is validated. + if ( ! filter_var( $url, FILTER_VALIDATE_URL ) ) { + wp_send_json( array( 'error' => __( 'The URL you entered is not valid.', 'buddypress-activity-link-preview' ) ) ); + } - $parse_url_data = bp_activity_link_parse_url( $url ); + // Parse URL to get host. + $parsed_url = wp_parse_url( $url ); + $host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : ''; - // If empty data then send error. - if ( empty( $parse_url_data ) ) { - wp_send_json( array( 'error' => __( 'Sorry! Preview is not available right now. Please try again later.', 'buddypress-activity-link-preview' ) ) ); - } + // Block requests to private/internal IP ranges and localhost. + if ( bp_activity_link_preview_is_blocked_host( $host ) ) { + wp_send_json( array( 'error' => __( 'This URL cannot be previewed for security reasons.', 'buddypress-activity-link-preview' ) ) ); + } - // Add comment_id to response if it exists. - if ( ! empty( $comment_id ) ) { - $parse_url_data['comment_id'] = $comment_id; - } + $parse_url_data = bp_activity_link_parse_url( $url ); - // Apply filter to allow modification of parsed data. - $parse_url_data = apply_filters( 'bp_activity_parse_url_preview', $parse_url_data, $url ); + // If empty data then send error. + if ( empty( $parse_url_data ) ) { + wp_send_json( array( 'error' => __( 'Sorry! Preview is not available right now. Please try again later.', 'buddypress-activity-link-preview' ) ) ); + } - // send json success. - wp_send_json( $parse_url_data ); -} + // Add comment_id to response if it exists. + if ( ! empty( $comment_id ) ) { + $parse_url_data['comment_id'] = $comment_id; + } -/** - * Determine whether a host points at a private, loopback, or reserved address. - * - * Centralizes the SSRF host guard so it can be applied both to the original - * input URL in the AJAX handler and to any redirect-resolved URL during - * short-URL resolution. Returns true when the host should be BLOCKED. - * - * @since 1.7.4 - * @param string $host The host portion of a URL (no scheme/path). - * @return bool True if the host must be blocked for security reasons. - */ -function bp_activity_link_preview_is_blocked_host( $host ) { - if ( empty( $host ) ) { - return true; - } + // Apply filter to allow modification of parsed data. + $parse_url_data = apply_filters( 'bp_activity_parse_url_preview', $parse_url_data, $url ); - if ( filter_var( $host, FILTER_VALIDATE_IP ) && - ( false === filter_var( $host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) ) - ) { - return true; + // send json success. + wp_send_json( $parse_url_data ); } +} - if ( '127.0.0.1' === $host || 'localhost' === $host ) { - return true; - } +if ( ! function_exists( 'bp_activity_link_preview_is_blocked_host' ) ) { + /** + * Determine whether a host points at a private, loopback, or reserved address. + * + * Centralizes the SSRF host guard so it can be applied both to the original + * input URL in the AJAX handler and to any redirect-resolved URL during + * short-URL resolution. Returns true when the host should be BLOCKED. + * + * @since 1.7.4 + * @param string $host The host portion of a URL (no scheme/path). + * @return bool True if the host must be blocked for security reasons. + */ + function bp_activity_link_preview_is_blocked_host( $host ) { + if ( empty( $host ) ) { + return true; + } - // Single-label hosts ("https://www/", intranet names) are never a public site. - if ( false === strpos( $host, '.' ) && ! filter_var( $host, FILTER_VALIDATE_IP ) ) { - return true; - } + if ( filter_var( $host, FILTER_VALIDATE_IP ) && + ( false === filter_var( $host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) ) + ) { + return true; + } - if ( preg_match( '/^(10\.|172\.(1[6-9]|2[0-9]|3[0-1])\.|192\.168\.)/', $host ) ) { - return true; - } + if ( '127.0.0.1' === $host || 'localhost' === $host ) { + return true; + } - return false; -} + // Single-label hosts ("https://www/", intranet names) are never a public site. + if ( false === strpos( $host, '.' ) && ! filter_var( $host, FILTER_VALIDATE_IP ) ) { + return true; + } -/** - * Parse a URL and return preview data. - * - * @param string $url The URL to parse. - * @param bool $cached_only When true, never performs a remote HTTP request: - * only the transient cache and the internal-URL - * (local DB) fast path are consulted. Used by - * render-time code so page views never block on - * external fetches. - * @return array Parsed URL data (empty array when unavailable). - */ -function bp_activity_link_parse_url( $url, $cached_only = false ) { - $parse_url_data = wp_parse_url( $url, PHP_URL_HOST ); - $original_url = $url; + if ( preg_match( '/^(10\.|172\.(1[6-9]|2[0-9]|3[0-1])\.|192\.168\.)/', $host ) ) { + return true; + } - if ( ! $cached_only && in_array( $parse_url_data, apply_filters( 'bp_activity_link_parse_url_shorten_url_provider', array( 'bit.ly', 'snip.ly', 'rb.gy', 'tinyurl.com', 'tiny.one', 'rotf.lol', 'b.link', '4ubr.short.gy', '' ) ), true ) ) { - $response = wp_safe_remote_get( - $url, - array( - 'stream' => true, - 'headers' => array( - 'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:71.0) Gecko/20100101 Firefox/71.0', - ), - ), - ); + return false; + } +} - if ( ! is_wp_error( $response ) && ! empty( $response['http_response']->get_response_object()->url ) && $response['http_response']->get_response_object()->url !== $url ) { - $new_url = $response['http_response']->get_response_object()->url; - if ( filter_var( $new_url, FILTER_VALIDATE_URL ) ) { - $url = $new_url; - } - } +if ( ! function_exists( 'bp_activity_link_parse_url' ) ) { + /** + * Parse a URL and return preview data. + * + * @param string $url The URL to parse. + * @param bool $cached_only When true, never performs a remote HTTP request: + * only the transient cache and the internal-URL + * (local DB) fast path are consulted. Used by + * render-time code so page views never block on + * external fetches. + * @return array Parsed URL data (empty array when unavailable). + */ + function bp_activity_link_parse_url( $url, $cached_only = false ) { + $parse_url_data = wp_parse_url( $url, PHP_URL_HOST ); + $original_url = $url; - if ( $original_url === $url ) { - // Fallback: resolve the short URL through the WordPress HTTP API so the - // request honours WP_HTTP_BLOCK_EXTERNAL and the configured transports. - // Follow a single redirect and read the resolved Location. - $head_response = wp_safe_remote_head( + if ( ! $cached_only && in_array( $parse_url_data, apply_filters( 'bp_activity_link_parse_url_shorten_url_provider', array( 'bit.ly', 'snip.ly', 'rb.gy', 'tinyurl.com', 'tiny.one', 'rotf.lol', 'b.link', '4ubr.short.gy', '' ) ), true ) ) { + $response = wp_safe_remote_get( $url, array( - 'redirection' => 1, - 'timeout' => 5, - 'headers' => array( + 'stream' => true, + 'headers' => array( 'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:71.0) Gecko/20100101 Firefox/71.0', ), - ) + ), ); - if ( ! is_wp_error( $head_response ) ) { - $new_url = wp_remote_retrieve_header( $head_response, 'location' ); - - if ( ! empty( $new_url ) && filter_var( $new_url, FILTER_VALIDATE_URL ) ) { - // Re-validate the redirect-resolved host against the same SSRF - // guard used on the original input URL. A malicious or compromised - // short-URL provider could redirect to a private/loopback address; - // if so, abort resolution and keep the original URL. - $resolved_host = wp_parse_url( $new_url, PHP_URL_HOST ); - if ( ! bp_activity_link_preview_is_blocked_host( $resolved_host ) ) { - $url = $new_url; + if ( ! is_wp_error( $response ) && ! empty( $response['http_response']->get_response_object()->url ) && $response['http_response']->get_response_object()->url !== $url ) { + $new_url = $response['http_response']->get_response_object()->url; + if ( filter_var( $new_url, FILTER_VALIDATE_URL ) ) { + $url = $new_url; + } + } + + if ( $original_url === $url ) { + // Fallback: resolve the short URL through the WordPress HTTP API so the + // request honours WP_HTTP_BLOCK_EXTERNAL and the configured transports. + // Follow a single redirect and read the resolved Location. + $head_response = wp_safe_remote_head( + $url, + array( + 'redirection' => 1, + 'timeout' => 5, + 'headers' => array( + 'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:71.0) Gecko/20100101 Firefox/71.0', + ), + ) + ); + + if ( ! is_wp_error( $head_response ) ) { + $new_url = wp_remote_retrieve_header( $head_response, 'location' ); + + if ( ! empty( $new_url ) && filter_var( $new_url, FILTER_VALIDATE_URL ) ) { + // Re-validate the redirect-resolved host against the same SSRF + // guard used on the original input URL. A malicious or compromised + // short-URL provider could redirect to a private/loopback address; + // if so, abort resolution and keep the original URL. + $resolved_host = wp_parse_url( $new_url, PHP_URL_HOST ); + if ( ! bp_activity_link_preview_is_blocked_host( $resolved_host ) ) { + $url = $new_url; + } } } } } - } - $cache_key = 'bp_oembed_' . md5( maybe_serialize( $url ) ); - $parsed_url_data = get_transient( $cache_key ); + $cache_key = 'bp_oembed_' . md5( maybe_serialize( $url ) ); + $parsed_url_data = get_transient( $cache_key ); - // Negative cache hit: a recent parse failed - do not retry on every call. - if ( 'bpalp_failed' === $parsed_url_data ) { - return apply_filters( 'bp_activity_link_parse_url', array() ); - } + // Negative cache hit: a recent parse failed - do not retry on every call. + if ( 'bpalp_failed' === $parsed_url_data ) { + return apply_filters( 'bp_activity_link_parse_url', array() ); + } - if ( ! empty( $parsed_url_data ) ) { - return $parsed_url_data; - } + if ( ! empty( $parsed_url_data ) ) { + return $parsed_url_data; + } - $parsed_url_data = array(); + $parsed_url_data = array(); - if ( strstr( $url, site_url() ) && ( strstr( $url, 'download_document_file' ) || strstr( $url, 'download_media_file' ) || strstr( $url, 'download_video_file' ) ) ) { - return array(); - } + if ( strstr( $url, site_url() ) && ( strstr( $url, 'download_document_file' ) || strstr( $url, 'download_media_file' ) || strstr( $url, 'download_video_file' ) ) ) { + return array(); + } - if ( $cached_only ) { - // Internal URLs resolve from the local database (no HTTP), so they are - // still allowed in cached-only mode; anything else returns empty rather - // than fetching a remote page synchronously during render. - if ( bp_is_same_site_url( $url ) ) { - $internal_data = bp_activity_link_parse_internal_url( $url ); - if ( ! empty( $internal_data ) ) { - set_transient( $cache_key, $internal_data, DAY_IN_SECONDS ); - return apply_filters( 'bp_activity_link_parse_url', $internal_data ); + if ( $cached_only ) { + // Internal URLs resolve from the local database (no HTTP), so they are + // still allowed in cached-only mode; anything else returns empty rather + // than fetching a remote page synchronously during render. + if ( bp_is_same_site_url( $url ) ) { + $internal_data = bp_activity_link_parse_internal_url( $url ); + if ( ! empty( $internal_data ) ) { + set_transient( $cache_key, $internal_data, DAY_IN_SECONDS ); + return apply_filters( 'bp_activity_link_parse_url', $internal_data ); + } } + return apply_filters( 'bp_activity_link_parse_url', array() ); } - return apply_filters( 'bp_activity_link_parse_url', array() ); - } - - if ( ! function_exists( '_wp_oembed_get_object' ) ) { - require ABSPATH . WPINC . '/class-oembed.php'; - } - // Distinguishes a transient fetch failure (timeout, blocked, provider - // momentarily down) from a genuine empty result (a real page with nothing - // to preview). Only genuine empties are negative-cached; a transient - // failure must be retried on the next request rather than blackholing a - // good URL for the whole negative-cache window. See the write below. - $transient_failure = false; - - $embed_code = ''; - $oembed_obj = _wp_oembed_get_object(); - $discover = apply_filters( 'bp_oembed_discover_support', false, $url ); - $is_oembed = $oembed_obj->get_data( $url, array( 'discover' => $discover ) ); - - if ( $is_oembed ) { - $embed_code = wp_oembed_get( $url, array( 'discover' => $discover ) ); - - // The URL IS a known oEmbed provider (YouTube, Vimeo, Twitter...) but - // the embed came back empty. That is the provider being slow or - // unreachable this once, not a URL with nothing to show - so it must - // not be negative-cached. This is the exact YouTube case: one hiccup - // would otherwise fall through to a server-side scrape of youtube.com, - // which many hosts get a consent wall for, cache that as "empty", and - // leave YouTube previews dead for 15 minutes even though a retry works. - if ( empty( $embed_code ) ) { - $transient_failure = true; + if ( ! function_exists( '_wp_oembed_get_object' ) ) { + require ABSPATH . WPINC . '/class-oembed.php'; } - } - // Fetch the oembed code for URL. - if ( ! empty( $embed_code ) ) { - $parsed_url_data['title'] = ' '; - $parsed_url_data['description'] = $embed_code; - $parsed_url_data['images'] = ''; - $parsed_url_data['error'] = ''; - $parsed_url_data['wp_embed'] = true; - } else { - // Try to parse internal URLs directly without HTTP request (avoids self-request performance issues). - if ( bp_is_same_site_url( $url ) ) { - $internal_data = bp_activity_link_parse_internal_url( $url ); - if ( ! empty( $internal_data ) ) { - $parsed_url_data = $internal_data; - // Cache the result. $internal_data was already checked above, so no - // second emptiness test is needed here. - set_transient( $cache_key, $parsed_url_data, DAY_IN_SECONDS ); - return apply_filters( 'bp_activity_link_parse_url', $parsed_url_data ); + // Distinguishes a transient fetch failure (timeout, blocked, provider + // momentarily down) from a genuine empty result (a real page with nothing + // to preview). Only genuine empties are negative-cached; a transient + // failure must be retried on the next request rather than blackholing a + // good URL for the whole negative-cache window. See the write below. + $transient_failure = false; + + $embed_code = ''; + $oembed_obj = _wp_oembed_get_object(); + $discover = apply_filters( 'bp_oembed_discover_support', false, $url ); + $is_oembed = $oembed_obj->get_data( $url, array( 'discover' => $discover ) ); + + if ( $is_oembed ) { + $embed_code = wp_oembed_get( $url, array( 'discover' => $discover ) ); + + // The URL IS a known oEmbed provider (YouTube, Vimeo, Twitter...) but + // the embed came back empty. That is the provider being slow or + // unreachable this once, not a URL with nothing to show - so it must + // not be negative-cached. This is the exact YouTube case: one hiccup + // would otherwise fall through to a server-side scrape of youtube.com, + // which many hosts get a consent wall for, cache that as "empty", and + // leave YouTube previews dead for 15 minutes even though a retry works. + if ( empty( $embed_code ) ) { + $transient_failure = true; } } - $args = array( - 'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:71.0) Gecko/20100101 Firefox/71.0', - 'timeout' => 15, - // Previews only need the ; cap what a remote page can make us download. - 'limit_response_size' => MB_IN_BYTES, - ); - - if ( bp_is_same_site_url( $url ) ) { - // BuddyBoss Platform compatibility - add JWT token if available. - if ( function_exists( 'bp_enable_private_network' ) && function_exists( 'bb_create_jwt' ) ) { - if ( ! bp_enable_private_network() ) { - // Add the custom header with the JWT token. - $args['headers'] = array( - 'bb-preview-token' => bb_create_jwt( - array( - 'url' => $url, - 'iat' => time(), - 'exp' => time() + 120, // Token validity 2 minutes. - ) - ), - ); + // Fetch the oembed code for URL. + if ( ! empty( $embed_code ) ) { + $parsed_url_data['title'] = ' '; + $parsed_url_data['description'] = $embed_code; + $parsed_url_data['images'] = ''; + $parsed_url_data['error'] = ''; + $parsed_url_data['wp_embed'] = true; + } else { + // Try to parse internal URLs directly without HTTP request (avoids self-request performance issues). + if ( bp_is_same_site_url( $url ) ) { + $internal_data = bp_activity_link_parse_internal_url( $url ); + if ( ! empty( $internal_data ) ) { + $parsed_url_data = $internal_data; + // Cache the result. $internal_data was already checked above, so no + // second emptiness test is needed here. + set_transient( $cache_key, $parsed_url_data, DAY_IN_SECONDS ); + return apply_filters( 'bp_activity_link_parse_url', $parsed_url_data ); } } - $args['sslverify'] = false; - } - - // safely get URL and response body. - $response = wp_safe_remote_get( $url, $args ); - $body = wp_remote_retrieve_body( $response ); - // A WP_Error means the request itself never completed - timeout, DNS - // failure, connection refused, or WP_HTTP_BLOCK_EXTERNAL. That is - // transient (or config), not a page with nothing to preview, so it must - // be retried rather than negative-cached. - if ( is_wp_error( $response ) ) { - $transient_failure = true; - } + $args = array( + 'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:71.0) Gecko/20100101 Firefox/71.0', + 'timeout' => 15, + // Previews only need the ; cap what a remote page can make us download. + 'limit_response_size' => MB_IN_BYTES, + ); - // if response is not empty. - if ( ! is_wp_error( $body ) && ! empty( $body ) ) { - - // Load HTML to DOM Object. - $dom = new DOMDocument(); - // Suppress warnings for malformed HTML and handle encoding properly. - libxml_use_internal_errors( true ); - $dom->loadHTML( '' . $body, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); - libxml_clear_errors(); - - $meta_tags = array(); - $images = array(); - $description = ''; - $title = ''; - - $xpath = new DOMXPath( $dom ); - $query = '//*/meta[starts-with(@property, \'og:\')]'; - $metas_query = $xpath->query( $query ); - foreach ( $metas_query as $meta ) { - // DOMXPath::query() is typed as returning DOMNode, which has no - // getAttribute(). The expression selects meta elements, so this is - // always a DOMElement in practice - guard so a malformed document - // can never reach an undefined method. - if ( ! $meta instanceof DOMElement ) { - continue; + if ( bp_is_same_site_url( $url ) ) { + // BuddyBoss Platform compatibility - add JWT token if available. + if ( function_exists( 'bp_enable_private_network' ) && function_exists( 'bb_create_jwt' ) ) { + if ( ! bp_enable_private_network() ) { + // Add the custom header with the JWT token. + $args['headers'] = array( + 'bb-preview-token' => bb_create_jwt( + array( + 'url' => $url, + 'iat' => time(), + 'exp' => time() + 120, // Token validity 2 minutes. + ) + ), + ); + } } - $property = $meta->getAttribute( 'property' ); - $content = $meta->getAttribute( 'content' ); - $meta_tags[] = array( $property, $content ); + $args['sslverify'] = false; } - foreach ( $meta_tags as $tag ) { - if ( 'og:title' === $tag[0] ) { - $title = $tag[1]; - } - if ( 'og:description' === $tag[0] || 'description' === strtolower( $tag[0] ) ) { - // DOMDocument already decodes attribute entities once; decoding again - // would turn a double-encoded payload on a remote page into live markup. - $description = $tag[1]; + // safely get URL and response body. + $response = wp_safe_remote_get( $url, $args ); + $body = wp_remote_retrieve_body( $response ); + + // A WP_Error means the request itself never completed - timeout, DNS + // failure, connection refused, or WP_HTTP_BLOCK_EXTERNAL. That is + // transient (or config), not a page with nothing to preview, so it must + // be retried rather than negative-cached. + if ( is_wp_error( $response ) ) { + $transient_failure = true; + } + + // if response is not empty. + if ( ! is_wp_error( $body ) && ! empty( $body ) ) { + + // Load HTML to DOM Object. + $dom = new DOMDocument(); + // Suppress warnings for malformed HTML and handle encoding properly. + libxml_use_internal_errors( true ); + $dom->loadHTML( '' . $body, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); + libxml_clear_errors(); + + $meta_tags = array(); + $images = array(); + $description = ''; + $title = ''; + + $xpath = new DOMXPath( $dom ); + $query = '//*/meta[starts-with(@property, \'og:\')]'; + $metas_query = $xpath->query( $query ); + foreach ( $metas_query as $meta ) { + // DOMXPath::query() is typed as returning DOMNode, which has no + // getAttribute(). The expression selects meta elements, so this is + // always a DOMElement in practice - guard so a malformed document + // can never reach an undefined method. + if ( ! $meta instanceof DOMElement ) { + continue; + } + $property = $meta->getAttribute( 'property' ); + $content = $meta->getAttribute( 'content' ); + $meta_tags[] = array( $property, $content ); } - if ( 'og:image' === $tag[0] ) { - $images[] = $tag[1]; + + foreach ( $meta_tags as $tag ) { + if ( 'og:title' === $tag[0] ) { + $title = $tag[1]; + } + if ( 'og:description' === $tag[0] || 'description' === strtolower( $tag[0] ) ) { + // DOMDocument already decodes attribute entities once; decoding again + // would turn a double-encoded payload on a remote page into live markup. + $description = $tag[1]; + } + if ( 'og:image' === $tag[0] ) { + $images[] = $tag[1]; + } } - } - // Parse DOM to get Title. - if ( empty( $title ) ) { - // getElementsByTagName() always returns a DOMNodeList, so only its - // length is worth testing. - $nodes = $dom->getElementsByTagName( 'title' ); - $title = $nodes->length > 0 ? $nodes->item( 0 )->nodeValue : ''; - } + // Parse DOM to get Title. + if ( empty( $title ) ) { + // getElementsByTagName() always returns a DOMNodeList, so only its + // length is worth testing. + $nodes = $dom->getElementsByTagName( 'title' ); + $title = $nodes->length > 0 ? $nodes->item( 0 )->nodeValue : ''; + } - // Parse DOM to get Meta Description. - if ( empty( $description ) ) { - $metas = $dom->getElementsByTagName( 'meta' ); - for ( $i = 0; $i < $metas->length; $i++ ) { - $meta = $metas->item( $i ); - if ( 'description' === $meta->getAttribute( 'name' ) ) { - $description = $meta->getAttribute( 'content' ); - break; + // Parse DOM to get Meta Description. + if ( empty( $description ) ) { + $metas = $dom->getElementsByTagName( 'meta' ); + for ( $i = 0; $i < $metas->length; $i++ ) { + $meta = $metas->item( $i ); + if ( 'description' === $meta->getAttribute( 'name' ) ) { + $description = $meta->getAttribute( 'content' ); + break; + } } } - } - // Parse DOM to get Images. - $image_elements = $dom->getElementsByTagName( 'img' ); - for ( $i = 0; $i < $image_elements->length; $i++ ) { - $image = $image_elements->item( $i ); - $src = $image->getAttribute( 'src' ); + // Parse DOM to get Images. + $image_elements = $dom->getElementsByTagName( 'img' ); + for ( $i = 0; $i < $image_elements->length; $i++ ) { + $image = $image_elements->item( $i ); + $src = $image->getAttribute( 'src' ); - if ( filter_var( $src, FILTER_VALIDATE_URL ) ) { - $images[] = $src; + if ( filter_var( $src, FILTER_VALIDATE_URL ) ) { + $images[] = $src; + } } - } - if ( ! empty( $description ) && '' === trim( $title ) ) { - $title = $description; - } + if ( ! empty( $description ) && '' === trim( $title ) ) { + $title = $description; + } - if ( ! empty( $title ) && '' === trim( $description ) ) { - $description = $title; - } + if ( ! empty( $title ) && '' === trim( $description ) ) { + $description = $title; + } - if ( ! empty( $title ) ) { - $parsed_url_data['title'] = $title; - } + if ( ! empty( $title ) ) { + $parsed_url_data['title'] = $title; + } - if ( ! empty( $description ) ) { - $parsed_url_data['description'] = $description; - } + if ( ! empty( $description ) ) { + $parsed_url_data['description'] = $description; + } - if ( ! empty( $images ) ) { - $parsed_url_data['images'] = $images; - } + if ( ! empty( $images ) ) { + $parsed_url_data['images'] = $images; + } - if ( ! empty( $title ) || ! empty( $description ) || ! empty( $images ) ) { - $parsed_url_data['error'] = ''; + if ( ! empty( $title ) || ! empty( $description ) || ! empty( $images ) ) { + $parsed_url_data['error'] = ''; + } } } - } - if ( ! empty( $parsed_url_data ) ) { - // set the transient. - set_transient( $cache_key, $parsed_url_data, DAY_IN_SECONDS ); - } elseif ( ! $transient_failure ) { - // Negative cache a GENUINE empty result - a page that was fetched - // successfully but has nothing to preview - so it is not re-scraped on - // every request. A transient_failure (timeout, blocked, or an oEmbed - // provider that came back empty this once) is deliberately NOT cached: - // caching it would leave a good URL dead for the whole window with no - // feedback and no way to clear it, which is exactly the "YouTube - // previews stopped working" report. Those retry on the next view. - // - // The window is filterable so a site hitting genuinely slow pages can - // tune it without editing the plugin. - $negative_ttl = (int) apply_filters( 'bp_activity_link_preview_negative_cache_ttl', 15 * MINUTE_IN_SECONDS, $url ); - if ( $negative_ttl > 0 ) { - set_transient( $cache_key, 'bpalp_failed', $negative_ttl ); + if ( ! empty( $parsed_url_data ) ) { + // set the transient. + set_transient( $cache_key, $parsed_url_data, DAY_IN_SECONDS ); + } elseif ( ! $transient_failure ) { + // Negative cache a GENUINE empty result - a page that was fetched + // successfully but has nothing to preview - so it is not re-scraped on + // every request. A transient_failure (timeout, blocked, or an oEmbed + // provider that came back empty this once) is deliberately NOT cached: + // caching it would leave a good URL dead for the whole window with no + // feedback and no way to clear it, which is exactly the "YouTube + // previews stopped working" report. Those retry on the next view. + // + // The window is filterable so a site hitting genuinely slow pages can + // tune it without editing the plugin. + $negative_ttl = (int) apply_filters( 'bp_activity_link_preview_negative_cache_ttl', 15 * MINUTE_IN_SECONDS, $url ); + if ( $negative_ttl > 0 ) { + set_transient( $cache_key, 'bpalp_failed', $negative_ttl ); + } } - } - return apply_filters( 'bp_activity_link_parse_url', $parsed_url_data ); -} - -/** - * Check if the requested URL is from same site. - * - * @param string $url The URL to check. - * @return bool True if the URL is from the same site. - */ -function bp_is_same_site_url( $url ) { - $parsed_url = wp_parse_url( $url ); - $home_url = wp_parse_url( home_url( '/' ) ); - - if ( ! empty( $parsed_url['host'] ) && ! empty( $parsed_url['scheme'] ) ) { - return ( strtolower( $parsed_url['host'] ) === strtolower( $home_url['host'] ) ) && ( $parsed_url['scheme'] === $home_url['scheme'] ); + return apply_filters( 'bp_activity_link_parse_url', $parsed_url_data ); } - - return false; } -/** - * Parse internal BuddyPress/BuddyBoss URLs directly without HTTP requests. - * This avoids self-requests that cause performance issues on live sites. - * - * @param string $url The internal URL to parse. - * @return array|false Preview data array or false if not an internal BP page. - */ -function bp_activity_link_parse_internal_url( $url ) { - if ( ! bp_is_same_site_url( $url ) ) { - return false; - } +if ( ! function_exists( 'bp_is_same_site_url' ) ) { + /** + * Check if the requested URL is from same site. + * + * @param string $url The URL to check. + * @return bool True if the URL is from the same site. + */ + function bp_is_same_site_url( $url ) { + $parsed_url = wp_parse_url( $url ); + $home_url = wp_parse_url( home_url( '/' ) ); - $parsed_url = wp_parse_url( $url ); - $path = isset( $parsed_url['path'] ) ? trim( $parsed_url['path'], '/' ) : ''; + if ( ! empty( $parsed_url['host'] ) && ! empty( $parsed_url['scheme'] ) ) { + return ( strtolower( $parsed_url['host'] ) === strtolower( $home_url['host'] ) ) && ( $parsed_url['scheme'] === $home_url['scheme'] ); + } - if ( empty( $path ) ) { return false; } +} - // Remove the site path if it's in the URL (for subdirectory installs). - $site_path = trim( wp_parse_url( home_url( '/' ), PHP_URL_PATH ), '/' ); - if ( ! empty( $site_path ) && 0 === strpos( $path, $site_path ) ) { - $path = trim( substr( $path, strlen( $site_path ) ), '/' ); - } +if ( ! function_exists( 'bp_activity_link_parse_internal_url' ) ) { + /** + * Parse internal BuddyPress/BuddyBoss URLs directly without HTTP requests. + * This avoids self-requests that cause performance issues on live sites. + * + * @param string $url The internal URL to parse. + * @return array|false Preview data array or false if not an internal BP page. + */ + function bp_activity_link_parse_internal_url( $url ) { + if ( ! bp_is_same_site_url( $url ) ) { + return false; + } - $path_parts = explode( '/', $path ); + $parsed_url = wp_parse_url( $url ); + $path = isset( $parsed_url['path'] ) ? trim( $parsed_url['path'], '/' ) : ''; - // Check for BuddyPress member profile: /members/username/ or /user/username/. - // explode() always yields at least one element, so $path_parts[0] is guaranteed. - if ( in_array( $path_parts[0], array( 'members', 'user' ), true ) && ! empty( $path_parts[1] ) ) { - $username = sanitize_user( $path_parts[1] ); - $user = get_user_by( 'slug', $username ); + if ( empty( $path ) ) { + return false; + } - if ( ! $user && is_numeric( $username ) ) { - $user = get_user_by( 'id', $username ); + // Remove the site path if it's in the URL (for subdirectory installs). + $site_path = trim( wp_parse_url( home_url( '/' ), PHP_URL_PATH ), '/' ); + if ( ! empty( $site_path ) && 0 === strpos( $path, $site_path ) ) { + $path = trim( substr( $path, strlen( $site_path ) ), '/' ); } - if ( $user ) { - // Get user data. - $display_name = $user->display_name; + $path_parts = explode( '/', $path ); - // Try to get BuddyPress/XProfile data. - if ( function_exists( 'bp_get_profile_field_data' ) ) { - $args = array( - 'field' => 'About', - 'user_id' => $user->ID, - ); - $about = bp_get_profile_field_data( $args ); - if ( empty( $about ) ) { - $args['field'] = 'Description'; - $about = bp_get_profile_field_data( $args ); - } - if ( empty( $about ) ) { - $about = get_user_meta( $user->ID, 'description', true ); - } - } else { - $about = get_user_meta( $user->ID, 'description', true ); - } + // Check for BuddyPress member profile: /members/username/ or /user/username/. + // explode() always yields at least one element, so $path_parts[0] is guaranteed. + if ( in_array( $path_parts[0], array( 'members', 'user' ), true ) && ! empty( $path_parts[1] ) ) { + $username = sanitize_user( $path_parts[1] ); + $user = get_user_by( 'slug', $username ); - // Get user avatar URL. - $avatar_url = ''; - if ( function_exists( 'bp_core_fetch_avatar' ) ) { - $avatar_url = bp_core_fetch_avatar( - array( - 'item_id' => $user->ID, - 'object' => 'user', - 'type' => 'full', - 'html' => false, - ) - ); - } - if ( empty( $avatar_url ) ) { - $avatar_url = get_avatar_url( $user->ID, array( 'size' => 150 ) ); + if ( ! $user && is_numeric( $username ) ) { + $user = get_user_by( 'id', $username ); } - return array( - 'title' => $display_name, - /* translators: %s: member display name. */ - 'description' => ! empty( $about ) ? wp_trim_words( $about, 30, '...' ) : sprintf( __( 'View %s\'s profile', 'buddypress-activity-link-preview' ), $display_name ), - 'images' => ! empty( $avatar_url ) ? array( $avatar_url ) : array(), - 'error' => '', - ); - } - } - - // Check for BuddyPress groups: /groups/group-name/. - if ( 'groups' === $path_parts[0] && ! empty( $path_parts[1] ) ) { - $group_slug = sanitize_text_field( $path_parts[1] ); - - if ( function_exists( 'groups_get_groups' ) ) { - $groups = groups_get_groups( - array( - 'slug' => $group_slug, - 'per_page' => 1, - ) - ); + if ( $user ) { + // Get user data. + $display_name = $user->display_name; - if ( ! empty( $groups['groups'] ) ) { - $group = $groups['groups'][0]; + // Try to get BuddyPress/XProfile data. + if ( function_exists( 'bp_get_profile_field_data' ) ) { + $args = array( + 'field' => 'About', + 'user_id' => $user->ID, + ); + $about = bp_get_profile_field_data( $args ); + if ( empty( $about ) ) { + $args['field'] = 'Description'; + $about = bp_get_profile_field_data( $args ); + } + if ( empty( $about ) ) { + $about = get_user_meta( $user->ID, 'description', true ); + } + } else { + $about = get_user_meta( $user->ID, 'description', true ); + } - // Get group avatar. + // Get user avatar URL. $avatar_url = ''; if ( function_exists( 'bp_core_fetch_avatar' ) ) { $avatar_url = bp_core_fetch_avatar( array( - 'item_id' => $group->id, - 'object' => 'group', - 'type' => 'full', - 'html' => false, - 'avatar_dir' => 'group-avatars', + 'item_id' => $user->ID, + 'object' => 'user', + 'type' => 'full', + 'html' => false, ) ); } - - // Get group description. - $description = ''; - if ( ! empty( $group->description ) ) { - $description = wp_trim_words( $group->description, 30, '...' ); + if ( empty( $avatar_url ) ) { + $avatar_url = get_avatar_url( $user->ID, array( 'size' => 150 ) ); } return array( - 'title' => $group->name, - /* translators: %s: group name. */ - 'description' => ! empty( $description ) ? $description : sprintf( __( 'View the %s group', 'buddypress-activity-link-preview' ), $group->name ), + 'title' => $display_name, + /* translators: %s: member display name. */ + 'description' => ! empty( $about ) ? wp_trim_words( $about, 30, '...' ) : sprintf( __( 'View %s\'s profile', 'buddypress-activity-link-preview' ), $display_name ), 'images' => ! empty( $avatar_url ) ? array( $avatar_url ) : array(), 'error' => '', ); } } - } - return false; -} + // Check for BuddyPress groups: /groups/group-name/. + if ( 'groups' === $path_parts[0] && ! empty( $path_parts[1] ) ) { + $group_slug = sanitize_text_field( $path_parts[1] ); -/** - * Save link preview data into activity meta - * - * @param BP_Activity_Activity $activity Activity object. - */ -function bp_activity_link_preview_save_link_data( $activity ) { - // phpcs:disable WordPress.Security.NonceVerification.Missing -- Nonce verification is handled by BuddyPress before bp_activity_after_save hook is fired. - - // Handle main activity posts. - if ( isset( $_POST['link_url'] ) && isset( $_POST['link_title'] ) && isset( $_POST['link_description'] ) && isset( $_POST['link_image'] ) ) { - $link_url = ! empty( $_POST['link_url'] ) ? sanitize_text_field( wp_unslash( $_POST['link_url'] ) ) : ''; - $link_title = ! empty( $_POST['link_title'] ) ? sanitize_text_field( wp_unslash( $_POST['link_title'] ) ) : ''; - $link_description = ! empty( $_POST['link_description'] ) ? sanitize_text_field( wp_unslash( $_POST['link_description'] ) ) : ''; - $link_image = ! empty( $_POST['link_image'] ) ? sanitize_text_field( wp_unslash( $_POST['link_image'] ) ) : ''; - $link_wp_embed = ! empty( $_POST['link_wp_embed'] ); - $link_preview_data['url'] = $link_url; - - if ( false !== strpos( $link_preview_data['url'], 'www.reddit.com' ) ) { - return; - } + if ( function_exists( 'groups_get_groups' ) ) { + $groups = groups_get_groups( + array( + 'slug' => $group_slug, + 'per_page' => 1, + ) + ); - if ( ! empty( $link_image ) ) { - $link_preview_data['image_url'] = $link_image; - } - if ( ! empty( $link_title ) ) { - $link_preview_data['title'] = $link_title; - } - if ( ! empty( $link_description ) ) { - $link_preview_data['description'] = $link_description; - } + if ( ! empty( $groups['groups'] ) ) { + $group = $groups['groups'][0]; + + // Get group avatar. + $avatar_url = ''; + if ( function_exists( 'bp_core_fetch_avatar' ) ) { + $avatar_url = bp_core_fetch_avatar( + array( + 'item_id' => $group->id, + 'object' => 'group', + 'type' => 'full', + 'html' => false, + 'avatar_dir' => 'group-avatars', + ) + ); + } - // Persist oEmbed videos (YouTube, Vimeo, etc.). The composer flags these - // with link_wp_embed; the embed HTML is regenerated server-side from the - // URL (wp_oembed_get, whitelisted providers only) so the saved activity - // renders the player without a live external fetch at render time. - if ( $link_wp_embed && ! empty( $link_url ) ) { - $embed_html = wp_oembed_get( $link_url ); - if ( ! empty( $embed_html ) ) { - $link_preview_data['wp_embed'] = true; - $link_preview_data['embed_html'] = $embed_html; + // Get group description. + $description = ''; + if ( ! empty( $group->description ) ) { + $description = wp_trim_words( $group->description, 30, '...' ); + } + + return array( + 'title' => $group->name, + /* translators: %s: group name. */ + 'description' => ! empty( $description ) ? $description : sprintf( __( 'View the %s group', 'buddypress-activity-link-preview' ), $group->name ), + 'images' => ! empty( $avatar_url ) ? array( $avatar_url ) : array(), + 'error' => '', + ); + } } } - bp_activity_update_meta( $activity->id, '_bp_activity_link_preview_data', $link_preview_data ); + return false; } +} + +if ( ! function_exists( 'bp_activity_link_preview_save_link_data' ) ) { + /** + * Save link preview data into activity meta + * + * @param BP_Activity_Activity $activity Activity object. + */ + function bp_activity_link_preview_save_link_data( $activity ) { + // phpcs:disable WordPress.Security.NonceVerification.Missing -- Nonce verification is handled by BuddyPress before bp_activity_after_save hook is fired. + + // Handle main activity posts. + if ( isset( $_POST['link_url'] ) && isset( $_POST['link_title'] ) && isset( $_POST['link_description'] ) && isset( $_POST['link_image'] ) ) { + $link_url = ! empty( $_POST['link_url'] ) ? sanitize_text_field( wp_unslash( $_POST['link_url'] ) ) : ''; + $link_title = ! empty( $_POST['link_title'] ) ? sanitize_text_field( wp_unslash( $_POST['link_title'] ) ) : ''; + $link_description = ! empty( $_POST['link_description'] ) ? sanitize_text_field( wp_unslash( $_POST['link_description'] ) ) : ''; + $link_image = ! empty( $_POST['link_image'] ) ? sanitize_text_field( wp_unslash( $_POST['link_image'] ) ) : ''; + $link_wp_embed = ! empty( $_POST['link_wp_embed'] ); + $link_preview_data['url'] = $link_url; + + if ( false !== strpos( $link_preview_data['url'], 'www.reddit.com' ) ) { + return; + } - // Handle comment link previews (only if enabled). - if ( apply_filters( 'bp_activity_link_preview_enable_comments', true ) && 'activity_comment' === $activity->type && isset( $_POST['comment_link_url'] ) && isset( $_POST['comment_link_title'] ) && isset( $_POST['comment_link_description'] ) && isset( $_POST['comment_link_image'] ) ) { - $comment_link_url = ! empty( $_POST['comment_link_url'] ) ? sanitize_text_field( wp_unslash( $_POST['comment_link_url'] ) ) : ''; - $comment_link_title = ! empty( $_POST['comment_link_title'] ) ? sanitize_text_field( wp_unslash( $_POST['comment_link_title'] ) ) : ''; - $comment_link_description = ! empty( $_POST['comment_link_description'] ) ? sanitize_text_field( wp_unslash( $_POST['comment_link_description'] ) ) : ''; - $comment_link_image = ! empty( $_POST['comment_link_image'] ) ? sanitize_text_field( wp_unslash( $_POST['comment_link_image'] ) ) : ''; - $comment_link_wp_embed = ! empty( $_POST['comment_link_wp_embed'] ); + if ( ! empty( $link_image ) ) { + $link_preview_data['image_url'] = $link_image; + } + if ( ! empty( $link_title ) ) { + $link_preview_data['title'] = $link_title; + } + if ( ! empty( $link_description ) ) { + $link_preview_data['description'] = $link_description; + } - $comment_link_preview_data['url'] = $comment_link_url; + // Persist oEmbed videos (YouTube, Vimeo, etc.). The composer flags these + // with link_wp_embed; the embed HTML is regenerated server-side from the + // URL (wp_oembed_get, whitelisted providers only) so the saved activity + // renders the player without a live external fetch at render time. + if ( $link_wp_embed && ! empty( $link_url ) ) { + $embed_html = wp_oembed_get( $link_url ); + if ( ! empty( $embed_html ) ) { + $link_preview_data['wp_embed'] = true; + $link_preview_data['embed_html'] = $embed_html; + } + } - if ( false !== strpos( $comment_link_preview_data['url'], 'www.reddit.com' ) ) { - return; + bp_activity_update_meta( $activity->id, '_bp_activity_link_preview_data', $link_preview_data ); } - if ( ! empty( $comment_link_image ) ) { - $comment_link_preview_data['image_url'] = $comment_link_image; - } - if ( ! empty( $comment_link_title ) ) { - $comment_link_preview_data['title'] = $comment_link_title; - } - if ( ! empty( $comment_link_description ) ) { - $comment_link_preview_data['description'] = $comment_link_description; - } + // Handle comment link previews (only if enabled). + if ( apply_filters( 'bp_activity_link_preview_enable_comments', true ) && 'activity_comment' === $activity->type && isset( $_POST['comment_link_url'] ) && isset( $_POST['comment_link_title'] ) && isset( $_POST['comment_link_description'] ) && isset( $_POST['comment_link_image'] ) ) { + $comment_link_url = ! empty( $_POST['comment_link_url'] ) ? sanitize_text_field( wp_unslash( $_POST['comment_link_url'] ) ) : ''; + $comment_link_title = ! empty( $_POST['comment_link_title'] ) ? sanitize_text_field( wp_unslash( $_POST['comment_link_title'] ) ) : ''; + $comment_link_description = ! empty( $_POST['comment_link_description'] ) ? sanitize_text_field( wp_unslash( $_POST['comment_link_description'] ) ) : ''; + $comment_link_image = ! empty( $_POST['comment_link_image'] ) ? sanitize_text_field( wp_unslash( $_POST['comment_link_image'] ) ) : ''; + $comment_link_wp_embed = ! empty( $_POST['comment_link_wp_embed'] ); + + $comment_link_preview_data['url'] = $comment_link_url; - // Persist oEmbed videos in comments (see main-activity handling above). - if ( $comment_link_wp_embed && ! empty( $comment_link_url ) ) { - $comment_embed_html = wp_oembed_get( $comment_link_url ); - if ( ! empty( $comment_embed_html ) ) { - $comment_link_preview_data['wp_embed'] = true; - $comment_link_preview_data['embed_html'] = $comment_embed_html; + if ( false !== strpos( $comment_link_preview_data['url'], 'www.reddit.com' ) ) { + return; } - } - bp_activity_update_meta( $activity->id, '_bp_activity_comment_link_preview_data', $comment_link_preview_data ); - } elseif ( apply_filters( 'bp_activity_link_preview_enable_comments', true ) && 'activity_comment' === $activity->type && ! empty( $activity->content ) ) { - // Fallback: comment has no saved preview data but its content has URLs, so extract and save (only if enabled). - $urls = bp_activity_link_preview_extract_urls_from_content( $activity->content, false ); - if ( ! empty( $urls ) ) { - $url = $urls[0]; // Use first URL found. - $parsed_data = bp_activity_link_parse_url( $url ); - - // Check if it's a social media URL that uses native embeds (Twitter, Facebook). - $is_social_media = bp_activity_link_preview_is_social_media_url( $url ); - - // Save preview data if we have title/description OR if it's a social media URL. - if ( ! empty( $parsed_data ) && ( ! empty( $parsed_data['title'] ) || ! empty( $parsed_data['description'] ) || $is_social_media ) ) { - // Scraped remote content is untrusted - sanitize before storing in activity meta. - $comment_link_preview_data = array( - 'url' => esc_url_raw( $url ), - 'title' => ! empty( $parsed_data['title'] ) ? sanitize_text_field( wp_strip_all_tags( $parsed_data['title'] ) ) : '', - 'description' => ! empty( $parsed_data['description'] ) ? sanitize_text_field( wp_strip_all_tags( $parsed_data['description'] ) ) : '', - 'image_url' => ! empty( $parsed_data['images'] ) ? esc_url_raw( $parsed_data['images'][0] ) : '', - ); + if ( ! empty( $comment_link_image ) ) { + $comment_link_preview_data['image_url'] = $comment_link_image; + } + if ( ! empty( $comment_link_title ) ) { + $comment_link_preview_data['title'] = $comment_link_title; + } + if ( ! empty( $comment_link_description ) ) { + $comment_link_preview_data['description'] = $comment_link_description; + } - if ( false === strpos( $url, 'www.reddit.com' ) ) { - bp_activity_update_meta( $activity->id, '_bp_activity_comment_link_preview_data', $comment_link_preview_data ); + // Persist oEmbed videos in comments (see main-activity handling above). + if ( $comment_link_wp_embed && ! empty( $comment_link_url ) ) { + $comment_embed_html = wp_oembed_get( $comment_link_url ); + if ( ! empty( $comment_embed_html ) ) { + $comment_link_preview_data['wp_embed'] = true; + $comment_link_preview_data['embed_html'] = $comment_embed_html; } } - } - } - // phpcs:enable WordPress.Security.NonceVerification.Missing -} + bp_activity_update_meta( $activity->id, '_bp_activity_comment_link_preview_data', $comment_link_preview_data ); + } elseif ( apply_filters( 'bp_activity_link_preview_enable_comments', true ) && 'activity_comment' === $activity->type && ! empty( $activity->content ) ) { + // Fallback: comment has no saved preview data but its content has URLs, so extract and save (only if enabled). + $urls = bp_activity_link_preview_extract_urls_from_content( $activity->content, false ); + if ( ! empty( $urls ) ) { + $url = $urls[0]; // Use first URL found. + $parsed_data = bp_activity_link_parse_url( $url ); + + // Check if it's a social media URL that uses native embeds (Twitter, Facebook). + $is_social_media = bp_activity_link_preview_is_social_media_url( $url ); + + // Save preview data if we have title/description OR if it's a social media URL. + if ( ! empty( $parsed_data ) && ( ! empty( $parsed_data['title'] ) || ! empty( $parsed_data['description'] ) || $is_social_media ) ) { + // Scraped remote content is untrusted - sanitize before storing in activity meta. + $comment_link_preview_data = array( + 'url' => esc_url_raw( $url ), + 'title' => ! empty( $parsed_data['title'] ) ? sanitize_text_field( wp_strip_all_tags( $parsed_data['title'] ) ) : '', + 'description' => ! empty( $parsed_data['description'] ) ? sanitize_text_field( wp_strip_all_tags( $parsed_data['description'] ) ) : '', + 'image_url' => ! empty( $parsed_data['images'] ) ? esc_url_raw( $parsed_data['images'][0] ) : '', + ); -/** - * Extract URLs from content - * - * @param string $content The content to extract URLs from. - * @param bool $exclude_internal Whether to exclude same-site URLs (default: true). - * @return array Array of URLs found in content. - */ -function bp_activity_link_preview_extract_urls_from_content( $content, $exclude_internal = true ) { - // Strip HTML tags so that URLs only in href attributes (e.g. @mention profile links - // converted to ) are not matched — only URLs visible as plain text are extracted. - $text_content = wp_strip_all_tags( $content ); - - $pattern = '/https?:\/\/[^\s<>"]{2,}/i'; - // preg_match_all() always populates $matches[0], empty when there are no hits. - preg_match_all( $pattern, $text_content, $matches ); - $urls = $matches[0]; - - // Filter out same-site URLs (like @mention profile links) if requested. - if ( $exclude_internal && ! empty( $urls ) ) { - $urls = array_filter( - $urls, - function ( $url ) { - return ! bp_is_same_site_url( $url ); + if ( false === strpos( $url, 'www.reddit.com' ) ) { + bp_activity_update_meta( $activity->id, '_bp_activity_comment_link_preview_data', $comment_link_preview_data ); + } + } } - ); - $urls = array_values( $urls ); // Re-index array. - } + } - return $urls; + // phpcs:enable WordPress.Security.NonceVerification.Missing + } } -/** - * Check if URL is a social media URL that uses native embeds. - * These platforms block scraping but have native embed widgets. - * - * @param string $url The URL to check. - * @return bool True if URL is a social media embed URL. - */ -function bp_activity_link_preview_is_social_media_url( $url ) { - $social_domains = array( - 'x.com', - 'twitter.com', - 'facebook.com', - ); - - foreach ( $social_domains as $domain ) { - if ( false !== strpos( $url, $domain ) ) { - return true; +if ( ! function_exists( 'bp_activity_link_preview_extract_urls_from_content' ) ) { + /** + * Extract URLs from content + * + * @param string $content The content to extract URLs from. + * @param bool $exclude_internal Whether to exclude same-site URLs (default: true). + * @return array Array of URLs found in content. + */ + function bp_activity_link_preview_extract_urls_from_content( $content, $exclude_internal = true ) { + // Strip HTML tags so that URLs only in href attributes (e.g. @mention profile links + // converted to ) are not matched — only URLs visible as plain text are extracted. + $text_content = wp_strip_all_tags( $content ); + + $pattern = '/https?:\/\/[^\s<>"]{2,}/i'; + // preg_match_all() always populates $matches[0], empty when there are no hits. + preg_match_all( $pattern, $text_content, $matches ); + $urls = $matches[0]; + + // Filter out same-site URLs (like @mention profile links) if requested. + if ( $exclude_internal && ! empty( $urls ) ) { + $urls = array_filter( + $urls, + function ( $url ) { + return ! bp_is_same_site_url( $url ); + } + ); + $urls = array_values( $urls ); // Re-index array. } + + return $urls; } +} + +if ( ! function_exists( 'bp_activity_link_preview_is_social_media_url' ) ) { + /** + * Check if URL is a social media URL that uses native embeds. + * These platforms block scraping but have native embed widgets. + * + * @param string $url The URL to check. + * @return bool True if URL is a social media embed URL. + */ + function bp_activity_link_preview_is_social_media_url( $url ) { + $social_domains = array( + 'x.com', + 'twitter.com', + 'facebook.com', + ); + + foreach ( $social_domains as $domain ) { + if ( false !== strpos( $url, $domain ) ) { + return true; + } + } - return false; + return false; + } } -/** - * Handle ONLY main activities (exclude comments completely). - * - * @param string $content The activity content. - * @param BP_Activity_Activity $activity The activity object. - * @return string Modified content. - */ -function bp_activity_link_preview_content_body_with_comments( $content, $activity ) { +if ( ! function_exists( 'bp_activity_link_preview_content_body_with_comments' ) ) { + /** + * Handle ONLY main activities (exclude comments completely). + * + * @param string $content The activity content. + * @param BP_Activity_Activity $activity The activity object. + * @return string Modified content. + */ + function bp_activity_link_preview_content_body_with_comments( $content, $activity ) { - $activity_id = $activity->id; + $activity_id = $activity->id; - // Skip if BuddyBoss is active and has its own link preview feature. - // BuddyBoss renders link previews with class 'bb-activity-link-preview'. - if ( function_exists( 'buddyboss_theme' ) || class_exists( 'BuddyBoss_Platform' ) ) { - // Check if BuddyBoss link preview is enabled. - if ( function_exists( 'bp_is_activity_link_preview_active' ) && bp_is_activity_link_preview_active() ) { - return $content; + // Skip if BuddyBoss is active and has its own link preview feature. + // BuddyBoss renders link previews with class 'bb-activity-link-preview'. + if ( function_exists( 'buddyboss_theme' ) || class_exists( 'BuddyBoss_Platform' ) ) { + // Check if BuddyBoss link preview is enabled. + if ( function_exists( 'bp_is_activity_link_preview_active' ) && bp_is_activity_link_preview_active() ) { + return $content; + } + // Also check content for BuddyBoss preview markup. + if ( strpos( $content, 'bb-activity-link-preview' ) !== false || strpos( $content, 'bb-url-scrapper' ) !== false ) { + return $content; + } } - // Also check content for BuddyBoss preview markup. - if ( strpos( $content, 'bb-activity-link-preview' ) !== false || strpos( $content, 'bb-url-scrapper' ) !== false ) { - return $content; + + // Skip if Youzify is active and has its own URL preview feature. + // Youzify stores link preview in 'url_preview' meta and renders via its own templates. + if ( defined( 'YOUZIFY_VERSION' ) || class_exists( 'Youzify' ) ) { + // Check if Youzify URL preview is enabled. + if ( function_exists( 'youzify_option' ) && 'on' === youzify_option( 'youzify_enable_wall_url_preview', 'on' ) ) { + return $content; + } + // Also check for Youzify preview meta data. + $youzify_preview = bp_activity_get_meta( $activity_id, 'url_preview', true ); + if ( ! empty( $youzify_preview ) ) { + return $content; + } } - } - // Skip if Youzify is active and has its own URL preview feature. - // Youzify stores link preview in 'url_preview' meta and renders via its own templates. - if ( defined( 'YOUZIFY_VERSION' ) || class_exists( 'Youzify' ) ) { - // Check if Youzify URL preview is enabled. - if ( function_exists( 'youzify_option' ) && 'on' === youzify_option( 'youzify_enable_wall_url_preview', 'on' ) ) { + // ONLY process main activities, NOT comments. + if ( 'activity_comment' === $activity->type ) { return $content; } - // Also check for Youzify preview meta data. - $youzify_preview = bp_activity_get_meta( $activity_id, 'url_preview', true ); - if ( ! empty( $youzify_preview ) ) { + + // Skip content that already carries a preview. A per-request "seen this id" guard is wrong here: + // BuddyBoss filters the same activity twice on its single permalink and the second call lost the card. + if ( strpos( $content, 'activity-link-preview-container' ) !== false ) { return $content; } - } - // ONLY process main activities, NOT comments. - if ( 'activity_comment' === $activity->type ) { - return $content; - } + // Handle main activity link preview only. + $preview_data = bp_activity_get_meta( $activity_id, '_bp_activity_link_preview_data', true ); + if ( ! empty( $preview_data ) ) { + $content = bp_activity_link_preview_render_preview( $content, $preview_data ); + } - // Skip content that already carries a preview. A per-request "seen this id" guard is wrong here: - // BuddyBoss filters the same activity twice on its single permalink and the second call lost the card. - if ( strpos( $content, 'activity-link-preview-container' ) !== false ) { + // Return the content as filtered: it has already passed BuddyPress's kses at + // priority 1, and decoding entities here would turn a member's escaped + // markup (<img onerror=...>) back into live HTML. return $content; } - - // Handle main activity link preview only. - $preview_data = bp_activity_get_meta( $activity_id, '_bp_activity_link_preview_data', true ); - if ( ! empty( $preview_data ) ) { - $content = bp_activity_link_preview_render_preview( $content, $preview_data ); - } - - // Return the content as filtered: it has already passed BuddyPress's kses at - // priority 1, and decoding entities here would turn a member's escaped - // markup (<img onerror=...>) back into live HTML. - return $content; } -/** - * Add comment-specific filter to ensure comments get processed. - * - * @param string $content The comment content. - * @return string Modified content. - */ -function bp_activity_link_preview_comment_content( $content ) { - // Check if comment link previews are enabled. - if ( ! apply_filters( 'bp_activity_link_preview_enable_comments', true ) ) { - return $content; - } +if ( ! function_exists( 'bp_activity_link_preview_comment_content' ) ) { + /** + * Add comment-specific filter to ensure comments get processed. + * + * @param string $content The comment content. + * @return string Modified content. + */ + function bp_activity_link_preview_comment_content( $content ) { + // Check if comment link previews are enabled. + if ( ! apply_filters( 'bp_activity_link_preview_enable_comments', true ) ) { + return $content; + } - global $activities_template; + global $activities_template; - // Make sure we have an activity object. - if ( empty( $activities_template->activity ) ) { - return $content; - } + // Make sure we have an activity object. + if ( empty( $activities_template->activity ) ) { + return $content; + } - $activity = $activities_template->activity; - $activity_id = $activity->current_comment->id; + $activity = $activities_template->activity; + $activity_id = $activity->current_comment->id; - // Check if content already contains a preview to avoid double processing. - if ( strpos( $content, 'activity-comment-link-preview-container' ) !== false ) { - return $content; - } + // Check if content already contains a preview to avoid double processing. + if ( strpos( $content, 'activity-comment-link-preview-container' ) !== false ) { + return $content; + } - // Check if we already have preview data. - $comment_preview_data = bp_activity_get_meta( $activity_id, '_bp_activity_comment_link_preview_data', true ); - - if ( ! empty( $comment_preview_data ) ) { - $content = bp_activity_link_preview_render_preview( $content, $comment_preview_data, true ); - } elseif ( ! empty( $content ) ) { - // No saved preview data but the content has URLs: generate from cache only. - // Render must never block on a live external fetch; save-time extraction - // (bp_activity_link_preview_save_link_data) is responsible for fetching. - $urls = bp_activity_link_preview_extract_urls_from_content( $content, false ); - if ( ! empty( $urls ) ) { - $url = $urls[0]; - $parsed_data = bp_activity_link_parse_url( $url, true ); - - // Check if it's a social media URL that uses native embeds. - $is_social_media = bp_activity_link_preview_is_social_media_url( $url ); - - // Generate preview if we have title/description OR if it's a social media URL. - if ( ! empty( $parsed_data ) && ( ! empty( $parsed_data['title'] ) || ! empty( $parsed_data['description'] ) || $is_social_media ) ) { - // Scraped remote content is untrusted - sanitize before storing in activity meta. - $comment_link_preview_data = array( - 'url' => esc_url_raw( $url ), - 'title' => ! empty( $parsed_data['title'] ) ? sanitize_text_field( wp_strip_all_tags( $parsed_data['title'] ) ) : '', - 'description' => ! empty( $parsed_data['description'] ) ? sanitize_text_field( wp_strip_all_tags( $parsed_data['description'] ) ) : '', - 'image_url' => ! empty( $parsed_data['images'] ) ? esc_url_raw( $parsed_data['images'][0] ) : '', - ); + // Check if we already have preview data. + $comment_preview_data = bp_activity_get_meta( $activity_id, '_bp_activity_comment_link_preview_data', true ); + + if ( ! empty( $comment_preview_data ) ) { + $content = bp_activity_link_preview_render_preview( $content, $comment_preview_data, true ); + } elseif ( ! empty( $content ) ) { + // No saved preview data but the content has URLs: generate from cache only. + // Render must never block on a live external fetch; save-time extraction + // (bp_activity_link_preview_save_link_data) is responsible for fetching. + $urls = bp_activity_link_preview_extract_urls_from_content( $content, false ); + if ( ! empty( $urls ) ) { + $url = $urls[0]; + $parsed_data = bp_activity_link_parse_url( $url, true ); + + // Check if it's a social media URL that uses native embeds. + $is_social_media = bp_activity_link_preview_is_social_media_url( $url ); + + // Generate preview if we have title/description OR if it's a social media URL. + if ( ! empty( $parsed_data ) && ( ! empty( $parsed_data['title'] ) || ! empty( $parsed_data['description'] ) || $is_social_media ) ) { + // Scraped remote content is untrusted - sanitize before storing in activity meta. + $comment_link_preview_data = array( + 'url' => esc_url_raw( $url ), + 'title' => ! empty( $parsed_data['title'] ) ? sanitize_text_field( wp_strip_all_tags( $parsed_data['title'] ) ) : '', + 'description' => ! empty( $parsed_data['description'] ) ? sanitize_text_field( wp_strip_all_tags( $parsed_data['description'] ) ) : '', + 'image_url' => ! empty( $parsed_data['images'] ) ? esc_url_raw( $parsed_data['images'][0] ) : '', + ); - if ( false === strpos( $url, 'www.reddit.com' ) ) { - // Save for future use and render. - bp_activity_update_meta( $activity_id, '_bp_activity_comment_link_preview_data', $comment_link_preview_data ); - $content = bp_activity_link_preview_render_preview( $content, $comment_link_preview_data, true ); + if ( false === strpos( $url, 'www.reddit.com' ) ) { + // Save for future use and render. + bp_activity_update_meta( $activity_id, '_bp_activity_comment_link_preview_data', $comment_link_preview_data ); + $content = bp_activity_link_preview_render_preview( $content, $comment_link_preview_data, true ); + } } } } - } - return $content; + return $content; + } } -/** - * Register comment content filter after plugins are loaded. - * This allows other plugins to disable comment previews via filter. - */ -function bp_activity_link_preview_init_comment_filter() { - if ( apply_filters( 'bp_activity_link_preview_enable_comments', true ) ) { - add_filter( 'bp_activity_comment_content', 'bp_activity_link_preview_comment_content' ); +if ( ! function_exists( 'bp_activity_link_preview_init_comment_filter' ) ) { + /** + * Register comment content filter after plugins are loaded. + * This allows other plugins to disable comment previews via filter. + */ + function bp_activity_link_preview_init_comment_filter() { + if ( apply_filters( 'bp_activity_link_preview_enable_comments', true ) ) { + add_filter( 'bp_activity_comment_content', 'bp_activity_link_preview_comment_content' ); + } } } -/** - * Helper function to render preview. - * - * @param string $content The activity content. - * @param array $preview_data The preview data array. - * @param bool $is_comment Whether this is a comment preview. - * @return string Modified content with preview appended. - */ -function bp_activity_link_preview_render_preview( $content, $preview_data, $is_comment = false ) { - $preview_data = bp_parse_args( - $preview_data, - array( - 'title' => '', - 'description' => '', - ) - ); - - if ( empty( $preview_data['url'] ) ) { - return $content; - } +if ( ! function_exists( 'bp_activity_link_preview_render_preview' ) ) { + /** + * Helper function to render preview. + * + * @param string $content The activity content. + * @param array $preview_data The preview data array. + * @param bool $is_comment Whether this is a comment preview. + * @return string Modified content with preview appended. + */ + function bp_activity_link_preview_render_preview( $content, $preview_data, $is_comment = false ) { + $preview_data = bp_parse_args( + $preview_data, + array( + 'title' => '', + 'description' => '', + ) + ); - $css_class = $is_comment ? 'activity-comment-link-preview-container' : 'activity-link-preview-container'; + if ( empty( $preview_data['url'] ) ) { + return $content; + } - // Social media embeds use native widgets, so bypass the title/description check. - if ( true === str_contains( $preview_data['url'], 'x.com' ) || true === str_contains( $preview_data['url'], 'twitter.com' ) ) { - $content .= '
'; - return $content; - } elseif ( true === str_contains( $preview_data['url'], 'facebook.com' ) ) { - $content .= '
'; - return $content; - } + $css_class = $is_comment ? 'activity-comment-link-preview-container' : 'activity-link-preview-container'; - // oEmbed videos (YouTube, Vimeo, etc.): output the embed HTML that was - // generated by wp_oembed_get() and persisted at save time. It is filtered - // through bp_activity_allowed_tags (which allows iframe) on display, so no - // live external fetch happens at render time. - if ( ! empty( $preview_data['wp_embed'] ) && ! empty( $preview_data['embed_html'] ) ) { - $content .= '
' . $preview_data['embed_html'] . '
'; - return $content; - } + // Social media embeds use native widgets, so bypass the title/description check. + if ( true === str_contains( $preview_data['url'], 'x.com' ) || true === str_contains( $preview_data['url'], 'twitter.com' ) ) { + $content .= '
'; + return $content; + } elseif ( true === str_contains( $preview_data['url'], 'facebook.com' ) ) { + $content .= '
'; + return $content; + } - // For regular URLs, require title or description for the preview. - if ( empty( trim( $preview_data['title'] ) ) && empty( trim( $preview_data['description'] ) ) ) { - return $content; - } + // oEmbed videos (YouTube, Vimeo, etc.): output the embed HTML that was + // generated by wp_oembed_get() and persisted at save time. It is filtered + // through bp_activity_allowed_tags (which allows iframe) on display, so no + // live external fetch happens at render time. + if ( ! empty( $preview_data['wp_embed'] ) && ! empty( $preview_data['embed_html'] ) ) { + $content .= '
' . $preview_data['embed_html'] . '
'; + return $content; + } - // Regular link preview. Escape the stored description before output; the - // "Read more" anchor is appended afterwards so it stays intact. - $description = esc_html( wp_strip_all_tags( $preview_data['description'] ) ); - $read_more = ' …
' . esc_html__( 'Read more', 'buddypress-activity-link-preview' ) . ''; - $description = wp_trim_words( $description, 40, $read_more ); + // For regular URLs, require title or description for the preview. + if ( empty( trim( $preview_data['title'] ) ) && empty( trim( $preview_data['description'] ) ) ) { + return $content; + } - $content = make_clickable( $content ); + // Regular link preview. Escape the stored description before output; the + // "Read more" anchor is appended afterwards so it stays intact. + $description = esc_html( wp_strip_all_tags( $preview_data['description'] ) ); + $read_more = ' … ' . esc_html__( 'Read more', 'buddypress-activity-link-preview' ) . ''; + $description = wp_trim_words( $description, 40, $read_more ); - $content .= '
'; - $content .= ''; - if ( ! empty( $preview_data['image_url'] ) ) { - $content .= ''; - return $content; + return $content; + } } -/** - * Add allowed HTML tags for link previews in BuddyPress activity content. - * - * @param array $tags Allowed HTML tags. - * @return array Modified allowed tags. - */ -function bp_activity_link_preview_allowed_tags( $tags ) { - $tags['div'] = array( - 'class' => array(), - 'id' => array(), - 'style' => array(), - 'data-url' => array(), - 'data-href' => array(), - 'data-width' => array(), - 'data-height' => array(), - ); - - $tags['img'] = array( - 'src' => array(), - 'alt' => array(), - 'width' => array(), - 'height' => array(), - 'class' => array(), - 'id' => array(), - ); - - $tags['button'] = array( - 'type' => array(), - 'id' => array(), - 'class' => array(), - ); - - $tags['span'] = array( - 'class' => array(), - 'id' => array(), - ); - - $tags['i'] = array( - 'class' => array(), - 'id' => array(), - ); - - // Allow oEmbed video iframes (YouTube, Vimeo, etc.). The iframe markup is - // produced server-side by wp_oembed_get() for whitelisted providers only, - // so it is trusted; this allowlist scopes which attributes survive display. - $tags['iframe'] = array( - 'src' => array(), - 'width' => array(), - 'height' => array(), - 'title' => array(), - 'class' => array(), - 'style' => array(), - 'frameborder' => array(), - 'allow' => array(), - 'allowfullscreen' => array(), - 'loading' => array(), - 'referrerpolicy' => array(), - ); - - return $tags; -} +if ( ! function_exists( 'bp_activity_link_preview_allowed_tags' ) ) { + /** + * Add allowed HTML tags for link previews in BuddyPress activity content. + * + * @param array $tags Allowed HTML tags. + * @return array Modified allowed tags. + */ + function bp_activity_link_preview_allowed_tags( $tags ) { + $tags['div'] = array( + 'class' => array(), + 'id' => array(), + 'style' => array(), + 'data-url' => array(), + 'data-href' => array(), + 'data-width' => array(), + 'data-height' => array(), + ); -/** - * Embed bp activity link preview data in rest api activity endpoint. - * - * @param WP_REST_Response $response The response object. - * @param WP_REST_Request $request The request object. - * @param BP_Activity_Activity $activity The activity object. - * @return WP_REST_Response Modified response. - */ -function bp_activity_link_preview_data_embed_rest_api( $response, $request, $activity ) { - $bp_activity_link_data = bp_activity_get_meta( $activity->id, '_bp_activity_link_preview_data', true ); - $response->data['bp_activity_link'] = $bp_activity_link_data; - - // Add comment link preview data if it's a comment. - if ( 'activity_comment' === $activity->type ) { - $bp_activity_comment_link_data = bp_activity_get_meta( $activity->id, '_bp_activity_comment_link_preview_data', true ); - $response->data['bp_activity_comment_link'] = $bp_activity_comment_link_data; + $tags['img'] = array( + 'src' => array(), + 'alt' => array(), + 'width' => array(), + 'height' => array(), + 'class' => array(), + 'id' => array(), + ); + + $tags['button'] = array( + 'type' => array(), + 'id' => array(), + 'class' => array(), + ); + + $tags['span'] = array( + 'class' => array(), + 'id' => array(), + ); + + $tags['i'] = array( + 'class' => array(), + 'id' => array(), + ); + + // Allow oEmbed video iframes (YouTube, Vimeo, etc.). The iframe markup is + // produced server-side by wp_oembed_get() for whitelisted providers only, + // so it is trusted; this allowlist scopes which attributes survive display. + $tags['iframe'] = array( + 'src' => array(), + 'width' => array(), + 'height' => array(), + 'title' => array(), + 'class' => array(), + 'style' => array(), + 'frameborder' => array(), + 'allow' => array(), + 'allowfullscreen' => array(), + 'loading' => array(), + 'referrerpolicy' => array(), + ); + + return $tags; } +} + +if ( ! function_exists( 'bp_activity_link_preview_data_embed_rest_api' ) ) { + /** + * Embed bp activity link preview data in rest api activity endpoint. + * + * @param WP_REST_Response $response The response object. + * @param WP_REST_Request $request The request object. + * @param BP_Activity_Activity $activity The activity object. + * @return WP_REST_Response Modified response. + */ + function bp_activity_link_preview_data_embed_rest_api( $response, $request, $activity ) { + $bp_activity_link_data = bp_activity_get_meta( $activity->id, '_bp_activity_link_preview_data', true ); + $response->data['bp_activity_link'] = $bp_activity_link_data; + + // Add comment link preview data if it's a comment. + if ( 'activity_comment' === $activity->type ) { + $bp_activity_comment_link_data = bp_activity_get_meta( $activity->id, '_bp_activity_comment_link_preview_data', true ); + $response->data['bp_activity_comment_link'] = $bp_activity_comment_link_data; + } - return $response; + return $response; + } } -/** - * Outputs a Facebook root div element in specific BuddyPress contexts. - */ -function bp_activity_link_preview_add_facebook_root_div() { - if ( bp_activity_link_preview_should_load_assets() ) { - echo '
'; +if ( ! function_exists( 'bp_activity_link_preview_add_facebook_root_div' ) ) { + /** + * Outputs a Facebook root div element in specific BuddyPress contexts. + */ + function bp_activity_link_preview_add_facebook_root_div() { + if ( bp_activity_link_preview_should_load_assets() ) { + echo '
'; + } } } From 979bba096adfa8377b61950b4a811a54175ed4ef Mon Sep 17 00:00:00 2001 From: Varun Kumar Dubey Date: Tue, 24 Nov 2026 09:17:40 +0530 Subject: [PATCH 2/3] Build: run the folder lock block without pipefail (unzip pipes can exit 141 on SIGPIPE) --- bin/build-release.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bin/build-release.sh b/bin/build-release.sh index 3489449..4eb2830 100755 --- a/bin/build-release.sh +++ b/bin/build-release.sh @@ -77,12 +77,15 @@ with zipfile.ZipFile(out,"w",zipfile.ZIP_DEFLATED,compresslevel=9) as z: for n in sorted(f): z.write(os.path.join(r,n))' "$ZIP_PATH" "$SLUG" ) rm -rf "$STAGE_DIR" zip="$ZIP_PATH" +# pipefail off for the lock block: unzip piped into head / grep -q exits 141 (SIGPIPE) on larger zips. +set +o pipefail # --- WordPress.org folder lock (Wbcom standard - do not edit) ---------------------- top=$(unzip -Z1 "$zip" | head -1 | cut -d/ -f1) [ "$top" = "$slug" ] || { echo "Zip folder '$top' is not the WordPress.org slug '$slug'"; exit 1; } unzip -Z1 "$zip" | grep -qx "$slug/$main" || { echo "Main file $slug/$main missing from zip"; exit 1; } [ "$(LC_ALL=C grep -a -c ' Date: Tue, 24 Nov 2026 09:23:08 +0530 Subject: [PATCH 3/3] Build: pipefail-safe folder lock, single zip builder --- .distignore | 3 +- bin/build-release.sh | 8 ++-- gruntfile.js | 89 ++------------------------------------------ package.json | 2 +- 4 files changed, 9 insertions(+), 93 deletions(-) diff --git a/.distignore b/.distignore index 6d8ea3a..bb7323d 100644 --- a/.distignore +++ b/.distignore @@ -1,6 +1,5 @@ # Files and folders that never ship in the customer zip. -# Read by bin/build-release.sh (rsync --exclude-from). Mirrors gruntfile.js copy:build; -# change both together. +# Read by bin/build-release.sh (rsync --exclude-from), the only zip builder. # VCS / CI / editor /.git diff --git a/bin/build-release.sh b/bin/build-release.sh index 4eb2830..b198533 100755 --- a/bin/build-release.sh +++ b/bin/build-release.sh @@ -77,15 +77,13 @@ with zipfile.ZipFile(out,"w",zipfile.ZIP_DEFLATED,compresslevel=9) as z: for n in sorted(f): z.write(os.path.join(r,n))' "$ZIP_PATH" "$SLUG" ) rm -rf "$STAGE_DIR" zip="$ZIP_PATH" -# pipefail off for the lock block: unzip piped into head / grep -q exits 141 (SIGPIPE) on larger zips. -set +o pipefail # --- WordPress.org folder lock (Wbcom standard - do not edit) ---------------------- -top=$(unzip -Z1 "$zip" | head -1 | cut -d/ -f1) +entries=$(unzip -Z1 "$zip") # read the whole list once: no early-exit pipe, safe under pipefail +top=${entries%%/*} [ "$top" = "$slug" ] || { echo "Zip folder '$top' is not the WordPress.org slug '$slug'"; exit 1; } -unzip -Z1 "$zip" | grep -qx "$slug/$main" || { echo "Main file $slug/$main missing from zip"; exit 1; } +grep -qx "$slug/$main" <<<"$entries" || { echo "Main file $slug/$main missing from zip"; exit 1; } [ "$(LC_ALL=C grep -a -c '.zip', - mode: 'zip' - }, - files: [ - { - expand: true, - cwd: 'build/', - src: [ '**' ], - dest: '' - } - ] - } - }, - // Check text domain checktextdomain: { options: { @@ -138,21 +68,10 @@ module.exports = function ( grunt ) { } } ); - // Build distribution zip - // NOTE: build does NOT run makepot. The shipped POT is produced by - // `wp i18n make-pot` (the whole free series does this). grunt-wp-i18n - // regenerates it with degraded headers (Report-Msgid-Bugs-To lost, - // X-Generator changed) and a fresh timestamp, so letting it run here bakes a - // worse POT into the zip than the one committed. Run `grunt makepot` by hand - // only if you deliberately want to regenerate the template. - grunt.registerTask( 'build', [ - 'clean:build', - 'clean:dist', - 'checktextdomain', - 'copy:build', - 'compress:build', - 'clean:build' - ] ); + // The release zip is built by bin/build-release.sh only (folder = WordPress.org slug). + // NOTE: the shipped POT is produced by `wp i18n make-pot` (the whole free series does + // this); grunt-wp-i18n regenerates it with degraded headers, so the release build never + // runs makepot. // Default task grunt.registerTask( 'default', [ 'checktextdomain', 'makepot' ] ); diff --git a/package.json b/package.json index f6d06c9..b2edd88 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "url": "https://github.com/wbcomdesigns/buddypress-activity-link-preview" }, "scripts": { - "build": "grunt build" + "build": "bash bin/build-release.sh" }, "overrides": { "minimatch": "^3.1.5"