networks
5 years ago
class-wordads-admin.php
2 years ago
class-wordads-api.php
4 years ago
class-wordads-california-privacy.php
3 years ago
class-wordads-ccpa-do-not-sell-link-widget.php
3 years ago
class-wordads-consent-management-provider.php
2 years ago
class-wordads-cron.php
5 years ago
class-wordads-params.php
2 years ago
class-wordads-sidebar-widget.php
2 years ago
class-wordads-sidebar-widget.php
168 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Widget for adding ads to a sidebar. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Widget for inserting an ad into your sidebar |
| 10 | * |
| 11 | * @since 4.5.0 |
| 12 | */ |
| 13 | class WordAds_Sidebar_Widget extends WP_Widget { |
| 14 | |
| 15 | /** |
| 16 | * Allowed tags. |
| 17 | * |
| 18 | * @var string[] |
| 19 | */ |
| 20 | private static $allowed_tags = array( 'mrec', 'wideskyscraper', 'leaderboard' ); |
| 21 | |
| 22 | /** |
| 23 | * Number of widgets. |
| 24 | * |
| 25 | * @var int |
| 26 | */ |
| 27 | private static $num_widgets = 0; |
| 28 | |
| 29 | /** |
| 30 | * WordAds_Sidebar_Widget constructor. |
| 31 | */ |
| 32 | public function __construct() { |
| 33 | parent::__construct( |
| 34 | 'wordads_sidebar_widget', |
| 35 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
| 36 | apply_filters( 'jetpack_widget_name', 'Ads' ), |
| 37 | array( |
| 38 | 'description' => __( 'Insert an ad unit wherever you can place a widget.', 'jetpack' ), |
| 39 | 'customize_selective_refresh' => true, |
| 40 | ) |
| 41 | ); |
| 42 | |
| 43 | add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Remove the Ad widget from the Legacy Widget block |
| 48 | * |
| 49 | * @param array $widget_types List of widgets that are currently removed from the Legacy Widget block. |
| 50 | * |
| 51 | * @return array $widget_types New list of widgets that will be removed. |
| 52 | */ |
| 53 | public function hide_widget_in_block_editor( $widget_types ) { |
| 54 | $widget_types[] = 'wordads_sidebar_widget'; |
| 55 | return $widget_types; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * The Widget outputter. |
| 60 | * |
| 61 | * @param array $args Widget args. |
| 62 | * @param array $instance The Widget instance. |
| 63 | * |
| 64 | * @return bool|void |
| 65 | */ |
| 66 | public function widget( $args, $instance ) { |
| 67 | global $wordads; |
| 68 | if ( $wordads->should_bail() ) { |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | if ( ! isset( $instance['unit'] ) ) { |
| 73 | $instance['unit'] = 'mrec'; |
| 74 | } |
| 75 | |
| 76 | ++self::$num_widgets; |
| 77 | $about = __( 'Advertisements', 'jetpack' ); |
| 78 | $width = WordAds::$ad_tag_ids[ $instance['unit'] ]['width']; |
| 79 | $height = WordAds::$ad_tag_ids[ $instance['unit'] ]['height']; |
| 80 | $unit_id = 1 === self::$num_widgets ? 3 : self::$num_widgets + 3; // 2nd belowpost is '4' |
| 81 | $section_id = 0 === $wordads->params->blog_id ? |
| 82 | WORDADS_API_TEST_ID : |
| 83 | $wordads->params->blog_id . $unit_id; |
| 84 | |
| 85 | $snippet = ''; |
| 86 | if ( $wordads->option( 'wordads_house', true ) ) { |
| 87 | $unit = 'mrec'; |
| 88 | if ( 'leaderboard' === $instance['unit'] && ! $this->params->mobile_device ) { |
| 89 | $unit = 'leaderboard'; |
| 90 | } elseif ( 'wideskyscraper' === $instance['unit'] ) { |
| 91 | $unit = 'widesky'; |
| 92 | } |
| 93 | |
| 94 | $snippet = $wordads->get_house_ad( $unit ); |
| 95 | } else { |
| 96 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 97 | echo $wordads->get_ad_snippet( $section_id, $height, $width, 'widget' ); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | ?> |
| 102 | <div class="wpcnt"> |
| 103 | <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 ?> |
| 107 | </div> |
| 108 | </div> |
| 109 | </div> |
| 110 | <?php |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * The widget settings form. |
| 115 | * |
| 116 | * @param array $instance Widget instance. |
| 117 | * |
| 118 | * @return string|void |
| 119 | */ |
| 120 | public function form( $instance ) { |
| 121 | // ad unit type. |
| 122 | if ( isset( $instance['unit'] ) ) { |
| 123 | $unit = $instance['unit']; |
| 124 | } else { |
| 125 | $unit = 'mrec'; |
| 126 | } |
| 127 | ?> |
| 128 | <p> |
| 129 | <label for="<?php echo esc_attr( $this->get_field_id( 'unit' ) ); ?>"><?php esc_html_e( 'Tag Dimensions:', 'jetpack' ); ?></label> |
| 130 | <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'unit' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'unit' ) ); ?>"> |
| 131 | <?php |
| 132 | foreach ( WordAds::$ad_tag_ids as $ad_unit => $properties ) { |
| 133 | if ( ! in_array( $ad_unit, self::$allowed_tags, true ) ) { |
| 134 | continue; |
| 135 | } |
| 136 | |
| 137 | $splits = explode( '_', $properties['tag'] ); |
| 138 | $unit_pretty = "{$splits[0]} {$splits[1]}"; |
| 139 | $selected = selected( $ad_unit, $unit, false ); |
| 140 | echo "<option value='", esc_attr( $ad_unit ), "' ", esc_attr( $selected ), '>', esc_html( $unit_pretty ), '</option>'; |
| 141 | } |
| 142 | ?> |
| 143 | </select> |
| 144 | </p> |
| 145 | <?php |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * The Widget updater. |
| 150 | * |
| 151 | * @param array $new_instance The revised instance. |
| 152 | * @param array $old_instance Original instance. |
| 153 | * |
| 154 | * @return array |
| 155 | */ |
| 156 | public function update( $new_instance, $old_instance ) { |
| 157 | $instance = $old_instance; |
| 158 | |
| 159 | if ( in_array( $new_instance['unit'], self::$allowed_tags, true ) ) { |
| 160 | $instance['unit'] = $new_instance['unit']; |
| 161 | } else { |
| 162 | $instance['unit'] = 'mrec'; |
| 163 | } |
| 164 | |
| 165 | return $instance; |
| 166 | } |
| 167 | } |
| 168 |