summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/modules/wordads')
-rw-r--r--plugins/jetpack/modules/wordads/css/style.css63
-rw-r--r--plugins/jetpack/modules/wordads/php/admin.php49
-rw-r--r--plugins/jetpack/modules/wordads/php/api.php143
-rw-r--r--plugins/jetpack/modules/wordads/php/cron.php48
-rw-r--r--plugins/jetpack/modules/wordads/php/networks/amazon.php3
-rw-r--r--plugins/jetpack/modules/wordads/php/params.php226
-rw-r--r--plugins/jetpack/modules/wordads/php/widgets.php115
-rw-r--r--plugins/jetpack/modules/wordads/wordads.php619
8 files changed, 1266 insertions, 0 deletions
diff --git a/plugins/jetpack/modules/wordads/css/style.css b/plugins/jetpack/modules/wordads/css/style.css
new file mode 100644
index 00000000..687334d6
--- /dev/null
+++ b/plugins/jetpack/modules/wordads/css/style.css
@@ -0,0 +1,63 @@
+/**
+ HTML markup structure of an ad:
+
+ <div class="wpcnt">
+ <div class="wpa [wpmrec|wpwidesky|wpleaderboard]">
+ <a class="wpa-about" href="http://wordpress.com/about-these-ads/" rel="nofollow">
+ About these ads
+ </a>
+ <div class="u">
+ [ad unit here]
+ </div>
+ </div>
+ </div>
+*/
+
+/* outer container */
+.wpcnt {
+ text-align: center;
+ line-height: 2;
+}
+
+/* inner container */
+.wpa {
+ position: relative;
+ overflow: hidden; /* this hides "about these ads" when there's no adfill */
+ display: inline-block;
+ max-width: 100%; /* important! this bit of CSS will *crop* any ad that's larger than the parent container! */
+}
+
+/* about these ads */
+.wpa-about {
+ position: absolute;
+ top: 5px;
+ left: 0;
+ right: 0;
+ display: block;
+ margin-top: 0;
+ color: #888;
+ font: 10px/1 "Open Sans", Arial, sans-serif !important;
+ text-align: left !important;
+ text-decoration: none !important;
+ opacity: 0.85;
+ border-bottom: none !important; /* some themes ad dotted underlines, that won't look nice */
+ box-shadow: none !important;
+}
+
+/* ad unit wrapper */
+.wpa .u>div { /* @todo: deprecate wpdvert */
+ display: block;
+ margin-top: 5px; /* this makes "about these ads" visible */
+ margin-bottom: 1em; /* every ad should have a little space below it */
+}
+
+div.wpa>div {
+ margin-top: 20px;
+}
+
+.wpa .u .adsbygoogle {
+ display: block;
+ margin-top: 17px; /* this makes "about these ads" visible */
+ margin-bottom: 1em; /* every ad should have a little space below it */
+ background-color: transparent;
+}
diff --git a/plugins/jetpack/modules/wordads/php/admin.php b/plugins/jetpack/modules/wordads/php/admin.php
new file mode 100644
index 00000000..b7071666
--- /dev/null
+++ b/plugins/jetpack/modules/wordads/php/admin.php
@@ -0,0 +1,49 @@
+<?php
+
+/**
+ * The standard set of admin pages for the user if Jetpack is installed
+ */
+class WordAds_Admin {
+
+ /**
+ * @since 4.5.0
+ */
+ function __construct() {
+ global $wordads;
+
+ if ( current_user_can( 'manage_options' ) && isset( $_GET['ads_debug'] ) ) {
+ WordAds_API::update_wordads_status_from_api();
+ add_action( 'admin_notices', array( $this, 'debug_output' ) );
+ }
+ }
+
+ /**
+ * Output the API connection debug
+ *
+ * @since 4.5.0
+ */
+ function debug_output() {
+ global $wordads, $wordads_status_response;
+ $response = $wordads_status_response;
+ if ( empty( $response ) ) {
+ $response = 'No response from API :(';
+ } else {
+ $response = print_r( $response, 1 );
+ }
+
+ $status = $wordads->option( 'wordads_approved' ) ?
+ '<span style="color:green;">Yes</span>' :
+ '<span style="color:red;">No</span>';
+
+ $type = $wordads->option( 'wordads_approved' ) ? 'updated' : 'error';
+ echo <<<HTML
+ <div class="notice $type is-dismissible">
+ <p>Status: $status</p>
+ <pre>$response</pre>
+ </div>
+HTML;
+ }
+}
+
+global $wordads_admin;
+$wordads_admin = new WordAds_Admin();
diff --git a/plugins/jetpack/modules/wordads/php/api.php b/plugins/jetpack/modules/wordads/php/api.php
new file mode 100644
index 00000000..34b5235a
--- /dev/null
+++ b/plugins/jetpack/modules/wordads/php/api.php
@@ -0,0 +1,143 @@
+<?php
+
+/**
+ * Methods for accessing data through the WPCOM REST API
+ *
+ * @since 4.5.0
+ */
+class WordAds_API {
+
+ private static $wordads_status = null;
+
+ /**
+ * Returns site's WordAds status
+ *
+ * @return array boolean values for 'approved' and 'active'
+ *
+ * @since 4.5.0
+ */
+ public static function get_wordads_status() {
+ global $wordads_status_response;
+ if ( Jetpack::is_development_mode() ) {
+ self::$wordads_status = array(
+ 'approved' => true,
+ 'active' => true,
+ 'house' => true,
+ 'unsafe' => false,
+ );
+
+ return self::$wordads_status;
+ }
+
+ $endpoint = sprintf( '/sites/%d/wordads/status', Jetpack::get_option( 'id' ) );
+ $wordads_status_response = $response = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint );
+ if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
+ return new WP_Error( 'api_error', __( 'Error connecting to API.', 'jetpack' ), $response );
+ }
+
+ $body = json_decode( wp_remote_retrieve_body( $response ) );
+ self::$wordads_status = array(
+ 'approved' => $body->approved,
+ 'active' => $body->active,
+ 'house' => $body->house,
+ 'unsafe' => $body->unsafe,
+ );
+
+ return self::$wordads_status;
+ }
+
+ /**
+ * Returns the ads.txt content needed to run WordAds.
+ *
+ * @return array string contents of the ads.txt file.
+ *
+ * @since 6.1.0
+ */
+ public static function get_wordads_ads_txt() {
+ $endpoint = sprintf( '/sites/%d/wordads/ads-txt', Jetpack::get_option( 'id' ) );
+ $wordads_status_response = $response = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint );
+ if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
+ return new WP_Error( 'api_error', __( 'Error connecting to API.', 'jetpack' ), $response );
+ }
+
+ $body = json_decode( wp_remote_retrieve_body( $response ) );
+ $ads_txt = str_replace( '\\n', PHP_EOL, $body->adstxt );
+ return $ads_txt;
+ }
+
+ /**
+ * Returns status of WordAds approval.
+ *
+ * @return boolean true if site is WordAds approved
+ *
+ * @since 4.5.0
+ */
+ public static function is_wordads_approved() {
+ if ( is_null( self::$wordads_status ) ) {
+ self::get_wordads_status();
+ }
+
+ return self::$wordads_status['approved'] ? '1' : '0';
+ }
+
+ /**
+ * Returns status of WordAds active.
+ *
+ * @return boolean true if ads are active on site
+ *
+ * @since 4.5.0
+ */
+ public static function is_wordads_active() {
+ if ( is_null( self::$wordads_status ) ) {
+ self::get_wordads_status();
+ }
+
+ return self::$wordads_status['active'] ? '1' : '0';
+ }
+
+ /**
+ * Returns status of WordAds house ads.
+ *
+ * @return boolean true if WP.com house ads should be shown
+ *
+ * @since 4.5.0
+ */
+ public static function is_wordads_house() {
+ if ( is_null( self::$wordads_status ) ) {
+ self::get_wordads_status();
+ }
+
+ return self::$wordads_status['house'] ? '1' : '0';
+ }
+
+
+ /**
+ * Returns whether or not this site is safe to run ads on.
+ *
+ * @return boolean true if ads shown not be shown on this site.
+ *
+ * @since 6.5.0
+ */
+ public static function is_wordads_unsafe() {
+ if ( is_null( self::$wordads_status ) ) {
+ self::get_wordads_status();
+ }
+
+ return self::$wordads_status['unsafe'] ? '1' : '0';
+ }
+
+ /**
+ * Grab WordAds status from WP.com API and store as option
+ *
+ * @since 4.5.0
+ */
+ static function update_wordads_status_from_api() {
+ $status = self::get_wordads_status();
+ if ( ! is_wp_error( $status ) ) {
+ update_option( 'wordads_approved', self::is_wordads_approved(), true );
+ update_option( 'wordads_active', self::is_wordads_active(), true );
+ update_option( 'wordads_house', self::is_wordads_house(), true );
+ update_option( 'wordads_unsafe', self::is_wordads_unsafe(), true );
+ }
+ }
+}
diff --git a/plugins/jetpack/modules/wordads/php/cron.php b/plugins/jetpack/modules/wordads/php/cron.php
new file mode 100644
index 00000000..88677e53
--- /dev/null
+++ b/plugins/jetpack/modules/wordads/php/cron.php
@@ -0,0 +1,48 @@
+<?php
+
+/**
+ * WordAds cron tasks
+ *
+ * @since 4.5.0
+ */
+class WordAds_Cron {
+
+ /**
+ * Add the actions the cron tasks will use
+ *
+ * @since 4.5.0
+ */
+ function __construct() {
+ add_action( 'wordads_cron_status', array( $this, 'update_wordads_status' ) );
+ }
+
+ /**
+ * Registered scheduled events on activation
+ *
+ * @since 4.5.0
+ */
+ static function activate() {
+ wp_schedule_event( time(), 'daily', 'wordads_cron_status' );
+ }
+
+ /**
+ * Clear scheduled hooks on deactivation
+ *
+ * @since 4.5.0
+ */
+ static function deactivate() {
+ wp_clear_scheduled_hook( 'wordads_cron_status' );
+ }
+
+ /**
+ * Grab WordAds status from WP.com API
+ *
+ * @since 4.5.0
+ */
+ static function update_wordads_status() {
+ WordAds_API::update_wordads_status_from_api();
+ }
+}
+
+global $wordads_cron;
+$wordads_cron = new WordAds_Cron();
diff --git a/plugins/jetpack/modules/wordads/php/networks/amazon.php b/plugins/jetpack/modules/wordads/php/networks/amazon.php
new file mode 100644
index 00000000..19f53fbe
--- /dev/null
+++ b/plugins/jetpack/modules/wordads/php/networks/amazon.php
@@ -0,0 +1,3 @@
+<?php
+
+// stub
diff --git a/plugins/jetpack/modules/wordads/php/params.php b/plugins/jetpack/modules/wordads/php/params.php
new file mode 100644
index 00000000..ef3f3326
--- /dev/null
+++ b/plugins/jetpack/modules/wordads/php/params.php
@@ -0,0 +1,226 @@
+<?php
+
+class WordAds_Params {
+
+ /**
+ * Setup parameters for serving the ads
+ *
+ * @since 4.5.0
+ */
+ public function __construct() {
+ // WordAds setting => default
+ $settings = array(
+ 'wordads_approved' => false,
+ 'wordads_active' => false,
+ 'wordads_house' => true,
+ 'wordads_unsafe' => false,
+ 'enable_header_ad' => true,
+ 'wordads_second_belowpost' => true,
+ 'wordads_display_front_page' => true,
+ 'wordads_display_post' => true,
+ 'wordads_display_page' => true,
+ 'wordads_display_archive' => true,
+ 'wordads_custom_adstxt' => '',
+ );
+
+ // grab settings, or set as default if it doesn't exist
+ $this->options = array();
+ foreach ( $settings as $setting => $default ) {
+ $option = get_option( $setting, null );
+
+ if ( is_null( $option ) ) {
+ update_option( $setting, $default, true );
+ $option = $default;
+ }
+
+ $this->options[ $setting ] = 'wordads_custom_adstxt' !== $setting ? (bool) $option : $option;
+ }
+
+ $host = 'localhost';
+ if ( isset( $_SERVER['HTTP_HOST'] ) ) {
+ $host = $_SERVER['HTTP_HOST'];
+ }
+
+ $this->url = ( is_ssl() ? 'https' : 'http' ) . '://' . $host . $_SERVER['REQUEST_URI'];
+ if ( ! ( false === strpos( $this->url, '?' ) ) && ! isset( $_GET['p'] ) ) {
+ $this->url = substr( $this->url, 0, strpos( $this->url, '?' ) );
+ }
+
+ $this->cloudflare = self::is_cloudflare();
+ $this->blog_id = Jetpack::get_option( 'id', 0 );
+ $this->mobile_device = jetpack_is_mobile( 'any', true );
+ $this->targeting_tags = array(
+ 'WordAds' => 1,
+ 'BlogId' => Jetpack::is_development_mode() ? 0 : Jetpack_Options::get_option( 'id' ),
+ 'Domain' => esc_js( parse_url( home_url(), PHP_URL_HOST ) ),
+ 'PageURL' => esc_js( $this->url ),
+ 'LangId' => false !== strpos( get_bloginfo( 'language' ), 'en' ) ? 1 : 0, // TODO something else?
+ 'AdSafe' => 1, // TODO
+ );
+ }
+
+ /**
+ * @return boolean true if the user is browsing on a mobile device (iPad not included)
+ *
+ * @since 4.5.0
+ */
+ public function is_mobile() {
+ return ! empty( $this->mobile_device );
+ }
+
+ /**
+ * @return boolean true if site is being served via CloudFlare
+ *
+ * @since 4.5.0
+ */
+ public static function is_cloudflare() {
+ if (
+ defined( 'WORDADS_CLOUDFLARE' )
+ || isset( $_SERVER['HTTP_CF_CONNECTING_IP'] )
+ || isset( $_SERVER['HTTP_CF_IPCOUNTRY'] )
+ || isset( $_SERVER['HTTP_CF_VISITOR'] )
+ ) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * @return boolean true if user is browsing in iOS device
+ *
+ * @since 4.5.0
+ */
+ public function is_ios() {
+ return in_array( $this->get_device(), array( 'ipad', 'iphone', 'ipod' ) );
+ }
+
+ /**
+ * Returns the user's device (see user-agent.php) or 'desktop'
+ *
+ * @return string user device
+ *
+ * @since 4.5.0
+ */
+ public function get_device() {
+ global $agent_info;
+
+ if ( ! empty( $this->mobile_device ) ) {
+ return $this->mobile_device;
+ }
+
+ if ( $agent_info->is_ipad() ) {
+ return 'ipad';
+ }
+
+ return 'desktop';
+ }
+
+ /**
+ * @return string The type of page that is being loaded
+ *
+ * @since 4.5.0
+ */
+ public function get_page_type() {
+ if ( ! empty( $this->page_type ) ) {
+ return $this->page_type;
+ }
+
+ if ( self::is_static_home() ) {
+ $this->page_type = 'static_home';
+ } elseif ( is_home() ) {
+ $this->page_type = 'home';
+ } elseif ( is_page() ) {
+ $this->page_type = 'page';
+ } elseif ( is_single() ) {
+ $this->page_type = 'post';
+ } elseif ( is_search() ) {
+ $this->page_type = 'search';
+ } elseif ( is_category() ) {
+ $this->page_type = 'category';
+ } elseif ( is_archive() ) {
+ $this->page_type = 'archive';
+ } else {
+ $this->page_type = 'wtf';
+ }
+
+ return $this->page_type;
+ }
+
+ /**
+ * @return int The page type code for ipw config
+ *
+ * @since 5.6.0
+ */
+ public function get_page_type_ipw() {
+ if ( ! empty( $this->page_type_ipw ) ) {
+ return $this->page_type_ipw;
+ }
+
+ $page_type_ipw = 6;
+ if ( self::is_static_home() || is_home() || is_front_page() ) {
+ $page_type_ipw = 0;
+ } elseif ( is_page() ) {
+ $page_type_ipw = 2;
+ } elseif ( is_singular() ) {
+ $page_type_ipw = 1;
+ } elseif ( is_search() ) {
+ $page_type_ipw = 4;
+ } elseif ( is_category() || is_tag() || is_archive() || is_author() ) {
+ $page_type_ipw = 3;
+ } elseif ( is_404() ) {
+ $page_type_ipw = 5;
+ }
+
+ $this->page_type_ipw = $page_type_ipw;
+ return $page_type_ipw;
+ }
+
+ /**
+ * Returns true if page is static home
+ *
+ * @return boolean true if page is static home
+ *
+ * @since 4.5.0
+ */
+ public static function is_static_home() {
+ return is_front_page() &&
+ 'page' == get_option( 'show_on_front' ) &&
+ get_option( 'page_on_front' );
+ }
+
+ /**
+ * Logic for if we should show an ad
+ *
+ * @since 4.5.0
+ */
+ public function should_show() {
+ global $wp_query;
+ if ( ( is_front_page() || is_home() ) && ! $this->options['wordads_display_front_page'] ) {
+ return false;
+ }
+
+ if ( is_single() && ! $this->options['wordads_display_post'] ) {
+ return false;
+ }
+
+ if ( is_page() && ! $this->options['wordads_display_page'] ) {
+ return false;
+ }
+
+ if ( ( is_archive() || is_search() ) && ! $this->options['wordads_display_archive'] ) {
+ return false;
+ }
+
+ if ( is_single() || ( is_page() && ! is_home() ) ) {
+ return true;
+ }
+
+ // TODO this would be a good place for allowing the user to specify
+ if ( ( is_home() || is_archive() || is_search() ) && 0 == $wp_query->current_post ) {
+ return true;
+ }
+
+ return false;
+ }
+}
diff --git a/plugins/jetpack/modules/wordads/php/widgets.php b/plugins/jetpack/modules/wordads/php/widgets.php
new file mode 100644
index 00000000..782c2dcd
--- /dev/null
+++ b/plugins/jetpack/modules/wordads/php/widgets.php
@@ -0,0 +1,115 @@
+<?php
+
+/**
+ * Widget for inserting an ad into your sidebar
+ *
+ * @since 4.5.0
+ */
+class WordAds_Sidebar_Widget extends WP_Widget {
+
+ private static $allowed_tags = array( 'mrec', 'wideskyscraper' );
+ private static $num_widgets = 0;
+
+ function __construct() {
+ parent::__construct(
+ 'wordads_sidebar_widget',
+ /** This filter is documented in modules/widgets/facebook-likebox.php */
+ apply_filters( 'jetpack_widget_name', 'Ads' ),
+ array(
+ 'description' => __( 'Insert an ad unit wherever you can place a widget.', 'jetpack' ),
+ 'customize_selective_refresh' => true,
+ )
+ );
+ }
+
+ public function widget( $args, $instance ) {
+ global $wordads;
+ if ( $wordads->should_bail() ) {
+ return false;
+ }
+
+ if ( ! isset( $instance['unit'] ) ) {
+ $instance['unit'] = 'mrec';
+ }
+
+ self::$num_widgets++;
+ $about = __( 'Advertisements', 'jetpack' );
+ $width = WordAds::$ad_tag_ids[ $instance['unit'] ]['width'];
+ $height = WordAds::$ad_tag_ids[ $instance['unit'] ]['height'];
+ $unit_id = 1 == self::$num_widgets ? 3 : self::$num_widgets + 3; // 2nd belowpost is '4'
+ $section_id = 0 === $wordads->params->blog_id ?
+ WORDADS_API_TEST_ID :
+ $wordads->params->blog_id . $unit_id;
+
+ $snippet = '';
+ if ( $wordads->option( 'wordads_house', true ) ) {
+ $unit = 'mrec';
+ if ( 'leaderboard' == $instance['unit'] && ! $this->params->mobile_device ) {
+ $unit = 'leaderboard';
+ } elseif ( 'wideskyscraper' == $instance['unit'] ) {
+ $unit = 'widesky';
+ }
+
+ $snippet = $wordads->get_house_ad( $unit );
+ } else {
+ $snippet = $wordads->get_ad_snippet( $section_id, $height, $width, 'widget' );
+ }
+
+ echo <<< HTML
+ <div class="wpcnt">
+ <div class="wpa">
+ <span class="wpa-about">$about</span>
+ <div class="u {$instance['unit']}">
+ $snippet
+ </div>
+ </div>
+ </div>
+HTML;
+ }
+
+ public function form( $instance ) {
+ // ad unit type
+ if ( isset( $instance['unit'] ) ) {
+ $unit = $instance['unit'];
+ } else {
+ $unit = 'mrec';
+ }
+ ?>
+ <p>
+ <label for="<?php echo esc_attr( $this->get_field_id( 'unit' ) ); ?>"><?php _e( 'Tag Dimensions:', 'jetpack' ); ?></label>
+ <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'unit' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'unit' ) ); ?>">
+ <?php
+ foreach ( WordAds::$ad_tag_ids as $ad_unit => $properties ) {
+ if ( ! in_array( $ad_unit, self::$allowed_tags ) ) {
+ continue;
+ }
+
+ $splits = explode( '_', $properties['tag'] );
+ $unit_pretty = "{$splits[0]} {$splits[1]}";
+ $selected = selected( $ad_unit, $unit, false );
+ echo "<option value='", esc_attr( $ad_unit ) ,"' ", $selected, '>', esc_html( $unit_pretty ) , '</option>';
+ }
+ ?>
+ </select>
+ </p>
+ <?php
+ }
+
+ public function update( $new_instance, $old_instance ) {
+ $instance = $old_instance;
+
+ if ( in_array( $new_instance['unit'], self::$allowed_tags ) ) {
+ $instance['unit'] = $new_instance['unit'];
+ } else {
+ $instance['unit'] = 'mrec';
+ }
+
+ return $instance;
+ }
+}
+
+function jetpack_wordads_widgets_init_callback() {
+ return register_widget( 'WordAds_Sidebar_Widget' );
+}
+
+add_action( 'widgets_init', 'jetpack_wordads_widgets_init_callback' );
diff --git a/plugins/jetpack/modules/wordads/wordads.php b/plugins/jetpack/modules/wordads/wordads.php
new file mode 100644
index 00000000..43a4e7d8
--- /dev/null
+++ b/plugins/jetpack/modules/wordads/wordads.php
@@ -0,0 +1,619 @@
+<?php
+
+define( 'WORDADS_ROOT', dirname( __FILE__ ) );
+define( 'WORDADS_BASENAME', plugin_basename( __FILE__ ) );
+define( 'WORDADS_FILE_PATH', WORDADS_ROOT . '/' . basename( __FILE__ ) );
+define( 'WORDADS_URL', plugins_url( '/', __FILE__ ) );
+define( 'WORDADS_API_TEST_ID', '26942' );
+define( 'WORDADS_API_TEST_ID2', '114160' );
+
+require_once WORDADS_ROOT . '/php/widgets.php';
+require_once WORDADS_ROOT . '/php/api.php';
+require_once WORDADS_ROOT . '/php/cron.php';
+
+class WordAds {
+
+ public $params = null;
+
+ public $ads = array();
+
+ /**
+ * Array of supported ad types.
+ *
+ * @var array
+ */
+ public static $ad_tag_ids = array(
+ 'mrec' => array(
+ 'tag' => '300x250_mediumrectangle',
+ 'height' => '250',
+ 'width' => '300',
+ ),
+ 'leaderboard' => array(
+ 'tag' => '728x90_leaderboard',
+ 'height' => '90',
+ 'width' => '728',
+ ),
+ 'mobile_leaderboard' => array(
+ 'tag' => '320x50_mobileleaderboard',
+ 'height' => '50',
+ 'width' => '320',
+ ),
+ 'wideskyscraper' => array(
+ 'tag' => '160x600_wideskyscraper',
+ 'height' => '600',
+ 'width' => '160',
+ ),
+ );
+
+ /**
+ * Mapping array of location slugs to placement ids
+ *
+ * @var array
+ */
+ public static $ad_location_ids = array(
+ 'top' => 110,
+ 'belowpost' => 120,
+ 'belowpost2' => 130,
+ 'sidebar' => 140,
+ 'widget' => 150,
+ 'gutenberg' => 200,
+ 'inline' => 310,
+ 'inline-plugin' => 320,
+ );
+
+ public static $SOLO_UNIT_CSS = 'float:left;margin-right:5px;margin-top:0px;';
+
+ /**
+ * Convenience function for grabbing options from params->options
+ *
+ * @param string $option the option to grab
+ * @param mixed $default (optional)
+ * @return option or $default if not set
+ *
+ * @since 4.5.0
+ */
+ function option( $option, $default = false ) {
+ if ( ! isset( $this->params->options[ $option ] ) ) {
+ return $default;
+ }
+
+ return $this->params->options[ $option ];
+ }
+
+ /**
+ * Returns the ad tag property array for supported ad types.
+ * @return array array with ad tags
+ *
+ * @since 7.1.0
+ */
+ function get_ad_tags() {
+ return self::$ad_tag_ids;
+ }
+
+ /**
+ * Returns the solo css for unit
+ * @return string the special css for solo units
+ *
+ * @since 7.1.0
+ */
+ function get_solo_unit_css() {
+ return self::$SOLO_UNIT_CSS;
+ }
+
+ /**
+ * Instantiate the plugin
+ *
+ * @since 4.5.0
+ */
+ function __construct() {
+ add_action( 'init', array( $this, 'init' ) );
+ }
+
+ /**
+ * Code to run on WordPress 'init' hook
+ *
+ * @since 4.5.0
+ */
+ function init() {
+ require_once WORDADS_ROOT . '/php/params.php';
+ $this->params = new WordAds_Params();
+
+ if ( $this->should_bail() || self::is_infinite_scroll() ) {
+ return;
+ }
+
+ if ( is_admin() ) {
+ require_once WORDADS_ROOT . '/php/admin.php';
+ return;
+ }
+
+ $this->insert_adcode();
+
+ if ( '/ads.txt' === $_SERVER['REQUEST_URI'] ) {
+
+ if ( false === ( $ads_txt_transient = get_transient( 'jetpack_ads_txt' ) ) ) {
+ $ads_txt_transient = ! is_wp_error( WordAds_API::get_wordads_ads_txt() ) ? WordAds_API::get_wordads_ads_txt() : '';
+ set_transient( 'jetpack_ads_txt', $ads_txt_transient, DAY_IN_SECONDS );
+ }
+
+ /**
+ * Provide plugins a way of modifying the contents of the automatically-generated ads.txt file.
+ *
+ * @module wordads
+ *
+ * @since 6.1.0
+ *
+ * @param string WordAds_API::get_wordads_ads_txt() The contents of the ads.txt file.
+ */
+ $ads_txt_content = apply_filters( 'wordads_ads_txt', $ads_txt_transient );
+
+ header( 'Content-Type: text/plain; charset=utf-8' );
+ echo esc_html( $ads_txt_content );
+ die();
+ }
+ }
+
+ /**
+ * Check for Jetpack's The_Neverending_Home_Page and use got_infinity
+ *
+ * @return boolean true if load came from infinite scroll
+ *
+ * @since 4.5.0
+ */
+ public static function is_infinite_scroll() {
+ return class_exists( 'The_Neverending_Home_Page' ) && The_Neverending_Home_Page::got_infinity();
+ }
+
+ /**
+ * Add the actions/filters to insert the ads. Checks for mobile or desktop.
+ *
+ * @since 4.5.0
+ */
+ private function insert_adcode() {
+ add_action( 'wp_head', array( $this, 'insert_head_meta' ), 20 );
+ add_action( 'wp_head', array( $this, 'insert_head_iponweb' ), 30 );
+ add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
+ add_filter( 'wordads_ads_txt', array( $this, 'insert_custom_adstxt' ) );
+
+ /**
+ * Filters enabling ads in `the_content` filter
+ *
+ * @see https://jetpack.com/support/ads/
+ *
+ * @module wordads
+ *
+ * @since 5.8.0
+ *
+ * @param bool True to disable ads in `the_content`
+ */
+ if ( ! apply_filters( 'wordads_content_disable', false ) ) {
+ add_filter( 'the_content', array( $this, 'insert_ad' ) );
+ }
+
+ /**
+ * Filters enabling ads in `the_excerpt` filter
+ *
+ * @see https://jetpack.com/support/ads/
+ *
+ * @module wordads
+ *
+ * @since 5.8.0
+ *
+ * @param bool True to disable ads in `the_excerpt`
+ */
+ if ( ! apply_filters( 'wordads_excerpt_disable', false ) ) {
+ add_filter( 'the_excerpt', array( $this, 'insert_ad' ) );
+ }
+
+ if ( $this->option( 'enable_header_ad', true ) ) {
+ switch ( get_stylesheet() ) {
+ case 'twentyseventeen':
+ case 'twentyfifteen':
+ case 'twentyfourteen':
+ add_action( 'wp_footer', array( $this, 'insert_header_ad_special' ) );
+ break;
+ default:
+ add_action( 'wp_head', array( $this, 'insert_header_ad' ), 100 );
+ break;
+ }
+ }
+ }
+
+ /**
+ * Register desktop scripts and styles
+ *
+ * @since 4.5.0
+ */
+ function enqueue_scripts() {
+ wp_enqueue_style(
+ 'wordads',
+ WORDADS_URL . 'css/style.css',
+ array(),
+ '2015-12-18'
+ );
+ }
+
+ /**
+ * IPONWEB metadata used by the various scripts
+ *
+ * @return [type] [description]
+ */
+ function insert_head_meta() {
+ $themename = esc_js( get_stylesheet() );
+ $pagetype = intval( $this->params->get_page_type_ipw() );
+ $data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
+ $site_id = $this->params->blog_id;
+ $consent = intval( isset( $_COOKIE['personalized-ads-consent'] ) );
+ echo <<<HTML
+ <script$data_tags type="text/javascript">
+ var __ATA_PP = { pt: $pagetype, ht: 2, tn: '$themename', amp: false, siteid: $site_id, consent: $consent };
+ var __ATA = __ATA || {};
+ __ATA.cmd = __ATA.cmd || [];
+ __ATA.criteo = __ATA.criteo || {};
+ __ATA.criteo.cmd = __ATA.criteo.cmd || [];
+ </script>
+HTML;
+ }
+
+ /**
+ * IPONWEB scripts in <head>
+ *
+ * @since 4.5.0
+ */
+ function insert_head_iponweb() {
+ $data_tags = ( $this->params->cloudflare ) ? ' data-cfasync="false"' : '';
+ echo <<<HTML
+ <link rel='dns-prefetch' href='//s.pubmine.com' />
+ <link rel='dns-prefetch' href='//x.bidswitch.net' />
+ <link rel='dns-prefetch' href='//static.criteo.net' />
+ <link rel='dns-prefetch' href='//ib.adnxs.com' />
+ <link rel='dns-prefetch' href='//aax.amazon-adsystem.com' />
+ <link rel='dns-prefetch' href='//bidder.criteo.com' />
+ <link rel='dns-prefetch' href='//cas.criteo.com' />
+ <link rel='dns-prefetch' href='//gum.criteo.com' />
+ <link rel='dns-prefetch' href='//ads.pubmatic.com' />
+ <link rel='dns-prefetch' href='//gads.pubmatic.com' />
+ <link rel='dns-prefetch' href='//tpc.googlesyndication.com' />
+ <link rel='dns-prefetch' href='//ad.doubleclick.net' />
+ <link rel='dns-prefetch' href='//googleads.g.doubleclick.net' />
+ <link rel='dns-prefetch' href='//www.googletagservices.com' />
+ <script$data_tags async type="text/javascript" src="//s.pubmine.com/head.js"></script>
+HTML;
+ }
+
+ /**
+ * Insert the ad onto the page
+ *
+ * @since 4.5.0
+ */
+ function insert_ad( $content ) {
+ // Don't insert ads in feeds, or for anything but the main display. (This is required for compatibility with the Publicize module).
+ if ( is_feed() || ! is_main_query() || ! in_the_loop() ) {
+ return $content;
+ }
+ /**
+ * Allow third-party tools to disable the display of in post ads.
+ *
+ * @module wordads
+ *
+ * @since 4.5.0
+ *
+ * @param bool true Should the in post unit be disabled. Default to false.
+ */
+ $disable = apply_filters( 'wordads_inpost_disable', false );
+ if ( ! $this->params->should_show() || $disable ) {
+ return $content;
+ }
+
+ $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
+ return $content . $this->get_ad( 'belowpost', $ad_type );
+ }
+
+ /**
+ * Insert an inline ad into a post content
+ * Used for rendering the `wordads` shortcode.
+ *
+ * @since 6.1.0
+ */
+ function insert_inline_ad( $content ) {
+ // Ad JS won't work in XML feeds.
+ if ( is_feed() ) {
+ return $content;
+ }
+ /**
+ * Allow third-party tools to disable the display of in post ads.
+ *
+ * @module wordads
+ *
+ * @since 4.5.0
+ *
+ * @param bool true Should the in post unit be disabled. Default to false.
+ */
+ $disable = apply_filters( 'wordads_inpost_disable', false );
+ if ( $disable ) {
+ return $content;
+ }
+
+ $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
+ $content .= $this->get_ad( 'inline', $ad_type );
+ return $content;
+ }
+
+ /**
+ * Inserts ad into header
+ *
+ * @since 4.5.0
+ */
+ function insert_header_ad() {
+ /**
+ * Allow third-party tools to disable the display of header ads.
+ *
+ * @module wordads
+ *
+ * @since 4.5.0
+ *
+ * @param bool true Should the header unit be disabled. Default to false.
+ */
+ if ( apply_filters( 'wordads_header_disable', false ) ) {
+ return;
+ }
+
+ $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
+ echo $this->get_ad( 'top', $ad_type );
+ }
+
+ /**
+ * Special cases for inserting header unit via jQuery
+ *
+ * @since 4.5.0
+ */
+ function insert_header_ad_special() {
+ /**
+ * Allow third-party tools to disable the display of header ads.
+ *
+ * @module wordads
+ *
+ * @since 4.5.0
+ *
+ * @param bool true Should the header unit be disabled. Default to false.
+ */
+ if ( apply_filters( 'wordads_header_disable', false ) ) {
+ return;
+ }
+
+ $selector = '#content';
+ switch ( get_stylesheet() ) {
+ case 'twentyseventeen':
+ $selector = '#content';
+ break;
+ case 'twentyfifteen':
+ $selector = '#main';
+ break;
+ case 'twentyfourteen':
+ $selector = 'article:first';
+ break;
+ }
+
+ $ad_type = $this->option( 'wordads_house' ) ? 'house' : 'iponweb';
+ echo $this->get_ad( 'top', $ad_type );
+ echo <<<HTML
+ <script type="text/javascript">
+ jQuery('.wpcnt-header').insertBefore('$selector');
+ </script>
+HTML;
+ }
+
+ /**
+ * Filter the latest ads.txt to include custom user entries. Strips any tags or whitespace.
+ *
+ * @param string $adstxt The ads.txt being filtered
+ * @return string Filtered ads.txt with custom entries, if applicable
+ *
+ * @since 6.5.0
+ */
+ function insert_custom_adstxt( $adstxt ) {
+ $custom_adstxt = trim( wp_strip_all_tags( $this->option( 'wordads_custom_adstxt' ) ) );
+ if ( $custom_adstxt ) {
+ $adstxt .= "\n\n#Jetpack - User Custom Entries\n";
+ $adstxt .= $custom_adstxt . "\n";
+ }
+
+ return $adstxt;
+ }
+
+ /**
+ * Get the ad for the spot and type.
+ *
+ * @param string $spot top, side, inline, or belowpost
+ * @param string $type iponweb or adsense
+ */
+ function get_ad( $spot, $type = 'iponweb' ) {
+ $snippet = '';
+ if ( 'iponweb' == $type ) {
+ // Default to mrec
+ $width = 300;
+ $height = 250;
+
+ $section_id = WORDADS_API_TEST_ID;
+ $second_belowpost = '';
+ $snippet = '';
+ if ( 'top' == $spot ) {
+ // mrec for mobile, leaderboard for desktop
+ $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '2';
+ $width = $this->params->mobile_device ? 300 : 728;
+ $height = $this->params->mobile_device ? 250 : 90;
+ $snippet = $this->get_ad_snippet( $section_id, $height, $width, $spot );
+ } elseif ( 'belowpost' == $spot ) {
+ $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '1';
+ $width = 300;
+ $height = 250;
+
+ $snippet = $this->get_ad_snippet( $section_id, $height, $width, $spot, self::$SOLO_UNIT_CSS );
+ if ( $this->option( 'wordads_second_belowpost', true ) ) {
+ $section_id2 = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID2 : $this->params->blog_id . '4';
+ $snippet .= $this->get_ad_snippet( $section_id2, $height, $width, $spot . '2', 'float:left;margin-top:0px;' );
+ }
+ } elseif ( 'inline' === $spot ) {
+ $section_id = 0 === $this->params->blog_id ? WORDADS_API_TEST_ID : $this->params->blog_id . '5';
+ $snippet = $this->get_ad_snippet( $section_id, $height, $width, $spot, self::$SOLO_UNIT_CSS );
+ }
+ } elseif ( 'house' == $type ) {
+ $leaderboard = 'top' == $spot && ! $this->params->mobile_device;
+ $snippet = $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
+ if ( 'belowpost' == $spot && $this->option( 'wordads_second_belowpost', true ) ) {
+ $snippet .= $this->get_house_ad( $leaderboard ? 'leaderboard' : 'mrec' );
+ }
+ }
+
+ return $this->get_ad_div( $spot, $snippet );
+ }
+
+
+ /**
+ * Returns the snippet to be inserted into the ad unit
+ *
+ * @param int $section_id
+ * @param int $height
+ * @param int $width
+ * @param int $location
+ * @param string $css
+ * @return string
+ *
+ * @since 5.7
+ */
+ function get_ad_snippet( $section_id, $height, $width, $location = '', $css = '' ) {
+ $this->ads[] = array(
+ 'location' => $location,
+ 'width' => $width,
+ 'height' => $height,
+ );
+ $ad_number = count( $this->ads ) . '-' . uniqid();
+
+ $data_tags = $this->params->cloudflare ? ' data-cfasync="false"' : '';
+ $css = esc_attr( $css );
+
+ $loc_id = 100;
+ if ( ! empty( self::$ad_location_ids[ $location ] ) ) {
+ $loc_id = self::$ad_location_ids[ $location ];
+ }
+
+ return <<<HTML
+ <div style="padding-bottom:15px;width:{$width}px;height:{$height}px;$css">
+ <div id="atatags-{$ad_number}">
+ <script$data_tags type="text/javascript">
+ __ATA.cmd.push(function() {
+ __ATA.initSlot('atatags-{$ad_number}', {
+ collapseEmpty: 'before',
+ sectionId: '{$section_id}',
+ location: {$loc_id},
+ width: {$width},
+ height: {$height}
+ });
+ });
+ </script>
+ </div>
+ </div>
+HTML;
+ }
+
+ /**
+ * Returns the complete ad div with snippet to be inserted into the page
+ *
+ * @param string $spot top, side, inline, or belowpost
+ * @param string $snippet The snippet to insert into the div
+ * @param array $css_classes
+ * @return string The supporting ad unit div
+ *
+ * @since 7.1
+ */
+ function get_ad_div( $spot, $snippet, array $css_classes = array() ) {
+ if ( empty( $css_classes ) ) {
+ $css_classes = array();
+ }
+
+ $css_classes[] = 'wpcnt';
+ if ( 'top' == $spot ) {
+ $css_classes[] = 'wpcnt-header';
+ }
+
+ $spot = esc_attr( $spot );
+ $classes = esc_attr( implode( ' ', $css_classes ) );
+ $about = esc_html__( 'Advertisements', 'jetpack' );
+ return <<<HTML
+ <div class="$classes">
+ <div class="wpa">
+ <span class="wpa-about">$about</span>
+ <div class="u $spot">
+ $snippet
+ </div>
+ </div>
+ </div>
+HTML;
+ }
+
+ /**
+ * Check the reasons to bail before we attempt to insert ads.
+ *
+ * @return true if we should bail (don't insert ads)
+ *
+ * @since 4.5.0
+ */
+ public function should_bail() {
+ return ! $this->option( 'wordads_approved' ) || (bool) $this->option( 'wordads_unsafe' );
+ }
+
+ /**
+ * Returns markup for HTML5 house ad base on unit
+ *
+ * @param string $unit mrec, widesky, or leaderboard
+ * @return string markup for HTML5 house ad
+ *
+ * @since 4.7.0
+ */
+ public function get_house_ad( $unit = 'mrec' ) {
+
+ switch ( $unit ) {
+ case 'widesky':
+ $width = 160;
+ $height = 600;
+ break;
+ case 'leaderboard':
+ $width = 728;
+ $height = 90;
+ break;
+ case 'mrec':
+ default:
+ $width = 300;
+ $height = 250;
+ break;
+ }
+
+ return <<<HTML
+ <iframe
+ src="https://s0.wp.com/wp-content/blog-plugins/wordads/house/html5/$unit/index.html"
+ width="$width"
+ height="$height"
+ frameborder="0"
+ scrolling="no"
+ marginheight="0"
+ marginwidth="0">
+ </iframe>
+HTML;
+ }
+
+ /**
+ * Activation hook actions
+ *
+ * @since 4.5.0
+ */
+ public static function activate() {
+ WordAds_API::update_wordads_status_from_api();
+ }
+}
+
+add_action( 'jetpack_activate_module_wordads', array( 'WordAds', 'activate' ) );
+add_action( 'jetpack_activate_module_wordads', array( 'WordAds_Cron', 'activate' ) );
+add_action( 'jetpack_deactivate_module_wordads', array( 'WordAds_Cron', 'deactivate' ) );
+
+global $wordads;
+$wordads = new WordAds();