| 1 |
<?php |
| 2 |
namespace Elementor\Modules\System_Info\Reporters; |
| 3 |
|
| 4 |
use Elementor\Modules\System_Info\Helpers\Model_Helper; |
| 5 |
use Elementor\Utils; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Elementor base reporter. |
| 13 |
* |
| 14 |
* A base abstract class that provides the needed properties and methods to |
| 15 |
* manage and handle reporter in inheriting classes. |
| 16 |
* |
| 17 |
* @since 2.9.0 |
| 18 |
* @abstract |
| 19 |
*/ |
| 20 |
abstract class Base { |
| 21 |
|
| 22 |
/** |
| 23 |
* Reporter properties. |
| 24 |
* |
| 25 |
* Holds the list of all the properties of the report. |
| 26 |
* |
| 27 |
* @access protected |
| 28 |
* @static |
| 29 |
* |
| 30 |
* @var array |
| 31 |
*/ |
| 32 |
protected $_properties; |
| 33 |
|
| 34 |
/** |
| 35 |
* Get report title. |
| 36 |
* |
| 37 |
* Retrieve the title of the report. |
| 38 |
* |
| 39 |
* @since 2.9.0 |
| 40 |
* @access public |
| 41 |
* @abstract |
| 42 |
*/ |
| 43 |
abstract public function get_title(); |
| 44 |
|
| 45 |
/** |
| 46 |
* Get report fields. |
| 47 |
* |
| 48 |
* Retrieve the required fields for the report. |
| 49 |
* |
| 50 |
* @since 2.9.0 |
| 51 |
* @access public |
| 52 |
* @abstract |
| 53 |
*/ |
| 54 |
abstract public function get_fields(); |
| 55 |
|
| 56 |
/** |
| 57 |
* Is report enabled. |
| 58 |
* |
| 59 |
* Whether the report is enabled. |
| 60 |
* |
| 61 |
* @since 2.9.0 |
| 62 |
* @access public |
| 63 |
* |
| 64 |
* @return bool Whether the report is enabled. |
| 65 |
*/ |
| 66 |
public function is_enabled() { |
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
public function print_html() { |
| 71 |
foreach ( $this->get_report( 'html' ) as $field ) { |
| 72 |
$warning_class = ! empty( $field['warning'] ) ? ' class="elementor-warning"' : ''; |
| 73 |
$log_label = ! empty( $field['label'] ) ? $field['label'] . ':' : ''; |
| 74 |
?> |
| 75 |
<tr<?php Utils::print_unescaped_internal_string( $warning_class ); ?>> |
| 76 |
<td><?php Utils::print_unescaped_internal_string( $log_label ); ?></td> |
| 77 |
<td><?php Utils::print_unescaped_internal_string( $field['value'] ); ?></td> |
| 78 |
<td><?php |
| 79 |
if ( ! empty( $field['recommendation'] ) ) : |
| 80 |
Utils::print_unescaped_internal_string( $field['recommendation'] ); |
| 81 |
endif; |
| 82 |
?></td> |
| 83 |
</tr> |
| 84 |
<?php |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
public function print_html_label( $label ) { |
| 89 |
Utils::print_unescaped_internal_string( $label ); |
| 90 |
} |
| 91 |
|
| 92 |
public function print_raw( $tabs_count ) { |
| 93 |
$indent = str_repeat( "\t", $tabs_count - 1 ); |
| 94 |
|
| 95 |
$report = $this->get_report( 'raw' ); |
| 96 |
|
| 97 |
echo PHP_EOL . $indent . '== ' . $this->get_title() . ' =='; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 98 |
echo PHP_EOL; |
| 99 |
|
| 100 |
foreach ( $report as $field_name => $field ) : |
| 101 |
$sub_indent = str_repeat( "\t", $tabs_count ); |
| 102 |
|
| 103 |
$label = $field['label']; |
| 104 |
|
| 105 |
if ( ! empty( $label ) ) { |
| 106 |
$label .= ': '; |
| 107 |
} |
| 108 |
echo "{$sub_indent}{$label}{$field['value']}" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 109 |
endforeach; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get report. |
| 114 |
* |
| 115 |
* Retrieve the report with all it's containing fields. |
| 116 |
* |
| 117 |
* @since 2.9.0 |
| 118 |
* @access public |
| 119 |
* |
| 120 |
* @return \WP_Error | array { |
| 121 |
* Report fields. |
| 122 |
* |
| 123 |
* @type string $name Field name. |
| 124 |
* @type string $label Field label. |
| 125 |
* } |
| 126 |
*/ |
| 127 |
final public function get_report( $format = '' ) { |
| 128 |
$result = []; |
| 129 |
|
| 130 |
$format = ( empty( $format ) ) ? '' : $format . '_'; |
| 131 |
|
| 132 |
foreach ( $this->get_fields() as $field_name => $field_label ) { |
| 133 |
$method = 'get_' . $format . $field_name; |
| 134 |
|
| 135 |
if ( ! method_exists( $this, $method ) ) { |
| 136 |
$method = 'get_' . $field_name; |
| 137 |
// fallback: |
| 138 |
if ( ! method_exists( $this, $method ) ) { |
| 139 |
return new \WP_Error( sprintf( "Getter method for the field '%s' wasn't found in %s.", $field_name, get_called_class() ) ); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
$reporter_field = [ |
| 144 |
'name' => $field_name, |
| 145 |
'label' => $field_label, |
| 146 |
]; |
| 147 |
|
| 148 |
$reporter_field = array_merge( $reporter_field, $this->$method() ); |
| 149 |
$result[ $field_name ] = $reporter_field; |
| 150 |
} |
| 151 |
|
| 152 |
return $result; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Get properties keys. |
| 157 |
* |
| 158 |
* Retrieve the keys of the properties. |
| 159 |
* |
| 160 |
* @since 2.9.0 |
| 161 |
* @access public |
| 162 |
* @static |
| 163 |
* |
| 164 |
* @return array { |
| 165 |
* Property keys. |
| 166 |
* |
| 167 |
* @type string $name Property name. |
| 168 |
* @type string $fields Property fields. |
| 169 |
* } |
| 170 |
*/ |
| 171 |
public static function get_properties_keys() { |
| 172 |
return [ |
| 173 |
'name', |
| 174 |
'format', |
| 175 |
'fields', |
| 176 |
]; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Filter possible properties. |
| 181 |
* |
| 182 |
* Retrieve possible properties filtered by property keys. |
| 183 |
* |
| 184 |
* @since 2.9.0 |
| 185 |
* @access public |
| 186 |
* @static |
| 187 |
* |
| 188 |
* @param array $properties Properties to filter. |
| 189 |
* |
| 190 |
* @return array Possible properties filtered by property keys. |
| 191 |
*/ |
| 192 |
final public static function filter_possible_properties( $properties ) { |
| 193 |
return Model_Helper::filter_possible_properties( self::get_properties_keys(), $properties ); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Set properties. |
| 198 |
* |
| 199 |
* Add/update properties to the report. |
| 200 |
* |
| 201 |
* @since 2.9.0 |
| 202 |
* @access public |
| 203 |
* |
| 204 |
* @param array $key Property key. |
| 205 |
* @param array $value Optional. Property value. Default is `null`. |
| 206 |
*/ |
| 207 |
final public function set_properties( $key, $value = null ) { |
| 208 |
if ( is_array( $key ) ) { |
| 209 |
$key = self::filter_possible_properties( $key ); |
| 210 |
|
| 211 |
foreach ( $key as $sub_key => $sub_value ) { |
| 212 |
$this->set_properties( $sub_key, $sub_value ); |
| 213 |
} |
| 214 |
|
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
if ( ! in_array( $key, self::get_properties_keys(), true ) ) { |
| 219 |
return; |
| 220 |
} |
| 221 |
|
| 222 |
$this->_properties[ $key ] = $value; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Reporter base constructor. |
| 227 |
* |
| 228 |
* Initializing the reporter base class. |
| 229 |
* |
| 230 |
* @since 2.9.0 |
| 231 |
* @access public |
| 232 |
* |
| 233 |
* @param array $properties Optional. Properties to filter. Default is `null`. |
| 234 |
*/ |
| 235 |
public function __construct( $properties = null ) { |
| 236 |
$this->_properties = array_fill_keys( self::get_properties_keys(), null ); |
| 237 |
|
| 238 |
if ( $properties ) { |
| 239 |
$this->set_properties( $properties, null ); |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|