Year = date("Y", current_time( 'timestamp' ));
$this->Month = date("m", current_time( 'timestamp' ));
$this->Day = date("d", current_time( 'timestamp' ));
$this->General = adsho_GetOptions(ADSHO_OPTIONS_GENERAL);
$this->TplPath = ADSHO_CONTENT_PATH.$this->General['tplpath'];
$this->TplUrl = ADSHO_CONTENT_URL.$this->General['tplpath'];
}
//=================================
public function holiday_handler($args) {
// check offline
if ($this->General['active'] != 'on')
return "";
// set args
$args = shortcode_atts( array (
'name' => '',
'active' => '',
'localdate' => '',
'localactive' => '',
'localshow' => '',
), $args);
return $this->holiday_function($args);
}
//=================================
public function holiday_function($args) {
global $wpdb;
// Filter chars and data
$args['name'] = strtolower(wp_specialchars_decode($args['name']));
$args['active'] = wp_specialchars_decode($args['active']);
$args['localdate'] = wp_specialchars_decode($args['localdate']);
$args['localactive'] = wp_specialchars_decode($args['localactive']);
$args['localshow'] = wp_specialchars_decode($args['localshow']);
$table_hol = $wpdb->prefix . ADSHO_TBLHOLIDAYS;
$table_eff = $wpdb->prefix . ADSHO_TBLEFFECTS;
$Sql = "SELECT * FROM $table_hol ".
"WHERE LOWER(Name) Like '".$args['name']."%';";
$HOS = $wpdb->get_results($Sql);
$text = "";
if ($HOS && count($HOS)){
foreach ($HOS as $HO) {
$TiAct = strtotime(date("Y-m-d", current_time( 'timestamp' )));
if ( $args['active'] != "*" ) {
$cn = empty($this->General['checkname']) ? '' : strtolower($this->General['checkname']);
if ( $cn == 'all' || $cn == strtolower($HO->Name) ) {
$HO->Active = $this->General['checkaction'];
if ( !empty($this->General['checkdate']) ) {
$TiAct = strtotime(date("Y-m-d", strtotime($this->General['checkdate'])));
$HO->Active = $this->General['checkaction'];
}
}
} else {
$HO->Active = ADSHO_ACTIVE_ON;
if ( !empty($args['localdate']) ) {
$TiAct = strtotime(date("Y-m-d", strtotime($args['localdate'])));
$HO->Active = ADSHO_ACTIVE_AUTOMATIC;
}
if ( !empty($args['localactive']) )
$HO->Active = $args['localactive'];
}
if ($HO->Active == ADSHO_ACTIVE_NONE ||
($HO->Active == ADSHO_ACTIVE_ADMIN && !is_admin()))
continue;
$TiHolFrom = $HO->DaFrom;
$TiHolTo = $HO->DaTo;
$Negate = false;
$text .= $this->calcDate($TiAct, $TiHolFrom, $TiHolTo, $Negate, $HO->Type);
if ($args['active'] == "*" && $args['localshow'] == 'on' )
echo "".$args['name'].": from=".date("Y-m-d",$TiHolFrom).", to=".date("Y-m-d",$TiHolTo).
", now=".date("Y-m-d",$TiAct)."
";
if ($HO->Active == ADSHO_ACTIVE_AUTOMATIC ) {
if ($Negate == false &&
!($TiAct >= $TiHolFrom && $TiAct <= $TiHolTo))
continue; // not into range
else if ($Negate == true &&
($TiAct < $TiHolFrom && $TiAct > $TiHolTo))
continue; // not into range
}
if (!empty($HO->Template))
$text .= $this->prepHtml($HO);
// add effekt
$Sql = "SELECT * FROM $table_eff ".
"WHERE Name Like '".$HO->Effect."' ORDER BY Name;";
$EF = $wpdb->get_results($Sql);
if ($EF && count($EF) > 0 ) {
$text .="\r\n";
$OldName = "-1";
$Last = "";
$First = true;
foreach($EF as $ef) {
if (strcasecmp($ef->Name, $OldName) != 0 )
$First = true;
if (strcasecmp($ef->CalcType, ADSHO_FUNCTION_FALLING) == 0) {
if ($First) {
$text .= $Last;
$text .='';
$First = false;
}
$siz = explode("-", $ef->Size);
$ma = count($siz) > 1 ? $siz[1] : $siz[0];
$text .= "Falling(".$ef->Count.",[";
if ($ef->ImgName == "textmode"){
$CHARS = explode(",", $ef->TextChars);
foreach($CHARS as $c) {// * \u2604 \u2744
$s = rand($siz[0],$ma);
$text .= "\"".esc_html($c)."\",";
}
$text = substr($text, 0, strlen($text)-1);
} else {
$pos = strpos($ef->ImgName, "*");
if( $pos > 0 ) {
$Img = str_ireplace(".", "\.", '/'.$ef->ImgName.'$/i');
$Img = str_ireplace("*", ".+", $Img); // '/^.+\.xml$/i';
$IMAGES = $this->getFilesToArray($this->TplPath.ADSHO_FALLING_DIR, $Img) ;
} else
$IMAGES = array($ef->ImgName);
foreach($IMAGES as $Img) {
$s = rand($siz[0],$ma);
$text .= "\"
\",";
}
$text = substr($text, 0, strlen($text)-1);
}
$text .= "],".$ef->TimeSec.");";
}
$OldName = $ef->Name;
}
$text .= $Last;
}
}
}
return $text;
}
#=================================================
private function getFilesToArray($dir, $search = '/^.+\.xml$/i') {
$Directory = new RecursiveDirectoryIterator($dir);
//$Iterator = new RecursiveIteratorIterator($Directory, RecursiveIteratorIterator::CHILD_FIRST);
$Regex = new RegexIterator($Directory, $search, RecursiveRegexIterator::GET_MATCH);
$files = array();
foreach ($Regex as $file) {
array_push($files, $file[0]);
}
return $files;
}
#=================================================
private function calcDate(&$TiAct, &$TiHolFrom, &$TiHolTo, &$Negate, $Type) {
$y = array("ccyy", "yy", "y");
$m = array("mm", "m");
$d = array("dd", "d");
$TiHolFrom = str_ireplace($y, date("Y", $TiAct), $TiHolFrom);
$TiHolFrom = str_ireplace($m, date("m", $TiAct), $TiHolFrom);
$TiHolFrom = str_ireplace($d, date("d", $TiAct), $TiHolFrom);
$TiHolTo = str_ireplace($y, date("Y", $TiAct), $TiHolTo);
$TiHolTo = str_ireplace($m, date("m", $TiAct), $TiHolTo );
$TiHolTo = str_ireplace($d, date("d", $TiAct), $TiHolTo );
// parse other, add/subtract day, month, year..
$oneDay = (24 * 3600);
$year = date("Y", $TiAct);
if ( $Type == ADSHO_TYPE_EASTER) {
$Etime = $this->getMyEasterSunday($year);
$TiHolFrom = $Etime - (3 * $oneDay); // -3 day = Thursday, Friday, Sadurday
$TiHolTo = $Etime + (1 * $oneDay); // + 1 day = Monday
} else if ( $Type >= ADSHO_TYPE_ADVENT) {
$TiHolFrom = $this->getMyFirstAdvent(2017, 1);
$TiHolTo = $mktime(0,0,0,12,24,$year);
} else{
$TiHolFrom = strtotime($TiHolFrom);
$TiHolTo = strtotime($TiHolTo);
}
if (empty($TiHolTo))
$TiHolTo = $TiHolFrom;
if ($TiHolFrom > $TiHolTo)
$Negate = true;
return ""; // return errors
}
#=================================================
private function prepHtml($HO) {
$lang = $HO->Lang == 'on' ? "-".substr(get_locale(),0,2) : "";
$Path = $this->TplPath.$HO->Template.$lang.".html";
if ($this->General['showerror'] == 'on')
$text = sprintf("
Error to get html template %s or pictures.
", $HO->Name);
else
$text= '';
$i = 0;
$img = array();
if (file_exists($Path)) {
$text = "";
$fp = fopen($Path, 'r');
if ($fp) {
$text = fread($fp, filesize($Path));
fclose($fp);
}
$img = $this->getFilesToArray($this->TplPath, '/'.$HO->Template.'.*\.(gif|jpg|png|jpeg)$/i');
$i = rand(0,count($img)-1);
}
$text = str_ireplace("%lang", substr(get_locale(),0,2), $text);
$text = str_ireplace("%imagepath", $this->TplUrl, $text);
$im = !empty($img[$i]) ? $img[$i] : "";
$text = str_ireplace("%picture", $im, $text);
return $text;
}
#=================================================
/*private function getEasterSunday($Year) {
$Days = easter_days($Year);
return mktime(0, 0, 0, 3, 21+$Days,$Year);
} */
#=================================================
private function getMyEasterSunday($Year) {
try {
$nMonth = 0;
$nDay = 0;
$nMoon = 0;
$nEpact = 0;
$nSunday = 0;
$nGold = 0;
$nCent = 0;
$nCorx = 0;
$nCorz = 0;
//The Golden Number of the year in the 19 year Metonic Cycle:
$nGold = ($Year % 19) + 1;
// Calculate the Century:
$nCent = intval($Year / 100) + 1;
// Number of years in which leap year was dropped in order...
// to keep in step with the sun:
$nCorx = intval((3 * $nCent) / 4) - 12;
// Special correction to syncronize Easter with moon's orbit:
$nCorz = ((8 * $nCent + 5) / 25) - 5;
// Find Sunday:
$nSunday = intval((5 * $Year) / 4) - $nCorx - 10;
// ^ To prevent overflow at year 6554
// Set Epact - specifies occurrence of full moon:
$nEpact = (11 * $nGold + 20 + $nCorz - $nCorx) % 30;
if ($nEpact < 0)
$nEpact = $nEpact + 30;
if ((($nEpact == 25) && ($nGold > 11)) || ($nEpact == 24))
$nEpact = $nEpact + 1;
// Find Full Moon:
$nMoon = 44 - $nEpact;
if ($nMoon < 21)
$nMoon = $nMoon + 30;
// Advance to Sunday:
$nMoon = $nMoon + 7 - (($nSunday + $nMoon) % 7);
if ($nMoon > 31) {
$nMonth = 4;
$nDay = $nMoon - 31;
} else {
$nMonth = 3;
$nDay = $nMoon;
}
return mktime(0,0,0,$nMonth, $nDay, $Year);
//strtotime(sprintf("%04d-%02d-%02d", $Year, $nMonth, $nDay));
}
catch (Exception $ex) { }
return mktime(0,0,0,1,1,2000);
}
private function getMyFirstAdvent($Year, $Advent) {
// Must be 11.27 and 12.03
$nTime = mktime(0,0,0,12,25,$Year);
$nWeekDay = date("w", $nTime);
$subDays = $nWeekDay+((4-$Advent)*7);
return strtotime("-".$subDays." days", $nTime);
}
}
#=================================================
add_shortcode( ADSHO_PLUGIN_NAMESCHO ,
array( adsho_clsShortCode::get_instance(), 'holiday_handler') );
?>