id = 'old_slugs'; $this->short_desc = __( 'Old Slugs', 'woocommerce-jetpack' ); $this->desc = __( 'Remove old products slugs.', 'woocommerce-jetpack' ); $this->link_slug = 'woocommerce-remove-old-products-slugs'; parent::__construct(); $this->add_tools( array( 'old_slugs' => array( 'title' => __( 'Remove Old Slugs', 'woocommerce-jetpack' ), 'desc' => __( 'Tool removes old slugs/permalinks from database.', 'woocommerce-jetpack' ), ), ) ); } /** * create_old_slugs_tool. * * @version 2.8.0 */ function create_old_slugs_tool() { global $wpdb; $wp_postmeta_table = $wpdb->prefix . 'postmeta'; $all_old_slugs = $wpdb->get_results( "SELECT * FROM $wp_postmeta_table WHERE meta_key = '_wp_old_slug' ORDER BY post_id" ); $num_old_slugs = count( $all_old_slugs ); $remove_result_html = ''; $headings = array( __( 'Old slug', 'woocommerce-jetpack' ), __( 'Post title', 'woocommerce-jetpack' ), __( 'Post id', 'woocommerce-jetpack' ), __( 'Post type', 'woocommerce-jetpack' ), __( 'Current slug', 'woocommerce-jetpack' ), ); $multi_table_data = array( 'products' => array( $headings ), 'non_products' => array( $headings ), ); $posts_ids = array( 'products' => array(), 'non_products' => array(), ); if ( $num_old_slugs > 0 ) { // Fill `multi_table_data` and `posts_ids` foreach ( $all_old_slugs as $old_slug_object ) { $slug_post_type = get_post_type( $old_slug_object->post_id ); $current_slug = get_post( $old_slug_object->post_id )->post_name; $type = ( in_array( $slug_post_type, array( 'product', 'product_variation' ) ) ) ? 'products' : 'non_products'; $multi_table_data[ $type ][] = array( '' . $old_slug_object->meta_value . '', get_the_title( $old_slug_object->post_id ), $old_slug_object->post_id, $slug_post_type, $current_slug, ); $posts_ids[ $type ][] = $old_slug_object->post_id; } // Actions if ( isset( $_POST['remove_old_products_slugs'] ) || isset( $_POST['remove_old_non_products_slugs'] ) ) { $post_ids_to_delete = join( ',', ( isset( $_POST['remove_old_products_slugs'] ) ? $posts_ids['products'] : $posts_ids['non_products'] ) ); $delete_result = $wpdb->get_results( "DELETE FROM $wp_postmeta_table WHERE meta_key = '_wp_old_slug' AND post_id IN ($post_ids_to_delete)" ); $recheck_result = $wpdb->get_results( "SELECT * FROM $wp_postmeta_table WHERE meta_key = '_wp_old_slug'" ); $recheck_result_count = count( $recheck_result ); $remove_result_html = '
' . sprintf( __( 'Removing old slugs from database finished! %d old slug(s) deleted.', 'woocommerce-jetpack' ), ( $num_old_slugs - $recheck_result_count ) ) . ' ' . __( 'Please refresh the page.', 'woocommerce-jetpack' ) . '
' . $type['table_content'] . '
'; $html .= ''; } } if ( $num_old_slugs == 0 ) { $html .= '' . __( 'No old slugs found.', 'woocommerce-jetpack' ) . '