| 1 |
<?php |
| 2 |
namespace Templately\Core\Developer; |
| 3 |
|
| 4 |
use Templately\Core\Importer\FullSiteImport; |
| 5 |
use Templately\Utils\Base; |
| 6 |
|
| 7 |
/** |
| 8 |
* Network Admin functionality for WordPress Multisite |
| 9 |
* |
| 10 |
* This class handles all network admin specific functionality for Templately |
| 11 |
* when running in a WordPress multisite environment under developer mode. |
| 12 |
* |
| 13 |
* @since 3.3.4 |
| 14 |
*/ |
| 15 |
class NetworkAdmin extends Base { |
| 16 |
|
| 17 |
/** |
| 18 |
* Initialize network admin functionality |
| 19 |
* |
| 20 |
* Only initializes if we're in developer mode and this is a multisite installation |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
// Only initialize if developer mode is enabled and we're in multisite |
| 24 |
if ( ! $this->should_initialize() ) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
$this->init_hooks(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Check if network admin functionality should be initialized |
| 33 |
* |
| 34 |
* @return bool |
| 35 |
*/ |
| 36 |
private function should_initialize() { |
| 37 |
// Check if developer mode is enabled |
| 38 |
if ( ! defined( 'TEMPLATELY_DEVELOPER_MODE' ) || ! constant( 'TEMPLATELY_DEVELOPER_MODE' ) ) { |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
// Check if this is a multisite installation |
| 43 |
if ( ! is_multisite() ) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
return true; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Initialize hooks for network admin functionality |
| 52 |
*/ |
| 53 |
private function init_hooks() { |
| 54 |
// Hook into admin menu for network admin |
| 55 |
add_action( 'network_admin_menu', [ $this, 'add_network_admin_menu' ] ); |
| 56 |
|
| 57 |
// Hook for FSI import functionality |
| 58 |
add_action( 'templately_network_admin_fsi_import', [ $this, 'handle_fsi_import' ] ); |
| 59 |
|
| 60 |
// Hook for multisite subsite creation during FSI |
| 61 |
add_action( 'templately_fsi_before_import', [ $this, 'handle_multisite_creation' ], 10, 2 ); |
| 62 |
|
| 63 |
// Filter for network admin localized data |
| 64 |
add_filter( 'templately_admin_localized_data', [ $this, 'filter_localized_data' ] ); |
| 65 |
|
| 66 |
// Filter for API request modifications |
| 67 |
add_filter( 'templately_api_request_params', [ $this, 'filter_api_request_params' ], 10, 3 ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Add network admin menu |
| 72 |
* |
| 73 |
* This replaces the inline network admin menu code from Admin.php |
| 74 |
*/ |
| 75 |
public function add_network_admin_menu() { |
| 76 |
// Add main menu page for network admin |
| 77 |
add_menu_page( |
| 78 |
'Templately', |
| 79 |
'Templately', |
| 80 |
'manage_network', |
| 81 |
'templately', |
| 82 |
[ $this, 'display_network_admin_page' ], |
| 83 |
templately()->assets->icon( 'logos/logo-icon.svg' ), |
| 84 |
'58.7' |
| 85 |
); |
| 86 |
|
| 87 |
// Add submenu page for template library |
| 88 |
add_submenu_page( |
| 89 |
'templately', |
| 90 |
'Templately', |
| 91 |
'Template Library', |
| 92 |
'manage_network', |
| 93 |
'templately', |
| 94 |
[ $this, 'display_network_admin_page' ], |
| 95 |
'58.7' |
| 96 |
); |
| 97 |
|
| 98 |
// add settings page to network admin |
| 99 |
add_submenu_page( |
| 100 |
'templately', |
| 101 |
'Settings', |
| 102 |
'Settings', |
| 103 |
'manage_network', |
| 104 |
'templately_settings', |
| 105 |
[ $this, 'display_settings_page' ] |
| 106 |
); |
| 107 |
|
| 108 |
// Note: Theme Builder is not added for network admin as per original logic |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Display network admin page |
| 113 |
*/ |
| 114 |
public function display_network_admin_page() { |
| 115 |
// Use the same display method as regular admin |
| 116 |
\Templately\Utils\Helper::views( 'template-library' ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Display settings page |
| 121 |
*/ |
| 122 |
public function display_settings_page() { |
| 123 |
\Templately\Utils\Helper::views( 'settings' ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Handle FSI import for network admin |
| 128 |
* |
| 129 |
* This method can be called via the action hook to trigger FSI import |
| 130 |
* functionality from network admin context. |
| 131 |
*/ |
| 132 |
public function handle_fsi_import() { |
| 133 |
// Ensure we have proper permissions |
| 134 |
if ( ! current_user_can( 'manage_network' ) ) { |
| 135 |
wp_die( __( 'You do not have sufficient permissions to access this page.', 'templately' ) ); |
| 136 |
} |
| 137 |
|
| 138 |
// Trigger the FSI import process |
| 139 |
// This would be called by the React component or AJAX handler |
| 140 |
do_action( 'templately_fsi_import_triggered' ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Handle multisite subsite creation during FSI import |
| 145 |
* |
| 146 |
* @param object $fsi_instance The FullSiteImport instance |
| 147 |
* @param array $request_params The request parameters |
| 148 |
*/ |
| 149 |
public function handle_multisite_creation( $fsi_instance, $request_params ) { |
| 150 |
// Only handle if we're in network admin and multisite |
| 151 |
if ( ! is_multisite() || empty( $_GET['isNetworkAdmin'] ) ) { |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
$progress = $request_params['progress'] ?? []; |
| 156 |
|
| 157 |
if ( empty( $progress['create_multi_site'] ) ) { |
| 158 |
// Create new multisite subsite with proper network type detection |
| 159 |
$title = $request_params['title']; |
| 160 |
$tagline = $request_params['slogan']; |
| 161 |
$user_id = get_current_user_id(); |
| 162 |
|
| 163 |
$new_site = $this->create_multisite_subsite( $user_id, $progress, $title, $tagline, $fsi_instance ); |
| 164 |
|
| 165 |
// Update request params after site creation |
| 166 |
$fsi_instance->request_params = $fsi_instance->get_session_data(); |
| 167 |
} else { |
| 168 |
$blog_id = $request_params['blog_id']; |
| 169 |
// Switch to the existing site |
| 170 |
switch_to_blog( $blog_id ); |
| 171 |
} |
| 172 |
|
| 173 |
$fsi_instance->request_params = $fsi_instance->get_session_data(); |
| 174 |
$fsi_instance->initialize_props(); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Filter localized data for network admin |
| 179 |
* |
| 180 |
* @param array $data The localized data array |
| 181 |
* @return array Modified localized data |
| 182 |
*/ |
| 183 |
public function filter_localized_data( $data ) { |
| 184 |
// Only modify if we're in network admin |
| 185 |
if ( ! is_network_admin() ) { |
| 186 |
return $data; |
| 187 |
} |
| 188 |
|
| 189 |
// Set the isNetworkAdmin flag to ensure it's properly set |
| 190 |
$data['isNetworkAdmin'] = true; |
| 191 |
|
| 192 |
// Add any network-specific configuration |
| 193 |
$data['networkAdminEnabled'] = true; |
| 194 |
|
| 195 |
return $data; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Filter API request arguments for network admin |
| 200 |
* |
| 201 |
* This allows network admin functionality to modify request arguments |
| 202 |
* when needed, including handling multisite-specific API configurations. |
| 203 |
* |
| 204 |
* @param array $args The API request arguments |
| 205 |
* @param string $method The HTTP method (GET/POST) |
| 206 |
* @param string $endpoint The API endpoint |
| 207 |
* @return array Modified arguments |
| 208 |
*/ |
| 209 |
public function filter_api_request_params( $args, $method, $endpoint ) { |
| 210 |
// Only modify if we're in multisite network admin context |
| 211 |
if ( ! is_multisite() || ! isset( $_GET['isNetworkAdmin'] ) || ! current_user_can( 'manage_network' ) ) { |
| 212 |
return $args; |
| 213 |
} |
| 214 |
|
| 215 |
// Handle multisite network admin API requests |
| 216 |
$main_site_id = get_main_site_id(); |
| 217 |
$network_home_url = network_home_url(); |
| 218 |
|
| 219 |
// Switch to main site to get the correct API key |
| 220 |
switch_to_blog( $main_site_id ); |
| 221 |
$api_key = \Templately\Utils\Options::get_instance()->get( 'api_key' ); |
| 222 |
restore_current_blog(); |
| 223 |
|
| 224 |
// Update headers with network admin specific values |
| 225 |
if ( isset( $args['headers'] ) ) { |
| 226 |
$args['headers']['Authorization'] = 'Bearer ' . $api_key; |
| 227 |
$args['headers']['x-templately-url'] = $network_home_url; |
| 228 |
} |
| 229 |
|
| 230 |
return $args; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Get network admin specific settings |
| 235 |
* |
| 236 |
* @return array Network admin settings |
| 237 |
*/ |
| 238 |
public function get_network_settings() { |
| 239 |
return [ |
| 240 |
'enabled' => $this->should_initialize(), |
| 241 |
'multisite_type' => $this->get_multisite_type(), |
| 242 |
'main_site_id' => get_main_site_id(), |
| 243 |
'network_home_url' => network_home_url(), |
| 244 |
]; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Detect multisite installation type |
| 249 |
* |
| 250 |
* @return string 'subdomain' or 'subdirectory' |
| 251 |
*/ |
| 252 |
private function get_multisite_type() { |
| 253 |
if ( defined( 'SUBDOMAIN_INSTALL' ) && SUBDOMAIN_INSTALL ) { |
| 254 |
return 'subdomain'; |
| 255 |
} |
| 256 |
|
| 257 |
if ( function_exists( 'is_subdomain_install' ) && is_subdomain_install() ) { |
| 258 |
return 'subdomain'; |
| 259 |
} |
| 260 |
|
| 261 |
return 'subdirectory'; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Create a new multisite subsite for FSI import |
| 266 |
* |
| 267 |
* @param int $user_id The user ID |
| 268 |
* @param array $progress The progress array |
| 269 |
* @param string $title The site title |
| 270 |
* @param string $tagline The site tagline/description |
| 271 |
* @param object $fsi_instance The FullSiteImport instance |
| 272 |
* @return int The new site ID |
| 273 |
* @throws Exception |
| 274 |
*/ |
| 275 |
private function create_multisite_subsite( $user_id, $progress, $title, $tagline, $fsi_instance ) { |
| 276 |
// Get the current network site |
| 277 |
$current_site = get_current_site(); |
| 278 |
$domain = $current_site->domain; |
| 279 |
|
| 280 |
// Detect network type (subdomain vs subdirectory) |
| 281 |
$is_subdomain_network = is_subdomain_install(); |
| 282 |
|
| 283 |
// Generate site identifier from title |
| 284 |
$site_slug = sanitize_title( $title ); |
| 285 |
|
| 286 |
if ( $is_subdomain_network ) { |
| 287 |
// For subdomain networks: create subdomain like sitename.maindomain.com |
| 288 |
$subdomain = $site_slug . '.' . $domain; |
| 289 |
$path = '/'; |
| 290 |
|
| 291 |
// Check if subdomain already exists |
| 292 |
$exists = domain_exists( $subdomain, $path, $current_site->id ); |
| 293 |
if ( $exists ) { |
| 294 |
// Generate a unique subdomain if it already exists |
| 295 |
$i = 1; |
| 296 |
do { |
| 297 |
$new_subdomain = $site_slug . '-' . $i . '.' . $domain; |
| 298 |
$new_title = $title . ' ' . $i; |
| 299 |
$exists = domain_exists( $new_subdomain, $path, $current_site->id ); |
| 300 |
$i++; |
| 301 |
} while ( $exists ); |
| 302 |
$subdomain = $new_subdomain; |
| 303 |
$title = $new_title; |
| 304 |
} |
| 305 |
|
| 306 |
$site_domain = $subdomain; |
| 307 |
$site_path = $path; |
| 308 |
} else { |
| 309 |
// For subdirectory networks: create path like maindomain.com/sitename |
| 310 |
$path = '/' . $site_slug . '/'; |
| 311 |
|
| 312 |
// Check if path already exists |
| 313 |
$exists = domain_exists( $domain, $path, $current_site->id ); |
| 314 |
if ( $exists ) { |
| 315 |
// Generate a unique path if it already exists |
| 316 |
$i = 1; |
| 317 |
do { |
| 318 |
$new_path = '/' . $site_slug . '-' . $i . '/'; |
| 319 |
$new_title = $title . ' ' . $i; |
| 320 |
$exists = domain_exists( $domain, $new_path, $current_site->id ); |
| 321 |
$i++; |
| 322 |
} while ( $exists ); |
| 323 |
$path = $new_path; |
| 324 |
$title = $new_title; |
| 325 |
} |
| 326 |
|
| 327 |
$site_domain = $domain; |
| 328 |
$site_path = $path; |
| 329 |
} |
| 330 |
|
| 331 |
// Prepare site data |
| 332 |
$new_site_data = array( |
| 333 |
'domain' => $site_domain, |
| 334 |
'path' => $site_path, |
| 335 |
'title' => $title, |
| 336 |
'user_id' => $user_id, |
| 337 |
'network_id' => $current_site->id, |
| 338 |
); |
| 339 |
|
| 340 |
// Create the new site |
| 341 |
$new_site = wp_insert_site( $new_site_data ); |
| 342 |
|
| 343 |
if ( is_wp_error( $new_site ) ) { |
| 344 |
$fsi_instance->throw( __( 'Error creating site: ' . $new_site->get_error_message(), 'templately' ) ); |
| 345 |
} |
| 346 |
|
| 347 |
// Initialize the new site |
| 348 |
wp_initialize_site( $new_site, array() ); |
| 349 |
|
| 350 |
// Update session data with the new site ID (before switching context) |
| 351 |
$fsi_instance->update_session_data( [ |
| 352 |
'blog_id' => $new_site, |
| 353 |
'network_url' => home_url( '/' ), |
| 354 |
] ); |
| 355 |
|
| 356 |
// Mark multisite creation as complete |
| 357 |
$progress['create_multi_site'] = true; |
| 358 |
$fsi_instance->update_session_data( [ 'progress' => $progress ] ); |
| 359 |
$fsi_instance->request_params = $fsi_instance->get_session_data(); |
| 360 |
|
| 361 |
// Switch to the new site |
| 362 |
switch_to_blog( $new_site ); |
| 363 |
|
| 364 |
$fsi_instance->update_session_data( $fsi_instance->request_params ); |
| 365 |
|
| 366 |
// Set the tagline for the new site (we're already switched to the new site context) |
| 367 |
if ( ! empty( $tagline ) ) { |
| 368 |
// Since we're already switched to the new blog, use update_option directly |
| 369 |
// $result = update_option('blogdescription', $tagline); |
| 370 |
// if (!$result) { |
| 371 |
// // Log warning but don't fail the entire process |
| 372 |
// error_log('Templately: Failed to update blog description for site ' . $new_site); |
| 373 |
// } |
| 374 |
} |
| 375 |
|
| 376 |
// Log the successful creation |
| 377 |
$fsi_instance->sse_message( [ |
| 378 |
'type' => 'eventLog', |
| 379 |
'action' => 'eventLog', |
| 380 |
'info' => 'create_log_dir', |
| 381 |
'results' => __METHOD__ . '::' . __LINE__, |
| 382 |
] ); |
| 383 |
|
| 384 |
return $new_site; |
| 385 |
} |
| 386 |
} |
| 387 |
|