- Created deploy-zoho-fixes.sh script to deploy only Zoho-related fixes - Script creates backups before making changes - Patch maintains existing credentials while adding .env loading - Enables diagnostics in wp-config.php - Properly handles file permissions and cache clearing 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
311 lines
No EOL
14 KiB
Bash
Executable file
311 lines
No EOL
14 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
# Deploy Zoho CRM integration fixes to staging server
|
|
# This script deploys only the Zoho-related files to fix the CRM integration
|
|
|
|
# Load environment variables
|
|
source "$(dirname "$0")/../.env"
|
|
|
|
# Check if environment variables are loaded
|
|
if [ -z "$UPSKILL_STAGING_IP" ] || [ -z "$UPSKILL_STAGING_SSH_USER" ]; then
|
|
echo "Error: Missing required environment variables"
|
|
echo "Please ensure .env file exists and contains UPSKILL_STAGING_IP and UPSKILL_STAGING_SSH_USER"
|
|
exit 1
|
|
fi
|
|
|
|
# Set variables
|
|
REMOTE_HOST="${UPSKILL_STAGING_IP}"
|
|
REMOTE_USER="${UPSKILL_STAGING_SSH_USER}"
|
|
REMOTE_PASS="${UPSKILL_STAGING_PASS}"
|
|
REMOTE_PATH="/home/974670.cloudwaysapps.com/uberrxmprk/public_html"
|
|
PLUGIN_PATH="${REMOTE_PATH}/wp-content/plugins/hvac-community-events"
|
|
|
|
# Colors for output
|
|
GREEN='\033[0;32m'
|
|
RED='\033[0;31m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
echo -e "${YELLOW}=== Deploying Zoho CRM Integration Fixes ===${NC}"
|
|
echo -e "${YELLOW}Target: ${REMOTE_USER}@${REMOTE_HOST}:${PLUGIN_PATH}${NC}"
|
|
|
|
# Create backup of current Zoho files
|
|
echo -e "${YELLOW}Creating backup of current Zoho files...${NC}"
|
|
BACKUP_CMD="mkdir -p \"${PLUGIN_PATH}/includes/zoho-backup\" &&
|
|
cp -r \"${PLUGIN_PATH}/includes/zoho\" \"${PLUGIN_PATH}/includes/zoho-backup/zoho-$(date +%Y%m%d%H%M%S)\" &&
|
|
cp -r \"${PLUGIN_PATH}/includes/admin/class-zoho-admin.php\" \"${PLUGIN_PATH}/includes/zoho-backup/class-zoho-admin-$(date +%Y%m%d%H%M%S).php\" &&
|
|
cp -r \"${PLUGIN_PATH}/assets/js/zoho-admin.js\" \"${PLUGIN_PATH}/includes/zoho-backup/zoho-admin-$(date +%Y%m%d%H%M%S).js\" &&
|
|
cp -r \"${PLUGIN_PATH}/assets/css/zoho-admin.css\" \"${PLUGIN_PATH}/includes/zoho-backup/zoho-admin-$(date +%Y%m%d%H%M%S).css\""
|
|
|
|
sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" "$BACKUP_CMD"
|
|
|
|
if [ $? -ne 0 ]; then
|
|
echo -e "${RED}Error: Failed to create backup. Aborting.${NC}"
|
|
exit 1
|
|
fi
|
|
echo -e "${GREEN}Backup created successfully.${NC}"
|
|
|
|
# Deploy Zoho integration files
|
|
echo -e "${YELLOW}Deploying Zoho CRM integration files...${NC}"
|
|
|
|
# Create temporary directory for the files to deploy
|
|
TMP_DIR=$(mktemp -d)
|
|
mkdir -p "${TMP_DIR}/zoho"
|
|
mkdir -p "${TMP_DIR}/admin"
|
|
mkdir -p "${TMP_DIR}/assets/js"
|
|
mkdir -p "${TMP_DIR}/assets/css"
|
|
|
|
# Copy the files we want to deploy to the temporary directory
|
|
cp "$(dirname "$0")/../wordpress/wp-content/plugins/hvac-community-events/includes/zoho/check-permissions.php" "${TMP_DIR}/zoho/"
|
|
cp "$(dirname "$0")/../wordpress/wp-content/plugins/hvac-community-events/includes/zoho/diagnostics.php" "${TMP_DIR}/zoho/"
|
|
cp "$(dirname "$0")/../wordpress/wp-content/plugins/hvac-community-events/includes/zoho/class-zoho-crm-auth.php" "${TMP_DIR}/zoho/"
|
|
cp "$(dirname "$0")/../wordpress/wp-content/plugins/hvac-community-events/includes/admin/class-zoho-admin.php" "${TMP_DIR}/admin/"
|
|
cp "$(dirname "$0")/../wordpress/wp-content/plugins/hvac-community-events/assets/js/zoho-admin.js" "${TMP_DIR}/assets/js/"
|
|
cp "$(dirname "$0")/../wordpress/wp-content/plugins/hvac-community-events/assets/css/zoho-admin.css" "${TMP_DIR}/assets/css/"
|
|
cp "$(dirname "$0")/check-zoho-env.php" "${TMP_DIR}/"
|
|
|
|
# Create deployment script to modify zoho-config.php on the server
|
|
cat > "${TMP_DIR}/patch-zoho-config.php" << 'EOF'
|
|
<?php
|
|
/**
|
|
* Zoho Config Patcher
|
|
*
|
|
* This script updates the zoho-config.php file with the .env loading functionality
|
|
* without overwriting the existing credentials.
|
|
*/
|
|
|
|
// Define the file to patch
|
|
$config_file = dirname(__FILE__) . '/../includes/zoho/zoho-config.php';
|
|
|
|
if (!file_exists($config_file)) {
|
|
echo "Error: zoho-config.php not found at: " . $config_file . "\n";
|
|
exit(1);
|
|
}
|
|
|
|
// Read the current file
|
|
$content = file_get_contents($config_file);
|
|
|
|
// Create backup
|
|
$backup_file = dirname(__FILE__) . '/../includes/zoho-backup/zoho-config-' . date('YmdHis') . '.php';
|
|
file_put_contents($backup_file, $content);
|
|
echo "Backup created at: " . $backup_file . "\n";
|
|
|
|
// Extract the current credentials
|
|
preg_match('/define\(\'ZOHO_CLIENT_ID\',\s*(.*?)\);/s', $content, $client_id_matches);
|
|
preg_match('/define\(\'ZOHO_CLIENT_SECRET\',\s*(.*?)\);/s', $content, $client_secret_matches);
|
|
preg_match('/define\(\'ZOHO_REFRESH_TOKEN\',\s*(.*?)\);/s', $content, $refresh_token_matches);
|
|
|
|
$client_id = isset($client_id_matches[1]) ? trim($client_id_matches[1]) : "getenv('ZOHO_CLIENT_ID') ?: ''";
|
|
$client_secret = isset($client_secret_matches[1]) ? trim($client_secret_matches[1]) : "getenv('ZOHO_CLIENT_SECRET') ?: ''";
|
|
$refresh_token = isset($refresh_token_matches[1]) ? trim($refresh_token_matches[1]) : "getenv('ZOHO_REFRESH_TOKEN') ?: ''";
|
|
|
|
// Create new content with .env loader
|
|
$new_content = <<<EOT
|
|
<?php
|
|
/**
|
|
* Zoho CRM Configuration
|
|
*
|
|
* This file contains the necessary constants for Zoho CRM integration.
|
|
* Modified with enhanced debugging and log file path.
|
|
*/
|
|
|
|
// Load environment variables from .env file
|
|
function load_env_from_dotenv() {
|
|
// Look for .env file in plugin directory and up to 3 levels up
|
|
\$search_dirs = [
|
|
dirname(dirname(dirname(__FILE__))), // Plugin directory
|
|
dirname(dirname(dirname(dirname(__FILE__)))), // wp-content/plugins
|
|
dirname(dirname(dirname(dirname(dirname(__FILE__))))), // wp-content
|
|
dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))), // WordPress root
|
|
];
|
|
|
|
foreach (\$search_dirs as \$dir) {
|
|
\$env_file = \$dir . '/.env';
|
|
if (file_exists(\$env_file)) {
|
|
\$lines = file(\$env_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
|
foreach (\$lines as \$line) {
|
|
if (strpos(\$line, '=') !== false && strpos(\$line, '#') !== 0) {
|
|
list(\$name, \$value) = explode('=', \$line, 2);
|
|
\$name = trim(\$name);
|
|
\$value = trim(\$value);
|
|
|
|
// Remove quotes if present
|
|
if (strpos(\$value, '"') === 0 && strrpos(\$value, '"') === strlen(\$value) - 1) {
|
|
\$value = substr(\$value, 1, -1);
|
|
} elseif (strpos(\$value, "'") === 0 && strrpos(\$value, "'") === strlen(\$value) - 1) {
|
|
\$value = substr(\$value, 1, -1);
|
|
}
|
|
|
|
putenv("\$name=\$value");
|
|
\$_ENV[\$name] = \$value;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Try to load environment variables
|
|
\$env_loaded = load_env_from_dotenv();
|
|
|
|
// Log directory setup
|
|
\$log_dir = dirname(dirname(__FILE__)) . '/logs';
|
|
if (!file_exists(\$log_dir)) {
|
|
mkdir(\$log_dir, 0755, true);
|
|
}
|
|
|
|
// OAuth Client Credentials
|
|
// IMPORTANT: You need to fill these values with your Zoho OAuth credentials
|
|
define('ZOHO_CLIENT_ID', $client_id);
|
|
define('ZOHO_CLIENT_SECRET', $client_secret);
|
|
define('ZOHO_REDIRECT_URI', 'https://wordpress-974670-5399585.cloudwaysapps.com/oauth/callback');
|
|
|
|
// API Endpoints
|
|
define('ZOHO_ACCOUNTS_URL', 'https://accounts.zoho.com');
|
|
define('ZOHO_API_BASE_URL', 'https://www.zohoapis.com/crm/v2');
|
|
|
|
// Scopes
|
|
define('ZOHO_SCOPES', 'ZohoCRM.settings.all,ZohoCRM.modules.all,ZohoCRM.users.all,ZohoCRM.org.all');
|
|
|
|
// Optional - Refresh Token (if already obtained)
|
|
define('ZOHO_REFRESH_TOKEN', $refresh_token);
|
|
|
|
// Debug Settings - Enhanced for better logging
|
|
define('ZOHO_DEBUG_MODE', true);
|
|
define('ZOHO_LOG_FILE', \$log_dir . '/zoho-debug.log');
|
|
|
|
// Add diagnostic information to log
|
|
if (defined('ZOHO_DEBUG_MODE') && ZOHO_DEBUG_MODE) {
|
|
\$timestamp = date('Y-m-d H:i:s');
|
|
\$debug_info = "[\$timestamp] Zoho CRM Configuration loaded\\n";
|
|
\$debug_info .= "[\$timestamp] .env file loaded: " . (\$env_loaded ? 'Yes' : 'No') . "\\n";
|
|
\$debug_info .= "[\$timestamp] Client ID exists: " . (!empty(ZOHO_CLIENT_ID) ? 'Yes' : 'No') . "\\n";
|
|
\$debug_info .= "[\$timestamp] Client ID value: " . (ZOHO_CLIENT_ID ? substr(ZOHO_CLIENT_ID, 0, 5) . '...' : 'EMPTY') . "\\n";
|
|
\$debug_info .= "[\$timestamp] Client Secret exists: " . (!empty(ZOHO_CLIENT_SECRET) ? 'Yes' : 'No') . "\\n";
|
|
\$debug_info .= "[\$timestamp] Client Secret value: " . (ZOHO_CLIENT_SECRET ? substr(ZOHO_CLIENT_SECRET, 0, 5) . '...' : 'EMPTY') . "\\n";
|
|
\$debug_info .= "[\$timestamp] Refresh Token exists: " . (!empty(ZOHO_REFRESH_TOKEN) ? 'Yes' : 'No') . "\\n";
|
|
\$debug_info .= "[\$timestamp] Refresh Token value: " . (ZOHO_REFRESH_TOKEN ? substr(ZOHO_REFRESH_TOKEN, 0, 5) . '...' : 'EMPTY') . "\\n";
|
|
\$debug_info .= "[\$timestamp] Log file path: " . ZOHO_LOG_FILE . "\\n";
|
|
|
|
if (function_exists('get_site_url')) {
|
|
\$debug_info .= "[\$timestamp] WordPress site URL: " . get_site_url() . "\\n";
|
|
\$debug_info .= "[\$timestamp] Staging mode: " . (strpos(get_site_url(), 'upskillhvac.com') === false ? 'Yes' : 'No') . "\\n";
|
|
} else {
|
|
\$debug_info .= "[\$timestamp] WordPress functions not available\\n";
|
|
}
|
|
|
|
// Check for environment variables directly
|
|
\$debug_info .= "[\$timestamp] Environment variables:\\n";
|
|
\$debug_info .= "[\$timestamp] - _ENV['ZOHO_CLIENT_ID']: " . (isset(\$_ENV['ZOHO_CLIENT_ID']) ? 'Set' : 'Not set') . "\\n";
|
|
\$debug_info .= "[\$timestamp] - _ENV['ZOHO_CLIENT_SECRET']: " . (isset(\$_ENV['ZOHO_CLIENT_SECRET']) ? 'Set' : 'Not set') . "\\n";
|
|
\$debug_info .= "[\$timestamp] - _ENV['ZOHO_REFRESH_TOKEN']: " . (isset(\$_ENV['ZOHO_REFRESH_TOKEN']) ? 'Set' : 'Not set') . "\\n";
|
|
|
|
// Log configuration details
|
|
error_log(\$debug_info, 3, ZOHO_LOG_FILE);
|
|
}
|
|
EOT;
|
|
|
|
// Write the updated file
|
|
file_put_contents($config_file, $new_content);
|
|
echo "Updated zoho-config.php successfully.\n";
|
|
|
|
// Update wp-config.php to enable diagnostics
|
|
$wp_config_file = dirname(__FILE__) . '/../wp-config.php';
|
|
if (file_exists($wp_config_file)) {
|
|
$wp_config = file_get_contents($wp_config_file);
|
|
|
|
// Check if diagnostic flag already exists
|
|
if (strpos($wp_config, 'ZOHO_DIAGNOSTICS_ENABLED') === false) {
|
|
// Find the line where custom values should be added
|
|
$pattern = "/\/\* Add any custom values between this line and the \"stop editing\" line\. \*\//";
|
|
$replacement = "/* Add any custom values between this line and the \"stop editing\" line. */\n\n/* Enable Zoho CRM Diagnostics */\ndefine('ZOHO_DIAGNOSTICS_ENABLED', true);";
|
|
|
|
$wp_config = preg_replace($pattern, $replacement, $wp_config);
|
|
|
|
// Create backup
|
|
$wp_backup = dirname(__FILE__) . '/../wp-config-' . date('YmdHis') . '.php.bak';
|
|
file_put_contents($wp_backup, file_get_contents($wp_config_file));
|
|
|
|
// Write updated config
|
|
file_put_contents($wp_config_file, $wp_config);
|
|
echo "Updated wp-config.php to enable Zoho diagnostics.\n";
|
|
} else {
|
|
echo "ZOHO_DIAGNOSTICS_ENABLED already present in wp-config.php.\n";
|
|
}
|
|
} else {
|
|
echo "Warning: wp-config.php not found at expected location.\n";
|
|
}
|
|
|
|
// Create logs directory if it doesn't exist
|
|
$logs_dir = dirname(__FILE__) . '/../wp-content/plugins/hvac-community-events/includes/logs';
|
|
if (!file_exists($logs_dir)) {
|
|
mkdir($logs_dir, 0755, true);
|
|
echo "Created logs directory: " . $logs_dir . "\n";
|
|
}
|
|
|
|
echo "Patching completed successfully.\n";
|
|
EOF
|
|
|
|
# Create logs directory locally for rsync
|
|
mkdir -p "$(dirname "$0")/../wordpress/wp-content/plugins/hvac-community-events/includes/logs"
|
|
|
|
# Create run script
|
|
cat > "${TMP_DIR}/run-deploy.sh" << 'EOF'
|
|
#!/bin/bash
|
|
# Copy files to their correct locations
|
|
cp zoho/* /home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events/includes/zoho/
|
|
cp admin/* /home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events/includes/admin/
|
|
cp assets/js/* /home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events/assets/js/
|
|
cp assets/css/* /home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events/assets/css/
|
|
cp check-zoho-env.php /home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events/
|
|
|
|
# Run the config patcher
|
|
php patch-zoho-config.php
|
|
|
|
# Set permissions
|
|
chmod 755 /home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events/includes/zoho/check-permissions.php
|
|
chmod 755 /home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events/includes/zoho/diagnostics.php
|
|
chmod 644 /home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events/includes/zoho/class-zoho-crm-auth.php
|
|
chmod 644 /home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events/includes/admin/class-zoho-admin.php
|
|
chmod 644 /home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events/assets/js/zoho-admin.js
|
|
chmod 644 /home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events/assets/css/zoho-admin.css
|
|
chmod 755 /home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-content/plugins/hvac-community-events/check-zoho-env.php
|
|
|
|
# Clear cache
|
|
wp cache flush --path=/home/974670.cloudwaysapps.com/uberrxmprk/public_html
|
|
|
|
echo "Deployment completed successfully!"
|
|
EOF
|
|
|
|
chmod +x "${TMP_DIR}/run-deploy.sh"
|
|
|
|
# Upload the files
|
|
echo -e "${YELLOW}Uploading files to staging server...${NC}"
|
|
sshpass -p "$REMOTE_PASS" scp -r -o StrictHostKeyChecking=no "${TMP_DIR}/"* "$REMOTE_USER@$REMOTE_HOST:~/"
|
|
|
|
if [ $? -ne 0 ]; then
|
|
echo -e "${RED}Error: Failed to upload files. Aborting.${NC}"
|
|
rm -rf "${TMP_DIR}"
|
|
exit 1
|
|
fi
|
|
|
|
# Execute the deployment script on the server
|
|
echo -e "${YELLOW}Executing deployment script on server...${NC}"
|
|
sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" "chmod +x ~/run-deploy.sh && ~/run-deploy.sh"
|
|
|
|
if [ $? -ne 0 ]; then
|
|
echo -e "${RED}Error: Deployment script failed. Check server logs for details.${NC}"
|
|
rm -rf "${TMP_DIR}"
|
|
exit 1
|
|
fi
|
|
|
|
# Clean up
|
|
rm -rf "${TMP_DIR}"
|
|
|
|
echo -e "${GREEN}=== Zoho CRM integration fixes deployed successfully! ===${NC}"
|
|
echo -e "${YELLOW}You can now test the connection in the WordPress admin panel.${NC}"
|
|
echo -e "${YELLOW}If issues persist, check the logs at: ${PLUGIN_PATH}/includes/logs/zoho-debug.log${NC}"
|
|
echo -e "${YELLOW}Or run the diagnostic tool at: ${REMOTE_PATH}/wp-content/plugins/hvac-community-events/includes/zoho/diagnostics.php?run_diagnostics=true${NC}"
|
|
|
|
exit 0 |