| @@ -6,9 +6,11 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | use Automattic\Jetpack\Assets; |
| 9 | 9 | |
| 10 | -// phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedScript | |
| 10 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | + exit( 0 ); | |
| 12 | +} | |
| 11 | 13 | |
| 12 | 14 | require_once WORDADS_ROOT . '/php/class-wordads-array-utils.php'; |
| 13 | 15 | |
| 14 | 16 | /** |
| @@ -23,22 +25,15 @@ | ||
| 23 | 25 | */ |
| 24 | 26 | protected static $instance = null; |
| 25 | 27 | |
| 26 | 28 | /** |
| 27 | - * Is this an AMP request? | |
| 29 | + * The parameters for WordAds. | |
| 28 | 30 | * |
| 29 | - * @var bool | |
| 31 | + * @var WordAds_Params | |
| 30 | 32 | */ |
| 31 | - private $is_amp; | |
| 33 | + private $params; | |
| 32 | 34 | |
| 33 | 35 | /** |
| 34 | - * Current blog theme from get_stylesheet(). | |
| 35 | - * | |
| 36 | - * @var string | |
| 37 | - */ | |
| 38 | - private $theme; | |
| 39 | - | |
| 40 | - /** | |
| 41 | 36 | * Has Smart asset been enqueued? |
| 42 | 37 | * |
| 43 | 38 | * @var bool True if Smart asset has been enqueued. |
| 44 | 39 | */ |
| @@ -44,13 +39,54 @@ | ||
| 44 | 39 | */ |
| 45 | 40 | private $is_asset_enqueued = false; |
| 46 | 41 | |
| 47 | 42 | /** |
| 48 | - * Toggle for inline ads. | |
| 43 | + * Supported formats. | |
| 44 | + * sidebar_widget formats represents the legacy Jetpack sidebar widget. | |
| 49 | 45 | * |
| 50 | - * @var bool True if inline ads are enabled. | |
| 46 | + * @var array | |
| 51 | 47 | */ |
| 52 | - private $is_inline_enabled; | |
| 48 | + private $formats = array( | |
| 49 | + 'top' => array( | |
| 50 | + 'enabled' => false, | |
| 51 | + ), | |
| 52 | + 'inline' => array( | |
| 53 | + 'enabled' => false, | |
| 54 | + ), | |
| 55 | + 'belowpost' => array( | |
| 56 | + 'enabled' => false, | |
| 57 | + ), | |
| 58 | + 'bottom_sticky' => array( | |
| 59 | + 'enabled' => false, | |
| 60 | + ), | |
| 61 | + 'sidebar_sticky_right' => array( | |
| 62 | + 'enabled' => false, | |
| 63 | + ), | |
| 64 | + 'gutenberg_rectangle' => array( | |
| 65 | + 'enabled' => false, | |
| 66 | + ), | |
| 67 | + 'gutenberg_leaderboard' => array( | |
| 68 | + 'enabled' => false, | |
| 69 | + ), | |
| 70 | + 'gutenberg_mobile_leaderboard' => array( | |
| 71 | + 'enabled' => false, | |
| 72 | + ), | |
| 73 | + 'gutenberg_skyscraper' => array( | |
| 74 | + 'enabled' => false, | |
| 75 | + ), | |
| 76 | + 'sidebar_widget_mediumrectangle' => array( | |
| 77 | + 'enabled' => false, | |
| 78 | + ), | |
| 79 | + 'sidebar_widget_leaderboard' => array( | |
| 80 | + 'enabled' => false, | |
| 81 | + ), | |
| 82 | + 'sidebar_widget_wideskyscraper' => array( | |
| 83 | + 'enabled' => false, | |
| 84 | + ), | |
| 85 | + 'shortcode' => array( | |
| 86 | + 'enabled' => false, | |
| 87 | + ), | |
| 88 | + ); | |
| 53 | 89 | |
| 54 | 90 | /** |
| 55 | 91 | * Private constructor. |
| 56 | 92 | */ |
| @@ -63,10 +99,10 @@ | ||
| 63 | 99 | * Ensures only one instance of WordAds_Smart is loaded or can be loaded. |
| 64 | 100 | * |
| 65 | 101 | * @return WordAds_Smart |
| 66 | 102 | */ |
| 67 | - public static function instance() { | |
| 68 | - if ( self::$instance === null ) { | |
| 103 | + public static function instance(): self { | |
| 104 | + if ( null === self::$instance ) { | |
| 69 | 105 | self::$instance = new self(); |
| 70 | 106 | } |
| 71 | 107 | return self::$instance; |
| 72 | 108 | } |
| @@ -78,19 +114,14 @@ | ||
| 78 | 114 | * |
| 79 | 115 | * @return void |
| 80 | 116 | */ |
| 81 | 117 | public function init( WordAds_Params $params ) { |
| 82 | - $this->is_amp = function_exists( 'amp_is_request' ) && amp_is_request(); | |
| 83 | - $this->theme = get_stylesheet(); | |
| 84 | - $this->is_inline_enabled = is_singular( 'post' ) && $params->options['wordads_inline_enabled']; | |
| 118 | + $this->params = $params; | |
| 85 | 119 | |
| 86 | - // Allow override. | |
| 87 | - // phpcs:disable WordPress.Security.NonceVerification.Recommended | |
| 88 | - if ( isset( $_GET['inline'] ) && 'true' === $_GET['inline'] ) { | |
| 89 | - $this->is_inline_enabled = true; | |
| 90 | - } | |
| 91 | - if ( $this->is_inline_enabled ) { | |
| 92 | - // Insert ads. | |
| 120 | + $this->enable_formats(); | |
| 121 | + $this->override_formats_from_query_string(); | |
| 122 | + | |
| 123 | + if ( $this->has_any_format_enabled() ) { | |
| 93 | 124 | $this->insert_ads(); |
| 94 | 125 | } |
| 95 | 126 | } |
| 96 | 127 | |
| @@ -135,9 +166,9 @@ | ||
| 135 | 166 | * |
| 136 | 167 | * @return void |
| 137 | 168 | */ |
| 138 | 169 | private function insert_ads() { |
| 139 | - if ( $this->is_amp ) { | |
| 170 | + if ( $this->params->is_amp ) { | |
| 140 | 171 | return; |
| 141 | 172 | } |
| 142 | 173 | |
| 143 | 174 | // Don't run on not found pages. |
| @@ -144,8 +175,11 @@ | ||
| 144 | 175 | if ( is_404() ) { |
| 145 | 176 | return; |
| 146 | 177 | } |
| 147 | 178 | |
| 179 | + // Add the resource hints. | |
| 180 | + add_filter( 'wp_resource_hints', array( $this, 'resource_hints' ), 10, 2 ); | |
| 181 | + | |
| 148 | 182 | // Enqueue JS assets. |
| 149 | 183 | $this->enqueue_assets(); |
| 150 | 184 | |
| 151 | 185 | $is_static_front_page = is_front_page() && 'page' === get_option( 'show_on_front' ); |
| @@ -150,9 +184,9 @@ | ||
| 150 | 184 | |
| 151 | 185 | $is_static_front_page = is_front_page() && 'page' === get_option( 'show_on_front' ); |
| 152 | 186 | |
| 153 | 187 | if ( ! ( $is_static_front_page || is_home() ) ) { |
| 154 | - if ( $this->is_inline_enabled ) { | |
| 188 | + if ( $this->formats['inline']['enabled'] ) { | |
| 155 | 189 | add_filter( |
| 156 | 190 | 'the_content', |
| 157 | 191 | array( $this, 'insert_inline_marker' ), |
| 158 | 192 | 10 |
| @@ -158,8 +192,18 @@ | ||
| 158 | 192 | 10 |
| 159 | 193 | ); |
| 160 | 194 | } |
| 161 | 195 | } |
| 196 | + | |
| 197 | + if ( $this->formats['bottom_sticky']['enabled'] ) { | |
| 198 | + // Disable IPW slot. | |
| 199 | + add_filter( 'wordads_iponweb_bottom_sticky_ad_disable', '__return_true', 10 ); | |
| 200 | + } | |
| 201 | + | |
| 202 | + if ( $this->formats['sidebar_sticky_right']['enabled'] ) { | |
| 203 | + // Disable IPW slot. | |
| 204 | + add_filter( 'wordads_iponweb_sidebar_sticky_right_ad_disable', '__return_true', 10 ); | |
| 205 | + } | |
| 162 | 206 | } |
| 163 | 207 | |
| 164 | 208 | /** |
| 165 | 209 | * Inserts JS configuration used by watl.js. |
| @@ -169,20 +213,13 @@ | ||
| 169 | 213 | public function insert_config() { |
| 170 | 214 | global $post; |
| 171 | 215 | |
| 172 | 216 | $config = array( |
| 173 | - 'blog_id' => $this->get_blog_id(), | |
| 174 | 217 | 'post_id' => ( $post instanceof WP_Post ) && is_singular( 'post' ) ? $post->ID : null, |
| 175 | - 'theme' => $this->theme, | |
| 218 | + 'origin' => 'jetpack', | |
| 219 | + 'theme' => get_stylesheet(), | |
| 176 | 220 | 'target' => $this->target_keywords(), |
| 177 | - '_' => array( | |
| 178 | - 'title' => __( 'Advertisement', 'jetpack' ), | |
| 179 | - 'privacy_settings' => __( 'Privacy Settings', 'jetpack' ), | |
| 180 | - ), | |
| 181 | - 'inline' => array( | |
| 182 | - 'enabled' => $this->is_inline_enabled, | |
| 183 | - ), | |
| 184 | - ); | |
| 221 | + ) + $this->formats; | |
| 185 | 222 | |
| 186 | 223 | // Do conversion. |
| 187 | 224 | $js_config = WordAds_Array_Utils::array_to_js_object( $config ); |
| 188 | 225 | |
| @@ -190,8 +227,24 @@ | ||
| 190 | 227 | wp_print_inline_script_tag( "var wa_smart = $js_config; wa_smart.cmd = [];" ); |
| 191 | 228 | } |
| 192 | 229 | |
| 193 | 230 | /** |
| 231 | + * Add the Smart resource hints. | |
| 232 | + * | |
| 233 | + * @param array $hints Domains for hinting. | |
| 234 | + * @param string $relation_type Resource type. | |
| 235 | + * | |
| 236 | + * @return array Domains for hinting. | |
| 237 | + */ | |
| 238 | + public function resource_hints( $hints, $relation_type ) { | |
| 239 | + if ( 'dns-prefetch' === $relation_type ) { | |
| 240 | + $hints[] = '//af.pubmine.com'; | |
| 241 | + } | |
| 242 | + | |
| 243 | + return $hints; | |
| 244 | + } | |
| 245 | + | |
| 246 | + /** | |
| 194 | 247 | * Gets the URL to a JSONP endpoint with configuration data. |
| 195 | 248 | * |
| 196 | 249 | * @return string The URL. |
| 197 | 250 | */ |
| @@ -197,9 +250,9 @@ | ||
| 197 | 250 | */ |
| 198 | 251 | private function get_config_url(): string { |
| 199 | 252 | return sprintf( |
| 200 | 253 | 'https://public-api.wordpress.com/wpcom/v2/sites/%1$d/adflow/conf/?_jsonp=a8c_adflow_callback', |
| 201 | - $this->get_blog_id() | |
| 254 | + $this->params->blog_id | |
| 202 | 255 | ); |
| 203 | 256 | } |
| 204 | 257 | |
| 205 | 258 | /** |
| @@ -207,11 +260,11 @@ | ||
| 207 | 260 | * |
| 208 | 261 | * @param string|null $content The post content. |
| 209 | 262 | * @return string|null The post content with the marker appended. |
| 210 | 263 | */ |
| 211 | - public function insert_inline_marker( $content ) { | |
| 212 | - if ( $content === null ) { | |
| 213 | - return $content; | |
| 264 | + public function insert_inline_marker( ?string $content ): ?string { | |
| 265 | + if ( null === $content ) { | |
| 266 | + return null; | |
| 214 | 267 | } |
| 215 | 268 | $inline_ad_marker = '<span id="wordads-inline-marker" style="display: none;"></span>'; |
| 216 | 269 | |
| 217 | 270 | // Append the ad to the post content. |
| @@ -226,9 +279,8 @@ | ||
| 226 | 279 | private function target_keywords(): string { |
| 227 | 280 | $target_keywords = array_merge( |
| 228 | 281 | $this->get_blog_keywords(), |
| 229 | 282 | $this->get_language_keywords() |
| 230 | - // TODO: Include categorization. | |
| 231 | 283 | ); |
| 232 | 284 | |
| 233 | 285 | return implode( ';', $target_keywords ); |
| 234 | 286 | } |
| @@ -238,9 +290,9 @@ | ||
| 238 | 290 | * |
| 239 | 291 | * @return array The list of blog keywords. |
| 240 | 292 | */ |
| 241 | 293 | private function get_blog_keywords(): array { |
| 242 | - return array( 'wp_blog_id=' . $this->get_blog_id() ); | |
| 294 | + return array( 'wp_blog_id=' . $this->params->blog_id ); | |
| 243 | 295 | } |
| 244 | 296 | |
| 245 | 297 | /** |
| 246 | 298 | * Gets the site language formatted as a keyword. |
| @@ -251,12 +303,44 @@ | ||
| 251 | 303 | return array( 'language=' . explode( '-', get_locale() )[0] ); |
| 252 | 304 | } |
| 253 | 305 | |
| 254 | 306 | /** |
| 255 | - * Gets the blog's ID. | |
| 307 | + * Enable formats by post types and the display options. | |
| 256 | 308 | * |
| 257 | - * @return int The blog's ID. | |
| 309 | + * @return void | |
| 258 | 310 | */ |
| 259 | - private function get_blog_id(): int { | |
| 260 | - return Jetpack::get_option( 'id', 0 ); | |
| 311 | + private function enable_formats(): void { | |
| 312 | + $this->formats['top']['enabled'] = $this->params->options['enable_header_ad']; | |
| 313 | + $this->formats['inline']['enabled'] = is_singular( 'post' ) && $this->params->options['wordads_inline_enabled']; | |
| 314 | + $this->formats['belowpost']['enabled'] = $this->params->should_show(); | |
| 315 | + $this->formats['bottom_sticky']['enabled'] = $this->params->options['wordads_bottom_sticky_enabled']; | |
| 316 | + $this->formats['sidebar_sticky_right']['enabled'] = $this->params->options['wordads_sidebar_sticky_right_enabled']; | |
| 317 | + } | |
| 318 | + | |
| 319 | + /** | |
| 320 | + * Allow format enabled override from query string, eg. ?inline=true. | |
| 321 | + * | |
| 322 | + * @return void | |
| 323 | + */ | |
| 324 | + private function override_formats_from_query_string(): void { | |
| 325 | + // phpcs:disable WordPress.Security.NonceVerification.Recommended | |
| 326 | + if ( ! isset( $_GET['wordads-logging'] ) ) { | |
| 327 | + return; | |
| 328 | + } | |
| 329 | + | |
| 330 | + foreach ( $this->formats as $format_type => $_ ) { | |
| 331 | + // phpcs:disable WordPress.Security.NonceVerification.Recommended | |
| 332 | + if ( isset( $_GET[ $format_type ] ) && 'true' === $_GET[ $format_type ] ) { | |
| 333 | + $this->formats[ $format_type ]['enabled'] = true; | |
| 334 | + } | |
| 335 | + } | |
| 336 | + } | |
| 337 | + | |
| 338 | + /** | |
| 339 | + * Check if has any format enabled. | |
| 340 | + * | |
| 341 | + * @return bool True if enabled, false otherwise. | |
| 342 | + */ | |
| 343 | + private function has_any_format_enabled(): bool { | |
| 344 | + return in_array( true, array_column( $this->formats, 'enabled' ), true ); | |
| 261 | 345 | } |
| 262 | 346 | } |