/** * 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; } }
/** * Test that we can't create a group for a product that is already being tested * * @since 1.1.0 * * @group price * @group helper * @covers \ingot\testing\utility\price::product_test_exists() */ public function testPriceTestExists() { $group = \ingot\testing\crud\group::create(['name' => 'd', 'type' => 'price', 'sub_type' => 'edd', 'meta' => ['product_ID' => 169], 'wp_ID' => 169], true); $this->assertTrue(is_numeric($group)); $existing = \ingot\testing\utility\price::product_test_exists(169); $this->assertEquals($existing, $group); }