Last active
September 14, 2020 14:25
-
-
Save wpmudev-sls/72adf1d435bbdabd80a200922c678340 to your computer and use it in GitHub Desktop.
[Hustle] - Import module json on new subsite
This file contains 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: [Hustle] - Import module json on new subsite | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Imports modules, from previously exported json file, on newly created subsites. | |
* Author: Ivan Svyrskyi @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
return; | |
} | |
add_action( | |
'wp_insert_site', | |
function( $new_site ) { | |
if ( class_exists( 'Hustle_Module_Model' ) && ! empty( $new_site->id ) ) { | |
$module_type = 'popup'; | |
$filename = WP_CONTENT_DIR . '/mu-plugins/hustle_module.json'; | |
$file_content = file_get_contents( $filename ); | |
//$file_content = ''; | |
$data = json_decode( $file_content, true ); | |
// Prevent importing ssharing into other module types, and vice versa. | |
if ( Hustle_Module_Model::SOCIAL_SHARING_MODULE === $module_type xor Hustle_Module_Model::SOCIAL_SHARING_MODULE === $data['data']['module_type'] ) { | |
return; | |
} | |
$data['data']['module_type'] = $module_type; | |
// Set the module mode according to the selected settings. | |
if ( Hustle_Module_Model::INFORMATIONAL_MODE !== $data['data']['module_mode'] ) { | |
$data['data']['module_mode'] = Hustle_Module_Model::OPTIN_MODE; | |
} | |
$module_data = array_merge( $data['data'], $data['meta'] ); | |
if ( Hustle_Module_Model::SOCIAL_SHARING_MODULE !== $module_type ) { | |
$module_model = new Hustle_Module_Model(); | |
} else { | |
$module_model = new Hustle_SShare_Model(); | |
} | |
switch_to_blog( $new_site->id ); | |
// Create Hustle custom tables. | |
Hustle_Db::maybe_create_tables(); | |
// Create new module. | |
$module_model->create_new( $module_data ); | |
restore_current_blog(); | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment