filter_custom_css( $value, $setting->stylesheet ); } /** * Filter `wp_get_custom_css`. * * @param string $css * @param string $stylesheet * * @return string */ public function filter_wp_get_custom_css( $css, $stylesheet ) { return $this->filter_custom_css( $css, $stylesheet ); } /** * Filter post data. * * @param WP_Post $post */ public function filter_post_data( $post ) { global $pages; $cache = $this->get_post_cache( $post->ID ); $to_cache = array(); if ( is_array( $pages ) && 1 === count( $pages ) && ! empty( $pages[0] ) ) { // Post already filtered and available on global $page array, continue $post->post_content = $pages[0]; } else { $post->post_content = $this->process_content( $post->post_content, $cache, $to_cache ); } $post->post_excerpt = $this->process_content( $post->post_excerpt, $cache, $to_cache ); $this->maybe_update_post_cache( $to_cache ); } /** * Filter content pagination. * * @param array $pages * * @return array */ public function filter_content_pagination( $pages ) { $cache = $this->get_post_cache(); $to_cache = array(); foreach ( $pages as $key => $page ) { $pages[ $key ] = $this->process_content( $page, $cache, $to_cache ); } $this->maybe_update_post_cache( $to_cache ); return $pages; } /** * Filter widget display. * * @param array $instance * @param WP_Widget $class * * @return array */ public function filter_widget_display( $instance, $class ) { return $this->handle_widget( $instance, $class ); } /** * Does URL need replacing? * * @param string $url * * @return bool */ protected function url_needs_replacing( $url ) { if ( str_replace( $this->get_bare_upload_base_urls(), '', $url ) === $url ) { // Remote URL, no replacement needed return false; } // Local URL, perform replacement return true; } /** * Get an array of bare base_urls that can be used for uploaded items. * * @return array */ private function get_bare_upload_base_urls() { static $base_urls = array(); if ( empty( $base_urls ) ) { $domains = array(); // Original domain and path. $uploads = wp_upload_dir(); $base_url = AS3CF_Utils::remove_scheme( $uploads['baseurl'] ); $orig_domain = AS3CF_Utils::parse_url( $base_url, PHP_URL_HOST ); $domains[] = $orig_domain; $base_urls[] = $base_url; // Current domain and path after potential domain mapping. $base_url = $this->as3cf->maybe_fix_local_subsite_url( $uploads['baseurl'] ); $base_url = AS3CF_Utils::remove_scheme( $base_url ); $curr_domain = AS3CF_Utils::parse_url( $base_url, PHP_URL_HOST ); if ( $curr_domain !== $orig_domain ) { $domains[] = $curr_domain; } /** * Allow alteration of the local domains that can be matched on. * * @param array $domains */ $domains = apply_filters( 'as3cf_local_domains', $domains ); if ( ! empty( $domains ) ) { foreach ( array_unique( $domains ) as $match_domain ) { $base_urls[] = substr_replace( $base_url, $match_domain, 2, strlen( $curr_domain ) ); } } } return $base_urls; } /** * Get URL * * @param int $attachment_id * @param null|string $size * * @return bool|string */ protected function get_url( $attachment_id, $size = null ) { return $this->as3cf->get_attachment_url( $attachment_id, null, $size ); } /** * Get base URL. * * @param int $attachment_id * * @return string|false */ protected function get_base_url( $attachment_id ) { return $this->as3cf->get_attachment_local_url( $attachment_id ); } /** * Get attachment ID from URL. * * @param string $url * * @return bool|int */ protected function get_attachment_id_from_url( $url ) { global $wpdb; $full_url = AS3CF_Utils::remove_scheme( AS3CF_Utils::remove_size_from_filename( $url ) ); if ( isset( $this->query_cache[ $full_url ] ) ) { // ID already cached, return return $this->query_cache[ $full_url ]; } $path = $this->as3cf->decode_filename_in_path( ltrim( str_replace( $this->get_bare_upload_base_urls(), '', $full_url ), '/' ) ); $sql = $wpdb->prepare( " SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s ", '_wp_attached_file', $path ); $result = $wpdb->get_var( $sql ); if ( is_null( $result ) ) { // Attachment ID not found, return false $this->query_cache[ $full_url ] = false; return false; } $this->query_cache[ $full_url ] = (int) $result; return (int) $result; } /** * Get attachment IDs from URLs. * * @param array $urls * * @return array url => attachment ID (or false) */ protected function get_attachment_ids_from_urls( $urls ) { global $wpdb; $results = array(); if ( empty( $urls ) ) { return $results; } if ( ! is_array( $urls ) ) { $urls = array( $urls ); } $paths = array(); $full_urls = array(); foreach ( $urls as $url ) { $full_url = AS3CF_Utils::remove_scheme( AS3CF_Utils::remove_size_from_filename( $url ) ); if ( isset( $this->query_cache[ $full_url ] ) ) { // ID already cached, use it. $results[ $url ] = $this->query_cache[ $full_url ]; continue; } $path = $this->as3cf->decode_filename_in_path( ltrim( str_replace( $this->get_bare_upload_base_urls(), '', $full_url ), '/' ) ); $paths[ $path ] = $full_url; $full_urls[ $full_url ][] = $url; $meta_values[] = "'" . esc_sql( $path ) . "'"; } if ( ! empty( $meta_values ) ) { $sql = " SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attached_file' AND meta_value IN ( " . implode( ',', array_unique( $meta_values ) ) . " ) "; $query_results = $wpdb->get_results( $sql ); if ( ! empty( $query_results ) ) { foreach ( $query_results as $postmeta ) { $attachment_id = (int) $postmeta->post_id; $full_url = $paths[ $postmeta->meta_value ]; $this->query_cache[ $full_url ] = $attachment_id; foreach ( $full_urls[ $full_url ] as $url ) { $results[ $url ] = $attachment_id; } unset( $full_urls[ $full_url ] ); } } // No more attachment IDs found, set remaining results as false. if ( count( $urls ) !== count( $results ) ) { foreach ( $full_urls as $full_url => $sizes ) { foreach ( $sizes as $url ) { if ( ! array_key_exists( $url, $results ) ) { $this->query_cache[ $full_url ] = false; $results[ $url ] = false; } } } } } return $results; } /** * Normalize find value. * * @param string $url * * @return string */ protected function normalize_find_value( $url ) { return $this->as3cf->decode_filename_in_path( $url ); } /** * Normalize replace value. * * @param string $url * * @return string */ protected function normalize_replace_value( $url ) { return $this->as3cf->encode_filename_in_path( $url ); } /** * Post process content. * * @param string $content * * @return string */ protected function post_process_content( $content ) { return $content; } /** * Pre replace content. * * @param string $content * * @return string */ protected function pre_replace_content( $content ) { $uploads = wp_upload_dir(); $base_url = AS3CF_Utils::remove_scheme( $uploads['baseurl'] ); return $this->remove_aws_query_strings( $content, $base_url ); } /** * Each time a URL is replaced this function is called to allow for logging or further updates etc. * * @param string $find URL with no scheme. * @param string $replace URL with no scheme. * @param string $content * * @return string */ protected function url_replaced( $find, $replace, $content ) { if ( (bool) $this->as3cf->get_setting( 'force-https' ) ) { $content = str_replace( 'http:' . $replace, 'https:' . $replace, $content ); } return $content; } }