groups = 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 getGroup( $id ) { $group[ 'details' ] = get_post( $id ); $group[ 'meta' ] = get_post_meta( $id ); return $group; } public function getGroups( $limit = - 1 ) { $args = array( 'post_type' => 'adfoxly_ad_group', 'posts_per_page' => $limit ); // $args = array( // 'meta_query' => array( // array( // 'key' => 'adfoxly_groups', // 'value' => '123' // ) // ), //// 'post_type' => 'adfoxly_groups', // 'posts_per_page' => $limit // ); // $groups = get_option('adfoxly_groups'); return get_posts( $args ); } public function getAllGroupsFromAdzoneByID( $id, $limit = - 1 ) { $args = array( 'meta_query' => array( array( 'key' => 'adfoxly-ad-group', 'value' => $id ) ), 'post_type' => 'adfoxly_groups', 'posts_per_page' => $limit ); return get_posts( $args ); } public function insert( $post ) { $data = array( 'post_title' => sanitize_text_field( $post[ 'adfoxly-group-name' ] ), 'post_status' => 'publish', 'post_type' => 'adfoxly_ad_group', '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-group-id' ] ), 'post_title' => sanitize_text_field( $post[ 'adfoxly-group-name' ] ), ); $result = wp_update_post( $data ); return $result; } public function insert_meta( $result ) { $post_id = $result; $groups = new adfoxlyGroupsSettings(); if ( isset( $_POST[ 'adfoxly-group-ads-list' ] ) && ! empty( isset( $_POST[ 'adfoxly-group-ads-list' ] ) ) && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { $adfoxly_group_ads_list = array_map( 'intval', wp_unslash( $_POST[ 'adfoxly-group-ads-list' ] ) ); update_post_meta( $post_id, 'adfoxly-ad-group', $adfoxly_group_ads_list ); } else if ( ! isset( $_POST[ 'adfoxly-group-ads-list' ] ) && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { delete_post_meta( $post_id, 'adfoxly-ad-group' ); } if ( isset( $_POST[ 'adfoxly-group-is-rotate' ] ) && ! empty( isset( $_POST[ 'adfoxly-group-is-rotate' ] ) ) && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { $adfoxly_group_is_rotate = sanitize_text_field( $_POST[ 'adfoxly-group-is-rotate' ] ); update_post_meta( $post_id, 'adfoxly-group-is-rotate', $adfoxly_group_is_rotate ); } if ( isset( $_POST[ 'adfoxly-group-how-rotate' ] ) && ! empty( isset( $_POST[ 'adfoxly-group-how-rotate' ] ) ) && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { $adfoxly_group_how_rotate = sanitize_text_field( $_POST[ 'adfoxly-group-how-rotate' ] ); update_post_meta( $post_id, 'adfoxly-group-how-rotate', $adfoxly_group_how_rotate ); } if ( isset( $_POST[ 'adfoxly-group-rotate-time' ] ) && ! empty( isset( $_POST[ 'adfoxly-group-rotate-time' ] ) ) && isset( $_REQUEST[ '_wpnonce' ] ) && wp_verify_nonce( $_REQUEST[ '_wpnonce' ] ) ) { $adfoxly_group_rotate_time = sanitize_text_field( $_POST[ 'adfoxly-group-rotate-time' ] ); update_post_meta( $post_id, 'adfoxly-group-rotate-time', $adfoxly_group_rotate_time ); } } public function getBannersByGroup( $id ) { $adGroup = get_post_meta( $id, 'adfoxly-ad-group', true ); $activeAds = array(); if ( isset( $adGroup ) && ! empty( $adGroup ) ) { foreach ( $adGroup as $ad ): $start = get_post_meta( $ad, 'adfoxly-ad-campaign-start', true ); $end = get_post_meta( $ad, 'adfoxly-ad-campaign-end', true ); $today = date( 'Y-m-d' ); $campaignsAdsArgs = array( 'meta_query' => array( array( 'key' => 'adfoxly-ad-campaign', 'value' => $ad, 'compare' => 'LIKE' ) ), 'post_type' => 'adfoxly_ad_campaign', 'posts_per_page' => - 1 ); $campaignsAds = get_posts( $campaignsAdsArgs ); if ( isset( $campaignsAds ) && ! empty( $campaignsAds ) ) { $campaignStart = get_post_meta( $campaignsAds[0]->ID, 'adfoxly-ad-campaign-start', true ); $campaignEnd = get_post_meta( $campaignsAds[0]->ID, 'adfoxly-ad-campaign-end', true ); } if ( isset( $campaignStart ) && ! empty( $campaignStart ) && isset( $campaignEnd ) && ! empty( $campaignEnd ) && $campaignStart <= $today && $campaignEnd >= $today ) { array_push( $activeAds, $ad ); } else if ( // if campaign start before today than ( $start <= $today && // if campaign ends after than today $end >= $today ) || // or if start or end is not set than show ( empty($start) && empty( $end ) && empty( $campaignStart ) && empty( $campaignEnd ) ) ) { array_push( $activeAds, $ad ); } endforeach; } return $activeAds; } public function renderStartOfWrapper( $id ) { $g_is_rotate = get_post_meta( $id, 'adfoxly-group-is-rotate', true ); $g_how_rotate = get_post_meta( $id, 'adfoxly-group-how-rotate', true ); $g_time_rotate = get_post_meta( $id, 'adfoxly-group-rotate-time', 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 getIpLocation() { $ip = Adfoxly::ip(); if ( $ip === '::1' || $ip === '::' ) { $ip = '104.27.178.42'; } $ipApi = wp_remote_get( 'http://ip-api.com/json/' . $ip . '?fields=status,message,country,countryCode,region,regionName,city,zip,timezone' ); return $ipApi[ 'body' ]; } public function getCityFromIP() { $json = json_decode( $this->getIpLocation() ); return $json[ 'body' ]; } public function checkCurrentCity() { } public function listOfCountries() { return array( "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegowina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Congo, the Democratic Republic of the", "Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia (Hrvatska)", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji", "Finland", "France", "France Metropolitan", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard and Mc Donald Islands", "Holy See (Vatican City State)", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan", "Lao, People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia, The Former Yugoslav Republic of", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of", "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation", "Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovakia (Slovak Republic)", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "Spain", "Sri Lanka", "St. Helena", "St. Pierre and Miquelon", "Sudan", "Suriname", "Svalbard and Jan Mayen Islands", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan, Province of China", "Tajikistan", "Tanzania, United Republic of", "Thailand", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", "Virgin Islands (British)", "Virgin Islands (U.S.)", "Wallis and Futuna Islands", "Western Sahara", "Yemen", "Yugoslavia", "Zambia", "Zimbabwe" ); } public function validateCampaignDate( $value ) { $start = get_post_meta( $value->ID, 'adfoxly-ad-campaign-start', true ); $end = get_post_meta( $value->ID, 'adfoxly-ad-campaign-end', true ); $today = date( 'Y-m-d' ); $args = array( 'meta_query' => array( array( 'key' => 'adfoxly-ad-campaign', 'value' => $value->ID, 'compare' => 'LIKE' ) ), 'post_type' => 'adfoxly_ad_campaign', 'posts_per_page' => - 1 ); $ads = get_posts( $args ); if ( isset( $ads ) && empty( $ads ) ) { if ( ( empty( $start ) && empty( $end ) ) || ( $start <= $today && $end >= $today ) ) { return true; } } else { $campaignStart = get_post_meta( $ads[ 0 ]->ID, 'adfoxly-ad-campaign-start', true ); $campaignEnd = get_post_meta( $ads[ 0 ]->ID, 'adfoxly-ad-campaign-end', true ); if ( ( empty( $campaignStart ) && empty( $campaignStart ) ) || ( $campaignStart <= $today && $campaignEnd >= $today ) ) { return true; } } return false; } public function validateCountry( $value ) { $args = array( 'meta_query' => array( array( 'key' => 'adfoxly-ad-campaign', 'value' => $value->ID, 'compare' => 'LIKE' ) ), 'post_type' => 'adfoxly_ad_campaign', 'posts_per_page' => - 1 ); $ads = get_posts( $args ); if ( ! empty( $ads ) ) { $excludedCountries = get_post_meta( $ads[ 0 ]->ID, 'adfoxly-campaign-excluded-countries', true ); $allowedCountries = get_post_meta( $ads[ 0 ]->ID, 'adfoxly-campaign-allowed-countries', true ); $excludedRegion = get_post_meta( $ads[ 0 ]->ID, 'adfoxly-campaign-excluded-region', true ); $allowedRegion = get_post_meta( $ads[ 0 ]->ID, 'adfoxly-campaign-allowed-region', true ); if ( empty( $excludedCountries ) && empty( $allowedCountries ) && empty( $excludedRegion ) && empty( $allowedRegion ) ) { return true; } if ( ! empty( $allowedCountries ) && in_array( $this->visitor->country, $allowedCountries ) ) { return true; } if ( ! empty( $excludedCountries ) && ! in_array( $this->visitor->country, $excludedCountries ) ) { return true; } if ( ! empty( $allowedRegion ) && ( in_array( $this->visitor->city, $allowedRegion ) || in_array( $this->visitor->regionName, $allowedRegion ) ) ) { return true; } if ( ! empty( $excludedRegion ) && ( ! in_array( $this->visitor->city, $excludedRegion ) && ! in_array( $this->visitor->regionName, $excludedRegion ) ) ) { return true; } } else if ( empty( $ads ) ) { return true; } return false; } public function validateCampaignDays( $value ) { $args = array( 'meta_query' => array( array( 'key' => 'adfoxly-ad-campaign', 'value' => $value->ID, 'compare' => 'LIKE' ) ), 'post_type' => 'adfoxly_ad_campaign', 'posts_per_page' => - 1 ); $ads = get_posts( $args ); if ( ! empty( $ads ) ) { $adfoxlyCampaignDays = get_post_meta( $ads[ 0 ]->ID, 'adfoxly-campaign-days', true ); $adfoxlyCampaignHours = get_post_meta( $ads[ 0 ]->ID, 'adfoxly-campaign-specific-hours', true ); if ( empty( $adfoxlyCampaignDays ) ) { return true; } $today = strtolower( date('l') ); if ( ! empty( $adfoxlyCampaignDays ) && in_array( $today, $adfoxlyCampaignDays ) ) { if ( empty( $adfoxlyCampaignHours ) ) { return true; } if ( ! empty( $adfoxlyCampaignHours ) && ! in_array( $today, $adfoxlyCampaignHours ) ) { return true; } if ( ! empty( $adfoxlyCampaignHours ) && in_array( $today, $adfoxlyCampaignHours ) ) { // todo: check timezone instead of server time $hour = date("g:i A"); $adfoxlyCampaignHourStart = get_post_meta( $ads[ 0 ]->ID, 'adfoxly-campaign-specific-hour-start-' . $today, true ); $adfoxlyCampaignHourEnd = get_post_meta( $ads[ 0 ]->ID, 'adfoxly-campaign-specific-hour-end-' . $today, true ); if ( strtotime($adfoxlyCampaignHourStart) <= strtotime($hour) && strtotime($adfoxlyCampaignHourEnd) >= strtotime($hour) ) { return true; } } } return false; } } public function validateUserViews( $value ) { $stats = new StatisticsController(); $stats->getFingerPrint(); $args = array( 'meta_query' => array( array( 'key' => 'adfoxly-ad-campaign', 'value' => $value->ID, 'compare' => 'LIKE' ) ), 'post_type' => 'adfoxly_ad_campaign', 'posts_per_page' => - 1 ); $ads = get_posts( $args ); if ( ! empty( $ads ) ) { $adfoxlyCampaignUserViews = get_post_meta( $ads[ 0 ]->ID, 'adfoxly-campaign-maxviews-user', true ); if ( isset( $adfoxlyCampaignUserViews ) && ( ! empty( $adfoxlyCampaignUserViews ) || $adfoxlyCampaignUserViews != 0 ) ) { global $wpdb; $countFingerPrint = $wpdb->get_var( "SELECT count(*) FROM " . $wpdb->prefix . "adfoxly_statistics_views WHERE banner_id = '" . $value->ID . "' AND fingerprint = '" . $stats->getFingerPrint() . "'" ); if ( $countFingerPrint > $adfoxlyCampaignUserViews ) { return false; } } } } public function validateAdInGroup( $id ) { foreach ( $this->getBannersByGroup( $id ) as $key => $value ) { $ad = (object) array( 'ID' => $value ); if ( $this->validateCampaignDate( $ad ) !== false && $this->validateCountry( $ad ) !== false && $this->validateCampaignDays( $ad ) !== false && $this->validateUserViews( $ad ) !== false ) { $post = get_post( $value ); if ( isset( $post ) && ! empty( $post ) ) { array_push( $this->meta, array( 'id' => $post->ID, 'post_title' => $post->post_title, 'meta' => get_post_meta( $post->ID ) ) ); } } } } public function renderGroup( $id ) { $this->group = $this->groups[ $id ]; foreach ( $this->getBannersByGroup( $id ) as $key => $value ) { $this->validateAdInGroup( $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; } }