$prop(); return $object->$prop; }, $template); } if (!$plg_active): class WPPostClass { var $id = 0, $_author = null; static $_metacache = array(), $_metachange = array(); private $_sort_ordering = 0; public function __construct($id) { $this->id = (int) $id; $cls = get_class($this); if (isset($cls::$_instances) && !isset($cls::$_instances[$this->id])) { $cls::$_instances[$this->id] = $this; } } public function store() { foreach (self::$_metachange[$this->id] as $key => $val) { update_field($key, $val, $this->id); self::$_metacache[$this->id][$key] = $val; } unset(self::$_metachange[$this->id]); return $this; } public function getChanges() { $ret = array(); foreach (self::$_metachange[$this->id] as $key => $val) { $name = $this->unresolveFieldname($key); $ret[$name] = array($this->getField($key, true), $val); } return $ret; } public function __call($func, $args) { $three = substr($func, 0, 3); if ($three === 'get' || $three === 'set') { $field = lcfirst(substr($func, 3)); array_unshift($args, $field); return call_user_func_array(array($this, $three.'Field'), $args); } throw new Exception(get_called_class().' heeft geen functie '.$func, 500); } public static function storeValues() { foreach (self::$_metachange as $id => $vals) { foreach ($vals as $key => $val) { update_field($key, $val, $id); self::$_metacache[$id][$key] = $val; } } self::$_metachange = array(); } public function getField($_name, $noChange = false, $raw = false) { $name = $this->resolveFieldname($_name); if (isset(self::$_metachange[$this->id][$name]) && !$noChange) return self::$_metachange[$this->id][$name]; if (!isset(self::$_metacache[$this->id][$name])) { $val = get_field($name, $this->id, !$raw); if (isset(static::$_metaMap, static::$_metaMap[$_name])) $val = call_user_func(static::$_metaMap[$_name], $val); self::$_metacache[$this->id][$name] = $val; } return self::$_metacache[$this->id][$name]; } public function setField($name, $val) { $name = $this->resolveFieldname($name); self::$_metachange[$this->id][$name] = $val; return $this; } private function resolveFieldname($name) { $const = strtoupper('FIELDKEY_'.get_class($this).'_'.$name); return defined($const) ? constant($const) : $name; } private function unresolveFieldname($fieldKey, $elseFalse = false) { $all = get_defined_constants(); $prefix = strtoupper('FIELDKEY_'.get_class($this).'_'); $prefixLength = strlen($prefix); foreach ($all as $key => $one) { if ($one === $fieldKey && substr($key, 0, $prefixLength) === $prefix) { return strtolower(substr($key, $prefixLength)); } } return $elseFalse ? false : $fieldKey; } public static $_instances; public static function get($id, $cls = null) { if ($id instanceof WP_Post) $id = $id->ID; if ($id instanceof WPPostClass) return $id; if (!$id) return false; if (!isset($cls)) $cls = get_called_class(); if (!isset($cls::$_instances[$id])) { $cls::$_instances[$id] = new $cls($id); } return $cls::$_instances[$id]; } public function getTags() { return get_the_tags($this->id); } public function getTitle() { return get_the_title($this->id); } public function setTitle($title) { return $this->updatePost(array('post_title' => $title)); } public function getLink() { return get_permalink($this->id); } public function getAuthorId() { if (!isset($this->_author)) { $this->_author = (int) get_post_field('post_author', $this->id); } return $this->_author; } public function getImage($size = null, $attr = null) { if (!isset($size)) $size = array(215, 141); return get_the_post_thumbnail($this->id, $size, $attr); } public function setGlobal() { global $post; $post =& get_post($this->id); setup_postdata($post); // http://wordpress.stackexchange.com/questions/51741/get-post-content-from-outside-the-loop } public function unsetGlobal() { global $post; wp_reset_postdata($post); } public function getContent() { $this->setGlobal(); $txt = get_the_content(); $this->unsetGlobal(); return $txt; } public function setMeta($key, $value) { $ok = update_post_meta($this->id, $key, $value); return true; } public function getMeta($key, $def = null) { $ret = get_post_meta($this->id, $key, true); if ($ret === '') return $def; return $ret; } function postContent() { $data = get_post($this->id); return $data->post_content; } function toWPPost() { return get_post($this->id); } function is($other) { if ($other instanceof WPPostClass) return $this->id == $other->id; if (is_numeric($other)) return $this->id == $other; return false; } function isSameType($other) { return get_class($this) === get_class($other); } function updatePost($fields = array()) { $fields['ID'] = $this->id; return wp_update_post($fields); } function setTerms($values, $append = false, &$ids = null) { foreach ($values as $tax => $term) { $ret = wp_set_object_terms($this->id, $term, $tax, $append); } if (is_array($ret)) { $ids = $ret; if (!empty($ret)) return true; } if (is_wp_error($ret)) $ids = $ret; if (is_string($ret)) $ids = new WP_Error('illegal_term', 'Ongeldige term gebruikt in '.get_class($this).'::setTerms', $ret); return false; } function getTerms($tax = 'post_tag') { return wp_get_post_terms($this->id, $tax); } function __toString() { return get_class($this).' #'.$this->id.' ('.$this->getTitle().')'; } public static function loadAll($type = null, $meta = null, $qa = array()) { if (!isset($type) && isset(static::$_posttype)) $type = static::$_posttype; $res = RPostQuery($type, get_called_class(), $meta, $qa); return $res; } public static function loadMine($meta = null, $qa = array(), $type = null) { $uid = get_current_user_id(); if (!empty($qa['author__in']) && is_array($qa['author__in'])) $qa['author__in'][] = $uid; else $qa['author'] = $uid; return static::loadAll($type, $meta, $qa); } public static function getByTitle($title, $type = null) { if (!isset($type) && isset(static::$_posttype)) $type = static::$_posttype; $post = get_page_by_title($title, 'OBJECT', $type); return static::get($post); } public static function create($data = array()) { $aliases = array( 'title' => 'post_title', 'content' => 'post_content', 'status' => 'post_status' ); if (!isset($data['post_type']) && isset(static::$_posttype)) $data['post_type'] = static::$_posttype; foreach ($aliases as $old => $new) { if (isset($data[$old])) { $data[$new] = $data[$old]; unset($data[$old]); } } $class = ifset('class', get_called_class(), $data); if (!$class || !class_exists($class) || !is_subclass_of($class, 'WPPostClass')) $class = null; $newid = wp_insert_post($data, true); if (is_wp_error($newid)) return false; return isset($class) ? $class::get($newid) : $newid; } private static $_sortByMethod = null, $_sortByMethod2 = null, $_sortRev = false, $_sortRev2 = false; public static function sortByMethod(&$posts, $method, $rev = false) { if (is_array($method)) { self::$_sortByMethod2 = $method[1]; $method = array_shift($method); if (is_array($rev)) { self::$_sortRev2 = $rev[1]; $rev = array_shift($rev); } else { self::$_sortRev2 = $rev; } } self::$_sortByMethod = $method; self::$_sortRev = $rev; $i = -1; foreach ($posts as $post) { $post->_sort_ordering = ++$i; } $ok = uasort($posts, array(__CLASS__, '_sort')); self::$_sortByMethod = self::$_sortByMethod2 = null; self::$_sortRev = self::$_sortRev2 = false; return $ok; } private function _sort($a, $b) { $rev = self::$_sortRev; if (isset(self::$_sortByMethod)) { $ra = $a->{self::$_sortByMethod}(); $rb = $b->{self::$_sortByMethod}(); } if (!isset($ra, $rb) || $ra === $rb) { if (isset(self::$_sortByMethod2)) { $rev = self::$_sortRev2; $ra = $a->{self::$_sortByMethod2}(); $rb = $b->{self::$_sortByMethod2}(); } if (!isset($ra, $rb) || $ra === $rb) return $a->_sort_ordering - $b->_sort_ordering; } $ret = $ra < $rb ? -1 : 1; return $rev ? -$ret : $ret; } } // http://codex.wordpress.org/Class_Reference/WP_Query function RPostQuery($type = 'arrangement', $class = null, $meta_query = null, $qa = array()) { if (!isset($qa['status'])) $qa['status'] = array('publish'); // , 'draft', 'private'); // 'any',; $args = array( 'post_type' => $type, 'nopaging' => true, 'post_status' => $qa['status'] ); foreach ($qa as $k => $v) { switch ($k) { case 'title': case 'status': $k = "post_$k"; default: $args[$k] = $v; break; } } // if (isset($qa['perm'])) $args['perm'] = $qa['perm']; // if (isset($qa['author'])) $args['author'] = $qa['author']; // if (isset($qa['title'])) $args['post_title'] = $qa['title']; if (isset($meta_query)) { $q = array(); foreach ($meta_query as $mq) { if (empty($q) && is_string($mq)) $q['relation'] = $mq; // OR, AND else { $partscount = count($mq); $part = array('compare' => '=', 'type' => 'CHAR'); if ($partscount === 2) list($part['key'], $part['value']) = $mq; elseif ($partscount === 3) list($part['key'], $part['compare'], $part['value']) = $mq; elseif ($partscount === 4) list($part['key'], $part['compare'], $part['value'], $part['type']) = $mq; $q[] = $part; } } $args['meta_query'] = $q; } $printr = !empty($args['printr']); $query = new WP_Query($args); if ($printr) echo '
', print_r($args, 1), '
'; // echo $query->last_query; // if (isset($qa['author']) || $type === 'nieuws') { // if ($class === 'NLVwaarde') { echo '
', print_r(func_get_args(), 1), print_r($query, 1), '
'; die; } // } $res = array(); while ($query->have_posts()) { $item = $query->the_post(); $itemId = get_the_ID(); $res[$itemId] = $class ? $class::get($itemId) : $item; if (isset($qa['callback'])) { $result = call_user_func($qa['callback'], $res[$itemId], $itemId); if ($result !== true) unset($res[$itemId]); if ($result === false) break; } } wp_reset_postdata(); return $res; } class RSession { function start() { if (!session_id()) session_start(); if (!isset($_SESSION['_rsess_'])) $_SESSION['_rsess_'] = array(); } function end() { session_destroy(); } function onLogin() { if (!session_id()) die('no session!'); // echo '
'; print_r($_SESSION); die;
		if (self::has('pending_reservation')) {
			// Arrangement::continueReservation();
		}
		else self::end();
	}

	function set($k, $v) {
		$_SESSION['_rsess_'][$k] = $v;
	}

	function get($k, $def = null) {
		return isset($_SESSION['_rsess_'][$k]) ? $_SESSION['_rsess_'][$k] : $def;
	}

	function has($k) {
		return isset($_SESSION['_rsess_'][$k]);
	}

	function del($k) {
		unset($_SESSION['_rsess_'][$k]);
	}

	function pop($k, $def = null) {
		$val = self::get($k, $def);
		self::del($k);
		return $val;
	}

	function checkMessage($message = null) {
		return self::pop('message', $message);
	}

	function showMessage() {
		$msg = self::checkMessage(null);
		if ($msg) echo '
'.$msg.'
'; } function setMessage($message) { self::set('message', $message); } function addMessage($message) { $existing = self::get('message', ''); self::setMessage($existing . ($existing ? '
' : '') . $message); } function login($msg = null) { if ($msg) self::setMessage($msg); wp_redirect( wp_login_url() ); exit; } } class RDate { var $ts = 0, $d, $m, $Y; function __construct($ts = null) { if (!is_int($ts)) $ts = strtotime($ts); $this->ts = $ts; $this->Y = date('Y', $ts); $this->m = date('m', $ts); $this->d = date('d', $ts); } function isValid() { return (bool) $this->ts; } function toWeekday($n, $dir = 'sameweek') { $n = (int) $n; $now = $this->getWeekday(); $diff = $n - $now; $abs = abs($diff); switch ($dir) { case 'nearest': if ($abs > 3) $diff = ($diff + 7) % 7 - 7; break; case 'prev': if ($diff > 0) $diff -= 7; break; case 'next': if ($diff < 0) $diff += 7; break; } // if ($diff > 0) $diff = "+$diff" return $this->offset("$diff days"); } function offset($str) { return new self(strtotime($str, $this->ts)); } function getWeekday() { return (int) date('N', $this->ts); } function __toString() { return date('Y-m-d H:i:s', $this->ts); } function format($format, $locale = false) { if ($locale && $this->d < 10) $format = str_replace(' %e', '%e', $format); return $locale ? trim(strftime($format, $this->ts)) : date($format, $this->ts); } function copy() { return new self($this->ts); } function isAfter(RDate $d) { return $this->ts > $d->ts; } function isBefore(RDate $d) { return $this->ts < $d->ts; } function diff(RDate $d, $unit = 'seconds') { $diff = $this->ts - $d->ts; switch ($unit) { case 'days': $diff /= 24; case 'hours': $diff /= 60; case 'minutes': $diff /= 60; // case 'seconds': } return round($diff); } function isEqual(RDate $d) { return $this->ts === $d->ts; } static function mergeDatesRanges(&$arr, RDate $from, RDate $to, $interval = '1 days', $format = 'Ymd', $locale = false) { if ($to < $from) { $f = $to; $to = $from; $from = $f; } $d = $from; $timeout = 500; while ($d <= $to && --$timeout !== 0) { $ymd = $d->format('Ymd'); if (!isset($arr[$ymd])) $arr[$ymd] = $d->format($format, $locale); $d = $d->offset($interval); } ksort($arr); } static function withRange(RDate $from, RDate $to, $callback, &$vars = null, $interval = '1 days') { $checkIfPast = ($interval[0] === '-' ? 'isBefore' : 'isAfter'); if ($from->$checkIfPast($to)) return false; $d = clone $from; while (!$d->$checkIfPast($to)) { $ret = call_user_func_array($callback, array($d, &$vars)); if ($ret === false) return false; $d = $d->offset($interval); } return true; } static function today() { return new self(date('Ymd')); } static function fromYmd($s) { return new self(substr($s, 0, 4) . '-' . substr($s, 4, 2) . '-' . substr($s, 6, 2)); } static function mostOf($func = 'isAfter') { $ret = null; $dates = array_slice(func_get_args(), 1); foreach ($dates as $d) { if (!isset($ret) || $d->$func($ret)) $ret = $d; } return $ret; } } function tot_dan($d) { $now = RDate::today()->offset('1 days'); $d = RDate::from($d); if ($d->isBefore($now)) { $time = $d->format('H'); if (!$time || $time < 12) return 'straks'; if ($time < 18) return 'vanmiddag'; return 'vanavond'; } $now = $now->offset('1 days'); if ($d->isBefore($now)) return 'morgen'; $now = $now->offset('1 days'); if ($d->isBefore($now)) return 'overmorgen'; $now = RDate::today()->toWeekday(0, 'next'); if ($d->isBefore($now)) return strtolower($d->format('%A', true)); $now = $now->offset('1 week'); if ($d->isBefore($now)) return 'volgende week '.strtolower($d->format('%A', true)); $now = RDate::today()->offset('1 month'); if ($now->format('mY') === $d->format('mY')) return 'volgende maand'; $now = RDate::today()->offset('1 year'); if ($now->format('Y') === $d->format('Y')) return 'volgend jaar'; $Y = $d->format('Y'); if ($Y === RDate::today()->format('Y')) $Y = ''; else $Y = ' '.$Y; return $d->format('%A %e %B', true).$Y; } function reduceByMethod($objects, $method, $args = array()) { $ret = array(); foreach ($objects as $obj) { $ret[] = call_user_func_array(array($obj, $method), $args); } return $ret; } function reduceByKey($array, $key) { $ret = array(); foreach ($array as $i => $one) { $ret[] = isset($one[$key]) ? $one[$key] : null; } return $ret; } function getByPath($path, $base, $def = null) { if (is_string($path)) $path = explode('.', $path); // $key = array_pop($path); foreach ($path as $one) { $isobj = is_object($base); if ($isobj) { if (isset($base->$one)) $base = $base->$one; else return $def; } else { if (isset($base[$one])) $base = $base[$one]; else return $def; } } return $base; } function getUserField($key, $def = null, $userId = null, $format = true) { if (!isset($userId)) $userId = get_current_user_id(); $val = get_field($key, "user_$userId", $format); return isset($val) ? $val : $def; } function setUserField($key, $val = null, $userId = null) { if (!isset($userId)) $userId = get_current_user_id(); return update_field($key, $val, "user_$userId"); } function permalink_vars($id = null, $vars = array()) { $link = get_permalink($id); $qm = strpos($link, '?'); if ($qm !== false) { $qs = substr($link, $qm + 1); parse_str($qs, $query); if ($query) $vars = array_merge($query, $vars); $link = substr($link, 0, $qm - 1); } $query = http_build_query($vars); return $link.(empty($query) ? '' : '?'.$query); } function ifset($key, $def = null, $arr = null, $esc = null) { if (!isset($arr)) $arr = $_REQUEST; $isArray = is_array($arr); if ($isArray) { if (!isset($arr[$key])) return $def; return isset($esc) ? $esc($arr[$key]) : $arr[$key]; } if (!isset($arr->$key)) return $def; return isset($esc) ? $esc($arr->$key) : $arr->$key; } function roundTo($nr, $to = 1) { return round($nr / $to) * $to; } function remo_cURL ($url, $post_data = null) { $headers = array( 'Content-type: application/x-www-form-urlencoded' ); $curl_connection = curl_init($url); curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($curl_connection, CURLOPT_HEADER, 0); curl_setopt($curl_connection, CURLOPT_VERBOSE, 1); curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)"); curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true); if ($post_data) { curl_setopt($curl_connection, CURLOPT_POST, true); curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_data); } $result = curl_exec($curl_connection); curl_close($curl_connection); return $result; } endif; // boilerplate return $plg_active;