5
|
1 |
<?php if ( ! defined( 'OT_VERSION' ) ) exit( 'No direct script access allowed' ); |
|
2 |
/** |
|
3 |
* OptionTree Cleanup API |
|
4 |
* |
|
5 |
* This class loads all the OptionTree Cleanup methods and helpers. |
|
6 |
* |
|
7 |
* @package OptionTree |
|
8 |
* @author Derek Herman <derek@valendesigns.com> |
|
9 |
* @copyright Copyright (c) 2014, Derek Herman |
|
10 |
*/ |
|
11 |
if ( ! class_exists( 'OT_Cleanup' ) ) { |
|
12 |
|
|
13 |
class OT_Cleanup { |
|
14 |
|
|
15 |
/** |
|
16 |
* PHP5 constructor method. |
|
17 |
* |
|
18 |
* This method adds other methods of the class to specific hooks within WordPress. |
|
19 |
* |
|
20 |
* @uses add_action() |
|
21 |
* |
|
22 |
* @return void |
|
23 |
* |
|
24 |
* @access public |
|
25 |
* @since 2.4.6 |
|
26 |
*/ |
|
27 |
function __construct() { |
|
28 |
if ( ! is_admin() ) |
|
29 |
return; |
|
30 |
|
|
31 |
// Load styles |
|
32 |
add_action( 'admin_head', array( $this, 'styles' ), 1 ); |
|
33 |
|
|
34 |
// Maybe Clean up OptionTree |
|
35 |
add_action( 'admin_menu', array( $this, 'maybe_cleanup' ), 100 ); |
|
36 |
|
|
37 |
// Increase timeout if allowed |
|
38 |
add_action( 'ot_pre_consolidate_posts', array( $this, 'increase_timeout' ) ); |
|
39 |
|
|
40 |
} |
|
41 |
|
|
42 |
/** |
|
43 |
* Adds the cleanup styles to the admin head |
|
44 |
* |
|
45 |
* @return string |
|
46 |
* |
|
47 |
* @access public |
|
48 |
* @since 2.5.0 |
|
49 |
*/ |
|
50 |
function styles() { |
|
51 |
|
|
52 |
echo '<style>#toplevel_page_ot-cleanup{display:none;}</style>'; |
|
53 |
|
|
54 |
} |
|
55 |
|
|
56 |
/** |
|
57 |
* Check if OptionTree needs to be cleaned up from a previous install. |
|
58 |
* |
|
59 |
* @return void |
|
60 |
* |
|
61 |
* @access public |
|
62 |
* @since 2.4.6 |
|
63 |
*/ |
|
64 |
public function maybe_cleanup() { |
|
65 |
global $wpdb, $ot_maybe_cleanup_posts, $ot_maybe_cleanup_table; |
|
66 |
|
|
67 |
$table_name = $wpdb->prefix . 'option_tree'; |
|
68 |
$page = isset( $_GET['page'] ) ? $_GET['page'] : ''; |
|
69 |
$ot_maybe_cleanup_posts = count( $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_type = 'option-tree' LIMIT 2" ) ) > 1; |
|
70 |
$ot_maybe_cleanup_table = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) ) == $table_name; |
|
71 |
|
|
72 |
if ( ! $ot_maybe_cleanup_posts && ! $ot_maybe_cleanup_table && $page == 'ot-cleanup' ) { |
|
73 |
wp_redirect( apply_filters( 'ot_theme_options_parent_slug', 'themes.php' ) . '?page=' . apply_filters( 'ot_theme_options_menu_slug', 'ot-theme-options' ) ); |
|
74 |
exit; |
|
75 |
} |
|
76 |
|
|
77 |
if ( $ot_maybe_cleanup_posts || $ot_maybe_cleanup_table ) { |
|
78 |
|
|
79 |
if ( $page != 'ot-cleanup' ) |
|
80 |
add_action( 'admin_notices', array( $this, 'cleanup_notice' ) ); |
|
81 |
|
|
82 |
$theme_check_bs = 'add_menu_page'; |
|
83 |
|
|
84 |
$theme_check_bs( apply_filters( 'ot_cleanup_page_title', __( 'OptionTree Cleanup', 'option-tree' ) ), apply_filters( 'ot_cleanup_menu_title', __( 'OptionTree Cleanup', 'option-tree' ) ), 'edit_theme_options', 'ot-cleanup', array( $this, 'options_page' ) ); |
|
85 |
|
|
86 |
} |
|
87 |
|
|
88 |
} |
|
89 |
|
|
90 |
/** |
|
91 |
* Adds an admin nag. |
|
92 |
* |
|
93 |
* @return string |
|
94 |
* |
|
95 |
* @access public |
|
96 |
* @since 2.4.6 |
|
97 |
*/ |
|
98 |
public function cleanup_notice() { |
|
99 |
|
|
100 |
if ( get_current_screen()->id != 'appearance_page_ot-cleanup' ) |
|
101 |
echo '<div class="update-nag">' . sprintf( __( 'OptionTree has outdated data that should be removed. Please go to %s for more information.', 'option-tree' ), sprintf( '<a href="%s">%s</a>', admin_url( 'themes.php?page=ot-cleanup' ), apply_filters( 'ot_cleanup_menu_title', __( 'OptionTree Cleanup', 'option-tree' ) ) ) ) . '</div>'; |
|
102 |
|
|
103 |
} |
|
104 |
|
|
105 |
/** |
|
106 |
* Adds a Tools sub page to clean up the database with. |
|
107 |
* |
|
108 |
* @return string |
|
109 |
* |
|
110 |
* @access public |
|
111 |
* @since 2.4.6 |
|
112 |
*/ |
|
113 |
public function options_page() { |
|
114 |
global $wpdb, $ot_maybe_cleanup_posts, $ot_maybe_cleanup_table; |
|
115 |
|
|
116 |
// Option ID |
|
117 |
$option_id = 'ot_media_post_ID'; |
|
118 |
|
|
119 |
// Get the media post ID |
|
120 |
$post_ID = get_option( $option_id, false ); |
|
121 |
|
|
122 |
// Zero loop count |
|
123 |
$count = 0; |
|
124 |
|
|
125 |
// Check for safe mode |
|
126 |
$safe_mode = ini_get( 'safe_mode' ); |
|
127 |
|
|
128 |
echo '<div class="wrap">'; |
|
129 |
|
|
130 |
echo '<h2>' . apply_filters( 'ot_cleanup_page_title', __( 'OptionTree Cleanup', 'option-tree' ) ) . '</h2>'; |
|
131 |
|
|
132 |
if ( $ot_maybe_cleanup_posts ) { |
|
133 |
|
|
134 |
$posts = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_type = 'option-tree'" ); |
|
135 |
|
|
136 |
echo '<h3>' . __( 'Multiple Media Posts', 'option-tree' ) . '</h3>'; |
|
137 |
|
|
138 |
echo '<p>' . sprintf( __( 'There are currently %s OptionTree media posts in your database. At some point in the past, a version of OptionTree added multiple %s media post objects cluttering up your %s table. There is no associated risk or harm that these posts have caused other than to add size to your overall database. Thankfully, there is a way to remove all these orphaned media posts and get your database cleaned up.', 'option-tree' ), '<code>' . number_format( count( $posts ) ) . '</code>', '<tt>option-tree</tt>', '<tt>' . $wpdb->posts . '</tt>' ) . '</p>'; |
|
139 |
|
|
140 |
echo '<p>' . sprintf( __( 'By clicking the button below, OptionTree will delete %s records and consolidate them into one single OptionTree media post for uploading attachments to. Additionally, the attachments will have their parent ID updated to the correct media post.', 'option-tree' ), '<code>' . number_format( count( $posts ) - 1 ) . '</code>' ) . '</p>'; |
|
141 |
|
|
142 |
echo '<p><strong>' . __( 'This could take a while to fully process depending on how many records you have in your database, so please be patient and wait for the script to finish.', 'option-tree' ) . '</strong></p>'; |
|
143 |
|
|
144 |
echo $safe_mode ? '<p>' . sprintf( __( '%s Your server is running in safe mode. Which means this page will automatically reload after deleting %s posts, you can filter this number using %s if your server is having trouble processing that many at one time.', 'option-tree' ), '<strong>Note</strong>:', apply_filters( 'ot_consolidate_posts_reload', 500 ), '<tt>ot_consolidate_posts_reload</tt>' ) . '</p>' : ''; |
|
145 |
|
|
146 |
echo '<p><a class="button button-primary" href="' . wp_nonce_url( admin_url( 'themes.php?page=ot-cleanup' ), 'consolidate-posts' ) . '">' . __( 'Consolidate Posts', 'option-tree' ) . '</a></p>'; |
|
147 |
|
|
148 |
if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'consolidate-posts' ) ) { |
|
149 |
|
|
150 |
if ( $post_ID === false || empty( $post_ID ) ) { |
|
151 |
$post_ID = isset( $posts[0]->ID ) ? $posts[0]->ID : null; |
|
152 |
|
|
153 |
// Add to the DB |
|
154 |
if ( $post_ID !== null ) |
|
155 |
update_option( $option_id, $post_ID ); |
|
156 |
|
|
157 |
} |
|
158 |
|
|
159 |
// Do pre consolidation action to increase timeout. |
|
160 |
do_action( 'ot_pre_consolidate_posts' ); |
|
161 |
|
|
162 |
// Loop over posts |
|
163 |
foreach( $posts as $post ) { |
|
164 |
|
|
165 |
// Don't destroy the correct post. |
|
166 |
if ( $post_ID == $post->ID ) |
|
167 |
continue; |
|
168 |
|
|
169 |
// Update count |
|
170 |
$count++; |
|
171 |
|
|
172 |
// Reload script in safe mode |
|
173 |
if ( $safe_mode && $count > apply_filters( 'ot_consolidate_posts_reload', 500 ) ) { |
|
174 |
echo '<br />' . __( 'Reloading...', 'option-tree' ); |
|
175 |
echo ' |
|
176 |
<script> |
|
177 |
setTimeout( ot_script_reload, 3000 ) |
|
178 |
function ot_script_reload() { |
|
179 |
window.location = "' . self_admin_url( 'themes.php?page=ot-cleanup&_wpnonce=' . wp_create_nonce( 'consolidate-posts' ) ) . '" |
|
180 |
} |
|
181 |
</script>'; |
|
182 |
break; |
|
183 |
} |
|
184 |
|
|
185 |
// Get the attachements |
|
186 |
$attachments = get_children( 'post_type=attachment&post_parent=' . $post->ID ); |
|
187 |
|
|
188 |
// Update the attachments parent ID |
|
189 |
if ( ! empty( $attachments ) ) { |
|
190 |
|
|
191 |
echo 'Updating Attachments parent ID for <tt>option-tree</tt> post <tt>#' . $post->ID . '</tt>.<br />'; |
|
192 |
|
|
193 |
foreach( $attachments as $attachment_id => $attachment ) { |
|
194 |
wp_update_post( |
|
195 |
array( |
|
196 |
'ID' => $attachment_id, |
|
197 |
'post_parent' => $post_ID |
|
198 |
) |
|
199 |
); |
|
200 |
} |
|
201 |
|
|
202 |
} |
|
203 |
|
|
204 |
// Delete post |
|
205 |
echo 'Deleting <tt>option-tree</tt> post <tt>#' . $post->ID . '</tt><br />'; |
|
206 |
wp_delete_post( $post->ID, true ); |
|
207 |
|
|
208 |
} |
|
209 |
|
|
210 |
echo '<br />' . __( 'Clean up script has completed, the page will now reload...', 'option-tree' ); |
|
211 |
|
|
212 |
echo ' |
|
213 |
<script> |
|
214 |
setTimeout( ot_script_reload, 3000 ) |
|
215 |
function ot_script_reload() { |
|
216 |
window.location = "' . self_admin_url( 'themes.php?page=ot-cleanup' ) . '" |
|
217 |
} |
|
218 |
</script>'; |
|
219 |
|
|
220 |
} |
|
221 |
|
|
222 |
} |
|
223 |
|
|
224 |
if ( $ot_maybe_cleanup_table ) { |
|
225 |
|
|
226 |
$table_name = $wpdb->prefix . 'option_tree'; |
|
227 |
|
|
228 |
echo $ot_maybe_cleanup_posts ? '<hr />' : ''; |
|
229 |
|
|
230 |
echo '<h3>' . __( 'Outdated Table', 'option-tree' ) . '</h3>'; |
|
231 |
|
|
232 |
echo '<p>' . sprintf( __( 'If you have upgraded from an old 1.x version of OptionTree at some point, you have an extra %s table in your database that can be removed. It\'s not hurting anything, but does not need to be there. If you want to remove it. Click the button below.', 'option-tree' ), '<tt>' . $table_name . '</tt>' ) . '</p>'; |
|
233 |
|
|
234 |
echo '<p><a class="button button-primary" href="' . wp_nonce_url( admin_url( 'themes.php?page=ot-cleanup' ), 'drop-table' ) . '">' . __( 'Drop Table', 'option-tree' ) . '</a></p>'; |
|
235 |
|
|
236 |
if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'drop-table' ) ) { |
|
237 |
|
|
238 |
echo '<p>' . sprintf( __( 'Deleting the outdated and unused %s table...', 'option-tree' ), '<tt>' . $table_name . '</tt>' ) . '</p>'; |
|
239 |
|
|
240 |
$wpdb->query( "DROP TABLE IF EXISTS $table_name" ); |
|
241 |
|
|
242 |
if ( $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) ) != $table_name ) { |
|
243 |
|
|
244 |
echo '<p>' . sprintf( __( 'The %s table has been successfully deleted. The page will now reload...', 'option-tree' ), '<tt>' . $table_name . '</tt>' ) . '</p>'; |
|
245 |
|
|
246 |
echo ' |
|
247 |
<script> |
|
248 |
setTimeout( ot_script_reload, 3000 ) |
|
249 |
function ot_script_reload() { |
|
250 |
window.location = "' . self_admin_url( 'themes.php?page=ot-cleanup' ) . '" |
|
251 |
} |
|
252 |
</script>'; |
|
253 |
|
|
254 |
} else { |
|
255 |
|
|
256 |
echo '<p>' . sprintf( __( 'Something went wrong. The %s table was not deleted.', 'option-tree' ), '<tt>' . $table_name . '</tt>' ) . '</p>'; |
|
257 |
|
|
258 |
} |
|
259 |
|
|
260 |
} |
|
261 |
|
|
262 |
} |
|
263 |
|
|
264 |
echo '</div>'; |
|
265 |
|
|
266 |
} |
|
267 |
|
|
268 |
/** |
|
269 |
* Increase PHP timeout. |
|
270 |
* |
|
271 |
* This is to prevent bulk operations from timing out |
|
272 |
* |
|
273 |
* @return void |
|
274 |
* |
|
275 |
* @access public |
|
276 |
* @since 2.4.6 |
|
277 |
*/ |
|
278 |
public function increase_timeout() { |
|
279 |
|
|
280 |
if ( ! ini_get( 'safe_mode' ) ) { |
|
281 |
|
|
282 |
@set_time_limit( 0 ); |
|
283 |
|
|
284 |
} |
|
285 |
|
|
286 |
} |
|
287 |
|
|
288 |
} |
|
289 |
|
|
290 |
} |
|
291 |
|
|
292 |
new OT_Cleanup(); |
|
293 |
|
|
294 |
/* End of file ot-cleanup-api.php */ |
|
295 |
/* Location: ./includes/ot-cleanup-api.php */ |