← All changes
|
modules/wordads/php/class-wordads-sidebar-widget.php
+68
-10
13.3.3
→
16.3-a.1
View file →
| @@ -4,8 +4,12 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * @package automattic/jetpack |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + exit( 0 ); | |
| 10 | +} | |
| 11 | + | |
| 8 | 12 | /** |
| 9 | 13 | * Widget for inserting an ad into your sidebar |
| 10 | 14 | * |
| 11 | 15 | * @since 4.5.0 |
| @@ -19,8 +23,19 @@ | ||
| 19 | 23 | */ |
| 20 | 24 | private static $allowed_tags = array( 'mrec', 'wideskyscraper', 'leaderboard' ); |
| 21 | 25 | |
| 22 | 26 | /** |
| 27 | + * Mapping array of widget sizes with the WordAds_Smart formats. | |
| 28 | + * | |
| 29 | + * @var string[] | |
| 30 | + */ | |
| 31 | + private static $sizes_x_smart_format = array( | |
| 32 | + '300x250' => 'sidebar_widget_mediumrectangle', | |
| 33 | + '728x90' => 'sidebar_widget_leaderboard', | |
| 34 | + '160x600' => 'sidebar_widget_wideskyscraper', | |
| 35 | + ); | |
| 36 | + | |
| 37 | + /** | |
| 23 | 38 | * Number of widgets. |
| 24 | 39 | * |
| 25 | 40 | * @var int |
| 26 | 41 | */ |
| @@ -73,9 +88,8 @@ | ||
| 73 | 88 | $instance['unit'] = 'mrec'; |
| 74 | 89 | } |
| 75 | 90 | |
| 76 | 91 | ++self::$num_widgets; |
| 77 | - $about = __( 'Advertisements', 'jetpack' ); | |
| 78 | 92 | $width = WordAds::$ad_tag_ids[ $instance['unit'] ]['width']; |
| 79 | 93 | $height = WordAds::$ad_tag_ids[ $instance['unit'] ]['height']; |
| 80 | 94 | $unit_id = 1 === self::$num_widgets ? 3 : self::$num_widgets + 3; // 2nd belowpost is '4' |
| 81 | 95 | $section_id = 0 === $wordads->params->blog_id ? |
| @@ -81,9 +95,52 @@ | ||
| 81 | 95 | $section_id = 0 === $wordads->params->blog_id ? |
| 82 | 96 | WORDADS_API_TEST_ID : |
| 83 | 97 | $wordads->params->blog_id . $unit_id; |
| 84 | 98 | |
| 85 | - $snippet = ''; | |
| 99 | + $smart_format = self::$sizes_x_smart_format[ "{$width}x{$height}" ]; | |
| 100 | + // phpcs:disable WordPress.Security.NonceVerification.Recommended | |
| 101 | + $is_watl_enabled = isset( $_GET['wordads-logging'] ) && isset( $_GET[ $smart_format ] ) && 'true' === $_GET[ $smart_format ]; | |
| 102 | + | |
| 103 | + // Get the widget snippet. | |
| 104 | + $widget_snippet = $this->get_widget_snippet( $instance, $section_id, $height, $width ); | |
| 105 | + | |
| 106 | + // Render the IPW or house ad if WATL is disabled. | |
| 107 | + if ( ! $is_watl_enabled ) { | |
| 108 | + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 109 | + echo $widget_snippet; | |
| 110 | + return; | |
| 111 | + } | |
| 112 | + | |
| 113 | + // Remove linebreaks and sanitize. | |
| 114 | + $tag = esc_js( str_replace( array( "\n", "\t", "\r" ), '', $widget_snippet ) ); | |
| 115 | + | |
| 116 | + // Add the fallback to be processed by WATL. | |
| 117 | + $fallback_snippet = <<<HTML | |
| 118 | + <script type="text/javascript"> | |
| 119 | + var sas_fallback = sas_fallback || []; | |
| 120 | + sas_fallback.push( | |
| 121 | + { tag: "$tag", type: '$smart_format' } | |
| 122 | + ); | |
| 123 | + </script> | |
| 124 | +HTML; | |
| 125 | + | |
| 126 | + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 127 | + echo $fallback_snippet . $wordads::get_watl_ad_html_tag( $smart_format ); | |
| 128 | + } | |
| 129 | + | |
| 130 | + /** | |
| 131 | + * The widget snippet. | |
| 132 | + * | |
| 133 | + * @param array $instance The widget instance. | |
| 134 | + * @param string $section_id The section id. | |
| 135 | + * @param int $height The ad height. | |
| 136 | + * @param int $width The ad width. | |
| 137 | + * | |
| 138 | + * @return string | |
| 139 | + */ | |
| 140 | + private function get_widget_snippet( $instance, $section_id, $height, $width ) { | |
| 141 | + global $wordads; | |
| 142 | + | |
| 86 | 143 | if ( $wordads->option( 'wordads_house', true ) ) { |
| 87 | 144 | $unit = 'mrec'; |
| 88 | 145 | if ( 'leaderboard' === $instance['unit'] && ! $this->params->mobile_device ) { |
| 89 | 146 | $unit = 'leaderboard'; |
| @@ -92,23 +149,24 @@ | ||
| 92 | 149 | } |
| 93 | 150 | |
| 94 | 151 | $snippet = $wordads->get_house_ad( $unit ); |
| 95 | 152 | } else { |
| 96 | - // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 97 | - echo $wordads->get_ad_snippet( $section_id, $height, $width, 'widget' ); | |
| 98 | - return; | |
| 153 | + return $wordads->get_ad_snippet( $section_id, $height, $width, 'widget' ); | |
| 99 | 154 | } |
| 100 | 155 | |
| 101 | - ?> | |
| 156 | + $about = __( 'Advertisements', 'jetpack' ); | |
| 157 | + $unit = esc_attr( $instance['unit'] ); | |
| 158 | + | |
| 159 | + return <<<HTML | |
| 102 | 160 | <div class="wpcnt"> |
| 103 | 161 | <div class="wpa"> |
| 104 | - <span class="wpa-about"><?php echo esc_html( $about ); ?></span> | |
| 105 | - <div class="u <?php echo esc_attr( $instance['unit'] ); ?>"> | |
| 106 | - <?php echo $snippet; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> | |
| 162 | + <span class="wpa-about">$about</span> | |
| 163 | + <div class="u $unit"> | |
| 164 | + $snippet | |
| 107 | 165 | </div> |
| 108 | 166 | </div> |
| 109 | 167 | </div> |
| 110 | - <?php | |
| 168 | +HTML; | |
| 111 | 169 | } |
| 112 | 170 | |
| 113 | 171 | /** |
| 114 | 172 | * The widget settings form. |