Completed
Branch FET/event-question-group-refac... (8c9768)
by
unknown
27:04 queued 17:53
created
core/libraries/form_sections/inputs/EE_File_Input.input.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -55,7 +55,7 @@
 block discarded – undo
55 55
                     esc_html__('Please provide a file of the requested filetype: %1$s', 'event_espresso'),
56 56
                     implode(', ', $this->allowed_file_extensions)
57 57
                 ),
58
-                '~.*\.(' . implode('|', $this->allowed_file_extensions) . ')$~'
58
+                '~.*\.('.implode('|', $this->allowed_file_extensions).')$~'
59 59
             )
60 60
         );
61 61
         parent::__construct($options);
Please login to merge, or discard this patch.
Indentation   +75 added lines, -75 removed lines patch added patch discarded remove patch
@@ -19,86 +19,86 @@
 block discarded – undo
19 19
  */
20 20
 class EE_File_Input extends EE_Form_Input_Base
21 21
 {
22
-    /**
23
-     * @var array
24
-     */
25
-    protected $allowed_file_extensions;
22
+	/**
23
+	 * @var array
24
+	 */
25
+	protected $allowed_file_extensions;
26 26
 
27
-    /**
28
-     * @var array
29
-     */
30
-    protected $allowed_mime_types;
27
+	/**
28
+	 * @var array
29
+	 */
30
+	protected $allowed_mime_types;
31 31
 
32
-    /**
33
-     * @param array $options
34
-     * @throws InvalidArgumentException
35
-     */
36
-    public function __construct($options = array())
37
-    {
38
-        if (isset($options['allowed_file_extensions'])) {
39
-            $this->allowed_file_extensions = (array) $options['allowed_file_extensions'];
40
-        } else {
41
-            $this->allowed_file_extensions = ['csv'];
42
-        }
43
-        if (isset($options['allowed_mime_types'])) {
44
-            $this->allowed_mime_types = (array) $options['allowed_file_extensions'];
45
-        } else {
46
-            $this->allowed_mime_types = ['text/csv'];
47
-        }
32
+	/**
33
+	 * @param array $options
34
+	 * @throws InvalidArgumentException
35
+	 */
36
+	public function __construct($options = array())
37
+	{
38
+		if (isset($options['allowed_file_extensions'])) {
39
+			$this->allowed_file_extensions = (array) $options['allowed_file_extensions'];
40
+		} else {
41
+			$this->allowed_file_extensions = ['csv'];
42
+		}
43
+		if (isset($options['allowed_mime_types'])) {
44
+			$this->allowed_mime_types = (array) $options['allowed_file_extensions'];
45
+		} else {
46
+			$this->allowed_mime_types = ['text/csv'];
47
+		}
48 48
 
49
-        $this->_set_display_strategy(new EE_File_Input_Display_Strategy());
50
-        $this->_set_normalization_strategy(new EE_File_Normalization());
51
-        $this->add_validation_strategy(
52
-            new EE_Text_Validation_Strategy(
53
-                sprintf(
54
-                    // translators: %1$s is a list of allowed file extensions.
55
-                    esc_html__('Please provide a file of the requested filetype: %1$s', 'event_espresso'),
56
-                    implode(', ', $this->allowed_file_extensions)
57
-                ),
58
-                '~.*\.(' . implode('|', $this->allowed_file_extensions) . ')$~'
59
-            )
60
-        );
61
-        parent::__construct($options);
49
+		$this->_set_display_strategy(new EE_File_Input_Display_Strategy());
50
+		$this->_set_normalization_strategy(new EE_File_Normalization());
51
+		$this->add_validation_strategy(
52
+			new EE_Text_Validation_Strategy(
53
+				sprintf(
54
+					// translators: %1$s is a list of allowed file extensions.
55
+					esc_html__('Please provide a file of the requested filetype: %1$s', 'event_espresso'),
56
+					implode(', ', $this->allowed_file_extensions)
57
+				),
58
+				'~.*\.(' . implode('|', $this->allowed_file_extensions) . ')$~'
59
+			)
60
+		);
61
+		parent::__construct($options);
62 62
 
63 63
 //        It would be great to add this HTML attribute, but jQuery validate chokes on it.
64
-        $this->set_other_html_attributes(
65
-            $this->other_html_attributes()
66
-            . ' extension="'
67
-            . implode(
68
-                ',',
69
-                $this->allowed_file_extensions
70
-            )
71
-            . '"'
72
-        );
73
-    }
64
+		$this->set_other_html_attributes(
65
+			$this->other_html_attributes()
66
+			. ' extension="'
67
+			. implode(
68
+				',',
69
+				$this->allowed_file_extensions
70
+			)
71
+			. '"'
72
+		);
73
+	}
74 74
 
75
-    /**
76
-     * $_FILES has a really weird structure. So we let `FilesDataHandler` take care of finding the file info for
77
-     * this input.
78
-     * @since 4.9.80.p
79
-     * @param array $req_data
80
-     * @return FileSubmissionInterface
81
-     * @throws InvalidArgumentException
82
-     * @throws InvalidDataTypeException
83
-     * @throws InvalidInterfaceException
84
-     */
85
-    public function find_form_data_for_this_section($req_data)
86
-    {
87
-        // ignore $req_data. Files are in the files data handler.
88
-        $fileDataHandler = LoaderFactory::getLoader()->getShared(
89
-            'EventEspresso\core\services\request\files\FilesDataHandler'
90
-        );
91
-        return $fileDataHandler->getFileObject($this->html_name());
92
-    }
75
+	/**
76
+	 * $_FILES has a really weird structure. So we let `FilesDataHandler` take care of finding the file info for
77
+	 * this input.
78
+	 * @since 4.9.80.p
79
+	 * @param array $req_data
80
+	 * @return FileSubmissionInterface
81
+	 * @throws InvalidArgumentException
82
+	 * @throws InvalidDataTypeException
83
+	 * @throws InvalidInterfaceException
84
+	 */
85
+	public function find_form_data_for_this_section($req_data)
86
+	{
87
+		// ignore $req_data. Files are in the files data handler.
88
+		$fileDataHandler = LoaderFactory::getLoader()->getShared(
89
+			'EventEspresso\core\services\request\files\FilesDataHandler'
90
+		);
91
+		return $fileDataHandler->getFileObject($this->html_name());
92
+	}
93 93
 
94
-    /**
95
-     * Don't transform the file submission object into a string, thanks.
96
-     *
97
-     * @param string $value
98
-     * @return null|string
99
-     */
100
-    protected function _sanitize($value)
101
-    {
102
-        return $value;
103
-    }
94
+	/**
95
+	 * Don't transform the file submission object into a string, thanks.
96
+	 *
97
+	 * @param string $value
98
+	 * @return null|string
99
+	 */
100
+	protected function _sanitize($value)
101
+	{
102
+		return $value;
103
+	}
104 104
 }
Please login to merge, or discard this patch.
form_sections/strategies/normalization/EE_File_Normalization.strategy.php 1 patch
Indentation   +34 added lines, -34 removed lines patch added patch discarded remove patch
@@ -13,40 +13,40 @@
 block discarded – undo
13 13
 class EE_File_Normalization extends EE_Normalization_Strategy_Base
14 14
 {
15 15
 
16
-    /**
17
-     * Keep in mind $value_to_normalize should be a FileSubmissionInterface or null, so this shouldn't really do
18
-     * much (other than NOT convert it to a string or something).
19
-     * @param string $value_to_normalize
20
-     * @return FileSubmissionInterface
21
-     */
22
-    public function normalize($value_to_normalize)
23
-    {
24
-        if ($value_to_normalize instanceof FileSubmissionInterface || is_null($value_to_normalize)) {
25
-            return $value_to_normalize;
26
-        } else {
27
-            throw new EE_Validation_Error(
28
-                esc_html__('The file input has an invalid format.', 'event_espresso')
29
-            );
30
-        }
31
-    }
16
+	/**
17
+	 * Keep in mind $value_to_normalize should be a FileSubmissionInterface or null, so this shouldn't really do
18
+	 * much (other than NOT convert it to a string or something).
19
+	 * @param string $value_to_normalize
20
+	 * @return FileSubmissionInterface
21
+	 */
22
+	public function normalize($value_to_normalize)
23
+	{
24
+		if ($value_to_normalize instanceof FileSubmissionInterface || is_null($value_to_normalize)) {
25
+			return $value_to_normalize;
26
+		} else {
27
+			throw new EE_Validation_Error(
28
+				esc_html__('The file input has an invalid format.', 'event_espresso')
29
+			);
30
+		}
31
+	}
32 32
 
33 33
 
34
-    /**
35
-     * This may be called prematurely on submitted data, so we actually don't want to convert it into a string because
36
-     * we'll lose all the FileSubmissionInterface data. So prefer to leave it alone. FileSubmissionInterface
37
-     * can be cast to a string just fine so it's good as-is.
38
-     *
39
-     * @param string $normalized_value
40
-     * @return string
41
-     */
42
-    public function unnormalize($normalized_value)
43
-    {
44
-        if ($normalized_value instanceof FileSubmissionInterface || is_null($normalized_value)) {
45
-            // Leave it as the object, it can be treated like a string because it
46
-            // overrides __toString()
47
-            return $normalized_value;
48
-        } else {
49
-            return (string) $normalized_value;
50
-        }
51
-    }
34
+	/**
35
+	 * This may be called prematurely on submitted data, so we actually don't want to convert it into a string because
36
+	 * we'll lose all the FileSubmissionInterface data. So prefer to leave it alone. FileSubmissionInterface
37
+	 * can be cast to a string just fine so it's good as-is.
38
+	 *
39
+	 * @param string $normalized_value
40
+	 * @return string
41
+	 */
42
+	public function unnormalize($normalized_value)
43
+	{
44
+		if ($normalized_value instanceof FileSubmissionInterface || is_null($normalized_value)) {
45
+			// Leave it as the object, it can be treated like a string because it
46
+			// overrides __toString()
47
+			return $normalized_value;
48
+		} else {
49
+			return (string) $normalized_value;
50
+		}
51
+	}
52 52
 }
Please login to merge, or discard this patch.
core/services/locators/FqcnLocator.php 2 patches
Indentation   +152 added lines, -152 removed lines patch added patch discarded remove patch
@@ -18,156 +18,156 @@
 block discarded – undo
18 18
 class FqcnLocator extends Locator
19 19
 {
20 20
 
21
-    /**
22
-     * @var array $FQCNs
23
-     */
24
-    protected $FQCNs = array();
25
-
26
-    /**
27
-     * @var array $namespaces
28
-     */
29
-    protected $namespaces = array();
30
-
31
-
32
-    /**
33
-     * @access protected
34
-     * @param string $namespace
35
-     * @param string $namespace_base_dir
36
-     * @throws InvalidDataTypeException
37
-     */
38
-    protected function setNamespace($namespace, $namespace_base_dir)
39
-    {
40
-        if (! is_string($namespace)) {
41
-            throw new InvalidDataTypeException('$namespace', $namespace, 'string');
42
-        }
43
-        if (! is_string($namespace_base_dir)) {
44
-            throw new InvalidDataTypeException('$namespace_base_dir', $namespace_base_dir, 'string');
45
-        }
46
-        $this->namespaces[ $namespace ] = $namespace_base_dir;
47
-    }
48
-
49
-
50
-    /**
51
-     * @access public
52
-     * @return array
53
-     */
54
-    public function getFQCNs()
55
-    {
56
-        return $this->FQCNs;
57
-    }
58
-
59
-
60
-    /**
61
-     * @access public
62
-     * @return int
63
-     */
64
-    public function count()
65
-    {
66
-        return count($this->FQCNs);
67
-    }
68
-
69
-
70
-    /**
71
-     * given a valid namespace, will find all files that match the provided mask
72
-     *
73
-     * @access public
74
-     * @param string|array $namespaces
75
-     * @return array
76
-     * @throws InvalidClassException
77
-     * @throws InvalidDataTypeException
78
-     */
79
-    public function locate($namespaces)
80
-    {
81
-        if (! (is_string($namespaces) || is_array($namespaces))) {
82
-            throw new InvalidDataTypeException('$namespaces', $namespaces, 'string or array');
83
-        }
84
-        foreach ((array) $namespaces as $namespace) {
85
-            foreach ($this->findFQCNsByNamespace($namespace) as $key => $file) {
86
-                $this->FQCNs[ $key ] = $file;
87
-            }
88
-        }
89
-        return $this->FQCNs;
90
-    }
91
-
92
-
93
-    /**
94
-     * given a partial namespace, will find all files in that folder
95
-     * ** PLZ NOTE **
96
-     * This assumes that all files within the specified folder should be loaded
97
-     *
98
-     * @access protected
99
-     * @param string $partial_namespace
100
-     * @return array
101
-     * @throws InvalidClassException
102
-     * @throws InvalidDataTypeException
103
-     */
104
-    protected function findFQCNsByNamespace($partial_namespace)
105
-    {
106
-        $iterator = new FilesystemIterator(
107
-            $this->getDirectoryFromPartialNamespace($partial_namespace)
108
-        );
109
-        $iterator->setFlags(FilesystemIterator::CURRENT_AS_FILEINFO);
110
-        $iterator->setFlags(FilesystemIterator::UNIX_PATHS);
111
-        if (iterator_count($iterator) === 0) {
112
-            return array();
113
-        }
114
-        foreach ($iterator as $file) {
115
-            if ($file->isFile() && $file->getExtension() === 'php') {
116
-                $file = $file->getPath() . DS . $file->getBasename('.php');
117
-                foreach ($this->namespaces as $namespace => $base_dir) {
118
-                    $namespace .= Psr4Autoloader::NS;
119
-                    if (strpos($file, $base_dir) === 0) {
120
-                        $this->FQCNs[] = Psr4Autoloader::NS . str_replace(
121
-                            array($base_dir, DS),
122
-                            array($namespace, Psr4Autoloader::NS),
123
-                            $file
124
-                        );
125
-                    }
126
-                }
127
-            }
128
-        }
129
-        return $this->FQCNs;
130
-    }
131
-
132
-
133
-    /**
134
-     * getDirectoryFromPartialNamespace
135
-     *
136
-     * @access protected
137
-     * @param  string $partial_namespace almost fully qualified class name ?
138
-     * @return string
139
-     * @throws InvalidDataTypeException
140
-     * @throws InvalidClassException
141
-     */
142
-    protected function getDirectoryFromPartialNamespace($partial_namespace)
143
-    {
144
-        if (empty($partial_namespace)) {
145
-            throw new InvalidClassException($partial_namespace);
146
-        }
147
-        // load our PSR-4 Autoloader so we can get the list of registered namespaces from it
148
-        $psr4_loader = \EE_Psr4AutoloaderInit::psr4_loader();
149
-        // breakup the incoming namespace into segments so we can loop thru them
150
-        $namespace_segments = explode(Psr4Autoloader::NS, trim($partial_namespace, Psr4Autoloader::NS));
151
-        // we're only interested in the Vendor and secondary base, so pull those from the array
152
-        $vendor_base = array_slice($namespace_segments, 0, 2);
153
-        $namespace = $prefix = null;
154
-        while (! empty($vendor_base)) {
155
-            $namespace = implode(Psr4Autoloader::NS, $vendor_base);
156
-            // check if there's a base directory registered for that namespace
157
-            $prefix = $psr4_loader->prefixes($namespace . Psr4Autoloader::NS);
158
-            if (! empty($prefix) && ! empty($prefix[0])) {
159
-                // found one!
160
-                break;
161
-            }
162
-            // remove base and try vendor only portion of namespace
163
-            array_pop($vendor_base);
164
-        }
165
-        // nope? then the incoming namespace is invalid
166
-        if (empty($prefix) || empty($prefix[0])) {
167
-            throw new InvalidClassException($partial_namespace);
168
-        }
169
-        $this->setNamespace($namespace, $prefix[0]);
170
-        // but if it's good, add that base directory to the rest of the path, and return it
171
-        return $prefix[0] . implode(DS, array_diff($namespace_segments, $vendor_base)) . DS;
172
-    }
21
+	/**
22
+	 * @var array $FQCNs
23
+	 */
24
+	protected $FQCNs = array();
25
+
26
+	/**
27
+	 * @var array $namespaces
28
+	 */
29
+	protected $namespaces = array();
30
+
31
+
32
+	/**
33
+	 * @access protected
34
+	 * @param string $namespace
35
+	 * @param string $namespace_base_dir
36
+	 * @throws InvalidDataTypeException
37
+	 */
38
+	protected function setNamespace($namespace, $namespace_base_dir)
39
+	{
40
+		if (! is_string($namespace)) {
41
+			throw new InvalidDataTypeException('$namespace', $namespace, 'string');
42
+		}
43
+		if (! is_string($namespace_base_dir)) {
44
+			throw new InvalidDataTypeException('$namespace_base_dir', $namespace_base_dir, 'string');
45
+		}
46
+		$this->namespaces[ $namespace ] = $namespace_base_dir;
47
+	}
48
+
49
+
50
+	/**
51
+	 * @access public
52
+	 * @return array
53
+	 */
54
+	public function getFQCNs()
55
+	{
56
+		return $this->FQCNs;
57
+	}
58
+
59
+
60
+	/**
61
+	 * @access public
62
+	 * @return int
63
+	 */
64
+	public function count()
65
+	{
66
+		return count($this->FQCNs);
67
+	}
68
+
69
+
70
+	/**
71
+	 * given a valid namespace, will find all files that match the provided mask
72
+	 *
73
+	 * @access public
74
+	 * @param string|array $namespaces
75
+	 * @return array
76
+	 * @throws InvalidClassException
77
+	 * @throws InvalidDataTypeException
78
+	 */
79
+	public function locate($namespaces)
80
+	{
81
+		if (! (is_string($namespaces) || is_array($namespaces))) {
82
+			throw new InvalidDataTypeException('$namespaces', $namespaces, 'string or array');
83
+		}
84
+		foreach ((array) $namespaces as $namespace) {
85
+			foreach ($this->findFQCNsByNamespace($namespace) as $key => $file) {
86
+				$this->FQCNs[ $key ] = $file;
87
+			}
88
+		}
89
+		return $this->FQCNs;
90
+	}
91
+
92
+
93
+	/**
94
+	 * given a partial namespace, will find all files in that folder
95
+	 * ** PLZ NOTE **
96
+	 * This assumes that all files within the specified folder should be loaded
97
+	 *
98
+	 * @access protected
99
+	 * @param string $partial_namespace
100
+	 * @return array
101
+	 * @throws InvalidClassException
102
+	 * @throws InvalidDataTypeException
103
+	 */
104
+	protected function findFQCNsByNamespace($partial_namespace)
105
+	{
106
+		$iterator = new FilesystemIterator(
107
+			$this->getDirectoryFromPartialNamespace($partial_namespace)
108
+		);
109
+		$iterator->setFlags(FilesystemIterator::CURRENT_AS_FILEINFO);
110
+		$iterator->setFlags(FilesystemIterator::UNIX_PATHS);
111
+		if (iterator_count($iterator) === 0) {
112
+			return array();
113
+		}
114
+		foreach ($iterator as $file) {
115
+			if ($file->isFile() && $file->getExtension() === 'php') {
116
+				$file = $file->getPath() . DS . $file->getBasename('.php');
117
+				foreach ($this->namespaces as $namespace => $base_dir) {
118
+					$namespace .= Psr4Autoloader::NS;
119
+					if (strpos($file, $base_dir) === 0) {
120
+						$this->FQCNs[] = Psr4Autoloader::NS . str_replace(
121
+							array($base_dir, DS),
122
+							array($namespace, Psr4Autoloader::NS),
123
+							$file
124
+						);
125
+					}
126
+				}
127
+			}
128
+		}
129
+		return $this->FQCNs;
130
+	}
131
+
132
+
133
+	/**
134
+	 * getDirectoryFromPartialNamespace
135
+	 *
136
+	 * @access protected
137
+	 * @param  string $partial_namespace almost fully qualified class name ?
138
+	 * @return string
139
+	 * @throws InvalidDataTypeException
140
+	 * @throws InvalidClassException
141
+	 */
142
+	protected function getDirectoryFromPartialNamespace($partial_namespace)
143
+	{
144
+		if (empty($partial_namespace)) {
145
+			throw new InvalidClassException($partial_namespace);
146
+		}
147
+		// load our PSR-4 Autoloader so we can get the list of registered namespaces from it
148
+		$psr4_loader = \EE_Psr4AutoloaderInit::psr4_loader();
149
+		// breakup the incoming namespace into segments so we can loop thru them
150
+		$namespace_segments = explode(Psr4Autoloader::NS, trim($partial_namespace, Psr4Autoloader::NS));
151
+		// we're only interested in the Vendor and secondary base, so pull those from the array
152
+		$vendor_base = array_slice($namespace_segments, 0, 2);
153
+		$namespace = $prefix = null;
154
+		while (! empty($vendor_base)) {
155
+			$namespace = implode(Psr4Autoloader::NS, $vendor_base);
156
+			// check if there's a base directory registered for that namespace
157
+			$prefix = $psr4_loader->prefixes($namespace . Psr4Autoloader::NS);
158
+			if (! empty($prefix) && ! empty($prefix[0])) {
159
+				// found one!
160
+				break;
161
+			}
162
+			// remove base and try vendor only portion of namespace
163
+			array_pop($vendor_base);
164
+		}
165
+		// nope? then the incoming namespace is invalid
166
+		if (empty($prefix) || empty($prefix[0])) {
167
+			throw new InvalidClassException($partial_namespace);
168
+		}
169
+		$this->setNamespace($namespace, $prefix[0]);
170
+		// but if it's good, add that base directory to the rest of the path, and return it
171
+		return $prefix[0] . implode(DS, array_diff($namespace_segments, $vendor_base)) . DS;
172
+	}
173 173
 }
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -37,13 +37,13 @@  discard block
 block discarded – undo
37 37
      */
38 38
     protected function setNamespace($namespace, $namespace_base_dir)
39 39
     {
40
-        if (! is_string($namespace)) {
40
+        if ( ! is_string($namespace)) {
41 41
             throw new InvalidDataTypeException('$namespace', $namespace, 'string');
42 42
         }
43
-        if (! is_string($namespace_base_dir)) {
43
+        if ( ! is_string($namespace_base_dir)) {
44 44
             throw new InvalidDataTypeException('$namespace_base_dir', $namespace_base_dir, 'string');
45 45
         }
46
-        $this->namespaces[ $namespace ] = $namespace_base_dir;
46
+        $this->namespaces[$namespace] = $namespace_base_dir;
47 47
     }
48 48
 
49 49
 
@@ -78,12 +78,12 @@  discard block
 block discarded – undo
78 78
      */
79 79
     public function locate($namespaces)
80 80
     {
81
-        if (! (is_string($namespaces) || is_array($namespaces))) {
81
+        if ( ! (is_string($namespaces) || is_array($namespaces))) {
82 82
             throw new InvalidDataTypeException('$namespaces', $namespaces, 'string or array');
83 83
         }
84 84
         foreach ((array) $namespaces as $namespace) {
85 85
             foreach ($this->findFQCNsByNamespace($namespace) as $key => $file) {
86
-                $this->FQCNs[ $key ] = $file;
86
+                $this->FQCNs[$key] = $file;
87 87
             }
88 88
         }
89 89
         return $this->FQCNs;
@@ -113,11 +113,11 @@  discard block
 block discarded – undo
113 113
         }
114 114
         foreach ($iterator as $file) {
115 115
             if ($file->isFile() && $file->getExtension() === 'php') {
116
-                $file = $file->getPath() . DS . $file->getBasename('.php');
116
+                $file = $file->getPath().DS.$file->getBasename('.php');
117 117
                 foreach ($this->namespaces as $namespace => $base_dir) {
118 118
                     $namespace .= Psr4Autoloader::NS;
119 119
                     if (strpos($file, $base_dir) === 0) {
120
-                        $this->FQCNs[] = Psr4Autoloader::NS . str_replace(
120
+                        $this->FQCNs[] = Psr4Autoloader::NS.str_replace(
121 121
                             array($base_dir, DS),
122 122
                             array($namespace, Psr4Autoloader::NS),
123 123
                             $file
@@ -151,11 +151,11 @@  discard block
 block discarded – undo
151 151
         // we're only interested in the Vendor and secondary base, so pull those from the array
152 152
         $vendor_base = array_slice($namespace_segments, 0, 2);
153 153
         $namespace = $prefix = null;
154
-        while (! empty($vendor_base)) {
154
+        while ( ! empty($vendor_base)) {
155 155
             $namespace = implode(Psr4Autoloader::NS, $vendor_base);
156 156
             // check if there's a base directory registered for that namespace
157
-            $prefix = $psr4_loader->prefixes($namespace . Psr4Autoloader::NS);
158
-            if (! empty($prefix) && ! empty($prefix[0])) {
157
+            $prefix = $psr4_loader->prefixes($namespace.Psr4Autoloader::NS);
158
+            if ( ! empty($prefix) && ! empty($prefix[0])) {
159 159
                 // found one!
160 160
                 break;
161 161
             }
@@ -168,6 +168,6 @@  discard block
 block discarded – undo
168 168
         }
169 169
         $this->setNamespace($namespace, $prefix[0]);
170 170
         // but if it's good, add that base directory to the rest of the path, and return it
171
-        return $prefix[0] . implode(DS, array_diff($namespace_segments, $vendor_base)) . DS;
171
+        return $prefix[0].implode(DS, array_diff($namespace_segments, $vendor_base)).DS;
172 172
     }
173 173
 }
Please login to merge, or discard this patch.
core/db_models/EEM_Datetime.model.php 2 patches
Indentation   +743 added lines, -743 removed lines patch added patch discarded remove patch
@@ -13,747 +13,747 @@
 block discarded – undo
13 13
 class EEM_Datetime extends EEM_Soft_Delete_Base
14 14
 {
15 15
 
16
-    /**
17
-     * @var EEM_Datetime $_instance
18
-     */
19
-    protected static $_instance;
20
-
21
-
22
-    /**
23
-     * private constructor to prevent direct creation
24
-     *
25
-     * @param string $timezone A string representing the timezone we want to set for returned Date Time Strings
26
-     *                         (and any incoming timezone data that gets saved).
27
-     *                         Note this just sends the timezone info to the date time model field objects.
28
-     *                         Default is NULL
29
-     *                         (and will be assumed using the set timezone in the 'timezone_string' wp option)
30
-     * @throws EE_Error
31
-     * @throws InvalidArgumentException
32
-     * @throws InvalidArgumentException
33
-     */
34
-    protected function __construct($timezone)
35
-    {
36
-        $this->singular_item           = esc_html__('Datetime', 'event_espresso');
37
-        $this->plural_item             = esc_html__('Datetimes', 'event_espresso');
38
-        $this->_tables                 = array(
39
-            'Datetime' => new EE_Primary_Table('esp_datetime', 'DTT_ID'),
40
-        );
41
-        $this->_fields                 = array(
42
-            'Datetime' => array(
43
-                'DTT_ID'          => new EE_Primary_Key_Int_Field(
44
-                    'DTT_ID',
45
-                    esc_html__('Datetime ID', 'event_espresso')
46
-                ),
47
-                'EVT_ID'          => new EE_Foreign_Key_Int_Field(
48
-                    'EVT_ID',
49
-                    esc_html__('Event ID', 'event_espresso'),
50
-                    false,
51
-                    0,
52
-                    'Event'
53
-                ),
54
-                'DTT_name'        => new EE_Plain_Text_Field(
55
-                    'DTT_name',
56
-                    esc_html__('Datetime Name', 'event_espresso'),
57
-                    false,
58
-                    ''
59
-                ),
60
-                'DTT_description' => new EE_Post_Content_Field(
61
-                    'DTT_description',
62
-                    esc_html__('Description for Datetime', 'event_espresso'),
63
-                    false,
64
-                    ''
65
-                ),
66
-                'DTT_EVT_start'   => new EE_Datetime_Field(
67
-                    'DTT_EVT_start',
68
-                    esc_html__('Start time/date of Event', 'event_espresso'),
69
-                    false,
70
-                    EE_Datetime_Field::now,
71
-                    $timezone
72
-                ),
73
-                'DTT_EVT_end'     => new EE_Datetime_Field(
74
-                    'DTT_EVT_end',
75
-                    esc_html__('End time/date of Event', 'event_espresso'),
76
-                    false,
77
-                    EE_Datetime_Field::now,
78
-                    $timezone
79
-                ),
80
-                'DTT_reg_limit'   => new EE_Infinite_Integer_Field(
81
-                    'DTT_reg_limit',
82
-                    esc_html__('Registration Limit for this time', 'event_espresso'),
83
-                    true,
84
-                    EE_INF
85
-                ),
86
-                'DTT_sold'        => new EE_Integer_Field(
87
-                    'DTT_sold',
88
-                    esc_html__('How many sales for this Datetime that have occurred', 'event_espresso'),
89
-                    true,
90
-                    0
91
-                ),
92
-                'DTT_reserved'    => new EE_Integer_Field(
93
-                    'DTT_reserved',
94
-                    esc_html__('Quantity of tickets reserved, but not yet fully purchased', 'event_espresso'),
95
-                    false,
96
-                    0
97
-                ),
98
-                'DTT_is_primary'  => new EE_Boolean_Field(
99
-                    'DTT_is_primary',
100
-                    esc_html__('Flag indicating datetime is primary one for event', 'event_espresso'),
101
-                    false,
102
-                    false
103
-                ),
104
-                'DTT_order'       => new EE_Integer_Field(
105
-                    'DTT_order',
106
-                    esc_html__('The order in which the Datetime is displayed', 'event_espresso'),
107
-                    false,
108
-                    0
109
-                ),
110
-                'DTT_parent'      => new EE_Integer_Field(
111
-                    'DTT_parent',
112
-                    esc_html__('Indicates what DTT_ID is the parent of this DTT_ID', 'event_espresso'),
113
-                    true,
114
-                    0
115
-                ),
116
-                'DTT_deleted'     => new EE_Trashed_Flag_Field(
117
-                    'DTT_deleted',
118
-                    esc_html__('Flag indicating datetime is archived', 'event_espresso'),
119
-                    false,
120
-                    false
121
-                ),
122
-            ),
123
-        );
124
-        $this->_model_relations        = array(
125
-            'Ticket'  => new EE_HABTM_Relation('Datetime_Ticket'),
126
-            'Event'   => new EE_Belongs_To_Relation(),
127
-            'Checkin' => new EE_Has_Many_Relation(),
128
-            'Datetime_Ticket' => new EE_Has_Many_Relation(),
129
-        );
130
-        $path_to_event_model = 'Event';
131
-        $this->model_chain_to_password = $path_to_event_model;
132
-        $this->_model_chain_to_wp_user = $path_to_event_model;
133
-        // this model is generally available for reading
134
-        $this->_cap_restriction_generators[ EEM_Base::caps_read ]       = new EE_Restriction_Generator_Event_Related_Public(
135
-            $path_to_event_model
136
-        );
137
-        $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Event_Related_Protected(
138
-            $path_to_event_model
139
-        );
140
-        $this->_cap_restriction_generators[ EEM_Base::caps_edit ]       = new EE_Restriction_Generator_Event_Related_Protected(
141
-            $path_to_event_model
142
-        );
143
-        $this->_cap_restriction_generators[ EEM_Base::caps_delete ]     = new EE_Restriction_Generator_Event_Related_Protected(
144
-            $path_to_event_model,
145
-            EEM_Base::caps_edit
146
-        );
147
-        parent::__construct($timezone);
148
-    }
149
-
150
-
151
-    /**
152
-     * create new blank datetime
153
-     *
154
-     * @access public
155
-     * @return EE_Datetime[] array on success, FALSE on fail
156
-     * @throws EE_Error
157
-     * @throws InvalidArgumentException
158
-     * @throws InvalidDataTypeException
159
-     * @throws ReflectionException
160
-     * @throws InvalidInterfaceException
161
-     */
162
-    public function create_new_blank_datetime()
163
-    {
164
-        // makes sure timezone is always set.
165
-        $timezone_string = $this->get_timezone();
166
-        /**
167
-         * Filters the initial start date for the new datetime.
168
-         * Any time included in this value will be overridden later so use additional filters to modify the time.
169
-         *
170
-         * @param int $start_date Unixtimestamp representing now + 30 days in seconds.
171
-         * @return int unixtimestamp
172
-         */
173
-        $start_date = apply_filters(
174
-            'FHEE__EEM_Datetime__create_new_blank_datetime__start_date',
175
-            $this->current_time_for_query('DTT_EVT_start', true) + MONTH_IN_SECONDS
176
-        );
177
-        /**
178
-         * Filters the initial end date for the new datetime.
179
-         * Any time included in this value will be overridden later so use additional filters to modify the time.
180
-         *
181
-         * @param int $end_data Unixtimestamp representing now + 30 days in seconds.
182
-         * @return int unixtimestamp
183
-         */
184
-        $end_date = apply_filters(
185
-            'FHEE__EEM_Datetime__create_new_blank_datetime__end_date',
186
-            $this->current_time_for_query('DTT_EVT_end', true) + MONTH_IN_SECONDS
187
-        );
188
-        $blank_datetime = EE_Datetime::new_instance(
189
-            array(
190
-                'DTT_EVT_start' => $start_date,
191
-                'DTT_EVT_end'   => $end_date,
192
-                'DTT_order'     => 1,
193
-                'DTT_reg_limit' => EE_INF,
194
-            ),
195
-            $timezone_string
196
-        );
197
-        /**
198
-         * Filters the initial start time and format for the new EE_Datetime instance.
199
-         *
200
-         * @param array $start_time An array having size 2.  First element is the time, second element is the time
201
-         *                          format.
202
-         * @return array
203
-         */
204
-        $start_time = apply_filters(
205
-            'FHEE__EEM_Datetime__create_new_blank_datetime__start_time',
206
-            ['8am', 'ga']
207
-        );
208
-        /**
209
-         * Filters the initial end time and format for the new EE_Datetime instance.
210
-         *
211
-         * @param array $end_time An array having size 2.  First element is the time, second element is the time
212
-         *                        format
213
-         * @return array
214
-         */
215
-        $end_time = apply_filters(
216
-            'FHEE__EEM_Datetime__create_new_blank_datetime__end_time',
217
-            ['5pm', 'ga']
218
-        );
219
-        $this->validateStartAndEndTimeForBlankDate($start_time, $end_time);
220
-        $blank_datetime->set_start_time(
221
-            $this->convert_datetime_for_query(
222
-                'DTT_EVT_start',
223
-                $start_time[0],
224
-                $start_time[1],
225
-                $timezone_string
226
-            )
227
-        );
228
-        $blank_datetime->set_end_time(
229
-            $this->convert_datetime_for_query(
230
-                'DTT_EVT_end',
231
-                $end_time[0],
232
-                $end_time[1],
233
-                $timezone_string
234
-            )
235
-        );
236
-        return array($blank_datetime);
237
-    }
238
-
239
-
240
-    /**
241
-     * Validates whether the start_time and end_time are in the expected format.
242
-     * @param array $start_time
243
-     * @param array $end_time
244
-     * @throws InvalidArgumentException
245
-     * @throws InvalidDataTypeException
246
-     */
247
-    private function validateStartAndEndTimeForBlankDate($start_time, $end_time)
248
-    {
249
-        if (! is_array($start_time)) {
250
-            throw new InvalidDataTypeException('start_time', $start_time, 'array');
251
-        }
252
-        if (! is_array($end_time)) {
253
-            throw new InvalidDataTypeException('end_time', $end_time, 'array');
254
-        }
255
-        if (count($start_time) !== 2) {
256
-            throw new InvalidArgumentException(
257
-                sprintf(
258
-                    'The variable %1$s is expected to be an array with two elements.  The first item in the '
259
-                    . 'array should be a valid time string, the second item in the array should be a valid time format',
260
-                    '$start_time'
261
-                )
262
-            );
263
-        }
264
-        if (count($end_time) !== 2) {
265
-            throw new InvalidArgumentException(
266
-                sprintf(
267
-                    'The variable %1$s is expected to be an array with two elements.  The first item in the '
268
-                    . 'array should be a valid time string, the second item in the array should be a valid time format',
269
-                    '$end_time'
270
-                )
271
-            );
272
-        }
273
-    }
274
-
275
-
276
-    /**
277
-     * get event start date from db
278
-     *
279
-     * @access public
280
-     * @param  int $EVT_ID
281
-     * @return EE_Datetime[] array on success, FALSE on fail
282
-     * @throws EE_Error
283
-     */
284
-    public function get_all_event_dates($EVT_ID = 0)
285
-    {
286
-        if (! $EVT_ID) { // on add_new_event event_id gets set to 0
287
-            return $this->create_new_blank_datetime();
288
-        }
289
-        $results = $this->get_datetimes_for_event_ordered_by_DTT_order($EVT_ID);
290
-        if (empty($results)) {
291
-            return $this->create_new_blank_datetime();
292
-        }
293
-        return $results;
294
-    }
295
-
296
-
297
-    /**
298
-     * get all datetimes attached to an event ordered by the DTT_order field
299
-     *
300
-     * @public
301
-     * @param  int    $EVT_ID     event id
302
-     * @param boolean $include_expired
303
-     * @param boolean $include_deleted
304
-     * @param  int    $limit      If included then limit the count of results by
305
-     *                            the given number
306
-     * @return EE_Datetime[]
307
-     * @throws EE_Error
308
-     */
309
-    public function get_datetimes_for_event_ordered_by_DTT_order(
310
-        $EVT_ID,
311
-        $include_expired = true,
312
-        $include_deleted = true,
313
-        $limit = null
314
-    ) {
315
-        // sanitize EVT_ID
316
-        $EVT_ID         = absint($EVT_ID);
317
-        $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object();
318
-        $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db);
319
-        $where_params = array('Event.EVT_ID' => $EVT_ID);
320
-        $query_params = ! empty($limit)
321
-            ? array(
322
-                $where_params,
323
-                'limit'                    => $limit,
324
-                'order_by'                 => array('DTT_order' => 'ASC'),
325
-                'default_where_conditions' => 'none',
326
-            )
327
-            : array(
328
-                $where_params,
329
-                'order_by'                 => array('DTT_order' => 'ASC'),
330
-                'default_where_conditions' => 'none',
331
-            );
332
-        if (! $include_expired) {
333
-            $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true));
334
-        }
335
-        if ($include_deleted) {
336
-            $query_params[0]['DTT_deleted'] = array('IN', array(true, false));
337
-        }
338
-        /** @var EE_Datetime[] $result */
339
-        $result = $this->get_all($query_params);
340
-        $this->assume_values_already_prepared_by_model_object($old_assumption);
341
-        return $result;
342
-    }
343
-
344
-
345
-    /**
346
-     * Gets the datetimes for the event (with the given limit), and orders them by "importance".
347
-     * By importance, we mean that the primary datetimes are most important (DEPRECATED FOR NOW),
348
-     * and then the earlier datetimes are the most important.
349
-     * Maybe we'll want this to take into account datetimes that haven't already passed, but we don't yet.
350
-     *
351
-     * @param int $EVT_ID
352
-     * @param int $limit
353
-     * @return EE_Datetime[]|EE_Base_Class[]
354
-     * @throws EE_Error
355
-     */
356
-    public function get_datetimes_for_event_ordered_by_importance($EVT_ID = 0, $limit = null)
357
-    {
358
-        return $this->get_all(
359
-            array(
360
-                array('Event.EVT_ID' => $EVT_ID),
361
-                'limit'                    => $limit,
362
-                'order_by'                 => array('DTT_EVT_start' => 'ASC'),
363
-                'default_where_conditions' => 'none',
364
-            )
365
-        );
366
-    }
367
-
368
-
369
-    /**
370
-     * @param int     $EVT_ID
371
-     * @param boolean $include_expired
372
-     * @param boolean $include_deleted
373
-     * @return EE_Datetime
374
-     * @throws EE_Error
375
-     */
376
-    public function get_oldest_datetime_for_event($EVT_ID, $include_expired = false, $include_deleted = false)
377
-    {
378
-        $results = $this->get_datetimes_for_event_ordered_by_start_time(
379
-            $EVT_ID,
380
-            $include_expired,
381
-            $include_deleted,
382
-            1
383
-        );
384
-        if ($results) {
385
-            return array_shift($results);
386
-        }
387
-        return null;
388
-    }
389
-
390
-
391
-    /**
392
-     * Gets the 'primary' datetime for an event.
393
-     *
394
-     * @param int  $EVT_ID
395
-     * @param bool $try_to_exclude_expired
396
-     * @param bool $try_to_exclude_deleted
397
-     * @return \EE_Datetime
398
-     * @throws EE_Error
399
-     */
400
-    public function get_primary_datetime_for_event(
401
-        $EVT_ID,
402
-        $try_to_exclude_expired = true,
403
-        $try_to_exclude_deleted = true
404
-    ) {
405
-        if ($try_to_exclude_expired) {
406
-            $non_expired = $this->get_oldest_datetime_for_event($EVT_ID, false, false);
407
-            if ($non_expired) {
408
-                return $non_expired;
409
-            }
410
-        }
411
-        if ($try_to_exclude_deleted) {
412
-            $expired_even = $this->get_oldest_datetime_for_event($EVT_ID, true);
413
-            if ($expired_even) {
414
-                return $expired_even;
415
-            }
416
-        }
417
-        return $this->get_oldest_datetime_for_event($EVT_ID, true, true);
418
-    }
419
-
420
-
421
-    /**
422
-     * Gets ALL the datetimes for an event (including trashed ones, for now), ordered
423
-     * only by start date
424
-     *
425
-     * @param int     $EVT_ID
426
-     * @param boolean $include_expired
427
-     * @param boolean $include_deleted
428
-     * @param int     $limit
429
-     * @return EE_Datetime[]
430
-     * @throws EE_Error
431
-     */
432
-    public function get_datetimes_for_event_ordered_by_start_time(
433
-        $EVT_ID,
434
-        $include_expired = true,
435
-        $include_deleted = true,
436
-        $limit = null
437
-    ) {
438
-        // sanitize EVT_ID
439
-        $EVT_ID         = absint($EVT_ID);
440
-        $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object();
441
-        $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db);
442
-        $query_params = array(array('Event.EVT_ID' => $EVT_ID), 'order_by' => array('DTT_EVT_start' => 'asc'));
443
-        if (! $include_expired) {
444
-            $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true));
445
-        }
446
-        if ($include_deleted) {
447
-            $query_params[0]['DTT_deleted'] = array('IN', array(true, false));
448
-        }
449
-        if ($limit) {
450
-            $query_params['limit'] = $limit;
451
-        }
452
-        /** @var EE_Datetime[] $result */
453
-        $result = $this->get_all($query_params);
454
-        $this->assume_values_already_prepared_by_model_object($old_assumption);
455
-        return $result;
456
-    }
457
-
458
-
459
-    /**
460
-     * Gets ALL the datetimes for an ticket (including trashed ones, for now), ordered
461
-     * only by start date
462
-     *
463
-     * @param int     $TKT_ID
464
-     * @param boolean $include_expired
465
-     * @param boolean $include_deleted
466
-     * @param int     $limit
467
-     * @return EE_Datetime[]
468
-     * @throws EE_Error
469
-     */
470
-    public function get_datetimes_for_ticket_ordered_by_start_time(
471
-        $TKT_ID,
472
-        $include_expired = true,
473
-        $include_deleted = true,
474
-        $limit = null
475
-    ) {
476
-        // sanitize TKT_ID
477
-        $TKT_ID         = absint($TKT_ID);
478
-        $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object();
479
-        $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db);
480
-        $query_params = array(array('Ticket.TKT_ID' => $TKT_ID), 'order_by' => array('DTT_EVT_start' => 'asc'));
481
-        if (! $include_expired) {
482
-            $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true));
483
-        }
484
-        if ($include_deleted) {
485
-            $query_params[0]['DTT_deleted'] = array('IN', array(true, false));
486
-        }
487
-        if ($limit) {
488
-            $query_params['limit'] = $limit;
489
-        }
490
-        /** @var EE_Datetime[] $result */
491
-        $result = $this->get_all($query_params);
492
-        $this->assume_values_already_prepared_by_model_object($old_assumption);
493
-        return $result;
494
-    }
495
-
496
-
497
-    /**
498
-     * Gets all the datetimes for a ticket (including trashed ones, for now), ordered by the DTT_order for the
499
-     * datetimes.
500
-     *
501
-     * @param  int      $TKT_ID          ID of ticket to retrieve the datetimes for
502
-     * @param  boolean  $include_expired whether to include expired datetimes or not
503
-     * @param  boolean  $include_deleted whether to include trashed datetimes or not.
504
-     * @param  int|null $limit           if null, no limit, if int then limit results by
505
-     *                                   that number
506
-     * @return EE_Datetime[]
507
-     * @throws EE_Error
508
-     */
509
-    public function get_datetimes_for_ticket_ordered_by_DTT_order(
510
-        $TKT_ID,
511
-        $include_expired = true,
512
-        $include_deleted = true,
513
-        $limit = null
514
-    ) {
515
-        // sanitize id.
516
-        $TKT_ID         = absint($TKT_ID);
517
-        $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object();
518
-        $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db);
519
-        $where_params = array('Ticket.TKT_ID' => $TKT_ID);
520
-        $query_params = array($where_params, 'order_by' => array('DTT_order' => 'ASC'));
521
-        if (! $include_expired) {
522
-            $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true));
523
-        }
524
-        if ($include_deleted) {
525
-            $query_params[0]['DTT_deleted'] = array('IN', array(true, false));
526
-        }
527
-        if ($limit) {
528
-            $query_params['limit'] = $limit;
529
-        }
530
-        /** @var EE_Datetime[] $result */
531
-        $result = $this->get_all($query_params);
532
-        $this->assume_values_already_prepared_by_model_object($old_assumption);
533
-        return $result;
534
-    }
535
-
536
-
537
-    /**
538
-     * Gets the most important datetime for a particular event (ie, the primary event usually. But if for some WACK
539
-     * reason it doesn't exist, we consider the earliest event the most important)
540
-     *
541
-     * @param int $EVT_ID
542
-     * @return EE_Datetime
543
-     * @throws EE_Error
544
-     */
545
-    public function get_most_important_datetime_for_event($EVT_ID)
546
-    {
547
-        $results = $this->get_datetimes_for_event_ordered_by_importance($EVT_ID, 1);
548
-        if ($results) {
549
-            return array_shift($results);
550
-        }
551
-        return null;
552
-    }
553
-
554
-
555
-    /**
556
-     * This returns a wpdb->results        Array of all DTT month and years matching the incoming query params and
557
-     * grouped by month and year.
558
-     *
559
-     * @param  array  $where_params      @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions
560
-     * @param  string $evt_active_status A string representing the evt active status to filter the months by.
561
-     *                                   Can be:
562
-     *                                   - '' = no filter
563
-     *                                   - upcoming = Published events with at least one upcoming datetime.
564
-     *                                   - expired = Events with all datetimes expired.
565
-     *                                   - active = Events that are published and have at least one datetime that
566
-     *                                   starts before now and ends after now.
567
-     *                                   - inactive = Events that are either not published.
568
-     * @return EE_Base_Class[]
569
-     * @throws EE_Error
570
-     * @throws InvalidArgumentException
571
-     * @throws InvalidArgumentException
572
-     */
573
-    public function get_dtt_months_and_years($where_params, $evt_active_status = '')
574
-    {
575
-        $current_time_for_DTT_EVT_start = $this->current_time_for_query('DTT_EVT_start');
576
-        $current_time_for_DTT_EVT_end   = $this->current_time_for_query('DTT_EVT_end');
577
-        switch ($evt_active_status) {
578
-            case 'upcoming':
579
-                $where_params['Event.status'] = 'publish';
580
-                // if there are already query_params matching DTT_EVT_start then we need to modify that to add them.
581
-                if (isset($where_params['DTT_EVT_start'])) {
582
-                    $where_params['DTT_EVT_start*****'] = $where_params['DTT_EVT_start'];
583
-                }
584
-                $where_params['DTT_EVT_start'] = array('>', $current_time_for_DTT_EVT_start);
585
-                break;
586
-            case 'expired':
587
-                if (isset($where_params['Event.status'])) {
588
-                    unset($where_params['Event.status']);
589
-                }
590
-                // get events to exclude
591
-                $exclude_query[0] = array_merge(
592
-                    $where_params,
593
-                    array('DTT_EVT_end' => array('>', $current_time_for_DTT_EVT_end))
594
-                );
595
-                // first get all events that have datetimes where its not expired.
596
-                $event_ids = $this->_get_all_wpdb_results(
597
-                    $exclude_query,
598
-                    OBJECT_K,
599
-                    'Datetime.EVT_ID'
600
-                );
601
-                $event_ids = array_keys($event_ids);
602
-                if (isset($where_params['DTT_EVT_end'])) {
603
-                    $where_params['DTT_EVT_end****'] = $where_params['DTT_EVT_end'];
604
-                }
605
-                $where_params['DTT_EVT_end']  = array('<', $current_time_for_DTT_EVT_end);
606
-                $where_params['Event.EVT_ID'] = array('NOT IN', $event_ids);
607
-                break;
608
-            case 'active':
609
-                $where_params['Event.status'] = 'publish';
610
-                if (isset($where_params['DTT_EVT_start'])) {
611
-                    $where_params['Datetime.DTT_EVT_start******'] = $where_params['DTT_EVT_start'];
612
-                }
613
-                if (isset($where_params['Datetime.DTT_EVT_end'])) {
614
-                    $where_params['Datetime.DTT_EVT_end*****'] = $where_params['DTT_EVT_end'];
615
-                }
616
-                $where_params['DTT_EVT_start'] = array('<', $current_time_for_DTT_EVT_start);
617
-                $where_params['DTT_EVT_end']   = array('>', $current_time_for_DTT_EVT_end);
618
-                break;
619
-            case 'inactive':
620
-                if (isset($where_params['Event.status'])) {
621
-                    unset($where_params['Event.status']);
622
-                }
623
-                if (isset($where_params['OR'])) {
624
-                    $where_params['AND']['OR'] = $where_params['OR'];
625
-                }
626
-                if (isset($where_params['DTT_EVT_end'])) {
627
-                    $where_params['AND']['DTT_EVT_end****'] = $where_params['DTT_EVT_end'];
628
-                    unset($where_params['DTT_EVT_end']);
629
-                }
630
-                if (isset($where_params['DTT_EVT_start'])) {
631
-                    $where_params['AND']['DTT_EVT_start'] = $where_params['DTT_EVT_start'];
632
-                    unset($where_params['DTT_EVT_start']);
633
-                }
634
-                $where_params['AND']['Event.status'] = array('!=', 'publish');
635
-                break;
636
-        }
637
-        $query_params[0]          = $where_params;
638
-        $query_params['group_by'] = array('dtt_year', 'dtt_month');
639
-        $query_params['order_by'] = array('DTT_EVT_start' => 'DESC');
640
-        $query_interval           = EEH_DTT_Helper::get_sql_query_interval_for_offset(
641
-            $this->get_timezone(),
642
-            'DTT_EVT_start'
643
-        );
644
-        $columns_to_select        = array(
645
-            'dtt_year'      => array('YEAR(' . $query_interval . ')', '%s'),
646
-            'dtt_month'     => array('MONTHNAME(' . $query_interval . ')', '%s'),
647
-            'dtt_month_num' => array('MONTH(' . $query_interval . ')', '%s'),
648
-        );
649
-        return $this->_get_all_wpdb_results($query_params, OBJECT, $columns_to_select);
650
-    }
651
-
652
-
653
-    /**
654
-     * Updates the DTT_sold attribute on each datetime (based on the registrations
655
-     * for the tickets for each datetime)
656
-     *
657
-     * @param EE_Base_Class[]|EE_Datetime[] $datetimes
658
-     * @throws EE_Error
659
-     */
660
-    public function update_sold($datetimes)
661
-    {
662
-        EE_Error::doing_it_wrong(
663
-            __FUNCTION__,
664
-            esc_html__(
665
-                'Please use \EEM_Ticket::update_tickets_sold() instead which will in turn correctly update both the Ticket AND Datetime counts.',
666
-                'event_espresso'
667
-            ),
668
-            '4.9.32.rc.005'
669
-        );
670
-        foreach ($datetimes as $datetime) {
671
-            $datetime->update_sold();
672
-        }
673
-    }
674
-
675
-
676
-    /**
677
-     *    Gets the total number of tickets available at a particular datetime
678
-     *    (does NOT take into account the datetime's spaces available)
679
-     *
680
-     * @param int   $DTT_ID
681
-     * @param array $query_params
682
-     * @return int of tickets available. If sold out, return less than 1. If infinite, returns EE_INF,  IF there are NO
683
-     *             tickets attached to datetime then FALSE is returned.
684
-     */
685
-    public function sum_tickets_currently_available_at_datetime($DTT_ID, array $query_params = array())
686
-    {
687
-        $datetime = $this->get_one_by_ID($DTT_ID);
688
-        if ($datetime instanceof EE_Datetime) {
689
-            return $datetime->tickets_remaining($query_params);
690
-        }
691
-        return 0;
692
-    }
693
-
694
-
695
-    /**
696
-     * This returns an array of counts of datetimes in the database for each Datetime status that can be queried.
697
-     *
698
-     * @param  array $stati_to_include If included you can restrict the statuses we return counts for by including the
699
-     *                                 stati you want counts for as values in the array.  An empty array returns counts
700
-     *                                 for all valid stati.
701
-     * @param  array $query_params     If included can be used to refine the conditions for returning the count (i.e.
702
-     *                                 only for Datetimes connected to a specific event, or specific ticket.
703
-     * @return array  The value returned is an array indexed by Datetime Status and the values are the counts.  The
704
-     * @throws EE_Error
705
-     *                                 stati used as index keys are: EE_Datetime::active EE_Datetime::upcoming
706
-     *                                 EE_Datetime::expired
707
-     */
708
-    public function get_datetime_counts_by_status(array $stati_to_include = array(), array $query_params = array())
709
-    {
710
-        // only accept where conditions for this query.
711
-        $_where            = isset($query_params[0]) ? $query_params[0] : array();
712
-        $status_query_args = array(
713
-            EE_Datetime::active   => array_merge(
714
-                $_where,
715
-                array('DTT_EVT_start' => array('<', time()), 'DTT_EVT_end' => array('>', time()))
716
-            ),
717
-            EE_Datetime::upcoming => array_merge(
718
-                $_where,
719
-                array('DTT_EVT_start' => array('>', time()))
720
-            ),
721
-            EE_Datetime::expired  => array_merge(
722
-                $_where,
723
-                array('DTT_EVT_end' => array('<', time()))
724
-            ),
725
-        );
726
-        if (! empty($stati_to_include)) {
727
-            foreach (array_keys($status_query_args) as $status) {
728
-                if (! in_array($status, $stati_to_include, true)) {
729
-                    unset($status_query_args[ $status ]);
730
-                }
731
-            }
732
-        }
733
-        // loop through and query counts for each stati.
734
-        $status_query_results = array();
735
-        foreach ($status_query_args as $status => $status_where_conditions) {
736
-            $status_query_results[ $status ] = EEM_Datetime::count(
737
-                array($status_where_conditions),
738
-                'DTT_ID',
739
-                true
740
-            );
741
-        }
742
-        return $status_query_results;
743
-    }
744
-
745
-
746
-    /**
747
-     * Returns the specific count for a given Datetime status matching any given query_params.
748
-     *
749
-     * @param string $status Valid string representation for Datetime status requested. (Defaults to Active).
750
-     * @param array  $query_params
751
-     * @return int
752
-     * @throws EE_Error
753
-     */
754
-    public function get_datetime_count_for_status($status = EE_Datetime::active, array $query_params = array())
755
-    {
756
-        $count = $this->get_datetime_counts_by_status(array($status), $query_params);
757
-        return ! empty($count[ $status ]) ? $count[ $status ] : 0;
758
-    }
16
+	/**
17
+	 * @var EEM_Datetime $_instance
18
+	 */
19
+	protected static $_instance;
20
+
21
+
22
+	/**
23
+	 * private constructor to prevent direct creation
24
+	 *
25
+	 * @param string $timezone A string representing the timezone we want to set for returned Date Time Strings
26
+	 *                         (and any incoming timezone data that gets saved).
27
+	 *                         Note this just sends the timezone info to the date time model field objects.
28
+	 *                         Default is NULL
29
+	 *                         (and will be assumed using the set timezone in the 'timezone_string' wp option)
30
+	 * @throws EE_Error
31
+	 * @throws InvalidArgumentException
32
+	 * @throws InvalidArgumentException
33
+	 */
34
+	protected function __construct($timezone)
35
+	{
36
+		$this->singular_item           = esc_html__('Datetime', 'event_espresso');
37
+		$this->plural_item             = esc_html__('Datetimes', 'event_espresso');
38
+		$this->_tables                 = array(
39
+			'Datetime' => new EE_Primary_Table('esp_datetime', 'DTT_ID'),
40
+		);
41
+		$this->_fields                 = array(
42
+			'Datetime' => array(
43
+				'DTT_ID'          => new EE_Primary_Key_Int_Field(
44
+					'DTT_ID',
45
+					esc_html__('Datetime ID', 'event_espresso')
46
+				),
47
+				'EVT_ID'          => new EE_Foreign_Key_Int_Field(
48
+					'EVT_ID',
49
+					esc_html__('Event ID', 'event_espresso'),
50
+					false,
51
+					0,
52
+					'Event'
53
+				),
54
+				'DTT_name'        => new EE_Plain_Text_Field(
55
+					'DTT_name',
56
+					esc_html__('Datetime Name', 'event_espresso'),
57
+					false,
58
+					''
59
+				),
60
+				'DTT_description' => new EE_Post_Content_Field(
61
+					'DTT_description',
62
+					esc_html__('Description for Datetime', 'event_espresso'),
63
+					false,
64
+					''
65
+				),
66
+				'DTT_EVT_start'   => new EE_Datetime_Field(
67
+					'DTT_EVT_start',
68
+					esc_html__('Start time/date of Event', 'event_espresso'),
69
+					false,
70
+					EE_Datetime_Field::now,
71
+					$timezone
72
+				),
73
+				'DTT_EVT_end'     => new EE_Datetime_Field(
74
+					'DTT_EVT_end',
75
+					esc_html__('End time/date of Event', 'event_espresso'),
76
+					false,
77
+					EE_Datetime_Field::now,
78
+					$timezone
79
+				),
80
+				'DTT_reg_limit'   => new EE_Infinite_Integer_Field(
81
+					'DTT_reg_limit',
82
+					esc_html__('Registration Limit for this time', 'event_espresso'),
83
+					true,
84
+					EE_INF
85
+				),
86
+				'DTT_sold'        => new EE_Integer_Field(
87
+					'DTT_sold',
88
+					esc_html__('How many sales for this Datetime that have occurred', 'event_espresso'),
89
+					true,
90
+					0
91
+				),
92
+				'DTT_reserved'    => new EE_Integer_Field(
93
+					'DTT_reserved',
94
+					esc_html__('Quantity of tickets reserved, but not yet fully purchased', 'event_espresso'),
95
+					false,
96
+					0
97
+				),
98
+				'DTT_is_primary'  => new EE_Boolean_Field(
99
+					'DTT_is_primary',
100
+					esc_html__('Flag indicating datetime is primary one for event', 'event_espresso'),
101
+					false,
102
+					false
103
+				),
104
+				'DTT_order'       => new EE_Integer_Field(
105
+					'DTT_order',
106
+					esc_html__('The order in which the Datetime is displayed', 'event_espresso'),
107
+					false,
108
+					0
109
+				),
110
+				'DTT_parent'      => new EE_Integer_Field(
111
+					'DTT_parent',
112
+					esc_html__('Indicates what DTT_ID is the parent of this DTT_ID', 'event_espresso'),
113
+					true,
114
+					0
115
+				),
116
+				'DTT_deleted'     => new EE_Trashed_Flag_Field(
117
+					'DTT_deleted',
118
+					esc_html__('Flag indicating datetime is archived', 'event_espresso'),
119
+					false,
120
+					false
121
+				),
122
+			),
123
+		);
124
+		$this->_model_relations        = array(
125
+			'Ticket'  => new EE_HABTM_Relation('Datetime_Ticket'),
126
+			'Event'   => new EE_Belongs_To_Relation(),
127
+			'Checkin' => new EE_Has_Many_Relation(),
128
+			'Datetime_Ticket' => new EE_Has_Many_Relation(),
129
+		);
130
+		$path_to_event_model = 'Event';
131
+		$this->model_chain_to_password = $path_to_event_model;
132
+		$this->_model_chain_to_wp_user = $path_to_event_model;
133
+		// this model is generally available for reading
134
+		$this->_cap_restriction_generators[ EEM_Base::caps_read ]       = new EE_Restriction_Generator_Event_Related_Public(
135
+			$path_to_event_model
136
+		);
137
+		$this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Event_Related_Protected(
138
+			$path_to_event_model
139
+		);
140
+		$this->_cap_restriction_generators[ EEM_Base::caps_edit ]       = new EE_Restriction_Generator_Event_Related_Protected(
141
+			$path_to_event_model
142
+		);
143
+		$this->_cap_restriction_generators[ EEM_Base::caps_delete ]     = new EE_Restriction_Generator_Event_Related_Protected(
144
+			$path_to_event_model,
145
+			EEM_Base::caps_edit
146
+		);
147
+		parent::__construct($timezone);
148
+	}
149
+
150
+
151
+	/**
152
+	 * create new blank datetime
153
+	 *
154
+	 * @access public
155
+	 * @return EE_Datetime[] array on success, FALSE on fail
156
+	 * @throws EE_Error
157
+	 * @throws InvalidArgumentException
158
+	 * @throws InvalidDataTypeException
159
+	 * @throws ReflectionException
160
+	 * @throws InvalidInterfaceException
161
+	 */
162
+	public function create_new_blank_datetime()
163
+	{
164
+		// makes sure timezone is always set.
165
+		$timezone_string = $this->get_timezone();
166
+		/**
167
+		 * Filters the initial start date for the new datetime.
168
+		 * Any time included in this value will be overridden later so use additional filters to modify the time.
169
+		 *
170
+		 * @param int $start_date Unixtimestamp representing now + 30 days in seconds.
171
+		 * @return int unixtimestamp
172
+		 */
173
+		$start_date = apply_filters(
174
+			'FHEE__EEM_Datetime__create_new_blank_datetime__start_date',
175
+			$this->current_time_for_query('DTT_EVT_start', true) + MONTH_IN_SECONDS
176
+		);
177
+		/**
178
+		 * Filters the initial end date for the new datetime.
179
+		 * Any time included in this value will be overridden later so use additional filters to modify the time.
180
+		 *
181
+		 * @param int $end_data Unixtimestamp representing now + 30 days in seconds.
182
+		 * @return int unixtimestamp
183
+		 */
184
+		$end_date = apply_filters(
185
+			'FHEE__EEM_Datetime__create_new_blank_datetime__end_date',
186
+			$this->current_time_for_query('DTT_EVT_end', true) + MONTH_IN_SECONDS
187
+		);
188
+		$blank_datetime = EE_Datetime::new_instance(
189
+			array(
190
+				'DTT_EVT_start' => $start_date,
191
+				'DTT_EVT_end'   => $end_date,
192
+				'DTT_order'     => 1,
193
+				'DTT_reg_limit' => EE_INF,
194
+			),
195
+			$timezone_string
196
+		);
197
+		/**
198
+		 * Filters the initial start time and format for the new EE_Datetime instance.
199
+		 *
200
+		 * @param array $start_time An array having size 2.  First element is the time, second element is the time
201
+		 *                          format.
202
+		 * @return array
203
+		 */
204
+		$start_time = apply_filters(
205
+			'FHEE__EEM_Datetime__create_new_blank_datetime__start_time',
206
+			['8am', 'ga']
207
+		);
208
+		/**
209
+		 * Filters the initial end time and format for the new EE_Datetime instance.
210
+		 *
211
+		 * @param array $end_time An array having size 2.  First element is the time, second element is the time
212
+		 *                        format
213
+		 * @return array
214
+		 */
215
+		$end_time = apply_filters(
216
+			'FHEE__EEM_Datetime__create_new_blank_datetime__end_time',
217
+			['5pm', 'ga']
218
+		);
219
+		$this->validateStartAndEndTimeForBlankDate($start_time, $end_time);
220
+		$blank_datetime->set_start_time(
221
+			$this->convert_datetime_for_query(
222
+				'DTT_EVT_start',
223
+				$start_time[0],
224
+				$start_time[1],
225
+				$timezone_string
226
+			)
227
+		);
228
+		$blank_datetime->set_end_time(
229
+			$this->convert_datetime_for_query(
230
+				'DTT_EVT_end',
231
+				$end_time[0],
232
+				$end_time[1],
233
+				$timezone_string
234
+			)
235
+		);
236
+		return array($blank_datetime);
237
+	}
238
+
239
+
240
+	/**
241
+	 * Validates whether the start_time and end_time are in the expected format.
242
+	 * @param array $start_time
243
+	 * @param array $end_time
244
+	 * @throws InvalidArgumentException
245
+	 * @throws InvalidDataTypeException
246
+	 */
247
+	private function validateStartAndEndTimeForBlankDate($start_time, $end_time)
248
+	{
249
+		if (! is_array($start_time)) {
250
+			throw new InvalidDataTypeException('start_time', $start_time, 'array');
251
+		}
252
+		if (! is_array($end_time)) {
253
+			throw new InvalidDataTypeException('end_time', $end_time, 'array');
254
+		}
255
+		if (count($start_time) !== 2) {
256
+			throw new InvalidArgumentException(
257
+				sprintf(
258
+					'The variable %1$s is expected to be an array with two elements.  The first item in the '
259
+					. 'array should be a valid time string, the second item in the array should be a valid time format',
260
+					'$start_time'
261
+				)
262
+			);
263
+		}
264
+		if (count($end_time) !== 2) {
265
+			throw new InvalidArgumentException(
266
+				sprintf(
267
+					'The variable %1$s is expected to be an array with two elements.  The first item in the '
268
+					. 'array should be a valid time string, the second item in the array should be a valid time format',
269
+					'$end_time'
270
+				)
271
+			);
272
+		}
273
+	}
274
+
275
+
276
+	/**
277
+	 * get event start date from db
278
+	 *
279
+	 * @access public
280
+	 * @param  int $EVT_ID
281
+	 * @return EE_Datetime[] array on success, FALSE on fail
282
+	 * @throws EE_Error
283
+	 */
284
+	public function get_all_event_dates($EVT_ID = 0)
285
+	{
286
+		if (! $EVT_ID) { // on add_new_event event_id gets set to 0
287
+			return $this->create_new_blank_datetime();
288
+		}
289
+		$results = $this->get_datetimes_for_event_ordered_by_DTT_order($EVT_ID);
290
+		if (empty($results)) {
291
+			return $this->create_new_blank_datetime();
292
+		}
293
+		return $results;
294
+	}
295
+
296
+
297
+	/**
298
+	 * get all datetimes attached to an event ordered by the DTT_order field
299
+	 *
300
+	 * @public
301
+	 * @param  int    $EVT_ID     event id
302
+	 * @param boolean $include_expired
303
+	 * @param boolean $include_deleted
304
+	 * @param  int    $limit      If included then limit the count of results by
305
+	 *                            the given number
306
+	 * @return EE_Datetime[]
307
+	 * @throws EE_Error
308
+	 */
309
+	public function get_datetimes_for_event_ordered_by_DTT_order(
310
+		$EVT_ID,
311
+		$include_expired = true,
312
+		$include_deleted = true,
313
+		$limit = null
314
+	) {
315
+		// sanitize EVT_ID
316
+		$EVT_ID         = absint($EVT_ID);
317
+		$old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object();
318
+		$this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db);
319
+		$where_params = array('Event.EVT_ID' => $EVT_ID);
320
+		$query_params = ! empty($limit)
321
+			? array(
322
+				$where_params,
323
+				'limit'                    => $limit,
324
+				'order_by'                 => array('DTT_order' => 'ASC'),
325
+				'default_where_conditions' => 'none',
326
+			)
327
+			: array(
328
+				$where_params,
329
+				'order_by'                 => array('DTT_order' => 'ASC'),
330
+				'default_where_conditions' => 'none',
331
+			);
332
+		if (! $include_expired) {
333
+			$query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true));
334
+		}
335
+		if ($include_deleted) {
336
+			$query_params[0]['DTT_deleted'] = array('IN', array(true, false));
337
+		}
338
+		/** @var EE_Datetime[] $result */
339
+		$result = $this->get_all($query_params);
340
+		$this->assume_values_already_prepared_by_model_object($old_assumption);
341
+		return $result;
342
+	}
343
+
344
+
345
+	/**
346
+	 * Gets the datetimes for the event (with the given limit), and orders them by "importance".
347
+	 * By importance, we mean that the primary datetimes are most important (DEPRECATED FOR NOW),
348
+	 * and then the earlier datetimes are the most important.
349
+	 * Maybe we'll want this to take into account datetimes that haven't already passed, but we don't yet.
350
+	 *
351
+	 * @param int $EVT_ID
352
+	 * @param int $limit
353
+	 * @return EE_Datetime[]|EE_Base_Class[]
354
+	 * @throws EE_Error
355
+	 */
356
+	public function get_datetimes_for_event_ordered_by_importance($EVT_ID = 0, $limit = null)
357
+	{
358
+		return $this->get_all(
359
+			array(
360
+				array('Event.EVT_ID' => $EVT_ID),
361
+				'limit'                    => $limit,
362
+				'order_by'                 => array('DTT_EVT_start' => 'ASC'),
363
+				'default_where_conditions' => 'none',
364
+			)
365
+		);
366
+	}
367
+
368
+
369
+	/**
370
+	 * @param int     $EVT_ID
371
+	 * @param boolean $include_expired
372
+	 * @param boolean $include_deleted
373
+	 * @return EE_Datetime
374
+	 * @throws EE_Error
375
+	 */
376
+	public function get_oldest_datetime_for_event($EVT_ID, $include_expired = false, $include_deleted = false)
377
+	{
378
+		$results = $this->get_datetimes_for_event_ordered_by_start_time(
379
+			$EVT_ID,
380
+			$include_expired,
381
+			$include_deleted,
382
+			1
383
+		);
384
+		if ($results) {
385
+			return array_shift($results);
386
+		}
387
+		return null;
388
+	}
389
+
390
+
391
+	/**
392
+	 * Gets the 'primary' datetime for an event.
393
+	 *
394
+	 * @param int  $EVT_ID
395
+	 * @param bool $try_to_exclude_expired
396
+	 * @param bool $try_to_exclude_deleted
397
+	 * @return \EE_Datetime
398
+	 * @throws EE_Error
399
+	 */
400
+	public function get_primary_datetime_for_event(
401
+		$EVT_ID,
402
+		$try_to_exclude_expired = true,
403
+		$try_to_exclude_deleted = true
404
+	) {
405
+		if ($try_to_exclude_expired) {
406
+			$non_expired = $this->get_oldest_datetime_for_event($EVT_ID, false, false);
407
+			if ($non_expired) {
408
+				return $non_expired;
409
+			}
410
+		}
411
+		if ($try_to_exclude_deleted) {
412
+			$expired_even = $this->get_oldest_datetime_for_event($EVT_ID, true);
413
+			if ($expired_even) {
414
+				return $expired_even;
415
+			}
416
+		}
417
+		return $this->get_oldest_datetime_for_event($EVT_ID, true, true);
418
+	}
419
+
420
+
421
+	/**
422
+	 * Gets ALL the datetimes for an event (including trashed ones, for now), ordered
423
+	 * only by start date
424
+	 *
425
+	 * @param int     $EVT_ID
426
+	 * @param boolean $include_expired
427
+	 * @param boolean $include_deleted
428
+	 * @param int     $limit
429
+	 * @return EE_Datetime[]
430
+	 * @throws EE_Error
431
+	 */
432
+	public function get_datetimes_for_event_ordered_by_start_time(
433
+		$EVT_ID,
434
+		$include_expired = true,
435
+		$include_deleted = true,
436
+		$limit = null
437
+	) {
438
+		// sanitize EVT_ID
439
+		$EVT_ID         = absint($EVT_ID);
440
+		$old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object();
441
+		$this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db);
442
+		$query_params = array(array('Event.EVT_ID' => $EVT_ID), 'order_by' => array('DTT_EVT_start' => 'asc'));
443
+		if (! $include_expired) {
444
+			$query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true));
445
+		}
446
+		if ($include_deleted) {
447
+			$query_params[0]['DTT_deleted'] = array('IN', array(true, false));
448
+		}
449
+		if ($limit) {
450
+			$query_params['limit'] = $limit;
451
+		}
452
+		/** @var EE_Datetime[] $result */
453
+		$result = $this->get_all($query_params);
454
+		$this->assume_values_already_prepared_by_model_object($old_assumption);
455
+		return $result;
456
+	}
457
+
458
+
459
+	/**
460
+	 * Gets ALL the datetimes for an ticket (including trashed ones, for now), ordered
461
+	 * only by start date
462
+	 *
463
+	 * @param int     $TKT_ID
464
+	 * @param boolean $include_expired
465
+	 * @param boolean $include_deleted
466
+	 * @param int     $limit
467
+	 * @return EE_Datetime[]
468
+	 * @throws EE_Error
469
+	 */
470
+	public function get_datetimes_for_ticket_ordered_by_start_time(
471
+		$TKT_ID,
472
+		$include_expired = true,
473
+		$include_deleted = true,
474
+		$limit = null
475
+	) {
476
+		// sanitize TKT_ID
477
+		$TKT_ID         = absint($TKT_ID);
478
+		$old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object();
479
+		$this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db);
480
+		$query_params = array(array('Ticket.TKT_ID' => $TKT_ID), 'order_by' => array('DTT_EVT_start' => 'asc'));
481
+		if (! $include_expired) {
482
+			$query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true));
483
+		}
484
+		if ($include_deleted) {
485
+			$query_params[0]['DTT_deleted'] = array('IN', array(true, false));
486
+		}
487
+		if ($limit) {
488
+			$query_params['limit'] = $limit;
489
+		}
490
+		/** @var EE_Datetime[] $result */
491
+		$result = $this->get_all($query_params);
492
+		$this->assume_values_already_prepared_by_model_object($old_assumption);
493
+		return $result;
494
+	}
495
+
496
+
497
+	/**
498
+	 * Gets all the datetimes for a ticket (including trashed ones, for now), ordered by the DTT_order for the
499
+	 * datetimes.
500
+	 *
501
+	 * @param  int      $TKT_ID          ID of ticket to retrieve the datetimes for
502
+	 * @param  boolean  $include_expired whether to include expired datetimes or not
503
+	 * @param  boolean  $include_deleted whether to include trashed datetimes or not.
504
+	 * @param  int|null $limit           if null, no limit, if int then limit results by
505
+	 *                                   that number
506
+	 * @return EE_Datetime[]
507
+	 * @throws EE_Error
508
+	 */
509
+	public function get_datetimes_for_ticket_ordered_by_DTT_order(
510
+		$TKT_ID,
511
+		$include_expired = true,
512
+		$include_deleted = true,
513
+		$limit = null
514
+	) {
515
+		// sanitize id.
516
+		$TKT_ID         = absint($TKT_ID);
517
+		$old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object();
518
+		$this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db);
519
+		$where_params = array('Ticket.TKT_ID' => $TKT_ID);
520
+		$query_params = array($where_params, 'order_by' => array('DTT_order' => 'ASC'));
521
+		if (! $include_expired) {
522
+			$query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true));
523
+		}
524
+		if ($include_deleted) {
525
+			$query_params[0]['DTT_deleted'] = array('IN', array(true, false));
526
+		}
527
+		if ($limit) {
528
+			$query_params['limit'] = $limit;
529
+		}
530
+		/** @var EE_Datetime[] $result */
531
+		$result = $this->get_all($query_params);
532
+		$this->assume_values_already_prepared_by_model_object($old_assumption);
533
+		return $result;
534
+	}
535
+
536
+
537
+	/**
538
+	 * Gets the most important datetime for a particular event (ie, the primary event usually. But if for some WACK
539
+	 * reason it doesn't exist, we consider the earliest event the most important)
540
+	 *
541
+	 * @param int $EVT_ID
542
+	 * @return EE_Datetime
543
+	 * @throws EE_Error
544
+	 */
545
+	public function get_most_important_datetime_for_event($EVT_ID)
546
+	{
547
+		$results = $this->get_datetimes_for_event_ordered_by_importance($EVT_ID, 1);
548
+		if ($results) {
549
+			return array_shift($results);
550
+		}
551
+		return null;
552
+	}
553
+
554
+
555
+	/**
556
+	 * This returns a wpdb->results        Array of all DTT month and years matching the incoming query params and
557
+	 * grouped by month and year.
558
+	 *
559
+	 * @param  array  $where_params      @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions
560
+	 * @param  string $evt_active_status A string representing the evt active status to filter the months by.
561
+	 *                                   Can be:
562
+	 *                                   - '' = no filter
563
+	 *                                   - upcoming = Published events with at least one upcoming datetime.
564
+	 *                                   - expired = Events with all datetimes expired.
565
+	 *                                   - active = Events that are published and have at least one datetime that
566
+	 *                                   starts before now and ends after now.
567
+	 *                                   - inactive = Events that are either not published.
568
+	 * @return EE_Base_Class[]
569
+	 * @throws EE_Error
570
+	 * @throws InvalidArgumentException
571
+	 * @throws InvalidArgumentException
572
+	 */
573
+	public function get_dtt_months_and_years($where_params, $evt_active_status = '')
574
+	{
575
+		$current_time_for_DTT_EVT_start = $this->current_time_for_query('DTT_EVT_start');
576
+		$current_time_for_DTT_EVT_end   = $this->current_time_for_query('DTT_EVT_end');
577
+		switch ($evt_active_status) {
578
+			case 'upcoming':
579
+				$where_params['Event.status'] = 'publish';
580
+				// if there are already query_params matching DTT_EVT_start then we need to modify that to add them.
581
+				if (isset($where_params['DTT_EVT_start'])) {
582
+					$where_params['DTT_EVT_start*****'] = $where_params['DTT_EVT_start'];
583
+				}
584
+				$where_params['DTT_EVT_start'] = array('>', $current_time_for_DTT_EVT_start);
585
+				break;
586
+			case 'expired':
587
+				if (isset($where_params['Event.status'])) {
588
+					unset($where_params['Event.status']);
589
+				}
590
+				// get events to exclude
591
+				$exclude_query[0] = array_merge(
592
+					$where_params,
593
+					array('DTT_EVT_end' => array('>', $current_time_for_DTT_EVT_end))
594
+				);
595
+				// first get all events that have datetimes where its not expired.
596
+				$event_ids = $this->_get_all_wpdb_results(
597
+					$exclude_query,
598
+					OBJECT_K,
599
+					'Datetime.EVT_ID'
600
+				);
601
+				$event_ids = array_keys($event_ids);
602
+				if (isset($where_params['DTT_EVT_end'])) {
603
+					$where_params['DTT_EVT_end****'] = $where_params['DTT_EVT_end'];
604
+				}
605
+				$where_params['DTT_EVT_end']  = array('<', $current_time_for_DTT_EVT_end);
606
+				$where_params['Event.EVT_ID'] = array('NOT IN', $event_ids);
607
+				break;
608
+			case 'active':
609
+				$where_params['Event.status'] = 'publish';
610
+				if (isset($where_params['DTT_EVT_start'])) {
611
+					$where_params['Datetime.DTT_EVT_start******'] = $where_params['DTT_EVT_start'];
612
+				}
613
+				if (isset($where_params['Datetime.DTT_EVT_end'])) {
614
+					$where_params['Datetime.DTT_EVT_end*****'] = $where_params['DTT_EVT_end'];
615
+				}
616
+				$where_params['DTT_EVT_start'] = array('<', $current_time_for_DTT_EVT_start);
617
+				$where_params['DTT_EVT_end']   = array('>', $current_time_for_DTT_EVT_end);
618
+				break;
619
+			case 'inactive':
620
+				if (isset($where_params['Event.status'])) {
621
+					unset($where_params['Event.status']);
622
+				}
623
+				if (isset($where_params['OR'])) {
624
+					$where_params['AND']['OR'] = $where_params['OR'];
625
+				}
626
+				if (isset($where_params['DTT_EVT_end'])) {
627
+					$where_params['AND']['DTT_EVT_end****'] = $where_params['DTT_EVT_end'];
628
+					unset($where_params['DTT_EVT_end']);
629
+				}
630
+				if (isset($where_params['DTT_EVT_start'])) {
631
+					$where_params['AND']['DTT_EVT_start'] = $where_params['DTT_EVT_start'];
632
+					unset($where_params['DTT_EVT_start']);
633
+				}
634
+				$where_params['AND']['Event.status'] = array('!=', 'publish');
635
+				break;
636
+		}
637
+		$query_params[0]          = $where_params;
638
+		$query_params['group_by'] = array('dtt_year', 'dtt_month');
639
+		$query_params['order_by'] = array('DTT_EVT_start' => 'DESC');
640
+		$query_interval           = EEH_DTT_Helper::get_sql_query_interval_for_offset(
641
+			$this->get_timezone(),
642
+			'DTT_EVT_start'
643
+		);
644
+		$columns_to_select        = array(
645
+			'dtt_year'      => array('YEAR(' . $query_interval . ')', '%s'),
646
+			'dtt_month'     => array('MONTHNAME(' . $query_interval . ')', '%s'),
647
+			'dtt_month_num' => array('MONTH(' . $query_interval . ')', '%s'),
648
+		);
649
+		return $this->_get_all_wpdb_results($query_params, OBJECT, $columns_to_select);
650
+	}
651
+
652
+
653
+	/**
654
+	 * Updates the DTT_sold attribute on each datetime (based on the registrations
655
+	 * for the tickets for each datetime)
656
+	 *
657
+	 * @param EE_Base_Class[]|EE_Datetime[] $datetimes
658
+	 * @throws EE_Error
659
+	 */
660
+	public function update_sold($datetimes)
661
+	{
662
+		EE_Error::doing_it_wrong(
663
+			__FUNCTION__,
664
+			esc_html__(
665
+				'Please use \EEM_Ticket::update_tickets_sold() instead which will in turn correctly update both the Ticket AND Datetime counts.',
666
+				'event_espresso'
667
+			),
668
+			'4.9.32.rc.005'
669
+		);
670
+		foreach ($datetimes as $datetime) {
671
+			$datetime->update_sold();
672
+		}
673
+	}
674
+
675
+
676
+	/**
677
+	 *    Gets the total number of tickets available at a particular datetime
678
+	 *    (does NOT take into account the datetime's spaces available)
679
+	 *
680
+	 * @param int   $DTT_ID
681
+	 * @param array $query_params
682
+	 * @return int of tickets available. If sold out, return less than 1. If infinite, returns EE_INF,  IF there are NO
683
+	 *             tickets attached to datetime then FALSE is returned.
684
+	 */
685
+	public function sum_tickets_currently_available_at_datetime($DTT_ID, array $query_params = array())
686
+	{
687
+		$datetime = $this->get_one_by_ID($DTT_ID);
688
+		if ($datetime instanceof EE_Datetime) {
689
+			return $datetime->tickets_remaining($query_params);
690
+		}
691
+		return 0;
692
+	}
693
+
694
+
695
+	/**
696
+	 * This returns an array of counts of datetimes in the database for each Datetime status that can be queried.
697
+	 *
698
+	 * @param  array $stati_to_include If included you can restrict the statuses we return counts for by including the
699
+	 *                                 stati you want counts for as values in the array.  An empty array returns counts
700
+	 *                                 for all valid stati.
701
+	 * @param  array $query_params     If included can be used to refine the conditions for returning the count (i.e.
702
+	 *                                 only for Datetimes connected to a specific event, or specific ticket.
703
+	 * @return array  The value returned is an array indexed by Datetime Status and the values are the counts.  The
704
+	 * @throws EE_Error
705
+	 *                                 stati used as index keys are: EE_Datetime::active EE_Datetime::upcoming
706
+	 *                                 EE_Datetime::expired
707
+	 */
708
+	public function get_datetime_counts_by_status(array $stati_to_include = array(), array $query_params = array())
709
+	{
710
+		// only accept where conditions for this query.
711
+		$_where            = isset($query_params[0]) ? $query_params[0] : array();
712
+		$status_query_args = array(
713
+			EE_Datetime::active   => array_merge(
714
+				$_where,
715
+				array('DTT_EVT_start' => array('<', time()), 'DTT_EVT_end' => array('>', time()))
716
+			),
717
+			EE_Datetime::upcoming => array_merge(
718
+				$_where,
719
+				array('DTT_EVT_start' => array('>', time()))
720
+			),
721
+			EE_Datetime::expired  => array_merge(
722
+				$_where,
723
+				array('DTT_EVT_end' => array('<', time()))
724
+			),
725
+		);
726
+		if (! empty($stati_to_include)) {
727
+			foreach (array_keys($status_query_args) as $status) {
728
+				if (! in_array($status, $stati_to_include, true)) {
729
+					unset($status_query_args[ $status ]);
730
+				}
731
+			}
732
+		}
733
+		// loop through and query counts for each stati.
734
+		$status_query_results = array();
735
+		foreach ($status_query_args as $status => $status_where_conditions) {
736
+			$status_query_results[ $status ] = EEM_Datetime::count(
737
+				array($status_where_conditions),
738
+				'DTT_ID',
739
+				true
740
+			);
741
+		}
742
+		return $status_query_results;
743
+	}
744
+
745
+
746
+	/**
747
+	 * Returns the specific count for a given Datetime status matching any given query_params.
748
+	 *
749
+	 * @param string $status Valid string representation for Datetime status requested. (Defaults to Active).
750
+	 * @param array  $query_params
751
+	 * @return int
752
+	 * @throws EE_Error
753
+	 */
754
+	public function get_datetime_count_for_status($status = EE_Datetime::active, array $query_params = array())
755
+	{
756
+		$count = $this->get_datetime_counts_by_status(array($status), $query_params);
757
+		return ! empty($count[ $status ]) ? $count[ $status ] : 0;
758
+	}
759 759
 }
Please login to merge, or discard this patch.
Spacing   +21 added lines, -21 removed lines patch added patch discarded remove patch
@@ -121,7 +121,7 @@  discard block
 block discarded – undo
121 121
                 ),
122 122
             ),
123 123
         );
124
-        $this->_model_relations        = array(
124
+        $this->_model_relations = array(
125 125
             'Ticket'  => new EE_HABTM_Relation('Datetime_Ticket'),
126 126
             'Event'   => new EE_Belongs_To_Relation(),
127 127
             'Checkin' => new EE_Has_Many_Relation(),
@@ -131,16 +131,16 @@  discard block
 block discarded – undo
131 131
         $this->model_chain_to_password = $path_to_event_model;
132 132
         $this->_model_chain_to_wp_user = $path_to_event_model;
133 133
         // this model is generally available for reading
134
-        $this->_cap_restriction_generators[ EEM_Base::caps_read ]       = new EE_Restriction_Generator_Event_Related_Public(
134
+        $this->_cap_restriction_generators[EEM_Base::caps_read]       = new EE_Restriction_Generator_Event_Related_Public(
135 135
             $path_to_event_model
136 136
         );
137
-        $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Event_Related_Protected(
137
+        $this->_cap_restriction_generators[EEM_Base::caps_read_admin] = new EE_Restriction_Generator_Event_Related_Protected(
138 138
             $path_to_event_model
139 139
         );
140
-        $this->_cap_restriction_generators[ EEM_Base::caps_edit ]       = new EE_Restriction_Generator_Event_Related_Protected(
140
+        $this->_cap_restriction_generators[EEM_Base::caps_edit]       = new EE_Restriction_Generator_Event_Related_Protected(
141 141
             $path_to_event_model
142 142
         );
143
-        $this->_cap_restriction_generators[ EEM_Base::caps_delete ]     = new EE_Restriction_Generator_Event_Related_Protected(
143
+        $this->_cap_restriction_generators[EEM_Base::caps_delete]     = new EE_Restriction_Generator_Event_Related_Protected(
144 144
             $path_to_event_model,
145 145
             EEM_Base::caps_edit
146 146
         );
@@ -246,10 +246,10 @@  discard block
 block discarded – undo
246 246
      */
247 247
     private function validateStartAndEndTimeForBlankDate($start_time, $end_time)
248 248
     {
249
-        if (! is_array($start_time)) {
249
+        if ( ! is_array($start_time)) {
250 250
             throw new InvalidDataTypeException('start_time', $start_time, 'array');
251 251
         }
252
-        if (! is_array($end_time)) {
252
+        if ( ! is_array($end_time)) {
253 253
             throw new InvalidDataTypeException('end_time', $end_time, 'array');
254 254
         }
255 255
         if (count($start_time) !== 2) {
@@ -283,7 +283,7 @@  discard block
 block discarded – undo
283 283
      */
284 284
     public function get_all_event_dates($EVT_ID = 0)
285 285
     {
286
-        if (! $EVT_ID) { // on add_new_event event_id gets set to 0
286
+        if ( ! $EVT_ID) { // on add_new_event event_id gets set to 0
287 287
             return $this->create_new_blank_datetime();
288 288
         }
289 289
         $results = $this->get_datetimes_for_event_ordered_by_DTT_order($EVT_ID);
@@ -329,7 +329,7 @@  discard block
 block discarded – undo
329 329
                 'order_by'                 => array('DTT_order' => 'ASC'),
330 330
                 'default_where_conditions' => 'none',
331 331
             );
332
-        if (! $include_expired) {
332
+        if ( ! $include_expired) {
333 333
             $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true));
334 334
         }
335 335
         if ($include_deleted) {
@@ -440,7 +440,7 @@  discard block
 block discarded – undo
440 440
         $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object();
441 441
         $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db);
442 442
         $query_params = array(array('Event.EVT_ID' => $EVT_ID), 'order_by' => array('DTT_EVT_start' => 'asc'));
443
-        if (! $include_expired) {
443
+        if ( ! $include_expired) {
444 444
             $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true));
445 445
         }
446 446
         if ($include_deleted) {
@@ -478,7 +478,7 @@  discard block
 block discarded – undo
478 478
         $old_assumption = $this->get_assumption_concerning_values_already_prepared_by_model_object();
479 479
         $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db);
480 480
         $query_params = array(array('Ticket.TKT_ID' => $TKT_ID), 'order_by' => array('DTT_EVT_start' => 'asc'));
481
-        if (! $include_expired) {
481
+        if ( ! $include_expired) {
482 482
             $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true));
483 483
         }
484 484
         if ($include_deleted) {
@@ -518,7 +518,7 @@  discard block
 block discarded – undo
518 518
         $this->assume_values_already_prepared_by_model_object(EEM_Base::prepared_for_use_in_db);
519 519
         $where_params = array('Ticket.TKT_ID' => $TKT_ID);
520 520
         $query_params = array($where_params, 'order_by' => array('DTT_order' => 'ASC'));
521
-        if (! $include_expired) {
521
+        if ( ! $include_expired) {
522 522
             $query_params[0]['DTT_EVT_end'] = array('>=', current_time('mysql', true));
523 523
         }
524 524
         if ($include_deleted) {
@@ -641,10 +641,10 @@  discard block
 block discarded – undo
641 641
             $this->get_timezone(),
642 642
             'DTT_EVT_start'
643 643
         );
644
-        $columns_to_select        = array(
645
-            'dtt_year'      => array('YEAR(' . $query_interval . ')', '%s'),
646
-            'dtt_month'     => array('MONTHNAME(' . $query_interval . ')', '%s'),
647
-            'dtt_month_num' => array('MONTH(' . $query_interval . ')', '%s'),
644
+        $columns_to_select = array(
645
+            'dtt_year'      => array('YEAR('.$query_interval.')', '%s'),
646
+            'dtt_month'     => array('MONTHNAME('.$query_interval.')', '%s'),
647
+            'dtt_month_num' => array('MONTH('.$query_interval.')', '%s'),
648 648
         );
649 649
         return $this->_get_all_wpdb_results($query_params, OBJECT, $columns_to_select);
650 650
     }
@@ -723,17 +723,17 @@  discard block
 block discarded – undo
723 723
                 array('DTT_EVT_end' => array('<', time()))
724 724
             ),
725 725
         );
726
-        if (! empty($stati_to_include)) {
726
+        if ( ! empty($stati_to_include)) {
727 727
             foreach (array_keys($status_query_args) as $status) {
728
-                if (! in_array($status, $stati_to_include, true)) {
729
-                    unset($status_query_args[ $status ]);
728
+                if ( ! in_array($status, $stati_to_include, true)) {
729
+                    unset($status_query_args[$status]);
730 730
                 }
731 731
             }
732 732
         }
733 733
         // loop through and query counts for each stati.
734 734
         $status_query_results = array();
735 735
         foreach ($status_query_args as $status => $status_where_conditions) {
736
-            $status_query_results[ $status ] = EEM_Datetime::count(
736
+            $status_query_results[$status] = EEM_Datetime::count(
737 737
                 array($status_where_conditions),
738 738
                 'DTT_ID',
739 739
                 true
@@ -754,6 +754,6 @@  discard block
 block discarded – undo
754 754
     public function get_datetime_count_for_status($status = EE_Datetime::active, array $query_params = array())
755 755
     {
756 756
         $count = $this->get_datetime_counts_by_status(array($status), $query_params);
757
-        return ! empty($count[ $status ]) ? $count[ $status ] : 0;
757
+        return ! empty($count[$status]) ? $count[$status] : 0;
758 758
     }
759 759
 }
Please login to merge, or discard this patch.
core/data_migration_scripts/EE_Data_Migration_Script_Stage_Table.core.php 2 patches
Indentation   +77 added lines, -77 removed lines patch added patch discarded remove patch
@@ -16,89 +16,89 @@
 block discarded – undo
16 16
 abstract class EE_Data_Migration_Script_Stage_Table extends EE_Data_Migration_Script_Stage
17 17
 {
18 18
 
19
-    protected $_old_table;
19
+	protected $_old_table;
20 20
 
21
-    /**
22
-     * @var string The columns to select. May be overridden for children.
23
-     */
24
-    protected $select_expression = '*';
21
+	/**
22
+	 * @var string The columns to select. May be overridden for children.
23
+	 */
24
+	protected $select_expression = '*';
25 25
 
26
-    /**
27
-     * Set in the constructor to add this sql to both the counting query in
28
-     * EE_Data_Migration_Script_Stage_Table::_count_records_to_migrate() and
29
-     * EE_Data_Migration_Script_Stage_Table::_get_rows().
30
-     * Eg "where column_name like '%some_value%'"
31
-     *
32
-     * @var string
33
-     */
34
-    protected $_extra_where_sql;
26
+	/**
27
+	 * Set in the constructor to add this sql to both the counting query in
28
+	 * EE_Data_Migration_Script_Stage_Table::_count_records_to_migrate() and
29
+	 * EE_Data_Migration_Script_Stage_Table::_get_rows().
30
+	 * Eg "where column_name like '%some_value%'"
31
+	 *
32
+	 * @var string
33
+	 */
34
+	protected $_extra_where_sql;
35 35
 
36 36
 
37
-    /**
38
-     * IMPORTANT: if an error is encountered, or everything is finished, this stage should update its status property
39
-     * accordingly. Note: it should not alter the count of items migrated. That is done in the public function that
40
-     * calls this. IMPORTANT: The count of items migrated should ONLY be less than $num_items_to_migrate when it's the
41
-     * last migration step, otherwise it should always return $num_items_to_migrate. (Eg, if we're migrating attendees
42
-     * rows from the database, and $num_items_to_migrate is set to 50, then we SHOULD actually migrate 50 rows,but at
43
-     * very least we MUST report/return 50 items migrated)
44
-     *
45
-     * @param int $num_items
46
-     * @return int number of items ACTUALLY migrated
47
-     */
48
-    public function _migration_step($num_items = 50)
49
-    {
50
-        $rows = $this->_get_rows($num_items);
51
-        $items_actually_migrated = 0;
52
-        foreach ($rows as $old_row) {
53
-            $this->_migrate_old_row($old_row);
54
-            $items_actually_migrated++;
55
-        }
56
-        if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
57
-            $this->set_completed();
58
-        }
59
-        return $items_actually_migrated;
60
-    }
37
+	/**
38
+	 * IMPORTANT: if an error is encountered, or everything is finished, this stage should update its status property
39
+	 * accordingly. Note: it should not alter the count of items migrated. That is done in the public function that
40
+	 * calls this. IMPORTANT: The count of items migrated should ONLY be less than $num_items_to_migrate when it's the
41
+	 * last migration step, otherwise it should always return $num_items_to_migrate. (Eg, if we're migrating attendees
42
+	 * rows from the database, and $num_items_to_migrate is set to 50, then we SHOULD actually migrate 50 rows,but at
43
+	 * very least we MUST report/return 50 items migrated)
44
+	 *
45
+	 * @param int $num_items
46
+	 * @return int number of items ACTUALLY migrated
47
+	 */
48
+	public function _migration_step($num_items = 50)
49
+	{
50
+		$rows = $this->_get_rows($num_items);
51
+		$items_actually_migrated = 0;
52
+		foreach ($rows as $old_row) {
53
+			$this->_migrate_old_row($old_row);
54
+			$items_actually_migrated++;
55
+		}
56
+		if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
57
+			$this->set_completed();
58
+		}
59
+		return $items_actually_migrated;
60
+	}
61 61
 
62
-    /**
63
-     * Gets the rows for each migration stage from the old table
64
-     *
65
-     * @global wpdb $wpdb
66
-     * @param int   $limit
67
-     * @return array of arrays like $wpdb->get_results($sql, ARRAY_A)
68
-     */
69
-    protected function _get_rows($limit)
70
-    {
71
-        global $wpdb;
72
-        $start_at_record = $this->count_records_migrated();
73
-        $query = "SELECT {$this->select_expression} FROM {$this->_old_table} {$this->_extra_where_sql} " . $wpdb->prepare(
74
-            "LIMIT %d, %d",
75
-            $start_at_record,
76
-            $limit
77
-        );
78
-        return $wpdb->get_results($query, ARRAY_A);
79
-    }
62
+	/**
63
+	 * Gets the rows for each migration stage from the old table
64
+	 *
65
+	 * @global wpdb $wpdb
66
+	 * @param int   $limit
67
+	 * @return array of arrays like $wpdb->get_results($sql, ARRAY_A)
68
+	 */
69
+	protected function _get_rows($limit)
70
+	{
71
+		global $wpdb;
72
+		$start_at_record = $this->count_records_migrated();
73
+		$query = "SELECT {$this->select_expression} FROM {$this->_old_table} {$this->_extra_where_sql} " . $wpdb->prepare(
74
+			"LIMIT %d, %d",
75
+			$start_at_record,
76
+			$limit
77
+		);
78
+		return $wpdb->get_results($query, ARRAY_A);
79
+	}
80 80
 
81 81
 
82
-    /**
83
-     * Counts the records to migrate; the public version may cache it
84
-     *
85
-     * @return int
86
-     */
87
-    public function _count_records_to_migrate()
88
-    {
89
-        global $wpdb;
90
-        $query = "SELECT COUNT(*) FROM {$this->_old_table} {$this->_extra_where_sql}";
91
-        $count = $wpdb->get_var($query);
92
-        return $count;
93
-    }
82
+	/**
83
+	 * Counts the records to migrate; the public version may cache it
84
+	 *
85
+	 * @return int
86
+	 */
87
+	public function _count_records_to_migrate()
88
+	{
89
+		global $wpdb;
90
+		$query = "SELECT COUNT(*) FROM {$this->_old_table} {$this->_extra_where_sql}";
91
+		$count = $wpdb->get_var($query);
92
+		return $count;
93
+	}
94 94
 
95
-    /**
96
-     * takes care of migrating this particular row from the OLD table to whatever its
97
-     * representation is in the new database. If there are errors, use $this->add_error to log them. If there is a
98
-     * fatal error which prevents all future migrations, throw an exception describing it
99
-     *
100
-     * @param array $old_row an associative array where keys are column names and values are their values.
101
-     * @return null
102
-     */
103
-    abstract protected function _migrate_old_row($old_row);
95
+	/**
96
+	 * takes care of migrating this particular row from the OLD table to whatever its
97
+	 * representation is in the new database. If there are errors, use $this->add_error to log them. If there is a
98
+	 * fatal error which prevents all future migrations, throw an exception describing it
99
+	 *
100
+	 * @param array $old_row an associative array where keys are column names and values are their values.
101
+	 * @return null
102
+	 */
103
+	abstract protected function _migrate_old_row($old_row);
104 104
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -70,7 +70,7 @@
 block discarded – undo
70 70
     {
71 71
         global $wpdb;
72 72
         $start_at_record = $this->count_records_migrated();
73
-        $query = "SELECT {$this->select_expression} FROM {$this->_old_table} {$this->_extra_where_sql} " . $wpdb->prepare(
73
+        $query = "SELECT {$this->select_expression} FROM {$this->_old_table} {$this->_extra_where_sql} ".$wpdb->prepare(
74 74
             "LIMIT %d, %d",
75 75
             $start_at_record,
76 76
             $limit
Please login to merge, or discard this patch.
core/helpers/EEH_Line_Item.helper.php 2 patches
Indentation   +2041 added lines, -2041 removed lines patch added patch discarded remove patch
@@ -21,2045 +21,2045 @@
 block discarded – undo
21 21
 class EEH_Line_Item
22 22
 {
23 23
 
24
-    /**
25
-     * Adds a simple item (unrelated to any other model object) to the provided PARENT line item.
26
-     * Does NOT automatically re-calculate the line item totals or update the related transaction.
27
-     * You should call recalculate_total_including_taxes() on the grant total line item after this
28
-     * to update the subtotals, and EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
29
-     * to keep the registration final prices in-sync with the transaction's total.
30
-     *
31
-     * @param EE_Line_Item $parent_line_item
32
-     * @param string       $name
33
-     * @param float        $unit_price
34
-     * @param string       $description
35
-     * @param int          $quantity
36
-     * @param boolean      $taxable
37
-     * @param boolean      $code if set to a value, ensures there is only one line item with that code
38
-     * @return boolean success
39
-     * @throws EE_Error
40
-     * @throws InvalidArgumentException
41
-     * @throws InvalidDataTypeException
42
-     * @throws InvalidInterfaceException
43
-     * @throws ReflectionException
44
-     */
45
-    public static function add_unrelated_item(
46
-        EE_Line_Item $parent_line_item,
47
-        $name,
48
-        $unit_price,
49
-        $description = '',
50
-        $quantity = 1,
51
-        $taxable = false,
52
-        $code = null
53
-    ) {
54
-        $items_subtotal = self::get_pre_tax_subtotal($parent_line_item);
55
-        $line_item = EE_Line_Item::new_instance(array(
56
-            'LIN_name'       => $name,
57
-            'LIN_desc'       => $description,
58
-            'LIN_unit_price' => $unit_price,
59
-            'LIN_quantity'   => $quantity,
60
-            'LIN_percent'    => null,
61
-            'LIN_is_taxable' => $taxable,
62
-            'LIN_order'      => $items_subtotal instanceof EE_Line_Item ? count($items_subtotal->children()) : 0,
63
-            'LIN_total'      => (float) $unit_price * (int) $quantity,
64
-            'LIN_type'       => EEM_Line_Item::type_line_item,
65
-            'LIN_code'       => $code,
66
-        ));
67
-        $line_item = apply_filters(
68
-            'FHEE__EEH_Line_Item__add_unrelated_item__line_item',
69
-            $line_item,
70
-            $parent_line_item
71
-        );
72
-        return self::add_item($parent_line_item, $line_item);
73
-    }
74
-
75
-
76
-    /**
77
-     * Adds a simple item ( unrelated to any other model object) to the total line item,
78
-     * in the correct spot in the line item tree. Does not automatically
79
-     * re-calculate the line item totals, nor update the related transaction, nor upgrade the transaction's
80
-     * registrations' final prices (which should probably change because of this).
81
-     * You should call recalculate_total_including_taxes() on the grand total line item, then
82
-     * update the transaction's total, and EE_Registration_Processor::update_registration_final_prices()
83
-     * after using this, to keep the registration final prices in-sync with the transaction's total.
84
-     *
85
-     * @param EE_Line_Item $parent_line_item
86
-     * @param string       $name
87
-     * @param float        $percentage_amount
88
-     * @param string       $description
89
-     * @param boolean      $taxable
90
-     * @return boolean success
91
-     * @throws EE_Error
92
-     */
93
-    public static function add_percentage_based_item(
94
-        EE_Line_Item $parent_line_item,
95
-        $name,
96
-        $percentage_amount,
97
-        $description = '',
98
-        $taxable = false
99
-    ) {
100
-        $line_item = EE_Line_Item::new_instance(array(
101
-            'LIN_name'       => $name,
102
-            'LIN_desc'       => $description,
103
-            'LIN_unit_price' => 0,
104
-            'LIN_percent'    => $percentage_amount,
105
-            'LIN_quantity'   => 1,
106
-            'LIN_is_taxable' => $taxable,
107
-            'LIN_total'      => (float) ($percentage_amount * ($parent_line_item->total() / 100)),
108
-            'LIN_type'       => EEM_Line_Item::type_line_item,
109
-            'LIN_parent'     => $parent_line_item->ID(),
110
-        ));
111
-        $line_item = apply_filters(
112
-            'FHEE__EEH_Line_Item__add_percentage_based_item__line_item',
113
-            $line_item
114
-        );
115
-        return $parent_line_item->add_child_line_item($line_item, false);
116
-    }
117
-
118
-
119
-    /**
120
-     * Returns the new line item created by adding a purchase of the ticket
121
-     * ensures that ticket line item is saved, and that cart total has been recalculated.
122
-     * If this ticket has already been purchased, just increments its count.
123
-     * Automatically re-calculates the line item totals and updates the related transaction. But
124
-     * DOES NOT automatically upgrade the transaction's registrations' final prices (which
125
-     * should probably change because of this).
126
-     * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
127
-     * after using this, to keep the registration final prices in-sync with the transaction's total.
128
-     *
129
-     * @param EE_Line_Item $total_line_item grand total line item of type EEM_Line_Item::type_total
130
-     * @param EE_Ticket    $ticket
131
-     * @param int          $qty
132
-     * @return EE_Line_Item
133
-     * @throws EE_Error
134
-     * @throws InvalidArgumentException
135
-     * @throws InvalidDataTypeException
136
-     * @throws InvalidInterfaceException
137
-     * @throws ReflectionException
138
-     */
139
-    public static function add_ticket_purchase(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1)
140
-    {
141
-        if (! $total_line_item instanceof EE_Line_Item || ! $total_line_item->is_total()) {
142
-            throw new EE_Error(
143
-                sprintf(
144
-                    esc_html__(
145
-                        'A valid line item total is required in order to add tickets. A line item of type "%s" was passed.',
146
-                        'event_espresso'
147
-                    ),
148
-                    $ticket->ID(),
149
-                    $total_line_item->ID()
150
-                )
151
-            );
152
-        }
153
-        // either increment the qty for an existing ticket
154
-        $line_item = self::increment_ticket_qty_if_already_in_cart($total_line_item, $ticket, $qty);
155
-        // or add a new one
156
-        if (! $line_item instanceof EE_Line_Item) {
157
-            $line_item = self::create_ticket_line_item($total_line_item, $ticket, $qty);
158
-        }
159
-        $total_line_item->recalculate_total_including_taxes();
160
-        return $line_item;
161
-    }
162
-
163
-
164
-    /**
165
-     * Returns the new line item created by adding a purchase of the ticket
166
-     *
167
-     * @param EE_Line_Item $total_line_item
168
-     * @param EE_Ticket    $ticket
169
-     * @param int          $qty
170
-     * @return EE_Line_Item
171
-     * @throws EE_Error
172
-     * @throws InvalidArgumentException
173
-     * @throws InvalidDataTypeException
174
-     * @throws InvalidInterfaceException
175
-     * @throws ReflectionException
176
-     */
177
-    public static function increment_ticket_qty_if_already_in_cart(
178
-        EE_Line_Item $total_line_item,
179
-        EE_Ticket $ticket,
180
-        $qty = 1
181
-    ) {
182
-        $line_item = null;
183
-        if ($total_line_item instanceof EE_Line_Item && $total_line_item->is_total()) {
184
-            $ticket_line_items = EEH_Line_Item::get_ticket_line_items($total_line_item);
185
-            foreach ((array) $ticket_line_items as $ticket_line_item) {
186
-                if ($ticket_line_item instanceof EE_Line_Item
187
-                    && (int) $ticket_line_item->OBJ_ID() === (int) $ticket->ID()
188
-                ) {
189
-                    $line_item = $ticket_line_item;
190
-                    break;
191
-                }
192
-            }
193
-        }
194
-        if ($line_item instanceof EE_Line_Item) {
195
-            EEH_Line_Item::increment_quantity($line_item, $qty);
196
-            return $line_item;
197
-        }
198
-        return null;
199
-    }
200
-
201
-
202
-    /**
203
-     * Increments the line item and all its children's quantity by $qty (but percent line items are unaffected).
204
-     * Does NOT save or recalculate other line items totals
205
-     *
206
-     * @param EE_Line_Item $line_item
207
-     * @param int          $qty
208
-     * @return void
209
-     * @throws EE_Error
210
-     * @throws InvalidArgumentException
211
-     * @throws InvalidDataTypeException
212
-     * @throws InvalidInterfaceException
213
-     * @throws ReflectionException
214
-     */
215
-    public static function increment_quantity(EE_Line_Item $line_item, $qty = 1)
216
-    {
217
-        if (! $line_item->is_percent()) {
218
-            $qty += $line_item->quantity();
219
-            $line_item->set_quantity($qty);
220
-            $line_item->set_total($line_item->unit_price() * $qty);
221
-            $line_item->save();
222
-        }
223
-        foreach ($line_item->children() as $child) {
224
-            if ($child->is_sub_line_item()) {
225
-                EEH_Line_Item::update_quantity($child, $qty);
226
-            }
227
-        }
228
-    }
229
-
230
-
231
-    /**
232
-     * Decrements the line item and all its children's quantity by $qty (but percent line items are unaffected).
233
-     * Does NOT save or recalculate other line items totals
234
-     *
235
-     * @param EE_Line_Item $line_item
236
-     * @param int          $qty
237
-     * @return void
238
-     * @throws EE_Error
239
-     * @throws InvalidArgumentException
240
-     * @throws InvalidDataTypeException
241
-     * @throws InvalidInterfaceException
242
-     * @throws ReflectionException
243
-     */
244
-    public static function decrement_quantity(EE_Line_Item $line_item, $qty = 1)
245
-    {
246
-        if (! $line_item->is_percent()) {
247
-            $qty = $line_item->quantity() - $qty;
248
-            $qty = max($qty, 0);
249
-            $line_item->set_quantity($qty);
250
-            $line_item->set_total($line_item->unit_price() * $qty);
251
-            $line_item->save();
252
-        }
253
-        foreach ($line_item->children() as $child) {
254
-            if ($child->is_sub_line_item()) {
255
-                EEH_Line_Item::update_quantity($child, $qty);
256
-            }
257
-        }
258
-    }
259
-
260
-
261
-    /**
262
-     * Updates the line item and its children's quantities to the specified number.
263
-     * Does NOT save them or recalculate totals.
264
-     *
265
-     * @param EE_Line_Item $line_item
266
-     * @param int          $new_quantity
267
-     * @throws EE_Error
268
-     * @throws InvalidArgumentException
269
-     * @throws InvalidDataTypeException
270
-     * @throws InvalidInterfaceException
271
-     * @throws ReflectionException
272
-     */
273
-    public static function update_quantity(EE_Line_Item $line_item, $new_quantity)
274
-    {
275
-        if (! $line_item->is_percent()) {
276
-            $line_item->set_quantity($new_quantity);
277
-            $line_item->set_total($line_item->unit_price() * $new_quantity);
278
-            $line_item->save();
279
-        }
280
-        foreach ($line_item->children() as $child) {
281
-            if ($child->is_sub_line_item()) {
282
-                EEH_Line_Item::update_quantity($child, $new_quantity);
283
-            }
284
-        }
285
-    }
286
-
287
-
288
-    /**
289
-     * Returns the new line item created by adding a purchase of the ticket
290
-     *
291
-     * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
292
-     * @param EE_Ticket    $ticket
293
-     * @param int          $qty
294
-     * @return EE_Line_Item
295
-     * @throws EE_Error
296
-     * @throws InvalidArgumentException
297
-     * @throws InvalidDataTypeException
298
-     * @throws InvalidInterfaceException
299
-     * @throws ReflectionException
300
-     */
301
-    public static function create_ticket_line_item(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1)
302
-    {
303
-        $datetimes = $ticket->datetimes();
304
-        $first_datetime = reset($datetimes);
305
-        $first_datetime_name = esc_html__('Event', 'event_espresso');
306
-        if ($first_datetime instanceof EE_Datetime && $first_datetime->event() instanceof EE_Event) {
307
-            $first_datetime_name = $first_datetime->event()->name();
308
-        }
309
-        $event = sprintf(_x('(For %1$s)', '(For Event Name)', 'event_espresso'), $first_datetime_name);
310
-        // get event subtotal line
311
-        $events_sub_total = self::get_event_line_item_for_ticket($total_line_item, $ticket);
312
-        // add $ticket to cart
313
-        $line_item = EE_Line_Item::new_instance(array(
314
-            'LIN_name'       => $ticket->name(),
315
-            'LIN_desc'       => $ticket->description() !== '' ? $ticket->description() . ' ' . $event : $event,
316
-            'LIN_unit_price' => $ticket->price(),
317
-            'LIN_quantity'   => $qty,
318
-            'LIN_is_taxable' => $ticket->taxable(),
319
-            'LIN_order'      => count($events_sub_total->children()),
320
-            'LIN_total'      => $ticket->price() * $qty,
321
-            'LIN_type'       => EEM_Line_Item::type_line_item,
322
-            'OBJ_ID'         => $ticket->ID(),
323
-            'OBJ_type'       => EEM_Line_Item::OBJ_TYPE_TICKET,
324
-        ));
325
-        $line_item = apply_filters(
326
-            'FHEE__EEH_Line_Item__create_ticket_line_item__line_item',
327
-            $line_item
328
-        );
329
-        $events_sub_total->add_child_line_item($line_item);
330
-        // now add the sub-line items
331
-        $running_total_for_ticket = 0;
332
-        foreach ($ticket->prices(array('order_by' => array('PRC_order' => 'ASC'))) as $price) {
333
-            $sign = $price->is_discount() ? -1 : 1;
334
-            $price_total = $price->is_percent()
335
-                ? $running_total_for_ticket * $price->amount() / 100
336
-                : $price->amount() * $qty;
337
-            $sub_line_item = EE_Line_Item::new_instance(array(
338
-                'LIN_name'       => $price->name(),
339
-                'LIN_desc'       => $price->desc(),
340
-                'LIN_quantity'   => $price->is_percent() ? null : $qty,
341
-                'LIN_is_taxable' => false,
342
-                'LIN_order'      => $price->order(),
343
-                'LIN_total'      => $sign * $price_total,
344
-                'LIN_type'       => EEM_Line_Item::type_sub_line_item,
345
-                'OBJ_ID'         => $price->ID(),
346
-                'OBJ_type'       => EEM_Line_Item::OBJ_TYPE_PRICE,
347
-            ));
348
-            $sub_line_item = apply_filters(
349
-                'FHEE__EEH_Line_Item__create_ticket_line_item__sub_line_item',
350
-                $sub_line_item
351
-            );
352
-            if ($price->is_percent()) {
353
-                $sub_line_item->set_percent($sign * $price->amount());
354
-            } else {
355
-                $sub_line_item->set_unit_price($sign * $price->amount());
356
-            }
357
-            $running_total_for_ticket += $price_total;
358
-            $line_item->add_child_line_item($sub_line_item);
359
-        }
360
-        return $line_item;
361
-    }
362
-
363
-
364
-    /**
365
-     * Adds the specified item under the pre-tax-sub-total line item. Automatically
366
-     * re-calculates the line item totals and updates the related transaction. But
367
-     * DOES NOT automatically upgrade the transaction's registrations' final prices (which
368
-     * should probably change because of this).
369
-     * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
370
-     * after using this, to keep the registration final prices in-sync with the transaction's total.
371
-     *
372
-     * @param EE_Line_Item $total_line_item
373
-     * @param EE_Line_Item $item to be added
374
-     * @return boolean
375
-     * @throws EE_Error
376
-     * @throws InvalidArgumentException
377
-     * @throws InvalidDataTypeException
378
-     * @throws InvalidInterfaceException
379
-     * @throws ReflectionException
380
-     */
381
-    public static function add_item(EE_Line_Item $total_line_item, EE_Line_Item $item)
382
-    {
383
-        $pre_tax_subtotal = self::get_pre_tax_subtotal($total_line_item);
384
-        if ($pre_tax_subtotal instanceof EE_Line_Item) {
385
-            $success = $pre_tax_subtotal->add_child_line_item($item);
386
-        } else {
387
-            return false;
388
-        }
389
-        $total_line_item->recalculate_total_including_taxes();
390
-        return $success;
391
-    }
392
-
393
-
394
-    /**
395
-     * cancels an existing ticket line item,
396
-     * by decrementing it's quantity by 1 and adding a new "type_cancellation" sub-line-item.
397
-     * ALL totals and subtotals will NEED TO BE UPDATED after performing this action
398
-     *
399
-     * @param EE_Line_Item $ticket_line_item
400
-     * @param int          $qty
401
-     * @return bool success
402
-     * @throws EE_Error
403
-     * @throws InvalidArgumentException
404
-     * @throws InvalidDataTypeException
405
-     * @throws InvalidInterfaceException
406
-     * @throws ReflectionException
407
-     */
408
-    public static function cancel_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1)
409
-    {
410
-        // validate incoming line_item
411
-        if ($ticket_line_item->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_TICKET) {
412
-            throw new EE_Error(
413
-                sprintf(
414
-                    esc_html__(
415
-                        'The supplied line item must have an Object Type of "Ticket", not %1$s.',
416
-                        'event_espresso'
417
-                    ),
418
-                    $ticket_line_item->type()
419
-                )
420
-            );
421
-        }
422
-        if ($ticket_line_item->quantity() < $qty) {
423
-            throw new EE_Error(
424
-                sprintf(
425
-                    esc_html__(
426
-                        'Can not cancel %1$d ticket(s) because the supplied line item has a quantity of %2$d.',
427
-                        'event_espresso'
428
-                    ),
429
-                    $qty,
430
-                    $ticket_line_item->quantity()
431
-                )
432
-            );
433
-        }
434
-        // decrement ticket quantity; don't rely on auto-fixing when recalculating totals to do this
435
-        $ticket_line_item->set_quantity($ticket_line_item->quantity() - $qty);
436
-        foreach ($ticket_line_item->children() as $child_line_item) {
437
-            if ($child_line_item->is_sub_line_item()
438
-                && ! $child_line_item->is_percent()
439
-                && ! $child_line_item->is_cancellation()
440
-            ) {
441
-                $child_line_item->set_quantity($child_line_item->quantity() - $qty);
442
-            }
443
-        }
444
-        // get cancellation sub line item
445
-        $cancellation_line_item = EEH_Line_Item::get_descendants_of_type(
446
-            $ticket_line_item,
447
-            EEM_Line_Item::type_cancellation
448
-        );
449
-        $cancellation_line_item = reset($cancellation_line_item);
450
-        // verify that this ticket was indeed previously cancelled
451
-        if ($cancellation_line_item instanceof EE_Line_Item) {
452
-            // increment cancelled quantity
453
-            $cancellation_line_item->set_quantity($cancellation_line_item->quantity() + $qty);
454
-        } else {
455
-            // create cancellation sub line item
456
-            $cancellation_line_item = EE_Line_Item::new_instance(array(
457
-                'LIN_name'       => esc_html__('Cancellation', 'event_espresso'),
458
-                'LIN_desc'       => sprintf(
459
-                    esc_html_x(
460
-                        'Cancelled %1$s : %2$s',
461
-                        'Cancelled Ticket Name : 2015-01-01 11:11',
462
-                        'event_espresso'
463
-                    ),
464
-                    $ticket_line_item->name(),
465
-                    current_time(get_option('date_format') . ' ' . get_option('time_format'))
466
-                ),
467
-                'LIN_unit_price' => 0, // $ticket_line_item->unit_price()
468
-                'LIN_quantity'   => $qty,
469
-                'LIN_is_taxable' => $ticket_line_item->is_taxable(),
470
-                'LIN_order'      => count($ticket_line_item->children()),
471
-                'LIN_total'      => 0, // $ticket_line_item->unit_price()
472
-                'LIN_type'       => EEM_Line_Item::type_cancellation,
473
-            ));
474
-            $ticket_line_item->add_child_line_item($cancellation_line_item);
475
-        }
476
-        if ($ticket_line_item->save_this_and_descendants() > 0) {
477
-            // decrement parent line item quantity
478
-            $event_line_item = $ticket_line_item->parent();
479
-            if ($event_line_item instanceof EE_Line_Item
480
-                && $event_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT
481
-            ) {
482
-                $event_line_item->set_quantity($event_line_item->quantity() - $qty);
483
-                $event_line_item->save();
484
-            }
485
-            EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item);
486
-            return true;
487
-        }
488
-        return false;
489
-    }
490
-
491
-
492
-    /**
493
-     * reinstates (un-cancels?) a previously canceled ticket line item,
494
-     * by incrementing it's quantity by 1, and decrementing it's "type_cancellation" sub-line-item.
495
-     * ALL totals and subtotals will NEED TO BE UPDATED after performing this action
496
-     *
497
-     * @param EE_Line_Item $ticket_line_item
498
-     * @param int          $qty
499
-     * @return bool success
500
-     * @throws EE_Error
501
-     * @throws InvalidArgumentException
502
-     * @throws InvalidDataTypeException
503
-     * @throws InvalidInterfaceException
504
-     * @throws ReflectionException
505
-     */
506
-    public static function reinstate_canceled_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1)
507
-    {
508
-        // validate incoming line_item
509
-        if ($ticket_line_item->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_TICKET) {
510
-            throw new EE_Error(
511
-                sprintf(
512
-                    esc_html__(
513
-                        'The supplied line item must have an Object Type of "Ticket", not %1$s.',
514
-                        'event_espresso'
515
-                    ),
516
-                    $ticket_line_item->type()
517
-                )
518
-            );
519
-        }
520
-        // get cancellation sub line item
521
-        $cancellation_line_item = EEH_Line_Item::get_descendants_of_type(
522
-            $ticket_line_item,
523
-            EEM_Line_Item::type_cancellation
524
-        );
525
-        $cancellation_line_item = reset($cancellation_line_item);
526
-        // verify that this ticket was indeed previously cancelled
527
-        if (! $cancellation_line_item instanceof EE_Line_Item) {
528
-            return false;
529
-        }
530
-        if ($cancellation_line_item->quantity() > $qty) {
531
-            // decrement cancelled quantity
532
-            $cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty);
533
-        } elseif ($cancellation_line_item->quantity() === $qty) {
534
-            // decrement cancelled quantity in case anyone still has the object kicking around
535
-            $cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty);
536
-            // delete because quantity will end up as 0
537
-            $cancellation_line_item->delete();
538
-            // and attempt to destroy the object,
539
-            // even though PHP won't actually destroy it until it needs the memory
540
-            unset($cancellation_line_item);
541
-        } else {
542
-            // what ?!?! negative quantity ?!?!
543
-            throw new EE_Error(
544
-                sprintf(
545
-                    esc_html__(
546
-                        'Can not reinstate %1$d cancelled ticket(s) because the cancelled ticket quantity is only %2$d.',
547
-                        'event_espresso'
548
-                    ),
549
-                    $qty,
550
-                    $cancellation_line_item->quantity()
551
-                )
552
-            );
553
-        }
554
-        // increment ticket quantity
555
-        $ticket_line_item->set_quantity($ticket_line_item->quantity() + $qty);
556
-        if ($ticket_line_item->save_this_and_descendants() > 0) {
557
-            // increment parent line item quantity
558
-            $event_line_item = $ticket_line_item->parent();
559
-            if ($event_line_item instanceof EE_Line_Item
560
-                && $event_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT
561
-            ) {
562
-                $event_line_item->set_quantity($event_line_item->quantity() + $qty);
563
-            }
564
-            EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item);
565
-            return true;
566
-        }
567
-        return false;
568
-    }
569
-
570
-
571
-    /**
572
-     * calls EEH_Line_Item::find_transaction_grand_total_for_line_item()
573
-     * then EE_Line_Item::recalculate_total_including_taxes() on the result
574
-     *
575
-     * @param EE_Line_Item $line_item
576
-     * @return float
577
-     * @throws EE_Error
578
-     * @throws InvalidArgumentException
579
-     * @throws InvalidDataTypeException
580
-     * @throws InvalidInterfaceException
581
-     * @throws ReflectionException
582
-     */
583
-    public static function get_grand_total_and_recalculate_everything(EE_Line_Item $line_item)
584
-    {
585
-        $grand_total_line_item = EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item);
586
-        return $grand_total_line_item->recalculate_total_including_taxes();
587
-    }
588
-
589
-
590
-    /**
591
-     * Gets the line item which contains the subtotal of all the items
592
-     *
593
-     * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
594
-     * @return EE_Line_Item
595
-     * @throws EE_Error
596
-     * @throws InvalidArgumentException
597
-     * @throws InvalidDataTypeException
598
-     * @throws InvalidInterfaceException
599
-     * @throws ReflectionException
600
-     */
601
-    public static function get_pre_tax_subtotal(EE_Line_Item $total_line_item)
602
-    {
603
-        $pre_tax_subtotal = $total_line_item->get_child_line_item('pre-tax-subtotal');
604
-        return $pre_tax_subtotal instanceof EE_Line_Item
605
-            ? $pre_tax_subtotal
606
-            : self::create_pre_tax_subtotal($total_line_item);
607
-    }
608
-
609
-
610
-    /**
611
-     * Gets the line item for the taxes subtotal
612
-     *
613
-     * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
614
-     * @return EE_Line_Item
615
-     * @throws EE_Error
616
-     * @throws InvalidArgumentException
617
-     * @throws InvalidDataTypeException
618
-     * @throws InvalidInterfaceException
619
-     * @throws ReflectionException
620
-     */
621
-    public static function get_taxes_subtotal(EE_Line_Item $total_line_item)
622
-    {
623
-        $taxes = $total_line_item->get_child_line_item('taxes');
624
-        return $taxes ? $taxes : self::create_taxes_subtotal($total_line_item);
625
-    }
626
-
627
-
628
-    /**
629
-     * sets the TXN ID on an EE_Line_Item if passed a valid EE_Transaction object
630
-     *
631
-     * @param EE_Line_Item   $line_item
632
-     * @param EE_Transaction $transaction
633
-     * @return void
634
-     * @throws EE_Error
635
-     * @throws InvalidArgumentException
636
-     * @throws InvalidDataTypeException
637
-     * @throws InvalidInterfaceException
638
-     * @throws ReflectionException
639
-     */
640
-    public static function set_TXN_ID(EE_Line_Item $line_item, $transaction = null)
641
-    {
642
-        if ($transaction) {
643
-            /** @type EEM_Transaction $EEM_Transaction */
644
-            $EEM_Transaction = EE_Registry::instance()->load_model('Transaction');
645
-            $TXN_ID = $EEM_Transaction->ensure_is_ID($transaction);
646
-            $line_item->set_TXN_ID($TXN_ID);
647
-        }
648
-    }
649
-
650
-
651
-    /**
652
-     * Creates a new default total line item for the transaction,
653
-     * and its tickets subtotal and taxes subtotal line items (and adds the
654
-     * existing taxes as children of the taxes subtotal line item)
655
-     *
656
-     * @param EE_Transaction $transaction
657
-     * @return EE_Line_Item of type total
658
-     * @throws EE_Error
659
-     * @throws InvalidArgumentException
660
-     * @throws InvalidDataTypeException
661
-     * @throws InvalidInterfaceException
662
-     * @throws ReflectionException
663
-     */
664
-    public static function create_total_line_item($transaction = null)
665
-    {
666
-        $total_line_item = EE_Line_Item::new_instance(array(
667
-            'LIN_code' => 'total',
668
-            'LIN_name' => esc_html__('Grand Total', 'event_espresso'),
669
-            'LIN_type' => EEM_Line_Item::type_total,
670
-            'OBJ_type' => EEM_Line_Item::OBJ_TYPE_TRANSACTION,
671
-        ));
672
-        $total_line_item = apply_filters(
673
-            'FHEE__EEH_Line_Item__create_total_line_item__total_line_item',
674
-            $total_line_item
675
-        );
676
-        self::set_TXN_ID($total_line_item, $transaction);
677
-        self::create_pre_tax_subtotal($total_line_item, $transaction);
678
-        self::create_taxes_subtotal($total_line_item, $transaction);
679
-        return $total_line_item;
680
-    }
681
-
682
-
683
-    /**
684
-     * Creates a default items subtotal line item
685
-     *
686
-     * @param EE_Line_Item   $total_line_item
687
-     * @param EE_Transaction $transaction
688
-     * @return EE_Line_Item
689
-     * @throws EE_Error
690
-     * @throws InvalidArgumentException
691
-     * @throws InvalidDataTypeException
692
-     * @throws InvalidInterfaceException
693
-     * @throws ReflectionException
694
-     */
695
-    protected static function create_pre_tax_subtotal(EE_Line_Item $total_line_item, $transaction = null)
696
-    {
697
-        $pre_tax_line_item = EE_Line_Item::new_instance(array(
698
-            'LIN_code' => 'pre-tax-subtotal',
699
-            'LIN_name' => esc_html__('Pre-Tax Subtotal', 'event_espresso'),
700
-            'LIN_type' => EEM_Line_Item::type_sub_total,
701
-        ));
702
-        $pre_tax_line_item = apply_filters(
703
-            'FHEE__EEH_Line_Item__create_pre_tax_subtotal__pre_tax_line_item',
704
-            $pre_tax_line_item
705
-        );
706
-        self::set_TXN_ID($pre_tax_line_item, $transaction);
707
-        $total_line_item->add_child_line_item($pre_tax_line_item);
708
-        self::create_event_subtotal($pre_tax_line_item, $transaction);
709
-        return $pre_tax_line_item;
710
-    }
711
-
712
-
713
-    /**
714
-     * Creates a line item for the taxes subtotal and finds all the tax prices
715
-     * and applies taxes to it
716
-     *
717
-     * @param EE_Line_Item   $total_line_item of type EEM_Line_Item::type_total
718
-     * @param EE_Transaction $transaction
719
-     * @return EE_Line_Item
720
-     * @throws EE_Error
721
-     * @throws InvalidArgumentException
722
-     * @throws InvalidDataTypeException
723
-     * @throws InvalidInterfaceException
724
-     * @throws ReflectionException
725
-     */
726
-    protected static function create_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null)
727
-    {
728
-        $tax_line_item = EE_Line_Item::new_instance(array(
729
-            'LIN_code'  => 'taxes',
730
-            'LIN_name'  => esc_html__('Taxes', 'event_espresso'),
731
-            'LIN_type'  => EEM_Line_Item::type_tax_sub_total,
732
-            'LIN_order' => 1000,// this should always come last
733
-        ));
734
-        $tax_line_item = apply_filters(
735
-            'FHEE__EEH_Line_Item__create_taxes_subtotal__tax_line_item',
736
-            $tax_line_item
737
-        );
738
-        self::set_TXN_ID($tax_line_item, $transaction);
739
-        $total_line_item->add_child_line_item($tax_line_item);
740
-        // and lastly, add the actual taxes
741
-        self::apply_taxes($total_line_item);
742
-        return $tax_line_item;
743
-    }
744
-
745
-
746
-    /**
747
-     * Creates a default items subtotal line item
748
-     *
749
-     * @param EE_Line_Item   $pre_tax_line_item
750
-     * @param EE_Transaction $transaction
751
-     * @param EE_Event       $event
752
-     * @return EE_Line_Item
753
-     * @throws EE_Error
754
-     * @throws InvalidArgumentException
755
-     * @throws InvalidDataTypeException
756
-     * @throws InvalidInterfaceException
757
-     * @throws ReflectionException
758
-     */
759
-    public static function create_event_subtotal(EE_Line_Item $pre_tax_line_item, $transaction = null, $event = null)
760
-    {
761
-        $event_line_item = EE_Line_Item::new_instance(array(
762
-            'LIN_code' => self::get_event_code($event),
763
-            'LIN_name' => self::get_event_name($event),
764
-            'LIN_desc' => self::get_event_desc($event),
765
-            'LIN_type' => EEM_Line_Item::type_sub_total,
766
-            'OBJ_type' => EEM_Line_Item::OBJ_TYPE_EVENT,
767
-            'OBJ_ID'   => $event instanceof EE_Event ? $event->ID() : 0,
768
-        ));
769
-        $event_line_item = apply_filters(
770
-            'FHEE__EEH_Line_Item__create_event_subtotal__event_line_item',
771
-            $event_line_item
772
-        );
773
-        self::set_TXN_ID($event_line_item, $transaction);
774
-        $pre_tax_line_item->add_child_line_item($event_line_item);
775
-        return $event_line_item;
776
-    }
777
-
778
-
779
-    /**
780
-     * Gets what the event ticket's code SHOULD be
781
-     *
782
-     * @param EE_Event $event
783
-     * @return string
784
-     * @throws EE_Error
785
-     */
786
-    public static function get_event_code($event)
787
-    {
788
-        return 'event-' . ($event instanceof EE_Event ? $event->ID() : '0');
789
-    }
790
-
791
-
792
-    /**
793
-     * Gets the event name
794
-     *
795
-     * @param EE_Event $event
796
-     * @return string
797
-     * @throws EE_Error
798
-     */
799
-    public static function get_event_name($event)
800
-    {
801
-        return $event instanceof EE_Event
802
-            ? mb_substr($event->name(), 0, 245)
803
-            : esc_html__('Event', 'event_espresso');
804
-    }
805
-
806
-
807
-    /**
808
-     * Gets the event excerpt
809
-     *
810
-     * @param EE_Event $event
811
-     * @return string
812
-     * @throws EE_Error
813
-     */
814
-    public static function get_event_desc($event)
815
-    {
816
-        return $event instanceof EE_Event ? $event->short_description() : '';
817
-    }
818
-
819
-
820
-    /**
821
-     * Given the grand total line item and a ticket, finds the event sub-total
822
-     * line item the ticket's purchase should be added onto
823
-     *
824
-     * @access public
825
-     * @param EE_Line_Item $grand_total the grand total line item
826
-     * @param EE_Ticket    $ticket
827
-     * @return EE_Line_Item
828
-     * @throws EE_Error
829
-     * @throws InvalidArgumentException
830
-     * @throws InvalidDataTypeException
831
-     * @throws InvalidInterfaceException
832
-     * @throws ReflectionException
833
-     */
834
-    public static function get_event_line_item_for_ticket(EE_Line_Item $grand_total, EE_Ticket $ticket)
835
-    {
836
-        $first_datetime = $ticket->first_datetime();
837
-        if (! $first_datetime instanceof EE_Datetime) {
838
-            throw new EE_Error(
839
-                sprintf(
840
-                    __('The supplied ticket (ID %d) has no datetimes', 'event_espresso'),
841
-                    $ticket->ID()
842
-                )
843
-            );
844
-        }
845
-        $event = $first_datetime->event();
846
-        if (! $event instanceof EE_Event) {
847
-            throw new EE_Error(
848
-                sprintf(
849
-                    esc_html__(
850
-                        'The supplied ticket (ID %d) has no event data associated with it.',
851
-                        'event_espresso'
852
-                    ),
853
-                    $ticket->ID()
854
-                )
855
-            );
856
-        }
857
-        $events_sub_total = EEH_Line_Item::get_event_line_item($grand_total, $event);
858
-        if (! $events_sub_total instanceof EE_Line_Item) {
859
-            throw new EE_Error(
860
-                sprintf(
861
-                    esc_html__(
862
-                        'There is no events sub-total for ticket %s on total line item %d',
863
-                        'event_espresso'
864
-                    ),
865
-                    $ticket->ID(),
866
-                    $grand_total->ID()
867
-                )
868
-            );
869
-        }
870
-        return $events_sub_total;
871
-    }
872
-
873
-
874
-    /**
875
-     * Gets the event line item
876
-     *
877
-     * @param EE_Line_Item $grand_total
878
-     * @param EE_Event     $event
879
-     * @return EE_Line_Item for the event subtotal which is a child of $grand_total
880
-     * @throws EE_Error
881
-     * @throws InvalidArgumentException
882
-     * @throws InvalidDataTypeException
883
-     * @throws InvalidInterfaceException
884
-     * @throws ReflectionException
885
-     */
886
-    public static function get_event_line_item(EE_Line_Item $grand_total, $event)
887
-    {
888
-        /** @type EE_Event $event */
889
-        $event = EEM_Event::instance()->ensure_is_obj($event, true);
890
-        $event_line_item = null;
891
-        $found = false;
892
-        foreach (EEH_Line_Item::get_event_subtotals($grand_total) as $event_line_item) {
893
-            // default event subtotal, we should only ever find this the first time this method is called
894
-            if (! $event_line_item->OBJ_ID()) {
895
-                // let's use this! but first... set the event details
896
-                EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
897
-                $found = true;
898
-                break;
899
-            }
900
-            if ($event_line_item->OBJ_ID() === $event->ID()) {
901
-                // found existing line item for this event in the cart, so break out of loop and use this one
902
-                $found = true;
903
-                break;
904
-            }
905
-        }
906
-        if (! $found) {
907
-            // there is no event sub-total yet, so add it
908
-            $pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($grand_total);
909
-            // create a new "event" subtotal below that
910
-            $event_line_item = EEH_Line_Item::create_event_subtotal($pre_tax_subtotal, null, $event);
911
-            // and set the event details
912
-            EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
913
-        }
914
-        return $event_line_item;
915
-    }
916
-
917
-
918
-    /**
919
-     * Creates a default items subtotal line item
920
-     *
921
-     * @param EE_Line_Item   $event_line_item
922
-     * @param EE_Event       $event
923
-     * @param EE_Transaction $transaction
924
-     * @return void
925
-     * @throws EE_Error
926
-     * @throws InvalidArgumentException
927
-     * @throws InvalidDataTypeException
928
-     * @throws InvalidInterfaceException
929
-     * @throws ReflectionException
930
-     */
931
-    public static function set_event_subtotal_details(
932
-        EE_Line_Item $event_line_item,
933
-        EE_Event $event,
934
-        $transaction = null
935
-    ) {
936
-        if ($event instanceof EE_Event) {
937
-            $event_line_item->set_code(self::get_event_code($event));
938
-            $event_line_item->set_name(self::get_event_name($event));
939
-            $event_line_item->set_desc(self::get_event_desc($event));
940
-            $event_line_item->set_OBJ_ID($event->ID());
941
-        }
942
-        self::set_TXN_ID($event_line_item, $transaction);
943
-    }
944
-
945
-
946
-    /**
947
-     * Finds what taxes should apply, adds them as tax line items under the taxes sub-total,
948
-     * and recalculates the taxes sub-total and the grand total. Resets the taxes, so
949
-     * any old taxes are removed
950
-     *
951
-     * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
952
-     * @param bool         $update_txn_status
953
-     * @return bool
954
-     * @throws EE_Error
955
-     * @throws InvalidArgumentException
956
-     * @throws InvalidDataTypeException
957
-     * @throws InvalidInterfaceException
958
-     * @throws ReflectionException
959
-     * @throws RuntimeException
960
-     */
961
-    public static function apply_taxes(EE_Line_Item $total_line_item, $update_txn_status = false)
962
-    {
963
-        /** @type EEM_Price $EEM_Price */
964
-        $EEM_Price = EE_Registry::instance()->load_model('Price');
965
-        // get array of taxes via Price Model
966
-        $ordered_taxes = $EEM_Price->get_all_prices_that_are_taxes();
967
-        ksort($ordered_taxes);
968
-        $taxes_line_item = self::get_taxes_subtotal($total_line_item);
969
-        // just to be safe, remove its old tax line items
970
-        $deleted = $taxes_line_item->delete_children_line_items();
971
-        $updates = false;
972
-        // loop thru taxes
973
-        foreach ($ordered_taxes as $order => $taxes) {
974
-            foreach ($taxes as $tax) {
975
-                if ($tax instanceof EE_Price) {
976
-                    $tax_line_item = EE_Line_Item::new_instance(
977
-                        array(
978
-                            'LIN_name'       => $tax->name(),
979
-                            'LIN_desc'       => $tax->desc(),
980
-                            'LIN_percent'    => $tax->amount(),
981
-                            'LIN_is_taxable' => false,
982
-                            'LIN_order'      => $order,
983
-                            'LIN_total'      => 0,
984
-                            'LIN_type'       => EEM_Line_Item::type_tax,
985
-                            'OBJ_type'       => EEM_Line_Item::OBJ_TYPE_PRICE,
986
-                            'OBJ_ID'         => $tax->ID(),
987
-                        )
988
-                    );
989
-                    $tax_line_item = apply_filters(
990
-                        'FHEE__EEH_Line_Item__apply_taxes__tax_line_item',
991
-                        $tax_line_item
992
-                    );
993
-                    $updates = $taxes_line_item->add_child_line_item($tax_line_item) ?
994
-                        true :
995
-                        $updates;
996
-                }
997
-            }
998
-        }
999
-        // only recalculate totals if something changed
1000
-        if ($deleted || $updates) {
1001
-            $total_line_item->recalculate_total_including_taxes($update_txn_status);
1002
-            return true;
1003
-        }
1004
-        return false;
1005
-    }
1006
-
1007
-
1008
-    /**
1009
-     * Ensures that taxes have been applied to the order, if not applies them.
1010
-     * Returns the total amount of tax
1011
-     *
1012
-     * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
1013
-     * @return float
1014
-     * @throws EE_Error
1015
-     * @throws InvalidArgumentException
1016
-     * @throws InvalidDataTypeException
1017
-     * @throws InvalidInterfaceException
1018
-     * @throws ReflectionException
1019
-     */
1020
-    public static function ensure_taxes_applied($total_line_item)
1021
-    {
1022
-        $taxes_subtotal = self::get_taxes_subtotal($total_line_item);
1023
-        if (! $taxes_subtotal->children()) {
1024
-            self::apply_taxes($total_line_item);
1025
-        }
1026
-        return $taxes_subtotal->total();
1027
-    }
1028
-
1029
-
1030
-    /**
1031
-     * Deletes ALL children of the passed line item
1032
-     *
1033
-     * @param EE_Line_Item $parent_line_item
1034
-     * @return bool
1035
-     * @throws EE_Error
1036
-     * @throws InvalidArgumentException
1037
-     * @throws InvalidDataTypeException
1038
-     * @throws InvalidInterfaceException
1039
-     * @throws ReflectionException
1040
-     */
1041
-    public static function delete_all_child_items(EE_Line_Item $parent_line_item)
1042
-    {
1043
-        $deleted = 0;
1044
-        foreach ($parent_line_item->children() as $child_line_item) {
1045
-            if ($child_line_item instanceof EE_Line_Item) {
1046
-                $deleted += EEH_Line_Item::delete_all_child_items($child_line_item);
1047
-                if ($child_line_item->ID()) {
1048
-                    $child_line_item->delete();
1049
-                    unset($child_line_item);
1050
-                } else {
1051
-                    $parent_line_item->delete_child_line_item($child_line_item->code());
1052
-                }
1053
-                $deleted++;
1054
-            }
1055
-        }
1056
-        return $deleted;
1057
-    }
1058
-
1059
-
1060
-    /**
1061
-     * Deletes the line items as indicated by the line item code(s) provided,
1062
-     * regardless of where they're found in the line item tree. Automatically
1063
-     * re-calculates the line item totals and updates the related transaction. But
1064
-     * DOES NOT automatically upgrade the transaction's registrations' final prices (which
1065
-     * should probably change because of this).
1066
-     * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
1067
-     * after using this, to keep the registration final prices in-sync with the transaction's total.
1068
-     *
1069
-     * @param EE_Line_Item      $total_line_item of type EEM_Line_Item::type_total
1070
-     * @param array|bool|string $line_item_codes
1071
-     * @return int number of items successfully removed
1072
-     * @throws EE_Error
1073
-     */
1074
-    public static function delete_items(EE_Line_Item $total_line_item, $line_item_codes = false)
1075
-    {
1076
-
1077
-        if ($total_line_item->type() !== EEM_Line_Item::type_total) {
1078
-            EE_Error::doing_it_wrong(
1079
-                'EEH_Line_Item::delete_items',
1080
-                esc_html__(
1081
-                    'This static method should only be called with a TOTAL line item, otherwise we won\'t recalculate the totals correctly',
1082
-                    'event_espresso'
1083
-                ),
1084
-                '4.6.18'
1085
-            );
1086
-        }
1087
-        do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1088
-
1089
-        // check if only a single line_item_id was passed
1090
-        if (! empty($line_item_codes) && ! is_array($line_item_codes)) {
1091
-            // place single line_item_id in an array to appear as multiple line_item_ids
1092
-            $line_item_codes = array($line_item_codes);
1093
-        }
1094
-        $removals = 0;
1095
-        // cycle thru line_item_ids
1096
-        foreach ($line_item_codes as $line_item_id) {
1097
-            $removals += $total_line_item->delete_child_line_item($line_item_id);
1098
-        }
1099
-
1100
-        if ($removals > 0) {
1101
-            $total_line_item->recalculate_taxes_and_tax_total();
1102
-            return $removals;
1103
-        } else {
1104
-            return false;
1105
-        }
1106
-    }
1107
-
1108
-
1109
-    /**
1110
-     * Overwrites the previous tax by clearing out the old taxes, and creates a new
1111
-     * tax and updates the total line item accordingly
1112
-     *
1113
-     * @param EE_Line_Item $total_line_item
1114
-     * @param float        $amount
1115
-     * @param string       $name
1116
-     * @param string       $description
1117
-     * @param string       $code
1118
-     * @param boolean      $add_to_existing_line_item
1119
-     *                          if true, and a duplicate line item with the same code is found,
1120
-     *                          $amount will be added onto it; otherwise will simply set the taxes to match $amount
1121
-     * @return EE_Line_Item the new tax line item created
1122
-     * @throws EE_Error
1123
-     * @throws InvalidArgumentException
1124
-     * @throws InvalidDataTypeException
1125
-     * @throws InvalidInterfaceException
1126
-     * @throws ReflectionException
1127
-     */
1128
-    public static function set_total_tax_to(
1129
-        EE_Line_Item $total_line_item,
1130
-        $amount,
1131
-        $name = null,
1132
-        $description = null,
1133
-        $code = null,
1134
-        $add_to_existing_line_item = false
1135
-    ) {
1136
-        $tax_subtotal = self::get_taxes_subtotal($total_line_item);
1137
-        $taxable_total = $total_line_item->taxable_total();
1138
-
1139
-        if ($add_to_existing_line_item) {
1140
-            $new_tax = $tax_subtotal->get_child_line_item($code);
1141
-            EEM_Line_Item::instance()->delete(
1142
-                array(array('LIN_code' => array('!=', $code), 'LIN_parent' => $tax_subtotal->ID()))
1143
-            );
1144
-        } else {
1145
-            $new_tax = null;
1146
-            $tax_subtotal->delete_children_line_items();
1147
-        }
1148
-        if ($new_tax) {
1149
-            $new_tax->set_total($new_tax->total() + $amount);
1150
-            $new_tax->set_percent($taxable_total ? $new_tax->total() / $taxable_total * 100 : 0);
1151
-        } else {
1152
-            // no existing tax item. Create it
1153
-            $new_tax = EE_Line_Item::new_instance(array(
1154
-                'TXN_ID'      => $total_line_item->TXN_ID(),
1155
-                'LIN_name'    => $name ? $name : esc_html__('Tax', 'event_espresso'),
1156
-                'LIN_desc'    => $description ? $description : '',
1157
-                'LIN_percent' => $taxable_total ? ($amount / $taxable_total * 100) : 0,
1158
-                'LIN_total'   => $amount,
1159
-                'LIN_parent'  => $tax_subtotal->ID(),
1160
-                'LIN_type'    => EEM_Line_Item::type_tax,
1161
-                'LIN_code'    => $code,
1162
-            ));
1163
-        }
1164
-
1165
-        $new_tax = apply_filters(
1166
-            'FHEE__EEH_Line_Item__set_total_tax_to__new_tax_subtotal',
1167
-            $new_tax,
1168
-            $total_line_item
1169
-        );
1170
-        $new_tax->save();
1171
-        $tax_subtotal->set_total($new_tax->total());
1172
-        $tax_subtotal->save();
1173
-        $total_line_item->recalculate_total_including_taxes();
1174
-        return $new_tax;
1175
-    }
1176
-
1177
-
1178
-    /**
1179
-     * Makes all the line items which are children of $line_item taxable (or not).
1180
-     * Does NOT save the line items
1181
-     *
1182
-     * @param EE_Line_Item $line_item
1183
-     * @param boolean      $taxable
1184
-     * @param string       $code_substring_for_whitelist if this string is part of the line item's code
1185
-     *                                                   it will be whitelisted (ie, except from becoming taxable)
1186
-     * @throws EE_Error
1187
-     */
1188
-    public static function set_line_items_taxable(
1189
-        EE_Line_Item $line_item,
1190
-        $taxable = true,
1191
-        $code_substring_for_whitelist = null
1192
-    ) {
1193
-        $whitelisted = false;
1194
-        if ($code_substring_for_whitelist !== null) {
1195
-            $whitelisted = strpos($line_item->code(), $code_substring_for_whitelist) !== false;
1196
-        }
1197
-        if (! $whitelisted && $line_item->is_line_item()) {
1198
-            $line_item->set_is_taxable($taxable);
1199
-        }
1200
-        foreach ($line_item->children() as $child_line_item) {
1201
-            EEH_Line_Item::set_line_items_taxable(
1202
-                $child_line_item,
1203
-                $taxable,
1204
-                $code_substring_for_whitelist
1205
-            );
1206
-        }
1207
-    }
1208
-
1209
-
1210
-    /**
1211
-     * Gets all descendants that are event subtotals
1212
-     *
1213
-     * @uses  EEH_Line_Item::get_subtotals_of_object_type()
1214
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1215
-     * @return EE_Line_Item[]
1216
-     * @throws EE_Error
1217
-     */
1218
-    public static function get_event_subtotals(EE_Line_Item $parent_line_item)
1219
-    {
1220
-        return self::get_subtotals_of_object_type($parent_line_item, EEM_Line_Item::OBJ_TYPE_EVENT);
1221
-    }
1222
-
1223
-
1224
-    /**
1225
-     * Gets all descendants subtotals that match the supplied object type
1226
-     *
1227
-     * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1228
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1229
-     * @param string       $obj_type
1230
-     * @return EE_Line_Item[]
1231
-     * @throws EE_Error
1232
-     */
1233
-    public static function get_subtotals_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '')
1234
-    {
1235
-        return self::_get_descendants_by_type_and_object_type(
1236
-            $parent_line_item,
1237
-            EEM_Line_Item::type_sub_total,
1238
-            $obj_type
1239
-        );
1240
-    }
1241
-
1242
-
1243
-    /**
1244
-     * Gets all descendants that are tickets
1245
-     *
1246
-     * @uses  EEH_Line_Item::get_line_items_of_object_type()
1247
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1248
-     * @return EE_Line_Item[]
1249
-     * @throws EE_Error
1250
-     */
1251
-    public static function get_ticket_line_items(EE_Line_Item $parent_line_item)
1252
-    {
1253
-        return self::get_line_items_of_object_type(
1254
-            $parent_line_item,
1255
-            EEM_Line_Item::OBJ_TYPE_TICKET
1256
-        );
1257
-    }
1258
-
1259
-
1260
-    /**
1261
-     * Gets all descendants subtotals that match the supplied object type
1262
-     *
1263
-     * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1264
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1265
-     * @param string       $obj_type
1266
-     * @return EE_Line_Item[]
1267
-     * @throws EE_Error
1268
-     */
1269
-    public static function get_line_items_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '')
1270
-    {
1271
-        return self::_get_descendants_by_type_and_object_type(
1272
-            $parent_line_item,
1273
-            EEM_Line_Item::type_line_item,
1274
-            $obj_type
1275
-        );
1276
-    }
1277
-
1278
-
1279
-    /**
1280
-     * Gets all the descendants (ie, children or children of children etc) that are of the type 'tax'
1281
-     *
1282
-     * @uses  EEH_Line_Item::get_descendants_of_type()
1283
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1284
-     * @return EE_Line_Item[]
1285
-     * @throws EE_Error
1286
-     */
1287
-    public static function get_tax_descendants(EE_Line_Item $parent_line_item)
1288
-    {
1289
-        return EEH_Line_Item::get_descendants_of_type(
1290
-            $parent_line_item,
1291
-            EEM_Line_Item::type_tax
1292
-        );
1293
-    }
1294
-
1295
-
1296
-    /**
1297
-     * Gets all the real items purchased which are children of this item
1298
-     *
1299
-     * @uses  EEH_Line_Item::get_descendants_of_type()
1300
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1301
-     * @return EE_Line_Item[]
1302
-     * @throws EE_Error
1303
-     */
1304
-    public static function get_line_item_descendants(EE_Line_Item $parent_line_item)
1305
-    {
1306
-        return EEH_Line_Item::get_descendants_of_type(
1307
-            $parent_line_item,
1308
-            EEM_Line_Item::type_line_item
1309
-        );
1310
-    }
1311
-
1312
-
1313
-    /**
1314
-     * Gets all descendants of supplied line item that match the supplied line item type
1315
-     *
1316
-     * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1317
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1318
-     * @param string       $line_item_type   one of the EEM_Line_Item constants
1319
-     * @return EE_Line_Item[]
1320
-     * @throws EE_Error
1321
-     */
1322
-    public static function get_descendants_of_type(EE_Line_Item $parent_line_item, $line_item_type)
1323
-    {
1324
-        return self::_get_descendants_by_type_and_object_type(
1325
-            $parent_line_item,
1326
-            $line_item_type,
1327
-            null
1328
-        );
1329
-    }
1330
-
1331
-
1332
-    /**
1333
-     * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type
1334
-     * as well
1335
-     *
1336
-     * @param EE_Line_Item  $parent_line_item - the line item to find descendants of
1337
-     * @param string        $line_item_type   one of the EEM_Line_Item constants
1338
-     * @param string | NULL $obj_type         object model class name (minus prefix) or NULL to ignore object type when
1339
-     *                                        searching
1340
-     * @return EE_Line_Item[]
1341
-     * @throws EE_Error
1342
-     */
1343
-    protected static function _get_descendants_by_type_and_object_type(
1344
-        EE_Line_Item $parent_line_item,
1345
-        $line_item_type,
1346
-        $obj_type = null
1347
-    ) {
1348
-        $objects = array();
1349
-        foreach ($parent_line_item->children() as $child_line_item) {
1350
-            if ($child_line_item instanceof EE_Line_Item) {
1351
-                if ($child_line_item->type() === $line_item_type
1352
-                    && (
1353
-                        $child_line_item->OBJ_type() === $obj_type || $obj_type === null
1354
-                    )
1355
-                ) {
1356
-                    $objects[] = $child_line_item;
1357
-                } else {
1358
-                    // go-through-all-its children looking for more matches
1359
-                    $objects = array_merge(
1360
-                        $objects,
1361
-                        self::_get_descendants_by_type_and_object_type(
1362
-                            $child_line_item,
1363
-                            $line_item_type,
1364
-                            $obj_type
1365
-                        )
1366
-                    );
1367
-                }
1368
-            }
1369
-        }
1370
-        return $objects;
1371
-    }
1372
-
1373
-
1374
-    /**
1375
-     * Gets all descendants subtotals that match the supplied object type
1376
-     *
1377
-     * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1378
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1379
-     * @param string       $OBJ_type         object type (like Event)
1380
-     * @param array        $OBJ_IDs          array of OBJ_IDs
1381
-     * @return EE_Line_Item[]
1382
-     * @throws EE_Error
1383
-     */
1384
-    public static function get_line_items_by_object_type_and_IDs(
1385
-        EE_Line_Item $parent_line_item,
1386
-        $OBJ_type = '',
1387
-        $OBJ_IDs = array()
1388
-    ) {
1389
-        return self::_get_descendants_by_object_type_and_object_ID(
1390
-            $parent_line_item,
1391
-            $OBJ_type,
1392
-            $OBJ_IDs
1393
-        );
1394
-    }
1395
-
1396
-
1397
-    /**
1398
-     * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type
1399
-     * as well
1400
-     *
1401
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1402
-     * @param string       $OBJ_type         object type (like Event)
1403
-     * @param array        $OBJ_IDs          array of OBJ_IDs
1404
-     * @return EE_Line_Item[]
1405
-     * @throws EE_Error
1406
-     */
1407
-    protected static function _get_descendants_by_object_type_and_object_ID(
1408
-        EE_Line_Item $parent_line_item,
1409
-        $OBJ_type,
1410
-        $OBJ_IDs
1411
-    ) {
1412
-        $objects = array();
1413
-        foreach ($parent_line_item->children() as $child_line_item) {
1414
-            if ($child_line_item instanceof EE_Line_Item) {
1415
-                if ($child_line_item->OBJ_type() === $OBJ_type
1416
-                    && is_array($OBJ_IDs)
1417
-                    && in_array($child_line_item->OBJ_ID(), $OBJ_IDs)
1418
-                ) {
1419
-                    $objects[] = $child_line_item;
1420
-                } else {
1421
-                    // go-through-all-its children looking for more matches
1422
-                    $objects = array_merge(
1423
-                        $objects,
1424
-                        self::_get_descendants_by_object_type_and_object_ID(
1425
-                            $child_line_item,
1426
-                            $OBJ_type,
1427
-                            $OBJ_IDs
1428
-                        )
1429
-                    );
1430
-                }
1431
-            }
1432
-        }
1433
-        return $objects;
1434
-    }
1435
-
1436
-
1437
-    /**
1438
-     * Uses a breadth-first-search in order to find the nearest descendant of
1439
-     * the specified type and returns it, else NULL
1440
-     *
1441
-     * @uses  EEH_Line_Item::_get_nearest_descendant()
1442
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1443
-     * @param string       $type             like one of the EEM_Line_Item::type_*
1444
-     * @return EE_Line_Item
1445
-     * @throws EE_Error
1446
-     * @throws InvalidArgumentException
1447
-     * @throws InvalidDataTypeException
1448
-     * @throws InvalidInterfaceException
1449
-     * @throws ReflectionException
1450
-     */
1451
-    public static function get_nearest_descendant_of_type(EE_Line_Item $parent_line_item, $type)
1452
-    {
1453
-        return self::_get_nearest_descendant($parent_line_item, 'LIN_type', $type);
1454
-    }
1455
-
1456
-
1457
-    /**
1458
-     * Uses a breadth-first-search in order to find the nearest descendant
1459
-     * having the specified LIN_code and returns it, else NULL
1460
-     *
1461
-     * @uses  EEH_Line_Item::_get_nearest_descendant()
1462
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1463
-     * @param string       $code             any value used for LIN_code
1464
-     * @return EE_Line_Item
1465
-     * @throws EE_Error
1466
-     * @throws InvalidArgumentException
1467
-     * @throws InvalidDataTypeException
1468
-     * @throws InvalidInterfaceException
1469
-     * @throws ReflectionException
1470
-     */
1471
-    public static function get_nearest_descendant_having_code(EE_Line_Item $parent_line_item, $code)
1472
-    {
1473
-        return self::_get_nearest_descendant($parent_line_item, 'LIN_code', $code);
1474
-    }
1475
-
1476
-
1477
-    /**
1478
-     * Uses a breadth-first-search in order to find the nearest descendant
1479
-     * having the specified LIN_code and returns it, else NULL
1480
-     *
1481
-     * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1482
-     * @param string       $search_field     name of EE_Line_Item property
1483
-     * @param string       $value            any value stored in $search_field
1484
-     * @return EE_Line_Item
1485
-     * @throws EE_Error
1486
-     * @throws InvalidArgumentException
1487
-     * @throws InvalidDataTypeException
1488
-     * @throws InvalidInterfaceException
1489
-     * @throws ReflectionException
1490
-     */
1491
-    protected static function _get_nearest_descendant(EE_Line_Item $parent_line_item, $search_field, $value)
1492
-    {
1493
-        foreach ($parent_line_item->children() as $child) {
1494
-            if ($child->get($search_field) == $value) {
1495
-                return $child;
1496
-            }
1497
-        }
1498
-        foreach ($parent_line_item->children() as $child) {
1499
-            $descendant_found = self::_get_nearest_descendant(
1500
-                $child,
1501
-                $search_field,
1502
-                $value
1503
-            );
1504
-            if ($descendant_found) {
1505
-                return $descendant_found;
1506
-            }
1507
-        }
1508
-        return null;
1509
-    }
1510
-
1511
-
1512
-    /**
1513
-     * if passed line item has a TXN ID, uses that to jump directly to the grand total line item for the transaction,
1514
-     * else recursively walks up the line item tree until a parent of type total is found,
1515
-     *
1516
-     * @param EE_Line_Item $line_item
1517
-     * @return EE_Line_Item
1518
-     * @throws EE_Error
1519
-     */
1520
-    public static function find_transaction_grand_total_for_line_item(EE_Line_Item $line_item)
1521
-    {
1522
-        if ($line_item->TXN_ID()) {
1523
-            $total_line_item = $line_item->transaction()->total_line_item(false);
1524
-            if ($total_line_item instanceof EE_Line_Item) {
1525
-                return $total_line_item;
1526
-            }
1527
-        } else {
1528
-            $line_item_parent = $line_item->parent();
1529
-            if ($line_item_parent instanceof EE_Line_Item) {
1530
-                if ($line_item_parent->is_total()) {
1531
-                    return $line_item_parent;
1532
-                }
1533
-                return EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item_parent);
1534
-            }
1535
-        }
1536
-        throw new EE_Error(
1537
-            sprintf(
1538
-                esc_html__(
1539
-                    'A valid grand total for line item %1$d was not found.',
1540
-                    'event_espresso'
1541
-                ),
1542
-                $line_item->ID()
1543
-            )
1544
-        );
1545
-    }
1546
-
1547
-
1548
-    /**
1549
-     * Prints out a representation of the line item tree
1550
-     *
1551
-     * @param EE_Line_Item $line_item
1552
-     * @param int          $indentation
1553
-     * @return void
1554
-     * @throws EE_Error
1555
-     */
1556
-    public static function visualize(EE_Line_Item $line_item, $indentation = 0)
1557
-    {
1558
-        echo defined('EE_TESTS_DIR') ? "\n" : '<br />';
1559
-        if (! $indentation) {
1560
-            echo defined('EE_TESTS_DIR') ? "\n" : '<br />';
1561
-        }
1562
-        for ($i = 0; $i < $indentation; $i++) {
1563
-            echo '. ';
1564
-        }
1565
-        $breakdown = '';
1566
-        if ($line_item->is_line_item()) {
1567
-            if ($line_item->is_percent()) {
1568
-                $breakdown = "{$line_item->percent()}%";
1569
-            } else {
1570
-                $breakdown = '$' . "{$line_item->unit_price()} x {$line_item->quantity()}";
1571
-            }
1572
-        }
1573
-        echo $line_item->name();
1574
-        echo " [ ID:{$line_item->ID()} | qty:{$line_item->quantity()} ] {$line_item->type()} : ";
1575
-        echo '$' . (string) $line_item->total();
1576
-        if ($breakdown) {
1577
-            echo " ( {$breakdown} )";
1578
-        }
1579
-        if ($line_item->is_taxable()) {
1580
-            echo '  * taxable';
1581
-        }
1582
-        if ($line_item->children()) {
1583
-            foreach ($line_item->children() as $child) {
1584
-                self::visualize($child, $indentation + 1);
1585
-            }
1586
-        }
1587
-    }
1588
-
1589
-
1590
-    /**
1591
-     * Calculates the registration's final price, taking into account that they
1592
-     * need to not only help pay for their OWN ticket, but also any transaction-wide surcharges and taxes,
1593
-     * and receive a portion of any transaction-wide discounts.
1594
-     * eg1, if I buy a $1 ticket and brent buys a $9 ticket, and we receive a $5 discount
1595
-     * then I'll get 1/10 of that $5 discount, which is $0.50, and brent will get
1596
-     * 9/10ths of that $5 discount, which is $4.50. So my final price should be $0.50
1597
-     * and brent's final price should be $5.50.
1598
-     * In order to do this, we basically need to traverse the line item tree calculating
1599
-     * the running totals (just as if we were recalculating the total), but when we identify
1600
-     * regular line items, we need to keep track of their share of the grand total.
1601
-     * Also, we need to keep track of the TAXABLE total for each ticket purchase, so
1602
-     * we can know how to apply taxes to it. (Note: "taxable total" does not equal the "pretax total"
1603
-     * when there are non-taxable items; otherwise they would be the same)
1604
-     *
1605
-     * @param EE_Line_Item $line_item
1606
-     * @param array        $billable_ticket_quantities  array of EE_Ticket IDs and their corresponding quantity that
1607
-     *                                                  can be included in price calculations at this moment
1608
-     * @return array        keys are line items for tickets IDs and values are their share of the running total,
1609
-     *                                                  plus the key 'total', and 'taxable' which also has keys of all
1610
-     *                                                  the ticket IDs.
1611
-     *                                                  Eg array(
1612
-     *                                                      12 => 4.3
1613
-     *                                                      23 => 8.0
1614
-     *                                                      'total' => 16.6,
1615
-     *                                                      'taxable' => array(
1616
-     *                                                          12 => 10,
1617
-     *                                                          23 => 4
1618
-     *                                                      ).
1619
-     *                                                  So to find which registrations have which final price, we need
1620
-     *                                                  to find which line item is theirs, which can be done with
1621
-     *                                                  `EEM_Line_Item::instance()->get_line_item_for_registration(
1622
-     *                                                  $registration );`
1623
-     * @throws EE_Error
1624
-     * @throws InvalidArgumentException
1625
-     * @throws InvalidDataTypeException
1626
-     * @throws InvalidInterfaceException
1627
-     * @throws ReflectionException
1628
-     */
1629
-    public static function calculate_reg_final_prices_per_line_item(
1630
-        EE_Line_Item $line_item,
1631
-        $billable_ticket_quantities = array()
1632
-    ) {
1633
-        $running_totals = [
1634
-            'total'   => 0,
1635
-            'taxable' => ['total' => 0]
1636
-        ];
1637
-        foreach ($line_item->children() as $child_line_item) {
1638
-            switch ($child_line_item->type()) {
1639
-                case EEM_Line_Item::type_sub_total:
1640
-                    $running_totals_from_subtotal = EEH_Line_Item::calculate_reg_final_prices_per_line_item(
1641
-                        $child_line_item,
1642
-                        $billable_ticket_quantities
1643
-                    );
1644
-                    // combine arrays but preserve numeric keys
1645
-                    $running_totals = array_replace_recursive($running_totals_from_subtotal, $running_totals);
1646
-                    $running_totals['total'] += $running_totals_from_subtotal['total'];
1647
-                    $running_totals['taxable']['total'] += $running_totals_from_subtotal['taxable']['total'];
1648
-                    break;
1649
-
1650
-                case EEM_Line_Item::type_tax_sub_total:
1651
-                    // find how much the taxes percentage is
1652
-                    if ($child_line_item->percent() !== 0) {
1653
-                        $tax_percent_decimal = $child_line_item->percent() / 100;
1654
-                    } else {
1655
-                        $tax_percent_decimal = EE_Taxes::get_total_taxes_percentage() / 100;
1656
-                    }
1657
-                    // and apply to all the taxable totals, and add to the pretax totals
1658
-                    foreach ($running_totals as $line_item_id => $this_running_total) {
1659
-                        // "total" and "taxable" array key is an exception
1660
-                        if ($line_item_id === 'taxable') {
1661
-                            continue;
1662
-                        }
1663
-                        $taxable_total = $running_totals['taxable'][ $line_item_id ];
1664
-                        $running_totals[ $line_item_id ] += ($taxable_total * $tax_percent_decimal);
1665
-                    }
1666
-                    break;
1667
-
1668
-                case EEM_Line_Item::type_line_item:
1669
-                    // ticket line items or ????
1670
-                    if ($child_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET) {
1671
-                        // kk it's a ticket
1672
-                        if (isset($running_totals[ $child_line_item->ID() ])) {
1673
-                            // huh? that shouldn't happen.
1674
-                            $running_totals['total'] += $child_line_item->total();
1675
-                        } else {
1676
-                            // its not in our running totals yet. great.
1677
-                            if ($child_line_item->is_taxable()) {
1678
-                                $taxable_amount = $child_line_item->unit_price();
1679
-                            } else {
1680
-                                $taxable_amount = 0;
1681
-                            }
1682
-                            // are we only calculating totals for some tickets?
1683
-                            if (isset($billable_ticket_quantities[ $child_line_item->OBJ_ID() ])) {
1684
-                                $quantity = $billable_ticket_quantities[ $child_line_item->OBJ_ID() ];
1685
-                                $running_totals[ $child_line_item->ID() ] = $quantity
1686
-                                    ? $child_line_item->unit_price()
1687
-                                    : 0;
1688
-                                $running_totals['taxable'][ $child_line_item->ID() ] = $quantity
1689
-                                    ? $taxable_amount
1690
-                                    : 0;
1691
-                            } else {
1692
-                                $quantity = $child_line_item->quantity();
1693
-                                $running_totals[ $child_line_item->ID() ] = $child_line_item->unit_price();
1694
-                                $running_totals['taxable'][ $child_line_item->ID() ] = $taxable_amount;
1695
-                            }
1696
-                            $running_totals['taxable']['total'] += $taxable_amount * $quantity;
1697
-                            $running_totals['total'] += $child_line_item->unit_price() * $quantity;
1698
-                        }
1699
-                    } else {
1700
-                        // it's some other type of item added to the cart
1701
-                        // it should affect the running totals
1702
-                        // basically we want to convert it into a PERCENT modifier. Because
1703
-                        // more clearly affect all registration's final price equally
1704
-                        $line_items_percent_of_running_total = $running_totals['total'] > 0
1705
-                            ? ($child_line_item->total() / $running_totals['total']) + 1
1706
-                            : 1;
1707
-                        foreach ($running_totals as $line_item_id => $this_running_total) {
1708
-                            // the "taxable" array key is an exception
1709
-                            if ($line_item_id === 'taxable') {
1710
-                                continue;
1711
-                            }
1712
-                            // update the running totals
1713
-                            // yes this actually even works for the running grand total!
1714
-                            $running_totals[ $line_item_id ] =
1715
-                                $line_items_percent_of_running_total * $this_running_total;
1716
-
1717
-                            if ($child_line_item->is_taxable()) {
1718
-                                $running_totals['taxable'][ $line_item_id ] =
1719
-                                    $line_items_percent_of_running_total * $running_totals['taxable'][ $line_item_id ];
1720
-                            }
1721
-                        }
1722
-                    }
1723
-                    break;
1724
-            }
1725
-        }
1726
-        return $running_totals;
1727
-    }
1728
-
1729
-
1730
-    /**
1731
-     * @param EE_Line_Item $total_line_item
1732
-     * @param EE_Line_Item $ticket_line_item
1733
-     * @return float | null
1734
-     * @throws EE_Error
1735
-     * @throws InvalidArgumentException
1736
-     * @throws InvalidDataTypeException
1737
-     * @throws InvalidInterfaceException
1738
-     * @throws OutOfRangeException
1739
-     * @throws ReflectionException
1740
-     */
1741
-    public static function calculate_final_price_for_ticket_line_item(
1742
-        EE_Line_Item $total_line_item,
1743
-        EE_Line_Item $ticket_line_item
1744
-    ) {
1745
-        static $final_prices_per_ticket_line_item = array();
1746
-        if (empty($final_prices_per_ticket_line_item) || empty($final_prices_per_ticket_line_item[ $total_line_item->ID() ])) {
1747
-            $final_prices_per_ticket_line_item[ $total_line_item->ID() ] = EEH_Line_Item::calculate_reg_final_prices_per_line_item(
1748
-                $total_line_item
1749
-            );
1750
-        }
1751
-        // ok now find this new registration's final price
1752
-        if (isset($final_prices_per_ticket_line_item[ $total_line_item->ID() ][ $ticket_line_item->ID() ])) {
1753
-            return $final_prices_per_ticket_line_item[ $total_line_item->ID() ][ $ticket_line_item->ID() ];
1754
-        }
1755
-        $message = sprintf(
1756
-            esc_html__(
1757
-                'The final price for the ticket line item (ID:%1$d) could not be calculated.',
1758
-                'event_espresso'
1759
-            ),
1760
-            $ticket_line_item->ID()
1761
-        );
1762
-        if (WP_DEBUG) {
1763
-            $message .= '<br>' . print_r($final_prices_per_ticket_line_item, true);
1764
-            throw new OutOfRangeException($message);
1765
-        }
1766
-        EE_Log::instance()->log(__CLASS__, __FUNCTION__, $message);
1767
-        return null;
1768
-    }
1769
-
1770
-
1771
-    /**
1772
-     * Creates a duplicate of the line item tree, except only includes billable items
1773
-     * and the portion of line items attributed to billable things
1774
-     *
1775
-     * @param EE_Line_Item      $line_item
1776
-     * @param EE_Registration[] $registrations
1777
-     * @return EE_Line_Item
1778
-     * @throws EE_Error
1779
-     * @throws InvalidArgumentException
1780
-     * @throws InvalidDataTypeException
1781
-     * @throws InvalidInterfaceException
1782
-     * @throws ReflectionException
1783
-     */
1784
-    public static function billable_line_item_tree(EE_Line_Item $line_item, $registrations)
1785
-    {
1786
-        $copy_li = EEH_Line_Item::billable_line_item($line_item, $registrations);
1787
-        foreach ($line_item->children() as $child_li) {
1788
-            $copy_li->add_child_line_item(
1789
-                EEH_Line_Item::billable_line_item_tree($child_li, $registrations)
1790
-            );
1791
-        }
1792
-        // if this is the grand total line item, make sure the totals all add up
1793
-        // (we could have duplicated this logic AS we copied the line items, but
1794
-        // it seems DRYer this way)
1795
-        if ($copy_li->type() === EEM_Line_Item::type_total) {
1796
-            $copy_li->recalculate_total_including_taxes();
1797
-        }
1798
-        return $copy_li;
1799
-    }
1800
-
1801
-
1802
-    /**
1803
-     * Creates a new, unsaved line item from $line_item that factors in the
1804
-     * number of billable registrations on $registrations.
1805
-     *
1806
-     * @param EE_Line_Item      $line_item
1807
-     * @param EE_Registration[] $registrations
1808
-     * @return EE_Line_Item
1809
-     * @throws EE_Error
1810
-     * @throws InvalidArgumentException
1811
-     * @throws InvalidDataTypeException
1812
-     * @throws InvalidInterfaceException
1813
-     * @throws ReflectionException
1814
-     */
1815
-    public static function billable_line_item(EE_Line_Item $line_item, $registrations)
1816
-    {
1817
-        $new_li_fields = $line_item->model_field_array();
1818
-        if ($line_item->type() === EEM_Line_Item::type_line_item &&
1819
-            $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1820
-        ) {
1821
-            $count = 0;
1822
-            foreach ($registrations as $registration) {
1823
-                if ($line_item->OBJ_ID() === $registration->ticket_ID() &&
1824
-                    in_array(
1825
-                        $registration->status_ID(),
1826
-                        EEM_Registration::reg_statuses_that_allow_payment(),
1827
-                        true
1828
-                    )
1829
-                ) {
1830
-                    $count++;
1831
-                }
1832
-            }
1833
-            $new_li_fields['LIN_quantity'] = $count;
1834
-        }
1835
-        // don't set the total. We'll leave that up to the code that calculates it
1836
-        unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent'], $new_li_fields['LIN_total']);
1837
-        return EE_Line_Item::new_instance($new_li_fields);
1838
-    }
1839
-
1840
-
1841
-    /**
1842
-     * Returns a modified line item tree where all the subtotals which have a total of 0
1843
-     * are removed, and line items with a quantity of 0
1844
-     *
1845
-     * @param EE_Line_Item $line_item |null
1846
-     * @return EE_Line_Item|null
1847
-     * @throws EE_Error
1848
-     * @throws InvalidArgumentException
1849
-     * @throws InvalidDataTypeException
1850
-     * @throws InvalidInterfaceException
1851
-     * @throws ReflectionException
1852
-     */
1853
-    public static function non_empty_line_items(EE_Line_Item $line_item)
1854
-    {
1855
-        $copied_li = EEH_Line_Item::non_empty_line_item($line_item);
1856
-        if ($copied_li === null) {
1857
-            return null;
1858
-        }
1859
-        // if this is an event subtotal, we want to only include it if it
1860
-        // has a non-zero total and at least one ticket line item child
1861
-        $ticket_children = 0;
1862
-        foreach ($line_item->children() as $child_li) {
1863
-            $child_li_copy = EEH_Line_Item::non_empty_line_items($child_li);
1864
-            if ($child_li_copy !== null) {
1865
-                $copied_li->add_child_line_item($child_li_copy);
1866
-                if ($child_li_copy->type() === EEM_Line_Item::type_line_item &&
1867
-                    $child_li_copy->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1868
-                ) {
1869
-                    $ticket_children++;
1870
-                }
1871
-            }
1872
-        }
1873
-        // if this is an event subtotal with NO ticket children
1874
-        // we basically want to ignore it
1875
-        if ($ticket_children === 0
1876
-            && $line_item->type() === EEM_Line_Item::type_sub_total
1877
-            && $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT
1878
-            && $line_item->total() === 0
1879
-        ) {
1880
-            return null;
1881
-        }
1882
-        return $copied_li;
1883
-    }
1884
-
1885
-
1886
-    /**
1887
-     * Creates a new, unsaved line item, but if it's a ticket line item
1888
-     * with a total of 0, or a subtotal of 0, returns null instead
1889
-     *
1890
-     * @param EE_Line_Item $line_item
1891
-     * @return EE_Line_Item
1892
-     * @throws EE_Error
1893
-     * @throws InvalidArgumentException
1894
-     * @throws InvalidDataTypeException
1895
-     * @throws InvalidInterfaceException
1896
-     * @throws ReflectionException
1897
-     */
1898
-    public static function non_empty_line_item(EE_Line_Item $line_item)
1899
-    {
1900
-        if ($line_item->type() === EEM_Line_Item::type_line_item
1901
-            && $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1902
-            && $line_item->quantity() === 0
1903
-        ) {
1904
-            return null;
1905
-        }
1906
-        $new_li_fields = $line_item->model_field_array();
1907
-        // don't set the total. We'll leave that up to the code that calculates it
1908
-        unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent']);
1909
-        return EE_Line_Item::new_instance($new_li_fields);
1910
-    }
1911
-
1912
-
1913
-    /**
1914
-     * Cycles through all of the ticket line items for the supplied total line item
1915
-     * and ensures that the line item's "is_taxable" field matches that of its corresponding ticket
1916
-     *
1917
-     * @param EE_Line_Item $total_line_item
1918
-     * @since 4.9.79.p
1919
-     * @throws EE_Error
1920
-     * @throws InvalidArgumentException
1921
-     * @throws InvalidDataTypeException
1922
-     * @throws InvalidInterfaceException
1923
-     * @throws ReflectionException
1924
-     */
1925
-    public static function resetIsTaxableForTickets(EE_Line_Item $total_line_item)
1926
-    {
1927
-        $ticket_line_items = self::get_ticket_line_items($total_line_item);
1928
-        foreach ($ticket_line_items as $ticket_line_item) {
1929
-            if ($ticket_line_item instanceof EE_Line_Item
1930
-                && $ticket_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1931
-            ) {
1932
-                $ticket = $ticket_line_item->ticket();
1933
-                if ($ticket instanceof EE_Ticket && $ticket->taxable() !== $ticket_line_item->is_taxable()) {
1934
-                    $ticket_line_item->set_is_taxable($ticket->taxable());
1935
-                    $ticket_line_item->save();
1936
-                }
1937
-            }
1938
-        }
1939
-    }
1940
-
1941
-
1942
-
1943
-    /**************************************** @DEPRECATED METHODS *************************************** */
1944
-    /**
1945
-     * @deprecated
1946
-     * @param EE_Line_Item $total_line_item
1947
-     * @return EE_Line_Item
1948
-     * @throws EE_Error
1949
-     * @throws InvalidArgumentException
1950
-     * @throws InvalidDataTypeException
1951
-     * @throws InvalidInterfaceException
1952
-     * @throws ReflectionException
1953
-     */
1954
-    public static function get_items_subtotal(EE_Line_Item $total_line_item)
1955
-    {
1956
-        EE_Error::doing_it_wrong(
1957
-            'EEH_Line_Item::get_items_subtotal()',
1958
-            sprintf(
1959
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
1960
-                'EEH_Line_Item::get_pre_tax_subtotal()'
1961
-            ),
1962
-            '4.6.0'
1963
-        );
1964
-        return self::get_pre_tax_subtotal($total_line_item);
1965
-    }
1966
-
1967
-
1968
-    /**
1969
-     * @deprecated
1970
-     * @param EE_Transaction $transaction
1971
-     * @return EE_Line_Item
1972
-     * @throws EE_Error
1973
-     * @throws InvalidArgumentException
1974
-     * @throws InvalidDataTypeException
1975
-     * @throws InvalidInterfaceException
1976
-     * @throws ReflectionException
1977
-     */
1978
-    public static function create_default_total_line_item($transaction = null)
1979
-    {
1980
-        EE_Error::doing_it_wrong(
1981
-            'EEH_Line_Item::create_default_total_line_item()',
1982
-            sprintf(
1983
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
1984
-                'EEH_Line_Item::create_total_line_item()'
1985
-            ),
1986
-            '4.6.0'
1987
-        );
1988
-        return self::create_total_line_item($transaction);
1989
-    }
1990
-
1991
-
1992
-    /**
1993
-     * @deprecated
1994
-     * @param EE_Line_Item   $total_line_item
1995
-     * @param EE_Transaction $transaction
1996
-     * @return EE_Line_Item
1997
-     * @throws EE_Error
1998
-     * @throws InvalidArgumentException
1999
-     * @throws InvalidDataTypeException
2000
-     * @throws InvalidInterfaceException
2001
-     * @throws ReflectionException
2002
-     */
2003
-    public static function create_default_tickets_subtotal(EE_Line_Item $total_line_item, $transaction = null)
2004
-    {
2005
-        EE_Error::doing_it_wrong(
2006
-            'EEH_Line_Item::create_default_tickets_subtotal()',
2007
-            sprintf(
2008
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
2009
-                'EEH_Line_Item::create_pre_tax_subtotal()'
2010
-            ),
2011
-            '4.6.0'
2012
-        );
2013
-        return self::create_pre_tax_subtotal($total_line_item, $transaction);
2014
-    }
2015
-
2016
-
2017
-    /**
2018
-     * @deprecated
2019
-     * @param EE_Line_Item   $total_line_item
2020
-     * @param EE_Transaction $transaction
2021
-     * @return EE_Line_Item
2022
-     * @throws EE_Error
2023
-     * @throws InvalidArgumentException
2024
-     * @throws InvalidDataTypeException
2025
-     * @throws InvalidInterfaceException
2026
-     * @throws ReflectionException
2027
-     */
2028
-    public static function create_default_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null)
2029
-    {
2030
-        EE_Error::doing_it_wrong(
2031
-            'EEH_Line_Item::create_default_taxes_subtotal()',
2032
-            sprintf(
2033
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
2034
-                'EEH_Line_Item::create_taxes_subtotal()'
2035
-            ),
2036
-            '4.6.0'
2037
-        );
2038
-        return self::create_taxes_subtotal($total_line_item, $transaction);
2039
-    }
2040
-
2041
-
2042
-    /**
2043
-     * @deprecated
2044
-     * @param EE_Line_Item   $total_line_item
2045
-     * @param EE_Transaction $transaction
2046
-     * @return EE_Line_Item
2047
-     * @throws EE_Error
2048
-     * @throws InvalidArgumentException
2049
-     * @throws InvalidDataTypeException
2050
-     * @throws InvalidInterfaceException
2051
-     * @throws ReflectionException
2052
-     */
2053
-    public static function create_default_event_subtotal(EE_Line_Item $total_line_item, $transaction = null)
2054
-    {
2055
-        EE_Error::doing_it_wrong(
2056
-            'EEH_Line_Item::create_default_event_subtotal()',
2057
-            sprintf(
2058
-                esc_html__('Method replaced with %1$s', 'event_espresso'),
2059
-                'EEH_Line_Item::create_event_subtotal()'
2060
-            ),
2061
-            '4.6.0'
2062
-        );
2063
-        return self::create_event_subtotal($total_line_item, $transaction);
2064
-    }
24
+	/**
25
+	 * Adds a simple item (unrelated to any other model object) to the provided PARENT line item.
26
+	 * Does NOT automatically re-calculate the line item totals or update the related transaction.
27
+	 * You should call recalculate_total_including_taxes() on the grant total line item after this
28
+	 * to update the subtotals, and EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
29
+	 * to keep the registration final prices in-sync with the transaction's total.
30
+	 *
31
+	 * @param EE_Line_Item $parent_line_item
32
+	 * @param string       $name
33
+	 * @param float        $unit_price
34
+	 * @param string       $description
35
+	 * @param int          $quantity
36
+	 * @param boolean      $taxable
37
+	 * @param boolean      $code if set to a value, ensures there is only one line item with that code
38
+	 * @return boolean success
39
+	 * @throws EE_Error
40
+	 * @throws InvalidArgumentException
41
+	 * @throws InvalidDataTypeException
42
+	 * @throws InvalidInterfaceException
43
+	 * @throws ReflectionException
44
+	 */
45
+	public static function add_unrelated_item(
46
+		EE_Line_Item $parent_line_item,
47
+		$name,
48
+		$unit_price,
49
+		$description = '',
50
+		$quantity = 1,
51
+		$taxable = false,
52
+		$code = null
53
+	) {
54
+		$items_subtotal = self::get_pre_tax_subtotal($parent_line_item);
55
+		$line_item = EE_Line_Item::new_instance(array(
56
+			'LIN_name'       => $name,
57
+			'LIN_desc'       => $description,
58
+			'LIN_unit_price' => $unit_price,
59
+			'LIN_quantity'   => $quantity,
60
+			'LIN_percent'    => null,
61
+			'LIN_is_taxable' => $taxable,
62
+			'LIN_order'      => $items_subtotal instanceof EE_Line_Item ? count($items_subtotal->children()) : 0,
63
+			'LIN_total'      => (float) $unit_price * (int) $quantity,
64
+			'LIN_type'       => EEM_Line_Item::type_line_item,
65
+			'LIN_code'       => $code,
66
+		));
67
+		$line_item = apply_filters(
68
+			'FHEE__EEH_Line_Item__add_unrelated_item__line_item',
69
+			$line_item,
70
+			$parent_line_item
71
+		);
72
+		return self::add_item($parent_line_item, $line_item);
73
+	}
74
+
75
+
76
+	/**
77
+	 * Adds a simple item ( unrelated to any other model object) to the total line item,
78
+	 * in the correct spot in the line item tree. Does not automatically
79
+	 * re-calculate the line item totals, nor update the related transaction, nor upgrade the transaction's
80
+	 * registrations' final prices (which should probably change because of this).
81
+	 * You should call recalculate_total_including_taxes() on the grand total line item, then
82
+	 * update the transaction's total, and EE_Registration_Processor::update_registration_final_prices()
83
+	 * after using this, to keep the registration final prices in-sync with the transaction's total.
84
+	 *
85
+	 * @param EE_Line_Item $parent_line_item
86
+	 * @param string       $name
87
+	 * @param float        $percentage_amount
88
+	 * @param string       $description
89
+	 * @param boolean      $taxable
90
+	 * @return boolean success
91
+	 * @throws EE_Error
92
+	 */
93
+	public static function add_percentage_based_item(
94
+		EE_Line_Item $parent_line_item,
95
+		$name,
96
+		$percentage_amount,
97
+		$description = '',
98
+		$taxable = false
99
+	) {
100
+		$line_item = EE_Line_Item::new_instance(array(
101
+			'LIN_name'       => $name,
102
+			'LIN_desc'       => $description,
103
+			'LIN_unit_price' => 0,
104
+			'LIN_percent'    => $percentage_amount,
105
+			'LIN_quantity'   => 1,
106
+			'LIN_is_taxable' => $taxable,
107
+			'LIN_total'      => (float) ($percentage_amount * ($parent_line_item->total() / 100)),
108
+			'LIN_type'       => EEM_Line_Item::type_line_item,
109
+			'LIN_parent'     => $parent_line_item->ID(),
110
+		));
111
+		$line_item = apply_filters(
112
+			'FHEE__EEH_Line_Item__add_percentage_based_item__line_item',
113
+			$line_item
114
+		);
115
+		return $parent_line_item->add_child_line_item($line_item, false);
116
+	}
117
+
118
+
119
+	/**
120
+	 * Returns the new line item created by adding a purchase of the ticket
121
+	 * ensures that ticket line item is saved, and that cart total has been recalculated.
122
+	 * If this ticket has already been purchased, just increments its count.
123
+	 * Automatically re-calculates the line item totals and updates the related transaction. But
124
+	 * DOES NOT automatically upgrade the transaction's registrations' final prices (which
125
+	 * should probably change because of this).
126
+	 * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
127
+	 * after using this, to keep the registration final prices in-sync with the transaction's total.
128
+	 *
129
+	 * @param EE_Line_Item $total_line_item grand total line item of type EEM_Line_Item::type_total
130
+	 * @param EE_Ticket    $ticket
131
+	 * @param int          $qty
132
+	 * @return EE_Line_Item
133
+	 * @throws EE_Error
134
+	 * @throws InvalidArgumentException
135
+	 * @throws InvalidDataTypeException
136
+	 * @throws InvalidInterfaceException
137
+	 * @throws ReflectionException
138
+	 */
139
+	public static function add_ticket_purchase(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1)
140
+	{
141
+		if (! $total_line_item instanceof EE_Line_Item || ! $total_line_item->is_total()) {
142
+			throw new EE_Error(
143
+				sprintf(
144
+					esc_html__(
145
+						'A valid line item total is required in order to add tickets. A line item of type "%s" was passed.',
146
+						'event_espresso'
147
+					),
148
+					$ticket->ID(),
149
+					$total_line_item->ID()
150
+				)
151
+			);
152
+		}
153
+		// either increment the qty for an existing ticket
154
+		$line_item = self::increment_ticket_qty_if_already_in_cart($total_line_item, $ticket, $qty);
155
+		// or add a new one
156
+		if (! $line_item instanceof EE_Line_Item) {
157
+			$line_item = self::create_ticket_line_item($total_line_item, $ticket, $qty);
158
+		}
159
+		$total_line_item->recalculate_total_including_taxes();
160
+		return $line_item;
161
+	}
162
+
163
+
164
+	/**
165
+	 * Returns the new line item created by adding a purchase of the ticket
166
+	 *
167
+	 * @param EE_Line_Item $total_line_item
168
+	 * @param EE_Ticket    $ticket
169
+	 * @param int          $qty
170
+	 * @return EE_Line_Item
171
+	 * @throws EE_Error
172
+	 * @throws InvalidArgumentException
173
+	 * @throws InvalidDataTypeException
174
+	 * @throws InvalidInterfaceException
175
+	 * @throws ReflectionException
176
+	 */
177
+	public static function increment_ticket_qty_if_already_in_cart(
178
+		EE_Line_Item $total_line_item,
179
+		EE_Ticket $ticket,
180
+		$qty = 1
181
+	) {
182
+		$line_item = null;
183
+		if ($total_line_item instanceof EE_Line_Item && $total_line_item->is_total()) {
184
+			$ticket_line_items = EEH_Line_Item::get_ticket_line_items($total_line_item);
185
+			foreach ((array) $ticket_line_items as $ticket_line_item) {
186
+				if ($ticket_line_item instanceof EE_Line_Item
187
+					&& (int) $ticket_line_item->OBJ_ID() === (int) $ticket->ID()
188
+				) {
189
+					$line_item = $ticket_line_item;
190
+					break;
191
+				}
192
+			}
193
+		}
194
+		if ($line_item instanceof EE_Line_Item) {
195
+			EEH_Line_Item::increment_quantity($line_item, $qty);
196
+			return $line_item;
197
+		}
198
+		return null;
199
+	}
200
+
201
+
202
+	/**
203
+	 * Increments the line item and all its children's quantity by $qty (but percent line items are unaffected).
204
+	 * Does NOT save or recalculate other line items totals
205
+	 *
206
+	 * @param EE_Line_Item $line_item
207
+	 * @param int          $qty
208
+	 * @return void
209
+	 * @throws EE_Error
210
+	 * @throws InvalidArgumentException
211
+	 * @throws InvalidDataTypeException
212
+	 * @throws InvalidInterfaceException
213
+	 * @throws ReflectionException
214
+	 */
215
+	public static function increment_quantity(EE_Line_Item $line_item, $qty = 1)
216
+	{
217
+		if (! $line_item->is_percent()) {
218
+			$qty += $line_item->quantity();
219
+			$line_item->set_quantity($qty);
220
+			$line_item->set_total($line_item->unit_price() * $qty);
221
+			$line_item->save();
222
+		}
223
+		foreach ($line_item->children() as $child) {
224
+			if ($child->is_sub_line_item()) {
225
+				EEH_Line_Item::update_quantity($child, $qty);
226
+			}
227
+		}
228
+	}
229
+
230
+
231
+	/**
232
+	 * Decrements the line item and all its children's quantity by $qty (but percent line items are unaffected).
233
+	 * Does NOT save or recalculate other line items totals
234
+	 *
235
+	 * @param EE_Line_Item $line_item
236
+	 * @param int          $qty
237
+	 * @return void
238
+	 * @throws EE_Error
239
+	 * @throws InvalidArgumentException
240
+	 * @throws InvalidDataTypeException
241
+	 * @throws InvalidInterfaceException
242
+	 * @throws ReflectionException
243
+	 */
244
+	public static function decrement_quantity(EE_Line_Item $line_item, $qty = 1)
245
+	{
246
+		if (! $line_item->is_percent()) {
247
+			$qty = $line_item->quantity() - $qty;
248
+			$qty = max($qty, 0);
249
+			$line_item->set_quantity($qty);
250
+			$line_item->set_total($line_item->unit_price() * $qty);
251
+			$line_item->save();
252
+		}
253
+		foreach ($line_item->children() as $child) {
254
+			if ($child->is_sub_line_item()) {
255
+				EEH_Line_Item::update_quantity($child, $qty);
256
+			}
257
+		}
258
+	}
259
+
260
+
261
+	/**
262
+	 * Updates the line item and its children's quantities to the specified number.
263
+	 * Does NOT save them or recalculate totals.
264
+	 *
265
+	 * @param EE_Line_Item $line_item
266
+	 * @param int          $new_quantity
267
+	 * @throws EE_Error
268
+	 * @throws InvalidArgumentException
269
+	 * @throws InvalidDataTypeException
270
+	 * @throws InvalidInterfaceException
271
+	 * @throws ReflectionException
272
+	 */
273
+	public static function update_quantity(EE_Line_Item $line_item, $new_quantity)
274
+	{
275
+		if (! $line_item->is_percent()) {
276
+			$line_item->set_quantity($new_quantity);
277
+			$line_item->set_total($line_item->unit_price() * $new_quantity);
278
+			$line_item->save();
279
+		}
280
+		foreach ($line_item->children() as $child) {
281
+			if ($child->is_sub_line_item()) {
282
+				EEH_Line_Item::update_quantity($child, $new_quantity);
283
+			}
284
+		}
285
+	}
286
+
287
+
288
+	/**
289
+	 * Returns the new line item created by adding a purchase of the ticket
290
+	 *
291
+	 * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
292
+	 * @param EE_Ticket    $ticket
293
+	 * @param int          $qty
294
+	 * @return EE_Line_Item
295
+	 * @throws EE_Error
296
+	 * @throws InvalidArgumentException
297
+	 * @throws InvalidDataTypeException
298
+	 * @throws InvalidInterfaceException
299
+	 * @throws ReflectionException
300
+	 */
301
+	public static function create_ticket_line_item(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1)
302
+	{
303
+		$datetimes = $ticket->datetimes();
304
+		$first_datetime = reset($datetimes);
305
+		$first_datetime_name = esc_html__('Event', 'event_espresso');
306
+		if ($first_datetime instanceof EE_Datetime && $first_datetime->event() instanceof EE_Event) {
307
+			$first_datetime_name = $first_datetime->event()->name();
308
+		}
309
+		$event = sprintf(_x('(For %1$s)', '(For Event Name)', 'event_espresso'), $first_datetime_name);
310
+		// get event subtotal line
311
+		$events_sub_total = self::get_event_line_item_for_ticket($total_line_item, $ticket);
312
+		// add $ticket to cart
313
+		$line_item = EE_Line_Item::new_instance(array(
314
+			'LIN_name'       => $ticket->name(),
315
+			'LIN_desc'       => $ticket->description() !== '' ? $ticket->description() . ' ' . $event : $event,
316
+			'LIN_unit_price' => $ticket->price(),
317
+			'LIN_quantity'   => $qty,
318
+			'LIN_is_taxable' => $ticket->taxable(),
319
+			'LIN_order'      => count($events_sub_total->children()),
320
+			'LIN_total'      => $ticket->price() * $qty,
321
+			'LIN_type'       => EEM_Line_Item::type_line_item,
322
+			'OBJ_ID'         => $ticket->ID(),
323
+			'OBJ_type'       => EEM_Line_Item::OBJ_TYPE_TICKET,
324
+		));
325
+		$line_item = apply_filters(
326
+			'FHEE__EEH_Line_Item__create_ticket_line_item__line_item',
327
+			$line_item
328
+		);
329
+		$events_sub_total->add_child_line_item($line_item);
330
+		// now add the sub-line items
331
+		$running_total_for_ticket = 0;
332
+		foreach ($ticket->prices(array('order_by' => array('PRC_order' => 'ASC'))) as $price) {
333
+			$sign = $price->is_discount() ? -1 : 1;
334
+			$price_total = $price->is_percent()
335
+				? $running_total_for_ticket * $price->amount() / 100
336
+				: $price->amount() * $qty;
337
+			$sub_line_item = EE_Line_Item::new_instance(array(
338
+				'LIN_name'       => $price->name(),
339
+				'LIN_desc'       => $price->desc(),
340
+				'LIN_quantity'   => $price->is_percent() ? null : $qty,
341
+				'LIN_is_taxable' => false,
342
+				'LIN_order'      => $price->order(),
343
+				'LIN_total'      => $sign * $price_total,
344
+				'LIN_type'       => EEM_Line_Item::type_sub_line_item,
345
+				'OBJ_ID'         => $price->ID(),
346
+				'OBJ_type'       => EEM_Line_Item::OBJ_TYPE_PRICE,
347
+			));
348
+			$sub_line_item = apply_filters(
349
+				'FHEE__EEH_Line_Item__create_ticket_line_item__sub_line_item',
350
+				$sub_line_item
351
+			);
352
+			if ($price->is_percent()) {
353
+				$sub_line_item->set_percent($sign * $price->amount());
354
+			} else {
355
+				$sub_line_item->set_unit_price($sign * $price->amount());
356
+			}
357
+			$running_total_for_ticket += $price_total;
358
+			$line_item->add_child_line_item($sub_line_item);
359
+		}
360
+		return $line_item;
361
+	}
362
+
363
+
364
+	/**
365
+	 * Adds the specified item under the pre-tax-sub-total line item. Automatically
366
+	 * re-calculates the line item totals and updates the related transaction. But
367
+	 * DOES NOT automatically upgrade the transaction's registrations' final prices (which
368
+	 * should probably change because of this).
369
+	 * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
370
+	 * after using this, to keep the registration final prices in-sync with the transaction's total.
371
+	 *
372
+	 * @param EE_Line_Item $total_line_item
373
+	 * @param EE_Line_Item $item to be added
374
+	 * @return boolean
375
+	 * @throws EE_Error
376
+	 * @throws InvalidArgumentException
377
+	 * @throws InvalidDataTypeException
378
+	 * @throws InvalidInterfaceException
379
+	 * @throws ReflectionException
380
+	 */
381
+	public static function add_item(EE_Line_Item $total_line_item, EE_Line_Item $item)
382
+	{
383
+		$pre_tax_subtotal = self::get_pre_tax_subtotal($total_line_item);
384
+		if ($pre_tax_subtotal instanceof EE_Line_Item) {
385
+			$success = $pre_tax_subtotal->add_child_line_item($item);
386
+		} else {
387
+			return false;
388
+		}
389
+		$total_line_item->recalculate_total_including_taxes();
390
+		return $success;
391
+	}
392
+
393
+
394
+	/**
395
+	 * cancels an existing ticket line item,
396
+	 * by decrementing it's quantity by 1 and adding a new "type_cancellation" sub-line-item.
397
+	 * ALL totals and subtotals will NEED TO BE UPDATED after performing this action
398
+	 *
399
+	 * @param EE_Line_Item $ticket_line_item
400
+	 * @param int          $qty
401
+	 * @return bool success
402
+	 * @throws EE_Error
403
+	 * @throws InvalidArgumentException
404
+	 * @throws InvalidDataTypeException
405
+	 * @throws InvalidInterfaceException
406
+	 * @throws ReflectionException
407
+	 */
408
+	public static function cancel_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1)
409
+	{
410
+		// validate incoming line_item
411
+		if ($ticket_line_item->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_TICKET) {
412
+			throw new EE_Error(
413
+				sprintf(
414
+					esc_html__(
415
+						'The supplied line item must have an Object Type of "Ticket", not %1$s.',
416
+						'event_espresso'
417
+					),
418
+					$ticket_line_item->type()
419
+				)
420
+			);
421
+		}
422
+		if ($ticket_line_item->quantity() < $qty) {
423
+			throw new EE_Error(
424
+				sprintf(
425
+					esc_html__(
426
+						'Can not cancel %1$d ticket(s) because the supplied line item has a quantity of %2$d.',
427
+						'event_espresso'
428
+					),
429
+					$qty,
430
+					$ticket_line_item->quantity()
431
+				)
432
+			);
433
+		}
434
+		// decrement ticket quantity; don't rely on auto-fixing when recalculating totals to do this
435
+		$ticket_line_item->set_quantity($ticket_line_item->quantity() - $qty);
436
+		foreach ($ticket_line_item->children() as $child_line_item) {
437
+			if ($child_line_item->is_sub_line_item()
438
+				&& ! $child_line_item->is_percent()
439
+				&& ! $child_line_item->is_cancellation()
440
+			) {
441
+				$child_line_item->set_quantity($child_line_item->quantity() - $qty);
442
+			}
443
+		}
444
+		// get cancellation sub line item
445
+		$cancellation_line_item = EEH_Line_Item::get_descendants_of_type(
446
+			$ticket_line_item,
447
+			EEM_Line_Item::type_cancellation
448
+		);
449
+		$cancellation_line_item = reset($cancellation_line_item);
450
+		// verify that this ticket was indeed previously cancelled
451
+		if ($cancellation_line_item instanceof EE_Line_Item) {
452
+			// increment cancelled quantity
453
+			$cancellation_line_item->set_quantity($cancellation_line_item->quantity() + $qty);
454
+		} else {
455
+			// create cancellation sub line item
456
+			$cancellation_line_item = EE_Line_Item::new_instance(array(
457
+				'LIN_name'       => esc_html__('Cancellation', 'event_espresso'),
458
+				'LIN_desc'       => sprintf(
459
+					esc_html_x(
460
+						'Cancelled %1$s : %2$s',
461
+						'Cancelled Ticket Name : 2015-01-01 11:11',
462
+						'event_espresso'
463
+					),
464
+					$ticket_line_item->name(),
465
+					current_time(get_option('date_format') . ' ' . get_option('time_format'))
466
+				),
467
+				'LIN_unit_price' => 0, // $ticket_line_item->unit_price()
468
+				'LIN_quantity'   => $qty,
469
+				'LIN_is_taxable' => $ticket_line_item->is_taxable(),
470
+				'LIN_order'      => count($ticket_line_item->children()),
471
+				'LIN_total'      => 0, // $ticket_line_item->unit_price()
472
+				'LIN_type'       => EEM_Line_Item::type_cancellation,
473
+			));
474
+			$ticket_line_item->add_child_line_item($cancellation_line_item);
475
+		}
476
+		if ($ticket_line_item->save_this_and_descendants() > 0) {
477
+			// decrement parent line item quantity
478
+			$event_line_item = $ticket_line_item->parent();
479
+			if ($event_line_item instanceof EE_Line_Item
480
+				&& $event_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT
481
+			) {
482
+				$event_line_item->set_quantity($event_line_item->quantity() - $qty);
483
+				$event_line_item->save();
484
+			}
485
+			EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item);
486
+			return true;
487
+		}
488
+		return false;
489
+	}
490
+
491
+
492
+	/**
493
+	 * reinstates (un-cancels?) a previously canceled ticket line item,
494
+	 * by incrementing it's quantity by 1, and decrementing it's "type_cancellation" sub-line-item.
495
+	 * ALL totals and subtotals will NEED TO BE UPDATED after performing this action
496
+	 *
497
+	 * @param EE_Line_Item $ticket_line_item
498
+	 * @param int          $qty
499
+	 * @return bool success
500
+	 * @throws EE_Error
501
+	 * @throws InvalidArgumentException
502
+	 * @throws InvalidDataTypeException
503
+	 * @throws InvalidInterfaceException
504
+	 * @throws ReflectionException
505
+	 */
506
+	public static function reinstate_canceled_ticket_line_item(EE_Line_Item $ticket_line_item, $qty = 1)
507
+	{
508
+		// validate incoming line_item
509
+		if ($ticket_line_item->OBJ_type() !== EEM_Line_Item::OBJ_TYPE_TICKET) {
510
+			throw new EE_Error(
511
+				sprintf(
512
+					esc_html__(
513
+						'The supplied line item must have an Object Type of "Ticket", not %1$s.',
514
+						'event_espresso'
515
+					),
516
+					$ticket_line_item->type()
517
+				)
518
+			);
519
+		}
520
+		// get cancellation sub line item
521
+		$cancellation_line_item = EEH_Line_Item::get_descendants_of_type(
522
+			$ticket_line_item,
523
+			EEM_Line_Item::type_cancellation
524
+		);
525
+		$cancellation_line_item = reset($cancellation_line_item);
526
+		// verify that this ticket was indeed previously cancelled
527
+		if (! $cancellation_line_item instanceof EE_Line_Item) {
528
+			return false;
529
+		}
530
+		if ($cancellation_line_item->quantity() > $qty) {
531
+			// decrement cancelled quantity
532
+			$cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty);
533
+		} elseif ($cancellation_line_item->quantity() === $qty) {
534
+			// decrement cancelled quantity in case anyone still has the object kicking around
535
+			$cancellation_line_item->set_quantity($cancellation_line_item->quantity() - $qty);
536
+			// delete because quantity will end up as 0
537
+			$cancellation_line_item->delete();
538
+			// and attempt to destroy the object,
539
+			// even though PHP won't actually destroy it until it needs the memory
540
+			unset($cancellation_line_item);
541
+		} else {
542
+			// what ?!?! negative quantity ?!?!
543
+			throw new EE_Error(
544
+				sprintf(
545
+					esc_html__(
546
+						'Can not reinstate %1$d cancelled ticket(s) because the cancelled ticket quantity is only %2$d.',
547
+						'event_espresso'
548
+					),
549
+					$qty,
550
+					$cancellation_line_item->quantity()
551
+				)
552
+			);
553
+		}
554
+		// increment ticket quantity
555
+		$ticket_line_item->set_quantity($ticket_line_item->quantity() + $qty);
556
+		if ($ticket_line_item->save_this_and_descendants() > 0) {
557
+			// increment parent line item quantity
558
+			$event_line_item = $ticket_line_item->parent();
559
+			if ($event_line_item instanceof EE_Line_Item
560
+				&& $event_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT
561
+			) {
562
+				$event_line_item->set_quantity($event_line_item->quantity() + $qty);
563
+			}
564
+			EEH_Line_Item::get_grand_total_and_recalculate_everything($ticket_line_item);
565
+			return true;
566
+		}
567
+		return false;
568
+	}
569
+
570
+
571
+	/**
572
+	 * calls EEH_Line_Item::find_transaction_grand_total_for_line_item()
573
+	 * then EE_Line_Item::recalculate_total_including_taxes() on the result
574
+	 *
575
+	 * @param EE_Line_Item $line_item
576
+	 * @return float
577
+	 * @throws EE_Error
578
+	 * @throws InvalidArgumentException
579
+	 * @throws InvalidDataTypeException
580
+	 * @throws InvalidInterfaceException
581
+	 * @throws ReflectionException
582
+	 */
583
+	public static function get_grand_total_and_recalculate_everything(EE_Line_Item $line_item)
584
+	{
585
+		$grand_total_line_item = EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item);
586
+		return $grand_total_line_item->recalculate_total_including_taxes();
587
+	}
588
+
589
+
590
+	/**
591
+	 * Gets the line item which contains the subtotal of all the items
592
+	 *
593
+	 * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
594
+	 * @return EE_Line_Item
595
+	 * @throws EE_Error
596
+	 * @throws InvalidArgumentException
597
+	 * @throws InvalidDataTypeException
598
+	 * @throws InvalidInterfaceException
599
+	 * @throws ReflectionException
600
+	 */
601
+	public static function get_pre_tax_subtotal(EE_Line_Item $total_line_item)
602
+	{
603
+		$pre_tax_subtotal = $total_line_item->get_child_line_item('pre-tax-subtotal');
604
+		return $pre_tax_subtotal instanceof EE_Line_Item
605
+			? $pre_tax_subtotal
606
+			: self::create_pre_tax_subtotal($total_line_item);
607
+	}
608
+
609
+
610
+	/**
611
+	 * Gets the line item for the taxes subtotal
612
+	 *
613
+	 * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
614
+	 * @return EE_Line_Item
615
+	 * @throws EE_Error
616
+	 * @throws InvalidArgumentException
617
+	 * @throws InvalidDataTypeException
618
+	 * @throws InvalidInterfaceException
619
+	 * @throws ReflectionException
620
+	 */
621
+	public static function get_taxes_subtotal(EE_Line_Item $total_line_item)
622
+	{
623
+		$taxes = $total_line_item->get_child_line_item('taxes');
624
+		return $taxes ? $taxes : self::create_taxes_subtotal($total_line_item);
625
+	}
626
+
627
+
628
+	/**
629
+	 * sets the TXN ID on an EE_Line_Item if passed a valid EE_Transaction object
630
+	 *
631
+	 * @param EE_Line_Item   $line_item
632
+	 * @param EE_Transaction $transaction
633
+	 * @return void
634
+	 * @throws EE_Error
635
+	 * @throws InvalidArgumentException
636
+	 * @throws InvalidDataTypeException
637
+	 * @throws InvalidInterfaceException
638
+	 * @throws ReflectionException
639
+	 */
640
+	public static function set_TXN_ID(EE_Line_Item $line_item, $transaction = null)
641
+	{
642
+		if ($transaction) {
643
+			/** @type EEM_Transaction $EEM_Transaction */
644
+			$EEM_Transaction = EE_Registry::instance()->load_model('Transaction');
645
+			$TXN_ID = $EEM_Transaction->ensure_is_ID($transaction);
646
+			$line_item->set_TXN_ID($TXN_ID);
647
+		}
648
+	}
649
+
650
+
651
+	/**
652
+	 * Creates a new default total line item for the transaction,
653
+	 * and its tickets subtotal and taxes subtotal line items (and adds the
654
+	 * existing taxes as children of the taxes subtotal line item)
655
+	 *
656
+	 * @param EE_Transaction $transaction
657
+	 * @return EE_Line_Item of type total
658
+	 * @throws EE_Error
659
+	 * @throws InvalidArgumentException
660
+	 * @throws InvalidDataTypeException
661
+	 * @throws InvalidInterfaceException
662
+	 * @throws ReflectionException
663
+	 */
664
+	public static function create_total_line_item($transaction = null)
665
+	{
666
+		$total_line_item = EE_Line_Item::new_instance(array(
667
+			'LIN_code' => 'total',
668
+			'LIN_name' => esc_html__('Grand Total', 'event_espresso'),
669
+			'LIN_type' => EEM_Line_Item::type_total,
670
+			'OBJ_type' => EEM_Line_Item::OBJ_TYPE_TRANSACTION,
671
+		));
672
+		$total_line_item = apply_filters(
673
+			'FHEE__EEH_Line_Item__create_total_line_item__total_line_item',
674
+			$total_line_item
675
+		);
676
+		self::set_TXN_ID($total_line_item, $transaction);
677
+		self::create_pre_tax_subtotal($total_line_item, $transaction);
678
+		self::create_taxes_subtotal($total_line_item, $transaction);
679
+		return $total_line_item;
680
+	}
681
+
682
+
683
+	/**
684
+	 * Creates a default items subtotal line item
685
+	 *
686
+	 * @param EE_Line_Item   $total_line_item
687
+	 * @param EE_Transaction $transaction
688
+	 * @return EE_Line_Item
689
+	 * @throws EE_Error
690
+	 * @throws InvalidArgumentException
691
+	 * @throws InvalidDataTypeException
692
+	 * @throws InvalidInterfaceException
693
+	 * @throws ReflectionException
694
+	 */
695
+	protected static function create_pre_tax_subtotal(EE_Line_Item $total_line_item, $transaction = null)
696
+	{
697
+		$pre_tax_line_item = EE_Line_Item::new_instance(array(
698
+			'LIN_code' => 'pre-tax-subtotal',
699
+			'LIN_name' => esc_html__('Pre-Tax Subtotal', 'event_espresso'),
700
+			'LIN_type' => EEM_Line_Item::type_sub_total,
701
+		));
702
+		$pre_tax_line_item = apply_filters(
703
+			'FHEE__EEH_Line_Item__create_pre_tax_subtotal__pre_tax_line_item',
704
+			$pre_tax_line_item
705
+		);
706
+		self::set_TXN_ID($pre_tax_line_item, $transaction);
707
+		$total_line_item->add_child_line_item($pre_tax_line_item);
708
+		self::create_event_subtotal($pre_tax_line_item, $transaction);
709
+		return $pre_tax_line_item;
710
+	}
711
+
712
+
713
+	/**
714
+	 * Creates a line item for the taxes subtotal and finds all the tax prices
715
+	 * and applies taxes to it
716
+	 *
717
+	 * @param EE_Line_Item   $total_line_item of type EEM_Line_Item::type_total
718
+	 * @param EE_Transaction $transaction
719
+	 * @return EE_Line_Item
720
+	 * @throws EE_Error
721
+	 * @throws InvalidArgumentException
722
+	 * @throws InvalidDataTypeException
723
+	 * @throws InvalidInterfaceException
724
+	 * @throws ReflectionException
725
+	 */
726
+	protected static function create_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null)
727
+	{
728
+		$tax_line_item = EE_Line_Item::new_instance(array(
729
+			'LIN_code'  => 'taxes',
730
+			'LIN_name'  => esc_html__('Taxes', 'event_espresso'),
731
+			'LIN_type'  => EEM_Line_Item::type_tax_sub_total,
732
+			'LIN_order' => 1000,// this should always come last
733
+		));
734
+		$tax_line_item = apply_filters(
735
+			'FHEE__EEH_Line_Item__create_taxes_subtotal__tax_line_item',
736
+			$tax_line_item
737
+		);
738
+		self::set_TXN_ID($tax_line_item, $transaction);
739
+		$total_line_item->add_child_line_item($tax_line_item);
740
+		// and lastly, add the actual taxes
741
+		self::apply_taxes($total_line_item);
742
+		return $tax_line_item;
743
+	}
744
+
745
+
746
+	/**
747
+	 * Creates a default items subtotal line item
748
+	 *
749
+	 * @param EE_Line_Item   $pre_tax_line_item
750
+	 * @param EE_Transaction $transaction
751
+	 * @param EE_Event       $event
752
+	 * @return EE_Line_Item
753
+	 * @throws EE_Error
754
+	 * @throws InvalidArgumentException
755
+	 * @throws InvalidDataTypeException
756
+	 * @throws InvalidInterfaceException
757
+	 * @throws ReflectionException
758
+	 */
759
+	public static function create_event_subtotal(EE_Line_Item $pre_tax_line_item, $transaction = null, $event = null)
760
+	{
761
+		$event_line_item = EE_Line_Item::new_instance(array(
762
+			'LIN_code' => self::get_event_code($event),
763
+			'LIN_name' => self::get_event_name($event),
764
+			'LIN_desc' => self::get_event_desc($event),
765
+			'LIN_type' => EEM_Line_Item::type_sub_total,
766
+			'OBJ_type' => EEM_Line_Item::OBJ_TYPE_EVENT,
767
+			'OBJ_ID'   => $event instanceof EE_Event ? $event->ID() : 0,
768
+		));
769
+		$event_line_item = apply_filters(
770
+			'FHEE__EEH_Line_Item__create_event_subtotal__event_line_item',
771
+			$event_line_item
772
+		);
773
+		self::set_TXN_ID($event_line_item, $transaction);
774
+		$pre_tax_line_item->add_child_line_item($event_line_item);
775
+		return $event_line_item;
776
+	}
777
+
778
+
779
+	/**
780
+	 * Gets what the event ticket's code SHOULD be
781
+	 *
782
+	 * @param EE_Event $event
783
+	 * @return string
784
+	 * @throws EE_Error
785
+	 */
786
+	public static function get_event_code($event)
787
+	{
788
+		return 'event-' . ($event instanceof EE_Event ? $event->ID() : '0');
789
+	}
790
+
791
+
792
+	/**
793
+	 * Gets the event name
794
+	 *
795
+	 * @param EE_Event $event
796
+	 * @return string
797
+	 * @throws EE_Error
798
+	 */
799
+	public static function get_event_name($event)
800
+	{
801
+		return $event instanceof EE_Event
802
+			? mb_substr($event->name(), 0, 245)
803
+			: esc_html__('Event', 'event_espresso');
804
+	}
805
+
806
+
807
+	/**
808
+	 * Gets the event excerpt
809
+	 *
810
+	 * @param EE_Event $event
811
+	 * @return string
812
+	 * @throws EE_Error
813
+	 */
814
+	public static function get_event_desc($event)
815
+	{
816
+		return $event instanceof EE_Event ? $event->short_description() : '';
817
+	}
818
+
819
+
820
+	/**
821
+	 * Given the grand total line item and a ticket, finds the event sub-total
822
+	 * line item the ticket's purchase should be added onto
823
+	 *
824
+	 * @access public
825
+	 * @param EE_Line_Item $grand_total the grand total line item
826
+	 * @param EE_Ticket    $ticket
827
+	 * @return EE_Line_Item
828
+	 * @throws EE_Error
829
+	 * @throws InvalidArgumentException
830
+	 * @throws InvalidDataTypeException
831
+	 * @throws InvalidInterfaceException
832
+	 * @throws ReflectionException
833
+	 */
834
+	public static function get_event_line_item_for_ticket(EE_Line_Item $grand_total, EE_Ticket $ticket)
835
+	{
836
+		$first_datetime = $ticket->first_datetime();
837
+		if (! $first_datetime instanceof EE_Datetime) {
838
+			throw new EE_Error(
839
+				sprintf(
840
+					__('The supplied ticket (ID %d) has no datetimes', 'event_espresso'),
841
+					$ticket->ID()
842
+				)
843
+			);
844
+		}
845
+		$event = $first_datetime->event();
846
+		if (! $event instanceof EE_Event) {
847
+			throw new EE_Error(
848
+				sprintf(
849
+					esc_html__(
850
+						'The supplied ticket (ID %d) has no event data associated with it.',
851
+						'event_espresso'
852
+					),
853
+					$ticket->ID()
854
+				)
855
+			);
856
+		}
857
+		$events_sub_total = EEH_Line_Item::get_event_line_item($grand_total, $event);
858
+		if (! $events_sub_total instanceof EE_Line_Item) {
859
+			throw new EE_Error(
860
+				sprintf(
861
+					esc_html__(
862
+						'There is no events sub-total for ticket %s on total line item %d',
863
+						'event_espresso'
864
+					),
865
+					$ticket->ID(),
866
+					$grand_total->ID()
867
+				)
868
+			);
869
+		}
870
+		return $events_sub_total;
871
+	}
872
+
873
+
874
+	/**
875
+	 * Gets the event line item
876
+	 *
877
+	 * @param EE_Line_Item $grand_total
878
+	 * @param EE_Event     $event
879
+	 * @return EE_Line_Item for the event subtotal which is a child of $grand_total
880
+	 * @throws EE_Error
881
+	 * @throws InvalidArgumentException
882
+	 * @throws InvalidDataTypeException
883
+	 * @throws InvalidInterfaceException
884
+	 * @throws ReflectionException
885
+	 */
886
+	public static function get_event_line_item(EE_Line_Item $grand_total, $event)
887
+	{
888
+		/** @type EE_Event $event */
889
+		$event = EEM_Event::instance()->ensure_is_obj($event, true);
890
+		$event_line_item = null;
891
+		$found = false;
892
+		foreach (EEH_Line_Item::get_event_subtotals($grand_total) as $event_line_item) {
893
+			// default event subtotal, we should only ever find this the first time this method is called
894
+			if (! $event_line_item->OBJ_ID()) {
895
+				// let's use this! but first... set the event details
896
+				EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
897
+				$found = true;
898
+				break;
899
+			}
900
+			if ($event_line_item->OBJ_ID() === $event->ID()) {
901
+				// found existing line item for this event in the cart, so break out of loop and use this one
902
+				$found = true;
903
+				break;
904
+			}
905
+		}
906
+		if (! $found) {
907
+			// there is no event sub-total yet, so add it
908
+			$pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($grand_total);
909
+			// create a new "event" subtotal below that
910
+			$event_line_item = EEH_Line_Item::create_event_subtotal($pre_tax_subtotal, null, $event);
911
+			// and set the event details
912
+			EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
913
+		}
914
+		return $event_line_item;
915
+	}
916
+
917
+
918
+	/**
919
+	 * Creates a default items subtotal line item
920
+	 *
921
+	 * @param EE_Line_Item   $event_line_item
922
+	 * @param EE_Event       $event
923
+	 * @param EE_Transaction $transaction
924
+	 * @return void
925
+	 * @throws EE_Error
926
+	 * @throws InvalidArgumentException
927
+	 * @throws InvalidDataTypeException
928
+	 * @throws InvalidInterfaceException
929
+	 * @throws ReflectionException
930
+	 */
931
+	public static function set_event_subtotal_details(
932
+		EE_Line_Item $event_line_item,
933
+		EE_Event $event,
934
+		$transaction = null
935
+	) {
936
+		if ($event instanceof EE_Event) {
937
+			$event_line_item->set_code(self::get_event_code($event));
938
+			$event_line_item->set_name(self::get_event_name($event));
939
+			$event_line_item->set_desc(self::get_event_desc($event));
940
+			$event_line_item->set_OBJ_ID($event->ID());
941
+		}
942
+		self::set_TXN_ID($event_line_item, $transaction);
943
+	}
944
+
945
+
946
+	/**
947
+	 * Finds what taxes should apply, adds them as tax line items under the taxes sub-total,
948
+	 * and recalculates the taxes sub-total and the grand total. Resets the taxes, so
949
+	 * any old taxes are removed
950
+	 *
951
+	 * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
952
+	 * @param bool         $update_txn_status
953
+	 * @return bool
954
+	 * @throws EE_Error
955
+	 * @throws InvalidArgumentException
956
+	 * @throws InvalidDataTypeException
957
+	 * @throws InvalidInterfaceException
958
+	 * @throws ReflectionException
959
+	 * @throws RuntimeException
960
+	 */
961
+	public static function apply_taxes(EE_Line_Item $total_line_item, $update_txn_status = false)
962
+	{
963
+		/** @type EEM_Price $EEM_Price */
964
+		$EEM_Price = EE_Registry::instance()->load_model('Price');
965
+		// get array of taxes via Price Model
966
+		$ordered_taxes = $EEM_Price->get_all_prices_that_are_taxes();
967
+		ksort($ordered_taxes);
968
+		$taxes_line_item = self::get_taxes_subtotal($total_line_item);
969
+		// just to be safe, remove its old tax line items
970
+		$deleted = $taxes_line_item->delete_children_line_items();
971
+		$updates = false;
972
+		// loop thru taxes
973
+		foreach ($ordered_taxes as $order => $taxes) {
974
+			foreach ($taxes as $tax) {
975
+				if ($tax instanceof EE_Price) {
976
+					$tax_line_item = EE_Line_Item::new_instance(
977
+						array(
978
+							'LIN_name'       => $tax->name(),
979
+							'LIN_desc'       => $tax->desc(),
980
+							'LIN_percent'    => $tax->amount(),
981
+							'LIN_is_taxable' => false,
982
+							'LIN_order'      => $order,
983
+							'LIN_total'      => 0,
984
+							'LIN_type'       => EEM_Line_Item::type_tax,
985
+							'OBJ_type'       => EEM_Line_Item::OBJ_TYPE_PRICE,
986
+							'OBJ_ID'         => $tax->ID(),
987
+						)
988
+					);
989
+					$tax_line_item = apply_filters(
990
+						'FHEE__EEH_Line_Item__apply_taxes__tax_line_item',
991
+						$tax_line_item
992
+					);
993
+					$updates = $taxes_line_item->add_child_line_item($tax_line_item) ?
994
+						true :
995
+						$updates;
996
+				}
997
+			}
998
+		}
999
+		// only recalculate totals if something changed
1000
+		if ($deleted || $updates) {
1001
+			$total_line_item->recalculate_total_including_taxes($update_txn_status);
1002
+			return true;
1003
+		}
1004
+		return false;
1005
+	}
1006
+
1007
+
1008
+	/**
1009
+	 * Ensures that taxes have been applied to the order, if not applies them.
1010
+	 * Returns the total amount of tax
1011
+	 *
1012
+	 * @param EE_Line_Item $total_line_item of type EEM_Line_Item::type_total
1013
+	 * @return float
1014
+	 * @throws EE_Error
1015
+	 * @throws InvalidArgumentException
1016
+	 * @throws InvalidDataTypeException
1017
+	 * @throws InvalidInterfaceException
1018
+	 * @throws ReflectionException
1019
+	 */
1020
+	public static function ensure_taxes_applied($total_line_item)
1021
+	{
1022
+		$taxes_subtotal = self::get_taxes_subtotal($total_line_item);
1023
+		if (! $taxes_subtotal->children()) {
1024
+			self::apply_taxes($total_line_item);
1025
+		}
1026
+		return $taxes_subtotal->total();
1027
+	}
1028
+
1029
+
1030
+	/**
1031
+	 * Deletes ALL children of the passed line item
1032
+	 *
1033
+	 * @param EE_Line_Item $parent_line_item
1034
+	 * @return bool
1035
+	 * @throws EE_Error
1036
+	 * @throws InvalidArgumentException
1037
+	 * @throws InvalidDataTypeException
1038
+	 * @throws InvalidInterfaceException
1039
+	 * @throws ReflectionException
1040
+	 */
1041
+	public static function delete_all_child_items(EE_Line_Item $parent_line_item)
1042
+	{
1043
+		$deleted = 0;
1044
+		foreach ($parent_line_item->children() as $child_line_item) {
1045
+			if ($child_line_item instanceof EE_Line_Item) {
1046
+				$deleted += EEH_Line_Item::delete_all_child_items($child_line_item);
1047
+				if ($child_line_item->ID()) {
1048
+					$child_line_item->delete();
1049
+					unset($child_line_item);
1050
+				} else {
1051
+					$parent_line_item->delete_child_line_item($child_line_item->code());
1052
+				}
1053
+				$deleted++;
1054
+			}
1055
+		}
1056
+		return $deleted;
1057
+	}
1058
+
1059
+
1060
+	/**
1061
+	 * Deletes the line items as indicated by the line item code(s) provided,
1062
+	 * regardless of where they're found in the line item tree. Automatically
1063
+	 * re-calculates the line item totals and updates the related transaction. But
1064
+	 * DOES NOT automatically upgrade the transaction's registrations' final prices (which
1065
+	 * should probably change because of this).
1066
+	 * You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item()
1067
+	 * after using this, to keep the registration final prices in-sync with the transaction's total.
1068
+	 *
1069
+	 * @param EE_Line_Item      $total_line_item of type EEM_Line_Item::type_total
1070
+	 * @param array|bool|string $line_item_codes
1071
+	 * @return int number of items successfully removed
1072
+	 * @throws EE_Error
1073
+	 */
1074
+	public static function delete_items(EE_Line_Item $total_line_item, $line_item_codes = false)
1075
+	{
1076
+
1077
+		if ($total_line_item->type() !== EEM_Line_Item::type_total) {
1078
+			EE_Error::doing_it_wrong(
1079
+				'EEH_Line_Item::delete_items',
1080
+				esc_html__(
1081
+					'This static method should only be called with a TOTAL line item, otherwise we won\'t recalculate the totals correctly',
1082
+					'event_espresso'
1083
+				),
1084
+				'4.6.18'
1085
+			);
1086
+		}
1087
+		do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1088
+
1089
+		// check if only a single line_item_id was passed
1090
+		if (! empty($line_item_codes) && ! is_array($line_item_codes)) {
1091
+			// place single line_item_id in an array to appear as multiple line_item_ids
1092
+			$line_item_codes = array($line_item_codes);
1093
+		}
1094
+		$removals = 0;
1095
+		// cycle thru line_item_ids
1096
+		foreach ($line_item_codes as $line_item_id) {
1097
+			$removals += $total_line_item->delete_child_line_item($line_item_id);
1098
+		}
1099
+
1100
+		if ($removals > 0) {
1101
+			$total_line_item->recalculate_taxes_and_tax_total();
1102
+			return $removals;
1103
+		} else {
1104
+			return false;
1105
+		}
1106
+	}
1107
+
1108
+
1109
+	/**
1110
+	 * Overwrites the previous tax by clearing out the old taxes, and creates a new
1111
+	 * tax and updates the total line item accordingly
1112
+	 *
1113
+	 * @param EE_Line_Item $total_line_item
1114
+	 * @param float        $amount
1115
+	 * @param string       $name
1116
+	 * @param string       $description
1117
+	 * @param string       $code
1118
+	 * @param boolean      $add_to_existing_line_item
1119
+	 *                          if true, and a duplicate line item with the same code is found,
1120
+	 *                          $amount will be added onto it; otherwise will simply set the taxes to match $amount
1121
+	 * @return EE_Line_Item the new tax line item created
1122
+	 * @throws EE_Error
1123
+	 * @throws InvalidArgumentException
1124
+	 * @throws InvalidDataTypeException
1125
+	 * @throws InvalidInterfaceException
1126
+	 * @throws ReflectionException
1127
+	 */
1128
+	public static function set_total_tax_to(
1129
+		EE_Line_Item $total_line_item,
1130
+		$amount,
1131
+		$name = null,
1132
+		$description = null,
1133
+		$code = null,
1134
+		$add_to_existing_line_item = false
1135
+	) {
1136
+		$tax_subtotal = self::get_taxes_subtotal($total_line_item);
1137
+		$taxable_total = $total_line_item->taxable_total();
1138
+
1139
+		if ($add_to_existing_line_item) {
1140
+			$new_tax = $tax_subtotal->get_child_line_item($code);
1141
+			EEM_Line_Item::instance()->delete(
1142
+				array(array('LIN_code' => array('!=', $code), 'LIN_parent' => $tax_subtotal->ID()))
1143
+			);
1144
+		} else {
1145
+			$new_tax = null;
1146
+			$tax_subtotal->delete_children_line_items();
1147
+		}
1148
+		if ($new_tax) {
1149
+			$new_tax->set_total($new_tax->total() + $amount);
1150
+			$new_tax->set_percent($taxable_total ? $new_tax->total() / $taxable_total * 100 : 0);
1151
+		} else {
1152
+			// no existing tax item. Create it
1153
+			$new_tax = EE_Line_Item::new_instance(array(
1154
+				'TXN_ID'      => $total_line_item->TXN_ID(),
1155
+				'LIN_name'    => $name ? $name : esc_html__('Tax', 'event_espresso'),
1156
+				'LIN_desc'    => $description ? $description : '',
1157
+				'LIN_percent' => $taxable_total ? ($amount / $taxable_total * 100) : 0,
1158
+				'LIN_total'   => $amount,
1159
+				'LIN_parent'  => $tax_subtotal->ID(),
1160
+				'LIN_type'    => EEM_Line_Item::type_tax,
1161
+				'LIN_code'    => $code,
1162
+			));
1163
+		}
1164
+
1165
+		$new_tax = apply_filters(
1166
+			'FHEE__EEH_Line_Item__set_total_tax_to__new_tax_subtotal',
1167
+			$new_tax,
1168
+			$total_line_item
1169
+		);
1170
+		$new_tax->save();
1171
+		$tax_subtotal->set_total($new_tax->total());
1172
+		$tax_subtotal->save();
1173
+		$total_line_item->recalculate_total_including_taxes();
1174
+		return $new_tax;
1175
+	}
1176
+
1177
+
1178
+	/**
1179
+	 * Makes all the line items which are children of $line_item taxable (or not).
1180
+	 * Does NOT save the line items
1181
+	 *
1182
+	 * @param EE_Line_Item $line_item
1183
+	 * @param boolean      $taxable
1184
+	 * @param string       $code_substring_for_whitelist if this string is part of the line item's code
1185
+	 *                                                   it will be whitelisted (ie, except from becoming taxable)
1186
+	 * @throws EE_Error
1187
+	 */
1188
+	public static function set_line_items_taxable(
1189
+		EE_Line_Item $line_item,
1190
+		$taxable = true,
1191
+		$code_substring_for_whitelist = null
1192
+	) {
1193
+		$whitelisted = false;
1194
+		if ($code_substring_for_whitelist !== null) {
1195
+			$whitelisted = strpos($line_item->code(), $code_substring_for_whitelist) !== false;
1196
+		}
1197
+		if (! $whitelisted && $line_item->is_line_item()) {
1198
+			$line_item->set_is_taxable($taxable);
1199
+		}
1200
+		foreach ($line_item->children() as $child_line_item) {
1201
+			EEH_Line_Item::set_line_items_taxable(
1202
+				$child_line_item,
1203
+				$taxable,
1204
+				$code_substring_for_whitelist
1205
+			);
1206
+		}
1207
+	}
1208
+
1209
+
1210
+	/**
1211
+	 * Gets all descendants that are event subtotals
1212
+	 *
1213
+	 * @uses  EEH_Line_Item::get_subtotals_of_object_type()
1214
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1215
+	 * @return EE_Line_Item[]
1216
+	 * @throws EE_Error
1217
+	 */
1218
+	public static function get_event_subtotals(EE_Line_Item $parent_line_item)
1219
+	{
1220
+		return self::get_subtotals_of_object_type($parent_line_item, EEM_Line_Item::OBJ_TYPE_EVENT);
1221
+	}
1222
+
1223
+
1224
+	/**
1225
+	 * Gets all descendants subtotals that match the supplied object type
1226
+	 *
1227
+	 * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1228
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1229
+	 * @param string       $obj_type
1230
+	 * @return EE_Line_Item[]
1231
+	 * @throws EE_Error
1232
+	 */
1233
+	public static function get_subtotals_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '')
1234
+	{
1235
+		return self::_get_descendants_by_type_and_object_type(
1236
+			$parent_line_item,
1237
+			EEM_Line_Item::type_sub_total,
1238
+			$obj_type
1239
+		);
1240
+	}
1241
+
1242
+
1243
+	/**
1244
+	 * Gets all descendants that are tickets
1245
+	 *
1246
+	 * @uses  EEH_Line_Item::get_line_items_of_object_type()
1247
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1248
+	 * @return EE_Line_Item[]
1249
+	 * @throws EE_Error
1250
+	 */
1251
+	public static function get_ticket_line_items(EE_Line_Item $parent_line_item)
1252
+	{
1253
+		return self::get_line_items_of_object_type(
1254
+			$parent_line_item,
1255
+			EEM_Line_Item::OBJ_TYPE_TICKET
1256
+		);
1257
+	}
1258
+
1259
+
1260
+	/**
1261
+	 * Gets all descendants subtotals that match the supplied object type
1262
+	 *
1263
+	 * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1264
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1265
+	 * @param string       $obj_type
1266
+	 * @return EE_Line_Item[]
1267
+	 * @throws EE_Error
1268
+	 */
1269
+	public static function get_line_items_of_object_type(EE_Line_Item $parent_line_item, $obj_type = '')
1270
+	{
1271
+		return self::_get_descendants_by_type_and_object_type(
1272
+			$parent_line_item,
1273
+			EEM_Line_Item::type_line_item,
1274
+			$obj_type
1275
+		);
1276
+	}
1277
+
1278
+
1279
+	/**
1280
+	 * Gets all the descendants (ie, children or children of children etc) that are of the type 'tax'
1281
+	 *
1282
+	 * @uses  EEH_Line_Item::get_descendants_of_type()
1283
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1284
+	 * @return EE_Line_Item[]
1285
+	 * @throws EE_Error
1286
+	 */
1287
+	public static function get_tax_descendants(EE_Line_Item $parent_line_item)
1288
+	{
1289
+		return EEH_Line_Item::get_descendants_of_type(
1290
+			$parent_line_item,
1291
+			EEM_Line_Item::type_tax
1292
+		);
1293
+	}
1294
+
1295
+
1296
+	/**
1297
+	 * Gets all the real items purchased which are children of this item
1298
+	 *
1299
+	 * @uses  EEH_Line_Item::get_descendants_of_type()
1300
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1301
+	 * @return EE_Line_Item[]
1302
+	 * @throws EE_Error
1303
+	 */
1304
+	public static function get_line_item_descendants(EE_Line_Item $parent_line_item)
1305
+	{
1306
+		return EEH_Line_Item::get_descendants_of_type(
1307
+			$parent_line_item,
1308
+			EEM_Line_Item::type_line_item
1309
+		);
1310
+	}
1311
+
1312
+
1313
+	/**
1314
+	 * Gets all descendants of supplied line item that match the supplied line item type
1315
+	 *
1316
+	 * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1317
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1318
+	 * @param string       $line_item_type   one of the EEM_Line_Item constants
1319
+	 * @return EE_Line_Item[]
1320
+	 * @throws EE_Error
1321
+	 */
1322
+	public static function get_descendants_of_type(EE_Line_Item $parent_line_item, $line_item_type)
1323
+	{
1324
+		return self::_get_descendants_by_type_and_object_type(
1325
+			$parent_line_item,
1326
+			$line_item_type,
1327
+			null
1328
+		);
1329
+	}
1330
+
1331
+
1332
+	/**
1333
+	 * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type
1334
+	 * as well
1335
+	 *
1336
+	 * @param EE_Line_Item  $parent_line_item - the line item to find descendants of
1337
+	 * @param string        $line_item_type   one of the EEM_Line_Item constants
1338
+	 * @param string | NULL $obj_type         object model class name (minus prefix) or NULL to ignore object type when
1339
+	 *                                        searching
1340
+	 * @return EE_Line_Item[]
1341
+	 * @throws EE_Error
1342
+	 */
1343
+	protected static function _get_descendants_by_type_and_object_type(
1344
+		EE_Line_Item $parent_line_item,
1345
+		$line_item_type,
1346
+		$obj_type = null
1347
+	) {
1348
+		$objects = array();
1349
+		foreach ($parent_line_item->children() as $child_line_item) {
1350
+			if ($child_line_item instanceof EE_Line_Item) {
1351
+				if ($child_line_item->type() === $line_item_type
1352
+					&& (
1353
+						$child_line_item->OBJ_type() === $obj_type || $obj_type === null
1354
+					)
1355
+				) {
1356
+					$objects[] = $child_line_item;
1357
+				} else {
1358
+					// go-through-all-its children looking for more matches
1359
+					$objects = array_merge(
1360
+						$objects,
1361
+						self::_get_descendants_by_type_and_object_type(
1362
+							$child_line_item,
1363
+							$line_item_type,
1364
+							$obj_type
1365
+						)
1366
+					);
1367
+				}
1368
+			}
1369
+		}
1370
+		return $objects;
1371
+	}
1372
+
1373
+
1374
+	/**
1375
+	 * Gets all descendants subtotals that match the supplied object type
1376
+	 *
1377
+	 * @uses  EEH_Line_Item::_get_descendants_by_type_and_object_type()
1378
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1379
+	 * @param string       $OBJ_type         object type (like Event)
1380
+	 * @param array        $OBJ_IDs          array of OBJ_IDs
1381
+	 * @return EE_Line_Item[]
1382
+	 * @throws EE_Error
1383
+	 */
1384
+	public static function get_line_items_by_object_type_and_IDs(
1385
+		EE_Line_Item $parent_line_item,
1386
+		$OBJ_type = '',
1387
+		$OBJ_IDs = array()
1388
+	) {
1389
+		return self::_get_descendants_by_object_type_and_object_ID(
1390
+			$parent_line_item,
1391
+			$OBJ_type,
1392
+			$OBJ_IDs
1393
+		);
1394
+	}
1395
+
1396
+
1397
+	/**
1398
+	 * Gets all descendants of supplied line item that match the supplied line item type and possibly the object type
1399
+	 * as well
1400
+	 *
1401
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1402
+	 * @param string       $OBJ_type         object type (like Event)
1403
+	 * @param array        $OBJ_IDs          array of OBJ_IDs
1404
+	 * @return EE_Line_Item[]
1405
+	 * @throws EE_Error
1406
+	 */
1407
+	protected static function _get_descendants_by_object_type_and_object_ID(
1408
+		EE_Line_Item $parent_line_item,
1409
+		$OBJ_type,
1410
+		$OBJ_IDs
1411
+	) {
1412
+		$objects = array();
1413
+		foreach ($parent_line_item->children() as $child_line_item) {
1414
+			if ($child_line_item instanceof EE_Line_Item) {
1415
+				if ($child_line_item->OBJ_type() === $OBJ_type
1416
+					&& is_array($OBJ_IDs)
1417
+					&& in_array($child_line_item->OBJ_ID(), $OBJ_IDs)
1418
+				) {
1419
+					$objects[] = $child_line_item;
1420
+				} else {
1421
+					// go-through-all-its children looking for more matches
1422
+					$objects = array_merge(
1423
+						$objects,
1424
+						self::_get_descendants_by_object_type_and_object_ID(
1425
+							$child_line_item,
1426
+							$OBJ_type,
1427
+							$OBJ_IDs
1428
+						)
1429
+					);
1430
+				}
1431
+			}
1432
+		}
1433
+		return $objects;
1434
+	}
1435
+
1436
+
1437
+	/**
1438
+	 * Uses a breadth-first-search in order to find the nearest descendant of
1439
+	 * the specified type and returns it, else NULL
1440
+	 *
1441
+	 * @uses  EEH_Line_Item::_get_nearest_descendant()
1442
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1443
+	 * @param string       $type             like one of the EEM_Line_Item::type_*
1444
+	 * @return EE_Line_Item
1445
+	 * @throws EE_Error
1446
+	 * @throws InvalidArgumentException
1447
+	 * @throws InvalidDataTypeException
1448
+	 * @throws InvalidInterfaceException
1449
+	 * @throws ReflectionException
1450
+	 */
1451
+	public static function get_nearest_descendant_of_type(EE_Line_Item $parent_line_item, $type)
1452
+	{
1453
+		return self::_get_nearest_descendant($parent_line_item, 'LIN_type', $type);
1454
+	}
1455
+
1456
+
1457
+	/**
1458
+	 * Uses a breadth-first-search in order to find the nearest descendant
1459
+	 * having the specified LIN_code and returns it, else NULL
1460
+	 *
1461
+	 * @uses  EEH_Line_Item::_get_nearest_descendant()
1462
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1463
+	 * @param string       $code             any value used for LIN_code
1464
+	 * @return EE_Line_Item
1465
+	 * @throws EE_Error
1466
+	 * @throws InvalidArgumentException
1467
+	 * @throws InvalidDataTypeException
1468
+	 * @throws InvalidInterfaceException
1469
+	 * @throws ReflectionException
1470
+	 */
1471
+	public static function get_nearest_descendant_having_code(EE_Line_Item $parent_line_item, $code)
1472
+	{
1473
+		return self::_get_nearest_descendant($parent_line_item, 'LIN_code', $code);
1474
+	}
1475
+
1476
+
1477
+	/**
1478
+	 * Uses a breadth-first-search in order to find the nearest descendant
1479
+	 * having the specified LIN_code and returns it, else NULL
1480
+	 *
1481
+	 * @param EE_Line_Item $parent_line_item - the line item to find descendants of
1482
+	 * @param string       $search_field     name of EE_Line_Item property
1483
+	 * @param string       $value            any value stored in $search_field
1484
+	 * @return EE_Line_Item
1485
+	 * @throws EE_Error
1486
+	 * @throws InvalidArgumentException
1487
+	 * @throws InvalidDataTypeException
1488
+	 * @throws InvalidInterfaceException
1489
+	 * @throws ReflectionException
1490
+	 */
1491
+	protected static function _get_nearest_descendant(EE_Line_Item $parent_line_item, $search_field, $value)
1492
+	{
1493
+		foreach ($parent_line_item->children() as $child) {
1494
+			if ($child->get($search_field) == $value) {
1495
+				return $child;
1496
+			}
1497
+		}
1498
+		foreach ($parent_line_item->children() as $child) {
1499
+			$descendant_found = self::_get_nearest_descendant(
1500
+				$child,
1501
+				$search_field,
1502
+				$value
1503
+			);
1504
+			if ($descendant_found) {
1505
+				return $descendant_found;
1506
+			}
1507
+		}
1508
+		return null;
1509
+	}
1510
+
1511
+
1512
+	/**
1513
+	 * if passed line item has a TXN ID, uses that to jump directly to the grand total line item for the transaction,
1514
+	 * else recursively walks up the line item tree until a parent of type total is found,
1515
+	 *
1516
+	 * @param EE_Line_Item $line_item
1517
+	 * @return EE_Line_Item
1518
+	 * @throws EE_Error
1519
+	 */
1520
+	public static function find_transaction_grand_total_for_line_item(EE_Line_Item $line_item)
1521
+	{
1522
+		if ($line_item->TXN_ID()) {
1523
+			$total_line_item = $line_item->transaction()->total_line_item(false);
1524
+			if ($total_line_item instanceof EE_Line_Item) {
1525
+				return $total_line_item;
1526
+			}
1527
+		} else {
1528
+			$line_item_parent = $line_item->parent();
1529
+			if ($line_item_parent instanceof EE_Line_Item) {
1530
+				if ($line_item_parent->is_total()) {
1531
+					return $line_item_parent;
1532
+				}
1533
+				return EEH_Line_Item::find_transaction_grand_total_for_line_item($line_item_parent);
1534
+			}
1535
+		}
1536
+		throw new EE_Error(
1537
+			sprintf(
1538
+				esc_html__(
1539
+					'A valid grand total for line item %1$d was not found.',
1540
+					'event_espresso'
1541
+				),
1542
+				$line_item->ID()
1543
+			)
1544
+		);
1545
+	}
1546
+
1547
+
1548
+	/**
1549
+	 * Prints out a representation of the line item tree
1550
+	 *
1551
+	 * @param EE_Line_Item $line_item
1552
+	 * @param int          $indentation
1553
+	 * @return void
1554
+	 * @throws EE_Error
1555
+	 */
1556
+	public static function visualize(EE_Line_Item $line_item, $indentation = 0)
1557
+	{
1558
+		echo defined('EE_TESTS_DIR') ? "\n" : '<br />';
1559
+		if (! $indentation) {
1560
+			echo defined('EE_TESTS_DIR') ? "\n" : '<br />';
1561
+		}
1562
+		for ($i = 0; $i < $indentation; $i++) {
1563
+			echo '. ';
1564
+		}
1565
+		$breakdown = '';
1566
+		if ($line_item->is_line_item()) {
1567
+			if ($line_item->is_percent()) {
1568
+				$breakdown = "{$line_item->percent()}%";
1569
+			} else {
1570
+				$breakdown = '$' . "{$line_item->unit_price()} x {$line_item->quantity()}";
1571
+			}
1572
+		}
1573
+		echo $line_item->name();
1574
+		echo " [ ID:{$line_item->ID()} | qty:{$line_item->quantity()} ] {$line_item->type()} : ";
1575
+		echo '$' . (string) $line_item->total();
1576
+		if ($breakdown) {
1577
+			echo " ( {$breakdown} )";
1578
+		}
1579
+		if ($line_item->is_taxable()) {
1580
+			echo '  * taxable';
1581
+		}
1582
+		if ($line_item->children()) {
1583
+			foreach ($line_item->children() as $child) {
1584
+				self::visualize($child, $indentation + 1);
1585
+			}
1586
+		}
1587
+	}
1588
+
1589
+
1590
+	/**
1591
+	 * Calculates the registration's final price, taking into account that they
1592
+	 * need to not only help pay for their OWN ticket, but also any transaction-wide surcharges and taxes,
1593
+	 * and receive a portion of any transaction-wide discounts.
1594
+	 * eg1, if I buy a $1 ticket and brent buys a $9 ticket, and we receive a $5 discount
1595
+	 * then I'll get 1/10 of that $5 discount, which is $0.50, and brent will get
1596
+	 * 9/10ths of that $5 discount, which is $4.50. So my final price should be $0.50
1597
+	 * and brent's final price should be $5.50.
1598
+	 * In order to do this, we basically need to traverse the line item tree calculating
1599
+	 * the running totals (just as if we were recalculating the total), but when we identify
1600
+	 * regular line items, we need to keep track of their share of the grand total.
1601
+	 * Also, we need to keep track of the TAXABLE total for each ticket purchase, so
1602
+	 * we can know how to apply taxes to it. (Note: "taxable total" does not equal the "pretax total"
1603
+	 * when there are non-taxable items; otherwise they would be the same)
1604
+	 *
1605
+	 * @param EE_Line_Item $line_item
1606
+	 * @param array        $billable_ticket_quantities  array of EE_Ticket IDs and their corresponding quantity that
1607
+	 *                                                  can be included in price calculations at this moment
1608
+	 * @return array        keys are line items for tickets IDs and values are their share of the running total,
1609
+	 *                                                  plus the key 'total', and 'taxable' which also has keys of all
1610
+	 *                                                  the ticket IDs.
1611
+	 *                                                  Eg array(
1612
+	 *                                                      12 => 4.3
1613
+	 *                                                      23 => 8.0
1614
+	 *                                                      'total' => 16.6,
1615
+	 *                                                      'taxable' => array(
1616
+	 *                                                          12 => 10,
1617
+	 *                                                          23 => 4
1618
+	 *                                                      ).
1619
+	 *                                                  So to find which registrations have which final price, we need
1620
+	 *                                                  to find which line item is theirs, which can be done with
1621
+	 *                                                  `EEM_Line_Item::instance()->get_line_item_for_registration(
1622
+	 *                                                  $registration );`
1623
+	 * @throws EE_Error
1624
+	 * @throws InvalidArgumentException
1625
+	 * @throws InvalidDataTypeException
1626
+	 * @throws InvalidInterfaceException
1627
+	 * @throws ReflectionException
1628
+	 */
1629
+	public static function calculate_reg_final_prices_per_line_item(
1630
+		EE_Line_Item $line_item,
1631
+		$billable_ticket_quantities = array()
1632
+	) {
1633
+		$running_totals = [
1634
+			'total'   => 0,
1635
+			'taxable' => ['total' => 0]
1636
+		];
1637
+		foreach ($line_item->children() as $child_line_item) {
1638
+			switch ($child_line_item->type()) {
1639
+				case EEM_Line_Item::type_sub_total:
1640
+					$running_totals_from_subtotal = EEH_Line_Item::calculate_reg_final_prices_per_line_item(
1641
+						$child_line_item,
1642
+						$billable_ticket_quantities
1643
+					);
1644
+					// combine arrays but preserve numeric keys
1645
+					$running_totals = array_replace_recursive($running_totals_from_subtotal, $running_totals);
1646
+					$running_totals['total'] += $running_totals_from_subtotal['total'];
1647
+					$running_totals['taxable']['total'] += $running_totals_from_subtotal['taxable']['total'];
1648
+					break;
1649
+
1650
+				case EEM_Line_Item::type_tax_sub_total:
1651
+					// find how much the taxes percentage is
1652
+					if ($child_line_item->percent() !== 0) {
1653
+						$tax_percent_decimal = $child_line_item->percent() / 100;
1654
+					} else {
1655
+						$tax_percent_decimal = EE_Taxes::get_total_taxes_percentage() / 100;
1656
+					}
1657
+					// and apply to all the taxable totals, and add to the pretax totals
1658
+					foreach ($running_totals as $line_item_id => $this_running_total) {
1659
+						// "total" and "taxable" array key is an exception
1660
+						if ($line_item_id === 'taxable') {
1661
+							continue;
1662
+						}
1663
+						$taxable_total = $running_totals['taxable'][ $line_item_id ];
1664
+						$running_totals[ $line_item_id ] += ($taxable_total * $tax_percent_decimal);
1665
+					}
1666
+					break;
1667
+
1668
+				case EEM_Line_Item::type_line_item:
1669
+					// ticket line items or ????
1670
+					if ($child_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET) {
1671
+						// kk it's a ticket
1672
+						if (isset($running_totals[ $child_line_item->ID() ])) {
1673
+							// huh? that shouldn't happen.
1674
+							$running_totals['total'] += $child_line_item->total();
1675
+						} else {
1676
+							// its not in our running totals yet. great.
1677
+							if ($child_line_item->is_taxable()) {
1678
+								$taxable_amount = $child_line_item->unit_price();
1679
+							} else {
1680
+								$taxable_amount = 0;
1681
+							}
1682
+							// are we only calculating totals for some tickets?
1683
+							if (isset($billable_ticket_quantities[ $child_line_item->OBJ_ID() ])) {
1684
+								$quantity = $billable_ticket_quantities[ $child_line_item->OBJ_ID() ];
1685
+								$running_totals[ $child_line_item->ID() ] = $quantity
1686
+									? $child_line_item->unit_price()
1687
+									: 0;
1688
+								$running_totals['taxable'][ $child_line_item->ID() ] = $quantity
1689
+									? $taxable_amount
1690
+									: 0;
1691
+							} else {
1692
+								$quantity = $child_line_item->quantity();
1693
+								$running_totals[ $child_line_item->ID() ] = $child_line_item->unit_price();
1694
+								$running_totals['taxable'][ $child_line_item->ID() ] = $taxable_amount;
1695
+							}
1696
+							$running_totals['taxable']['total'] += $taxable_amount * $quantity;
1697
+							$running_totals['total'] += $child_line_item->unit_price() * $quantity;
1698
+						}
1699
+					} else {
1700
+						// it's some other type of item added to the cart
1701
+						// it should affect the running totals
1702
+						// basically we want to convert it into a PERCENT modifier. Because
1703
+						// more clearly affect all registration's final price equally
1704
+						$line_items_percent_of_running_total = $running_totals['total'] > 0
1705
+							? ($child_line_item->total() / $running_totals['total']) + 1
1706
+							: 1;
1707
+						foreach ($running_totals as $line_item_id => $this_running_total) {
1708
+							// the "taxable" array key is an exception
1709
+							if ($line_item_id === 'taxable') {
1710
+								continue;
1711
+							}
1712
+							// update the running totals
1713
+							// yes this actually even works for the running grand total!
1714
+							$running_totals[ $line_item_id ] =
1715
+								$line_items_percent_of_running_total * $this_running_total;
1716
+
1717
+							if ($child_line_item->is_taxable()) {
1718
+								$running_totals['taxable'][ $line_item_id ] =
1719
+									$line_items_percent_of_running_total * $running_totals['taxable'][ $line_item_id ];
1720
+							}
1721
+						}
1722
+					}
1723
+					break;
1724
+			}
1725
+		}
1726
+		return $running_totals;
1727
+	}
1728
+
1729
+
1730
+	/**
1731
+	 * @param EE_Line_Item $total_line_item
1732
+	 * @param EE_Line_Item $ticket_line_item
1733
+	 * @return float | null
1734
+	 * @throws EE_Error
1735
+	 * @throws InvalidArgumentException
1736
+	 * @throws InvalidDataTypeException
1737
+	 * @throws InvalidInterfaceException
1738
+	 * @throws OutOfRangeException
1739
+	 * @throws ReflectionException
1740
+	 */
1741
+	public static function calculate_final_price_for_ticket_line_item(
1742
+		EE_Line_Item $total_line_item,
1743
+		EE_Line_Item $ticket_line_item
1744
+	) {
1745
+		static $final_prices_per_ticket_line_item = array();
1746
+		if (empty($final_prices_per_ticket_line_item) || empty($final_prices_per_ticket_line_item[ $total_line_item->ID() ])) {
1747
+			$final_prices_per_ticket_line_item[ $total_line_item->ID() ] = EEH_Line_Item::calculate_reg_final_prices_per_line_item(
1748
+				$total_line_item
1749
+			);
1750
+		}
1751
+		// ok now find this new registration's final price
1752
+		if (isset($final_prices_per_ticket_line_item[ $total_line_item->ID() ][ $ticket_line_item->ID() ])) {
1753
+			return $final_prices_per_ticket_line_item[ $total_line_item->ID() ][ $ticket_line_item->ID() ];
1754
+		}
1755
+		$message = sprintf(
1756
+			esc_html__(
1757
+				'The final price for the ticket line item (ID:%1$d) could not be calculated.',
1758
+				'event_espresso'
1759
+			),
1760
+			$ticket_line_item->ID()
1761
+		);
1762
+		if (WP_DEBUG) {
1763
+			$message .= '<br>' . print_r($final_prices_per_ticket_line_item, true);
1764
+			throw new OutOfRangeException($message);
1765
+		}
1766
+		EE_Log::instance()->log(__CLASS__, __FUNCTION__, $message);
1767
+		return null;
1768
+	}
1769
+
1770
+
1771
+	/**
1772
+	 * Creates a duplicate of the line item tree, except only includes billable items
1773
+	 * and the portion of line items attributed to billable things
1774
+	 *
1775
+	 * @param EE_Line_Item      $line_item
1776
+	 * @param EE_Registration[] $registrations
1777
+	 * @return EE_Line_Item
1778
+	 * @throws EE_Error
1779
+	 * @throws InvalidArgumentException
1780
+	 * @throws InvalidDataTypeException
1781
+	 * @throws InvalidInterfaceException
1782
+	 * @throws ReflectionException
1783
+	 */
1784
+	public static function billable_line_item_tree(EE_Line_Item $line_item, $registrations)
1785
+	{
1786
+		$copy_li = EEH_Line_Item::billable_line_item($line_item, $registrations);
1787
+		foreach ($line_item->children() as $child_li) {
1788
+			$copy_li->add_child_line_item(
1789
+				EEH_Line_Item::billable_line_item_tree($child_li, $registrations)
1790
+			);
1791
+		}
1792
+		// if this is the grand total line item, make sure the totals all add up
1793
+		// (we could have duplicated this logic AS we copied the line items, but
1794
+		// it seems DRYer this way)
1795
+		if ($copy_li->type() === EEM_Line_Item::type_total) {
1796
+			$copy_li->recalculate_total_including_taxes();
1797
+		}
1798
+		return $copy_li;
1799
+	}
1800
+
1801
+
1802
+	/**
1803
+	 * Creates a new, unsaved line item from $line_item that factors in the
1804
+	 * number of billable registrations on $registrations.
1805
+	 *
1806
+	 * @param EE_Line_Item      $line_item
1807
+	 * @param EE_Registration[] $registrations
1808
+	 * @return EE_Line_Item
1809
+	 * @throws EE_Error
1810
+	 * @throws InvalidArgumentException
1811
+	 * @throws InvalidDataTypeException
1812
+	 * @throws InvalidInterfaceException
1813
+	 * @throws ReflectionException
1814
+	 */
1815
+	public static function billable_line_item(EE_Line_Item $line_item, $registrations)
1816
+	{
1817
+		$new_li_fields = $line_item->model_field_array();
1818
+		if ($line_item->type() === EEM_Line_Item::type_line_item &&
1819
+			$line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1820
+		) {
1821
+			$count = 0;
1822
+			foreach ($registrations as $registration) {
1823
+				if ($line_item->OBJ_ID() === $registration->ticket_ID() &&
1824
+					in_array(
1825
+						$registration->status_ID(),
1826
+						EEM_Registration::reg_statuses_that_allow_payment(),
1827
+						true
1828
+					)
1829
+				) {
1830
+					$count++;
1831
+				}
1832
+			}
1833
+			$new_li_fields['LIN_quantity'] = $count;
1834
+		}
1835
+		// don't set the total. We'll leave that up to the code that calculates it
1836
+		unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent'], $new_li_fields['LIN_total']);
1837
+		return EE_Line_Item::new_instance($new_li_fields);
1838
+	}
1839
+
1840
+
1841
+	/**
1842
+	 * Returns a modified line item tree where all the subtotals which have a total of 0
1843
+	 * are removed, and line items with a quantity of 0
1844
+	 *
1845
+	 * @param EE_Line_Item $line_item |null
1846
+	 * @return EE_Line_Item|null
1847
+	 * @throws EE_Error
1848
+	 * @throws InvalidArgumentException
1849
+	 * @throws InvalidDataTypeException
1850
+	 * @throws InvalidInterfaceException
1851
+	 * @throws ReflectionException
1852
+	 */
1853
+	public static function non_empty_line_items(EE_Line_Item $line_item)
1854
+	{
1855
+		$copied_li = EEH_Line_Item::non_empty_line_item($line_item);
1856
+		if ($copied_li === null) {
1857
+			return null;
1858
+		}
1859
+		// if this is an event subtotal, we want to only include it if it
1860
+		// has a non-zero total and at least one ticket line item child
1861
+		$ticket_children = 0;
1862
+		foreach ($line_item->children() as $child_li) {
1863
+			$child_li_copy = EEH_Line_Item::non_empty_line_items($child_li);
1864
+			if ($child_li_copy !== null) {
1865
+				$copied_li->add_child_line_item($child_li_copy);
1866
+				if ($child_li_copy->type() === EEM_Line_Item::type_line_item &&
1867
+					$child_li_copy->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1868
+				) {
1869
+					$ticket_children++;
1870
+				}
1871
+			}
1872
+		}
1873
+		// if this is an event subtotal with NO ticket children
1874
+		// we basically want to ignore it
1875
+		if ($ticket_children === 0
1876
+			&& $line_item->type() === EEM_Line_Item::type_sub_total
1877
+			&& $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_EVENT
1878
+			&& $line_item->total() === 0
1879
+		) {
1880
+			return null;
1881
+		}
1882
+		return $copied_li;
1883
+	}
1884
+
1885
+
1886
+	/**
1887
+	 * Creates a new, unsaved line item, but if it's a ticket line item
1888
+	 * with a total of 0, or a subtotal of 0, returns null instead
1889
+	 *
1890
+	 * @param EE_Line_Item $line_item
1891
+	 * @return EE_Line_Item
1892
+	 * @throws EE_Error
1893
+	 * @throws InvalidArgumentException
1894
+	 * @throws InvalidDataTypeException
1895
+	 * @throws InvalidInterfaceException
1896
+	 * @throws ReflectionException
1897
+	 */
1898
+	public static function non_empty_line_item(EE_Line_Item $line_item)
1899
+	{
1900
+		if ($line_item->type() === EEM_Line_Item::type_line_item
1901
+			&& $line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1902
+			&& $line_item->quantity() === 0
1903
+		) {
1904
+			return null;
1905
+		}
1906
+		$new_li_fields = $line_item->model_field_array();
1907
+		// don't set the total. We'll leave that up to the code that calculates it
1908
+		unset($new_li_fields['LIN_ID'], $new_li_fields['LIN_parent']);
1909
+		return EE_Line_Item::new_instance($new_li_fields);
1910
+	}
1911
+
1912
+
1913
+	/**
1914
+	 * Cycles through all of the ticket line items for the supplied total line item
1915
+	 * and ensures that the line item's "is_taxable" field matches that of its corresponding ticket
1916
+	 *
1917
+	 * @param EE_Line_Item $total_line_item
1918
+	 * @since 4.9.79.p
1919
+	 * @throws EE_Error
1920
+	 * @throws InvalidArgumentException
1921
+	 * @throws InvalidDataTypeException
1922
+	 * @throws InvalidInterfaceException
1923
+	 * @throws ReflectionException
1924
+	 */
1925
+	public static function resetIsTaxableForTickets(EE_Line_Item $total_line_item)
1926
+	{
1927
+		$ticket_line_items = self::get_ticket_line_items($total_line_item);
1928
+		foreach ($ticket_line_items as $ticket_line_item) {
1929
+			if ($ticket_line_item instanceof EE_Line_Item
1930
+				&& $ticket_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET
1931
+			) {
1932
+				$ticket = $ticket_line_item->ticket();
1933
+				if ($ticket instanceof EE_Ticket && $ticket->taxable() !== $ticket_line_item->is_taxable()) {
1934
+					$ticket_line_item->set_is_taxable($ticket->taxable());
1935
+					$ticket_line_item->save();
1936
+				}
1937
+			}
1938
+		}
1939
+	}
1940
+
1941
+
1942
+
1943
+	/**************************************** @DEPRECATED METHODS *************************************** */
1944
+	/**
1945
+	 * @deprecated
1946
+	 * @param EE_Line_Item $total_line_item
1947
+	 * @return EE_Line_Item
1948
+	 * @throws EE_Error
1949
+	 * @throws InvalidArgumentException
1950
+	 * @throws InvalidDataTypeException
1951
+	 * @throws InvalidInterfaceException
1952
+	 * @throws ReflectionException
1953
+	 */
1954
+	public static function get_items_subtotal(EE_Line_Item $total_line_item)
1955
+	{
1956
+		EE_Error::doing_it_wrong(
1957
+			'EEH_Line_Item::get_items_subtotal()',
1958
+			sprintf(
1959
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
1960
+				'EEH_Line_Item::get_pre_tax_subtotal()'
1961
+			),
1962
+			'4.6.0'
1963
+		);
1964
+		return self::get_pre_tax_subtotal($total_line_item);
1965
+	}
1966
+
1967
+
1968
+	/**
1969
+	 * @deprecated
1970
+	 * @param EE_Transaction $transaction
1971
+	 * @return EE_Line_Item
1972
+	 * @throws EE_Error
1973
+	 * @throws InvalidArgumentException
1974
+	 * @throws InvalidDataTypeException
1975
+	 * @throws InvalidInterfaceException
1976
+	 * @throws ReflectionException
1977
+	 */
1978
+	public static function create_default_total_line_item($transaction = null)
1979
+	{
1980
+		EE_Error::doing_it_wrong(
1981
+			'EEH_Line_Item::create_default_total_line_item()',
1982
+			sprintf(
1983
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
1984
+				'EEH_Line_Item::create_total_line_item()'
1985
+			),
1986
+			'4.6.0'
1987
+		);
1988
+		return self::create_total_line_item($transaction);
1989
+	}
1990
+
1991
+
1992
+	/**
1993
+	 * @deprecated
1994
+	 * @param EE_Line_Item   $total_line_item
1995
+	 * @param EE_Transaction $transaction
1996
+	 * @return EE_Line_Item
1997
+	 * @throws EE_Error
1998
+	 * @throws InvalidArgumentException
1999
+	 * @throws InvalidDataTypeException
2000
+	 * @throws InvalidInterfaceException
2001
+	 * @throws ReflectionException
2002
+	 */
2003
+	public static function create_default_tickets_subtotal(EE_Line_Item $total_line_item, $transaction = null)
2004
+	{
2005
+		EE_Error::doing_it_wrong(
2006
+			'EEH_Line_Item::create_default_tickets_subtotal()',
2007
+			sprintf(
2008
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
2009
+				'EEH_Line_Item::create_pre_tax_subtotal()'
2010
+			),
2011
+			'4.6.0'
2012
+		);
2013
+		return self::create_pre_tax_subtotal($total_line_item, $transaction);
2014
+	}
2015
+
2016
+
2017
+	/**
2018
+	 * @deprecated
2019
+	 * @param EE_Line_Item   $total_line_item
2020
+	 * @param EE_Transaction $transaction
2021
+	 * @return EE_Line_Item
2022
+	 * @throws EE_Error
2023
+	 * @throws InvalidArgumentException
2024
+	 * @throws InvalidDataTypeException
2025
+	 * @throws InvalidInterfaceException
2026
+	 * @throws ReflectionException
2027
+	 */
2028
+	public static function create_default_taxes_subtotal(EE_Line_Item $total_line_item, $transaction = null)
2029
+	{
2030
+		EE_Error::doing_it_wrong(
2031
+			'EEH_Line_Item::create_default_taxes_subtotal()',
2032
+			sprintf(
2033
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
2034
+				'EEH_Line_Item::create_taxes_subtotal()'
2035
+			),
2036
+			'4.6.0'
2037
+		);
2038
+		return self::create_taxes_subtotal($total_line_item, $transaction);
2039
+	}
2040
+
2041
+
2042
+	/**
2043
+	 * @deprecated
2044
+	 * @param EE_Line_Item   $total_line_item
2045
+	 * @param EE_Transaction $transaction
2046
+	 * @return EE_Line_Item
2047
+	 * @throws EE_Error
2048
+	 * @throws InvalidArgumentException
2049
+	 * @throws InvalidDataTypeException
2050
+	 * @throws InvalidInterfaceException
2051
+	 * @throws ReflectionException
2052
+	 */
2053
+	public static function create_default_event_subtotal(EE_Line_Item $total_line_item, $transaction = null)
2054
+	{
2055
+		EE_Error::doing_it_wrong(
2056
+			'EEH_Line_Item::create_default_event_subtotal()',
2057
+			sprintf(
2058
+				esc_html__('Method replaced with %1$s', 'event_espresso'),
2059
+				'EEH_Line_Item::create_event_subtotal()'
2060
+			),
2061
+			'4.6.0'
2062
+		);
2063
+		return self::create_event_subtotal($total_line_item, $transaction);
2064
+	}
2065 2065
 }
Please login to merge, or discard this patch.
Spacing   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -138,7 +138,7 @@  discard block
 block discarded – undo
138 138
      */
139 139
     public static function add_ticket_purchase(EE_Line_Item $total_line_item, EE_Ticket $ticket, $qty = 1)
140 140
     {
141
-        if (! $total_line_item instanceof EE_Line_Item || ! $total_line_item->is_total()) {
141
+        if ( ! $total_line_item instanceof EE_Line_Item || ! $total_line_item->is_total()) {
142 142
             throw new EE_Error(
143 143
                 sprintf(
144 144
                     esc_html__(
@@ -153,7 +153,7 @@  discard block
 block discarded – undo
153 153
         // either increment the qty for an existing ticket
154 154
         $line_item = self::increment_ticket_qty_if_already_in_cart($total_line_item, $ticket, $qty);
155 155
         // or add a new one
156
-        if (! $line_item instanceof EE_Line_Item) {
156
+        if ( ! $line_item instanceof EE_Line_Item) {
157 157
             $line_item = self::create_ticket_line_item($total_line_item, $ticket, $qty);
158 158
         }
159 159
         $total_line_item->recalculate_total_including_taxes();
@@ -214,7 +214,7 @@  discard block
 block discarded – undo
214 214
      */
215 215
     public static function increment_quantity(EE_Line_Item $line_item, $qty = 1)
216 216
     {
217
-        if (! $line_item->is_percent()) {
217
+        if ( ! $line_item->is_percent()) {
218 218
             $qty += $line_item->quantity();
219 219
             $line_item->set_quantity($qty);
220 220
             $line_item->set_total($line_item->unit_price() * $qty);
@@ -243,7 +243,7 @@  discard block
 block discarded – undo
243 243
      */
244 244
     public static function decrement_quantity(EE_Line_Item $line_item, $qty = 1)
245 245
     {
246
-        if (! $line_item->is_percent()) {
246
+        if ( ! $line_item->is_percent()) {
247 247
             $qty = $line_item->quantity() - $qty;
248 248
             $qty = max($qty, 0);
249 249
             $line_item->set_quantity($qty);
@@ -272,7 +272,7 @@  discard block
 block discarded – undo
272 272
      */
273 273
     public static function update_quantity(EE_Line_Item $line_item, $new_quantity)
274 274
     {
275
-        if (! $line_item->is_percent()) {
275
+        if ( ! $line_item->is_percent()) {
276 276
             $line_item->set_quantity($new_quantity);
277 277
             $line_item->set_total($line_item->unit_price() * $new_quantity);
278 278
             $line_item->save();
@@ -312,7 +312,7 @@  discard block
 block discarded – undo
312 312
         // add $ticket to cart
313 313
         $line_item = EE_Line_Item::new_instance(array(
314 314
             'LIN_name'       => $ticket->name(),
315
-            'LIN_desc'       => $ticket->description() !== '' ? $ticket->description() . ' ' . $event : $event,
315
+            'LIN_desc'       => $ticket->description() !== '' ? $ticket->description().' '.$event : $event,
316 316
             'LIN_unit_price' => $ticket->price(),
317 317
             'LIN_quantity'   => $qty,
318 318
             'LIN_is_taxable' => $ticket->taxable(),
@@ -462,7 +462,7 @@  discard block
 block discarded – undo
462 462
                         'event_espresso'
463 463
                     ),
464 464
                     $ticket_line_item->name(),
465
-                    current_time(get_option('date_format') . ' ' . get_option('time_format'))
465
+                    current_time(get_option('date_format').' '.get_option('time_format'))
466 466
                 ),
467 467
                 'LIN_unit_price' => 0, // $ticket_line_item->unit_price()
468 468
                 'LIN_quantity'   => $qty,
@@ -524,7 +524,7 @@  discard block
 block discarded – undo
524 524
         );
525 525
         $cancellation_line_item = reset($cancellation_line_item);
526 526
         // verify that this ticket was indeed previously cancelled
527
-        if (! $cancellation_line_item instanceof EE_Line_Item) {
527
+        if ( ! $cancellation_line_item instanceof EE_Line_Item) {
528 528
             return false;
529 529
         }
530 530
         if ($cancellation_line_item->quantity() > $qty) {
@@ -729,7 +729,7 @@  discard block
 block discarded – undo
729 729
             'LIN_code'  => 'taxes',
730 730
             'LIN_name'  => esc_html__('Taxes', 'event_espresso'),
731 731
             'LIN_type'  => EEM_Line_Item::type_tax_sub_total,
732
-            'LIN_order' => 1000,// this should always come last
732
+            'LIN_order' => 1000, // this should always come last
733 733
         ));
734 734
         $tax_line_item = apply_filters(
735 735
             'FHEE__EEH_Line_Item__create_taxes_subtotal__tax_line_item',
@@ -785,7 +785,7 @@  discard block
 block discarded – undo
785 785
      */
786 786
     public static function get_event_code($event)
787 787
     {
788
-        return 'event-' . ($event instanceof EE_Event ? $event->ID() : '0');
788
+        return 'event-'.($event instanceof EE_Event ? $event->ID() : '0');
789 789
     }
790 790
 
791 791
 
@@ -834,7 +834,7 @@  discard block
 block discarded – undo
834 834
     public static function get_event_line_item_for_ticket(EE_Line_Item $grand_total, EE_Ticket $ticket)
835 835
     {
836 836
         $first_datetime = $ticket->first_datetime();
837
-        if (! $first_datetime instanceof EE_Datetime) {
837
+        if ( ! $first_datetime instanceof EE_Datetime) {
838 838
             throw new EE_Error(
839 839
                 sprintf(
840 840
                     __('The supplied ticket (ID %d) has no datetimes', 'event_espresso'),
@@ -843,7 +843,7 @@  discard block
 block discarded – undo
843 843
             );
844 844
         }
845 845
         $event = $first_datetime->event();
846
-        if (! $event instanceof EE_Event) {
846
+        if ( ! $event instanceof EE_Event) {
847 847
             throw new EE_Error(
848 848
                 sprintf(
849 849
                     esc_html__(
@@ -855,7 +855,7 @@  discard block
 block discarded – undo
855 855
             );
856 856
         }
857 857
         $events_sub_total = EEH_Line_Item::get_event_line_item($grand_total, $event);
858
-        if (! $events_sub_total instanceof EE_Line_Item) {
858
+        if ( ! $events_sub_total instanceof EE_Line_Item) {
859 859
             throw new EE_Error(
860 860
                 sprintf(
861 861
                     esc_html__(
@@ -891,7 +891,7 @@  discard block
 block discarded – undo
891 891
         $found = false;
892 892
         foreach (EEH_Line_Item::get_event_subtotals($grand_total) as $event_line_item) {
893 893
             // default event subtotal, we should only ever find this the first time this method is called
894
-            if (! $event_line_item->OBJ_ID()) {
894
+            if ( ! $event_line_item->OBJ_ID()) {
895 895
                 // let's use this! but first... set the event details
896 896
                 EEH_Line_Item::set_event_subtotal_details($event_line_item, $event);
897 897
                 $found = true;
@@ -903,7 +903,7 @@  discard block
 block discarded – undo
903 903
                 break;
904 904
             }
905 905
         }
906
-        if (! $found) {
906
+        if ( ! $found) {
907 907
             // there is no event sub-total yet, so add it
908 908
             $pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($grand_total);
909 909
             // create a new "event" subtotal below that
@@ -1020,7 +1020,7 @@  discard block
 block discarded – undo
1020 1020
     public static function ensure_taxes_applied($total_line_item)
1021 1021
     {
1022 1022
         $taxes_subtotal = self::get_taxes_subtotal($total_line_item);
1023
-        if (! $taxes_subtotal->children()) {
1023
+        if ( ! $taxes_subtotal->children()) {
1024 1024
             self::apply_taxes($total_line_item);
1025 1025
         }
1026 1026
         return $taxes_subtotal->total();
@@ -1087,7 +1087,7 @@  discard block
 block discarded – undo
1087 1087
         do_action('AHEE_log', __FILE__, __FUNCTION__, '');
1088 1088
 
1089 1089
         // check if only a single line_item_id was passed
1090
-        if (! empty($line_item_codes) && ! is_array($line_item_codes)) {
1090
+        if ( ! empty($line_item_codes) && ! is_array($line_item_codes)) {
1091 1091
             // place single line_item_id in an array to appear as multiple line_item_ids
1092 1092
             $line_item_codes = array($line_item_codes);
1093 1093
         }
@@ -1194,7 +1194,7 @@  discard block
 block discarded – undo
1194 1194
         if ($code_substring_for_whitelist !== null) {
1195 1195
             $whitelisted = strpos($line_item->code(), $code_substring_for_whitelist) !== false;
1196 1196
         }
1197
-        if (! $whitelisted && $line_item->is_line_item()) {
1197
+        if ( ! $whitelisted && $line_item->is_line_item()) {
1198 1198
             $line_item->set_is_taxable($taxable);
1199 1199
         }
1200 1200
         foreach ($line_item->children() as $child_line_item) {
@@ -1556,7 +1556,7 @@  discard block
 block discarded – undo
1556 1556
     public static function visualize(EE_Line_Item $line_item, $indentation = 0)
1557 1557
     {
1558 1558
         echo defined('EE_TESTS_DIR') ? "\n" : '<br />';
1559
-        if (! $indentation) {
1559
+        if ( ! $indentation) {
1560 1560
             echo defined('EE_TESTS_DIR') ? "\n" : '<br />';
1561 1561
         }
1562 1562
         for ($i = 0; $i < $indentation; $i++) {
@@ -1567,12 +1567,12 @@  discard block
 block discarded – undo
1567 1567
             if ($line_item->is_percent()) {
1568 1568
                 $breakdown = "{$line_item->percent()}%";
1569 1569
             } else {
1570
-                $breakdown = '$' . "{$line_item->unit_price()} x {$line_item->quantity()}";
1570
+                $breakdown = '$'."{$line_item->unit_price()} x {$line_item->quantity()}";
1571 1571
             }
1572 1572
         }
1573 1573
         echo $line_item->name();
1574 1574
         echo " [ ID:{$line_item->ID()} | qty:{$line_item->quantity()} ] {$line_item->type()} : ";
1575
-        echo '$' . (string) $line_item->total();
1575
+        echo '$'.(string) $line_item->total();
1576 1576
         if ($breakdown) {
1577 1577
             echo " ( {$breakdown} )";
1578 1578
         }
@@ -1660,8 +1660,8 @@  discard block
 block discarded – undo
1660 1660
                         if ($line_item_id === 'taxable') {
1661 1661
                             continue;
1662 1662
                         }
1663
-                        $taxable_total = $running_totals['taxable'][ $line_item_id ];
1664
-                        $running_totals[ $line_item_id ] += ($taxable_total * $tax_percent_decimal);
1663
+                        $taxable_total = $running_totals['taxable'][$line_item_id];
1664
+                        $running_totals[$line_item_id] += ($taxable_total * $tax_percent_decimal);
1665 1665
                     }
1666 1666
                     break;
1667 1667
 
@@ -1669,7 +1669,7 @@  discard block
 block discarded – undo
1669 1669
                     // ticket line items or ????
1670 1670
                     if ($child_line_item->OBJ_type() === EEM_Line_Item::OBJ_TYPE_TICKET) {
1671 1671
                         // kk it's a ticket
1672
-                        if (isset($running_totals[ $child_line_item->ID() ])) {
1672
+                        if (isset($running_totals[$child_line_item->ID()])) {
1673 1673
                             // huh? that shouldn't happen.
1674 1674
                             $running_totals['total'] += $child_line_item->total();
1675 1675
                         } else {
@@ -1680,18 +1680,18 @@  discard block
 block discarded – undo
1680 1680
                                 $taxable_amount = 0;
1681 1681
                             }
1682 1682
                             // are we only calculating totals for some tickets?
1683
-                            if (isset($billable_ticket_quantities[ $child_line_item->OBJ_ID() ])) {
1684
-                                $quantity = $billable_ticket_quantities[ $child_line_item->OBJ_ID() ];
1685
-                                $running_totals[ $child_line_item->ID() ] = $quantity
1683
+                            if (isset($billable_ticket_quantities[$child_line_item->OBJ_ID()])) {
1684
+                                $quantity = $billable_ticket_quantities[$child_line_item->OBJ_ID()];
1685
+                                $running_totals[$child_line_item->ID()] = $quantity
1686 1686
                                     ? $child_line_item->unit_price()
1687 1687
                                     : 0;
1688
-                                $running_totals['taxable'][ $child_line_item->ID() ] = $quantity
1688
+                                $running_totals['taxable'][$child_line_item->ID()] = $quantity
1689 1689
                                     ? $taxable_amount
1690 1690
                                     : 0;
1691 1691
                             } else {
1692 1692
                                 $quantity = $child_line_item->quantity();
1693
-                                $running_totals[ $child_line_item->ID() ] = $child_line_item->unit_price();
1694
-                                $running_totals['taxable'][ $child_line_item->ID() ] = $taxable_amount;
1693
+                                $running_totals[$child_line_item->ID()] = $child_line_item->unit_price();
1694
+                                $running_totals['taxable'][$child_line_item->ID()] = $taxable_amount;
1695 1695
                             }
1696 1696
                             $running_totals['taxable']['total'] += $taxable_amount * $quantity;
1697 1697
                             $running_totals['total'] += $child_line_item->unit_price() * $quantity;
@@ -1711,12 +1711,12 @@  discard block
 block discarded – undo
1711 1711
                             }
1712 1712
                             // update the running totals
1713 1713
                             // yes this actually even works for the running grand total!
1714
-                            $running_totals[ $line_item_id ] =
1714
+                            $running_totals[$line_item_id] =
1715 1715
                                 $line_items_percent_of_running_total * $this_running_total;
1716 1716
 
1717 1717
                             if ($child_line_item->is_taxable()) {
1718
-                                $running_totals['taxable'][ $line_item_id ] =
1719
-                                    $line_items_percent_of_running_total * $running_totals['taxable'][ $line_item_id ];
1718
+                                $running_totals['taxable'][$line_item_id] =
1719
+                                    $line_items_percent_of_running_total * $running_totals['taxable'][$line_item_id];
1720 1720
                             }
1721 1721
                         }
1722 1722
                     }
@@ -1743,14 +1743,14 @@  discard block
 block discarded – undo
1743 1743
         EE_Line_Item $ticket_line_item
1744 1744
     ) {
1745 1745
         static $final_prices_per_ticket_line_item = array();
1746
-        if (empty($final_prices_per_ticket_line_item) || empty($final_prices_per_ticket_line_item[ $total_line_item->ID() ])) {
1747
-            $final_prices_per_ticket_line_item[ $total_line_item->ID() ] = EEH_Line_Item::calculate_reg_final_prices_per_line_item(
1746
+        if (empty($final_prices_per_ticket_line_item) || empty($final_prices_per_ticket_line_item[$total_line_item->ID()])) {
1747
+            $final_prices_per_ticket_line_item[$total_line_item->ID()] = EEH_Line_Item::calculate_reg_final_prices_per_line_item(
1748 1748
                 $total_line_item
1749 1749
             );
1750 1750
         }
1751 1751
         // ok now find this new registration's final price
1752
-        if (isset($final_prices_per_ticket_line_item[ $total_line_item->ID() ][ $ticket_line_item->ID() ])) {
1753
-            return $final_prices_per_ticket_line_item[ $total_line_item->ID() ][ $ticket_line_item->ID() ];
1752
+        if (isset($final_prices_per_ticket_line_item[$total_line_item->ID()][$ticket_line_item->ID()])) {
1753
+            return $final_prices_per_ticket_line_item[$total_line_item->ID()][$ticket_line_item->ID()];
1754 1754
         }
1755 1755
         $message = sprintf(
1756 1756
             esc_html__(
@@ -1760,7 +1760,7 @@  discard block
 block discarded – undo
1760 1760
             $ticket_line_item->ID()
1761 1761
         );
1762 1762
         if (WP_DEBUG) {
1763
-            $message .= '<br>' . print_r($final_prices_per_ticket_line_item, true);
1763
+            $message .= '<br>'.print_r($final_prices_per_ticket_line_item, true);
1764 1764
             throw new OutOfRangeException($message);
1765 1765
         }
1766 1766
         EE_Log::instance()->log(__CLASS__, __FUNCTION__, $message);
Please login to merge, or discard this patch.
core/services/loaders/ClassInterfaceCache.php 1 patch
Indentation   +161 added lines, -161 removed lines patch added patch discarded remove patch
@@ -17,165 +17,165 @@
 block discarded – undo
17 17
 class ClassInterfaceCache
18 18
 {
19 19
 
20
-    /**
21
-     * array of interfaces indexed by FQCNs where values are arrays of interface FQNs
22
-     *
23
-     * @var string[][] $interfaces
24
-     */
25
-    private $interfaces = array();
26
-
27
-    /**
28
-     * @type string[][] $aliases
29
-     */
30
-    protected $aliases = array();
31
-
32
-
33
-    /**
34
-     * @param string $fqn
35
-     * @return string
36
-     */
37
-    public function getFqn($fqn)
38
-    {
39
-        $fqn = $fqn instanceof FullyQualifiedName ? $fqn->string() : $fqn;
40
-        return ltrim($fqn, '\\');
41
-    }
42
-
43
-
44
-    /**
45
-     * @param string $fqn
46
-     * @return array
47
-     */
48
-    public function getInterfaces($fqn)
49
-    {
50
-        $fqn = $this->getFqn($fqn);
51
-        // have we already seen this FQCN ?
52
-        if (! array_key_exists($fqn, $this->interfaces)) {
53
-            $this->interfaces[ $fqn ] = array();
54
-            if (class_exists($fqn)) {
55
-                $this->interfaces[ $fqn ] = class_implements($fqn, false);
56
-                $this->interfaces[ $fqn ] = $this->interfaces[ $fqn ] !== false
57
-                    ? $this->interfaces[ $fqn ]
58
-                    : array();
59
-            }
60
-        }
61
-        return $this->interfaces[ $fqn ];
62
-    }
63
-
64
-
65
-    /**
66
-     * @param string $fqn
67
-     * @param string $interface
68
-     * @return bool
69
-     */
70
-    public function hasInterface($fqn, $interface)
71
-    {
72
-        $fqn        = $this->getFqn($fqn);
73
-        $interfaces = $this->getInterfaces($fqn);
74
-        return in_array($interface, $interfaces, true);
75
-    }
76
-
77
-
78
-    /**
79
-     * adds an alias for a classname
80
-     *
81
-     * @param string $fqn       the class name that should be used (concrete class to replace interface)
82
-     * @param string $alias     the class name that would be type hinted for (abstract parent or interface)
83
-     * @param string $for_class the class that has the dependency (is type hinting for the interface)
84
-     * @throws InvalidAliasException
85
-     */
86
-    public function addAlias($fqn, $alias, $for_class = '')
87
-    {
88
-        $fqn   = $this->getFqn($fqn);
89
-        $alias = $this->getFqn($alias);
90
-        if (strpos($alias, '\\') !== false && ! is_subclass_of($fqn, $alias)) {
91
-            throw new InvalidAliasException($fqn, $alias);
92
-        }
93
-        // are we adding an alias for a specific class?
94
-        if ($for_class !== '') {
95
-            // make sure it's set up as an array
96
-            if (! isset($this->aliases[ $for_class ])) {
97
-                $this->aliases[ $for_class ] = array();
98
-            }
99
-            $this->aliases[ $for_class ][ $alias ] = $fqn;
100
-            return;
101
-        }
102
-        $this->aliases[ $alias ] = $fqn;
103
-    }
104
-
105
-
106
-    /**
107
-     * returns TRUE if the provided FQN is an alias
108
-     *
109
-     * @param string $fqn
110
-     * @param string $for_class
111
-     * @return bool
112
-     */
113
-    public function isAlias($fqn = '', $for_class = '')
114
-    {
115
-        $fqn = $this->getFqn($fqn);
116
-        if ($this->isAliasForClass($fqn, $for_class)) {
117
-            return true;
118
-        }
119
-        if ($this->isDirectAlias($fqn)) {
120
-            return true;
121
-        }
122
-        return false;
123
-    }
124
-
125
-
126
-    /**
127
-     * returns TRUE if the provided FQN is an alias
128
-     *
129
-     * @param string $fqn
130
-     * @return bool
131
-     */
132
-    protected function isDirectAlias($fqn = '')
133
-    {
134
-        return isset($this->aliases[ (string) $fqn ]) && ! is_array($this->aliases[ (string) $fqn ]);
135
-    }
136
-
137
-
138
-    /**
139
-     * returns TRUE if the provided FQN is an alias for the specified class
140
-     *
141
-     * @param string $fqn
142
-     * @param string $for_class
143
-     * @return bool
144
-     */
145
-    protected function isAliasForClass($fqn = '', $for_class = '')
146
-    {
147
-        return (
148
-            $for_class !== ''
149
-            && isset($this->aliases[ (string) $for_class ][ (string) $fqn ])
150
-        );
151
-    }
152
-
153
-
154
-    /**
155
-     * returns FQN for provided alias if one exists, otherwise returns the original FQN
156
-     * functions recursively, so that multiple aliases can be used to drill down to a FQN
157
-     *  for example:
158
-     *      if the following two entries were added to the aliases array:
159
-     *          array(
160
-     *              'interface_alias'           => 'some\namespace\interface'
161
-     *              'some\namespace\interface'  => 'some\namespace\classname'
162
-     *          )
163
-     *      then one could use Loader::getNew( 'interface_alias' )
164
-     *      to load an instance of 'some\namespace\classname'
165
-     *
166
-     * @param string $alias
167
-     * @param string $for_class
168
-     * @return string
169
-     */
170
-    public function getFqnForAlias($alias = '', $for_class = '')
171
-    {
172
-        $alias = $this->getFqn($alias);
173
-        if ($this->isAliasForClass($alias, $for_class)) {
174
-            return $this->getFqnForAlias($this->aliases[ (string) $for_class ][ (string) $alias ], $for_class);
175
-        }
176
-        if ($this->isDirectAlias($alias)) {
177
-            return $this->getFqnForAlias($this->aliases[ (string) $alias ], '');
178
-        }
179
-        return $alias;
180
-    }
20
+	/**
21
+	 * array of interfaces indexed by FQCNs where values are arrays of interface FQNs
22
+	 *
23
+	 * @var string[][] $interfaces
24
+	 */
25
+	private $interfaces = array();
26
+
27
+	/**
28
+	 * @type string[][] $aliases
29
+	 */
30
+	protected $aliases = array();
31
+
32
+
33
+	/**
34
+	 * @param string $fqn
35
+	 * @return string
36
+	 */
37
+	public function getFqn($fqn)
38
+	{
39
+		$fqn = $fqn instanceof FullyQualifiedName ? $fqn->string() : $fqn;
40
+		return ltrim($fqn, '\\');
41
+	}
42
+
43
+
44
+	/**
45
+	 * @param string $fqn
46
+	 * @return array
47
+	 */
48
+	public function getInterfaces($fqn)
49
+	{
50
+		$fqn = $this->getFqn($fqn);
51
+		// have we already seen this FQCN ?
52
+		if (! array_key_exists($fqn, $this->interfaces)) {
53
+			$this->interfaces[ $fqn ] = array();
54
+			if (class_exists($fqn)) {
55
+				$this->interfaces[ $fqn ] = class_implements($fqn, false);
56
+				$this->interfaces[ $fqn ] = $this->interfaces[ $fqn ] !== false
57
+					? $this->interfaces[ $fqn ]
58
+					: array();
59
+			}
60
+		}
61
+		return $this->interfaces[ $fqn ];
62
+	}
63
+
64
+
65
+	/**
66
+	 * @param string $fqn
67
+	 * @param string $interface
68
+	 * @return bool
69
+	 */
70
+	public function hasInterface($fqn, $interface)
71
+	{
72
+		$fqn        = $this->getFqn($fqn);
73
+		$interfaces = $this->getInterfaces($fqn);
74
+		return in_array($interface, $interfaces, true);
75
+	}
76
+
77
+
78
+	/**
79
+	 * adds an alias for a classname
80
+	 *
81
+	 * @param string $fqn       the class name that should be used (concrete class to replace interface)
82
+	 * @param string $alias     the class name that would be type hinted for (abstract parent or interface)
83
+	 * @param string $for_class the class that has the dependency (is type hinting for the interface)
84
+	 * @throws InvalidAliasException
85
+	 */
86
+	public function addAlias($fqn, $alias, $for_class = '')
87
+	{
88
+		$fqn   = $this->getFqn($fqn);
89
+		$alias = $this->getFqn($alias);
90
+		if (strpos($alias, '\\') !== false && ! is_subclass_of($fqn, $alias)) {
91
+			throw new InvalidAliasException($fqn, $alias);
92
+		}
93
+		// are we adding an alias for a specific class?
94
+		if ($for_class !== '') {
95
+			// make sure it's set up as an array
96
+			if (! isset($this->aliases[ $for_class ])) {
97
+				$this->aliases[ $for_class ] = array();
98
+			}
99
+			$this->aliases[ $for_class ][ $alias ] = $fqn;
100
+			return;
101
+		}
102
+		$this->aliases[ $alias ] = $fqn;
103
+	}
104
+
105
+
106
+	/**
107
+	 * returns TRUE if the provided FQN is an alias
108
+	 *
109
+	 * @param string $fqn
110
+	 * @param string $for_class
111
+	 * @return bool
112
+	 */
113
+	public function isAlias($fqn = '', $for_class = '')
114
+	{
115
+		$fqn = $this->getFqn($fqn);
116
+		if ($this->isAliasForClass($fqn, $for_class)) {
117
+			return true;
118
+		}
119
+		if ($this->isDirectAlias($fqn)) {
120
+			return true;
121
+		}
122
+		return false;
123
+	}
124
+
125
+
126
+	/**
127
+	 * returns TRUE if the provided FQN is an alias
128
+	 *
129
+	 * @param string $fqn
130
+	 * @return bool
131
+	 */
132
+	protected function isDirectAlias($fqn = '')
133
+	{
134
+		return isset($this->aliases[ (string) $fqn ]) && ! is_array($this->aliases[ (string) $fqn ]);
135
+	}
136
+
137
+
138
+	/**
139
+	 * returns TRUE if the provided FQN is an alias for the specified class
140
+	 *
141
+	 * @param string $fqn
142
+	 * @param string $for_class
143
+	 * @return bool
144
+	 */
145
+	protected function isAliasForClass($fqn = '', $for_class = '')
146
+	{
147
+		return (
148
+			$for_class !== ''
149
+			&& isset($this->aliases[ (string) $for_class ][ (string) $fqn ])
150
+		);
151
+	}
152
+
153
+
154
+	/**
155
+	 * returns FQN for provided alias if one exists, otherwise returns the original FQN
156
+	 * functions recursively, so that multiple aliases can be used to drill down to a FQN
157
+	 *  for example:
158
+	 *      if the following two entries were added to the aliases array:
159
+	 *          array(
160
+	 *              'interface_alias'           => 'some\namespace\interface'
161
+	 *              'some\namespace\interface'  => 'some\namespace\classname'
162
+	 *          )
163
+	 *      then one could use Loader::getNew( 'interface_alias' )
164
+	 *      to load an instance of 'some\namespace\classname'
165
+	 *
166
+	 * @param string $alias
167
+	 * @param string $for_class
168
+	 * @return string
169
+	 */
170
+	public function getFqnForAlias($alias = '', $for_class = '')
171
+	{
172
+		$alias = $this->getFqn($alias);
173
+		if ($this->isAliasForClass($alias, $for_class)) {
174
+			return $this->getFqnForAlias($this->aliases[ (string) $for_class ][ (string) $alias ], $for_class);
175
+		}
176
+		if ($this->isDirectAlias($alias)) {
177
+			return $this->getFqnForAlias($this->aliases[ (string) $alias ], '');
178
+		}
179
+		return $alias;
180
+	}
181 181
 }
Please login to merge, or discard this patch.
core/helpers/EEH_Autoloader.helper.php 2 patches
Indentation   +292 added lines, -292 removed lines patch added patch discarded remove patch
@@ -15,296 +15,296 @@
 block discarded – undo
15 15
 {
16 16
 
17 17
 
18
-    /**
19
-     *    instance of the EE_System object
20
-     *
21
-     * @var    $_instance
22
-     * @access    private
23
-     */
24
-    private static $_instance = null;
25
-
26
-    /**
27
-    *   $_autoloaders
28
-    *   @var array $_autoloaders
29
-    *   @access     private
30
-    */
31
-    private static $_autoloaders;
32
-
33
-    /**
34
-     * set to "paths" to display autoloader class => path mappings
35
-     * set to "times" to display autoloader loading times
36
-     * set to "all" to display both
37
-     *
38
-     * @var string $debug
39
-     * @access    private
40
-     */
41
-    public static $debug = false;
42
-
43
-
44
-    /**
45
-     *    class constructor
46
-     *
47
-     * @access    private
48
-     * @return \EEH_Autoloader
49
-     * @throws Exception
50
-     */
51
-    private function __construct()
52
-    {
53
-        if (self::$_autoloaders === null) {
54
-            self::$_autoloaders = array();
55
-            $this->_register_custom_autoloaders();
56
-            spl_autoload_register(array( $this, 'espresso_autoloader' ));
57
-        }
58
-    }
59
-
60
-
61
-
62
-    /**
63
-     * @access public
64
-     * @return EEH_Autoloader
65
-     */
66
-    public static function instance()
67
-    {
68
-        // check if class object is instantiated
69
-        if (! self::$_instance instanceof EEH_Autoloader) {
70
-            self::$_instance = new self();
71
-        }
72
-        return self::$_instance;
73
-    }
74
-
75
-
76
-
77
-    /**
78
-     *    espresso_autoloader
79
-     *
80
-     * @access    public
81
-     * @param   $class_name
82
-     * @internal  param $className
83
-     * @internal  param string $class_name - simple class name ie: session
84
-     * @return  void
85
-     */
86
-    public static function espresso_autoloader($class_name)
87
-    {
88
-        if (isset(self::$_autoloaders[ $class_name ])) {
89
-            require_once(self::$_autoloaders[ $class_name ]);
90
-        }
91
-    }
92
-
93
-
94
-
95
-    /**
96
-     *    register_autoloader
97
-     *
98
-     * @access    public
99
-     * @param array | string $class_paths - array of key => value pairings between class names and paths
100
-     * @param bool           $read_check  true if we need to check whether the file is readable or not.
101
-     * @param bool           $debug **deprecated**
102
-     * @throws \EE_Error
103
-     */
104
-    public static function register_autoloader($class_paths, $read_check = true, $debug = false)
105
-    {
106
-        $class_paths = is_array($class_paths) ? $class_paths : array( $class_paths );
107
-        foreach ($class_paths as $class => $path) {
108
-            // skip all files that are not PHP
109
-            if (substr($path, strlen($path) - 3) !== 'php') {
110
-                continue;
111
-            }
112
-            // don't give up! you gotta...
113
-            // get some class
114
-            if (empty($class)) {
115
-                throw new EE_Error(sprintf(__('No Class name was specified while registering an autoloader for the following path: %s.', 'event_espresso'), $path));
116
-            }
117
-            // one day you will find the path young grasshopper
118
-            if (empty($path)) {
119
-                throw new EE_Error(sprintf(__('No path was specified while registering an autoloader for the %s class.', 'event_espresso'), $class));
120
-            }
121
-            // is file readable ?
122
-            if ($read_check && ! is_readable($path)) {
123
-                throw new EE_Error(sprintf(__('The file for the %s class could not be found or is not readable due to file permissions. Please ensure the following path is correct: %s', 'event_espresso'), $class, $path));
124
-            }
125
-            if (! isset(self::$_autoloaders[ $class ])) {
126
-                self::$_autoloaders[ $class ] = str_replace(array( '/', '\\' ), DS, $path);
127
-                if (EE_DEBUG && ( EEH_Autoloader::$debug === 'paths' || EEH_Autoloader::$debug === 'all' || $debug )) {
128
-                    EEH_Debug_Tools::printr(self::$_autoloaders[ $class ], $class, __FILE__, __LINE__);
129
-                }
130
-            }
131
-        }
132
-    }
133
-
134
-
135
-
136
-
137
-    /**
138
-     *  get_autoloaders
139
-     *
140
-     *  @access public
141
-     *  @return array
142
-     */
143
-    public static function get_autoloaders()
144
-    {
145
-        return self::$_autoloaders;
146
-    }
147
-
148
-
149
-    /**
150
-     *  register core, model and class 'autoloaders'
151
-     *
152
-     * @access private
153
-     * @return void
154
-     * @throws EE_Error
155
-     */
156
-    private function _register_custom_autoloaders()
157
-    {
158
-        EEH_Autoloader::$debug = '';
159
-        \EEH_Autoloader::register_helpers_autoloaders();
160
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE . 'interfaces');
161
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE);
162
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_INTERFACES, true);
163
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_MODELS, true);
164
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CLASSES);
165
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_FORM_SECTIONS, true);
166
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'messages');
167
-        if (EEH_Autoloader::$debug === 'times' || EEH_Autoloader::$debug === 'all') {
168
-            EEH_Debug_Tools::instance()->show_times();
169
-        }
170
-    }
171
-
172
-
173
-
174
-    /**
175
-     *    register core, model and class 'autoloaders'
176
-     *
177
-     * @access public
178
-     */
179
-    public static function register_helpers_autoloaders()
180
-    {
181
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_HELPERS);
182
-    }
183
-
184
-
185
-
186
-
187
-    /**
188
-     *  register core, model and class 'autoloaders'
189
-     *
190
-     *  @access public
191
-     *  @return void
192
-     */
193
-    public static function register_form_sections_autoloaders()
194
-    {
195
-        // EEH_Autoloader::register_autoloaders_for_each_file_in_folder( EE_FORM_SECTIONS, true );
196
-    }
197
-
198
-
199
-    /**
200
-     *  register core, model and class 'autoloaders'
201
-     *
202
-     * @access public
203
-     * @return void
204
-     * @throws EE_Error
205
-     */
206
-    public static function register_line_item_display_autoloaders()
207
-    {
208
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'line_item_display', true);
209
-    }
210
-
211
-
212
-    /**
213
-     *  register core, model and class 'autoloaders'
214
-     *
215
-     * @access public
216
-     * @return void
217
-     * @throws EE_Error
218
-     */
219
-    public static function register_line_item_filter_autoloaders()
220
-    {
221
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'line_item_filters', true);
222
-    }
223
-
224
-
225
-    /**
226
-     *  register template part 'autoloaders'
227
-     *
228
-     * @access public
229
-     * @return void
230
-     * @throws EE_Error
231
-     */
232
-    public static function register_template_part_autoloaders()
233
-    {
234
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'template_parts', true);
235
-    }
236
-
237
-
238
-    /**
239
-     * @return void
240
-     * @throws EE_Error
241
-     */
242
-    public static function register_business_classes()
243
-    {
244
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE . 'business');
245
-    }
246
-
247
-
248
-
249
-    /**
250
-     * Assumes all the files in this folder have the normal naming scheme (namely that their classname
251
-     * is the file's name, plus ".whatever.php".) and adds each of them to the autoloader list.
252
-     * If that's not the case, you'll need to improve this function or just use EEH_File::get_classname_from_filepath_with_standard_filename() directly.
253
-     * Yes this has to scan the directory for files, but it only does it once -- not on EACH
254
-     * time the autoloader is used
255
-     *
256
-     * @param string $folder name, with or without trailing /, doesn't matter
257
-     * @param bool   $recursive
258
-     * @param bool   $debug  **deprecated**
259
-     * @throws \EE_Error
260
-     */
261
-    public static function register_autoloaders_for_each_file_in_folder($folder, $recursive = false, $debug = false)
262
-    {
263
-        if (EEH_Autoloader::$debug === 'times' || EEH_Autoloader::$debug === 'all' || $debug) {
264
-            EEH_Debug_Tools::instance()->start_timer(basename($folder));
265
-        }
266
-        // make sure last char is a /
267
-        $folder .= $folder[ strlen($folder)-1 ] !== DS ? DS : '';
268
-        $class_to_filepath_map = array();
269
-        $exclude = array( 'index' );
270
-        // get all the files in that folder that end in php
271
-        $filepaths = glob($folder.'*');
272
-
273
-        if (empty($filepaths)) {
274
-            return;
275
-        }
276
-
277
-        foreach ($filepaths as $filepath) {
278
-            if (substr($filepath, -4, 4) === '.php') {
279
-                $class_name = EEH_File::get_classname_from_filepath_with_standard_filename($filepath);
280
-                if (! in_array($class_name, $exclude)) {
281
-                    $class_to_filepath_map [ $class_name ] = $filepath;
282
-                }
283
-            } elseif ($recursive) {
284
-                EEH_Autoloader::register_autoloaders_for_each_file_in_folder($filepath, $recursive, $debug);
285
-            }
286
-        }
287
-        // we remove the necessity to do a is_readable() check via the $read_check flag because glob by nature will not return non_readable files/directories.
288
-        self::register_autoloader($class_to_filepath_map, false, $debug);
289
-        if (EEH_Autoloader::$debug === 'times' || EEH_Autoloader::$debug === 'all') {
290
-            EEH_Debug_Tools::instance()->stop_timer(basename($folder));
291
-        }
292
-    }
293
-
294
-
295
-
296
-    /**
297
-     * add_alias
298
-     * register additional autoloader based on variation of the classname for an existing autoloader
299
-     *
300
-     * @access    public
301
-     * @param string $class_name - simple class name ie: EE_Session
302
-     * @param string $alias - variation on class name ie: EE_session, session, etc
303
-     */
304
-    public static function add_alias($class_name, $alias)
305
-    {
306
-        if (isset(self::$_autoloaders[ $class_name ])) {
307
-            self::$_autoloaders[ $alias ] = self::$_autoloaders[ $class_name ];
308
-        }
309
-    }
18
+	/**
19
+	 *    instance of the EE_System object
20
+	 *
21
+	 * @var    $_instance
22
+	 * @access    private
23
+	 */
24
+	private static $_instance = null;
25
+
26
+	/**
27
+	 *   $_autoloaders
28
+	 *   @var array $_autoloaders
29
+	 *   @access     private
30
+	 */
31
+	private static $_autoloaders;
32
+
33
+	/**
34
+	 * set to "paths" to display autoloader class => path mappings
35
+	 * set to "times" to display autoloader loading times
36
+	 * set to "all" to display both
37
+	 *
38
+	 * @var string $debug
39
+	 * @access    private
40
+	 */
41
+	public static $debug = false;
42
+
43
+
44
+	/**
45
+	 *    class constructor
46
+	 *
47
+	 * @access    private
48
+	 * @return \EEH_Autoloader
49
+	 * @throws Exception
50
+	 */
51
+	private function __construct()
52
+	{
53
+		if (self::$_autoloaders === null) {
54
+			self::$_autoloaders = array();
55
+			$this->_register_custom_autoloaders();
56
+			spl_autoload_register(array( $this, 'espresso_autoloader' ));
57
+		}
58
+	}
59
+
60
+
61
+
62
+	/**
63
+	 * @access public
64
+	 * @return EEH_Autoloader
65
+	 */
66
+	public static function instance()
67
+	{
68
+		// check if class object is instantiated
69
+		if (! self::$_instance instanceof EEH_Autoloader) {
70
+			self::$_instance = new self();
71
+		}
72
+		return self::$_instance;
73
+	}
74
+
75
+
76
+
77
+	/**
78
+	 *    espresso_autoloader
79
+	 *
80
+	 * @access    public
81
+	 * @param   $class_name
82
+	 * @internal  param $className
83
+	 * @internal  param string $class_name - simple class name ie: session
84
+	 * @return  void
85
+	 */
86
+	public static function espresso_autoloader($class_name)
87
+	{
88
+		if (isset(self::$_autoloaders[ $class_name ])) {
89
+			require_once(self::$_autoloaders[ $class_name ]);
90
+		}
91
+	}
92
+
93
+
94
+
95
+	/**
96
+	 *    register_autoloader
97
+	 *
98
+	 * @access    public
99
+	 * @param array | string $class_paths - array of key => value pairings between class names and paths
100
+	 * @param bool           $read_check  true if we need to check whether the file is readable or not.
101
+	 * @param bool           $debug **deprecated**
102
+	 * @throws \EE_Error
103
+	 */
104
+	public static function register_autoloader($class_paths, $read_check = true, $debug = false)
105
+	{
106
+		$class_paths = is_array($class_paths) ? $class_paths : array( $class_paths );
107
+		foreach ($class_paths as $class => $path) {
108
+			// skip all files that are not PHP
109
+			if (substr($path, strlen($path) - 3) !== 'php') {
110
+				continue;
111
+			}
112
+			// don't give up! you gotta...
113
+			// get some class
114
+			if (empty($class)) {
115
+				throw new EE_Error(sprintf(__('No Class name was specified while registering an autoloader for the following path: %s.', 'event_espresso'), $path));
116
+			}
117
+			// one day you will find the path young grasshopper
118
+			if (empty($path)) {
119
+				throw new EE_Error(sprintf(__('No path was specified while registering an autoloader for the %s class.', 'event_espresso'), $class));
120
+			}
121
+			// is file readable ?
122
+			if ($read_check && ! is_readable($path)) {
123
+				throw new EE_Error(sprintf(__('The file for the %s class could not be found or is not readable due to file permissions. Please ensure the following path is correct: %s', 'event_espresso'), $class, $path));
124
+			}
125
+			if (! isset(self::$_autoloaders[ $class ])) {
126
+				self::$_autoloaders[ $class ] = str_replace(array( '/', '\\' ), DS, $path);
127
+				if (EE_DEBUG && ( EEH_Autoloader::$debug === 'paths' || EEH_Autoloader::$debug === 'all' || $debug )) {
128
+					EEH_Debug_Tools::printr(self::$_autoloaders[ $class ], $class, __FILE__, __LINE__);
129
+				}
130
+			}
131
+		}
132
+	}
133
+
134
+
135
+
136
+
137
+	/**
138
+	 *  get_autoloaders
139
+	 *
140
+	 *  @access public
141
+	 *  @return array
142
+	 */
143
+	public static function get_autoloaders()
144
+	{
145
+		return self::$_autoloaders;
146
+	}
147
+
148
+
149
+	/**
150
+	 *  register core, model and class 'autoloaders'
151
+	 *
152
+	 * @access private
153
+	 * @return void
154
+	 * @throws EE_Error
155
+	 */
156
+	private function _register_custom_autoloaders()
157
+	{
158
+		EEH_Autoloader::$debug = '';
159
+		\EEH_Autoloader::register_helpers_autoloaders();
160
+		EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE . 'interfaces');
161
+		EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE);
162
+		EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_INTERFACES, true);
163
+		EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_MODELS, true);
164
+		EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CLASSES);
165
+		EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_FORM_SECTIONS, true);
166
+		EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'messages');
167
+		if (EEH_Autoloader::$debug === 'times' || EEH_Autoloader::$debug === 'all') {
168
+			EEH_Debug_Tools::instance()->show_times();
169
+		}
170
+	}
171
+
172
+
173
+
174
+	/**
175
+	 *    register core, model and class 'autoloaders'
176
+	 *
177
+	 * @access public
178
+	 */
179
+	public static function register_helpers_autoloaders()
180
+	{
181
+		EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_HELPERS);
182
+	}
183
+
184
+
185
+
186
+
187
+	/**
188
+	 *  register core, model and class 'autoloaders'
189
+	 *
190
+	 *  @access public
191
+	 *  @return void
192
+	 */
193
+	public static function register_form_sections_autoloaders()
194
+	{
195
+		// EEH_Autoloader::register_autoloaders_for_each_file_in_folder( EE_FORM_SECTIONS, true );
196
+	}
197
+
198
+
199
+	/**
200
+	 *  register core, model and class 'autoloaders'
201
+	 *
202
+	 * @access public
203
+	 * @return void
204
+	 * @throws EE_Error
205
+	 */
206
+	public static function register_line_item_display_autoloaders()
207
+	{
208
+		EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'line_item_display', true);
209
+	}
210
+
211
+
212
+	/**
213
+	 *  register core, model and class 'autoloaders'
214
+	 *
215
+	 * @access public
216
+	 * @return void
217
+	 * @throws EE_Error
218
+	 */
219
+	public static function register_line_item_filter_autoloaders()
220
+	{
221
+		EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'line_item_filters', true);
222
+	}
223
+
224
+
225
+	/**
226
+	 *  register template part 'autoloaders'
227
+	 *
228
+	 * @access public
229
+	 * @return void
230
+	 * @throws EE_Error
231
+	 */
232
+	public static function register_template_part_autoloaders()
233
+	{
234
+		EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'template_parts', true);
235
+	}
236
+
237
+
238
+	/**
239
+	 * @return void
240
+	 * @throws EE_Error
241
+	 */
242
+	public static function register_business_classes()
243
+	{
244
+		EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE . 'business');
245
+	}
246
+
247
+
248
+
249
+	/**
250
+	 * Assumes all the files in this folder have the normal naming scheme (namely that their classname
251
+	 * is the file's name, plus ".whatever.php".) and adds each of them to the autoloader list.
252
+	 * If that's not the case, you'll need to improve this function or just use EEH_File::get_classname_from_filepath_with_standard_filename() directly.
253
+	 * Yes this has to scan the directory for files, but it only does it once -- not on EACH
254
+	 * time the autoloader is used
255
+	 *
256
+	 * @param string $folder name, with or without trailing /, doesn't matter
257
+	 * @param bool   $recursive
258
+	 * @param bool   $debug  **deprecated**
259
+	 * @throws \EE_Error
260
+	 */
261
+	public static function register_autoloaders_for_each_file_in_folder($folder, $recursive = false, $debug = false)
262
+	{
263
+		if (EEH_Autoloader::$debug === 'times' || EEH_Autoloader::$debug === 'all' || $debug) {
264
+			EEH_Debug_Tools::instance()->start_timer(basename($folder));
265
+		}
266
+		// make sure last char is a /
267
+		$folder .= $folder[ strlen($folder)-1 ] !== DS ? DS : '';
268
+		$class_to_filepath_map = array();
269
+		$exclude = array( 'index' );
270
+		// get all the files in that folder that end in php
271
+		$filepaths = glob($folder.'*');
272
+
273
+		if (empty($filepaths)) {
274
+			return;
275
+		}
276
+
277
+		foreach ($filepaths as $filepath) {
278
+			if (substr($filepath, -4, 4) === '.php') {
279
+				$class_name = EEH_File::get_classname_from_filepath_with_standard_filename($filepath);
280
+				if (! in_array($class_name, $exclude)) {
281
+					$class_to_filepath_map [ $class_name ] = $filepath;
282
+				}
283
+			} elseif ($recursive) {
284
+				EEH_Autoloader::register_autoloaders_for_each_file_in_folder($filepath, $recursive, $debug);
285
+			}
286
+		}
287
+		// we remove the necessity to do a is_readable() check via the $read_check flag because glob by nature will not return non_readable files/directories.
288
+		self::register_autoloader($class_to_filepath_map, false, $debug);
289
+		if (EEH_Autoloader::$debug === 'times' || EEH_Autoloader::$debug === 'all') {
290
+			EEH_Debug_Tools::instance()->stop_timer(basename($folder));
291
+		}
292
+	}
293
+
294
+
295
+
296
+	/**
297
+	 * add_alias
298
+	 * register additional autoloader based on variation of the classname for an existing autoloader
299
+	 *
300
+	 * @access    public
301
+	 * @param string $class_name - simple class name ie: EE_Session
302
+	 * @param string $alias - variation on class name ie: EE_session, session, etc
303
+	 */
304
+	public static function add_alias($class_name, $alias)
305
+	{
306
+		if (isset(self::$_autoloaders[ $class_name ])) {
307
+			self::$_autoloaders[ $alias ] = self::$_autoloaders[ $class_name ];
308
+		}
309
+	}
310 310
 }
Please login to merge, or discard this patch.
Spacing   +21 added lines, -21 removed lines patch added patch discarded remove patch
@@ -53,7 +53,7 @@  discard block
 block discarded – undo
53 53
         if (self::$_autoloaders === null) {
54 54
             self::$_autoloaders = array();
55 55
             $this->_register_custom_autoloaders();
56
-            spl_autoload_register(array( $this, 'espresso_autoloader' ));
56
+            spl_autoload_register(array($this, 'espresso_autoloader'));
57 57
         }
58 58
     }
59 59
 
@@ -66,7 +66,7 @@  discard block
 block discarded – undo
66 66
     public static function instance()
67 67
     {
68 68
         // check if class object is instantiated
69
-        if (! self::$_instance instanceof EEH_Autoloader) {
69
+        if ( ! self::$_instance instanceof EEH_Autoloader) {
70 70
             self::$_instance = new self();
71 71
         }
72 72
         return self::$_instance;
@@ -85,8 +85,8 @@  discard block
 block discarded – undo
85 85
      */
86 86
     public static function espresso_autoloader($class_name)
87 87
     {
88
-        if (isset(self::$_autoloaders[ $class_name ])) {
89
-            require_once(self::$_autoloaders[ $class_name ]);
88
+        if (isset(self::$_autoloaders[$class_name])) {
89
+            require_once(self::$_autoloaders[$class_name]);
90 90
         }
91 91
     }
92 92
 
@@ -103,7 +103,7 @@  discard block
 block discarded – undo
103 103
      */
104 104
     public static function register_autoloader($class_paths, $read_check = true, $debug = false)
105 105
     {
106
-        $class_paths = is_array($class_paths) ? $class_paths : array( $class_paths );
106
+        $class_paths = is_array($class_paths) ? $class_paths : array($class_paths);
107 107
         foreach ($class_paths as $class => $path) {
108 108
             // skip all files that are not PHP
109 109
             if (substr($path, strlen($path) - 3) !== 'php') {
@@ -122,10 +122,10 @@  discard block
 block discarded – undo
122 122
             if ($read_check && ! is_readable($path)) {
123 123
                 throw new EE_Error(sprintf(__('The file for the %s class could not be found or is not readable due to file permissions. Please ensure the following path is correct: %s', 'event_espresso'), $class, $path));
124 124
             }
125
-            if (! isset(self::$_autoloaders[ $class ])) {
126
-                self::$_autoloaders[ $class ] = str_replace(array( '/', '\\' ), DS, $path);
127
-                if (EE_DEBUG && ( EEH_Autoloader::$debug === 'paths' || EEH_Autoloader::$debug === 'all' || $debug )) {
128
-                    EEH_Debug_Tools::printr(self::$_autoloaders[ $class ], $class, __FILE__, __LINE__);
125
+            if ( ! isset(self::$_autoloaders[$class])) {
126
+                self::$_autoloaders[$class] = str_replace(array('/', '\\'), DS, $path);
127
+                if (EE_DEBUG && (EEH_Autoloader::$debug === 'paths' || EEH_Autoloader::$debug === 'all' || $debug)) {
128
+                    EEH_Debug_Tools::printr(self::$_autoloaders[$class], $class, __FILE__, __LINE__);
129 129
                 }
130 130
             }
131 131
         }
@@ -157,13 +157,13 @@  discard block
 block discarded – undo
157 157
     {
158 158
         EEH_Autoloader::$debug = '';
159 159
         \EEH_Autoloader::register_helpers_autoloaders();
160
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE . 'interfaces');
160
+        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE.'interfaces');
161 161
         EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE);
162 162
         EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_INTERFACES, true);
163 163
         EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_MODELS, true);
164 164
         EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CLASSES);
165 165
         EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_FORM_SECTIONS, true);
166
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'messages');
166
+        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES.'messages');
167 167
         if (EEH_Autoloader::$debug === 'times' || EEH_Autoloader::$debug === 'all') {
168 168
             EEH_Debug_Tools::instance()->show_times();
169 169
         }
@@ -205,7 +205,7 @@  discard block
 block discarded – undo
205 205
      */
206 206
     public static function register_line_item_display_autoloaders()
207 207
     {
208
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'line_item_display', true);
208
+        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES.'line_item_display', true);
209 209
     }
210 210
 
211 211
 
@@ -218,7 +218,7 @@  discard block
 block discarded – undo
218 218
      */
219 219
     public static function register_line_item_filter_autoloaders()
220 220
     {
221
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'line_item_filters', true);
221
+        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES.'line_item_filters', true);
222 222
     }
223 223
 
224 224
 
@@ -231,7 +231,7 @@  discard block
 block discarded – undo
231 231
      */
232 232
     public static function register_template_part_autoloaders()
233 233
     {
234
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'template_parts', true);
234
+        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES.'template_parts', true);
235 235
     }
236 236
 
237 237
 
@@ -241,7 +241,7 @@  discard block
 block discarded – undo
241 241
      */
242 242
     public static function register_business_classes()
243 243
     {
244
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE . 'business');
244
+        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_CORE.'business');
245 245
     }
246 246
 
247 247
 
@@ -264,9 +264,9 @@  discard block
 block discarded – undo
264 264
             EEH_Debug_Tools::instance()->start_timer(basename($folder));
265 265
         }
266 266
         // make sure last char is a /
267
-        $folder .= $folder[ strlen($folder)-1 ] !== DS ? DS : '';
267
+        $folder .= $folder[strlen($folder) - 1] !== DS ? DS : '';
268 268
         $class_to_filepath_map = array();
269
-        $exclude = array( 'index' );
269
+        $exclude = array('index');
270 270
         // get all the files in that folder that end in php
271 271
         $filepaths = glob($folder.'*');
272 272
 
@@ -277,8 +277,8 @@  discard block
 block discarded – undo
277 277
         foreach ($filepaths as $filepath) {
278 278
             if (substr($filepath, -4, 4) === '.php') {
279 279
                 $class_name = EEH_File::get_classname_from_filepath_with_standard_filename($filepath);
280
-                if (! in_array($class_name, $exclude)) {
281
-                    $class_to_filepath_map [ $class_name ] = $filepath;
280
+                if ( ! in_array($class_name, $exclude)) {
281
+                    $class_to_filepath_map [$class_name] = $filepath;
282 282
                 }
283 283
             } elseif ($recursive) {
284 284
                 EEH_Autoloader::register_autoloaders_for_each_file_in_folder($filepath, $recursive, $debug);
@@ -303,8 +303,8 @@  discard block
 block discarded – undo
303 303
      */
304 304
     public static function add_alias($class_name, $alias)
305 305
     {
306
-        if (isset(self::$_autoloaders[ $class_name ])) {
307
-            self::$_autoloaders[ $alias ] = self::$_autoloaders[ $class_name ];
306
+        if (isset(self::$_autoloaders[$class_name])) {
307
+            self::$_autoloaders[$alias] = self::$_autoloaders[$class_name];
308 308
         }
309 309
     }
310 310
 }
Please login to merge, or discard this patch.
form_sections/strategies/layout/EE_Form_Section_Layout_Base.strategy.php 2 patches
Indentation   +284 added lines, -284 removed lines patch added patch discarded remove patch
@@ -12,288 +12,288 @@
 block discarded – undo
12 12
 abstract class EE_Form_Section_Layout_Base
13 13
 {
14 14
 
15
-    /**
16
-     * Form form section to lay out
17
-     *
18
-     * @var EE_Form_Section_Proper
19
-     */
20
-    protected $_form_section;
21
-
22
-
23
-
24
-    /**
25
-     *  __construct
26
-     */
27
-    public function __construct()
28
-    {
29
-    }
30
-
31
-
32
-
33
-    /**
34
-     * The form section on which this strategy is to perform
35
-     *
36
-     * @param EE_Form_Section_Proper $form
37
-     */
38
-    public function _construct_finalize(EE_Form_Section_Proper $form)
39
-    {
40
-        $this->_form_section = $form;
41
-    }
42
-
43
-
44
-
45
-    /**
46
-     * @return EE_Form_Section_Proper
47
-     */
48
-    public function form_section()
49
-    {
50
-        return $this->_form_section;
51
-    }
52
-
53
-
54
-
55
-    /**
56
-     * Also has teh side effect of enqueuing any needed JS and CSS for
57
-     * this form.
58
-     * Creates all the HTML necessary for displaying this form, its inputs, and
59
-     * proper subsections.
60
-     * Returns the HTML
61
-     *
62
-     * @return string HTML for displaying
63
-     * @throws EE_Error
64
-     */
65
-    public function layout_form()
66
-    {
67
-        $html = '';
68
-        // layout_form_begin
69
-        $html .= apply_filters(
70
-            'FHEE__EE_Form_Section_Layout_Base__layout_form__start__for_' . $this->_form_section->name(),
71
-            $this->layout_form_begin(),
72
-            $this->_form_section
73
-        );
74
-        // layout_form_loop
75
-        $html .= apply_filters(
76
-            'FHEE__EE_Form_Section_Layout_Base__layout_form__loop__for_' . $this->_form_section->name(),
77
-            $this->layout_form_loop(),
78
-            $this->_form_section
79
-        );
80
-        // layout_form_end
81
-        $html .= apply_filters(
82
-            'FHEE__EE_Form_Section_Layout_Base__layout_form__end__for_' . $this->_form_section->name(),
83
-            $this->layout_form_end(),
84
-            $this->_form_section
85
-        );
86
-        $html = $this->add_form_section_hooks_and_filters($html);
87
-        return $html;
88
-    }
89
-
90
-
91
-
92
-    /**
93
-     * @return string
94
-     * @throws EE_Error
95
-     */
96
-    public function layout_form_loop()
97
-    {
98
-        $html = '';
99
-        foreach ($this->_form_section->subsections() as $name => $subsection) {
100
-            if ($subsection instanceof EE_Form_Input_Base) {
101
-                $html .= apply_filters(
102
-                    'FHEE__EE_Form_Section_Layout_Base__layout_form__loop_for_input_'
103
-                    . $name . '__in_' . $this->_form_section->name(),
104
-                    $this->layout_input($subsection),
105
-                    $this->_form_section,
106
-                    $subsection
107
-                );
108
-            } elseif ($subsection instanceof EE_Form_Section_Base) {
109
-                $html .= apply_filters(
110
-                    'FHEE__EE_Form_Section_Layout_Base__layout_form__loop_for_non_input_'
111
-                    . $name . '__in_' . $this->_form_section->name(),
112
-                    $this->layout_subsection($subsection),
113
-                    $this->_form_section,
114
-                    $subsection
115
-                );
116
-            }
117
-        }
118
-        return $html;
119
-    }
120
-
121
-
122
-
123
-    /**
124
-     * Should be used to start teh form section (Eg a table tag, or a div tag, etc.)
125
-     *
126
-     * @return string
127
-     */
128
-    abstract public function layout_form_begin();
129
-
130
-
131
-
132
-    /**
133
-     * Should be used to end the form section (eg a /table tag, or a /div tag, etc)
134
-     *
135
-     * @return string
136
-     */
137
-    abstract public function layout_form_end();
138
-
139
-
140
-
141
-    /**
142
-     * Should be used internally by layout_form() to layout each input (eg, if this layout
143
-     * is putting each input in a row of its own, this should probably be called by a
144
-     *  foreach loop in layout_form() (WITHOUT adding any content directly within layout_form()'s foreach loop.
145
-     * Eg, this method should add the tr and td tags). This method is exposed in case you want to completely
146
-     * customize the form's layout, but would like to make use of it for laying out
147
-     * 'easy-to-layout' inputs
148
-     *
149
-     * @param EE_Form_Input_Base $input
150
-     * @return string html
151
-     */
152
-    abstract public function layout_input($input);
153
-
154
-
155
-
156
-    /**
157
-     * Similar to layout_input(), should be used internally by layout_form() within a
158
-     * loop to layout each proper subsection. Unlike layout_input(), however, it is assumed
159
-     * that the proper subsection will layout its container, label, etc on its own.
160
-     *
161
-     * @param EE_Form_Section_Base $subsection
162
-     * @return string html
163
-     */
164
-    abstract public function layout_subsection($subsection);
165
-
166
-
167
-
168
-    /**
169
-     * Gets the HTML for the label tag and its contents for the input
170
-     *
171
-     * @param EE_Form_Input_Base $input
172
-     * @return string
173
-     */
174
-    public function display_label($input)
175
-    {
176
-        if ($input->get_display_strategy() instanceof EE_Hidden_Display_Strategy) {
177
-            return '';
178
-        }
179
-        $class = $input->required()
180
-            ? 'ee-required-label ' . $input->html_label_class()
181
-            : $input->html_label_class();
182
-        $label_text = $input->required()
183
-            ? $input->html_label_text() . '<span class="ee-asterisk">*</span>'
184
-            : $input->html_label_text();
185
-        return '<label id="'
186
-               . $input->html_label_id()
187
-               . '" class="'
188
-               . $class
189
-               . '" style="'
190
-               . $input->html_label_style()
191
-               . '" for="' . $input->html_id()
192
-               . '">'
193
-               . $label_text
194
-               . '</label>';
195
-    }
196
-
197
-
198
-
199
-    /**
200
-     * Gets the HTML for all the form's form-wide errors (ie, errors which
201
-     * are not for specific inputs. E.g., if two inputs somehow disagree,
202
-     * those errors would probably be on the form section, not one of its inputs)
203
-     * @return string
204
-     */
205
-    public function display_form_wide_errors()
206
-    {
207
-        $html = '';
208
-        if ($this->_form_section->get_validation_errors()) {
209
-            $html .= "<div class='ee-form-wide-errors'>";
210
-            // get all the errors on THIS form section (errors which aren't
211
-            // for specific inputs, but instead for the entire form section)
212
-            foreach ($this->_form_section->get_validation_errors() as $error) {
213
-                $html .= $error->getMessage() . '<br>';
214
-            }
215
-            $html .= '</div>';
216
-        }
217
-        return apply_filters(
218
-            'FHEE__EE_Form_Section_Layout_Base__display_form_wide_errors',
219
-            $html,
220
-            $this
221
-        );
222
-    }
223
-
224
-
225
-
226
-    /**
227
-     * returns the HTML for the server-side validation errors for the specified input
228
-     * Note that if JS is enabled, it should remove these and instead
229
-     * populate the form's errors in the jquery validate fashion
230
-     * using the localized data provided to the JS
231
-     *
232
-     * @param EE_Form_Input_Base $input
233
-     * @return string
234
-     */
235
-    public function display_errors($input)
236
-    {
237
-        if ($input->get_validation_errors()) {
238
-            return "<label  id='"
239
-                   . $input->html_id()
240
-                   . "-error' class='error' for='{$input->html_name()}'>"
241
-                   . $input->get_validation_error_string()
242
-                   . '</label>';
243
-        }
244
-        return '';
245
-    }
246
-
247
-
248
-
249
-    /**
250
-     * Displays the help span for the specified input
251
-     *
252
-     * @param EE_Form_Input_Base $input
253
-     * @return string
254
-     */
255
-    public function display_help_text($input)
256
-    {
257
-        $help_text  = $input->html_help_text();
258
-        if ($help_text !== '' && $help_text !== null) {
259
-            $tag = is_admin() ? 'p' : 'span';
260
-            return '<'
261
-                   . $tag
262
-                   . ' id="'
263
-                   . $input->html_id()
264
-                   . '-help" class="'
265
-                   . $input->html_help_class()
266
-                   . '" style="'
267
-                   . $input->html_help_style()
268
-                   . '">'
269
-                   . $help_text
270
-                   . '</'
271
-                   . $tag
272
-                   . '>';
273
-        }
274
-        return '';
275
-    }
276
-
277
-
278
-
279
-    /**
280
-     * Does an action and hook onto the end of teh form
281
-     *
282
-     * @param string $html
283
-     * @return string
284
-     */
285
-    public function add_form_section_hooks_and_filters($html)
286
-    {
287
-        // replace dashes and spaces with underscores
288
-        $hook_name = str_replace(array('-', ' '), '_', $this->_form_section->html_id());
289
-        do_action('AHEE__Form_Section_Layout__' . $hook_name, $this->_form_section);
290
-        $html = (string) apply_filters(
291
-            'AFEE__Form_Section_Layout__' . $hook_name . '__html',
292
-            $html,
293
-            $this->_form_section
294
-        );
295
-        $html .= EEH_HTML::nl() . '<!-- AHEE__Form_Section_Layout__' . $hook_name . '__html -->';
296
-        $html .= EEH_HTML::nl() . '<!-- AFEE__Form_Section_Layout__' . $hook_name . ' -->';
297
-        return $html;
298
-    }
15
+	/**
16
+	 * Form form section to lay out
17
+	 *
18
+	 * @var EE_Form_Section_Proper
19
+	 */
20
+	protected $_form_section;
21
+
22
+
23
+
24
+	/**
25
+	 *  __construct
26
+	 */
27
+	public function __construct()
28
+	{
29
+	}
30
+
31
+
32
+
33
+	/**
34
+	 * The form section on which this strategy is to perform
35
+	 *
36
+	 * @param EE_Form_Section_Proper $form
37
+	 */
38
+	public function _construct_finalize(EE_Form_Section_Proper $form)
39
+	{
40
+		$this->_form_section = $form;
41
+	}
42
+
43
+
44
+
45
+	/**
46
+	 * @return EE_Form_Section_Proper
47
+	 */
48
+	public function form_section()
49
+	{
50
+		return $this->_form_section;
51
+	}
52
+
53
+
54
+
55
+	/**
56
+	 * Also has teh side effect of enqueuing any needed JS and CSS for
57
+	 * this form.
58
+	 * Creates all the HTML necessary for displaying this form, its inputs, and
59
+	 * proper subsections.
60
+	 * Returns the HTML
61
+	 *
62
+	 * @return string HTML for displaying
63
+	 * @throws EE_Error
64
+	 */
65
+	public function layout_form()
66
+	{
67
+		$html = '';
68
+		// layout_form_begin
69
+		$html .= apply_filters(
70
+			'FHEE__EE_Form_Section_Layout_Base__layout_form__start__for_' . $this->_form_section->name(),
71
+			$this->layout_form_begin(),
72
+			$this->_form_section
73
+		);
74
+		// layout_form_loop
75
+		$html .= apply_filters(
76
+			'FHEE__EE_Form_Section_Layout_Base__layout_form__loop__for_' . $this->_form_section->name(),
77
+			$this->layout_form_loop(),
78
+			$this->_form_section
79
+		);
80
+		// layout_form_end
81
+		$html .= apply_filters(
82
+			'FHEE__EE_Form_Section_Layout_Base__layout_form__end__for_' . $this->_form_section->name(),
83
+			$this->layout_form_end(),
84
+			$this->_form_section
85
+		);
86
+		$html = $this->add_form_section_hooks_and_filters($html);
87
+		return $html;
88
+	}
89
+
90
+
91
+
92
+	/**
93
+	 * @return string
94
+	 * @throws EE_Error
95
+	 */
96
+	public function layout_form_loop()
97
+	{
98
+		$html = '';
99
+		foreach ($this->_form_section->subsections() as $name => $subsection) {
100
+			if ($subsection instanceof EE_Form_Input_Base) {
101
+				$html .= apply_filters(
102
+					'FHEE__EE_Form_Section_Layout_Base__layout_form__loop_for_input_'
103
+					. $name . '__in_' . $this->_form_section->name(),
104
+					$this->layout_input($subsection),
105
+					$this->_form_section,
106
+					$subsection
107
+				);
108
+			} elseif ($subsection instanceof EE_Form_Section_Base) {
109
+				$html .= apply_filters(
110
+					'FHEE__EE_Form_Section_Layout_Base__layout_form__loop_for_non_input_'
111
+					. $name . '__in_' . $this->_form_section->name(),
112
+					$this->layout_subsection($subsection),
113
+					$this->_form_section,
114
+					$subsection
115
+				);
116
+			}
117
+		}
118
+		return $html;
119
+	}
120
+
121
+
122
+
123
+	/**
124
+	 * Should be used to start teh form section (Eg a table tag, or a div tag, etc.)
125
+	 *
126
+	 * @return string
127
+	 */
128
+	abstract public function layout_form_begin();
129
+
130
+
131
+
132
+	/**
133
+	 * Should be used to end the form section (eg a /table tag, or a /div tag, etc)
134
+	 *
135
+	 * @return string
136
+	 */
137
+	abstract public function layout_form_end();
138
+
139
+
140
+
141
+	/**
142
+	 * Should be used internally by layout_form() to layout each input (eg, if this layout
143
+	 * is putting each input in a row of its own, this should probably be called by a
144
+	 *  foreach loop in layout_form() (WITHOUT adding any content directly within layout_form()'s foreach loop.
145
+	 * Eg, this method should add the tr and td tags). This method is exposed in case you want to completely
146
+	 * customize the form's layout, but would like to make use of it for laying out
147
+	 * 'easy-to-layout' inputs
148
+	 *
149
+	 * @param EE_Form_Input_Base $input
150
+	 * @return string html
151
+	 */
152
+	abstract public function layout_input($input);
153
+
154
+
155
+
156
+	/**
157
+	 * Similar to layout_input(), should be used internally by layout_form() within a
158
+	 * loop to layout each proper subsection. Unlike layout_input(), however, it is assumed
159
+	 * that the proper subsection will layout its container, label, etc on its own.
160
+	 *
161
+	 * @param EE_Form_Section_Base $subsection
162
+	 * @return string html
163
+	 */
164
+	abstract public function layout_subsection($subsection);
165
+
166
+
167
+
168
+	/**
169
+	 * Gets the HTML for the label tag and its contents for the input
170
+	 *
171
+	 * @param EE_Form_Input_Base $input
172
+	 * @return string
173
+	 */
174
+	public function display_label($input)
175
+	{
176
+		if ($input->get_display_strategy() instanceof EE_Hidden_Display_Strategy) {
177
+			return '';
178
+		}
179
+		$class = $input->required()
180
+			? 'ee-required-label ' . $input->html_label_class()
181
+			: $input->html_label_class();
182
+		$label_text = $input->required()
183
+			? $input->html_label_text() . '<span class="ee-asterisk">*</span>'
184
+			: $input->html_label_text();
185
+		return '<label id="'
186
+			   . $input->html_label_id()
187
+			   . '" class="'
188
+			   . $class
189
+			   . '" style="'
190
+			   . $input->html_label_style()
191
+			   . '" for="' . $input->html_id()
192
+			   . '">'
193
+			   . $label_text
194
+			   . '</label>';
195
+	}
196
+
197
+
198
+
199
+	/**
200
+	 * Gets the HTML for all the form's form-wide errors (ie, errors which
201
+	 * are not for specific inputs. E.g., if two inputs somehow disagree,
202
+	 * those errors would probably be on the form section, not one of its inputs)
203
+	 * @return string
204
+	 */
205
+	public function display_form_wide_errors()
206
+	{
207
+		$html = '';
208
+		if ($this->_form_section->get_validation_errors()) {
209
+			$html .= "<div class='ee-form-wide-errors'>";
210
+			// get all the errors on THIS form section (errors which aren't
211
+			// for specific inputs, but instead for the entire form section)
212
+			foreach ($this->_form_section->get_validation_errors() as $error) {
213
+				$html .= $error->getMessage() . '<br>';
214
+			}
215
+			$html .= '</div>';
216
+		}
217
+		return apply_filters(
218
+			'FHEE__EE_Form_Section_Layout_Base__display_form_wide_errors',
219
+			$html,
220
+			$this
221
+		);
222
+	}
223
+
224
+
225
+
226
+	/**
227
+	 * returns the HTML for the server-side validation errors for the specified input
228
+	 * Note that if JS is enabled, it should remove these and instead
229
+	 * populate the form's errors in the jquery validate fashion
230
+	 * using the localized data provided to the JS
231
+	 *
232
+	 * @param EE_Form_Input_Base $input
233
+	 * @return string
234
+	 */
235
+	public function display_errors($input)
236
+	{
237
+		if ($input->get_validation_errors()) {
238
+			return "<label  id='"
239
+				   . $input->html_id()
240
+				   . "-error' class='error' for='{$input->html_name()}'>"
241
+				   . $input->get_validation_error_string()
242
+				   . '</label>';
243
+		}
244
+		return '';
245
+	}
246
+
247
+
248
+
249
+	/**
250
+	 * Displays the help span for the specified input
251
+	 *
252
+	 * @param EE_Form_Input_Base $input
253
+	 * @return string
254
+	 */
255
+	public function display_help_text($input)
256
+	{
257
+		$help_text  = $input->html_help_text();
258
+		if ($help_text !== '' && $help_text !== null) {
259
+			$tag = is_admin() ? 'p' : 'span';
260
+			return '<'
261
+				   . $tag
262
+				   . ' id="'
263
+				   . $input->html_id()
264
+				   . '-help" class="'
265
+				   . $input->html_help_class()
266
+				   . '" style="'
267
+				   . $input->html_help_style()
268
+				   . '">'
269
+				   . $help_text
270
+				   . '</'
271
+				   . $tag
272
+				   . '>';
273
+		}
274
+		return '';
275
+	}
276
+
277
+
278
+
279
+	/**
280
+	 * Does an action and hook onto the end of teh form
281
+	 *
282
+	 * @param string $html
283
+	 * @return string
284
+	 */
285
+	public function add_form_section_hooks_and_filters($html)
286
+	{
287
+		// replace dashes and spaces with underscores
288
+		$hook_name = str_replace(array('-', ' '), '_', $this->_form_section->html_id());
289
+		do_action('AHEE__Form_Section_Layout__' . $hook_name, $this->_form_section);
290
+		$html = (string) apply_filters(
291
+			'AFEE__Form_Section_Layout__' . $hook_name . '__html',
292
+			$html,
293
+			$this->_form_section
294
+		);
295
+		$html .= EEH_HTML::nl() . '<!-- AHEE__Form_Section_Layout__' . $hook_name . '__html -->';
296
+		$html .= EEH_HTML::nl() . '<!-- AFEE__Form_Section_Layout__' . $hook_name . ' -->';
297
+		return $html;
298
+	}
299 299
 }
Please login to merge, or discard this patch.
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -67,19 +67,19 @@  discard block
 block discarded – undo
67 67
         $html = '';
68 68
         // layout_form_begin
69 69
         $html .= apply_filters(
70
-            'FHEE__EE_Form_Section_Layout_Base__layout_form__start__for_' . $this->_form_section->name(),
70
+            'FHEE__EE_Form_Section_Layout_Base__layout_form__start__for_'.$this->_form_section->name(),
71 71
             $this->layout_form_begin(),
72 72
             $this->_form_section
73 73
         );
74 74
         // layout_form_loop
75 75
         $html .= apply_filters(
76
-            'FHEE__EE_Form_Section_Layout_Base__layout_form__loop__for_' . $this->_form_section->name(),
76
+            'FHEE__EE_Form_Section_Layout_Base__layout_form__loop__for_'.$this->_form_section->name(),
77 77
             $this->layout_form_loop(),
78 78
             $this->_form_section
79 79
         );
80 80
         // layout_form_end
81 81
         $html .= apply_filters(
82
-            'FHEE__EE_Form_Section_Layout_Base__layout_form__end__for_' . $this->_form_section->name(),
82
+            'FHEE__EE_Form_Section_Layout_Base__layout_form__end__for_'.$this->_form_section->name(),
83 83
             $this->layout_form_end(),
84 84
             $this->_form_section
85 85
         );
@@ -100,7 +100,7 @@  discard block
 block discarded – undo
100 100
             if ($subsection instanceof EE_Form_Input_Base) {
101 101
                 $html .= apply_filters(
102 102
                     'FHEE__EE_Form_Section_Layout_Base__layout_form__loop_for_input_'
103
-                    . $name . '__in_' . $this->_form_section->name(),
103
+                    . $name.'__in_'.$this->_form_section->name(),
104 104
                     $this->layout_input($subsection),
105 105
                     $this->_form_section,
106 106
                     $subsection
@@ -108,7 +108,7 @@  discard block
 block discarded – undo
108 108
             } elseif ($subsection instanceof EE_Form_Section_Base) {
109 109
                 $html .= apply_filters(
110 110
                     'FHEE__EE_Form_Section_Layout_Base__layout_form__loop_for_non_input_'
111
-                    . $name . '__in_' . $this->_form_section->name(),
111
+                    . $name.'__in_'.$this->_form_section->name(),
112 112
                     $this->layout_subsection($subsection),
113 113
                     $this->_form_section,
114 114
                     $subsection
@@ -177,10 +177,10 @@  discard block
 block discarded – undo
177 177
             return '';
178 178
         }
179 179
         $class = $input->required()
180
-            ? 'ee-required-label ' . $input->html_label_class()
180
+            ? 'ee-required-label '.$input->html_label_class()
181 181
             : $input->html_label_class();
182 182
         $label_text = $input->required()
183
-            ? $input->html_label_text() . '<span class="ee-asterisk">*</span>'
183
+            ? $input->html_label_text().'<span class="ee-asterisk">*</span>'
184 184
             : $input->html_label_text();
185 185
         return '<label id="'
186 186
                . $input->html_label_id()
@@ -188,7 +188,7 @@  discard block
 block discarded – undo
188 188
                . $class
189 189
                . '" style="'
190 190
                . $input->html_label_style()
191
-               . '" for="' . $input->html_id()
191
+               . '" for="'.$input->html_id()
192 192
                . '">'
193 193
                . $label_text
194 194
                . '</label>';
@@ -210,7 +210,7 @@  discard block
 block discarded – undo
210 210
             // get all the errors on THIS form section (errors which aren't
211 211
             // for specific inputs, but instead for the entire form section)
212 212
             foreach ($this->_form_section->get_validation_errors() as $error) {
213
-                $html .= $error->getMessage() . '<br>';
213
+                $html .= $error->getMessage().'<br>';
214 214
             }
215 215
             $html .= '</div>';
216 216
         }
@@ -254,7 +254,7 @@  discard block
 block discarded – undo
254 254
      */
255 255
     public function display_help_text($input)
256 256
     {
257
-        $help_text  = $input->html_help_text();
257
+        $help_text = $input->html_help_text();
258 258
         if ($help_text !== '' && $help_text !== null) {
259 259
             $tag = is_admin() ? 'p' : 'span';
260 260
             return '<'
@@ -286,14 +286,14 @@  discard block
 block discarded – undo
286 286
     {
287 287
         // replace dashes and spaces with underscores
288 288
         $hook_name = str_replace(array('-', ' '), '_', $this->_form_section->html_id());
289
-        do_action('AHEE__Form_Section_Layout__' . $hook_name, $this->_form_section);
289
+        do_action('AHEE__Form_Section_Layout__'.$hook_name, $this->_form_section);
290 290
         $html = (string) apply_filters(
291
-            'AFEE__Form_Section_Layout__' . $hook_name . '__html',
291
+            'AFEE__Form_Section_Layout__'.$hook_name.'__html',
292 292
             $html,
293 293
             $this->_form_section
294 294
         );
295
-        $html .= EEH_HTML::nl() . '<!-- AHEE__Form_Section_Layout__' . $hook_name . '__html -->';
296
-        $html .= EEH_HTML::nl() . '<!-- AFEE__Form_Section_Layout__' . $hook_name . ' -->';
295
+        $html .= EEH_HTML::nl().'<!-- AHEE__Form_Section_Layout__'.$hook_name.'__html -->';
296
+        $html .= EEH_HTML::nl().'<!-- AFEE__Form_Section_Layout__'.$hook_name.' -->';
297 297
         return $html;
298 298
     }
299 299
 }
Please login to merge, or discard this patch.