diff options
Diffstat (limited to 'plugins/jetpack/modules/wordads/php')
-rw-r--r-- | plugins/jetpack/modules/wordads/php/admin.php | 49 | ||||
-rw-r--r-- | plugins/jetpack/modules/wordads/php/api.php | 143 | ||||
-rw-r--r-- | plugins/jetpack/modules/wordads/php/cron.php | 48 | ||||
-rw-r--r-- | plugins/jetpack/modules/wordads/php/networks/amazon.php | 3 | ||||
-rw-r--r-- | plugins/jetpack/modules/wordads/php/params.php | 226 | ||||
-rw-r--r-- | plugins/jetpack/modules/wordads/php/widgets.php | 115 |
6 files changed, 584 insertions, 0 deletions
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' ); |