PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 1.8.3
Advanced Ads – Ad Manager & AdSense v1.8.3
2.0.26 2.0.25 2.0.24 2.0.23 2.0.22 2.0.21 1.38.0 1.39.0 1.39.1 1.39.2 1.39.3 1.39.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.40.0 1.40.1 1.40.2 1.41.0 1.42.0 1.42.1 1.42.2 1.43.0 1.44.0 1.44.1 1.45.0 1.45.1 1.46.0 1.47.0 1.47.1 1.47.2 1.47.3 1.47.4 1.47.5 1.48.0 1.48.1 1.49.0 1.5.0 1.5.0.1 1.5.1 1.5.2 1.5.2.1 1.5.4 1.5.4.1 1.5.5 1.50.0 1.51.0 1.51.1 1.51.2 1.51.3 1.52.0 1.52.1 1.52.2 1.52.3 1.52.4 1.53.0 1.53.1 1.53.2 1.54.0 1.54.1 1.55.0 1.56.0 1.56.1 1.56.2 1.56.3 1.56.4 1.6 1.6.1 1.6.10 1.6.10.1 1.6.10.2 1.6.11 1.6.11.1 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.17.1 1.6.17.2 1.6.2 1.6.2.1 1.6.3 1.6.4 1.6.4.1 1.6.5 1.6.6 1.6.6.1 1.6.7 1.6.7.1 1.6.8 1.6.8.1 1.6.8.2 1.6.8.3 1.6.9 1.6.9.1 1.6.9.2 1.6.9.3 1.6.9.4 1.7 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.1.1 1.7.1.2 1.7.1.3 1.7.1.4 1.7.1.5 1.7.10 trunk 1.7.11 1.0.1 1.7.12 1.0.2 1.7.13 1.0.3 1.7.14 1.1.0 1.7.15 1.1.1 1.7.16 1.1.2 1.7.17 1.1.3 1.7.18 1.10 1.7.19 1.10.1 1.7.2 1.10.10 1.7.2.1 1.10.11 1.7.20 1.10.12 1.7.21 1.10.2 1.7.22 1.10.3 1.7.23 1.10.4 1.7.24 1.10.5 1.7.25 1.10.6 1.7.3 1.10.7 1.7.4 1.10.8 1.7.4.1 1.10.9 1.7.4.2 1.11 1.7.4.3 1.11.1 1.7.4.4 1.11.2 1.7.4.5 1.12 1.7.5 1.13 1.7.5.1 1.13.1 1.7.6 1.13.2 1.7.7 1.13.3 1.7.8 1.13.4 1.7.9 1.13.5 1.7.9.1 1.13.6 1.7.9.2 1.13.7 1.7.9.3 1.13.8 1.8 1.14 1.8.1 1.14.1 1.8.10 1.14.10 1.8.11 1.14.11 1.8.12 1.14.2 1.8.13 1.14.3 1.8.14 1.14.4 1.8.15 1.14.5 1.8.16 1.14.6 1.8.17 1.14.7 1.8.18 1.14.8 1.8.19 1.14.9 1.8.2 1.15 1.8.20 1.16 1.8.21 1.16.1 1.8.22 1.17 1.8.23 1.17.1 1.8.24 1.17.10 1.8.25 1.17.10-rc.1 1.8.26 1.17.11 1.8.27 1.17.12 1.8.28 1.17.12-rc.1 1.8.29 1.17.2 1.8.3 1.17.3 1.8.30 1.17.4 1.8.4 1.17.5 1.8.5 1.17.6 1.8.6 1.17.7 1.8.7 1.17.8 1.8.8 1.17.9 1.8.9 1.17.9-beta.1 1.9 1.18.0 2.0.0 1.19.0 2.0.1 1.19.1 2.0.10 1.2 2.0.11 1.2.1 2.0.12 1.2.2 2.0.13 1.2.3 2.0.14 1.2.4 2.0.15 1.2.5 2.0.16 1.2.6 2.0.17 1.2.7 2.0.18 1.20.0 2.0.19 1.20.0-rc.1 2.0.2 1.20.0-rc.2 2.0.20 1.20.1 2.0.3 1.20.2 2.0.4 1.20.3 2.0.5 1.21.0 2.0.6 1.21.1 2.0.7 1.22.0 2.0.8 1.22.1 2.0.9 1.22.2 1.23.0 1.23.1 1.23.2 1.24.0 1.24.1 1.24.2 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.29.0 1.29.1 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.30.0 1.30.1 1.30.2 1.30.2-rc.1 1.30.3 1.30.4 1.30.4-rc.1 1.30.5 1.31.0 1.31.1 1.32.0 1.32.0-rc.1 1.33.0 1.33.1 1.33.2 1.34.0 1.35.0 1.35.1 1.36.0 1.36.1 1.36.2 1.36.3 1.37.0 1.37.1 1.37.2
advanced-ads / admin / includes / class-ad-groups-list.php
advanced-ads / admin / includes Last commit date
class-ad-groups-list.php 9 years ago class-ad-type.php 9 years ago class-licenses.php 9 years ago class-menu.php 9 years ago class-meta-box.php 9 years ago class-notices.php 9 years ago class-options.php 9 years ago class-overview-widgets.php 9 years ago class-settings.php 9 years ago class-shortcode-creator.php 9 years ago notices.php 9 years ago shortcode-creator-l10n.php 10 years ago
class-ad-groups-list.php
477 lines
1 <?php
2 /**
3 * Groups List Table class.
4 *
5 * @package Advanced Ads
6 * @since 1.4.4
7 */
8 class Advanced_Ads_Groups_List {
9
10 /**
11 * array with all groups
12 */
13 public $groups = array();
14
15 /**
16 * array with all ad group types
17 */
18 public $types = array();
19
20 /**
21 * construct the current list
22 */
23 public function __construct(){
24
25 // set default vars
26 $this->taxonomy = Advanced_Ads::AD_GROUP_TAXONOMY;
27 $this->post_type = Advanced_Ads::POST_TYPE_SLUG;
28
29 $this->load_groups();
30
31 $this->types = $this->get_ad_group_types();
32 }
33
34 /**
35 * load ad groups
36 */
37 public function load_groups(){
38
39 // load all groups
40 $search = ! empty($_REQUEST['s']) ? trim( wp_unslash( $_REQUEST['s'] ) ) : '';
41
42 $args = array(
43 'taxonomy' => $this->taxonomy,
44 'search' => $search,
45 'hide_empty' => 0,
46 );
47 // get wp term objects
48 $terms = Advanced_Ads::get_ad_groups( $args );
49
50 // add meta data to groups
51 $this->groups = $this->load_groups_objects_from_terms( $terms );
52 }
53
54 /**
55 * load ad groups objects from wp term objects
56 *
57 * @param arr $terms array of wp term objects
58 */
59 protected function load_groups_objects_from_terms(array $terms){
60
61 $groups = array();
62 foreach ( $terms as $_group ){
63 $groups[] = new Advanced_Ads_Group( $_group );
64 }
65
66 return $groups;
67 }
68
69 /**
70 * render group list header
71 */
72 public function render_header(){
73 $file = ADVADS_BASE_PATH . 'admin/views/ad-group-list-header.php';
74 require_once($file);
75 }
76
77 /**
78 * render list rows
79 */
80 public function render_rows(){
81 foreach ( $this->groups as $_group ){
82 $this->render_row( $_group );
83 $this->render_form_row( $_group );
84 }
85 }
86
87
88 /**
89 * render a single row
90 *
91 * @param obj $group the ad group object
92 */
93 public function render_row( Advanced_Ads_Group $group ) {
94 $file = ADVADS_BASE_PATH . 'admin/views/ad-group-list-row.php';
95 require($file);
96 }
97
98 /**
99 * render the form row of a group
100 *
101 * @param obj $group the ad group object
102 */
103 public function render_form_row(Advanced_Ads_Group $group){
104
105 // query ads
106 $ads = $this->get_ads( $group );
107
108 $weights = $group->get_ad_weights();
109 $ad_form_rows = $weights;
110 arsort( $ad_form_rows );
111
112 // The Loop
113 if ( $ads->post_count ) {
114 foreach ( $ads->posts as $_ad ) {
115 $row = '';
116 $ad_id = $_ad->ID;
117 $row .= '<tr data-ad-id="'. $ad_id . '"><td>' . $_ad->post_title . '</td><td>';
118 $row .= '<select name="advads-groups['. $group->id . '][ads]['.$_ad->ID.']">';
119 $ad_weight = (isset($weights[$ad_id])) ? $weights[$ad_id] : Advanced_Ads_Group::MAX_AD_GROUP_WEIGHT;
120 for ( $i = 0; $i <= Advanced_Ads_Group::MAX_AD_GROUP_WEIGHT; $i++ ) {
121 $row .= '<option ' . selected( $ad_weight, $i, false ) . '>' . $i . '</option>';
122 }
123 $row .= '</select</td><td><button type="button" class="advads-remove-ad-from-group button">x</button></td></tr>';
124 $ad_form_rows[$_ad->ID] = $row;
125 }
126 }
127 $ad_form_rows = $this->remove_empty_weights( $ad_form_rows );
128 // Restore original Post Data
129 wp_reset_postdata();
130
131 $ads_for_select = $this->ads_for_select();
132 $new_ad_weights = '<select class="advads-group-add-ad-list-weights">';
133 for ( $i = 0; $i <= Advanced_Ads_Group::MAX_AD_GROUP_WEIGHT; $i++ ) {
134 $new_ad_weights .= '<option ' . selected( 10, $i, false ) . '>' . $i . '</option>';
135 }
136 $new_ad_weights .= '</select>';
137
138 require ADVADS_BASE_PATH . 'admin/views/ad-group-list-form-row.php';
139 }
140
141 /**
142 * render the ads list
143 *
144 * @param $obj $group group object
145 */
146 public function render_ads_list(Advanced_Ads_Group $group){
147
148 $ads = $this->get_ads( $group );
149
150 $weights = $group->get_ad_weights();
151 $weight_sum = array_sum( $weights );
152 $ads_output = $weights;
153 arsort( $ads_output );
154
155 // The Loop
156 if ( $ads->have_posts() ) {
157 echo ($group->type == 'default' && $weight_sum) ? '<ul>' : '<ol>';
158 while ( $ads->have_posts() ) {
159 $ads->the_post();
160 $line_output = '<li><a href="' . get_edit_post_link( get_the_ID() ) . '">' . get_the_title() . '</a>';
161
162 $_weight = (isset($weights[get_the_ID()])) ? $weights[get_the_ID()] : Advanced_Ads_Group::MAX_AD_GROUP_WEIGHT;
163 if ( $group->type == 'default' && $weight_sum ) {
164 $line_output .= '<span class="ad-weight" title="'.__( 'Ad weight', 'advanced-ads' ).'">' . number_format( ($_weight / $weight_sum) * 100 ) .'%</span>';
165 }
166
167 $ad = new Advanced_Ads_Ad( get_the_ID() );
168 $expiry_date_format = get_option( 'date_format' ). ', ' . get_option( 'time_format' );
169 $post_start = get_the_date('U', $ad->id );
170
171 if ( $post_start > time() ) {
172 $line_output .= '<br />' . sprintf( __( 'starts %s', 'advanced-ads' ), date( $expiry_date_format, $post_start ) );
173 }
174 if ( isset( $ad->expiry_date ) && $ad->expiry_date ) {
175 $expiry = $ad->expiry_date;
176 $expiry_date = date_create( '@' . $expiry );
177 $tz_option = get_option( 'timezone_string' );
178
179 if ( $tz_option ) {
180 $expiry_date->setTimezone( Advanced_Ads_Admin::get_wp_timezone() );
181 } else {
182 $tz_name = Advanced_Ads_Admin::timezone_get_name( Advanced_Ads_Admin::get_wp_timezone() );
183 $tz_offset = substr( $tz_name, 3 );
184 $off_time = date_create( $expiry_date->format( 'Y-m-d\TH:i:s' ) . $tz_offset );
185 $offset_in_sec = date_offset_get( $off_time );
186 $expiry_date = date_create( '@' . ( $expiry + $offset_in_sec ) );
187 }
188
189 $TZ = ' ( ' . Advanced_Ads_Admin::timezone_get_name( Advanced_Ads_Admin::get_wp_timezone() ) . ' )';
190 if ( $expiry > time() ) {
191 $line_output .= '<br />' . sprintf( __( 'expires %s', 'advanced-ads' ), $expiry_date->format( $expiry_date_format ) ) . $TZ;
192 } elseif ( $expiry <= time() ) {
193 $line_output .= '<br />' . sprintf( __( '<strong>expired</strong> %s', 'advanced-ads' ), $expiry_date->format( $expiry_date_format ) ) . $TZ;
194 }
195 }
196 $line_output .= '</li>';
197
198 $ads_output[get_the_ID()] = $line_output;
199 }
200
201 $ads_output = $this->remove_empty_weights( $ads_output );
202
203 echo implode( '', $ads_output );
204 echo ($group->type == 'default' && $weight_sum) ? '</ul>' : '</ol>';
205
206 if( $ads->post_count > 4 ){
207 $hidden_ads = $ads->post_count - 3;
208 echo '<p><a href="javascript:void(0)" class="advads-group-ads-list-show-more">+ ' . sprintf(__( 'show %d more ads', 'advanced-ads' ), $hidden_ads ) . '</a></p>';
209 }
210
211 if ( $group->ad_count === 'all' ) {
212 echo '<p>' . __( 'all published ads are displayed', 'advanced-ads' ) . '</p>';
213 } elseif ( $group->ad_count > 1 ) {
214 echo '<p>' . sprintf( __( 'up to %d ads displayed', 'advanced-ads' ), $group->ad_count ) . '</p>';
215 }
216 } else {
217 _e( 'No ads assigned', 'advanced-ads' );
218 ?><br/><a class="edit">+ <?php _e( 'Add some', 'advanced-ads' ); ?></a><?php
219 }
220 // Restore original Post Data
221 wp_reset_postdata();
222 }
223
224 /**
225 * remove entries from the ad weight array that are just id
226 *
227 * @since 1.5.1
228 * @param arr $ads_output array with any output other that an integer
229 * @return arr $ads_output array with ad output
230 */
231 private function remove_empty_weights(array $ads_output){
232
233 foreach ( $ads_output as $key => $value ){
234 if ( is_int( $value ) ) {
235 unset($ads_output[$key]); }
236 }
237
238 return $ads_output;
239 }
240
241 /**
242 * get ads for this group
243 *
244 * @param obj $group group object
245 * @return obj $ads WP_Query result with ads for this group
246 */
247 public function get_ads($group){
248 $args = array(
249 'post_type' => $this->post_type,
250 'post_status' => array('publish', 'pending', 'future', 'private'),
251 'taxonomy' => $group->taxonomy,
252 'term' => $group->slug,
253 'posts_per_page' => -1
254 );
255 return $ads = new WP_Query( $args );
256 }
257
258 /**
259 * list of all ads to display in select dropdown
260 *
261 * @return array
262 */
263 public function ads_for_select(){
264 $select = array();
265 $model = Advanced_Ads::get_instance()->get_model();
266
267 // load all ads
268 $ads = $model->get_ads( array('orderby' => 'title', 'order' => 'ASC') );
269 foreach ( $ads as $_ad ){
270 $select[ $_ad->ID ] = esc_html( $_ad->post_title );
271 }
272
273 return $select;
274 }
275
276 /**
277 * return ad group types
278 *
279 * @return arr $types ad group information
280 */
281 public function get_ad_group_types(){
282 $types = array(
283 'default' => array(
284 'title' => __( 'Random ads', 'advanced-ads' ),
285 'description' => __( 'Display random ads based on ad weight', 'advanced-ads' )
286 ),
287 'ordered' => array(
288 'title' => __( 'Ordered ads', 'advanced-ads' ),
289 'description' => __( 'Display ads with the highest ad weight first', 'advanced-ads' ),
290 )
291 );
292
293 return apply_filters( 'advanced-ads-group-types', $types );
294 }
295
296 /**
297 * render ad group action links
298 *
299 * @param $obj $group group object
300 */
301 public function render_action_links($group){
302 global $tax;
303
304 $tax = get_taxonomy( $this->taxonomy );
305
306 $actions = array();
307 if ( current_user_can( $tax->cap->edit_terms ) ) {
308 $actions['edit'] = '<a class="edit">' . __( 'Edit', 'advanced-ads' ) . '</a>';
309 $actions['usage'] = '<a class="usage">' . __( 'Usage', 'advanced-ads' ) . '</a>';
310 }
311
312 if ( current_user_can( $tax->cap->delete_terms ) ){
313 $args = array(
314 'action' => 'delete',
315 'group_id' => $group->id
316 );
317 $delete_link = self::group_page_url( $args );
318 $actions['delete'] = "<a class='delete-tag' href='" . wp_nonce_url( $delete_link, 'delete-tag_' . $group->id ) . "'>" . __( 'Delete', 'advanced-ads' ) . '</a>';
319 }
320
321 if ( ! count( $actions ) ) { return; }
322
323 echo '<div class="row-actions">';
324 foreach ( $actions as $action => $link ) {
325 echo "<span class='$action'>$link</span>";
326 }
327 echo '</div>';
328 }
329
330 /**
331 * create a new group
332 *
333 */
334 public function create_group(){
335 // check nonce
336 if ( ! isset( $_POST['advads-group-add-nonce'] )
337 || ! wp_verify_nonce( $_POST['advads-group-add-nonce'], 'add-advads-groups' ) ){
338
339 return new WP_Error( 'invalid_ad_group', __( 'Invalid Ad Group', 'advanced-ads' ) );
340 }
341
342 // check user rights
343 if( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) {
344 return new WP_Error( 'invalid_ad_group_rights', __( 'You don’t have permission to change the ad groups', 'advanced-ads' ) );
345 }
346
347 if ( isset($_POST['advads-group-name']) && '' !== $_POST['advads-group-name'] ){
348
349 $title = sanitize_text_field( $_POST['advads-group-name'] );
350 $new_group = wp_create_term( $title, Advanced_Ads::AD_GROUP_TAXONOMY );
351
352 if( is_wp_error($new_group ) ){
353 return $new_group;
354 }
355
356 // save default values
357 if( is_array( $new_group ) ){
358 $group = new Advanced_Ads_Group( $new_group['term_id'] );
359
360 // allow other add-ons to save their own group attributes
361 $atts = apply_filters( 'advanced-ads-group-save-atts', array(
362 'type' => 'default',
363 'ad_count' => 0,
364 'options' => array(),
365 ), $group);
366
367 $group->save( $atts );
368 }
369
370 // reload groups
371 $this->load_groups();
372
373 } else {
374 return new WP_Error( 'no_ad_group_created', __( 'No ad group created', 'advanced-ads' ) );
375 }
376
377 return true;
378 }
379
380 /**
381 * bulk update groups
382 *
383 */
384 public function update_groups(){
385 // check nonce
386 if ( ! isset( $_POST['advads-group-update-nonce'] )
387 || ! wp_verify_nonce( $_POST['advads-group-update-nonce'], 'update-advads-groups' ) ){
388
389 return new WP_Error( 'invalid_ad_group', __( 'Invalid Ad Group', 'advanced-ads' ) );
390 }
391
392 // check user rights
393 if( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) {
394 return new WP_Error( 'invalid_ad_group_rights', __( 'You don’t have permission to change the ad groups', 'advanced-ads' ) );
395 }
396
397 /** empty group settings
398 * edit: emptying disabled, because when only a few groups are saved (e.g. when filtered by search), options are reset
399 * todo: needs a solution that also removes options when the group is removed
400 */
401 // update_option( 'advads-ad-groups', array() );
402 // empty weights
403 update_option( 'advads-ad-weights', array() );
404
405 // ad_id => group_ids
406 $ad_groups_assoc = array();
407
408 if ( isset( $_POST['advads-groups-removed-ads'] ) && is_array( $_POST['advads-groups-removed-ads'] ) ) {
409 foreach ( $_POST['advads-groups-removed-ads'] as $ad_id ) {
410 $ad_groups_assoc[ $ad_id ] = array();
411 }
412 }
413
414 // iterate through groups
415 if ( isset($_POST['advads-groups']) && count( $_POST['advads-groups'] ) ){
416
417 foreach ( $_POST['advads-groups'] as $_group_id => $_group ){
418 // save basic wp term
419 wp_update_term( $_group_id, Advanced_Ads::AD_GROUP_TAXONOMY, $_group );
420
421 $group = new Advanced_Ads_Group( $_group['id'] );
422 if ( isset( $_group['ads'] ) && is_array( $_group['ads'] ) ) {
423 // save ad weights
424 $group->save_ad_weights( $_group['ads'] );
425
426 foreach ( $_group['ads'] as $_ad_id => $_ad_weight ) {
427 $ad_groups_assoc[ $_ad_id ][] = (int) $_group_id;
428 }
429 }
430
431 // save other attributes
432 $type = isset($_group['type']) ? $_group['type'] : 'default';
433 $ad_count = isset($_group['ad_count']) ? $_group['ad_count'] : 1;
434 $options = isset($_group['options']) ? $_group['options'] : array();
435
436 // allow other add-ons to save their own group attributes
437 $atts = apply_filters( 'advanced-ads-group-save-atts', array(
438 'type' => $type,
439 'ad_count' => $ad_count,
440 'options' => $options,
441 ), $_group);
442
443 $group->save( $atts );
444 }
445
446 foreach ( $ad_groups_assoc as $_ad_id => $group_ids ) {
447 wp_set_object_terms( $_ad_id, $group_ids, $this->taxonomy);
448 }
449
450 }
451
452 // reload groups
453 $this->load_groups();
454
455 return true;
456 }
457
458 /**
459 * returns a link to the ad group list page
460 *
461 * @since 1.0.0
462 * @param arr $args additional arguments, e.g. action or group_id
463 * @return string admin url
464 */
465 public static function group_page_url($args = array()) {
466 $plugin = Advanced_Ads::get_instance();
467
468 $defaultargs = array(
469 // 'post_type' => constant("Advanced_Ads::POST_TYPE_SLUG"),
470 'page' => 'advanced-ads-groups',
471 );
472 $args = $args + $defaultargs;
473
474 return add_query_arg( $args, admin_url( 'admin.php' ) );
475 }
476
477 }