| 1 |
<?php |
| 2 |
/** |
| 3 |
* Friends Feed Parser |
| 4 |
* |
| 5 |
* This contains the reference implementation for a parser. |
| 6 |
* |
| 7 |
* @package Friends |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Friends; |
| 11 |
|
| 12 |
/** |
| 13 |
* This class describes a friends feed parser. |
| 14 |
*/ |
| 15 |
abstract class Feed_Parser { |
| 16 |
/** |
| 17 |
* Get the badge to display for this parser type. |
| 18 |
* |
| 19 |
* Return an array with badge info: |
| 20 |
* array( |
| 21 |
* 'label' => 'RSS', // Short text to display |
| 22 |
* 'color' => '#ee802f', // Background color |
| 23 |
* 'title' => 'RSS/Atom Feed' // Tooltip text |
| 24 |
* ) |
| 25 |
* |
| 26 |
* @return array|null Badge info array, or null for no badge. |
| 27 |
*/ |
| 28 |
public function get_badge() { |
| 29 |
return null; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Determines if this is a supported feed and to what degree we feel it's supported. |
| 34 |
* |
| 35 |
* @param string $url The url. |
| 36 |
* @param string $mime_type The mime type. |
| 37 |
* @param string $title The title. |
| 38 |
* @param string|null $content The content, it can't be assumed that it's always available. |
| 39 |
* |
| 40 |
* @return int Return 0 if unsupported, a positive value representing the confidence for the feed, use 10 if you're reasonably confident. |
| 41 |
*/ |
| 42 |
public function feed_support_confidence( $url, $mime_type, $title, $content = null ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
| 43 |
return 0; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Allow augmenting or modifying the details of a feed. |
| 48 |
* |
| 49 |
* The incoming $feed_details array looks like this: |
| 50 |
* |
| 51 |
* $feed_details = array( |
| 52 |
* 'url' => 'https://url.of/the/feed', |
| 53 |
* 'title' => 'Title from the <link> tag if any', |
| 54 |
* 'mime-type' => 'mime-type from the <link> tag if any', |
| 55 |
* // You can add these fields in the response: |
| 56 |
* 'autoselect' => true|false, |
| 57 |
* 'post-format' => 'standard', // or 'aside', etc. see get_post_format_strings() of WordPress core |
| 58 |
* ); |
| 59 |
* |
| 60 |
* @param array $feed_details The feed details. |
| 61 |
* |
| 62 |
* @return array The (potentially) modified feed details. |
| 63 |
*/ |
| 64 |
public function update_feed_details( $feed_details ) { |
| 65 |
return $feed_details; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Discover the feeds available at the URL specified. |
| 70 |
* |
| 71 |
* The content for the URL has already been fetched for you which can be analyzed. |
| 72 |
* |
| 73 |
* Return an array of supported feeds in the format of the $feed_details above: |
| 74 |
* |
| 75 |
* return array( |
| 76 |
* array( |
| 77 |
* 'url' => 'https://url.of/the/feed', |
| 78 |
* 'title' => 'Title for the feed', |
| 79 |
* 'mime-type' => 'mime-type for the feed', |
| 80 |
* 'rel' => 'e.g. alternate', |
| 81 |
* ), |
| 82 |
* ); |
| 83 |
* |
| 84 |
* @param string $content The content for the URL is already provided here. |
| 85 |
* @param string $url The url to search. |
| 86 |
* |
| 87 |
* @return array A list of supported feeds at the URL. |
| 88 |
*/ |
| 89 |
public function discover_available_feeds( $content, $url ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
| 90 |
return array(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Strip tracking parameters from a URL. |
| 95 |
* |
| 96 |
* @param string $url The URL to clean. |
| 97 |
* |
| 98 |
* @return string The URL without tracking parameters. |
| 99 |
*/ |
| 100 |
public static function strip_tracking_parameters( $url ) { |
| 101 |
$parsed = wp_parse_url( $url ); |
| 102 |
if ( ! $parsed || empty( $parsed['query'] ) ) { |
| 103 |
return $url; |
| 104 |
} |
| 105 |
|
| 106 |
parse_str( $parsed['query'], $params ); |
| 107 |
|
| 108 |
/** |
| 109 |
* Filter the list of tracking parameter prefixes to strip from URLs. |
| 110 |
* |
| 111 |
* @param array $prefixes List of parameter prefixes to strip (e.g., 'utm_' matches utm_source, utm_medium, etc.). |
| 112 |
*/ |
| 113 |
$tracking_prefixes = apply_filters( |
| 114 |
'friends_tracking_parameter_prefixes', |
| 115 |
array( |
| 116 |
'utm_', // Google Analytics. |
| 117 |
'mtm_', // Matomo. |
| 118 |
'pk_', // Piwik/Matomo. |
| 119 |
) |
| 120 |
); |
| 121 |
|
| 122 |
/** |
| 123 |
* Filter the list of exact tracking parameters to strip from URLs. |
| 124 |
* |
| 125 |
* @param array $params List of exact parameter names to strip. |
| 126 |
*/ |
| 127 |
$tracking_params = apply_filters( |
| 128 |
'friends_tracking_parameters', |
| 129 |
array( |
| 130 |
'fbclid', // Facebook. |
| 131 |
'gclid', // Google Ads. |
| 132 |
'gclsrc', // Google Ads. |
| 133 |
'dclid', // Google Display Network. |
| 134 |
'gbraid', // Google Ads (iOS). |
| 135 |
'wbraid', // Google Ads (web-to-app). |
| 136 |
'msclkid', // Microsoft Ads. |
| 137 |
'twclid', // Twitter. |
| 138 |
'igshid', // Instagram. |
| 139 |
'mc_eid', // Mailchimp. |
| 140 |
'mc_cid', // Mailchimp. |
| 141 |
'oly_enc_id', // Omeda. |
| 142 |
'oly_anon_id', // Omeda. |
| 143 |
'vero_id', // Vero. |
| 144 |
'_hsenc', // HubSpot. |
| 145 |
'_hsmi', // HubSpot. |
| 146 |
'hsCtaTracking', // HubSpot. |
| 147 |
'ref', // Generic referrer. |
| 148 |
'ref_src', // Generic referrer. |
| 149 |
'ref_url', // Generic referrer. |
| 150 |
) |
| 151 |
); |
| 152 |
|
| 153 |
$cleaned_params = array(); |
| 154 |
foreach ( $params as $key => $value ) { |
| 155 |
$is_tracking = false; |
| 156 |
|
| 157 |
// Check exact matches. |
| 158 |
if ( in_array( $key, $tracking_params, true ) ) { |
| 159 |
$is_tracking = true; |
| 160 |
} |
| 161 |
|
| 162 |
// Check prefix matches. |
| 163 |
if ( ! $is_tracking ) { |
| 164 |
foreach ( $tracking_prefixes as $prefix ) { |
| 165 |
if ( str_starts_with( $key, $prefix ) ) { |
| 166 |
$is_tracking = true; |
| 167 |
break; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if ( ! $is_tracking ) { |
| 173 |
$cleaned_params[ $key ] = $value; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
// Rebuild the URL. |
| 178 |
$clean_url = ''; |
| 179 |
if ( ! empty( $parsed['scheme'] ) ) { |
| 180 |
$clean_url .= $parsed['scheme'] . '://'; |
| 181 |
} |
| 182 |
if ( ! empty( $parsed['host'] ) ) { |
| 183 |
if ( ! empty( $parsed['user'] ) ) { |
| 184 |
$clean_url .= $parsed['user']; |
| 185 |
if ( ! empty( $parsed['pass'] ) ) { |
| 186 |
$clean_url .= ':' . $parsed['pass']; |
| 187 |
} |
| 188 |
$clean_url .= '@'; |
| 189 |
} |
| 190 |
$clean_url .= $parsed['host']; |
| 191 |
if ( ! empty( $parsed['port'] ) ) { |
| 192 |
$clean_url .= ':' . $parsed['port']; |
| 193 |
} |
| 194 |
} |
| 195 |
if ( ! empty( $parsed['path'] ) ) { |
| 196 |
$clean_url .= $parsed['path']; |
| 197 |
} |
| 198 |
if ( ! empty( $cleaned_params ) ) { |
| 199 |
$clean_url .= '?' . http_build_query( $cleaned_params ); |
| 200 |
} |
| 201 |
if ( ! empty( $parsed['fragment'] ) ) { |
| 202 |
$clean_url .= '#' . $parsed['fragment']; |
| 203 |
} |
| 204 |
|
| 205 |
return $clean_url; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Convert relative URLs to absolute ones in incoming content. |
| 210 |
* |
| 211 |
* @param string $html The html. |
| 212 |
* @param string $permalink The permalink of the feed. |
| 213 |
* |
| 214 |
* @return string The HTML with URLs replaced to their absolute represenation. |
| 215 |
*/ |
| 216 |
public function convert_relative_urls_to_absolute_urls( $html, $permalink ) { |
| 217 |
if ( ! $html ) { |
| 218 |
$html = ''; |
| 219 |
} |
| 220 |
|
| 221 |
// Strip off the hash. |
| 222 |
$permalink = strtok( $permalink, '#' ); |
| 223 |
|
| 224 |
// For now this only converts links and image srcs. |
| 225 |
return preg_replace_callback( |
| 226 |
'~(src|href)=(?:"([^"]+)|\'([^\']+))~i', |
| 227 |
function ( $m ) use ( $permalink ) { |
| 228 |
// Don't update hash-only links. |
| 229 |
if ( str_starts_with( $m[2], '#' ) ) { |
| 230 |
return $m[0]; |
| 231 |
} |
| 232 |
|
| 233 |
// Remove absolute URL from hashes so that it can become relative. |
| 234 |
if ( str_starts_with( $m[2], $permalink . '#' ) ) { |
| 235 |
return str_replace( $permalink, '', $m[0] ); |
| 236 |
} |
| 237 |
|
| 238 |
// Don't convert content URLs like data:image/png;base64, etc. |
| 239 |
if ( str_starts_with( $m[2], 'data:' ) ) { |
| 240 |
return $m[0]; |
| 241 |
} |
| 242 |
|
| 243 |
// Convert relative URLs to absolute ones and strip tracking parameters. |
| 244 |
$absolute_url = Mf2\resolveUrl( $permalink, $m[2] ); |
| 245 |
$clean_url = self::strip_tracking_parameters( $absolute_url ); |
| 246 |
return str_replace( $m[2], $clean_url, $m[0] ); |
| 247 |
}, |
| 248 |
$html |
| 249 |
); |
| 250 |
} |
| 251 |
} |
| 252 |
|