| 1 |
<?php |
| 2 |
/** |
| 3 |
* The Abstract class inherited by all conflicts. |
| 4 |
* |
| 5 |
* @package \Optimole\Inc\Conflicts |
| 6 |
* @author Optimole <friends@optimole.com> |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Optml_Abstract_Conflict |
| 11 |
* |
| 12 |
* @since 2.0.6 |
| 13 |
*/ |
| 14 |
abstract class Optml_Abstract_Conflict { |
| 15 |
/** |
| 16 |
* Constant for low severity. |
| 17 |
* |
| 18 |
* @since 2.0.6 |
| 19 |
* @const string SEVERITY_LOW |
| 20 |
*/ |
| 21 |
const SEVERITY_LOW = 'low'; |
| 22 |
/** |
| 23 |
* Constant for medium severity. |
| 24 |
* |
| 25 |
* @since 2.0.6 |
| 26 |
* @const string SEVERITY_MEDIUM |
| 27 |
*/ |
| 28 |
const SEVERITY_MEDIUM = 'medium'; |
| 29 |
/** |
| 30 |
* Constant for high severity. |
| 31 |
* |
| 32 |
* @since 2.0.6 |
| 33 |
* @const string SEVERITY_HIGH |
| 34 |
*/ |
| 35 |
const SEVERITY_HIGH = 'high'; |
| 36 |
/** |
| 37 |
* The type of the conflict if required. |
| 38 |
* |
| 39 |
* @since 2.0.6 |
| 40 |
* @access protected |
| 41 |
* @var string $type |
| 42 |
*/ |
| 43 |
protected $type = 'base_conflict'; |
| 44 |
/** |
| 45 |
* Level of conflict severity. |
| 46 |
* |
| 47 |
* @since 2.0.6 |
| 48 |
* @access protected |
| 49 |
* @var string $severity |
| 50 |
*/ |
| 51 |
protected $severity = self::SEVERITY_LOW; |
| 52 |
/** |
| 53 |
* Message of the conflict. |
| 54 |
* |
| 55 |
* @since 2.0.6 |
| 56 |
* @access protected |
| 57 |
* @var string |
| 58 |
*/ |
| 59 |
protected $message = ''; |
| 60 |
/** |
| 61 |
* Priority of conflict for same level of severity conflicts. |
| 62 |
* |
| 63 |
* @since 2.0.6 |
| 64 |
* @access protected |
| 65 |
* @var int |
| 66 |
*/ |
| 67 |
protected $priority = 1; |
| 68 |
|
| 69 |
/** |
| 70 |
* Optml_Abstract_Conflict constructor. |
| 71 |
*/ |
| 72 |
public function __construct() { |
| 73 |
$this->define_message(); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Set the message property |
| 78 |
* |
| 79 |
* @since 2.0.6 |
| 80 |
* @access public |
| 81 |
*/ |
| 82 |
abstract public function define_message(); |
| 83 |
|
| 84 |
/** |
| 85 |
* Checks if conflict is active. |
| 86 |
* |
| 87 |
* @param array $dismissed_conflicts A list of dismissed conflicts. Passed by the manager. |
| 88 |
* |
| 89 |
* @return bool |
| 90 |
* @since 2.0.6 |
| 91 |
* @access public |
| 92 |
*/ |
| 93 |
public function is_active( $dismissed_conflicts = [] ) { |
| 94 |
$conflict_id = $this->get_id(); |
| 95 |
if ( isset( $dismissed_conflicts[ $conflict_id ] ) && $dismissed_conflicts[ $conflict_id ] === 'true' ) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
return $this->is_conflict_valid(); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get the id for the conflict. |
| 104 |
* |
| 105 |
* @param int $length Optional. A length for the generated ID. |
| 106 |
* |
| 107 |
* @return bool|string |
| 108 |
* @since 2.0.6 |
| 109 |
* @access public |
| 110 |
*/ |
| 111 |
public function get_id( $length = 8 ) { |
| 112 |
$hash = sha1( strtolower( get_called_class() ) . $this->type . $this->severity . $this->priority ); |
| 113 |
|
| 114 |
return substr( $hash, 0, $length ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Determine if conflict is applicable. |
| 119 |
* |
| 120 |
* @return bool |
| 121 |
* @since 2.0.6 |
| 122 |
* @access public |
| 123 |
*/ |
| 124 |
abstract public function is_conflict_valid(); |
| 125 |
|
| 126 |
/** |
| 127 |
* Get the conflict information. |
| 128 |
* |
| 129 |
* @return array |
| 130 |
* @since 2.0.6 |
| 131 |
* @access public |
| 132 |
*/ |
| 133 |
public function get_conflict() { |
| 134 |
return [ |
| 135 |
'id' => $this->get_id(), |
| 136 |
'type' => $this->type, |
| 137 |
'priority' => $this->priority, |
| 138 |
'severity' => $this->severity, |
| 139 |
'message' => $this->message, |
| 140 |
]; |
| 141 |
} |
| 142 |
} |
| 143 |
|