Skip to content

Commit 3d82810

Browse files
committed
Merge branch 'master' into update/site-empty
2 parents e1fb981 + 04e9a16 commit 3d82810

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
@@ -78,6 +93,13 @@ Feature: Empty a WordPress site of its data
7893
[]
7994
"""
8095

96+
When I run `wp db query "SELECT COUNT(link_id) FROM wp_links;"`
97+
Then STDOUT should be:
98+
"""
99+
COUNT(link_id)
100+
0
101+
"""
102+
81103
Scenario: Empty a site and its uploads directory
82104
Given a WP multisite installation
83105
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
*/
@@ -214,7 +242,7 @@ private function reset_options() {
214242
* ## EXAMPLES
215243
*
216244
* $ wp site empty
217-
* Are you sure you want to empty the site at http://www.example.com of all posts, comments, and terms? [y/n] y
245+
* Are you sure you want to empty the site at http://www.example.com of all posts, links, comments, and terms? [y/n] y
218246
* Success: The site at 'http://www.example.com' was emptied.
219247
*
220248
* @subcommand empty
@@ -226,9 +254,10 @@ public function empty_( $args, $assoc_args ) {
226254
$upload_message = ', and delete its uploads directory';
227255
}
228256

229-
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 );
257+
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 );
230258

231259
$this->empty_posts();
260+
$this->empty_links();
232261
$this->empty_comments();
233262
$this->empty_taxonomies();
234263
$this->insert_default_terms();

0 commit comments

Comments
 (0)