'', 'host' => 'web.adblade.com', 'protocol' => 'http', 'type' => 2, 'width' => 1, 'height' => 1, ), $atts ); // Do nothing if no container id was set. if ( empty( $a['container_id'] ) ) { return ''; } return sprintf( '', $a['container_id'], $a['host'], $a['protocol'], $a['width'], $a['height'], $a['type'], $a['protocol'], $a['host'] ); } add_shortcode( 'adblade', 'adblade_shortcode' ); /** * Replace "[AdsWithin] with a specified ad tag * @param array $atts - The short code attribures. * @return The replacement text */ function adblade_ads_within_shortcode( $atts ) { $options = get_option( 'adblade_options' ); if ( array_key_exists( 'adsWithinTag', $options ) && ! empty( $options['adsWithinTag'] ) ) { return $options['adsWithinTag']; } return ''; } add_shortcode( 'AdsWithin', 'adblade_ads_within_shortcode' ); /** * Filter posts so we can add tags before and after the content. * @param string $content The content of the post. * @return filtered content */ function adblade_content_filter( $content ) { if ( ! is_single() ) { return $content; } $options = get_option( 'adblade_options' ); $beforeTag = ''; $afterTag = ''; if ( array_key_exists( 'beforePostTag', $options ) && ! empty( $options['beforePostTag'] ) ) { $beforeTag = stripslashes( $options['beforePostTag'] ); } if ( array_key_exists( 'afterPostTag', $options ) && ! empty( $options['afterPostTag'] ) ) { $afterTag = stripslashes( $options['afterPostTag'] ); } return $beforeTag . $content . $afterTag; } add_filter( 'the_content', 'adblade_content_filter' ); /** * Add scripts to page. */ function adblade_enqueue_scripts() { // These should not be added until after jQuery load. wp_enqueue_script( 'blockadblock', plugins_url() . '/adblade-publisher-tools/js/blockadblock.js', array( 'jquery' ) ); wp_enqueue_script( 'log-ab', plugins_url() . '/adblade-publisher-tools/js/log-ab.php', array( 'blockadblock', 'jquery' ) ); } add_action( 'wp_enqueue_scripts', 'adblade_enqueue_scripts' ); /** * Make requests to the adblade servers. * @param string $host The host the request is going to. * @param string $path The path of the URL. * @param boolean $skipCache Whether or not we should skip the cache. */ function adblade_proxy( $host, $path, $skipCache = false ) { // Make sure it is an adblade domain. $validDomains = array( 'web.adblade.com', 'web.industrybrains.com', 'static.adblade.com', 'staticd.cdn.adblade.com', 'static.industrybrains.com', 'staticd.cdn.industrybrains.com', ); if ( ! in_array( $host, $validDomains ) ) { return; } // Make sure the request matches one we are familiar with. $requestMatch = false; $validRequestPatterns = array( '#^/banners/images/\d+x\d+/.*\.[a-z]{3,4}$#', '#^/css/.*css$#', '#^/impsc?.php#', '#^/js/ads/async/show.js$#', ); foreach ( $validRequestPatterns as $pattern ) { if ( preg_match( $pattern, $path ) ) { $requestMatch = true; break; } } if ( ! $requestMatch ) { return; } $url = sprintf( 'http://%s%s', $host, $path ); /** * A callback function to proxy requests. * @param $url * @return mixed (array|false) false if there was an error */ $proxy = function ( $url ) { $bypassUrl = plugins_url() . '/adblade-publisher-tools/bypass.php?path='; $defaultDisclosure = 'Advertisement'; $replacements = array( 'http://staticd.cdn.adblade.com' => $bypassUrl, 'https://staticd.cdn.adblade.com' => $bypassUrl, 'ad_type_1' => 'blade_type_1', 'Ads by Adblade' => $defaultDisclosure, 'clicks.php?' => sprintf( 'clicks.php?%s=1&', ADBLADE_URL_PARAM ), '_common_dz.css' => $bypassUrl . '/css/zones/_common_dz.css', ); // Make the request. $response = wp_safe_remote_get( $url, array( 'user-agent' => 'Adblade WordPress Plugin', 'headers' => array( 'X-Forwarded-For' => array_key_exists( 'HTTP_X_FORWARDED_FOR', $_SERVER ) ? filter_input( INPUT_SERVER, 'HTTP_X_FORWARDED_FOR' ) : filter_input( INPUT_SERVER, 'REMOTE_ADDR' ), ), 'cookies' => array( '__tuid' => '6198487235506032324', ), ) ); if ( is_wp_error( $response ) ) { header( 'HTTP/1.0 500 Internal Server Error' ); echo $response->get_error_message(); return false; } $result = array( // Replace anything that an ad blocker might not like. 'body' => str_replace( array_keys( $replacements ), array_values( $replacements ), $response['body'] ), ); // Add the content type header, if one was sent from adblade. if ( array_key_exists( 'content-type', $response['headers'] ) ) { $result['headers'] = array( 'content-type' => $response['headers']['content-type'], ); } return $result; }; $responseHandler = function ( $response ) { if ( $response ) { if ( array_key_exists( 'headers', $response ) && array_key_exists( 'content-type', $response['headers'] ) ) { header( 'Content-type: ' . $response['headers']['content-type'] ); } echo $response['body']; return; } }; if ( $skipCache ) { $response = $proxy($url); $responseHandler($response); return; } else { // This is a static call, use a cache. header( 'Cache-Control: public' ); header( sprintf( 'Expires: %s GMT', gmdate( 'D, d M Y H:i:s', time() + HOUR_IN_SECONDS ) ) ); $transient = get_transient( $url ); if ( ! $transient ) { // Get transient and set it. $response = $proxy($url); if ( ! $response ) { set_transient( $url, $response, MINUTE_IN_SECONDS ); } $responseHandler($response); return; } else { $responseHandler($transient); return; } } }