campaigns = get_option( 'adfoxly_places' ); $this->meta = array(); $this->render = null; $settings = get_option( 'adfoxly_settings' ); if ( ! isset( $settings[ 'redirect-slug' ] ) || empty( $settings[ 'redirect-slug' ] ) ) { $settings[ 'redirect-slug' ] = 'rdir-adfoxly'; } $this->settings = $settings; $this->statistics = new StatisticsController(); } public function getCampaign( $id ) { $campaign[ 'details' ] = get_post( $id ); $campaign[ 'meta' ] = get_post_meta( $id ); return $campaign; } public function getCampaigns( $limit = - 1 ) { $args = array( 'post_type' => 'adfoxly_ad_campaign', 'posts_per_page' => $limit ); // $args = array( // 'meta_query' => array( // array( // 'key' => 'adfoxly_campaigns', // 'value' => '123' // ) // ), //// 'post_type' => 'adfoxly_campaigns', // 'posts_per_page' => $limit // ); // $campaigns = get_option('adfoxly_campaigns'); return get_posts( $args ); } public function getAllCampaignsFromAdzoneByID( $id, $limit = - 1 ) { $args = array( 'meta_query' => array( array( 'key' => 'adfoxly-ad-campaign', 'value' => $id ) ), 'post_type' => 'adfoxly_campaigns', 'posts_per_page' => $limit ); return get_posts( $args ); } public function insert( $post ) { $data = array( 'post_title' => sanitize_text_field( $post[ 'adfoxly-campaign-name' ] ), 'post_status' => 'publish', 'post_type' => 'adfoxly_ad_campaign', '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-campaign-id' ] ), 'post_title' => sanitize_text_field( $post[ 'adfoxly-campaign-name' ] ), ); $result = wp_update_post( $data ); return $result; } public function insert_meta( $result ) { $post_id = $result; $campaigns = new adfoxlyCampaignsSettings(); if ( isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { if ( isset( $_POST[ 'adfoxly-campaign-ads-list' ] ) && ! empty( isset( $_POST[ 'adfoxly-campaign-ads-list' ] ) ) && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { $adfoxly_campaign_ads_list = array_map( 'intval', $_POST[ 'adfoxly-campaign-ads-list' ] ); update_post_meta( $post_id, 'adfoxly-ad-campaign', $adfoxly_campaign_ads_list ); } else if ( ! isset( $_POST[ 'adfoxly-campaign-ads-list' ] ) && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { delete_post_meta( $post_id, 'adfoxly-ad-campaign' ); } if ( isset( $_POST[ 'adfoxly-ad-options-maxviews' ] ) && ! empty( isset( $_POST[ 'adfoxly-ad-options-maxviews' ] ) ) && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { update_post_meta( $post_id, 'adfoxly-ad-options-maxviews', sanitize_text_field( $_POST[ 'adfoxly-ad-options-maxviews' ] ) ); } if ( isset( $_POST[ 'adfoxly-ad-options-maxclicks' ] ) && ! empty( isset( $_POST[ 'adfoxly-ad-options-maxclicks' ] ) ) && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { update_post_meta( $post_id, 'adfoxly-ad-options-maxclicks', sanitize_text_field( $_POST[ 'adfoxly-ad-options-maxclicks' ] ) ); } if ( isset( $_POST[ 'adfoxly-ad-campaign-start' ] ) && ! empty( isset( $_POST[ 'adfoxly-ad-campaign-start' ] ) ) && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { update_post_meta( $post_id, 'adfoxly-ad-campaign-start', sanitize_text_field( $_POST[ 'adfoxly-ad-campaign-start' ] ) ); } if ( isset( $_POST[ 'adfoxly-ad-campaign-end' ] ) && ! empty( isset( $_POST[ 'adfoxly-ad-campaign-end' ] ) ) && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { update_post_meta( $post_id, 'adfoxly-ad-campaign-end', sanitize_text_field( $_POST[ 'adfoxly-ad-campaign-end' ] ) ); } if ( isset( $_POST[ 'adfoxly-campaign-excluded-countries' ] ) && ! empty( isset( $_POST[ 'adfoxly-campaign-excluded-countries' ] ) ) && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { $adfoxly_campaign_excluded_countries = array_map( 'strval', $_POST[ 'adfoxly-campaign-excluded-countries' ] ); update_post_meta( $post_id, 'adfoxly-campaign-excluded-countries', $adfoxly_campaign_excluded_countries ); } else if ( ! isset( $_POST[ 'adfoxly-campaign-excluded-countries' ] ) || empty( $_POST[ 'adfoxly-campaign-excluded-countries' ] ) ) { delete_post_meta( $post_id, 'adfoxly-campaign-excluded-countries' ); } if ( isset( $_POST[ 'adfoxly-campaign-allowed-countries' ] ) && ! empty( isset( $_POST[ 'adfoxly-campaign-allowed-countries' ] ) ) && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { $adfoxly_campaign_allowed_countries = array_map( 'strval', $_POST[ 'adfoxly-campaign-allowed-countries' ] ); update_post_meta( $post_id, 'adfoxly-campaign-allowed-countries', $adfoxly_campaign_allowed_countries ); } else if ( ! isset( $_POST[ 'adfoxly-campaign-allowed-countries' ] ) || empty( $_POST[ 'adfoxly-campaign-allowed-countries' ] ) ) { delete_post_meta( $post_id, 'adfoxly-campaign-allowed-countries' ); } if ( isset( $_POST[ 'adfoxly-campaign-excluded-region' ] ) && ! empty( isset( $_POST[ 'adfoxly-campaign-excluded-region' ] ) ) && $_POST[ 'adfoxly-campaign-excluded-region' ] !== '' && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { $adfoxly_campaign_excluded_region_array = explode(",", $_POST[ 'adfoxly-campaign-excluded-region' ] ); $adfoxly_campaign_excluded_region = array_map( 'strval', $adfoxly_campaign_excluded_region_array ); update_post_meta( $post_id, 'adfoxly-campaign-excluded-region', $adfoxly_campaign_excluded_region ); } else if ( ( ! isset( $_POST[ 'adfoxly-campaign-excluded-region' ] ) || empty( $_POST[ 'adfoxly-campaign-excluded-region' ] ) || $_POST[ 'adfoxly-campaign-excluded-region' ] === '' ) && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { delete_post_meta( $post_id, 'adfoxly-campaign-excluded-region' ); } if ( isset( $_POST[ 'adfoxly-campaign-allowed-region' ] ) && ! empty( isset( $_POST[ 'adfoxly-campaign-allowed-region' ] ) ) && $_POST[ 'adfoxly-campaign-excluded-region' ] !== '' && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { $adfoxly_campaign_allowed_region_array = explode(",", $_POST[ 'adfoxly-campaign-allowed-region' ] ); $adfoxly_campaign_allowed_region = array_map( 'strval', $adfoxly_campaign_allowed_region_array ); update_post_meta( $post_id, 'adfoxly-campaign-allowed-region', $adfoxly_campaign_allowed_region ); } else if ( ( ! isset( $_POST[ 'adfoxly-campaign-allowed-region' ] ) || empty( $_POST[ 'adfoxly-campaign-allowed-region' ] ) || $_POST[ 'adfoxly-campaign-excluded-region' ] === '' ) && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { delete_post_meta( $post_id, 'adfoxly-campaign-allowed-region' ); } // adfoxly-campaign-days - array // adfoxly-campaign-specific-hours - array // adfoxly-campaign-specific-hour-start-saturday // adfoxly-campaign-specific-hour-start-[DAY] - string if ( isset( $_POST[ 'adfoxly-campaign-days' ] ) && ! empty( isset( $_POST[ 'adfoxly-campaign-days' ] ) ) && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { $adfoxly_campaign_days = array_map( 'strval', $_POST[ 'adfoxly-campaign-days' ] ); update_post_meta( $post_id, 'adfoxly-campaign-days', $adfoxly_campaign_days ); } else if ( ! isset( $_POST[ 'adfoxly-campaign-days' ] ) || empty( $_POST[ 'adfoxly-campaign-days' ] ) ) { delete_post_meta( $post_id, 'adfoxly-campaign-days' ); } if ( isset( $_POST[ 'adfoxly-campaign-specific-hours' ] ) && ! empty( isset( $_POST[ 'adfoxly-campaign-specific-hours' ] ) ) && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { $adfoxly_campaign_specific_hours = array_map( 'strval', $_POST[ 'adfoxly-campaign-specific-hours' ] ); update_post_meta( $post_id, 'adfoxly-campaign-specific-hours', $adfoxly_campaign_specific_hours ); } else if ( ! isset( $_POST[ 'adfoxly-campaign-specific-hours' ] ) || empty( $_POST[ 'adfoxly-campaign-specific-hours' ] ) ) { delete_post_meta( $post_id, 'adfoxly-campaign-specific-hours' ); } $days = array( 'monday' => 'Monday', 'tuesday' => 'Tuesday', 'wednesday' => 'Wednesday', 'thursday' => 'Thursday', 'friday' => 'Friday', 'saturday' => 'Saturday', 'sunday' => 'Sunday' ); foreach ( $days as $key => $day ) { delete_post_meta( $post_id, 'adfoxly-campaign-specific-hour-start-' . $key ); delete_post_meta( $post_id, 'adfoxly-campaign-specific-hour-end-' . $key ); } if ( isset( $_POST[ 'adfoxly-campaign-specific-hours' ] ) && ! empty( $_POST[ 'adfoxly-campaign-specific-hours' ] ) ) { foreach ( $_POST[ 'adfoxly-campaign-specific-hours' ] as $day ) { $start = $_POST[ 'adfoxly-campaign-specific-hour-start-' . $day ]; $end = $_POST[ 'adfoxly-campaign-specific-hour-end-' . $day ]; if ( isset( $start ) && isset( $end ) && ! empty( $start ) && ! empty( $end ) ) { update_post_meta( $post_id, 'adfoxly-campaign-specific-hour-start-' . $day , $start ); update_post_meta( $post_id, 'adfoxly-campaign-specific-hour-end-' . $day , $end ); } } } else { foreach ( $days as $key => $day ) { delete_post_meta( $post_id, 'adfoxly-campaign-specific-hour-start-' . $key ); delete_post_meta( $post_id, 'adfoxly-campaign-specific-hour-end-' . $key ); } } if ( isset( $_POST[ 'adfoxly-campaign-maxviews-user' ] ) && ! empty( isset( $_POST[ 'adfoxly-campaign-maxviews-user' ] ) ) && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { update_post_meta( $post_id, 'adfoxly-campaign-maxviews-user', sanitize_text_field( $_POST[ 'adfoxly-campaign-maxviews-user' ] ) ); } } } public function getBannersByCampaign( $id ) { $adCampaign = get_post_meta( $id, 'adfoxly-ad-campaign', true ); return $adCampaign; } public function renderStartOfWrapper( $id ) { $g_is_rotate = get_post_meta( $id, 'adfoxly-ad-options-maxclicks', true ); $g_how_rotate = get_post_meta( $id, 'adfoxly-ad-campaign-start', true ); $g_time_rotate = get_post_meta( $id, 'adfoxly-ad-campaign-end', true ); $render = '
'; } public function renderAd( $value, $first ) { if ( $value[ 'meta' ][ 'adfoxly-format' ][ 0 ] === '2' && isset( $value[ 'meta' ][ 'adfoxly-adsense-code' ] ) ) { $render = $this->renderAdsenseAd( $value, $first ); } else if ( $value[ 'meta' ][ 'adfoxly-format' ][ 0 ] === '1' && isset( $value[ 'meta' ][ 'adfoxly-image' ] ) ) { $render = $this->renderImageAd( $value, $first ); } if ( isset( $render ) ) { return $render; } } public function renderAdsenseAd( $value, $first ) { $render = 'settings[ 'redirect-slug' ] . '/' . $value[ 'id' ] . '"'; $render .= ' data-adfoxly-banner-id="' . $value[ 'id' ] . '"'; $render .= ' class="'; if ( true === $first ): $render .= 'active'; endif; $render .= '">'; $render .= $value[ 'meta' ][ 'adfoxly-adsense-code' ][ 0 ]; // $render .= ''; return $render; } public function renderImageAd( $value, $first ) { $render = 'settings[ 'redirect-slug' ] . '/' . $value[ 'id' ] . '"'; $render .= 'title="' . $value[ 'post_title' ] . '"'; $render .= 'data-adfoxly-banner-id="' . $value[ 'id' ] . '"'; $render .= 'target="_blank" rel="nofollow" class="'; if ( true === $first ): $render .= 'active'; endif; $render .= '">'; $render .= ''; $render .= ''; return $render; } public function renderCampaign( $id ) { $this->campaign = $this->campaigns[ $id ]; foreach ( $this->getBannersByCampaign( $id ) as $key => $value ) { $post = get_post( $value ); array_push( $this->meta, array( 'id' => $post->ID, 'post_title' => $post->post_title, 'meta' => get_post_meta( $post->ID ) ) ); } // todo $shuffleMeta = $this->meta; shuffle( $shuffleMeta ); if ( isset( $this->meta ) && ! empty( $this->meta[ 0 ] ) ) { $currentBannerMeta = get_post_meta( $this->meta[ 0 ][ 'id' ] ); } if ( ! empty( $currentBannerMeta ) ): $this->render .= $this->renderStartOfWrapper( $id ); $first = true; foreach ( $shuffleMeta as $key => $value ): $this->render .= $this->renderAd( $value, $first ); $first = false; endforeach; $this->render .= $this->renderEndOfWrapper(); $this->render .= $this->statistics->render( $id ); $this->statistics->saveView( $id ); return $this->render; endif; } }