7.95, 'adsense-now' => 6.95, 'google-adsense' => 9.45); static $border = ''; static $kills = array('feed', 'page', 'sticky', 'home', 'front_page', 'category', 'tag', 'archive', 'search', 'single', 'attachment'); static $unSettable = array('Phone', 'Tablet', 'All', 'Mobile'); static $noAds = false; static $noAdsReason = ''; static function isActive() { if (strpos(__FILE__, 'mu-plugins') !== false) { return true; } if (class_exists("GoogleAdSense")) { return true; } if (!function_exists('is_plugin_active')) { include_once ABSPATH . 'wp-admin/includes/plugin.php'; } $plgSlug = self::getPlgMode(); $plgSlug = str_replace("-ultra", "", $plgSlug); $plugin = basename(dirname(__FILE__)) . "/$plgSlug.php"; if (is_plugin_active($plugin)) { return true; } if (is_plugin_active_for_network($plugin)) { return true; } return false; } static function isLoggedIn() { if (!defined('WP_USE_THEMES')) { define('WP_USE_THEMES', false); } if (!defined('WP_INSTALLING')) { define('WP_INSTALLING', true); } global $wpdb; // used within $wpHeader later for multisite. Do not remove! $isLoggedIn = false; // check from admin and ajax foreach (array("../../../..", "../../../../..") as $dir) { $wpHeader = "$dir/wp-blog-header.php"; if (@file_exists($wpHeader)) { require_once $wpHeader; break; } } if (function_exists('current_user_can')) { if (current_user_can('activate_plugins')) { $isLoggedIn = true; } } return $isLoggedIn; } static function doPluginActions() { $suspend_ads = self::getGenOption('suspend_ads'); if (!empty($suspend_ads)) { return; } $plugin_slug = self::getSlug(); require_once plugin_dir_path(__FILE__) . $plugin_slug . '-frontend.php'; } static function urlExists($url) {//se passar a URL existe $c = curl_init(); curl_setopt($c, CURLOPT_URL, $url); curl_setopt($c, CURLOPT_HEADER, 1); //get the header curl_setopt($c, CURLOPT_NOBODY, 1); //and *only* get the header curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); //get the response as a string from curl_exec(), rather than echoing it curl_setopt($c, CURLOPT_FRESH_CONNECT, 1); //don't use a cached version of the url if (!curl_exec($c)) { return false; } else { return true; } //$httpcode=curl_getinfo($c,CURLINFO_HTTP_CODE); //return ($httpcode<400); } static function validate_url($url) { $format = "Use the format http[s]://[www].site.com[/file[?p=v]]"; if (!filter_var($url, FILTER_VALIDATE_URL)) { $text = "$format"; return $text; } $pattern = '#^(http(?:s)?\:\/\/[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*\.[a-zA-Z]{2,6}(?:\/?|(?:\/[\w\-]+)*)(?:\/?|\/\w+\.[a-zA-Z]{2,4}(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*)$#'; if (!preg_match($pattern, $url)) { $text = "$format"; return $text; } if (!self::urlExists($url)) { $text = "URL not accessible"; return $text; } return true; } static function validate_email($s) { if (!filter_var($s, FILTER_VALIDATE_EMAIL)) { return "Bad email address"; } return true; } static function validate_notNull($s) { $s = trim($s); if (empty($s)) { return "Null value not allowed"; } return true; } static function validate_number($s) { if (!is_numeric($s)) { return "Need a number here"; } return true; } static function validate_alnum($s) { $aValid = array('_', '-'); $s = str_replace($aValid, '', $s); if (!ctype_alnum($s)) { return "Please use only letters, numbers, - and _"; } return true; } // AJAX CRUD implementation. Create. static function create() { // creates a new DB record self::update(); } // AJAX CRUD implementation. Delete. static function read() { // not implemented } // AJAX CRUD implementation. Update using wpdb->replace() static function update() { $posted_pk = $posted_name = $posted_value = $posted_validator = ""; if (!EzGA::isLoggedIn()) { http_response_code(400); die("Please login before changing options!"); } global $wpdb; if (empty($wpdb)) { http_response_code(400); die("Global variable wpdb not set!"); } $table = $wpdb->prefix . "ez_adsense_options"; $row = array(); extract($_POST, EXTR_PREFIX_ALL, 'posted'); if (empty($posted_pk)) { http_response_code(400); die("Empty primary key"); } if (empty($posted_name)) { http_response_code(400); die("Empty name ($posted_name) in data"); } if (!isset($posted_value)) { // Checkbox, unchecked $posted_value = 0; } if (is_array($posted_value)) { // Checkbox (from checklist), checked $posted_value = 1; } if (!empty($posted_validator)) { // a server-side validator is specified $fun = "validate_$posted_validator"; if (method_exists('EzGA', $fun)) { $valid = self::$fun($posted_value); } else { http_response_code(400); die("Unknown validator ($posted_validator) specified"); } if ($valid !== true) { http_response_code(400); die("$valid"); } } $row['name'] = $posted_pk; $row['value'] = $posted_value; foreach (array('plugin_slug', 'theme', 'provider', 'optionset') as $col) { $posted = "posted_$col"; if (!empty($$posted)) { $row[$col] = $$posted; } else { $row[$col] = "All"; } } $status = $wpdb->replace($table, $row); if ($status === false) { http_response_code(400); die("Database Replace/Insert Error"); } http_response_code(200); exit(); } // AJAX CRUD implementation. Delete. static function delete() { // not implemented } static function isAssoc($options) { $isAssoc = count(array_filter(array_keys($options), 'is_string')) > 0; return $isAssoc; } static function mkSelectSource($options) { $isAssoc = self::isAssoc($options); $source = "["; foreach ($options as $k => $o) { if ($isAssoc) { $source .= "{value: '$k', text: '$o'},"; } else { $source .= "{value: '$o', text: '$o'},"; } } $source .= "]"; return $source; } static function getThemes($killCurrent = true) { global $wpdb; $table = $wpdb->prefix . "ez_adsense_options"; $sql = "SELECT DISTINCT theme FROM $table "; $themsDB = $wpdb->get_results($sql); $themes = array(); foreach ($themsDB as $t) { if ($t->theme != 'All') { $themes[] = $t->theme; } } $theme = array(get_option('stylesheet')); if ($killCurrent) { $themes = array_diff($themes, $theme); } $themes = array_values($themes); return $themes; } static function getOptionSets($settableOnly = false) { global $wpdb; $table = $wpdb->prefix . "ez_adsense_options"; $plugin_slug = self::getSlug(); $sql = "SELECT DISTINCT optionset FROM $table WHERE plugin_slug = '$plugin_slug'"; $optionSetsDB = $wpdb->get_results($sql); $optionSets = array(); foreach ($optionSetsDB as $t) { if ($t->optionset != 'All') { $optionSets[] = $t->optionset; } } if ($settableOnly) { $unSettable = self::$unSettable; $unSettable[] = self::getGenOption('optionset'); $optionSets = array_diff($optionSets, $unSettable); } $optionSets = array_values($optionSets); return $optionSets; } static function setOptionset($name) { self::$options = self::getOptions($name); self::putGenOption('editing', $name); if (!in_array($name, self::$unSettable)) { self::putGenOption('optionset', $name); } } static function getAllOptions() { global $wpdb; $table = $wpdb->prefix . "ez_adsense_options"; $sql = "SELECT name, value FROM $table WHERE plugin_slug='All' AND theme='All' AND provider='All' AND optionset='All'"; $row = $wpdb->get_results($sql); $options = array(); if (!empty($row)) { foreach ($row as $r) { $options[$r->name] = $r->value; } } return $options; } static function getOptions($optionset = "", $provider = "AdSense") { $allOptions = self::getAllOptions(); global $wpdb; $table = $wpdb->prefix . "ez_adsense_options"; if (empty($optionset)) { if (is_admin()) { $optionset = self::getGenOption('editing'); } else { $optionset = self::getGenOption('optionset'); } } if (empty($optionset)) { $optionset = "Default"; self::setOptionset($optionset); } $plugin_slug = self::getSlug(); $lookup = array('google-adsense' => 'Google AdSense', 'easy-adsense' => 'Easy AdSense', 'adsense-now' => 'AdSense Now!'); $plugin = $lookup[$plugin_slug]; $theme = get_option('stylesheet'); self::putGenOption('theme', $theme); $sql = "SELECT name,value FROM $table WHERE plugin_slug='$plugin_slug' AND theme='$theme' AND provider='$provider' AND optionset='$optionset'"; $rows = $wpdb->get_results($sql); $defaultText = 'Please generate and paste your ad code here. If left empty, the ad location will be highlighted on your blog pages with a reminder to enter your code.'; $options = compact('plugin', 'theme', 'provider', 'optionset', 'plugin_slug', 'defaultText'); foreach ($rows as $row) { $options[$row->name] = $row->value; } self::$options = $options = array_merge($allOptions, $options); require "admin/$plugin_slug-options.php"; $defaults = EzGA::getDefaults($ezOptions); self::$options = $options = array_merge($defaults, $options); return $options; } static function info($hide = true) { if (!function_exists('get_plugin_data')) { require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); } $dir = plugin_dir_path(__FILE__); $slug = ''; foreach (self::$plgPrice as $plg => $price) { if (strpos($dir, $plg) !== false) { $slug = $plg; break; } } if (empty($slug)) { $slug = 'easy-adsense'; } $plugin_data = get_plugin_data("$dir$slug.php"); $str = "{$plugin_data['Name']} V{$plugin_data['Version']}"; if ($hide) { $str = ""; } return $str; } static function getDefaults($ezOptions, $echo = false) { $defaults = array(); $msg = '
';
      foreach ($ezOptions as $k => $v) {
        if (isset($v['value'])) {
          $defaults[$k] = $v['value'];
        }
        else {
          $msg .= "No default for $k\n";
        }
      }
      $msg .= "
"; if ($echo) { echo $msg; } return $defaults; } static function putDefaults($ezOptions, $optionset = '', $force = false, $provider = "AdSense") { $row = array(); foreach (array('plugin_slug', 'theme', 'provider', 'optionset') as $col) { $row[$col] = self::$options[$col]; } if (!empty($optionset)) { $row['optionset'] = $optionset; } global $wpdb; $table = $wpdb->prefix . "ez_adsense_options"; $defaults = self::getDefaults($ezOptions); foreach ($defaults as $name => $value) { if ($force || !isset(self::$options[$name])) { $row['name'] = $name; $row['value'] = $value; $wpdb->replace($table, $row); } } } static function renderOption($pk, $option) { $optionsDB = EzGA::$options; if (isset($optionsDB[$pk])) { $value = $optionsDB[$pk]; $option['value'] = $value; if (!empty($option['options']) && is_array($option['options']) && self::isAssoc($option['options'])) { $option['value'] = $option['options'][$value]; } } return self::renderRow($pk, $option); } static function renderOptionCell($pk, $option) { $optionsDB = EzGA::$options; if (isset($optionsDB[$pk])) { $value = $optionsDB[$pk]; $option['value'] = $value; if (!empty($option['type']) && $option['type'] == 'select' && !empty($option['options']) && is_array($option['options']) && self::isAssoc($option['options']) ) { $option['key'] = $value; $option['value'] = $option['options'][$value]; } } $name = $option['name']; if (!empty($option['type'])) { $type = $option['type']; } else { $type = 'text'; } $helpAttr = self::renderHelpAttr($pk, $option); switch ($type) { case 'textarea': $value = self::renderOptionValue($pk, $option); $cell = "
$value
"; break; case 'colorpicker': // render as a
with a colorpicker $value = self::renderOptionValue($pk, $option); $cell = "
$value
"; break; case 'colorpicker2': // render as a
with a wider colorpicker $option['type'] = 'colorpicker'; $value = self::renderOptionValue($pk, $option); $cell = "
$value
"; break; case 'radio': // render as $value = self::renderOptionValue($pk, $option); $cell = "$name"; foreach ($option['options'] as $k => $o) { if (!empty($optionsDB[$pk]) && $optionsDB[$pk] !== 0 && $optionsDB[$pk] == $o) { $checked = "checked='checked'"; } else { $checked = ''; } $cell .= ""; } $cell .= "\n"; break; case 'radio2': // render as a line if (empty($option['noCenter'])) { $class = "class='center-block'"; } else { $class = ""; } $cell = "
"; foreach ($option['options'] as $o => $v) { if (!empty($optionsDB[$pk]) && $optionsDB[$pk] !== 0 && $optionsDB[$pk] == $o) { $checked = "checked='checked'"; } else { $checked = ''; } $cell .= " $v"; } $cell .= "
\n"; break; case 'checkbox2': // render in two s $option['type'] = 'checkbox'; $value = self::renderOptionValue($pk, $option); $cell = "$name$value"; break; default: if (empty($option['noCenter'])) { $center = "center-block"; } else { $center = ""; } $value = self::renderOptionValue($pk, $option); $cell = "
$value
"; } return $cell; } static function renderHelpAttr($pk, $option, $tip = false) { $placement = $type = $dataMode = $name = $more_help = $help = ""; extract($option); if (empty($placement)) { if ($dataMode == 'popup' || $type == 'checkbox') { $placement = 'bottom'; } else { $placement = 'top'; } } if (!empty($more_help)) { $clickHelp = "class='btn-help'"; } else { $clickHelp = ''; } if ($tip) { return "data-toggle='tooltip' data-placement='top' title='$name
$help'p"; } else { return "data-content='$help' data-help='$more_help' data-toggle='popover' data-placement='$placement' data-trigger='hover' title='$name' $clickHelp"; } } static function renderOptionHelp($pk, $option, $size = "1.5em") { $type = $name = $more_help = $help = ""; extract($option); if ($type == 'hidden') { $tr = ''; return $tr; } if (!empty($more_help)) { $clickHelp = "class='btn-help'"; } else { $clickHelp = ''; } $helpAttr = self::renderHelpAttr($pk, $option); $str = "\n"; return $str; } static function renderRow($pk, $option) { $type = $name = ""; extract($option); if ($type == 'hidden') { $str = ''; return $str; } $str = "$name"; $str .= "" . self::renderOptionValue($pk, $option) . "" . self::renderOptionHelp($pk, $option) . "\n"; return $str; } static function renderOptionValue($pk, $option, $dbValue = "") { $type = 'text'; $dataMode = "data-mode='inline'"; $options = array(); // for select and radio controls $value = $more_help = $dataValue = $dataTpl = $dataSource = $slug = $validator = $button = $reveal = ""; extract($option); if ($dataMode == 'popup') { $dataMode = "data-mode='popup'"; } else { $dataMode = "data-mode='inline'"; } if (!empty($dbValue)) { $value = $dbValue; } if ($type == 'hidden') { $str = ''; return $str; } $dataType = "data-type='$type'"; if (!empty($more_help)) { $clickHelp = "class='btn-help'"; } else { $clickHelp = ''; } if (empty($slug)) { $slug = "$pk-value"; } switch ($type) { case 'radio': return ''; case 'no-edit': $class = "black"; break; case 'checkbox' : $class = "xedit-checkbox"; $dataType = "data-type='checklist'"; $dataMode = ''; $dataValue = "data-value='$value'"; if ($value) { $class .= ' btn-sm btn-success'; $value = ""; } else { $class .= ' btn-sm btn-danger'; $value = ""; } break; case 'select': $class = "xedit"; $dataType = "data-type='select'"; if (!empty($key)) { $dataValue = "data-value='$key'"; } else { $dataValue = "data-value='$value'"; } $dataSource = 'data-source="' . self::mkSelectSource($options) . '"'; break; case 'file': // special case, return from here $type = ''; $class = 'red'; $value = ""; break; case 'colorpicker': $str = ""; break; case 'submit': case 'button': $class = "btn btn-primary btn-ez"; break; case 'textarea': $str = "" . htmlspecialchars(stripslashes($value)) . ""; break; case 'dbselect': case 'dbeditableselect': case 'editableselect': case 'text': default : $class = "xedit"; break; } if (!empty($validator)) { $valid = "data-validator='$validator'"; } else { $valid = ""; } if (!empty($button)) { $fun = "proc_$reveal"; $options = self::$options; if (!empty($options[$reveal])) { $revealOption = $options[$reveal]; } else { $revealOption = ''; } if (method_exists("EzGA", $fun)) { $dataReveal = @self::$fun($revealOption); } else { $dataReveal = "data-value='$revealOption' class='btn-sm btn-success reveal'"; } $reveal = "$button"; } else { $reveal = ''; } if (empty($str)) { $str = "$value $reveal\n"; } return $str; } static function isUpdateAvailable() { // not ready yet return false; } static function randString($len = 32) { $chars = 'abcdefghijklmnopqrstuvwxyz'; $chars .= strtoupper($chars) . '0123456789'; $charLen = strlen($chars) - 1; $string = ''; for ($i = 0; $i < $len; $i++) { $pos = rand(0, $charLen); $string .= $chars[$pos]; } return $string; } static function flashMsg($msg, $class, $noflash = false, $noPre = false) { if ($noflash) { $fun = "show"; } else { $fun = "flash"; } $cleaned = str_replace(array("\n"), array('\n'), $msg); if (!empty($cleaned)) { $msg = htmlspecialchars($cleaned); if (!$noPre) { $msg = "
$msg
"; } echo ''; } } static function flashError($msg, $noPre = false) { self::flashMsg($msg, 'Error', false, $noPre); } static function showError($msg, $noPre = false) { self::flashMsg($msg, 'Error', true, $noPre); } static function flashWarning($msg, $noPre = false) { self::flashMsg($msg, 'Warning', false, $noPre); } static function showWarning($msg, $noPre = false) { self::flashMsg($msg, 'Warning', true, $noPre); } static function flashSuccess($msg, $noPre = false) { self::flashMsg($msg, 'Success', false, $noPre); } static function showSuccess($msg, $noPre = false) { self::flashMsg($msg, 'Success', true, $noPre); } static function flashInfo($msg, $noPre = false) { self::flashMsg($msg, 'Info', false, $noPre); } static function showInfo($msg, $noPre = false) { self::flashMsg($msg, 'Info', true, $noPre); } static function toggleMenu($header) { if (!empty($_SERVER["HTTP_REFERER"])) { $referer = $_SERVER["HTTP_REFERER"]; } else { $referer = ''; } $standAlone = (strpos($referer, 'wp-admin/options-general.php') === false) && !isset($_REQUEST['inframe']); if (!$standAlone) { $search = array('
', '