/**
  * @return SLN_Wrapper_Booking[]
  */
 public function getBookingsByHour($hour, $minutes = null)
 {
     if (!isset($hour)) {
         $hour = $this->getDate()->format('H');
     }
     if (isset($this->cache[$hour . $minutes])) {
         return $this->cache[$hour . $minutes];
     }
     $ret = array();
     $now = clone $this->getDate();
     $now->setTime($hour, $minutes ? $minutes : 0);
     foreach ($this->getBookings() as $b) {
         if ($b->getStartsAt() <= $now && $b->getEndsAt() >= $now) {
             $ret[] = $b;
         }
     }
     if (!empty($ret)) {
         SLN_Plugin::addLog(__CLASS__ . ' - checking hour(' . $hour . ')');
         SLN_Plugin::addLog(__CLASS__ . ' - found(' . count($ret) . ')');
         foreach ($ret as $b) {
             SLN_Plugin::addLog(' - ' . $b->getId() . ' => ' . $b->getStartsAt()->format('H:i') . ' - ' . $b->getEndsAt()->format('H:i'));
         }
     } else {
         SLN_Plugin::addLog(__CLASS__ . ' - checking hour(' . $hour . ') EMPTY');
     }
     $this->cache[$hour] = $ret;
     return $ret;
 }
 public static function execute($force = false)
 {
     $data = (require SLN_PLUGIN_DIR . '/_install_data.php');
     $ids = array();
     foreach ($data['posts'] as $label => $post) {
         if (!self::checkPost($post['post']['post_title'], $post['post']['post_type'])) {
             $id = wp_insert_post($post['post']);
             if (isset($post['meta'])) {
                 foreach ($post['meta'] as $k => $v) {
                     add_post_meta($id, $k, $v);
                 }
             }
             $ids[$label] = $id;
         }
     }
     if (!get_option(SLN_Settings::KEY)) {
         if (isset($ids['thankyou'])) {
             $data['settings']['thankyou'] = $ids['thankyou'];
         }
         if (isset($ids['booking'])) {
             $data['settings']['booking'] = $ids['booking'];
         }
         update_option(SLN_Settings::KEY, $data['settings']);
     }
     new SLN_UserRole_SalonStaff(SLN_Plugin::getInstance(), SLN_Plugin::USER_ROLE_STAFF, __('Salon staff', 'sln'));
 }
 public function countServicesByHour($hour = null, $minutes = null)
 {
     SLN_Plugin::addLog(__CLASS__ . ' - count services by hour(' . $hour . ')');
     $ret = array();
     foreach ($this->getBookingsByHour($hour, $minutes) as $b) {
         foreach ($b->getServicesIds() as $id) {
             $ret[$id] = 1 + (isset($ret[$id]) ? $ret[$id] : 0);
         }
     }
     SLN_Plugin::addLog(print_r($ret, true));
     return $ret;
 }
 public function getTimes($date)
 {
     $ret = array();
     $avItems = $this->getItems();
     $hb = $this->getHoursBeforeHelper();
     foreach (SLN_Func::getMinutesIntervals() as $time) {
         $d = new DateTime($date->format('Y-m-d') . ' ' . $time);
         if ($avItems->isValidDatetime($d) && $this->isValidDate($d) && $this->isValidTime($d)) {
             $ret[$time] = $time;
         }
     }
     SLN_Plugin::addLog(__CLASS__ . ' getTimes ' . print_r($ret, true));
     return $ret;
 }
 function evalDuration()
 {
     $h = 0;
     $i = 0;
     SLN_Plugin::addLog(__CLASS__ . ' eval duration of' . $this->getId());
     foreach ($this->getServices() as $s) {
         $d = $s->getDuration();
         $h = $h + intval($d->format('H'));
         $i = $i + intval($d->format('i'));
         SLN_Plugin::addLog(' - service ' . $s . ' +' . $d->format('H:i'));
     }
     $i += $h * 60;
     if ($i == 0) {
         $i = 60;
     }
     $str = SLN_Func::convertToHoursMins($i);
     update_post_meta($this->getId(), '_sln_booking_duration', $str);
     return $str;
 }
 public function setDatetime(DateTime $date)
 {
     if ($timezone = get_option('timezone_string')) {
         date_default_timezone_set($timezone);
     }
     $this->initialDate = $this->bindInitialDate($date);
     $ah = $this->availabilityHelper;
     $times = $ah->getTimes($date);
     $i = 0;
     while (empty($times) && $i < 100) {
         $date->modify('+1 days');
         $times = $ah->getTimes($date);
         $i++;
     }
     if (empty($times)) {
         $date->modify('-99 days');
         while (empty($times) && $i > 0) {
             $date->modify('-1 days');
             $times = $ah->getTimes($date);
             $i--;
         }
     }
     $this->times = $times;
     $suggestedTime = $date->format('H:i');
     $i = SLN_Plugin::getInstance()->getSettings()->getInterval();
     $timeout = 0;
     while ($timeout < 86400 && !isset($times[$suggestedTime])) {
         $date->modify("+{$i} minutes");
         $suggestedTime = $date->format('H:i');
         $timeout++;
     }
     $this->suggestedDate = $date;
     $this->bindDates($ah->getDays());
     ksort($this->times);
     ksort($this->years);
     ksort($this->days);
     ksort($this->months);
     if ($timezone = get_option('timezone_string')) {
         date_default_timezone_set('UTC');
     }
 }
 public function __construct(SLN_Settings $settings)
 {
     //https://weston.ruter.net/2013/04/02/do-not-change-the-default-timezone-from-utc-in-wordpress/
     //https://wordpress.org/support/topic/why-does-wordpress-set-timezone-to-utc
     if ($timezone = get_option('timezone_string')) {
         date_default_timezone_set($timezone);
     }
     $this->settings = $settings;
     $this->from = $this->settings->get('hours_before_from');
     $this->to = $this->settings->get('hours_before_to');
     $txt = SLN_Func::getIntervalItems();
     if ($this->from) {
         $this->fromString = $txt[$this->from];
     }
     if ($this->to) {
         $this->toString = $txt[$this->to];
     }
     $this->fromDate = $now = new DateTime();
     $minutes = $this->minutes($now);
     $now->setTime($now->format('H'), $minutes);
     $this->toDate = $now2 = clone $now;
     if ($this->from) {
         $now->modify($this->from);
     } else {
         $now->modify('+30 minutes');
     }
     if ($this->to) {
         $now2->modify($this->to);
     } else {
         $this->toDate = new DateTime('+1 year');
     }
     $str = $this->getHoursBeforeString();
     SLN_Plugin::addLog(__CLASS__ . 'Initialized with' . print_r($str, true));
     if ($timezone = get_option('timezone_string')) {
         date_default_timezone_set('UTC');
     }
 }
 public static function create($attrs)
 {
     $obj = new self(SLN_Plugin::getInstance(), $attrs);
     return $obj->execute();
 }
 public function ajax()
 {
     //check_ajax_referer('ajax_post_validation', 'security');
     $method = $_REQUEST['method'];
     $className = 'SLN_Action_Ajax_' . ucwords($method);
     if (class_exists($className)) {
         SLN_Plugin::addLog('calling ajax ' . $className);
         //SLN_Plugin::addLog(print_r($_POST,true));
         /** @var SLN_Action_Ajax_Abstract $obj */
         $obj = new $className($this);
         $ret = $obj->execute();
         SLN_Plugin::addLog("{$className} returned:\r\n" . json_encode($ret));
         if (is_array($ret)) {
             header('Content-Type: application/json');
             echo json_encode($ret);
         } elseif (is_string($ret)) {
             echo $ret;
         } else {
             throw new Exception("no content returned from {$className}");
         }
         exit;
     } else {
         throw new Exception("ajax method not found '{$method}'");
     }
 }
 public function __construct(SLN_Plugin $plugin)
 {
     $this->plugin = $plugin;
     $this->settings = $plugin->getSettings();
     add_action('admin_menu', array($this, 'admin_menu'));
 }
 public static function getMinutesIntervals($interval = null, $maxItems = null)
 {
     $start = "00:00";
     $curr = strtotime($start);
     $interval = isset($interval) ? $interval : SLN_Plugin::getInstance()->getSettings()->getInterval();
     $maxItems = isset($maxItems) ? $maxItems : 1440;
     $items = array();
     do {
         $items[] = date("H:i", $curr);
         $curr = strtotime('+' . $interval . ' minutes', $curr);
         $maxItems--;
     } while (date("H:i", $curr) != $start && $maxItems > 0);
     return $items;
 }
