/** * @param int $service_id * * @return TranslationProxy_Project|WP_Error */ public static function select_service($service_id) { global $sitepress; $service_selected = false; $error = false; try { /** @var $service TranslationProxy_Service */ $service = TranslationProxy_Api::proxy_request("/services/{$service_id}.json"); if ($service) { self::deselect_active_service(); //set language map $service->languages_map = self::languages_map($service); //set information about custom fields $service->custom_fields = self::get_custom_fields($service_id, true); $service->custom_fields_data = false; $sitepress->set_setting('translation_service', $service); $result = $service; $service_selected = true; //Force authentication if no user input is needed if (!TranslationProxy::service_requires_authentication($service)) { self::authenticate_service($service_id, array()); } } else { $result = new WP_Error('0', 'No service selected', array('service_id' => $service_id)); } } catch (TranslationProxy_Api_Error $e) { $error = new WP_Error(); $error->add($e->getCode(), 'Unable to select service'); $error->add_data(array('details' => $e->getMessage()), $e->getCode()); $result = $error; } catch (Exception $e) { $error = new WP_Error(); $error->add($e->getCode(), 'Unable to select service'); $error->add_data(array('details' => $e->getMessage()), $e->getCode()); $result = $error; } //Do not store selected service if this operation failed; if ($error || !$service_selected) { $sitepress->set_setting('translation_service', false); } $sitepress->save_settings(); return $result; }
/** * * @return mixed WP_Error or Array (result) */ function do_payment($params) { $sandbox_mode = $this->parent_settings["sandbox_mode"] == "yes"; // Yup, the checkbox settings return as 'yes' or 'no' $test_mode = $this->parent_settings["test_mode"] == "yes"; $order_text = json_encode($params); $url = $sandbox_mode ? $this->sandbox_url : ($url = $this->live_url); // Deferred payments need to post to the /credit_cards endpoint. if (isset($params["deferred"]) && $params["deferred"]) { // Replace the URL with the tokenize method and re-create the order text (json payload) $url = str_replace("purchases", "credit_cards", $url); $payload = array("card_holder" => $params["card_holder"], "card_number" => $params["card_number"], "card_expiry" => $params["card_expiry"], "cvv" => $params["cvv"]); $ip = get_post_meta($post_id, "Customer IP Address", true); if (empty($ip)) { $ip = "127.0.0.1"; } if (!isset($payload["customer_ip"])) { $payload["customer_ip"] = $ip; } $order_text = json_encode($payload); } $args = array('method' => 'POST', 'body' => $order_text, 'headers' => array('Authorization' => 'Basic ' . base64_encode($this->parent_settings["username"] . ":" . $this->parent_settings["token"]), 'X-Test-Mode' => $test_mode, 'User-Agent' => "WooCommerce Plugin " . $this->version), 'timeout' => 30); try { $this->response = (array) wp_remote_request($url, $args); if ((int) $this->response["response"]["code"] != 200 && (int) $this->response["response"]["code"] != 201) { $error = new WP_Error(); $error->add(1, "Credit Card Payment failed: " . $this->response["response"]["message"]); $error->add_data($this->response); return $error; } $this->response_data = json_decode($this->response['body']); if (!$this->response_data->successful) { $error = new WP_Error(); $error->add(2, "Gateway Error", $this->response_data->errors); return $error; } // If we are doing a deferred payments we override the data here. if (isset($params["deferred"]) && $params["deferred"]) { return array("card_token" => $this->response_data->response->token, "card_number" => $this->response_data->response->card_number, "card_expiry" => $this->response_data->response->card_expiry, "card_holder" => $this->response_data->response->card_holder); } if (!$this->response_data->response->successful) { $error = new WP_Error(); $error->add(3, "Payment Declined", array("message" => $this->response_data->response->message, "id" => $this->response_data->response->id)); return $error; } if ($this->response_data->response->successful) { return array("transaction_id" => $this->response_data->response->id, "card_token" => $this->response_data->response->card_token); } } catch (Exception $e) { $error = new WP_Error(); $error->add(4, "Unknown Error", $e); return $error; } }
/** * Join array of statuses into one status * * @since 2.0 * @access public * * @param array of WP_Errors objects $statuses * @param ( object | array of object ) $join_status second status to join may be single WP_Error object or array of WP_Error objects * @return object WP_Error */ function join_errors($statuses = array(), $join_status = null) { $return = new WP_Error(); // If multiple arguments were passed join different wp errors if (!empty($join_status)) { if (is_array($statuses)) { $statuses[] = $join_status; } else { $statuses = array($statuses, $join_status); } } if (empty($statuses)) { return $return; } // Loop through statuses foreach ($statuses as $status) { // Skip empty statuses if (!is_wp_error($status) or !$status->get_error_codes()) { continue; } foreach ($status->get_error_codes() as $code) { // Add messages first $messages = $status->get_error_messages($code); // we need only unique messages if (in_array($code, $return->get_error_codes())) { $messages = array_diff($messages, $return->get_error_messages($code)); } // add messages if they present if (!empty($messages)) { foreach ($messages as $message) { $return->add($code, $message); } } // Add code data $data = $status->get_error_data($code); // Join return data and our data if (!empty($data) and $return->get_error_data($code)) { // add new data according to return data type if (is_array($return->get_error_data($code))) { // passed data is array $data = array_merge($data, $return->get_error_data($code)); } elseif (is_array($data)) { $data[] = $return->get_error_data($code); } elseif (is_array($return->get_error_data($code))) { $data = array_push($return->get_error_data($code), $data); } elseif (is_string($data) and is_string($return->get_error_data($code))) { $data = $return->get_error_data($code) . $data; } } if (!empty($data)) { $return->add_data($data, $code); } } // Loop for each code inside status } // Loop for each passed statuses return $return; }
/** * Wrapper around HTTP calls, always returns an array of theme information. * * @param string $action One of the supported actions of the repository * @param array $params Parameters for the action * @param string $locale * @return array<object>|\WP_Error */ protected function doApiQuery($action, array $params = [], $locale = '') { $url_params = ['key' => $this->settings['key'], 'action' => $action]; if ($params) { if (function_exists('gzcompress')) { $url_params['gzparams'] = gzcompress(json_encode($params), 9); } else { $url_params['params'] = json_encode($params); } } $url_params = array_map('rawurlencode', $url_params); $url = add_query_arg($url_params, self::URL); if (strlen($url) > 2000) { // Lengths beyond 2000 seem unhealthy. return new \WP_Error(815, __('Your theme repository query is too long.', 'pfadfinden-theme-updater')); } $headers = []; if (!strlen($locale)) { $locale = get_locale(); } if (strlen($locale)) { $locale = str_replace('_', '-', $locale); $headers['Accept-Language'] = "{$locale}, en; q=0.6, *; q=0.1"; } // A GET request allows for caching $response = wp_remote_get($url, ['headers' => $headers]); if (is_wp_error($response)) { return $response; } $body = json_decode(wp_remote_retrieve_body($response), true); if (isset($body['type']) && 'success' === $body['type']) { return array_map(function (array $theme) { return (object) $theme; }, $body['themes']); } if (WP_DEBUG) { trigger_error(wp_remote_retrieve_body($response), E_USER_ERROR); } $error = new \WP_Error(wp_remote_retrieve_response_code($response), isset($body['message']) ? $body['message'] : __('Unknown theme repository server error, no message attached.', 'pfadfinden-theme-updater')); if (isset($body['exception'])) { $error->add_data($body['exception']); } return $error; }
/** * Creates a single user. * * @since 4.7.0 * @access public * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function create_item($request) { if (!empty($request['id'])) { return new WP_Error('rest_user_exists', __('Cannot create existing user.'), array('status' => 400)); } $schema = $this->get_item_schema(); if (!empty($request['roles']) && !empty($schema['properties']['roles'])) { $check_permission = $this->check_role_update($request['id'], $request['roles']); if (is_wp_error($check_permission)) { return $check_permission; } } $user = $this->prepare_item_for_database($request); if (is_multisite()) { $ret = wpmu_validate_user_signup($user->user_login, $user->user_email); if (is_wp_error($ret['errors']) && !empty($ret['errors']->errors)) { $error = new WP_Error('rest_invalid_param', __('Invalid user parameter(s).'), array('status' => 400)); foreach ($ret['errors']->errors as $code => $messages) { foreach ($messages as $message) { $error->add($code, $message); } if ($error_data = $error->get_error_data($code)) { $error->add_data($error_data, $code); } } return $error; } } if (is_multisite()) { $user_id = wpmu_create_user($user->user_login, $user->user_pass, $user->user_email); if (!$user_id) { return new WP_Error('rest_user_create', __('Error creating new user.'), array('status' => 500)); } $user->ID = $user_id; $user_id = wp_update_user(wp_slash((array) $user)); if (is_wp_error($user_id)) { return $user_id; } add_user_to_blog(get_site()->id, $user_id, ''); } else { $user_id = wp_insert_user(wp_slash((array) $user)); if (is_wp_error($user_id)) { return $user_id; } } $user = get_user_by('id', $user_id); /** * Fires immediately after a user is created or updated via the REST API. * * @since 4.7.0 * * @param WP_User $user Inserted or updated user object. * @param WP_REST_Request $request Request object. * @param bool $creating True when creating a user, false when updating. */ do_action('rest_insert_user', $user, $request, true); if (!empty($request['roles']) && !empty($schema['properties']['roles'])) { array_map(array($user, 'add_role'), $request['roles']); } if (!empty($schema['properties']['meta']) && isset($request['meta'])) { $meta_update = $this->meta->update_value($request['meta'], $user_id); if (is_wp_error($meta_update)) { return $meta_update; } } $user = get_user_by('id', $user_id); $fields_update = $this->update_additional_fields_for_object($user, $request); if (is_wp_error($fields_update)) { return $fields_update; } $request->set_param('context', 'edit'); $response = $this->prepare_item_for_response($user, $request); $response = rest_ensure_response($response); $response->set_status(201); $response->header('Location', rest_url(sprintf('%s/%s/%d', $this->namespace, $this->rest_base, $user_id))); return $response; }
/** * * @return mixed WP_Error or Array (result) */ function do_payment($params) { $sandbox_mode = $this->parent_settings["sandbox_mode"] == "yes"; // Yup, the checkbox settings return as 'yes' or 'no' $test_mode = $this->parent_settings["test_mode"] == "yes"; $order_text = json_encode($params); $url = $sandbox_mode ? $this->sandbox_url : ($url = $this->live_url); $args = array('method' => 'POST', 'body' => $order_text, 'headers' => array('Authorization' => 'Basic ' . base64_encode($this->parent_settings["username"] . ":" . $this->parent_settings["token"]), 'X-Test-Mode' => $test_mode, 'User-Agent' => "WooCommerce Plugin " . $this->version), 'timeout' => 30); try { $this->response = (array) wp_remote_request($url, $args); if ((int) $this->response["response"]["code"] != 200 && (int) $this->response["response"]["code"] != 201) { $error = new WP_Error(); $error->add(1, "Credit Card Payment failed: " . $this->response["response"]["message"]); $error->add_data($this->response); return $error; } $this->response_data = json_decode($this->response['body']); if (!$this->response_data->successful) { $error = new WP_Error(); $error->add(2, "Gateway Error", $this->response_data->errors); return $error; } // If we are doing a deferred payments we override the data here. if (isset($params["deferred"]) && $params["deferred"]) { return array("card_token" => $this->response_data->response->token, "card_number" => $this->response_data->response->card_number, "card_expiry" => $this->response_data->response->card_expiry, "card_holder" => $this->response_data->response->card_holder); } if (!$this->response_data->response->successful) { $error = new WP_Error(); $error->add(3, "Payment Declined", array("message" => $this->response_data->response->message, "id" => $this->response_data->response->id, "fraud_result" => $this->response_data->response->fraud_result, "fraud_messages" => $this->response_data->response->fraud_messages)); return $error; } if ($this->response_data->response->successful) { return array("transaction_id" => $this->response_data->response->id, "card_token" => $this->response_data->response->card_token, "fraud_result" => $this->response_data->response->fraud_result, "fraud_messages" => $this->response_data->response->fraud_messages); } } catch (Exception $e) { $error = new WP_Error(); $error->add(4, "Unknown Error", $e); return $error; } }
private function add_data_to_wp_error(WP_Error $error, array $data) { $error_data = (array) $error->get_error_data(); $error->add_data(array_merge($error_data, $data)); return $error; }
function convertErrorArray($data) { if (!is_array(@$data['errors'])) { return $data; } $errors = new WP_Error(); foreach ($data['errors'] as $code => $error) { if (is_array($error)) { foreach ($error as $message) { $errors->add($code, $message); } } elseif (is_string($error)) { $errors->add($code, $error); } } if (isset($data['error_data'])) { if (is_array($data['error_data'])) { foreach ($data['error_data'] as $code => $errData) { $errors->add_data($errData, $code); } } } return $errors; }