wo_meta = new WOMeta( AdCommander::ns() ); } /** * Assumes input has been sanitized and user has been authorized to make this request. */ public function list_ads( $ad_ids = array(), $post_statuses = array(), $detailed = false, $include_groups = false ) { $allowed_post_statuses = Abilities::allowed_post_statuses(); $post_statuses = array_intersect( $post_statuses, $allowed_post_statuses ); if ( empty( $post_statuses ) ) { $post_statuses = $allowed_post_statuses; } $singular = false; if ( ! is_array( $ad_ids ) ) { $singular = $ad_ids; $ad_ids = array( $ad_ids ); } $ads = Query::ads( 'post_title', 'asc', $post_statuses, array(), array(), $ad_ids ); if ( ! $ads || empty( $ads ) ) { return array(); } $parsed_ads = array(); foreach ( $ads as $ad ) { $ad_item = array( 'ad_id' => $ad->ID, 'ad_title' => $ad->post_title, 'ad_status' => $ad->post_status, ); if ( $include_groups ) { $groups = wp_get_post_terms( $ad->ID, AdCommander::tax_group(), array( 'fields' => 'all' ) ); $ad_item['groups'] = array(); if ( ! is_wp_error( $groups ) && ! empty( $groups ) ) { foreach ( $groups as $group ) { $ad_item['groups'][] = array( 'group_id' => $group->term_id, 'group_name' => $group->name, ); } } } if ( $detailed ) { $meta = $this->wo_meta->get_post_meta( $ad->ID, AdPostMeta::post_meta_keys() ); if ( $meta ) { $ad_type = $this->wo_meta->get_value( $meta, 'adtype', false ); $ad_item['ad_type'] = AdPostMeta::ad_types()[ $ad_type ] ?? $ad_type; if ( $ad_type === 'bannerad' ) { $ad_item['banner_linked_url'] = $this->wo_meta->get_value( $meta, 'bannerurl', false ); $attachment_id = get_post_thumbnail_id( $ad ); if ( $attachment_id ) { $image = wp_get_attachment_image_src( $attachment_id, 'full' ); $width = $this->wo_meta->get_value( $meta, 'display_width' ); $height = $this->wo_meta->get_value( $meta, 'display_height' ); if ( $image ) { $ad_item['banner_image_url'] = $image[0]; if ( ! $width ) { $width = $image[1]; } if ( ! $height ) { $height = $image[2]; } } $ad_item['banner_image_width'] = absint( $width ); $ad_item['banner_image_height'] = absint( $height ); } } elseif ( $ad_type === 'adsense' ) { $ad_item['adsense_ad_mode'] = $this->wo_meta->get_value( $meta, 'adsense_ad_mode', false ); } if ( ProBridge::instance()->is_pro_loaded() ) { $ad_item['expire_gmt'] = intval( $this->wo_meta->get_value( $meta, 'expire_gmt', false ) ); $ad_item['expire_clicks'] = intval( $this->wo_meta->get_value( $meta, 'expire_clicks', false ) ); $ad_item['expire_impressions'] = intval( $this->wo_meta->get_value( $meta, 'expire_impressions', false ) ); } } } $parsed_ads[] = $ad_item; } if ( $singular !== false ) { return array_column( $parsed_ads, null, 'ad_id' )[ $singular ] ?? array(); } return $parsed_ads; } }