Data_Encryption.php
1 month ago
PluginSilentUpgrader.php
1 month ago
PluginSilentUpgraderSkin.php
1 month ago
SBR_Error_Handler.php
1 month ago
SBR_Install_Skin.php
1 month ago
SBR_Error_Handler.php
171 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Summary of namespace SmashBalloon\Reviews\Common\Helpers |
| 5 | */ |
| 6 | |
| 7 | namespace SmashBalloon\Reviews\Common\Helpers; |
| 8 | |
| 9 | /** |
| 10 | * Summary of SBR_Error_Handler |
| 11 | */ |
| 12 | class SBR_Error_Handler |
| 13 | { |
| 14 | /** |
| 15 | * Errors Options |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | private static $errors_opt = 'sbr_errors'; |
| 20 | |
| 21 | /** |
| 22 | * Get All Errors |
| 23 | * |
| 24 | * @return array |
| 25 | * |
| 26 | * @since 1.0 |
| 27 | */ |
| 28 | public static function get_errors() |
| 29 | { |
| 30 | $errors = get_option(self::$errors_opt, []); |
| 31 | |
| 32 | // get_option() returns the stored value whenever the option row exists, |
| 33 | // even when that value is falsy (false / '' from a corrupted, legacy or |
| 34 | // third-party write) — the [] default only applies when the row is |
| 35 | // absent. Coerce any non-array to [] so the foreach() in check_error(), |
| 36 | // the array_push() in log_error() and the array_merge() in |
| 37 | // update_errors() never receive a bool and fatal on PHP 8. SMASH-1544. |
| 38 | return is_array($errors) ? $errors : []; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Clear All Logs |
| 43 | * |
| 44 | * @return void |
| 45 | * |
| 46 | * @since 1.0 |
| 47 | */ |
| 48 | public static function clear_all_errors() |
| 49 | { |
| 50 | update_option(self::$errors_opt, []); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Update Reviews Feed Errors |
| 55 | * |
| 56 | * @param array $error Error to update |
| 57 | * |
| 58 | * @return boolean |
| 59 | * |
| 60 | * @since 1.0 |
| 61 | */ |
| 62 | public static function update_errors($errors, $type = 'merge') |
| 63 | { |
| 64 | $current_errors = self::get_errors(); |
| 65 | $updated_errors = $type === 'merge' |
| 66 | ? array_merge($current_errors, $errors) |
| 67 | : $errors; |
| 68 | |
| 69 | $updated_errors = self::truncate_errors($updated_errors); |
| 70 | |
| 71 | return update_option(self::$errors_opt, $updated_errors); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Check if Error Exists |
| 76 | * |
| 77 | * @param array $error |
| 78 | * |
| 79 | * @return int|boolean |
| 80 | * |
| 81 | * @since 1.0 |
| 82 | */ |
| 83 | public static function check_error($error) |
| 84 | { |
| 85 | $current_errors = self::get_errors(); |
| 86 | $exists = 'not_defined'; |
| 87 | foreach ($current_errors as $key => $error_elm) { |
| 88 | // Skip malformed error entries |
| 89 | if (! isset($error_elm['type'], $error_elm['id'], $error['type'], $error['id'])) { |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | if ( |
| 94 | empty($error_elm['type']) || |
| 95 | empty($error_elm['id']) || |
| 96 | empty($error['type']) || |
| 97 | empty($error['id']) || |
| 98 | $error_elm['type'] === $error['type'] && |
| 99 | $error_elm['id'] === $error['id'] && |
| 100 | ( |
| 101 | ( |
| 102 | ! empty($error_elm['provider']) && |
| 103 | $error_elm['provider'] === $error['provider'] |
| 104 | ) || empty($error_elm['provider']) |
| 105 | ) |
| 106 | ) { |
| 107 | $exists = $key; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | return $exists; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Log API Error |
| 116 | * |
| 117 | * @param array $erros Error to add |
| 118 | * |
| 119 | * @return void |
| 120 | * |
| 121 | * @since 1.0 |
| 122 | */ |
| 123 | public static function log_error($error) |
| 124 | { |
| 125 | $current_errors = self::get_errors(); |
| 126 | $error_index = self::check_error($error); |
| 127 | |
| 128 | if ($error_index !== 'not_defined') { |
| 129 | $current_errors[$error_index] = $error; |
| 130 | } else { |
| 131 | array_push( |
| 132 | $current_errors, |
| 133 | $error |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | self::update_errors($current_errors, 'no_merge'); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Log Only the Latest 20 error |
| 142 | * |
| 143 | * @param array $erros Error |
| 144 | * |
| 145 | * @return array |
| 146 | * |
| 147 | * @since 1.0 |
| 148 | */ |
| 149 | public static function truncate_errors($errors) |
| 150 | { |
| 151 | return array_slice($errors, -20); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Clear All Logs Ajax |
| 156 | * |
| 157 | * @return void |
| 158 | * |
| 159 | * @since 1.0 |
| 160 | */ |
| 161 | public static function clear_all_error_ajax() |
| 162 | { |
| 163 | check_ajax_referer('sbr-admin', 'nonce'); |
| 164 | if (!sbr_current_user_can('manage_reviews_feed_options')) { |
| 165 | wp_send_json_error(); |
| 166 | } |
| 167 | self::clear_all_errors(); |
| 168 | wp_send_json_success(); |
| 169 | } |
| 170 | } |
| 171 |