Skip to content

Instantly share code, notes, and snippets.

@codezz
Created November 21, 2024 07:56
Show Gist options
  • Select an option

  • Save codezz/4ed0d1cf8c98ceb2ebc6a6b4e4fb7ca3 to your computer and use it in GitHub Desktop.

Select an option

Save codezz/4ed0d1cf8c98ceb2ebc6a6b4e4fb7ca3 to your computer and use it in GitHub Desktop.
Dollie WordPress Hub - Launch Site helper
<?php
/*
Plugin Name: Dollie Site Helper
Description: A helper plugin to launch Dollie sites.
Version: 1.0.0
Author: getdollie.com
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Launch a dollie site helper. Returns the container URL or WP_Error class.
*
* @param string $domain
* @param ?int $blueprint_id
* @param ?string $email
* @return \WP_Error|string
*/
function wpdol_launch_site_helper( $domain, $blueprint_id = null, $email = null ) {
if ( ! is_user_logged_in() ) {
return new WP_Error('not_logged_in', 'You need to be logged in to launch a site');
}
if (empty($domain)) {
return new WP_Error('empty_domain', 'Domain is empty');
}
$domain = wpdol_get_checked_container_name(strtolower($domain));
$user = wp_get_current_user();
$user_id = $user->ID;
$username = $user->user_login;
if (empty($email)) {
$email = $user->user_email;
}
if ( ! empty($blueprint_id) ) {
$container = dollie()->get_container( $blueprint_id );
if ( ! is_wp_error( $container ) && $container->is_blueprint() ) {
$blueprint_id = $container->get_id();
$blueprint_hash = $container->get_hash();
}
// Remove the Blueprint cookie if it exists.
if ( isset( $_COOKIE[ DOLLIE_BLUEPRINTS_COOKIE ] ) ) {
setcookie( DOLLIE_BLUEPRINTS_COOKIE, '', time() - 3600, '/' );
}
}
$deploy_data = [
'owner_id' => $user_id,
'blueprint_id' => $blueprint_id,
'blueprint' => $blueprint_hash,
'email' => $email,
'username' => $username,
'name' => 'WordPress Site name',
'description' => 'WordPress Site Description',
'redirect' => '',
];
$deploy_data = apply_filters( 'dollie/launch_site/form_deploy_data', $deploy_data );
$container = \Dollie\Core\Services\DeployService::instance()->start(
'0',
$domain,
$deploy_data
);
if ( is_wp_error( $container ) ) {
return new WP_Error(
'container_error',
esc_html__( 'Something went wrong. Please try again or contact our support if the problem persists.', 'dollie' )
);
}
return $container->get_permalink();
}
function wpdol_get_checked_container_name($name, $increment = 0) {
$args = array(
'post_type' => 'container',
'name' => $name . ($increment ? 'v' . $increment : ''),
'post_status' => 'any',
'posts_per_page' => 1
);
$query = new WP_Query($args);
$exists = $query->have_posts();
if ($exists) {
// If the name exists, recursively call the function with an incremented value
return wpdol_get_checked_container_name($name, $increment + 1);
}
return $name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment