/** * Update post associated with group by searching post content for them * * @since 1.0.0 * * @param \WP_Post $post */ public static function update_groups_in_post($post) { if (is_a($post, 'WP_Post')) { $obj = new \ingot\testing\object\posts($post); $current = self::find_ids($post->post_content); if (empty($current)) { $obj->add([], true); } else { $existing = $obj->get_groups(); if (empty($existing) || !empty(array_diff($existing, $current))) { $obj->add($current, true); } } } }
/** * Test that we can find the IDs from the shortcodes in post content * * @since 1.1.0 * * @group group * @group objects * @group posts_object * * @covers ingot\testing\utility\posts::update_groups_in_post() */ public function testUpdatePost() { $id = $this->factory->post->create(['post_title' => 'good', 'post_content' => 'sfdjlakjsdf [ingot id="9"] sfdj fsdkdfs SDF657R542 ingot id="7" fsd [ingot id="42"][ingot id="11"]']); $post = get_post($id); \ingot\testing\utility\posts::update_groups_in_post($post); $obj = new \ingot\testing\object\posts(get_post($id)); $expected = [9, 42, 11]; $this->assertSame($expected, $obj->get_groups()); $post->post_content = 'sfdjlakjsdf [ingot id="9"]'; \ingot\testing\utility\posts::update_groups_in_post($post); $obj = new \ingot\testing\object\posts(get_post($id)); $this->assertSame([9], $obj->get_groups()); }