| 1 |
<?php |
| 2 |
/** |
| 3 |
* Troubleshoot Case. |
| 4 |
* |
| 5 |
* @since 0.0.0 |
| 6 |
* @package Troubleshoot |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Troubleshoot Case. |
| 11 |
* |
| 12 |
* @since 0.0.0 |
| 13 |
*/ |
| 14 |
class PDT_Cases { |
| 15 |
/** |
| 16 |
* Parent plugin class. |
| 17 |
* |
| 18 |
* @since 0.0.0 |
| 19 |
* |
| 20 |
* @var Troubleshoot |
| 21 |
*/ |
| 22 |
protected $plugin = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* Constructor. |
| 26 |
* |
| 27 |
* @since 0.0.0 |
| 28 |
* |
| 29 |
* @param Troubleshoot $plugin Main plugin object. |
| 30 |
*/ |
| 31 |
public function __construct( $plugin ) { |
| 32 |
$this->plugin = $plugin; |
| 33 |
$this->hooks(); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Initiate our hooks. |
| 38 |
* |
| 39 |
* @since 0.0.0 |
| 40 |
*/ |
| 41 |
public function hooks() { |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
public function get_schema() { |
| 46 |
return array( |
| 47 |
'id' => array( |
| 48 |
'name' => 'id', |
| 49 |
'access' => 'private', |
| 50 |
'type' => 'int', |
| 51 |
), |
| 52 |
'user_id' => array( |
| 53 |
'name' => 'user_id', |
| 54 |
'access' => 'private', |
| 55 |
'type' => 'int', |
| 56 |
), |
| 57 |
'status' => array( |
| 58 |
'name' => 'status', |
| 59 |
'access' => 'private', |
| 60 |
'type' => 'string', |
| 61 |
), |
| 62 |
'stage' => array( |
| 63 |
'name' => 'stage', |
| 64 |
'access' => 'private', |
| 65 |
'type' => 'string', |
| 66 |
), |
| 67 |
'outcome' => array( |
| 68 |
'name' => 'outcome', |
| 69 |
'access' => 'private', |
| 70 |
'type' => 'string', |
| 71 |
), |
| 72 |
'url' => array( |
| 73 |
'name' => 'url', |
| 74 |
'access' => 'public', |
| 75 |
'type' => 'string', |
| 76 |
), |
| 77 |
'clues' => array( |
| 78 |
'name' => 'clues', |
| 79 |
'access' => 'private', |
| 80 |
'type' => 'array', |
| 81 |
), |
| 82 |
'all_suspects' => array( |
| 83 |
'name' => 'all_suspects', |
| 84 |
'access' => 'private', |
| 85 |
'type' => 'array', |
| 86 |
), |
| 87 |
'required_plugins' => array( |
| 88 |
'name' => 'required_plugins', |
| 89 |
'access' => 'public', |
| 90 |
'type' => 'array', |
| 91 |
), |
| 92 |
'suspects_under_interrogation' => array( |
| 93 |
'name' => 'suspects_under_interrogation', |
| 94 |
'access' => 'private', |
| 95 |
'type' => 'array', |
| 96 |
), |
| 97 |
'cleared_suspects' => array( |
| 98 |
'name' => 'cleared_suspects', |
| 99 |
'access' => 'private', |
| 100 |
'type' => 'array', |
| 101 |
), |
| 102 |
'prime_suspects' => array( |
| 103 |
'name' => 'prime_suspects', |
| 104 |
'access' => 'private', |
| 105 |
'type' => 'array', |
| 106 |
), |
| 107 |
'culprits' => array( |
| 108 |
'name' => 'culprits', |
| 109 |
'access' => 'private', |
| 110 |
'type' => 'array', |
| 111 |
), |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
public function open( $args=array() ) { |
| 116 |
// Check for an active case first (only one case at a time) |
| 117 |
$active = $this->get_active( $args ); |
| 118 |
if ( is_a( $active, 'WP_Error' ) && $active->get_error_code() !== 'no_active_case' ) { |
| 119 |
return $active; |
| 120 |
} |
| 121 |
|
| 122 |
$all_cases = $this->get_all(); |
| 123 |
$new_case_id = substr( wp_hash( site_url() . time() ), 0, 10 ); |
| 124 |
if ( in_array( $new_case_id, $all_cases ) ) { |
| 125 |
return new WP_Error( 'error_creating_case', __( 'Plugin Detective was unable to open a new case. Please contact support.', 'plugin-detective' ) ); |
| 126 |
} |
| 127 |
$all_cases[] = $new_case_id; |
| 128 |
if ( isset( $args['id'] )) { |
| 129 |
unset( $args['id'] ); |
| 130 |
} |
| 131 |
if ( isset( $args['status'] )) { |
| 132 |
unset( $args['status'] ); |
| 133 |
} |
| 134 |
foreach ($this->get_schema() as $key => $field) { |
| 135 |
if ( isset( $args[$field['name']] ) && $field['access'] == 'private' ) { |
| 136 |
unset( $args[$field['name']] ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
$active_plugins = array(); |
| 141 |
$all_plugins = $this->plugin->installed->get_plugins(); |
| 142 |
foreach ($all_plugins as $key => $plugin) { |
| 143 |
if ( empty( $plugin['Active'] ) ) { |
| 144 |
continue; |
| 145 |
} |
| 146 |
|
| 147 |
if ( empty( $plugin['slug'] ) ) { |
| 148 |
continue; |
| 149 |
} |
| 150 |
|
| 151 |
$active_plugins[$plugin['plugin_file']] = $plugin; |
| 152 |
} |
| 153 |
|
| 154 |
update_option( 'pdt_tmp_active_plugins_backup_from_case_opening', get_option( 'active_plugins' ) ); |
| 155 |
|
| 156 |
$this->create( array_merge( array( |
| 157 |
'id' => $new_case_id, |
| 158 |
'user_id' => 0, |
| 159 |
'status' => 'open', |
| 160 |
'stage' => 'investigating', |
| 161 |
'date_opened' => gmdate( 'Y-m-d H:i:s' ), |
| 162 |
'url' => site_url(), |
| 163 |
'required_plugins' => array(), |
| 164 |
'all_suspects' => $active_plugins, |
| 165 |
'cleared_suspects' => array(), |
| 166 |
'suspects_under_interrogation' => array(), |
| 167 |
'prime_suspects' => array(), |
| 168 |
'clues' => array(), |
| 169 |
'culprits' => array(), |
| 170 |
'outcome' => '', |
| 171 |
), $args ) ); |
| 172 |
update_option( 'pdt_active_case_id', $new_case_id ); |
| 173 |
update_option( 'pdt_cases', json_encode( $all_cases ) ); |
| 174 |
|
| 175 |
return $this->get_active(); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
public function suspend( $args=array() ) { |
| 180 |
$active_case = $this->get_active( $args ); |
| 181 |
if ( is_a( $active_case, 'WP_Error' ) ) { |
| 182 |
return $active_case; |
| 183 |
} |
| 184 |
|
| 185 |
$active_case['status'] = 'suspended'; |
| 186 |
$response = $this->update( $active_case ); |
| 187 |
if ( is_a( $response, 'WP_Error' ) ) { |
| 188 |
return $response; |
| 189 |
} |
| 190 |
|
| 191 |
delete_option( 'pdt_active_case_id' ); |
| 192 |
|
| 193 |
return $this->get( $active_case ); |
| 194 |
} |
| 195 |
|
| 196 |
public function close( $args=array() ) { |
| 197 |
$args = shortcode_atts( array( |
| 198 |
'reset_active_plugins' => false, |
| 199 |
), $args ); |
| 200 |
|
| 201 |
$active_case = $this->get_active( $args ); |
| 202 |
if ( is_a( $active_case, 'WP_Error' ) ) { |
| 203 |
return $active_case; |
| 204 |
} |
| 205 |
|
| 206 |
if ( !empty( $active_case['status'] ) && $active_case['status'] == 'closed' ) { |
| 207 |
return $active_case; |
| 208 |
} |
| 209 |
|
| 210 |
if ( !empty( $args['reset_active_plugins'] ) ) { |
| 211 |
// Revert WP active_plugins to value when we opened the case |
| 212 |
if ( isset( $active_case['all_suspects'] ) && is_array( $active_case['all_suspects'] ) ) { |
| 213 |
$this->plugin->plugins->set_active( array_keys( $active_case['all_suspects'] ) ); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
$active_case['status'] = 'closed'; |
| 218 |
|
| 219 |
$response = $this->update( $active_case ); |
| 220 |
if ( is_a( $response, 'WP_Error' ) ) { |
| 221 |
return $response; |
| 222 |
} |
| 223 |
|
| 224 |
return $this->get( $active_case ); |
| 225 |
} |
| 226 |
|
| 227 |
public function get_active( $args=array() ) { |
| 228 |
$active_case_id = get_option( 'pdt_active_case_id' ); |
| 229 |
if ( empty( $active_case_id )) { |
| 230 |
return new WP_Error( 'no_active_case' , __( 'No active case found', 'plugin-detective' ) ); |
| 231 |
} |
| 232 |
|
| 233 |
$active_case = $this->get( array( 'id' => $active_case_id ) ); |
| 234 |
if ( is_a( $active_case, 'WP_Error' ) ) { |
| 235 |
return $active_case; |
| 236 |
} |
| 237 |
|
| 238 |
if ( empty( $active_case ) ) { |
| 239 |
return new WP_Error( 'case_data_missing' , __( 'Case ID '.$active_case_id.' missing', 'plugin-detective' ) ); |
| 240 |
} |
| 241 |
|
| 242 |
return $active_case; |
| 243 |
} |
| 244 |
|
| 245 |
public function get( $args=array() ) { |
| 246 |
if ( empty( $args['id'] ) ) { |
| 247 |
return new WP_Error( 'get_id_missing', __( 'No case id specified for get()', 'plugin-detective' ) ); |
| 248 |
} |
| 249 |
|
| 250 |
$case = get_option( 'pdt_case_' . esc_attr( $args['id'] ) ); |
| 251 |
if ( empty( $case ) ) { |
| 252 |
return new WP_Error( 'case_data_missing' , __( 'Case ID '.$args['id'].' missing', 'plugin-detective' ) ); |
| 253 |
} |
| 254 |
|
| 255 |
return json_decode( $case, true ); |
| 256 |
} |
| 257 |
|
| 258 |
public function create( $args=array() ) { |
| 259 |
if ( empty( $args['id'] ) ) { |
| 260 |
return new WP_Error( 'update_id_missing', __( 'No case id specified for create()', 'plugin-detective' ) ); |
| 261 |
} |
| 262 |
|
| 263 |
update_option( 'pdt_case_'.esc_attr( $args['id'] ), json_encode( $args ) ); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
public function update( $args=array() ) { |
| 268 |
$case = $this->get_active(); |
| 269 |
if ( is_a( $case, 'WP_Error' ) ) { |
| 270 |
return $case; |
| 271 |
} |
| 272 |
|
| 273 |
$args['id'] = $case['id']; |
| 274 |
|
| 275 |
// foreach ($this->get_schema() as $field) { |
| 276 |
// if ( isset( $args[$field['name']] ) && $field['access'] === 'private' ) { |
| 277 |
// unset( $args[$field['name']] ); |
| 278 |
// } |
| 279 |
// } |
| 280 |
|
| 281 |
$case = array_merge( $case, $args ); |
| 282 |
update_option( 'pdt_case_'.esc_attr( $args['id'] ), json_encode( $case ) ); |
| 283 |
return $this->get( array( 'id' => $args['id'] ) ); |
| 284 |
} |
| 285 |
|
| 286 |
public function review( $args=array() ) { |
| 287 |
$case = $this->get_active(); |
| 288 |
if ( is_a( $case, 'WP_Error' ) ) { |
| 289 |
return $case; |
| 290 |
} |
| 291 |
|
| 292 |
$case = $this->plugin->detective->review_case( $case ); // update $case['suspects_under_interrogation'] |
| 293 |
|
| 294 |
if ( $case['stage'] != 'investigating' ) { |
| 295 |
$case = $this->update( $case ); |
| 296 |
$case = $this->close( array( |
| 297 |
'reset_active_plugins' => true, |
| 298 |
) ); |
| 299 |
} else { |
| 300 |
$this->plugin->plugins->set_active( $case['suspects_under_interrogation'] ); |
| 301 |
$case = $this->update( $case ); |
| 302 |
} |
| 303 |
|
| 304 |
return $case; |
| 305 |
} |
| 306 |
|
| 307 |
public function get_all( $args=array() ) { |
| 308 |
$cases = get_option( 'pdt_cases', json_encode( array() ) ); |
| 309 |
$cases = json_decode( $cases, true ); |
| 310 |
if ( empty( $cases ) ) { |
| 311 |
$cases = array(); |
| 312 |
} |
| 313 |
|
| 314 |
return $cases; |
| 315 |
} |
| 316 |
} |
| 317 |
|