balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

base.php (4050B)


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