| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * WordPress Feed API 4 * 5 * Many of the functions used in here belong in The Loop, or The Loop for the 6 * Feeds. 7 * 8 * @package WordPress 9 * @subpackage Feed 10 * @since 2.1.0 11 */ 12 13 /** 14 * Retrieves RSS container for the bloginfo function. 15 * 16 * You can retrieve anything that you can using the get_bloginfo() function. 17 * Everything will be stripped of tags and characters converted, when the values 18 * are retrieved for use in the feeds. 19 * 20 * @since 1.5.1 21 * 22 * @see get_bloginfo() For the list of possible values to display. 23 * 24 * @param string $show See get_bloginfo() for possible values. 25 * @return string 26 */ 27 function get_bloginfo_rss( $show = '' ) { 28 $info = strip_tags( get_bloginfo( $show ) ); 29 /** 30 * Filters the bloginfo for use in RSS feeds. 31 * 32 * @since 2.2.0 33 * 34 * @see convert_chars() 35 * @see get_bloginfo() 36 * 37 * @param string $info Converted string value of the blog information. 38 * @param string $show The type of blog information to retrieve. 39 */ 40 return apply_filters( 'get_bloginfo_rss', convert_chars( $info ), $show ); 41 } 42 43 /** 44 * Displays RSS container for the bloginfo function. 45 * 46 * You can retrieve anything that you can using the get_bloginfo() function. 47 * Everything will be stripped of tags and characters converted, when the values 48 * are retrieved for use in the feeds. 49 * 50 * @since 0.71 51 * 52 * @see get_bloginfo() For the list of possible values to display. 53 * 54 * @param string $show See get_bloginfo() for possible values. 55 */ 56 function bloginfo_rss( $show = '' ) { 57 /** 58 * Filters the bloginfo for display in RSS feeds. 59 * 60 * @since 2.1.0 61 * 62 * @see get_bloginfo() 63 * 64 * @param string $rss_container RSS container for the blog information. 65 * @param string $show The type of blog information to retrieve. 66 */ 67 echo apply_filters( 'bloginfo_rss', get_bloginfo_rss( $show ), $show ); 68 } 69 70 /** 71 * Retrieves the default feed. 72 * 73 * The default feed is 'rss2', unless a plugin changes it through the 74 * {@see 'default_feed'} filter. 75 * 76 * @since 2.5.0 77 * 78 * @return string Default feed, or for example 'rss2', 'atom', etc. 79 */ 80 function get_default_feed() { 81 /** 82 * Filters the default feed type. 83 * 84 * @since 2.5.0 85 * 86 * @param string $feed_type Type of default feed. Possible values include 'rss2', 'atom'. 87 * Default 'rss2'. 88 */ 89 $default_feed = apply_filters( 'default_feed', 'rss2' ); 90 91 return ( 'rss' === $default_feed ) ? 'rss2' : $default_feed; 92 } 93 94 /** 95 * Retrieves the blog title for the feed title. 96 * 97 * @since 2.2.0 98 * @since 4.4.0 The optional `$sep` parameter was deprecated and renamed to `$deprecated`. 99 * 100 * @param string $deprecated Unused. 101 * @return string The document title. 102 * 103 * @phpstan-param '–' $deprecated 104 */ 105 function get_wp_title_rss( $deprecated = '–' ) { 106 if ( '–' !== $deprecated ) { 107 /* translators: %s: 'document_title_separator' filter name. */ 108 _deprecated_argument( __FUNCTION__, '4.4.0', sprintf( __( 'Use the %s filter instead.' ), '<code>document_title_separator</code>' ) ); 109 } 110 111 /** 112 * Filters the blog title for use as the feed title. 113 * 114 * @since 2.2.0 115 * @since 4.4.0 The `$sep` parameter was deprecated and renamed to `$deprecated`. 116 * 117 * @param string $title The current blog title. 118 * @param string $deprecated Unused. 119 */ 120 return apply_filters( 'get_wp_title_rss', wp_get_document_title(), $deprecated ); 121 } 122 123 /** 124 * Displays the blog title for display of the feed title. 125 * 126 * @since 2.2.0 127 * @since 4.4.0 The optional `$sep` parameter was deprecated and renamed to `$deprecated`. 128 * 129 * @param string $deprecated Unused. 130 * 131 * @phpstan-param '–' $deprecated 132 */ 133 function wp_title_rss( $deprecated = '–' ) { 134 if ( '–' !== $deprecated ) { 135 /* translators: %s: 'document_title_separator' filter name. */ 136 _deprecated_argument( __FUNCTION__, '4.4.0', sprintf( __( 'Use the %s filter instead.' ), '<code>document_title_separator</code>' ) ); 137 } 138 139 /** 140 * Filters the blog title for display of the feed title. 141 * 142 * @since 2.2.0 143 * @since 4.4.0 The `$sep` parameter was deprecated and renamed to `$deprecated`. 144 * 145 * @see get_wp_title_rss() 146 * 147 * @param string $wp_title_rss The current blog title. 148 * @param string $deprecated Unused. 149 */ 150 echo apply_filters( 'wp_title_rss', get_wp_title_rss(), $deprecated ); 151 } 152 153 /** 154 * Retrieves the current post title for the feed. 155 * 156 * @since 2.0.0 157 * @since 6.6.0 Added the `$post` parameter. 158 * 159 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 160 * @return string Current post title. 161 */ 162 function get_the_title_rss( $post = 0 ) { 163 $title = get_the_title( $post ); 164 165 /** 166 * Filters the post title for use in a feed. 167 * 168 * @since 1.2.0 169 * 170 * @param string $title The current post title. 171 */ 172 return apply_filters( 'the_title_rss', $title ); 173 } 174 175 /** 176 * Displays the post title in the feed. 177 * 178 * @since 0.71 179 */ 180 function the_title_rss() { 181 echo get_the_title_rss(); 182 } 183 184 /** 185 * Retrieves the post content for feeds. 186 * 187 * @since 2.9.0 188 * 189 * @see get_the_content() 190 * 191 * @param string $feed_type The type of feed. rss2 | atom | rss | rdf 192 * @return string The filtered content. 193 */ 194 function get_the_content_feed( $feed_type = null ) { 195 if ( ! $feed_type ) { 196 $feed_type = get_default_feed(); 197 } 198 199 /** This filter is documented in wp-includes/post-template.php */ 200 $content = apply_filters( 'the_content', get_the_content() ); 201 $content = str_replace( ']]>', ']]>', $content ); 202 203 /** 204 * Filters the post content for use in feeds. 205 * 206 * @since 2.9.0 207 * 208 * @param string $content The current post content. 209 * @param string $feed_type Type of feed. Possible values include 'rss2', 'atom'. 210 * Default 'rss2'. 211 */ 212 return apply_filters( 'the_content_feed', $content, $feed_type ); 213 } 214 215 /** 216 * Displays the post content for feeds. 217 * 218 * @since 2.9.0 219 * 220 * @param string $feed_type The type of feed. rss2 | atom | rss | rdf 221 */ 222 function the_content_feed( $feed_type = null ) { 223 echo get_the_content_feed( $feed_type ); 224 } 225 226 /** 227 * Displays the post excerpt for the feed. 228 * 229 * @since 0.71 230 */ 231 function the_excerpt_rss() { 232 $output = get_the_excerpt(); 233 /** 234 * Filters the post excerpt for a feed. 235 * 236 * @since 1.2.0 237 * 238 * @param string $output The current post excerpt. 239 */ 240 echo apply_filters( 'the_excerpt_rss', $output ); 241 } 242 243 /** 244 * Displays the permalink to the post for use in feeds. 245 * 246 * @since 2.3.0 247 */ 248 function the_permalink_rss() { 249 /** 250 * Filters the permalink to the post for use in feeds. 251 * 252 * @since 2.3.0 253 * 254 * @param string $post_permalink The current post permalink. 255 */ 256 echo esc_url( apply_filters( 'the_permalink_rss', get_permalink() ) ); 257 } 258 259 /** 260 * Outputs the link to the comments for the current post in an XML safe way. 261 * 262 * @since 3.0.0 263 */ 264 function comments_link_feed() { 265 /** 266 * Filters the comments permalink for the current post. 267 * 268 * @since 3.6.0 269 * 270 * @param string $comment_permalink The current comment permalink with 271 * '#comments' appended. 272 */ 273 echo esc_url( apply_filters( 'comments_link_feed', get_comments_link() ) ); 274 } 275 276 /** 277 * Displays the feed GUID for the current comment. 278 * 279 * @since 2.5.0 280 * 281 * @param int|WP_Comment $comment_id Optional comment object or ID. Defaults to global comment object. 282 */ 283 function comment_guid( $comment_id = null ) { 284 echo esc_url( get_comment_guid( $comment_id ) ); 285 } 286 287 /** 288 * Retrieves the feed GUID for the current comment. 289 * 290 * @since 2.5.0 291 * 292 * @param int|WP_Comment $comment_id Optional comment object or ID. Defaults to global comment object. 293 * @return string|false GUID for comment on success, false on failure. 294 */ 295 function get_comment_guid( $comment_id = null ) { 296 $comment = get_comment( $comment_id ); 297 298 if ( ! is_object( $comment ) ) { 299 return false; 300 } 301 302 return get_the_guid( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID; 303 } 304 305 /** 306 * Displays the link to the comments. 307 * 308 * @since 1.5.0 309 * @since 4.4.0 Introduced the `$comment` argument. 310 * 311 * @param int|WP_Comment $comment Optional. Comment object or ID. Defaults to global comment object. 312 */ 313 function comment_link( $comment = null ) { 314 /** 315 * Filters the current comment's permalink. 316 * 317 * @since 3.6.0 318 * 319 * @see get_comment_link() 320 * 321 * @param string $comment_permalink The current comment permalink. 322 */ 323 echo esc_url( apply_filters( 'comment_link', get_comment_link( $comment ) ) ); 324 } 325 326 /** 327 * Retrieves the current comment author for use in the feeds. 328 * 329 * @since 2.0.0 330 * 331 * @return string Comment Author. 332 */ 333 function get_comment_author_rss() { 334 /** 335 * Filters the current comment author for use in a feed. 336 * 337 * @since 1.5.0 338 * 339 * @see get_comment_author() 340 * 341 * @param string $comment_author The current comment author. 342 */ 343 return apply_filters( 'comment_author_rss', get_comment_author() ); 344 } 345 346 /** 347 * Displays the current comment author in the feed. 348 * 349 * @since 1.0.0 350 */ 351 function comment_author_rss() { 352 echo get_comment_author_rss(); 353 } 354 355 /** 356 * Displays the current comment content for use in the feeds. 357 * 358 * @since 1.0.0 359 */ 360 function comment_text_rss() { 361 $comment_text = get_comment_text(); 362 /** 363 * Filters the current comment content for use in a feed. 364 * 365 * @since 1.5.0 366 * 367 * @param string $comment_text The content of the current comment. 368 */ 369 $comment_text = apply_filters( 'comment_text_rss', $comment_text ); 370 echo $comment_text; 371 } 372 373 /** 374 * Retrieves all of the post categories, formatted for use in feeds. 375 * 376 * All of the categories for the current post in the feed loop, will be 377 * retrieved and have feed markup added, so that they can easily be added to the 378 * RSS2, Atom, or RSS1 and RSS0.91 RDF feeds. 379 * 380 * @since 2.1.0 381 * 382 * @param string $type Optional, default is the type returned by get_default_feed(). 383 * @return string All of the post categories for displaying in the feed. 384 */ 385 function get_the_category_rss( $type = null ) { 386 if ( empty( $type ) ) { 387 $type = get_default_feed(); 388 } 389 $categories = get_the_category(); 390 $tags = get_the_tags(); 391 $the_list = ''; 392 $cat_names = array(); 393 394 $filter = 'rss'; 395 if ( 'atom' === $type ) { 396 $filter = 'raw'; 397 } 398 399 if ( ! empty( $categories ) ) { 400 foreach ( (array) $categories as $category ) { 401 $cat_names[] = sanitize_term_field( 'name', $category->name, $category->term_id, 'category', $filter ); 402 } 403 } 404 405 if ( ! empty( $tags ) ) { 406 foreach ( (array) $tags as $tag ) { 407 $cat_names[] = sanitize_term_field( 'name', $tag->name, $tag->term_id, 'post_tag', $filter ); 408 } 409 } 410 411 $cat_names = array_unique( $cat_names ); 412 413 $atom_scheme = ''; 414 $blog_charset = ''; 415 416 if ( $cat_names ) { 417 if ( 'atom' === $type ) { 418 $atom_scheme = get_bloginfo_rss( 'url' ); 419 } elseif ( 'rdf' !== $type ) { 420 $blog_charset = get_option( 'blog_charset' ); 421 } 422 } 423 424 foreach ( $cat_names as $cat_name ) { 425 if ( 'rdf' === $type ) { 426 $the_list .= "\t\t<dc:subject><![CDATA[$cat_name]]></dc:subject>\n"; 427 } elseif ( 'atom' === $type ) { 428 $the_list .= sprintf( '<category scheme="%1$s" term="%2$s" />', esc_attr( $atom_scheme ), esc_attr( $cat_name ) ); 429 } else { 430 $the_list .= "\t\t<category><![CDATA[" . html_entity_decode( $cat_name, ENT_COMPAT, $blog_charset ) . "]]></category>\n"; 431 } 432 } 433 434 /** 435 * Filters all of the post categories for display in a feed. 436 * 437 * @since 1.2.0 438 * 439 * @param string $the_list All of the RSS post categories. 440 * @param string $type Type of feed. Possible values include 'rss2', 'atom'. 441 * Default 'rss2'. 442 */ 443 return apply_filters( 'the_category_rss', $the_list, $type ); 444 } 445 446 /** 447 * Displays the post categories in the feed. 448 * 449 * @since 0.71 450 * 451 * @see get_the_category_rss() For better explanation. 452 * 453 * @param string $type Optional, default is the type returned by get_default_feed(). 454 */ 455 function the_category_rss( $type = null ) { 456 echo get_the_category_rss( $type ); 457 } 458 459 /** 460 * Displays the HTML type based on the blog setting. 461 * 462 * The two possible values are either 'xhtml' or 'html'. 463 * 464 * @since 2.2.0 465 */ 466 function html_type_rss() { 467 $type = get_bloginfo( 'html_type' ); 468 if ( str_contains( $type, 'xhtml' ) ) { 469 $type = 'xhtml'; 470 } else { 471 $type = 'html'; 472 } 473 echo $type; 474 } 475 476 /** 477 * Displays the rss enclosure for the current post. 478 * 479 * Uses the global $post to check whether the post requires a password and if 480 * the user has the password for the post. If not then it will return before 481 * displaying. 482 * 483 * Also uses the function get_post_custom() to get the post's 'enclosure' 484 * metadata field and parses the value to display the enclosure(s). The 485 * enclosure(s) consist of enclosure HTML tag(s) with a URI and other 486 * attributes. 487 * 488 * @since 1.5.0 489 */ 490 function rss_enclosure() { 491 if ( post_password_required() ) { 492 return; 493 } 494 495 foreach ( (array) get_post_custom() as $key => $val ) { 496 if ( 'enclosure' === $key ) { 497 foreach ( (array) $val as $enc ) { 498 $enclosure = explode( "\n", $enc ); 499 500 if ( count( $enclosure ) < 3 ) { 501 continue; 502 } 503 504 // Only get the first element, e.g. 'audio/mpeg' from 'audio/mpeg mpga mp2 mp3'. 505 $t = preg_split( '/[ \t]/', trim( $enclosure[2] ) ); 506 $type = $t[0]; 507 508 /** 509 * Filters the RSS enclosure HTML link tag for the current post. 510 * 511 * @since 2.2.0 512 * 513 * @param string $html_link_tag The HTML link tag with a URI and other attributes. 514 */ 515 echo apply_filters( 'rss_enclosure', '<enclosure url="' . esc_url( trim( $enclosure[0] ) ) . '" length="' . absint( trim( $enclosure[1] ) ) . '" type="' . esc_attr( $type ) . '" />' . "\n" ); 516 } 517 } 518 } 519 } 520 521 /** 522 * Displays the atom enclosure for the current post. 523 * 524 * Uses the global $post to check whether the post requires a password and if 525 * the user has the password for the post. If not then it will return before 526 * displaying. 527 * 528 * Also uses the function get_post_custom() to get the post's 'enclosure' 529 * metadata field and parses the value to display the enclosure(s). The 530 * enclosure(s) consist of link HTML tag(s) with a URI and other attributes. 531 * 532 * @since 2.2.0 533 */ 534 function atom_enclosure() { 535 if ( post_password_required() ) { 536 return; 537 } 538 539 foreach ( (array) get_post_custom() as $key => $val ) { 540 if ( 'enclosure' === $key ) { 541 foreach ( (array) $val as $enc ) { 542 $enclosure = explode( "\n", $enc ); 543 544 $url = ''; 545 $type = ''; 546 $length = 0; 547 548 $mimes = get_allowed_mime_types(); 549 550 // Parse URL. 551 if ( isset( $enclosure[0] ) && is_string( $enclosure[0] ) ) { 552 $url = trim( $enclosure[0] ); 553 } 554 555 // Parse length and type. 556 for ( $i = 1; $i <= 2; $i++ ) { 557 if ( isset( $enclosure[ $i ] ) ) { 558 if ( is_numeric( $enclosure[ $i ] ) ) { 559 $length = trim( $enclosure[ $i ] ); 560 } elseif ( in_array( $enclosure[ $i ], $mimes, true ) ) { 561 $type = trim( $enclosure[ $i ] ); 562 } 563 } 564 } 565 566 $html_link_tag = sprintf( 567 "<link href=\"%s\" rel=\"enclosure\" length=\"%d\" type=\"%s\" />\n", 568 esc_url( $url ), 569 esc_attr( $length ), 570 esc_attr( $type ) 571 ); 572 573 /** 574 * Filters the atom enclosure HTML link tag for the current post. 575 * 576 * @since 2.2.0 577 * 578 * @param string $html_link_tag The HTML link tag with a URI and other attributes. 579 */ 580 echo apply_filters( 'atom_enclosure', $html_link_tag ); 581 } 582 } 583 } 584 } 585 586 /** 587 * Determines the type of a string of data with the data formatted. 588 * 589 * Tell whether the type is text, HTML, or XHTML, per RFC 4287 section 3.1. 590 * 591 * In the case of WordPress, text is defined as containing no markup, 592 * XHTML is defined as "well formed", and HTML as tag soup (i.e., the rest). 593 * 594 * Container div tags are added to XHTML values, per section 3.1.1.3. 595 * 596 * @link http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rfc.section.3.1 597 * 598 * @since 2.5.0 599 * 600 * @param string $data Input string. 601 * @return array array(type, value) 602 * 603 * @phpstan-return array{'html'|'text'|'xhtml', string} 604 */ 605 function prep_atom_text_construct( $data ) { 606 if ( ! str_contains( $data, '<' ) && ! str_contains( $data, '&' ) ) { 607 return array( 'text', $data ); 608 } 609 610 if ( ! function_exists( 'xml_parser_create' ) ) { 611 wp_trigger_error( '', __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) ); 612 613 return array( 'html', "<![CDATA[$data]]>" ); 614 } 615 616 $parser = xml_parser_create(); 617 xml_parse( $parser, '<div>' . $data . '</div>', true ); 618 $code = xml_get_error_code( $parser ); 619 620 if ( PHP_VERSION_ID < 80000 ) { // xml_parser_free() has no effect as of PHP 8.0. 621 xml_parser_free( $parser ); 622 } 623 624 unset( $parser ); 625 626 if ( ! $code ) { 627 if ( ! str_contains( $data, '<' ) ) { 628 return array( 'text', $data ); 629 } else { 630 $data = "<div xmlns='http://www.w3.org/1999/xhtml'>$data</div>"; 631 return array( 'xhtml', $data ); 632 } 633 } 634 635 if ( ! str_contains( $data, ']]>' ) ) { 636 return array( 'html', "<![CDATA[$data]]>" ); 637 } else { 638 return array( 'html', htmlspecialchars( $data ) ); 639 } 640 } 641 642 /** 643 * Displays Site Icon in atom feeds. 644 * 645 * @since 4.3.0 646 * 647 * @see get_site_icon_url() 648 */ 649 function atom_site_icon() { 650 $url = get_site_icon_url( 32 ); 651 if ( $url ) { 652 echo '<icon>' . convert_chars( $url ) . "</icon>\n"; 653 } 654 } 655 656 /** 657 * Displays Site Icon in RSS2. 658 * 659 * @since 4.3.0 660 */ 661 function rss2_site_icon() { 662 $rss_title = get_wp_title_rss(); 663 if ( empty( $rss_title ) ) { 664 $rss_title = get_bloginfo_rss( 'name' ); 665 } 666 667 $url = get_site_icon_url( 32 ); 668 if ( $url ) { 669 echo ' 670 <image> 671 <url>' . convert_chars( $url ) . '</url> 672 <title>' . $rss_title . '</title> 673 <link>' . get_bloginfo_rss( 'url' ) . '</link> 674 <width>32</width> 675 <height>32</height> 676 </image> ' . "\n"; 677 } 678 } 679 680 /** 681 * Returns the link for the currently displayed feed. 682 * 683 * @since 5.3.0 684 * 685 * @return string Correct link for the atom:self element. 686 */ 687 function get_self_link() { 688 $parsed = parse_url( home_url() ); 689 690 $domain = $parsed['host']; 691 if ( isset( $parsed['port'] ) ) { 692 $domain .= ':' . $parsed['port']; 693 } 694 695 return set_url_scheme( 'http://' . $domain . wp_unslash( $_SERVER['REQUEST_URI'] ) ); 696 } 697 698 /** 699 * Displays the link for the currently displayed feed in a XSS safe way. 700 * 701 * Generate a correct link for the atom:self element. 702 * 703 * @since 2.5.0 704 */ 705 function self_link() { 706 /** 707 * Filters the current feed URL. 708 * 709 * @since 3.6.0 710 * 711 * @see set_url_scheme() 712 * @see wp_unslash() 713 * 714 * @param string $feed_link The link for the feed with set URL scheme. 715 */ 716 echo esc_url( apply_filters( 'self_link', get_self_link() ) ); 717 } 718 719 /** 720 * Retrieves the XML namespaces for the root element of a feed. 721 * 722 * Namespaces are keyed by their prefix, so the same prefix cannot be 723 * declared twice. The default namespaces of a feed type cannot be removed, 724 * as the bundled feed templates use them in their static markup. 725 * 726 * @since 7.2.0 727 * 728 * @param string $type Type of feed. Possible values include 'rss2', 'rss2-comments', 729 * 'rdf', 'atom', and 'atom-comments'. 730 * @return array<string, string> Array of namespace URIs, keyed by their prefix. 731 * @phpstan-param non-falsy-string $type 732 * @phpstan-return array<non-falsy-string, non-falsy-string> 733 */ 734 function wp_get_feed_namespaces( string $type ): array { 735 $defaults = array(); 736 737 switch ( $type ) { 738 case 'rss2': 739 $defaults = array( 740 'content' => 'http://purl.org/rss/1.0/modules/content/', 741 'wfw' => 'http://wellformedweb.org/CommentAPI/', 742 'dc' => 'http://purl.org/dc/elements/1.1/', 743 'atom' => 'http://www.w3.org/2005/Atom', 744 'sy' => 'http://purl.org/rss/1.0/modules/syndication/', 745 'slash' => 'http://purl.org/rss/1.0/modules/slash/', 746 ); 747 break; 748 749 case 'rss2-comments': 750 $defaults = array( 751 'content' => 'http://purl.org/rss/1.0/modules/content/', 752 'dc' => 'http://purl.org/dc/elements/1.1/', 753 'atom' => 'http://www.w3.org/2005/Atom', 754 'sy' => 'http://purl.org/rss/1.0/modules/syndication/', 755 ); 756 break; 757 758 case 'rdf': 759 $defaults = array( 760 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 761 'dc' => 'http://purl.org/dc/elements/1.1/', 762 'sy' => 'http://purl.org/rss/1.0/modules/syndication/', 763 'admin' => 'http://webns.net/mvcb/', 764 'content' => 'http://purl.org/rss/1.0/modules/content/', 765 ); 766 break; 767 768 case 'atom': 769 case 'atom-comments': 770 $defaults = array( 771 'thr' => 'http://purl.org/syndication/thread/1.0', 772 ); 773 break; 774 } 775 776 /** 777 * Filters the XML namespaces of a feed's root element. 778 * 779 * Namespaces are keyed by their prefix, which makes duplicate `xmlns` 780 * attributes impossible. 781 * 782 * @since 7.2.0 783 * 784 * @param array<string, string> $namespaces Array of namespace URIs, keyed by their prefix. 785 * @param string $type Type of feed. Possible values include 'rss2', 786 * 'rss2-comments', 'rdf', 'atom', and 'atom-comments'. 787 */ 788 $namespaces = apply_filters( 'wp_feed_namespaces', $defaults, $type ); 789 790 if ( ! is_array( $namespaces ) ) { 791 $namespaces = array(); 792 } 793 794 // The bundled feed templates use the default namespaces, so they cannot be removed. 795 $namespaces = array_merge( $namespaces, $defaults ); 796 797 $sanitized = array(); 798 799 foreach ( $namespaces as $prefix => $uri ) { 800 $prefix = (string) $prefix; 801 802 // Prefixes must be valid XML names, and the `xml` and `xmlns` prefixes are reserved. 803 if ( ! preg_match( '/^[\p{L}_][\p{L}\p{M}\p{N}._\-\x{B7}]*\z/u', $prefix ) 804 || in_array( strtolower( $prefix ), array( 'xml', 'xmlns' ), true ) 805 ) { 806 continue; 807 } 808 809 if ( ! is_string( $uri ) || empty( $uri ) ) { 810 continue; 811 } 812 813 $sanitized[ $prefix ] = $uri; 814 } 815 816 return $sanitized; 817 } 818 819 /** 820 * Displays the XML namespaces for the root element of a feed. 821 * 822 * Plugins should add namespaces via the {@see 'wp_feed_namespaces'} filter instead 823 * of the older {@see "{$type}_ns"} actions, as two action callbacks printing the 824 * same namespace produce a duplicate attribute, which is a well-formedness 825 * error in XML. 826 * 827 * @since 7.2.0 828 * 829 * @param string $type Type of feed. Possible values include 'rss2', 'rss2-comments', 830 * 'rdf', 'atom', and 'atom-comments'. 831 * @phpstan-param non-falsy-string $type 832 */ 833 function wp_feed_namespaces( string $type ): void { 834 foreach ( wp_get_feed_namespaces( $type ) as $prefix => $uri ) { 835 printf( "xmlns:%s=\"%s\"\n\t", $prefix, esc_attr( $uri ) ); 836 } 837 } 838 839 /** 840 * Gets the UTC time of the most recently modified post from WP_Query. 841 * 842 * If viewing a comment feed, the time of the most recently modified 843 * comment will be returned. 844 * 845 * @since 5.2.0 846 * 847 * @global WP_Query $wp_query WordPress Query object. 848 * 849 * @param string $format Date format string to return the time in. 850 * @return string|false The time in requested format, or false on failure. 851 */ 852 function get_feed_build_date( $format ) { 853 global $wp_query; 854 855 $datetime = false; 856 $max_modified_time = false; 857 $utc = new DateTimeZone( 'UTC' ); 858 859 if ( $wp_query instanceof WP_Query && is_array( $wp_query->posts ) ) { 860 /* 861 * Collect the post modified times. WP_Post objects are read as-is, since 862 * passing them through get_post() would look them up again by ID, which 863 * drops virtual posts and discards in-memory changes. Queries using 864 * fields => 'ids' (or 'id=>parent') yield post IDs instead, which are 865 * looked up after priming the post cache to avoid a query per post. 866 */ 867 $modified_times = array(); 868 $post_ids = array(); 869 foreach ( $wp_query->posts as $post ) { 870 if ( $post instanceof WP_Post ) { 871 $modified_times[] = $post->post_modified_gmt; 872 } elseif ( is_int( $post ) ) { 873 // A post ID, from a query with fields => 'ids'. 874 $post_ids[] = $post; 875 } elseif ( is_object( $post ) && isset( $post->ID ) && is_int( $post->ID ) ) { 876 /* 877 * A partial post object of shape object{ ID: int, post_parent: int }, 878 * from a query with fields => 'id=>parent'. It lacks post_modified_gmt, 879 * so the post is looked up by its ID. 880 */ 881 $post_ids[] = $post->ID; 882 } 883 } 884 885 if ( $post_ids ) { 886 _prime_post_caches( $post_ids, false, false ); 887 foreach ( $post_ids as $post_id ) { 888 $post = get_post( $post_id ); 889 if ( $post instanceof WP_Post ) { 890 $modified_times[] = $post->post_modified_gmt; 891 } 892 } 893 } 894 895 // If this is a comment feed, check those objects too. 896 if ( $wp_query->is_comment_feed() && is_array( $wp_query->comments ) ) { 897 foreach ( $wp_query->comments as $comment ) { 898 // A 'get_comment' filter may have returned null for a comment. 899 if ( $comment instanceof WP_Comment ) { 900 $modified_times[] = $comment->comment_date_gmt; 901 } 902 } 903 } 904 905 // Determine the maximum modified time. 906 if ( $modified_times ) { 907 $datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', max( $modified_times ), $utc ); 908 } 909 } 910 911 if ( false === $datetime ) { 912 // Fall back to last time any post was modified or published. 913 $datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', get_lastpostmodified( 'GMT' ), $utc ); 914 } 915 916 if ( false !== $datetime ) { 917 $max_modified_time = $datetime->format( $format ); 918 } 919 920 /** 921 * Filters the date the last post or comment in the query was modified. 922 * 923 * @since 5.2.0 924 * 925 * @param string|false $max_modified_time Date the last post or comment was modified in the query, in UTC. 926 * False on failure. 927 * @param string $format The date format requested in get_feed_build_date(). 928 */ 929 return apply_filters( 'get_feed_build_date', $max_modified_time, $format ); 930 } 931 932 /** 933 * Returns the content type for specified feed type. 934 * 935 * @since 2.8.0 936 * 937 * @param string $type Type of feed. Possible values include 'rss', rss2', 'atom', and 'rdf'. 938 * @return string Content type for specified feed type. 939 */ 940 function feed_content_type( $type = '' ) { 941 if ( empty( $type ) ) { 942 $type = get_default_feed(); 943 } 944 945 $types = array( 946 'rss' => 'application/rss+xml', 947 'rss2' => 'application/rss+xml', 948 'rss-http' => 'text/xml', 949 'atom' => 'application/atom+xml', 950 'rdf' => 'application/rdf+xml', 951 ); 952 953 $content_type = ( ! empty( $types[ $type ] ) ) ? $types[ $type ] : 'application/octet-stream'; 954 955 /** 956 * Filters the content type for a specific feed type. 957 * 958 * @since 2.8.0 959 * 960 * @param string $content_type Content type indicating the type of data that a feed contains. 961 * @param string $type Type of feed. Possible values include 'rss', rss2', 'atom', and 'rdf'. 962 */ 963 return apply_filters( 'feed_content_type', $content_type, $type ); 964 } 965 966 /** 967 * Builds SimplePie object based on RSS or Atom feed from URL. 968 * 969 * @since 2.8.0 970 * 971 * @param string|string[] $url URL of feed to retrieve. If an array of URLs, the feeds are merged 972 * using SimplePie's multifeed feature. 973 * See also {@link http://simplepie.org/wiki/faq/typical_multifeed_gotchas} 974 * @return SimplePie\SimplePie|WP_Error SimplePie object on success or WP_Error object on failure. 975 */ 976 function fetch_feed( $url ) { 977 if ( ! class_exists( 'SimplePie\SimplePie', false ) ) { 978 require_once ABSPATH . WPINC . '/class-simplepie.php'; 979 } 980 981 require_once ABSPATH . WPINC . '/class-wp-feed-cache-transient.php'; 982 require_once ABSPATH . WPINC . '/class-wp-simplepie-file.php'; 983 require_once ABSPATH . WPINC . '/class-wp-simplepie-sanitize-kses.php'; 984 985 $feed = new SimplePie\SimplePie(); 986 987 $feed->get_registry()->register( SimplePie\Sanitize::class, 'WP_SimplePie_Sanitize_KSES', true ); 988 989 /* 990 * We must manually overwrite $feed->sanitize because SimplePie's constructor 991 * sets it before we have a chance to set the sanitization class. 992 */ 993 $feed->sanitize = new WP_SimplePie_Sanitize_KSES(); 994 995 // Register the cache handler using the recommended method for SimplePie 1.3 or later. 996 if ( method_exists( 'SimplePie_Cache', 'register' ) ) { 997 SimplePie_Cache::register( 'wp_transient', 'WP_Feed_Cache_Transient' ); 998 $feed->set_cache_location( 'wp_transient' ); 999 } else { 1000 // Back-compat for SimplePie 1.2.x. 1001 require_once ABSPATH . WPINC . '/class-wp-feed-cache.php'; 1002 $feed->set_cache_class( 'WP_Feed_Cache' ); 1003 } 1004 1005 $feed->get_registry()->register( SimplePie\File::class, 'WP_SimplePie_File', true ); 1006 1007 /** This filter is documented in wp-includes/class-wp-feed-cache-transient.php */ 1008 $feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url ) ); 1009 1010 /** 1011 * Fires just before processing the SimplePie feed object. 1012 * 1013 * @since 3.0.0 1014 * 1015 * @param SimplePie\SimplePie $feed SimplePie feed object (passed by reference). 1016 * @param string|string[] $url URL of feed or array of URLs of feeds to retrieve. 1017 */ 1018 do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) ); 1019 1020 if ( empty( $url ) ) { 1021 /* 1022 * @todo: Set $url to empty string once supported by SimplePie. 1023 * 1024 * The early return without proceeding is to work around a PHP 8.5 1025 * deprecation issue resolved in https://github.com/simplepie/simplepie/pull/949 1026 * 1027 * To avoid the duplicate code, this block can be replaced with `$url = '';` once SimplePie 1028 * is upgraded to a version that includes the fix. 1029 */ 1030 $feed->init(); 1031 $feed->set_output_encoding( get_bloginfo( 'charset' ) ); 1032 1033 if ( $feed->error() ) { 1034 return new WP_Error( 'simplepie-error', $feed->error() ); 1035 } 1036 1037 return $feed; 1038 } elseif ( is_array( $url ) && count( $url ) === 1 ) { 1039 $url = array_shift( $url ); 1040 } elseif ( is_array( $url ) ) { 1041 $feeds = array(); 1042 $simplepie_errors = array(); 1043 foreach ( $url as $feed_url ) { 1044 $simplepie_instance = clone $feed; 1045 $simplepie_instance->set_feed_url( $feed_url ); 1046 $simplepie_instance->init(); 1047 $simplepie_instance->set_output_encoding( get_bloginfo( 'charset' ) ); 1048 1049 if ( $simplepie_instance->error() ) { 1050 $simplepie_errors[] = sprintf( 1051 /* translators: %1$s is the feed URL, %2$s is the error message. */ 1052 __( 'Error fetching feed %1$s: %2$s' ), 1053 esc_url( $feed_url ), 1054 $simplepie_instance->error() 1055 ); 1056 unset( $simplepie_instance ); 1057 continue; 1058 } 1059 1060 $feeds[] = $simplepie_instance; 1061 unset( $simplepie_instance ); 1062 } 1063 1064 if ( ! empty( $simplepie_errors ) ) { 1065 return new WP_Error( 'simplepie-error', $simplepie_errors ); 1066 } 1067 1068 $feed->init(); 1069 $feed->data['items'] = SimplePie\SimplePie::merge_items( $feeds ); 1070 return $feed; 1071 } 1072 1073 $feed->set_feed_url( $url ); 1074 $feed->init(); 1075 $feed->set_output_encoding( get_bloginfo( 'charset' ) ); 1076 1077 if ( $feed->error() ) { 1078 return new WP_Error( 'simplepie-error', $feed->error() ); 1079 } 1080 1081 return $feed; 1082 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sat Dec 5 08:20:02 2026 | Cross-referenced by PHPXref |