PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.21
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.21
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / rosell-dk / webp-convert / src / Convert / Converters / ConverterTraits / ExecTrait.php

ExecTrait.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.21, at vendor/rosell-dk/webp-convert/src/Convert/Converters/ConverterTraits/ExecTrait.php

108 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WebPConvert\Convert\Converters\ConverterTraits;
4
5 use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException;
6 use ExecWithFallback\ExecWithFallback;
7
8 /**
9 * Trait for converters that uses exec() or similar
10 *
11 * @package WebPConvert
12 * @author Bjørn Rosell <[email protected]>
13 * @since Class available since Release 2.0.0
14 */
15 trait ExecTrait
16 {
17
18 abstract protected function logLn($msg, $style = '');
19
20
21 /**
22 * Helper function for examining if "nice" command is available
23 *
24 * @return boolean true if nice is available
25 */
26 protected static function hasNiceSupport()
27 {
28 ExecWithFallback::exec("nice 2>&1", $niceOutput);
29
30 if (is_array($niceOutput) && isset($niceOutput[0])) {
31 if (preg_match('/usage/', $niceOutput[0]) || (preg_match('/^\d+$/', $niceOutput[0]))) {
32 /*
33 * Nice is available - default niceness (+10)
34 * https://www.lifewire.com/uses-of-commands-nice-renice-2201087
35 * https://www.computerhope.com/unix/unice.htm
36 */
37
38 return true;
39 }
40 return false;
41 }
42 return false; // to satisfy phpstan
43 }
44
45 protected function checkNiceSupport()
46 {
47 $ok = self::hasNiceSupport();
48 if ($ok) {
49 $this->logLn('Tested "nice" command - it works :)');
50 } else {
51 $this->logLn(
52 '**No "nice" support. To save a few ms, you can disable the "use-nice" option.**'
53 );
54 }
55 return $ok;
56 }
57
58 protected static function niceOption()
59 {
60 return ['use-nice', 'boolean', [
61 'title' => 'Use nice',
62 'description' =>
63 'If *use-nice* is set, it will be examined if the *nice* command is available. ' .
64 'If it is, the binary is executed using *nice*. This assigns low priority to the process and ' .
65 'will save system resources - but result in slower conversion.',
66 'default' => true,
67 'ui' => [
68 'component' => 'checkbox',
69 'advanced' => true
70 ]
71 ]];
72 }
73
74 /**
75 * Logs output from the exec call.
76 *
77 * @param array $output
78 *
79 * @return void
80 */
81 protected function logExecOutput($output)
82 {
83 if (is_array($output) && count($output) > 0) {
84 $this->logLn('');
85 $this->logLn('Output:', 'italic');
86 foreach ($output as $line) {
87 $this->logLn(print_r($line, true));
88 }
89 $this->logLn('');
90 }
91 }
92
93 /**
94 * Check basic operationality of exec converters (that the "exec" or similar function is available)
95 *
96 * @throws WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException
97 * @return void
98 */
99 public function checkOperationalityExecTrait()
100 {
101 if (!ExecWithFallback::anyAvailable()) {
102 throw new SystemRequirementsNotMetException(
103 'exec() is not enabled (nor is alternative methods, such as proc_open())'
104 );
105 }
106 }
107 }
108