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