PluginProbe
VikBooking Hotel Booking Engine & PMS / trunk
VikBooking Hotel Booking Engine & PMS vtrunk
1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 All 36 releases
vikbooking / admin / helpers / src / cron / tracker / array.php

array.php in VikBooking Hotel Booking Engine & PMS trunk, at admin/helpers/src/cron/tracker/array.php

139 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * VikBooking cron job scalar-elements array tracker.
16 *
17 * @since 1.5.10
18 */
19 trait VBOCronTrackerArray
20 {
21 /**
22 * Defines the maximum number of characters that can occupy the list
23 * containing all the tracked elements. Children classes can alter
24 * this value according to their needs.
25 *
26 * @var int
27 */
28 protected $maximumCharsTrackableElements = 10000;
29
30 /**
31 * Helper method used to flag the specified element as tracked. This is really helpful to easily
32 * check whether a specific record has been already parsed, which can be done by passing the same
33 * element argument to the `isTracked` method.
34 *
35 * It is recommended to register only scalar values in order to prevent an uncontrolled increase of the
36 * total length, which can arrive up to 2^16-1 characters (65535). It's up to the sub-classes to take care of
37 * this limit, which should clean the flag_char property in order to always have less than 65536 characters.
38 *
39 * @param mixed $element The element to track.
40 *
41 * @return boolean True on success, false otherwise.
42 */
43 protected function track($element)
44 {
45 if ($this->isTracked($element) || !is_scalar($element))
46 {
47 // element already tracked, avoid duplicate registration
48 return false;
49 }
50
51 if (is_numeric($element))
52 {
53 // cast to number to save some space occupied by json_encode for strings
54 $element = is_float($element) ? (float) $element : (int) $element;
55 }
56
57 // make sure this trait has been attached only to a cron job instance
58 if (!$this instanceof VBOCronJob)
59 {
60 throw new Exception('This tracker can be attached only to a VBOCronJob instance', 500);
61 }
62
63 // register the element at the beginning of the list
64 $data = $this->getData();
65 array_unshift($data->flag_char, $element);
66
67 // sanitize the elements contained within the cache in order
68 // avoid a length exceeding the maximum threshold
69 $this->sanitizeFlagCache($data);
70
71 return true;
72 }
73
74 /**
75 * Checks whether the specified element has been already processed.
76 *
77 * @param mixed $element The element to check.
78 *
79 * @return boolean True if already processed, false otherwise.
80 */
81 protected function isTracked($element)
82 {
83 // make sure this trait has been attached only to a cron job instance
84 if (!$this instanceof VBOCronJob)
85 {
86 throw new Exception('This tracker can be attached only to a VBOCronJob instance', 500);
87 }
88
89 return in_array($element, $this->getData()->flag_char);
90 }
91
92 /**
93 * Ensures the array with the tracked elements doesn't exceed the maximum limit.
94 *
95 * @param object $data The cron job data.
96 *
97 * @return void
98 */
99 private function sanitizeFlagCache($data)
100 {
101 // count the total number of characters that occupy the cache
102 $len = 0;
103
104 foreach ($data->flag_char as $element)
105 {
106 // sum value with the total number of characters
107 $len += strlen((string) $element);
108
109 if (!is_numeric($element))
110 {
111 // we have a string element, we need to include other 2 characters reserved for wrapping
112 $len += 2;
113 }
114
115 // increase the length by 1 to include also the elements separator (,)
116 $len++;
117 }
118
119 if ($data->flag_char)
120 {
121 // remove the trailing comma separator
122 $len--;
123 }
124
125 // sum also the initial and trailing brackets
126 $len += 2;
127
128 // even if the maximum number of allowed characters is 65535, we prefer to always stay under
129 // 10000 elements, since json_encode may use a specific encoding for some characters
130 if ($len > $this->maximumCharsTrackableElements)
131 {
132 // it is enough to pop the last 2 items from the list in order to stay under the desired limit,
133 // since this method always triggers while registering a new element
134 array_pop($data->flag_char);
135 array_pop($data->flag_char);
136 }
137 }
138 }
139