Skip to content

Commit 04e9a16

Browse files
authored
Merge pull request #284 from wp-cli/empty-links-table
Update `site empty` command to include removal of `wp_links` table data
2 parents 497bb46 + c64811e commit 04e9a16

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

features/site-empty.feature

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ Feature: Empty a WordPress site of its data
66
And download:
77
| path | url |
88
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
9+
And a insert_link_data.sql file:
10+
"""
11+
INSERT INTO `wp_links` (`link_url`, `link_name`, `link_image`, `link_target`, `link_description`, `link_visible`, `link_owner`, `link_rating`, `link_rel`, `link_notes`, `link_rss`)
12+
VALUES ('http://wordpress.org/', 'test', '', '', 'test', 'Y', 1, 0, '', '', '')
13+
"""
14+
15+
When I run `wp db query "SOURCE insert_link_data.sql;"`
16+
Then STDERR should be empty
17+
18+
When I run `wp db query "SELECT COUNT(link_id) FROM wp_links;"`
19+
Then STDOUT should be:
20+
"""
21+
COUNT(link_id)
22+
1
23+
"""
924

1025
When I run `wp media import {CACHE_DIR}/large-image.jpg --post_id=1`
1126
Then the wp-content/uploads/large-image.jpg file should exist
@@ -63,6 +78,13 @@ Feature: Empty a WordPress site of its data
6378
0
6479
"""
6580

81+
When I run `wp db query "SELECT COUNT(link_id) FROM wp_links;"`
82+
Then STDOUT should be:
83+
"""
84+
COUNT(link_id)
85+
0
86+
"""
87+
6688
Scenario: Empty a site and its uploads directory
6789
Given a WP multisite installation
6890
And I run `wp site create --slug=foo`

src/Site_Command.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,34 @@ private function empty_taxonomies() {
115115
}
116116
}
117117

118+
/**
119+
* Delete all links, link_category terms, and related cache.
120+
*/
121+
private function empty_links() {
122+
global $wpdb;
123+
124+
// Remove links and related cached data.
125+
$links_query = "SELECT link_id FROM {$wpdb->links}";
126+
$links = new QueryIterator( $links_query, 10000 );
127+
128+
// Remove bookmarks cache group.
129+
wp_cache_delete( 'get_bookmarks', 'bookmark' );
130+
131+
while ( $links->valid() ) {
132+
$link_id = $links->current()->link_id;
133+
134+
// Remove cache for the link.
135+
wp_delete_object_term_relationships( $link_id, 'link_category' );
136+
wp_cache_delete( $link_id, 'bookmark' );
137+
clean_object_term_cache( $link_id, 'link' );
138+
139+
$links->next();
140+
}
141+
142+
// Empty the table once link related cache and term is removed.
143+
$wpdb->query( "TRUNCATE {$wpdb->links}" );
144+
}
145+
118146
/**
119147
* Insert default terms.
120148
*/
@@ -211,7 +239,7 @@ private function reset_options() {
211239
* ## EXAMPLES
212240
*
213241
* $ wp site empty
214-
* Are you sure you want to empty the site at http://www.example.com of all posts, comments, and terms? [y/n] y
242+
* Are you sure you want to empty the site at http://www.example.com of all posts, links, comments, and terms? [y/n] y
215243
* Success: The site at 'http://www.example.com' was emptied.
216244
*
217245
* @subcommand empty
@@ -223,9 +251,10 @@ public function empty_( $args, $assoc_args ) {
223251
$upload_message = ', and delete its uploads directory';
224252
}
225253

226-
WP_CLI::confirm( "Are you sure you want to empty the site at '" . site_url() . "' of all posts, comments, and terms" . $upload_message . '?', $assoc_args );
254+
WP_CLI::confirm( "Are you sure you want to empty the site at '" . site_url() . "' of all posts, links, comments, and terms" . $upload_message . '?', $assoc_args );
227255

228256
$this->empty_posts();
257+
$this->empty_links();
229258
$this->empty_comments();
230259
$this->empty_taxonomies();
231260
$this->insert_default_terms();

0 commit comments

Comments
 (0)