/*
Plugin Name: Salon Booking Wordpress Plugin - Free Version
Description: Let your customers book you services through your website. Perfect for hairdressing salons, barber shops and beauty centers.
Version: 1.0.6
Plugin URI: http://salon.wpchef.it/
Author: Wordpress Chef / Plugins 
Author URI: http://plugins.wpchef.it/
Text Domain: sln
*/
define('SLN_PLUGIN_BASENAME', plugin_basename(__FILE__));
define('SLN_PLUGIN_DIR', untrailingslashit(dirname(__FILE__)));
define('SLN_PLUGIN_URL', untrailingslashit(plugins_url('', __FILE__)));
define('SLN_VERSION', '1.0.6');
function sln_autoload($className)
{
    if (strpos($className, 'SLN_') === 0) {
        include_once SLN_PLUGIN_DIR . "/src/" . str_replace("_", "/", $className) . '.php';
    }
}
function my_update_notice()
{
    $info = __('ATTENTION! Back-up your translations files before update.', 'sln');
    echo '<span class="spam">' . strip_tags($info, '<br><a><b><i><span>') . '</span>';
}
if (is_admin()) {
    add_action('in_plugin_update_message-' . plugin_basename(__FILE__), 'my_update_notice');
}
spl_autoload_register('sln_autoload');
SLN_Plugin::getInstance();
ob_start();