PluginProbe
Advanced Ads – Ad Manager & AdSense / 1.6
Advanced Ads – Ad Manager & AdSense v1.6
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 All 348 releases
advanced-ads / admin / includes / class-ad-groups-list.php
class-ad-groups-list.php
333 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * render a single row
89 *
90 * @param obj $group the ad group object
91 */
92 public function render_row($group){
93 $file = ADVADS_BASE_PATH . 'admin/views/ad-group-list-row.php';
94 require($file);
95 }
96
97 /**
98 * render the form row of a group
99 *
100 * @param obj $group the ad group object
101 */
102 public function render_form_row(Advanced_Ads_Group $group){
103
104 // query ads
105 $ads = $this->get_ads( $group );
106 $weights = $group->get_ad_weights();
107 $ad_form_rows = $weights;
108 arsort( $ad_form_rows );
109
110 // The Loop
111 if ( $ads->post_count ) {
112 foreach ( $ads->posts as $_ad ) {
113 $row = '';
114 $ad_id = $_ad->ID;
115 $row .= '<tr><td>' . $_ad->post_title . '</td><td>';
116 $row .= '<select name="advads-groups['. $group->id . '][ads]['.$_ad->ID.']">';
117 $ad_weight = (isset($weights[$ad_id])) ? $weights[$ad_id] : Advanced_Ads_Group::MAX_AD_GROUP_WEIGHT;
118 for ( $i = 0; $i <= Advanced_Ads_Group::MAX_AD_GROUP_WEIGHT; $i++ ) {
119 $row .= '<option ' . selected( $ad_weight, $i, false ) . '>' . $i . '</option>';
120 }
121 $row .= '</select></td></tr>';
122 $ad_form_rows[$_ad->ID] = $row;
123 }
124
125 $ad_form_rows = $this->remove_empty_weights( $ad_form_rows );
126 }
127 // Restore original Post Data
128 wp_reset_postdata();
129
130 $file = ADVADS_BASE_PATH . 'admin/views/ad-group-list-form-row.php';
131 require($file);
132 }
133
134 /**
135 * render the ads list
136 *
137 * @param $obj $group group object
138 */
139 public function render_ads_list(Advanced_Ads_Group $group){
140
141 $ads = $this->get_ads( $group );
142
143 $weights = $group->get_ad_weights();
144 $weight_sum = array_sum( $weights );
145 $ads_output = $weights;
146 arsort( $ads_output );
147
148 // The Loop
149 if ( $ads->have_posts() ) {
150 echo ($group->type == 'default' && $weight_sum) ? '<ul>' : '<ol>';
151 while ( $ads->have_posts() ) {
152 $ads->the_post();
153 $line_output = '<li><a href="' . get_edit_post_link( get_the_ID() ) . '">' . get_the_title() . '</a>';
154
155 $status = get_post_status();
156 switch ( $status ){
157 case 'future' :
158 $line_output .= '<i>(' . __( 'scheduled', ADVADS_SLUG ) . ')</i>';
159 break;
160 case 'pending' :
161 $line_output .= '<i>(' . __( 'pending', ADVADS_SLUG ) . ')</i>';
162 break;
163 }
164 // check expiry date
165 $ad = new Advanced_Ads_Ad( get_the_ID() );
166 if( ! $ad->can_display_by_expiry_date() ) {
167 $line_output .= '<i>(' . __( 'expired', ADVADS_SLUG ) . ')</i>';
168 }
169
170 $_weight = (isset($weights[get_the_ID()])) ? $weights[get_the_ID()] : Advanced_Ads_Group::MAX_AD_GROUP_WEIGHT;
171 if ( $group->type == 'default' && $weight_sum ) {
172 $line_output .= '<span class="ad-weight" title="'.__( 'Ad weight', ADVADS_SLUG ).'">' . number_format( ($_weight / $weight_sum) * 100 ) .'%</span></li>';
173 }
174 $ads_output[get_the_ID()] = $line_output;
175 }
176
177 $ads_output = $this->remove_empty_weights( $ads_output );
178
179 echo implode( '', $ads_output );
180 echo ($group->type == 'default' && $weight_sum) ? '</ul>' : '</ol>';
181 if ( $group->ad_count === 'all' ) {
182 echo '<p>' . __( 'all published ads are displayed', ADVADS_SLUG ) . '</p>';
183 } elseif ( $group->ad_count > 1 ) {
184 echo '<p>' . sprintf( __( 'up to %d ads displayed', ADVADS_SLUG ), $group->ad_count ) . '</p>';
185 }
186 } else {
187 _e( 'No ads assigned', ADVADS_SLUG );
188 }
189 // Restore original Post Data
190 wp_reset_postdata();
191 }
192
193 /**
194 * remove entries from the ad weight array that are just id
195 *
196 * @since 1.5.1
197 * @param arr $ads_output array with any output other that an integer
198 * @return arr $ads_output array with ad output
199 */
200 private function remove_empty_weights(array $ads_output){
201
202 foreach ( $ads_output as $key => $value ){
203 if ( is_int( $value ) ) {
204 unset($ads_output[$key]); }
205 }
206
207 return $ads_output;
208 }
209
210 /**
211 * get ads for this group
212 *
213 * @param obj $group group object
214 * @return obj $ads WP_Query result with ads for this group
215 */
216 public function get_ads($group){
217 $args = array(
218 'post_type' => $this->post_type,
219 'post_status' => array('publish', 'pending', 'future', 'private'),
220 'taxonomy' => $group->taxonomy,
221 'term' => $group->slug
222 );
223 return $ads = new WP_Query( $args );
224 }
225
226 /**
227 * return ad group types
228 *
229 * @return arr $types ad group information
230 */
231 public function get_ad_group_types(){
232 $types = array(
233 'default' => array(
234 'title' => __( 'Random ads', ADVADS_SLUG ),
235 'description' => __( 'Display random ads based on ad weight', ADVADS_SLUG )
236 ),
237 'ordered' => array(
238 'title' => __( 'Ordered ads', ADVADS_SLUG ),
239 'description' => __( 'Display ads with the highest ad weight first', ADVADS_SLUG ),
240 )
241 );
242
243 return apply_filters( 'advanced-ads-group-types', $types );
244 }
245
246 /**
247 * render ad group action links
248 *
249 * @param $obj $group group object
250 */
251 public function render_action_links($group){
252 global $tax;
253
254 $tax = get_taxonomy( $this->taxonomy );
255
256 $actions = array();
257 if ( current_user_can( $tax->cap->edit_terms ) ) {
258 $actions['edit'] = '<a class="edit">' . __( 'Edit', ADVADS_SLUG ) . '</a>';
259 $actions['usage'] = '<a class="usage">' . __( 'Usage', ADVADS_SLUG ) . '</a>';
260 }
261
262 if ( current_user_can( $tax->cap->delete_terms ) ){
263 $args = array(
264 'action' => 'delete',
265 'group_id' => $group->id
266 );
267 $delete_link = Advanced_Ads_Admin::group_page_url( $args );
268 $actions['delete'] = "<a class='delete-tag' href='" . wp_nonce_url( $delete_link, 'delete-tag_' . $group->id ) . "'>" . __( 'Delete' ) . '</a>';
269 }
270
271 if ( ! count( $actions ) ) { return; }
272
273 echo '<div class="row-actions">';
274 foreach ( $actions as $action => $link ) {
275 echo "<span class='$action'>$link</span>";
276 }
277 echo '</div>';
278 }
279
280 /**
281 * bulk update groups
282 *
283 */
284 public function update_groups(){
285 // check nonce
286 if ( ! isset( $_POST['advads-group-update-nonce'] )
287 || ! wp_verify_nonce( $_POST['advads-group-update-nonce'], 'update-advads-groups' ) ){
288
289 return new WP_Error( 'invalid_ad_group', __( 'Invalid Ad Group', ADVADS_SLUG ) );
290 }
291
292 // check user rights
293 if ( ! current_user_can( 'manage_options' ) ){
294 return new WP_Error( 'invalid_ad_group_rights', __( 'You don’t have permission to change the ad groups', ADVADS_SLUG ) );
295 }
296
297 // iterate through groups
298 if ( isset($_POST['advads-groups']) && count( $_POST['advads-groups'] ) ){
299 // empty group settings
300 update_option( 'advads-ad-groups', array() );
301
302 foreach ( $_POST['advads-groups'] as $_group_id => $_group ){
303 // save basic wp term
304 wp_update_term( $_group_id, Advanced_Ads::AD_GROUP_TAXONOMY, $_group );
305
306 // save ad weights
307 $group = new Advanced_Ads_Group( $_group['id'] );
308 if ( isset($_group['ads']) ) {
309 $group->save_ad_weights( $_group['ads'] ); }
310
311 // save other attributes
312 $type = isset($_group['type']) ? $_group['type'] : 'default';
313 $ad_count = isset($_group['ad_count']) ? $_group['ad_count'] : 1;
314 $options = isset($_group['options']) ? $_group['options'] : array();
315
316 // allow other add-ons to save their own group attributes
317 $atts = apply_filters( 'advanced-ads-group-save-atts', array(
318 'type' => $type,
319 'ad_count' => $ad_count,
320 'options' => $options,
321 ), $_group);
322
323 $group->save( $atts );
324 }
325 }
326
327 // reload groups
328 $this->load_groups();
329
330 return true;
331 }
332
333 }