/** * Get a variant's HTML * * @since 0.4.0 * * @param \WP_REST_Request $request Full data about the request. * @return \WP_Error|\WP_REST_Request */ public function get_item($request) { return $this->not_yet_response(); $context = $request->get_param('context'); $url = $request->get_url_params(); $id = helpers::v('ID', $url, 0); if (0 == absint($id) || !is_array(\ingot\testing\crud\variant::read($id))) { return new \WP_Error('ingot-invalid-test'); } elseif ('context' != 'view') { return new \WP_Error('ingot-test-context-invalid'); } else { if ('view' == $context) { $test = \ingot\testing\crud\variantP::read($id); $html = ingot_click_test($test); return ingot_rest_response($html); } } }
/** * Bulk create, update and delete items. * * @since 2.7.0 * @param WP_REST_Request $request Full details about the request. * @return array Of WP_Error or WP_REST_Response. */ public function batch_items($request) { $items = array_filter($request->get_params()); $params = $request->get_url_params(); $product_id = $params['product_id']; $body_params = array(); foreach (array('update', 'create', 'delete') as $batch_type) { if (!empty($items[$batch_type])) { $injected_items = array(); foreach ($items[$batch_type] as $item) { $injected_items[] = array_merge(array('product_id' => $product_id), $item); } $body_params[$batch_type] = $injected_items; } } $request = new WP_REST_Request($request->get_method()); $request->set_body_params($body_params); return parent::batch_items($request); }
/** * @param \WP_REST_Request $request Full data about the request. * * @return array|mixed|null|object|void */ protected function get_session_by_url_params($request) { $url = $request->get_url_params(); $id = helpers::v('id', $url, 0); if (0 != absint($id) && is_array($session = \ingot\testing\crud\session::read($id))) { return $session; } else { return new \WP_Error('ingot-invalid-session'); } }
/** * Bulk create, update and delete items. * * @since 2.7.0 * @param WP_REST_Request $request Full details about the request. * @return array Of WP_Error or WP_REST_Response. */ public function batch_items($request) { // Get the request params. $items = array_filter($request->get_params()); /* * Since our batch settings update is group-specific and matches based on the route, * we inject the URL parameters (containing group) into the batch items */ if (!empty($items['update'])) { $to_update = array(); foreach ($items['update'] as $item) { $to_update[] = array_merge($request->get_url_params(), $item); } $request = new WP_REST_Request($request->get_method()); $request->set_body_params(array('update' => $to_update)); } return parent::batch_items($request); }
/** * Get price of a product * * @since 1.1.0 * * @param \WP_REST_Request $request Full data about the request. * @return \WP_Error|\WP_REST_Response */ public function get_price($request) { $price = price::get_price($request->get_param('plugin'), $request->get_url_params()['id']); return ingot_rest_response(['price' => $price]); }
/** * Update groups associated with a post * * @since 1.0.0 * * @param \WP_REST_Request $request Full data about the request. * @return \WP_Error|\WP_REST_Response */ public function update_posts($request) { $url = $request->get_url_params(); $post_id = (int) helpers::v('id', $url, 0); $post = get_post($post_id); if (!is_a($post, 'WP_POST')) { return ingot_rest_response(['message' => esc_html__('No group found', 'ingot')]); } $obj = new posts($post); $obj->add($request->get_param('group_ids')); return ingot_rest_response($obj->get_groups()); }
/** * Get stats for a group * * @since 0.4.0 * * @param \WP_REST_Request $request Full data about the request. * @return \WP_Error|\WP_REST_Response */ public function get_stats($request) { $url = $request->get_url_params(); $id = helpers::v('id', $url, 0); $group = group::read($id); if (!is_array($group)) { if (is_wp_error($group)) { return $group; } return ingot_rest_response(['message' => esc_html__('No group found', 'ingot')]); } $obj = new \ingot\testing\object\group($group); $stats = $obj->get_stats(); if ('admin' == $request->get_param('context')) { $names = $obj->names(); if (!empty($stats['variants']) && !empty($names['variants'])) { foreach ($names['variants'] as $v_id => $name) { if (isset($stats['variants'][$v_id])) { $stats['variants'][$v_id] = (array) $stats['variants'][$v_id]; $stats['variants'][$v_id]['name'] = $name; $stats['variants'][$v_id] = (object) $stats['variants'][$v_id]; } } } $stats['names'] = $names; } return ingot_rest_response($stats, 200); }