diff -r 372f2766ea20 -r bf1778c34b9a wp/wp-content/plugins/option-tree/includes/class-ot-cleanup.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wp/wp-content/plugins/option-tree/includes/class-ot-cleanup.php Mon Oct 14 18:35:50 2019 +0200 @@ -0,0 +1,297 @@ +#toplevel_page_ot-cleanup{display:none;}'; + } + + /** + * Check if OptionTree needs to be cleaned up from a previous install. + * + * @access public + * @since 2.4.6 + */ + public function maybe_cleanup() { + global $wpdb, $ot_maybe_cleanup_posts, $ot_maybe_cleanup_table; + + $table_name = $wpdb->prefix . 'option_tree'; + $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore + $ot_maybe_cleanup_posts = count( $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_type = 'option-tree' LIMIT 2" ) ) > 1; // phpcs:ignore + $ot_maybe_cleanup_table = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) ) == $table_name; // phpcs:ignore + + if ( ! $ot_maybe_cleanup_posts && ! $ot_maybe_cleanup_table && 'ot-cleanup' === $page ) { + wp_safe_redirect( apply_filters( 'ot_theme_options_parent_slug', 'themes.php' ) . '?page=' . apply_filters( 'ot_theme_options_menu_slug', 'ot-theme-options' ) ); + exit; + } + + if ( $ot_maybe_cleanup_posts || $ot_maybe_cleanup_table ) { + + if ( 'ot-cleanup' !== $page ) { + add_action( 'admin_notices', array( $this, 'cleanup_notice' ) ); + } + + $theme_check_bs = 'add_menu_' . 'page'; // phpcs:ignore + + $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' ) ); + } + } + + /** + * Adds an admin nag. + * + * @access public + * @since 2.4.6 + */ + public function cleanup_notice() { + + if ( 'appearance_page_ot-cleanup' !== get_current_screen()->id ) { + $link = sprintf( '%s', admin_url( 'themes.php?page=ot-cleanup' ), apply_filters( 'ot_cleanup_menu_title', esc_html__( 'OptionTree Cleanup', 'option-tree' ) ) ); + + /* translators: %s: internal admin page URL */ + echo '
' . sprintf( $string, '' . number_format( count( $posts ) ) . '
', 'option-tree', '' . $wpdb->posts . '' ) . '
' . sprintf( esc_html__( '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' ), '' . number_format( count( $posts ) - 1 ) . '
' ) . '
' . esc_html__( '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' ) . '
'; + + /* translators: %1$s: the word Note wrapped in a strong attribute, %2$s: number of posts being deleted */ + $string = __( '%1$s: Your server is running in safe mode. Which means this page will automatically reload after deleting %2$s posts, you can filter this number using %3$s if your server is having trouble processing that many at one time.', 'option-tree' ); + echo $safe_mode ? '' . sprintf( $string, '' . esc_html__( 'Note', 'option-tree' ) . ':', apply_filters( 'ot_consolidate_posts_reload', 500 ), 'ot_consolidate_posts_reload' ) . '
' : ''; // phpcs:ignore + + echo '' . esc_html__( 'Consolidate Posts', 'option-tree' ) . '
'; // phpcs:ignore + + if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'consolidate-posts' ) ) { // phpcs:ignore + + if ( false === $post_ID || empty( $post_ID ) ) { + $post_ID = isset( $posts[0]->ID ) ? $posts[0]->ID : null; + + // Add to the DB. + if ( null !== $post_ID ) { + update_option( $option_id, $post_ID ); + } + } + + // Do pre consolidation action to increase timeout. + do_action( 'ot_pre_consolidate_posts' ); + + // Loop over posts. + foreach ( $posts as $post ) { + + // Don't destroy the correct post. + if ( $post_ID === $post->ID ) { + continue; + } + + // Update count. + $count++; + + // Reload script in safe mode. + if ( $safe_mode && $count > absint( apply_filters( 'ot_consolidate_posts_reload', 500 ) ) ) { + echo '' . sprintf( $string, '' . $table_name . '' ) . '
'; // phpcs:ignore + + echo '' . esc_html__( 'Drop Table', 'option-tree' ) . '
'; + + if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'drop-table' ) ) { // phpcs:ignore + + /* translators: %s: table name */ + $string = esc_html__( 'Deleting the outdated and unused %s table...', 'option-tree' ); + + echo '' . sprintf( $string, '' . $table_name . '' ) . '
'; // phpcs:ignore + + $wpdb->query( "DROP TABLE IF EXISTS $table_name" ); // phpcs:ignore + + if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) ) != $table_name ) { // phpcs:ignore + + /* translators: %s: table name */ + $string = esc_html__( 'The %s table has been successfully deleted. The page will now reload...', 'option-tree' ); + + echo '' . sprintf( $string, '' . $table_name . '' ) . '
'; // phpcs:ignore + + echo ' + '; + + } else { + + /* translators: %s: table name */ + $string = esc_html__( 'Something went wrong. The %s table was not deleted.', 'option-tree' ); + + echo '' . sprintf( $string, '' . $table_name . '' ) . '
'; // phpcs:ignore + } + } + } + + echo '