settings = $settings; $this->audio = $audio; } /** * Hooks. Kept out of the constructor like Audio::register_hooks(). */ public function register_hooks() { add_action( 'init', array($this, 'register_feed'), 20 ); // The date WordPress sends as Last-Modified (and hashes into the ETag) for this feed, // so podcast apps hear "changed" after a new recording, not "not modified". add_filter( 'get_lastpostmodified', array($this, 'last_modified'), 10, 2 ); // The rendered feed is kept until a recording, a post or the settings change. foreach ( array('added_post_meta', 'updated_post_meta', 'deleted_post_meta') as $hook ) { add_action( $hook, array(__CLASS__, 'forget_on_meta'), 10, 3 ); } foreach ( array( 'save_post', 'deleted_post', 'update_option_aifi_settings', 'update_site_option_aifi_settings' ) as $hook ) { add_action( $hook, array(__CLASS__, 'forget_cache') ); } } /** * Drops the rendered feed. */ public static function forget_cache() { delete_transient( self::CACHE ); } /** * Drops the rendered feed when post meta the feed reads changes: a recording saved, * removed or hidden, or a new episode image. * * @param int|int[] $meta_ids Meta ids. * @param int $object_id Post ID. * @param string $meta_key Meta key. */ public static function forget_on_meta( $meta_ids, $object_id, $meta_key ) { if ( in_array( $meta_key, array( Audio::META_ID, Audio::META_HIDDEN, Audio::META_SECONDS, Audio::META_TIME, '_thumbnail_id' ), true ) ) { self::forget_cache(); } } /** * The feed name: "podcast", or "aifi-podcast" when another plugin owns "podcast". * * @return string */ public static function slug() { if ( '' !== self::$active ) { return self::$active; } global $wp_filter; $hook = 'do_feed_' . self::FEED; if ( empty( $wp_filter[$hook] ) || !is_object( $wp_filter[$hook] ) ) { return self::FEED; } // Another plugin's feed takes the address; this plugin's own feed does not count. foreach ( $wp_filter[$hook]->callbacks as $callbacks ) { foreach ( $callbacks as $callback ) { $fn = $callback['function']; if ( is_array( $fn ) && isset( $fn[0], $fn[1] ) && $fn[0] instanceof self && 'render' === $fn[1] ) { continue; } return self::FEED_ALT; } } return self::FEED; } /** * Address to submit to podcast apps. * * @return string */ public static function url() { return get_feed_link( self::slug() ); } /** * Whether the feed is on and the site may use it. * * @return bool */ public function is_enabled() { $settings = $this->settings->get_settings(); return Providers::can_use_premium() && !empty( $settings['podcast_enabled'] ); } /** * Registers the feed when it is on, and refreshes the rewrite rules once whenever * that changes (on, off, or a new feed name). Never flushes on a normal request. * * The stored feed name is the flag: when the setting changes it no longer matches, * and the rules are rebuilt on wp_loaded, after every plugin has added its own. */ public function register_feed() { $want = ( $this->is_enabled() ? self::slug() : '' ); if ( '' !== $want ) { add_feed( $want, array($this, 'render') ); self::$active = $want; } if ( get_option( self::OPTION_FLUSHED, '' ) !== $want ) { if ( did_action( 'wp_loaded' ) ) { $this->flush_rules(); } elseif ( false === has_action( 'wp_loaded', array($this, 'flush_rules') ) ) { add_action( 'wp_loaded', array($this, 'flush_rules') ); } } } /** * Rebuilds the rewrite rules for the feed as it is now, and records that it was done. */ public function flush_rules() { $want = ( '' !== self::$active && $this->is_enabled() ? self::$active : '' ); flush_rewrite_rules( false ); // Autoloaded: it is read on every request, so it should cost no extra query. update_option( self::OPTION_FLUSHED, $want, true ); } /** * Whether this request is for the podcast feed. * * @return bool */ private function is_feed_request() { global $wp; return $wp instanceof \WP && isset( $wp->query_vars['feed'] ) && self::slug() === $wp->query_vars['feed']; } /** * Last-Modified date of the podcast feed: the newest recording or the last post * change, whichever is later. Other requests are left alone. * * @param string|false $date Last post change, Y-m-d H:i:s. * @param string $timezone Timezone of $date. * @return string|false */ public function last_modified( $date, $timezone ) { if ( 'gmt' !== strtolower( (string) $timezone ) || !$this->is_feed_request() || !$this->is_enabled() ) { return $date; } $feed = $this->feed(); if ( empty( $feed['newest'] ) ) { return $date; } $newest = gmdate( 'Y-m-d H:i:s', (int) $feed['newest'] ); return ( !$date || strcmp( $newest, (string) $date ) > 0 ? $newest : $date ); } /** * Removes the feed's rewrite rules. Called when the plugin is deactivated. */ public static function deactivate() { global $wp_rewrite; delete_option( self::OPTION_FLUSHED ); if ( $wp_rewrite instanceof \WP_Rewrite ) { $wp_rewrite->feeds = array_values( array_diff( (array) $wp_rewrite->feeds, array(self::FEED, self::FEED_ALT) ) ); flush_rewrite_rules( false ); } self::$active = ''; } /** * Channel details, with the defaults filled in. * * @param array|null $s Settings, or null to read them. * @return array */ public function channel( $s = null ) { if ( null === $s ) { $s = $this->settings->get_settings(); } $site = wp_strip_all_tags( html_entity_decode( (string) get_bloginfo( 'name' ), ENT_QUOTES, 'UTF-8' ) ); $tagline = wp_strip_all_tags( html_entity_decode( (string) get_bloginfo( 'description' ), ENT_QUOTES, 'UTF-8' ) ); $text = function ( $key, $fallback ) use($s) { $value = ( isset( $s[$key] ) ? trim( (string) $s[$key] ) : '' ); return ( '' !== $value ? $value : $fallback ); }; $category = ( isset( $s['podcast_category'] ) && in_array( $s['podcast_category'], self::CATEGORIES, true ) ? $s['podcast_category'] : self::DEFAULT_CATEGORY ); $limit = ( isset( $s['podcast_limit'] ) ? (int) $s['podcast_limit'] : self::DEFAULT_LIMIT ); return array( 'title' => $text( 'podcast_title', $site ), 'description' => $text( 'podcast_description', ( '' !== $tagline ? $tagline : $site ) ), 'author' => $text( 'podcast_author', $site ), 'email' => ( isset( $s['podcast_email'] ) ? (string) $s['podcast_email'] : '' ), 'image_id' => ( isset( $s['podcast_image'] ) ? (int) $s['podcast_image'] : 0 ), 'category' => $category, 'explicit' => isset( $s['podcast_explicit'] ) && 'yes' === $s['podcast_explicit'], 'limit' => max( 1, min( 200, ( $limit ? $limit : self::DEFAULT_LIMIT ) ) ), ); } /** * "00:04:12" style length for itunes:duration. * * @param int $seconds Seconds. * @return string */ public static function format_hhmmss( $seconds ) { $seconds = max( 0, (int) $seconds ); return sprintf( '%02d:%02d:%02d', intdiv( $seconds, 3600 ), intdiv( $seconds % 3600, 60 ), $seconds % 60 ); } /** * Plain text, without tags or HTML entities, ready for esc_xml(). * * @param string $text Text, possibly with HTML or entities. * @return string */ private static function plain( $text ) { $text = wp_strip_all_tags( html_entity_decode( (string) $text, ENT_QUOTES, 'UTF-8' ) ); // Characters XML 1.0 does not allow (a pasted form feed, say) would break the whole // feed for every podcast app, so they are dropped. $clean = preg_replace( '/[^\\x{9}\\x{A}\\x{D}\\x{20}-\\x{D7FF}\\x{E000}-\\x{FFFD}\\x{10000}-\\x{10FFFF}]/u', '', $text ); return ( is_string( $clean ) ? $clean : $text ); } /** * Prints the feed. */ public function render() { if ( !$this->is_enabled() ) { status_header( 404 ); return; } status_header( 404 ); } /** * The rendered feed and the time of its newest recording, from the cache while * nothing it shows has changed. * * @return array xml, newest (Unix time, 0 when there are no episodes). */ private function feed() { $c = $this->channel(); $cfg = $this->audio->config(); // Anything the feed shows that is not a post: when one changes, the copy is rebuilt. $sig = md5( (string) wp_json_encode( array( $c, self::slug(), self::url(), home_url( '/' ), get_bloginfo( 'language' ), $cfg['enabled'], $cfg['post_types'], AIFI_VERSION ) ) ); $cached = get_transient( self::CACHE ); if ( is_array( $cached ) && isset( $cached['sig'], $cached['xml'], $cached['newest'] ) && $sig === $cached['sig'] ) { return $cached; } $items = $this->items( $c['limit'] ); $newest = 0; foreach ( $items as $item ) { $newest = max( $newest, (int) $item['date'] ); } $feed = array( 'sig' => $sig, 'xml' => $this->build_xml( $c, $items, $newest ), 'newest' => $newest, ); set_transient( self::CACHE, $feed, self::CACHE_TTL ); return $feed; } /** * The feed as RSS 2.0 with the Apple Podcasts tags. * * @param array $c From channel(). * @param array[] $items From items(). * @param int $newest Newest recording time, or 0. * @return string */ private function build_xml( $c, $items, $newest ) { $image = ( $c['image_id'] ? wp_get_attachment_image_url( $c['image_id'], 'full' ) : '' ); if ( !$image ) { $image = (string) get_site_icon_url( 1400 ); } $built = ( $newest ? $newest : time() ); $out = '' . "\n"; $out .= '' . "\n"; $out .= "\n"; $out .= '' . esc_xml( self::plain( $c['title'] ) ) . "\n"; $out .= '' . esc_xml( self::plain( home_url( '/' ) ) ) . "\n"; $out .= '' . "\n"; $out .= '' . esc_xml( self::plain( $c['description'] ) ) . "\n"; $out .= '' . esc_xml( self::plain( get_bloginfo( 'language' ) ) ) . "\n"; $out .= '' . esc_xml( self::plain( gmdate( 'D, d M Y H:i:s +0000', $built ) ) ) . "\n"; $out .= '' . esc_xml( self::plain( $c['author'] ) ) . "\n"; $out .= '' . esc_xml( self::plain( $c['description'] ) ) . "\n"; $out .= '' . esc_xml( self::plain( $c['author'] ) ) . ''; if ( '' !== $c['email'] ) { $out .= '' . esc_xml( self::plain( $c['email'] ) ) . ''; } $out .= "\n"; if ( $image ) { $out .= '' . "\n"; } $out .= '' . "\n"; $out .= '' . esc_xml( ( $c['explicit'] ? 'true' : 'false' ) ) . "\n"; $out .= "episodic\n"; foreach ( $items as $item ) { $out .= "\n"; $out .= '' . esc_xml( self::plain( $item['title'] ) ) . "\n"; $out .= '' . esc_xml( self::plain( $item['link'] ) ) . "\n"; $out .= '' . esc_xml( self::plain( $item['guid'] ) ) . "\n"; $out .= '' . esc_xml( self::plain( gmdate( 'D, d M Y H:i:s +0000', $item['date'] ) ) ) . "\n"; $out .= '' . esc_xml( self::plain( $item['description'] ) ) . "\n"; $out .= '' . "\n"; $out .= '' . esc_xml( self::format_hhmmss( $item['seconds'] ) ) . "\n"; if ( '' !== $item['image'] ) { $out .= '' . "\n"; } $out .= "full\n"; $out .= "\n"; } $out .= "\n\n"; return $out; } /** * Episode id that stays the same when the recording is made again, so podcast apps * update the episode instead of listing it twice. * * @param int $post_id Post ID. * @return string */ public static function guid( $post_id ) { // Saved on the post when the recording is made (see remember_guid()), so moving the // site to a new domain does not make every episode look new to podcast apps. $kept = (string) get_post_meta( (int) $post_id, self::META_GUID, true ); if ( '' !== $kept ) { return $kept; } $host = (string) wp_parse_url( home_url(), PHP_URL_HOST ); return 'aifi-audio-' . (int) $post_id . '-' . $host; } /** * Save the post's episode id the first time it gets a recording. Reading the feed never * writes anything. * * @param int $post_id Post ID. */ public static function remember_guid( $post_id ) { if ( '' === (string) get_post_meta( (int) $post_id, self::META_GUID, true ) ) { update_post_meta( (int) $post_id, self::META_GUID, self::guid( $post_id ) ); } } /** * Published posts with a recording that plays on the site, newest first. * Recordings hidden on their post and password-protected posts are left out, and * every one is left out while the players are switched off on the Audio tab. * * @param int $limit Most episodes. * @return array[] */ public function items( $limit ) { if ( !Providers::can_use_premium() ) { return array(); } return array(); } }