/** * Create a group * * @since 0.4.0 * * @param \WP_REST_Request $request Full data about the request. * @return \WP_Error|\WP_REST_Response */ public function create_item($request) { $variants = helpers::sanitize($request->get_param('variants')); $group_args = $this->prepare_item_for_database($request, true); $group_args['variants'] = []; $id = group::create($group_args); $item = new \WP_Error('ingot-unknown-error'); if (is_wp_error($id)) { $item = $id; } if (is_numeric($id)) { $item = group::read($id); $item['variants'] = $variants; if (is_array($item)) { $variants_ids = $this->save_variants($item); if (is_wp_error($variants_ids)) { return ingot_rest_response($variants_ids, 500); } $item['variants'] = $variants_ids; group::update($item, $item['ID']); $item = group::read($item['ID']); if (is_array($item)) { $item = $this->prepare_group($item, $request->get_param('context')); return ingot_rest_response($item, 201, 1); } } } return ingot_rest_response($item, 500); }
/** * Validate item config * * @since 0.4.0 * * @access protected * * @param array $data Item config * * @return \WP_Error|array Item config array if valid, WP_Error if not. */ protected static function validate_config($data) { $required = static::required(); foreach ($required as $key) { if (!isset($data[$key])) { return new \WP_Error('ingot-invalid-config', __(sprintf('Groups require the field %s', $key), 'ingot'), $data); } } if (!isset($data['IP'])) { $data['IP'] = ingot_get_ip(); } if (isset($data['UTM']) && $data['UTM']) { $data['UTM'] = helpers::sanitize($data['UTM']); } $data['time'] = self::date_validation($data['time']); return $data; }
/** * Generic save for read/update * * @since 0.0.4 * * @param array $data Item con * @param int $id Optional. Item ID. Not used or needed if using to create. * @param bool|false $bypass_cap * * @return int|bool||WP_Error Item ID if created, or false if not created, or error if not allowed to create. */ protected static function save($data, $id = null, $bypass_cap = false) { $data = static::prepare_data($data); if (is_wp_error($data) || !is_array($data)) { return $data; } if ('group' === static::what() && is_null($id)) { if ('price' == $data['type']) { if (!isset($data['wp_ID'])) { //shouldn't be needed. return new \WP_Error(); } if (isset($data['wp_ID']) && false !== ($existing = price::product_test_exists($data['wp_ID']))) { return new \WP_Error('ingot-price-test-for-product-exists', __(sprintf('Product ID %d is already being tested by test group ID %d', $data['meta']['product_ID'], $existing), 'ingot'), ['product_ID' => $data['meta']['product_ID'], 'group_ID' => $existing]); } } } $table_name = static::get_table_name(); foreach ($data as $key => $datum) { if (is_array($data[$key])) { if (empty($data[$key])) { $data[$key] = serialize([]); } else { $data[$key] = helpers::sanitize($data[$key]); $data[$key] = serialize($datum); } } } if (self::can($id, $bypass_cap)) { global $wpdb; if ($id) { $data['ID'] = $id; $wpdb->update($table_name, $data, array('ID' => (int) $id)); } else { unset($data['ID']); $wpdb->insert($table_name, $data); $id = $wpdb->insert_id; } return $id; } else { return false; } }
/** * Generic save for read/update * * @since 0.0.4 * * @param array $data Item con * @param int $id Optional. Item ID. Not used or needed if using to create. * @param bool|false $bypass_cap * * @return int|bool||WP_Error Item ID if created, or false if not created, or error if not allowed to create. */ protected static function save($data, $id = null, $bypass_cap = false) { $data = static::prepare_data($data); if (is_wp_error($data) || !is_array($data)) { return $data; } $table_name = static::get_table_name(); foreach ($data as $key => $datum) { if (is_array($data[$key])) { if (empty($data[$key])) { $data[$key] = serialize([]); } else { $data[$key] = helpers::sanitize($data[$key]); $data[$key] = serialize($datum); } } } if (self::can($id, $bypass_cap)) { global $wpdb; if ($id) { $data['ID'] = $id; $wpdb->update($table_name, $data, array('ID' => (int) $id)); } else { unset($data['ID']); $wpdb->insert($table_name, $data); $id = $wpdb->insert_id; } return $id; } else { return false; } }