示例#1
0
 public function get_ad_by_id($args)
 {
     if (isset($args['override'])) {
         return $args['override'];
     }
     if (!isset($args['id']) || $args['id'] == 0) {
         return;
     }
     // get ad
     $ad = new Advanced_Ads_Ad((int) $args['id'], $args);
     // check conditions
     if ($ad->can_display()) {
         return $ad->output();
     }
 }
 /**
  * prepare the ad post type to be saved
  *
  * @since 1.0.0
  * @param int $post_id id of the post
  * @todo handling this more dynamic based on ad type
  */
 public function save_ad($post_id)
 {
     // only use for ads, no other post type
     if (!isset($_POST['post_type']) || $this->post_type != $_POST['post_type'] || !isset($_POST['advanced_ad']['type'])) {
         return;
     }
     // don’t do this on revisions
     if (wp_is_post_revision($post_id)) {
         return;
     }
     // get ad object
     $ad = new Advanced_Ads_Ad($post_id);
     if (!$ad instanceof Advanced_Ads_Ad) {
         return;
     }
     // filter to allow change of submitted ad settings
     $_POST['advanced_ad'] = apply_filters('advanced-ads-ad-settings-pre-save', $_POST['advanced_ad']);
     $ad->type = $_POST['advanced_ad']['type'];
     if (isset($_POST['advanced_ad']['output'])) {
         $ad->set_option('output', $_POST['advanced_ad']['output']);
     } else {
         $ad->set_option('output', array());
     }
     /**
      * deprecated since introduction of "visitors" in 1.5.4
      */
     if (isset($_POST['advanced_ad']['visitor'])) {
         $ad->set_option('visitor', $_POST['advanced_ad']['visitor']);
     } else {
         $ad->set_option('visitor', array());
     }
     // visitor conditions
     if (isset($_POST['advanced_ad']['visitors'])) {
         $ad->set_option('visitors', $_POST['advanced_ad']['visitors']);
     } else {
         $ad->set_option('visitors', array());
     }
     // save size
     $ad->width = 0;
     if (isset($_POST['advanced_ad']['width'])) {
         $ad->width = absint($_POST['advanced_ad']['width']);
     }
     $ad->height = 0;
     if (isset($_POST['advanced_ad']['height'])) {
         $ad->height = absint($_POST['advanced_ad']['height']);
     }
     if (!empty($_POST['advanced_ad']['description'])) {
         $ad->description = esc_textarea($_POST['advanced_ad']['description']);
     } else {
         $ad->description = '';
     }
     if (!empty($_POST['advanced_ad']['content'])) {
         $ad->content = $_POST['advanced_ad']['content'];
     } else {
         $ad->content = '';
     }
     if (!empty($_POST['advanced_ad']['conditions'])) {
         $ad->conditions = $_POST['advanced_ad']['conditions'];
     } else {
         $ad->conditions = array();
     }
     // prepare expiry date
     if (isset($_POST['advanced_ad']['expiry_date']['enabled'])) {
         $year = absint($_POST['advanced_ad']['expiry_date']['year']);
         $month = absint($_POST['advanced_ad']['expiry_date']['month']);
         $day = absint($_POST['advanced_ad']['expiry_date']['day']);
         $hour = absint($_POST['advanced_ad']['expiry_date']['hour']);
         $minute = absint($_POST['advanced_ad']['expiry_date']['minute']);
         $expiration_date = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $year, $month, $day, $hour, $minute, '00');
         $valid_date = wp_checkdate($month, $day, $year, $expiration_date);
         if (!$valid_date) {
             $ad->expiry_date = 0;
         } else {
             // as PHP 5.2 has not reliable 'u' option need to calculate timestamps this way
             $gmDate = get_gmt_from_date($expiration_date, 'Y-m-d-H-i');
             list($year, $month, $day, $hour, $minute) = explode('-', $gmDate);
             $ad->expiry_date = gmmktime($hour, $minute, 0, $month, $day, $year);
         }
     } else {
         $ad->expiry_date = 0;
     }
     $ad->save();
 }
 /**
  * render the ads list
  *
  * @param $obj $group group object
  */
 public function render_ads_list(Advanced_Ads_Group $group)
 {
     $ads = $this->get_ads($group);
     $weights = $group->get_ad_weights();
     $weight_sum = array_sum($weights);
     $ads_output = $weights;
     arsort($ads_output);
     // The Loop
     if ($ads->have_posts()) {
         echo $group->type == 'default' && $weight_sum ? '<ul>' : '<ol>';
         while ($ads->have_posts()) {
             $ads->the_post();
             $line_output = '<li><a href="' . get_edit_post_link(get_the_ID()) . '">' . get_the_title() . '</a>';
             $status = get_post_status();
             switch ($status) {
                 case 'future':
                     $line_output .= '<i>(' . __('scheduled', ADVADS_SLUG) . ')</i>';
                     break;
                 case 'pending':
                     $line_output .= '<i>(' . __('pending', ADVADS_SLUG) . ')</i>';
                     break;
             }
             // check expiry date
             $ad = new Advanced_Ads_Ad(get_the_ID());
             if (!$ad->can_display_by_expiry_date()) {
                 $line_output .= '<i>(' . __('expired', ADVADS_SLUG) . ')</i>';
             }
             $_weight = isset($weights[get_the_ID()]) ? $weights[get_the_ID()] : Advanced_Ads_Group::MAX_AD_GROUP_WEIGHT;
             if ($group->type == 'default' && $weight_sum) {
                 $line_output .= '<span class="ad-weight" title="' . __('Ad weight', ADVADS_SLUG) . '">' . number_format($_weight / $weight_sum * 100) . '%</span></li>';
             }
             $ads_output[get_the_ID()] = $line_output;
         }
         $ads_output = $this->remove_empty_weights($ads_output);
         echo implode('', $ads_output);
         echo $group->type == 'default' && $weight_sum ? '</ul>' : '</ol>';
         if ($group->ad_count === 'all') {
             echo '<p>' . __('all published ads are displayed', ADVADS_SLUG) . '</p>';
         } elseif ($group->ad_count > 1) {
             echo '<p>' . sprintf(__('up to %d ads displayed', ADVADS_SLUG), $group->ad_count) . '</p>';
         }
     } else {
         _e('No ads assigned', ADVADS_SLUG);
     }
     // Restore original Post Data
     wp_reset_postdata();
 }