EDD_SL_Plugin_Updater.php
8 years ago
ad-ajax.php
8 years ago
ad-debug.php
8 years ago
ad-model.php
8 years ago
ad-select.php
9 years ago
ad.php
7 years ago
ad_ajax_callbacks.php
7 years ago
ad_group.php
7 years ago
ad_placements.php
7 years ago
ad_type_abstract.php
8 years ago
ad_type_content.php
8 years ago
ad_type_dummy.php
8 years ago
ad_type_group.php
8 years ago
ad_type_image.php
7 years ago
ad_type_plain.php
8 years ago
checks.php
7 years ago
compatibility.php
7 years ago
display-conditions.php
7 years ago
filesystem.php
8 years ago
frontend_checks.php
7 years ago
plugin.php
7 years ago
upgrades.php
9 years ago
utils.php
7 years ago
visitor-conditions.php
7 years ago
widget.php
7 years ago
widget.php
257 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Advanced Ads Widget |
| 4 | * |
| 5 | * @package Advanced_Ads_Widget |
| 6 | * @author Thomas Maier <thomas.maier@webgilde.com> |
| 7 | * @license GPL-2.0+ |
| 8 | * @link http://webgilde.com |
| 9 | * @copyright 2014 Thomas Maier, webgilde GmbH |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * Ad widget |
| 14 | * |
| 15 | */ |
| 16 | class Advanced_Ads_Widget extends WP_Widget { |
| 17 | |
| 18 | function __construct() { |
| 19 | $prefix = Advanced_Ads_Plugin::get_instance()->get_frontend_prefix(); |
| 20 | $classname = $prefix . 'widget'; |
| 21 | |
| 22 | $widget_ops = array('classname' => $classname, 'description' => __( 'Display Ads and Ad Groups.', 'advanced-ads' )); |
| 23 | $control_ops = array(); |
| 24 | $base_id = Advanced_Ads_Widget::get_base_id(); |
| 25 | |
| 26 | parent::__construct( $base_id,'Advanced Ads', $widget_ops, $control_ops ); |
| 27 | |
| 28 | add_filter( 'q2w3-fixed-widgets', array( $this, 'q2w3_replace_frontend_id' ) ); |
| 29 | } |
| 30 | |
| 31 | function widget($args, $instance) { |
| 32 | /** This filter is documented in wp-includes/default-widgets.php */ |
| 33 | $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); |
| 34 | |
| 35 | extract( $args ); |
| 36 | $item_id = empty($instance['item_id']) ? '' : $instance['item_id']; |
| 37 | |
| 38 | $output = self::output( $item_id ); |
| 39 | if( $output == '' ){ |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | $before_widget = $this->maybe_replace_frontend_id( $before_widget, $instance ); |
| 44 | |
| 45 | echo $before_widget; |
| 46 | if ( ! empty( $title ) ) { |
| 47 | echo $before_title . $title . $after_title; |
| 48 | } |
| 49 | echo $output; |
| 50 | echo $after_widget; |
| 51 | } |
| 52 | |
| 53 | function update($new_instance, $old_instance) { |
| 54 | $instance = $old_instance; |
| 55 | $instance['title'] = $new_instance['title']; |
| 56 | $instance['item_id'] = $new_instance['item_id']; |
| 57 | |
| 58 | // Allow to remove/replace id for new widgets and if it was allowed earlier. |
| 59 | if ( $old_instance === array() || ! empty( $old_instance['remove-widget-id'] ) ) { |
| 60 | $instance['remove-widget-id'] = true; |
| 61 | } |
| 62 | return $instance; |
| 63 | return $instance; |
| 64 | } |
| 65 | |
| 66 | function form($instance) { |
| 67 | $instance = wp_parse_args( (array) $instance, array('title' => '', 'item_id' => '') ); |
| 68 | $title = strip_tags( $instance['title'] ); |
| 69 | $elementid = $instance['item_id']; |
| 70 | |
| 71 | ?><p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'advanced-ads' ); ?></label> |
| 72 | <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p><?php |
| 73 | |
| 74 | $items = array_merge( self::items_for_select(), self::widget_placements_for_select() ); |
| 75 | ?> |
| 76 | <select id="<?php echo $this->get_field_id( 'item_id' ); ?>" name="<?php echo $this->get_field_name( 'item_id' ); ?>"> |
| 77 | <option value=""><?php _e( '--empty--', 'advanced-ads' ); ?></option> |
| 78 | <?php if ( isset($items['placements']) ) : ?> |
| 79 | <optgroup label="<?php _e( 'Placements', 'advanced-ads' ); ?>"> |
| 80 | <?php foreach ( $items['placements'] as $_item_id => $_item_title ) : ?> |
| 81 | <option value="<?php echo $_item_id; ?>" <?php selected( $_item_id, $elementid ); ?>><?php echo $_item_title; ?></option> |
| 82 | <?php endforeach; ?> |
| 83 | </optgroup> |
| 84 | <?php endif; ?> |
| 85 | <?php if ( isset($items['groups']) ) : ?> |
| 86 | <optgroup label="<?php _e( 'Ad Groups', 'advanced-ads' ); ?>"> |
| 87 | <?php foreach ( $items['groups'] as $_item_id => $_item_title ) : ?> |
| 88 | <option value="<?php echo $_item_id; ?>" <?php selected( $_item_id, $elementid ); ?>><?php echo $_item_title; ?></option> |
| 89 | <?php endforeach; ?> |
| 90 | </optgroup> |
| 91 | <?php endif; ?> |
| 92 | <?php if ( isset($items['ads']) ) : ?> |
| 93 | <optgroup label="<?php _e( 'Ads', 'advanced-ads' ); ?>"> |
| 94 | <?php foreach ( $items['ads'] as $_item_id => $_item_title ) : ?> |
| 95 | <option value="<?php echo $_item_id; ?>" <?php selected( $_item_id, $elementid ); ?>><?php echo $_item_title; ?></option> |
| 96 | <?php endforeach; ?> |
| 97 | </optgroup> |
| 98 | <?php endif; ?> |
| 99 | </select><?php |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * get items for widget select field |
| 104 | * |
| 105 | * @since 1.2 |
| 106 | * @return arr $select items for select field |
| 107 | */ |
| 108 | static function items_for_select(){ |
| 109 | $select = array(); |
| 110 | $model = Advanced_Ads::get_instance()->get_model(); |
| 111 | |
| 112 | // load all ads |
| 113 | $ads = $model->get_ads( array('orderby' => 'title', 'order' => 'ASC') ); |
| 114 | foreach ( $ads as $_ad ){ |
| 115 | $select['ads']['ad_' . $_ad->ID] = $_ad->post_title; |
| 116 | } |
| 117 | |
| 118 | // load all ad groups |
| 119 | $groups = $model->get_ad_groups(); |
| 120 | foreach ( $groups as $_group ){ |
| 121 | $select['groups']['group_' . $_group->term_id] = $_group->name; |
| 122 | } |
| 123 | |
| 124 | return $select; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * get widget placements for select field |
| 129 | * |
| 130 | * @since 1.6.11 |
| 131 | * @return arr $items for select field |
| 132 | */ |
| 133 | public static function widget_placements_for_select(){ |
| 134 | $select = array(); |
| 135 | $placements = Advanced_Ads::get_ad_placements_array(); |
| 136 | |
| 137 | if ( is_array( $placements ) ) { |
| 138 | ksort( $placements ); |
| 139 | } |
| 140 | |
| 141 | foreach( $placements as $_placement_slug => $_placement ){ |
| 142 | if( isset( $_placement['type'] ) && 'sidebar_widget' === $_placement['type'] ){ |
| 143 | $select['placements']['placement_' . $_placement_slug ] = $_placement['name']; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return $select; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * return content of an in a widget |
| 152 | * |
| 153 | * @since 1.2 |
| 154 | * @param string $id slug of the display |
| 155 | */ |
| 156 | static function output($id = ''){ |
| 157 | // get placement data for the slug |
| 158 | if ( empty($id) ) { return; } |
| 159 | |
| 160 | $item = explode( '_', $id, 2 ); |
| 161 | |
| 162 | if ( isset($item[1]) ) { |
| 163 | $item_id = $item[1]; |
| 164 | } elseif (empty($item_id)) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | // return either ad or group content |
| 169 | if ( $item[0] == 'ad' ){ |
| 170 | return get_ad( absint( $item_id ) ); |
| 171 | } elseif ( $item[0] == 'group' ){ |
| 172 | return get_ad_group( absint( $item_id ) ); |
| 173 | } elseif ( $item[0] == 'placement' ){ |
| 174 | return get_ad_placement( $item_id ); |
| 175 | } |
| 176 | |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * get the base id of the widget |
| 182 | * |
| 183 | * @return string |
| 184 | */ |
| 185 | public static function get_base_id() { |
| 186 | $options = Advanced_Ads_Plugin::get_instance()->options(); |
| 187 | |
| 188 | // deprecated to keep previously changed prefixed working |
| 189 | $prefix2 = ( isset( $options['id-prefix'] ) && $options['id-prefix'] !== '' ) ? $options['id-prefix'] : 'advads_ad_'; |
| 190 | return $prefix2 . 'widget'; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Get frontend widget id. |
| 195 | * |
| 196 | * @param int $number Unique ID number of the current widget instance. |
| 197 | * @return str |
| 198 | */ |
| 199 | private function get_frontend_id( $number ) { |
| 200 | $prefix = Advanced_Ads_Plugin::get_instance()->get_frontend_prefix(); |
| 201 | return $prefix . 'widget-' . $number; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Make it harder for ad blockers to block the widget. |
| 206 | * removes the pre-defined widget ID (e.g., advads_ad_widget-20) and replaces it with one that uses the individual frontend prefix |
| 207 | * |
| 208 | * @param str $before_widget |
| 209 | * @param array $instance Settings for the current widget instance. |
| 210 | * @return str $before_widget |
| 211 | */ |
| 212 | private function maybe_replace_frontend_id( $before_widget, $instance ) { |
| 213 | if ( empty( $instance['remove-widget-id'] ) |
| 214 | || defined( 'JNEWS_THEME_ID' ) // the JNews theme overrides the widget ID and resets it, so we target this specifically. |
| 215 | ) { |
| 216 | $pattern = '#\sid=("|\')[^"\']+["\']#'; |
| 217 | if ( ( defined( 'ADVANCED_ADS_SHOW_WIDGET_ID' ) && ADVANCED_ADS_SHOW_WIDGET_ID ) |
| 218 | || ! empty( $instance['q2w3_fixed_widget'] ) |
| 219 | ) { |
| 220 | // Replace id. |
| 221 | $number = ! empty( $this->number ) ? $this->number : ''; |
| 222 | $before_widget = preg_replace( $pattern, ' id=$01' . $this->get_frontend_id( $number ) . '$01', $before_widget ); |
| 223 | } else { |
| 224 | // Remove id. |
| 225 | $before_widget = preg_replace( $pattern, '', $before_widget ); |
| 226 | } |
| 227 | } |
| 228 | return $before_widget; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Provide the 'Q2W3 Fixed Widget' plugin with the new frontend widget id. |
| 233 | * |
| 234 | * @param array $sidebars_widgets |
| 235 | * @return array $sidebars_widgets |
| 236 | */ |
| 237 | public function q2w3_replace_frontend_id( $sidebars_widgets ) { |
| 238 | foreach ( $sidebars_widgets as $sidebar => $widgets ) { |
| 239 | foreach ( $widgets as $k => $widget ) { |
| 240 | $pos = strrpos( $widget, '-' ); |
| 241 | $option_name = substr( $widget, 0, $pos ); |
| 242 | $number = substr( $widget, $pos + 1 ); |
| 243 | |
| 244 | if ( $option_name === self::get_base_id() ) { |
| 245 | $widget_options = get_option('widget_' . $option_name); |
| 246 | if ( ! empty( $widget_options[ $number ]['remove-widget-id'] ) ) { |
| 247 | $sidebars_widgets[ $sidebar ][ $k ] = $this->get_frontend_id( $number ); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | } |
| 252 | } |
| 253 | return $sidebars_widgets; |
| 254 | } |
| 255 | |
| 256 | } |
| 257 |