'adfoxly_banners', 'posts_per_page' => $limit ); return get_posts( $args ); } public function getAllBannersFromAdzoneByID( $id, $limit = - 1 ) { $args = array( 'meta_query' => array( array( 'key' => 'adfoxly-adzone', 'value' => $id ) ), 'post_type' => 'adfoxly_banners', 'posts_per_page' => $limit ); return get_posts( $args ); } public function insert( $post ) { $data = array( 'post_title' => sanitize_text_field( $post[ 'adfoxly-ad-name' ] ), 'post_status' => 'publish', 'post_type' => 'adfoxly_banners', 'post_author' => get_current_user_id(), ); $result = wp_insert_post( $data ); return $result; } public function update( $post ) { $data = array( 'ID' => sanitize_text_field( $post[ 'adfoxly-banner-id' ] ), 'post_title' => sanitize_text_field( $post[ 'adfoxly-ad-name' ] ), ); $result = wp_update_post( $data ); return $result; } public function insert_meta( $result ) { $post_id = $result; $image = new adfoxlyFormatImage(); foreach ( $image->meta_fields as $meta_field ): if ( $meta_field[ 'id' ] !== 'adfoxly-image_button' && $meta_field[ 'id' ] !== 'adfoxly-ad-preview' ) { if ( empty ( sanitize_text_field( $_POST[ $meta_field[ 'id' ] ] ) ) ) { $error[ $meta_field[ 'id' ] ] = 'Empty value'; } else { update_post_meta( $post_id, sanitize_text_field( $meta_field[ 'id' ] ), sanitize_text_field( $_POST[ $meta_field[ 'id' ] ] ) ); } } if ( $meta_field[ 'id' ] === 'adfoxly-adsense-code' ) { update_post_meta( $post_id, sanitize_text_field( $meta_field[ 'id' ] ), $_POST[ $meta_field[ 'id' ] ] ); } endforeach; $format = new adfoxlyFormats(); foreach ( $format->meta_fields as $meta_field ): if ( empty ( $_POST[ $meta_field[ 'id' ] ] ) ) { $error[ $meta_field[ 'id' ] ] = 'Empty value'; } else { update_post_meta( $post_id, sanitize_text_field( $meta_field[ 'id' ] ), sanitize_text_field( $_POST[ $meta_field[ 'id' ] ] ) ); } endforeach; $place = new adfoxlyPlaces(); foreach ( $place->meta_fields as $meta_field ): if ( empty ( $_POST[ $meta_field[ 'id' ] ] ) ) { $error[ $meta_field[ 'id' ] ] = 'Empty value'; } else { $add_place = array_map( 'sanitize_text_field', wp_unslash( $_POST[ $meta_field[ 'id' ] ] ) ); update_post_meta( $post_id, sanitize_text_field( $meta_field[ 'id' ] ), $add_place ); } endforeach; $options = new adfoxlyAdOptions(); foreach ( $options->meta_fields as $meta_field ): if ( empty ( $_POST[ $meta_field[ 'id' ] ] ) ) { $error[ $meta_field[ 'id' ] ] = 'Empty value'; } else { update_post_meta( $post_id, sanitize_text_field( $meta_field[ 'id' ] ), sanitize_text_field( $_POST[ $meta_field[ 'id' ] ] ) ); } endforeach; if ( $_POST['adfoxly-campaign-exists'] === 'no-exists' ) { $campaigns = new CampaignController(); $campaignResult = $campaigns->insert( $_POST ); if ( isset( $campaignResult ) && ! is_wp_error( $campaignResult ) ) { $campaigns->insert_meta( $campaignResult ); update_post_meta( $campaignResult, 'adfoxly-ad-campaign', array( $result ) ); } } else if ( $_POST['adfoxly-campaign-exists'] === 'exists' ) { /* * todo: for now in afoxly-banners&edit=XXX user cannot save unselected campaign. saves only selected. * todo: there is no such mechanism to remove if is unselected **/ $g_id = $_GET[ 'edit' ]; $g_id_int = intval( $g_id ); $campaignsAdsArgs = array( // 'meta_query' => array( // array( // 'key' => 'adfoxly-ad-campaign', // 'value' => $g_id_int, // 'compare' => 'LIKE' // ) // ), 'post_type' => 'adfoxly_ad_campaign', 'posts_per_page' => -1, 'fields' => 'ids' ); $campaignsAds = get_posts( $campaignsAdsArgs ); if ( isset( $campaignsAds ) && ! empty( $campaignsAds ) ) { foreach ( $campaignsAds as $campaign ): $arrayOfAdsInCampaign = get_post_meta( $campaign, 'adfoxly-ad-campaign', true ); if ( in_array( $g_id_int, $arrayOfAdsInCampaign ) ) { // banner is in the campaign db if ( isset( $_POST[ 'adfoxly-wizard-campaign-list' ] ) && ! empty( $_POST[ 'adfoxly-wizard-campaign-list' ] ) ) { // list is not empty. check and uncheck only this item which need be checked or unchecked foreach ( $arrayOfAdsInCampaign as $key => $value ): if ( $value === $g_id_int ): unset( $arrayOfAdsInCampaign[ $key ] ); endif; endforeach; update_post_meta( $campaign, 'adfoxly-ad-campaign', $arrayOfAdsInCampaign ); } else { // list is empty. remove from every meta foreach ( $arrayOfAdsInCampaign as $key => $value ): if ( $value === $g_id_int ): unset( $arrayOfAdsInCampaign[ $key ] ); endif; endforeach; update_post_meta( $campaign, 'adfoxly-ad-campaign', $arrayOfAdsInCampaign ); } } endforeach; } if ( ! empty( $_POST[ 'adfoxly-wizard-campaign-list' ] ) ) { foreach ( $_POST[ 'adfoxly-wizard-campaign-list' ] as $item ): $campaignAdsList = get_post_meta( $item, 'adfoxly-ad-campaign', true ); // if campaign is not empty if ( isset( $campaignAdsList ) && ! empty( $campaignAdsList ) ) { // if banner is not in the campaign - add if ( ! in_array( $post_id, $campaignAdsList ) ) { array_push( $campaignAdsList, $post_id ); update_post_meta( $item, 'adfoxly-ad-campaign', $campaignAdsList ); } } else { // if campaign is empty $campaignAdsList = array( $post_id ); // todo here is adding int, in other places string update_post_meta( $item, 'adfoxly-ad-campaign', $campaignAdsList ); } endforeach; } } else { // $campaign = new adfoxlyAdCampaign(); // foreach ( $campaign->meta_fields as $meta_field ): // if ( empty ( $_POST[ $meta_field[ 'id' ] ] ) ) { // $error[ $meta_field[ 'id' ] ] = 'Empty value'; // } else { // update_post_meta( $post_id, sanitize_text_field( $meta_field[ 'id' ] ), sanitize_text_field( $_POST[ $meta_field[ 'id' ] ] ) ); // } // endforeach; } } }