PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.3.53
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.3.53
1.4.17 1.4.16 1.4.15 1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 All 180 releases
disco / vendor / micropackage / requirements / src / Requirements.php

Requirements.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.3.53, at vendor/micropackage/requirements/src/Requirements.php

293 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Requirements class
4 *
5 * @package micropackage/requirements
6 */
7
8 namespace Micropackage\Requirements;
9
10 use Micropackage\Internationalization\Internationalization;
11 use Micropackage\Requirements\Interfaces\Checkable;
12 use Micropackage\Requirements\Checker;
13
14 /**
15 * Requirements class
16 */
17 class Requirements {
18
19 /**
20 * Plugin display name
21 *
22 * @var string
23 */
24 private $plugin_name;
25
26 /**
27 * Textdomain
28 *
29 * @var string
30 */
31 public static $textdomain = 'micropackage-requirements';
32
33 /**
34 * Requirements array
35 *
36 * @var array
37 */
38 protected $requirements = [];
39
40 /**
41 * Checkers array
42 *
43 * @var array
44 */
45 protected $checkers = [];
46
47 /**
48 * Errors array
49 *
50 * @var array
51 */
52 protected $errors = [];
53
54 /**
55 * If check has been performed
56 *
57 * @var bool
58 */
59 private $did_check = false;
60
61 /**
62 * Requirements constructor
63 *
64 * @since 1.0.0
65 * @param string $plugin_name Plugin display name.
66 * @param array $requirements Array with requirements.
67 * @param bool $autoload_checkers If default checkers should be autoloaded.
68 * Default: true.
69 */
70 public function __construct( $plugin_name, $requirements = [], $autoload_checkers = true ) {
71 $this->plugin_name = $plugin_name;
72
73 // Add requirements.
74 array_map( [ $this, 'add' ], array_keys( $requirements ), $requirements );
75
76 // Register default checkers.
77 if ( $autoload_checkers ) {
78 $this->load_default_checkers();
79 }
80
81 // Load translation.
82 $i18n = new Internationalization( 'micropackage-requirements', dirname( __DIR__ ) . '/languages' );
83 $i18n->load_translation();
84 }
85
86 /**
87 * Loads default checkers
88 *
89 * @since 1.0.0
90 * @return void
91 */
92 public function load_default_checkers() {
93 array_map(
94 [ $this, 'register_checker' ],
95 [
96 Checker\DocHooks::class,
97 Checker\PHP::class,
98 Checker\PHPExtensions::class,
99 Checker\Plugins::class,
100 Checker\SSL::class,
101 Checker\Theme::class,
102 Checker\WP::class,
103 ]
104 );
105 }
106
107 /**
108 * Adds the requirement to collection
109 *
110 * @since 1.0.0
111 * @throws \Exception When requirement with given slug already added.
112 * @param string $requirement_slug Check slug.
113 * @param mixed $checked_value Value to check.
114 * @return $this
115 */
116 public function add( $requirement_slug, $checked_value ) {
117 if ( isset( $this->requirements[ $requirement_slug ] ) ) {
118 throw new \Exception( sprintf( 'Requirement %s already exists', $requirement_slug ) );
119 }
120
121 $this->requirements[ $requirement_slug ] = $checked_value;
122
123 return $this;
124 }
125
126 /**
127 * Gets all the requirements
128 *
129 * @since 1.0.0
130 * @return array
131 */
132 public function get() {
133 return $this->requirements;
134 }
135
136 /**
137 * Registers checker
138 *
139 * @since 1.0.0
140 * @throws \Exception When checker doesn't implement given interface.
141 * @throws \Exception When checker with given name already registered.
142 * @param mixed $checker Checker class instance or \
143 * fully qualified class name.
144 * @return $this
145 */
146 public function register_checker( $checker ) {
147 $implements = class_implements( $checker );
148 $interface = Checkable::class;
149
150 if ( ! isset( $implements[ $interface ] ) ) {
151 throw new \Exception( sprintf( 'Checker must implement %s interface', $interface ) );
152 }
153
154 if ( is_string( $checker ) ) {
155 $checker = new $checker();
156 }
157
158 if ( isset( $this->checkers[ $checker->get_name() ] ) ) {
159 throw new \Exception( sprintf( 'Checker %s already exists', $checker->get_name() ) );
160 }
161
162 $this->checkers[ $checker->get_name() ] = $checker;
163
164 return $this;
165 }
166
167 /**
168 * Checks if the checker has been registered
169 *
170 * @since 1.0.0
171 * @param string $name Checker name.
172 * @return bool
173 */
174 public function has_checker( $name ) {
175 return isset( $this->checkers[ $name ] );
176 }
177
178 /**
179 * Gets checker instance
180 *
181 * @since 1.0.0
182 * @param string $name Checker name.
183 * @return false|Checkable
184 */
185 public function get_checker( $name ) {
186 if ( ! $this->has_checker( $name ) ) {
187 return false;
188 }
189
190 return $this->checkers[ $name ];
191 }
192
193 /**
194 * Checks the requirements
195 *
196 * @since 1.0.0
197 * @return void
198 */
199 public function check() {
200 // Reset state.
201 $this->errors = [];
202
203 foreach ( $this->get() as $checker_name => $requirement ) {
204 if ( $this->has_checker( $checker_name ) ) {
205 call_user_func( [ $this->get_checker( $checker_name ), 'check' ], $requirement );
206 $this->errors = array_merge( $this->errors, $this->get_checker( $checker_name )->get_errors() );
207 }
208 }
209
210 $this->did_check = true;
211 }
212
213 /**
214 * Determines if all the requirements has been satisfied
215 *
216 * @since 1.0.0
217 * @return bool
218 */
219 public function satisfied() {
220 if ( ! $this->did_check ) {
221 $this->check();
222 }
223
224 return empty( $this->errors );
225 }
226
227 /**
228 * Returns message with requirements info if any of them is not met.
229 *
230 * @since 1.2.0
231 * @param string|null $message Message to display.
232 * @return string|null Message or null if requirements are met.
233 */
234 protected function get_message( $message = null ) {
235 if ( $this->satisfied() ) {
236 return null;
237 }
238
239 if ( ! is_string( $message ) || '' === $message ) {
240 return sprintf(
241 // Translators: Plugin name.
242 __( 'The plugin: <strong>%s</strong> cannot be activated.', self::$textdomain ),
243 esc_html( $this->plugin_name )
244 );
245 }
246
247 return $message;
248 }
249
250 /**
251 * Prints notice
252 *
253 * @since 1.0.0
254 * @param string|null $message Message to display.
255 * @return void
256 */
257 public function print_notice( $message = null ) {
258 $message = $this->get_message( $message );
259
260 if ( null === $message ) {
261 return;
262 }
263
264 add_action( 'admin_notices', function() use ( $message ) {
265 include __DIR__ . '/notice.php';
266 } );
267 }
268
269 /**
270 * Runs wp_die with proper message if any of the checks failed.
271 * This method shoudl be used interchangeably with `print_notice`.
272 *
273 * @since 1.2.0
274 * @param string|null $message Message to display.
275 * @return void
276 */
277 public function kill( $message = null ) {
278 $message = $this->get_message( $message );
279
280 if ( null === $message ) {
281 return;
282 }
283
284 ob_start();
285
286 include __DIR__ . '/die-message.php';
287
288 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
289 wp_die( ob_get_clean(), wp_strip_all_tags( $message ) );
290 }
291
292 }
293