Created
February 9, 2023 23:26
-
-
Save seb86/694a7d47d5577be6b7aeb5d3c0e71f83 to your computer and use it in GitHub Desktop.
A must-use plugin that removes the need to run the packages within WooCommerce making it run better for performance.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WooCommerce Lite | |
* Description: Removes the need to run the packages within WooCommerce making it run better for performance. | |
* Version: 0.0.1 | |
*/ | |
add_action( 'upgrader_process_complete', 'depackage_woocommerce_after_update', 10, 2 ); | |
function depackage_woocommerce_after_update( $upgrader_object, $options ) { | |
// Only run this code if WooCommerce was updated. | |
if ( ! array_key_exists( 'woocommerce/woocommerce.php', $options['plugins'] ) ) { | |
return; | |
} | |
$woocommerce_file = WP_PLUGIN_DIR . '/woocommerce/woocommerce.php'; | |
$lines_to_remove = array( | |
'require __DIR__ . \'/src/Packages.php\';', | |
'\\Automattic\\WooCommerce\\Packages::init();', | |
); | |
$new_name = 'WooCommerce Core'; | |
$old_name = 'WooCommerce'; | |
// Read the file into a string. | |
$file_contents = file_get_contents( $woocommerce_file ); | |
// Check if the lines to be removed exist in the file. | |
foreach ( $lines_to_remove as $line ) { | |
if ( strpos( $file_contents, $line ) === false ) { | |
return; | |
} | |
} | |
// Remove the specified lines. | |
$file_contents = str_replace( $lines_to_remove, '', $file_contents ); | |
// Check if the current name exists in the file. | |
if ( strpos( $file_contents, $old_name ) === false ) { | |
return; | |
} | |
// Replace the old name with the new name. | |
$file_contents = str_replace( "Plugin Name: $old_name", "Plugin Name: $new_name", $file_contents ); | |
// Write the updated string back to the file. | |
file_put_contents( $woocommerce_file, $file_contents ); | |
$woocommerce_admin_folder = WP_PLUGIN_DIR . '/woocommerce/packages/woocommerce-admin'; | |
// Remove packages not needed anymore. | |
if ( file_exists( $woocommerce_admin_folder ) ) { | |
array_map( 'unlink', glob("$woocommerce_admin_folder/*.*") ); | |
rmdir( $woocommerce_admin_folder ); | |
} | |
$upgrader_object->skin->feedback( "WooCommerce has been unpacked." ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment