} // Delete old user keys from usermeta foreach ($api_users as $_user) { $user_id = intval($_user->user_id); delete_user_meta($user_id, 'woocommerce_api_consumer_key'); delete_user_meta($user_id, 'woocommerce_api_consumer_secret'); delete_user_meta($user_id, 'woocommerce_api_key_permissions'); } } /** * Webhooks * Make sure order.update webhooks get the woocommerce_order_edit_status hook */ $order_update_webhooks = get_posts(array('posts_per_page' => -1, 'post_type' => 'shop_webhook', 'meta_key' => '_topic', 'meta_value' => 'order.updated')); foreach ($order_update_webhooks as $order_update_webhook) { $webhook = new WC_Webhook($order_update_webhook->ID); $webhook->set_topic('order.updated'); } /** * Refunds for full refunded orders. * Update fully refunded orders to ensure they have a refund line item so reports add up. */ $refunded_orders = get_posts(array('posts_per_page' => -1, 'post_type' => 'shop_order', 'post_status' => array('wc-refunded'))); foreach ($refunded_orders as $refunded_order) { $order_total = get_post_meta($refunded_order->ID, '_order_total', true); $refunded_total = $wpdb->get_var($wpdb->prepare("\n\t\tSELECT SUM( postmeta.meta_value )\n\t\tFROM {$wpdb->postmeta} AS postmeta\n\t\tINNER JOIN {$wpdb->posts} AS posts ON ( posts.post_type = 'shop_order_refund' AND posts.post_parent = %d )\n\t\tWHERE postmeta.meta_key = '_refund_amount'\n\t\tAND postmeta.post_id = posts.ID\n\t", $refunded_order->ID)); if ($order_total > $refunded_total) { $refund = wc_create_refund(array('amount' => $order_total - $refunded_total, 'reason' => __('Order Fully Refunded', 'woocommerce'), 'order_id' => $refunded_order->ID, 'line_items' => array(), 'date' => $refunded_order->post_modified)); } } wc_delete_shop_order_transients();
/** * Edit a webhook * * @since 2.2 * @param int $id webhook ID * @param array $data parsed webhook data * @return array */ public function edit_webhook($id, $data) { $data = isset($data['webhook']) ? $data['webhook'] : array(); try { $id = $this->validate_request($id, 'shop_webhook', 'edit'); if (is_wp_error($id)) { return $id; } $data = apply_filters('woocommerce_api_edit_webhook_data', $data, $id, $this); $webhook = new WC_Webhook($id); // update topic if (!empty($data['topic'])) { if (wc_is_webhook_valid_topic(strtolower($data['topic']))) { $webhook->set_topic($data['topic']); } else { throw new WC_API_Exception('woocommerce_api_invalid_webhook_topic', __('Webhook topic must be valid', 'woocommerce'), 400); } } // update delivery URL if (!empty($data['delivery_url'])) { if (wc_is_valid_url($data['delivery_url'])) { $webhook->set_delivery_url($data['delivery_url']); } else { throw new WC_API_Exception('woocommerce_api_invalid_webhook_delivery_url', __('Webhook delivery URL must be a valid URL starting with http:// or https://', 'woocommerce'), 400); } } // update secret if (!empty($data['secret'])) { $webhook->set_secret($data['secret']); } // update status if (!empty($data['status'])) { $webhook->update_status($data['status']); } // update user ID $webhook_data = array('ID' => $webhook->id, 'post_author' => get_current_user_id()); // update name if (!empty($data['name'])) { $webhook_data['post_title'] = $data['name']; } // update post wp_update_post($webhook_data); do_action('woocommerce_api_edit_webhook', $webhook->id, $this); delete_transient('woocommerce_webhook_ids'); return $this->get_webhook($id); } catch (WC_API_Exception $e) { return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); } }
/** * Update a single webhook. * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|WP_REST_Response */ public function update_item($request) { $id = (int) $request['id']; $post = get_post($id); if (empty($id) || empty($post->ID) || $this->post_type !== $post->post_type) { return new WP_Error("woocommerce_rest_{$this->post_type}_invalid_id", __('ID is invalid.', 'woocommerce'), array('status' => 400)); } $webhook = new WC_Webhook($id); // Update topic. if (!empty($request['topic'])) { if (wc_is_webhook_valid_topic(strtolower($request['topic']))) { $webhook->set_topic($request['topic']); } else { return new WP_Error("woocommerce_rest_{$this->post_type}_invalid_topic", __('Webhook topic must be valid.', 'woocommerce'), array('status' => 400)); } } // Update delivery URL. if (!empty($request['delivery_url'])) { if (wc_is_valid_url($request['delivery_url'])) { $webhook->set_delivery_url($request['delivery_url']); } else { return new WP_Error("woocommerce_rest_{$this->post_type}_invalid_delivery_url", __('Webhook delivery URL must be a valid URL starting with http:// or https://.', 'woocommerce'), array('status' => 400)); } } // Update secret. if (!empty($request['secret'])) { $webhook->set_secret($request['secret']); } // Update status. if (!empty($request['status'])) { $webhook->update_status($request['status']); } $post = $this->prepare_item_for_database($request); if (is_wp_error($post)) { return $post; } // Convert the post object to an array, otherwise wp_update_post will expect non-escaped input. $post_id = wp_update_post((array) $post, true); if (is_wp_error($post_id)) { if (in_array($post_id->get_error_code(), array('db_update_error'))) { $post_id->add_data(array('status' => 500)); } else { $post_id->add_data(array('status' => 400)); } return $post_id; } $post = get_post($post_id); $this->update_additional_fields_for_object($post, $request); /** * Fires after a single item is created or updated via the REST API. * * @param WP_Post $post Inserted object. * @param WP_REST_Request $request Request object. * @param boolean $creating True when creating item, false when updating. */ do_action("woocommerce_rest_insert_{$this->post_type}", $post, $request, false); $request->set_param('context', 'edit'); $response = $this->prepare_item_for_response($post, $request); // Clear cache. delete_transient('woocommerce_webhook_ids'); return rest_ensure_response($response); }
/** * Updated the Webhook topic. * * @param WC_Webhook $webhook */ private function update_topic($webhook) { if (!empty($_POST['webhook_topic'])) { $resource = ''; $event = ''; switch ($_POST['webhook_topic']) { case 'custom': if (!empty($_POST['webhook_custom_topic'])) { list($resource, $event) = explode('.', wc_clean($_POST['webhook_custom_topic'])); } break; case 'action': $resource = 'action'; $event = !empty($_POST['webhook_action_event']) ? wc_clean($_POST['webhook_action_event']) : ''; break; default: list($resource, $event) = explode('.', wc_clean($_POST['webhook_topic'])); break; } $topic = $resource . '.' . $event; if (wc_is_webhook_valid_topic($topic)) { $webhook->set_topic($topic); } } }
function wc_update_270_webhooks() { /** * Make sure product.update webhooks get the woocommerce_product_quick_edit_save * and woocommerce_product_bulk_edit_save hooks. */ $product_update_webhooks = get_posts(array('posts_per_page' => -1, 'post_type' => 'shop_webhook', 'meta_key' => '_topic', 'meta_value' => 'product.updated')); foreach ($product_update_webhooks as $product_update_webhook) { $webhook = new WC_Webhook($product_update_webhook->ID); $webhook->set_topic('product.updated'); } }
function wc_update_240_webhooks() { global $wpdb; /** * Webhooks. * Make sure order.update webhooks get the woocommerce_order_edit_status hook. */ $order_update_webhooks = get_posts(array('posts_per_page' => -1, 'post_type' => 'shop_webhook', 'meta_key' => '_topic', 'meta_value' => 'order.updated')); foreach ($order_update_webhooks as $order_update_webhook) { $webhook = new WC_Webhook($order_update_webhook->ID); $webhook->set_topic('order.updated'); } }