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 | } |