'; $api_response = (array)$ex->error; foreach($api_response['errors'] as $field => $errors) { $error .= ucwords(str_replace('_', ' ', $field)) . '
'; } } return $error; } /* * Determine whether the current site has been hooked up * with Broadstreet */ public static function hasNetwork() { return (bool)self::getOption(self::KEY_NETWORK_ID, false); } /* * Determine whether the current site has been hooked up * with Broadstreet */ public static function hasAdserving($enabled = null, $email = false) { $success = false; if($enabled !== null) { self::setOption (Broadstreet_Adwidget_Mini_Utility::KEY_ADSERVER_ENABLED, (bool)$enabled); $message = $enabled ? 'Subscribed' : 'Unsubscribed'; if($enabled) $success = self::importOldAds($email); return $success; } else { return (bool)self::getOption(self::KEY_ADSERVER_ENABLED, false); } } /* * Determine whether the current site has been hooked up * with Broadstreet */ public static function getNetworkID() { return self::getOption(self::KEY_NETWORK_ID, false); } /** * Escape a javascript tag * @param string $tag */ public static function escapeJSTag($tag) { $tag = str_ireplace('', '\x3C/script>', $tag); return $tag; } /** * Get a Broadstreet API client * @return Broadstreet */ public static function getClient() { $key = self::getOption(self::KEY_API_KEY); if($key) return new Broadstreet($key); else return new Broadstreet(); } /** * Does this account have any free ads left? */ public static function hasFreeAds() { $net_id = self::getOption(Broadstreet_Mini::KEY_NETWORK_ID); # If we haven't seen them before they probably do if(!$net_id) return true; $net = self::getClient()->getNetwork($net_id); # Check if you've used up all the hacks # Note to l33t haxors. We check on the server side too. return ($net->comp_count < $net->comp_count_max); } /** * Run a function which may or may not be defined * @param string $name * @param array $args */ public static function runHook($name, $args = array()) { if(function_exists($name)) { return call_user_func($name, $args); } return null; } /** * Print a link to open an editable link * @param type $label_or_markup */ public static function editableLink($label_or_markup = false, $key = 'solo') { if(!$label_or_markup) $label_or_markup = 'Create Editable'; echo ''.$label_or_markup.''; } /** * Output JS for placing the ad HTML into the * new ad form */ public static function editableJS($selector = false, $key = 'solo') { bs_editable_js($selector, $key); } /** * Is this phone number valid? * @param string $num * @return boolean */ public static function isPhoneValid($num) { if(preg_match('/^[+]?([0-9]?[0-9]?[0-9]?)[(|s|-|.]?([0-9]{3})[)|s|-|.]*([0-9]{3})[s|-|.]*([0-9]{4})$/', $num)) { return true; } else { return false; } } /** * Import or update an image ad * @param type $network_id Network Id * @param type $advertiser_id Advertiser Id * @param type $image_url Image URL * @param type $link Destination URL */ public static function importImageAd($network_id, $advertiser_id, $image_url, $link, $advertisement_id = false) { $api = self::getClient(); $img = wp_remote_get($image_url); $img = $img['body']; $params = array(); $params['name'] = 'WordPress Widget Ad ' . date('Y-m-d H:i:s'); $params['active_base64'] = base64_encode($img); $params['destination'] = $link; try { if($advertisement_id === false) return $api->createAdvertisement($network_id, $advertiser_id, $params['name'], 'static', $params); else return $api->updateAdvertisement($network_id, $advertiser_id, $advertisement_id, $params); } catch(Exception $ex) { exit($ex->__toString()); return false; } } /** * Import or update an HTML Ad into Broadstreet * @param type $network_id * @param type $advertiser_id * @param type $html */ public static function importHTMLAd($network_id, $advertiser_id, $html, $advertisement_id = false) { $api = self::getClient(); $params = array(); $params['name'] = 'WordPress Widget Ad ' . date('Y-m-d H:i:s'); $params['html'] = $html; try { if($advertisement_id === false) return $api->createAdvertisement($network_id, $advertiser_id, $params['name'], 'html', $params); else return $api->updateAdvertisement($network_id, $advertiser_id, $advertisement_id, $params); } catch(Exception $ex) { exit($ex->__toString()); return false; } } /** * Import old adwidget ads to Broadstreet ads * @param string $email */ public static function importOldAds($email = false) { $api = new Broadstreet(); try { if(!Broadstreet_Adwidget_Mini_Utility::hasNetwork()) { # Register the user by email address $user = $api->register($email); Broadstreet_Adwidget_Mini_Utility::setOption(Broadstreet_Adwidget_Mini_Utility::KEY_API_KEY, $user->access_token); # Create a network for the new user # Don't change this unless you want a higher tier. There's no lower tier, you haxor you $net = $api->createNetwork('Wordpress - ' . get_bloginfo('name'), array('tier_id' => 4)); Broadstreet_Adwidget_Mini_Utility::setOption(Broadstreet_Adwidget_Mini_Utility::KEY_NETWORK_ID, $net->id); } else { $api = self::getClient(); $net = (object)array('id' => Broadstreet_Adwidget_Mini_Utility::getNetworkID()); } /* Import Image widgets */ $ads = Broadstreet_Adwidget_Mini_Utility::getOption('widget_adwidget_imagewidget'); foreach($ads as $id => $data) { /* Ad already imported? Skip it */ if(!is_numeric($id) || is_numeric(@$data['bs_ad_id'])) continue; $adv = $api->createAdvertiser($net->id, self::arrayGet($data, 'w_adv', 'New Advertiser - Image')); Broadstreet_Adwidget_Mini_Utility::setOption(Broadstreet_Adwidget_Mini_Utility::KEY_ADVERTISER_ID, $adv->id); $ad = self::importImageAd($net->id, $adv->id, $data['w_img'], $data['w_link']); if(!$ad) continue; $ads[$id]['bs_ad_html'] = $ad->html; $ads[$id]['bs_ad_id'] = $ad->id; $ads[$id]['bs_adv_id'] = $adv->id; } Broadstreet_Adwidget_Mini_Utility::setOption('widget_adwidget_imagewidget', $ads); /* Import HTML widgets */ $ads = Broadstreet_Adwidget_Mini_Utility::getOption('widget_adwidget_htmlwidget'); foreach($ads as $id => $data) { /* Ad already imported? Skip it */ if(!is_numeric($id) || is_numeric(@$data['bs_ad_id'])) continue; $adv = $api->createAdvertiser($net->id, self::arrayGet($data, 'w_adv', 'New Advertiser - HTML')); Broadstreet_Adwidget_Mini_Utility::setOption(Broadstreet_Adwidget_Mini_Utility::KEY_ADVERTISER_ID, $adv->id); $ad = self::importHTMLAd($net->id, $adv->id, $data['w_adcode']); if(!$ad) continue; $ads[$id]['bs_ad_html'] = $ad->html; $ads[$id]['bs_ad_id'] = $ad->id; $ads[$id]['bs_adv_id'] = $adv->id; } Broadstreet_Adwidget_Mini_Utility::setOption('widget_adwidget_htmlwidget', $ads); } catch(Exception $ex) { return false; } return true; } } endif;