File manager - Edit - /home/aresglob/public_html/wp/wp-includes/images/smilies/onboarding.tar
Back
class-onboarding.php 0000644 00000001374 15104367571 0010520 0 ustar 00 <?php /** * Intelligent Starter Templates * * @since 3.0.0 * @package Astra Sites */ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. } if ( ! defined( 'INTELLIGENT_TEMPLATES_FILE' ) ) { define( 'INTELLIGENT_TEMPLATES_FILE', __FILE__ ); } if ( ! defined( 'INTELLIGENT_TEMPLATES_BASE' ) ) { define( 'INTELLIGENT_TEMPLATES_BASE', plugin_basename( INTELLIGENT_TEMPLATES_FILE ) ); } if ( ! defined( 'INTELLIGENT_TEMPLATES_DIR' ) ) { define( 'INTELLIGENT_TEMPLATES_DIR', plugin_dir_path( INTELLIGENT_TEMPLATES_FILE ) ); } if ( ! defined( 'INTELLIGENT_TEMPLATES_URI' ) ) { define( 'INTELLIGENT_TEMPLATES_URI', plugins_url( '/', INTELLIGENT_TEMPLATES_FILE ) ); } require_once INTELLIGENT_TEMPLATES_DIR . 'class-onboarding-loader.php'; classes/class-astra-sites-install-plugin.php 0000644 00000072373 15104367571 0015241 0 ustar 00 <?php /** * Astra Sites Install Plugin Class * * This file contains the logic for handling plugin installations during site import via REST API. * * @since 4.4.31 * @package Astra Sites */ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. } if ( ! class_exists( 'Astra_Sites_Install_Plugin' ) ) : /** * Class Astra_Sites_Install_Plugin * * Main class for handling plugin installations during site import via AJAX. */ class Astra_Sites_Install_Plugin { /** * Instance * * @access private * @var object Class object. * @since 4.4.31 */ private static $instance; /** * AJAX action name * * @var string */ private $ajax_action = 'astra_sites_install_plugin'; /** * Initiator * * @since 4.4.31 * @return object initialized object of class. */ public static function get_instance() { if (self::$instance === null) { self::$instance = new self(); } return self::$instance; } /** * Constructor */ public function __construct() { // Register AJAX handler only for logged-in users (plugin installation requires authentication). add_action( 'wp_ajax_' . $this->ajax_action, array( $this, 'handle_ajax_install_plugin' ) ); } /** * Handle AJAX request for plugin installation * * @return void */ public function handle_ajax_install_plugin() { // Set proper content type for JSON response. header( 'Content-Type: application/json' ); try { // Verify nonce for security. if ( ! $this->verify_ajax_nonce() ) { wp_send_json_error( array( 'message' => __( 'Security verification failed. Please refresh the page and try again.', 'astra-sites' ), 'code' => 'nonce_verification_failed', ), 403 ); } // Check user permissions. $permission_check = $this->check_install_plugin_permissions(); if ( is_wp_error( $permission_check ) ) { wp_send_json_error( array( 'message' => $permission_check->get_error_message(), 'code' => $permission_check->get_error_code(), ), 403 ); } // Get and validate plugin data. $plugin_data = $this->get_and_validate_plugin_data(); if ( is_wp_error( $plugin_data ) ) { wp_send_json_error( array( 'message' => $plugin_data->get_error_message(), 'code' => $plugin_data->get_error_code(), ), 400 ); } // Extract plugin information. $plugin_slug = $plugin_data['slug']; $plugin_name = $plugin_data['name']; // Include necessary WordPress files. $this->include_required_files(); // Check if plugin is already installed. $installed_plugins = get_plugins(); $plugin_file = $this->get_plugin_file( $plugin_slug ); if ( $plugin_file && isset( $installed_plugins[ $plugin_file ] ) ) { // Plugin already installed. wp_send_json_success( array( 'message' => sprintf( __( '%s plugin is already installed.', 'astra-sites' ), $plugin_name ), 'status' => 'already_installed', 'data' => array( 'plugin' => array( 'slug' => $plugin_slug, 'name' => $plugin_name, 'file' => $plugin_file, ), ), ) ); } // Install plugin. $install_result = $this->perform_plugin_installation( $plugin_slug, $plugin_name ); if ( is_wp_error( $install_result ) ) { wp_send_json_error( array( 'message' => $install_result->get_error_message(), 'code' => $install_result->get_error_code(), 'data' => $install_result->get_error_data(), ), 500 ); } // Get plugin file after installation. $plugin_file = $this->get_plugin_file( $plugin_slug ); if ( ! $plugin_file ) { wp_send_json_error( array( 'message' => sprintf( __( 'Plugin %s was installed but could not locate plugin file.', 'astra-sites' ), $plugin_name ), 'code' => 'plugin_file_not_found_after_install', ), 500 ); } // Success response. wp_send_json_success( array( 'message' => sprintf( __( '%s plugin installed successfully.', 'astra-sites' ), $plugin_name ), 'status' => 'installed', 'data' => array( 'plugin' => array( 'slug' => $plugin_slug, 'name' => $plugin_name, 'file' => $plugin_file, ), ), ) ); } catch ( Exception $e ) { // Handle any unexpected exceptions. astra_sites_error_log( 'Astra Sites Plugin Installation Error: ' . $e->getMessage() ); wp_send_json_error( array( 'message' => __( 'An unexpected error occurred during plugin installation.', 'astra-sites' ), 'code' => 'unexpected_error', ), 500 ); } } /** * Verify AJAX nonce for security * * @return bool True if nonce is valid, false otherwise. */ private function verify_ajax_nonce() { $nonce = isset( $_POST['_ajax_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_ajax_nonce'] ) ) : ''; if ( empty( $nonce ) ) { return false; } // Try multiple nonce actions that might be used in Astra Sites. $nonce_actions = array( 'astra-sites', // Primary nonce action used in other Astra Sites AJAX calls. 'astra_sites_ajax', // Alternative nonce action. 'wp_rest', // WordPress REST API nonce. ); foreach ( $nonce_actions as $action ) { if ( wp_verify_nonce( $nonce, $action ) ) { return true; } } return false; } /** * Check permissions for plugin installation * * @return bool|WP_Error True if user has permission, otherwise WP_Error */ private function check_install_plugin_permissions() { // Check if user has permission to install plugins if ( ! current_user_can( 'install_plugins' ) ) { return new WP_Error( 'insufficient_permissions', __( 'You do not have permissions to install plugins.', 'astra-sites' ) ); } // Additional security check - ensure user is logged in if ( ! is_user_logged_in() ) { return new WP_Error( 'user_not_logged_in', __( 'You must be logged in to install plugins.', 'astra-sites' ) ); } return true; } /** * Get and validate plugin data from AJAX request * * @return array<string, mixed>|WP_Error Plugin data array or WP_Error on failure */ private function get_and_validate_plugin_data() { // Get plugin slug $plugin_slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : ''; if ( empty( $plugin_slug ) ) { return new WP_Error( 'missing_plugin_slug', __( 'Plugin slug is required.', 'astra-sites' ) ); } // Validate plugin slug format if ( ! $this->validate_plugin_slug( $plugin_slug ) ) { return new WP_Error( 'invalid_plugin_slug', __( 'Plugin slug contains invalid characters.', 'astra-sites' ) ); } // Get plugin name (optional, defaults to slug) $plugin_name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : $plugin_slug; // Get plugin init (optional) $plugin_init = isset( $_POST['init'] ) ? sanitize_text_field( wp_unslash( $_POST['init'] ) ) : ''; return array( 'slug' => $plugin_slug, 'name' => $plugin_name, 'init' => $plugin_init, ); } /** * Validate plugin slug format * * @param string $slug Plugin slug to validate * @return mixed 1 if valid, false if failure */ private function validate_plugin_slug( $slug ) { // Basic slug validation (alphanumeric, hyphens, underscores) return preg_match( '/^[a-zA-Z0-9_-]+$/', $slug ); } /** * Include required WordPress files * * @return void */ private function include_required_files() { if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } if ( ! function_exists( 'request_filesystem_credentials' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } if ( ! class_exists( 'Plugin_Upgrader' ) ) { require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; } } /** * Get plugin file path from slug * * @param string $plugin_slug Plugin slug. * @return string|false Plugin file path or false if not found. */ private function get_plugin_file( $plugin_slug ) { // Try common plugin file patterns $possible_files = array( $plugin_slug . '/' . $plugin_slug . '.php', $plugin_slug . '/index.php', $plugin_slug . '/plugin.php', ); $installed_plugins = get_plugins(); foreach ( $possible_files as $file ) { if ( isset( $installed_plugins[ $file ] ) ) { return $file; } } // Search through all installed plugins for this slug foreach ( $installed_plugins as $file => $plugin_data ) { if ( strpos( $file, $plugin_slug . '/' ) === 0 ) { return $file; } } return false; } /** * Perform plugin installation using WordPress.org repository * * @param string $plugin_slug Plugin slug. * @param string $plugin_name Plugin name. * @return true|WP_Error True on success, WP_Error on failure. */ private function perform_plugin_installation( $plugin_slug, $plugin_name ) { try { // Validate input parameters if ( empty( $plugin_slug ) || empty( $plugin_name ) ) { throw new Exception( __( 'Plugin slug and name are required for installation.', 'astra-sites' ), 0 // code must be int ); } astra_sites_error_log( sprintf( 'Starting installation of plugin %s (%s)', $plugin_name, $plugin_slug ) ); // Step 1: Get plugin API information $api = $this->get_plugin_api_info( $plugin_slug, $plugin_name ); if ( is_wp_error( $api ) ) { return $api; } // Step 2: Perform the actual installation $result = $this->execute_plugin_installation( $api, $plugin_slug, $plugin_name ); if ( is_wp_error( $result ) ) { return $result; } astra_sites_error_log( sprintf( 'Successfully installed plugin %s (%s)', $plugin_name, $plugin_slug ) ); return true; } catch ( Exception $e ) { // Catch any unexpected exceptions $error_message = sprintf( __( 'Unexpected error during %s plugin installation: %s', 'astra-sites' ), $plugin_name, $e->getMessage() ); astra_sites_error_log( sprintf( 'Unexpected exception for plugin %s: %s', $plugin_slug, $e->getMessage() ) ); return new WP_Error( 'unexpected_error', $error_message, array( 'plugin_slug' => $plugin_slug, 'exception_message' => $e->getMessage(), 'exception_code' => $e->getCode(), 'troubleshooting_tips' => array( __( 'Please try the installation again.', 'astra-sites' ), __( 'If the problem persists, try installing the plugin manually from the WordPress admin.', 'astra-sites' ), __( 'Contact your hosting provider if you continue to experience issues.', 'astra-sites' ), ), ) ); } } /** * Get plugin information from WordPress.org API * * @param string $plugin_slug Plugin slug. * @param string $plugin_name Plugin name. * @return object|WP_Error Plugin API object on success, WP_Error on failure. */ private function get_plugin_api_info( $plugin_slug, $plugin_name ) { // Include required WordPress files with error handling try { if ( ! function_exists( 'plugins_api' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; } if ( ! class_exists( 'WP_Ajax_Upgrader_Skin' ) ) { require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php'; } } catch ( Exception $e ) { astra_sites_error_log( sprintf( 'Failed to load required WordPress files for %s: %s', $plugin_slug, $e->getMessage() ) ); return new WP_Error( 'files_load_failed', sprintf( __( 'Failed to load required WordPress files: %s', 'astra-sites' ), $e->getMessage() ), array( 'plugin_slug' => $plugin_slug ) ); } // Get plugin information from WordPress.org API with timeout and retry logic $api_attempts = 0; $max_api_attempts = 2; $api = null; while ( $api_attempts <= $max_api_attempts && ( is_null( $api ) || is_wp_error( $api ) ) ) { $api_attempts++; try { astra_sites_error_log( sprintf( 'API attempt %d for plugin %s', $api_attempts, $plugin_slug ) ); $api = plugins_api( 'plugin_information', array( 'slug' => $plugin_slug, 'fields' => array( 'short_description' => false, 'sections' => false, 'requires' => false, 'rating' => false, 'ratings' => false, 'downloaded' => false, 'last_updated' => false, 'added' => false, 'tags' => false, 'compatibility' => false, 'homepage' => false, 'donate_link' => false, ), ) ); // Break if successful if ( ! is_wp_error( $api ) && ! empty( $api->download_link ) ) { break; } // Wait before retry (except on last attempt) if ( $api_attempts < $max_api_attempts ) { sleep( 1 ); } } catch ( Exception $e ) { astra_sites_error_log( sprintf( 'API exception on attempt %d for %s: %s', $api_attempts, $plugin_slug, $e->getMessage() ) ); if ( $api_attempts >= $max_api_attempts ) { return new WP_Error( 'api_exception', sprintf( __( 'Plugin API request failed after %d attempts: %s', 'astra-sites' ), $max_api_attempts, $e->getMessage() ), array( 'plugin_slug' => $plugin_slug ) ); } } } if ( ! $api ) { astra_sites_error_log( sprintf( 'API failed to return data for plugin %s', $plugin_slug ) ); return new WP_Error( 'plugin_api_failed', sprintf( __( 'Could not retrieve plugin information for %s', 'astra-sites' ), $plugin_name ) ); } // Handle API errors if ( is_wp_error( $api ) && $api instanceof WP_Error ) { $api_error_message = $api->get_error_message(); $api_error_code = $api->get_error_code(); astra_sites_error_log( sprintf( 'API error for plugin %s: %s (%s)', $plugin_slug, $api_error_message, $api_error_code ) ); // Provide specific error messages based on API error codes switch ( $api_error_code ) { case 'plugins_api_failed': $user_message = sprintf( __( 'Could not connect to WordPress.org plugin repository for %s. Please check your internet connection and try again.', 'astra-sites' ), $plugin_name ); break; case 'plugin_not_found': $user_message = sprintf( __( 'Plugin %s was not found in the WordPress.org repository. Please verify the plugin name and try again.', 'astra-sites' ), $plugin_name ); break; default: $user_message = sprintf( __( 'Could not retrieve plugin information for %s from WordPress.org. Error: %s', 'astra-sites' ), $plugin_name, $api_error_message ); break; } return new WP_Error( 'plugin_api_failed', $user_message, array( 'plugin_slug' => $plugin_slug, 'api_error' => $api_error_message, 'api_error_code' => $api_error_code, 'attempts_made' => $api_attempts, ) ); } // Validate API response if ( ! is_object( $api ) ) { astra_sites_error_log( sprintf( 'Invalid API response for plugin %s', $plugin_slug ) ); return new WP_Error( 'invalid_api_response', sprintf( __( 'Received invalid response from WordPress.org for plugin %s.', 'astra-sites' ), $plugin_name ), array( 'plugin_slug' => $plugin_slug ) ); } // Check for download URL if ( empty( $api->download_link ) ) { astra_sites_error_log( sprintf( 'No download URL found for plugin %s', $plugin_slug ) ); return new WP_Error( 'no_download_url', sprintf( __( 'Could not find download URL for %s. The plugin may not be available in WordPress.org repository or may be a premium plugin.', 'astra-sites' ), $plugin_name ), array( 'plugin_slug' => $plugin_slug, 'api_response' => $api, ) ); } return $api; } /** * Execute the actual plugin installation * * @param object $api Plugin API object. * @param string $plugin_slug Plugin slug. * @param string $plugin_name Plugin name. * @return true|WP_Error True on success, WP_Error on failure. */ private function execute_plugin_installation( $api, $plugin_slug, $plugin_name ) { // Validate WordPress and PHP requirements try { $this->validate_plugin_requirements( $api, $plugin_name ); } catch ( Exception $e ) { astra_sites_error_log( sprintf( 'Requirements not met for plugin %s: %s', $plugin_slug, $e->getMessage() ) ); return new WP_Error( 'requirements_not_met', $e->getMessage(), array( 'plugin_slug' => $plugin_slug, 'requirements' => array( 'requires' => isset( $api->requires ) ? $api->requires : '', 'requires_php' => isset( $api->requires_php ) ? $api->requires_php : '', ), ) ); } // Check available disk space try { $this->check_disk_space( $plugin_name ); } catch ( Exception $e ) { astra_sites_error_log( sprintf( 'Insufficient disk space for plugin %s: %s', $plugin_slug, $e->getMessage() ) ); return new WP_Error( 'insufficient_disk_space', $e->getMessage(), array( 'plugin_slug' => $plugin_slug ) ); } // Set up the Plugin_Upgrader with enhanced error handling try { $upgrader_skin = new WP_Ajax_Upgrader_Skin(); $upgrader = new Plugin_Upgrader( $upgrader_skin ); } catch ( Exception $e ) { astra_sites_error_log( sprintf( 'Failed to initialize upgrader for plugin %s: %s', $plugin_slug, $e->getMessage() ) ); return new WP_Error( 'upgrader_init_failed', sprintf( __( 'Failed to initialize plugin installer for %s: %s', 'astra-sites' ), $plugin_name, $e->getMessage() ), array( 'plugin_slug' => $plugin_slug ) ); } // Perform the actual installation with detailed error handling // @phpstan-ignore-next-line astra_sites_error_log( sprintf( 'Starting plugin installation for %s from %s', $plugin_slug, $api->download_link ) ); $result = null; try { // @phpstan-ignore-next-line $result = $upgrader->install( $api->download_link ); } catch ( Exception $e ) { astra_sites_error_log( sprintf( 'Installation exception for plugin %s: %s', $plugin_slug, $e->getMessage() ) ); return new WP_Error( 'installation_exception', sprintf( __( 'Plugin %s installation failed with exception: %s', 'astra-sites' ), $plugin_name, $e->getMessage() ), array( 'plugin_slug' => $plugin_slug, 'exception_message' => $e->getMessage(), ) ); } // Handle installation result errors if ( is_wp_error( $result ) ) { $error_message = $result->get_error_message(); $error_code = $result->get_error_code(); $error_data = $result->get_error_data(); astra_sites_error_log( sprintf( 'Installation error for plugin %s: %s (%s)', $plugin_slug, $error_message, $error_code ) ); // Enhanced error messages based on common installation issues $enhanced_message = $this->get_enhanced_error_message( $error_code, $error_message, $plugin_name ); return new WP_Error( 'installation_failed', $enhanced_message, array( 'plugin_slug' => $plugin_slug, 'original_error' => $error_message, 'error_code' => $error_code, 'error_data' => $error_data, 'troubleshooting_tips' => $this->get_troubleshooting_tips( $error_code ), ) ); } // Check if installation was unsuccessful if ( ! $result ) { astra_sites_error_log( sprintf( 'Installation returned false for plugin %s', $plugin_slug ) ); return new WP_Error( 'installation_failed', sprintf( __( 'Plugin %s installation failed: Unknown error occurred during installation. Please try again or install the plugin manually.', 'astra-sites' ), $plugin_name ), array( 'plugin_slug' => $plugin_slug, 'troubleshooting_tips' => array( __( 'Try refreshing the page and attempting the installation again.', 'astra-sites' ), __( 'Check if you have sufficient permissions to install plugins.', 'astra-sites' ), __( 'Verify that your WordPress installation has write permissions to the plugins directory.', 'astra-sites' ), ), ) ); } // Verify installation by checking if plugin files exist try { $this->verify_plugin_installation( $plugin_slug, $plugin_name ); } catch ( Exception $e ) { astra_sites_error_log( sprintf( 'Installation verification failed for plugin %s: %s', $plugin_slug, $e->getMessage() ) ); return new WP_Error( 'installation_verification_failed', $e->getMessage(), array( 'plugin_slug' => $plugin_slug ) ); } return true; } /** * Validate plugin requirements against current WordPress and PHP versions * * @param object $api Plugin API response object. * @param string $plugin_name Plugin name. * @throws Exception If requirements are not met. * @return void */ private function validate_plugin_requirements( $api, $plugin_name ) { global $wp_version; // Check WordPress version requirement if ( ! empty( $api->requires ) ) { if ( version_compare( $wp_version, $api->requires, '<' ) ) { throw new Exception( sprintf( __( 'Plugin %s requires WordPress version %s or higher. You are currently running version %s. Please update WordPress first.', 'astra-sites' ), $plugin_name, $api->requires, $wp_version ) ); } } // Check PHP version requirement if ( ! empty( $api->requires_php ) ) { if ( version_compare( PHP_VERSION, $api->requires_php, '<' ) ) { throw new Exception( sprintf( __( 'Plugin %s requires PHP version %s or higher. You are currently running version %s. Please contact your hosting provider to upgrade PHP.', 'astra-sites' ), $plugin_name, $api->requires_php, PHP_VERSION ) ); } } } /** * Check available disk space before installation * * @param string $plugin_name Plugin name. * @throws Exception If insufficient disk space. * @return void */ private function check_disk_space( $plugin_name ) { $wp_content_dir = WP_CONTENT_DIR; // Check if we can get disk space information if ( function_exists( 'disk_free_space' ) && is_dir( $wp_content_dir ) ) { $free_bytes = disk_free_space( $wp_content_dir ); $required_bytes = 50 * 1024 * 1024; // 50MB minimum requirement if ( $free_bytes !== false && $free_bytes < $required_bytes ) { throw new Exception( sprintf( __( 'Insufficient disk space to install %s. At least 50MB of free space is required. Please free up some disk space and try again.', 'astra-sites' ), $plugin_name ) ); } } } /** * Verify plugin installation by checking if plugin files exist * * @param string $plugin_slug Plugin slug. * @param string $plugin_name Plugin name. * @throws Exception If plugin verification fails. * @return void */ private function verify_plugin_installation( $plugin_slug, $plugin_name ) { // Clear plugin cache to ensure fresh data wp_clean_plugins_cache(); // Check if plugin directory exists $plugin_dir = WP_PLUGIN_DIR . '/' . $plugin_slug; if ( ! is_dir( $plugin_dir ) ) { throw new Exception( sprintf( __( 'Plugin %s installation verification failed: Plugin directory not found after installation. The installation may have been incomplete.', 'astra-sites' ), $plugin_name ) ); } // Try to find the main plugin file $plugin_file = $this->get_plugin_file( $plugin_slug ); if ( ! $plugin_file ) { throw new Exception( sprintf( __( 'Plugin %s installation verification failed: Could not locate main plugin file after installation.', 'astra-sites' ), $plugin_name ) ); } // Check if the plugin file is readable $full_plugin_path = WP_PLUGIN_DIR . '/' . $plugin_file; if ( ! is_readable( $full_plugin_path ) ) { throw new Exception( sprintf( __( 'Plugin %s installation verification failed: Plugin file exists but is not readable. Please check file permissions.', 'astra-sites' ), $plugin_name ) ); } } /** * Get enhanced error message based on error code * * @param string|int $error_code Error code. * @param string $error_message Original error message. * @param string $plugin_name Plugin name. * @return string Enhanced error message. */ private function get_enhanced_error_message( $error_code, $error_message, $plugin_name ) { $error_code = (string) $error_code; switch ( $error_code ) { case 'folder_exists': return sprintf( __( 'Plugin %s installation failed: Plugin folder already exists. This usually means the plugin is already installed. Try refreshing the plugins page to see if it appears.', 'astra-sites' ), $plugin_name ); case 'no_credentials': case 'request_filesystem_credentials': return sprintf( __( 'Plugin %s installation failed: WordPress does not have permission to write files. Please check that your web server has write permissions to the plugins directory (/wp-content/plugins/), or contact your hosting provider for assistance.', 'astra-sites' ), $plugin_name ); case 'fs_unavailable': case 'fs_error': return sprintf( __( 'Plugin %s installation failed: Filesystem is not accessible. This is usually a server configuration issue. Please contact your hosting provider to resolve filesystem access problems.', 'astra-sites' ), $plugin_name ); case 'download_failed': case 'http_request_failed': return sprintf( __( 'Plugin %s installation failed: Could not download plugin file from WordPress.org. Please check your internet connection and try again. If the problem persists, your server may be blocking outbound connections.', 'astra-sites' ), $plugin_name ); case 'incompatible_archive': case 'empty_archive': return sprintf( __( 'Plugin %s installation failed: The downloaded plugin file appears to be corrupted or invalid. Please try the installation again.', 'astra-sites' ), $plugin_name ); case 'mkdir_failed': return sprintf( __( 'Plugin %s installation failed: Could not create plugin directory. Please check that your web server has write permissions to the plugins directory.', 'astra-sites' ), $plugin_name ); case 'copy_failed': return sprintf( __( 'Plugin %s installation failed: Could not copy plugin files. This may be due to insufficient disk space or file permission issues.', 'astra-sites' ), $plugin_name ); default: return sprintf( __( 'Plugin %s installation failed: %s. Please try again or install the plugin manually from the WordPress admin area.', 'astra-sites' ), $plugin_name, $error_message ); } } /** * Get troubleshooting tips based on error code * * @param string|int $error_code Error code. * @return array<int, string> Array of troubleshooting tips. */ private function get_troubleshooting_tips( $error_code ) { $common_tips = array( __( 'Try refreshing the page and attempting the installation again.', 'astra-sites' ), __( 'If the problem persists, try installing the plugin manually from WordPress admin > Plugins > Add New.', 'astra-sites' ), ); switch ( $error_code ) { case 'folder_exists': return array_merge( array( __( 'Check if the plugin is already installed by going to Plugins > Installed Plugins.', 'astra-sites' ), __( 'If the plugin appears but is not working, try deactivating and reactivating it.', 'astra-sites' ), ), $common_tips ); case 'no_credentials': case 'request_filesystem_credentials': case 'fs_unavailable': case 'fs_error': case 'mkdir_failed': case 'copy_failed': return array_merge( array( __( 'Contact your hosting provider to check file permissions for the /wp-content/plugins/ directory.', 'astra-sites' ), __( 'Ensure your web server has write permissions to the WordPress installation directory.', 'astra-sites' ), __( 'Check if there is sufficient disk space available on your server.', 'astra-sites' ), ), $common_tips ); case 'download_failed': case 'http_request_failed': return array_merge( array( __( 'Check your internet connection and try again.', 'astra-sites' ), __( 'Contact your hosting provider if your server is blocking outbound connections to WordPress.org.', 'astra-sites' ), __( 'Try installing the plugin at a different time when server load might be lower.', 'astra-sites' ), ), $common_tips ); case 'incompatible_archive': case 'empty_archive': return array_merge( array( __( 'The plugin download may have been corrupted. Try the installation again.', 'astra-sites' ), __( 'Clear your browser cache and try again.', 'astra-sites' ), ), $common_tips ); default: return array_merge( array( __( 'Check the WordPress error logs for more detailed information about the failure.', 'astra-sites' ), __( 'Contact your hosting provider if you continue to experience issues.', 'astra-sites' ), ), $common_tips ); } } } endif; // Initialize the class Astra_Sites_Install_Plugin::get_instance(); classes/class-astra-sites-zipwp-helper.php 0000644 00000015234 15104367571 0014716 0 ustar 00 <?php /** * ZipWP Helper. * * @package {{package}} * @since 4.0.0 */ /** * Importer Helper * * @since 4.0.0 */ class Astra_Sites_ZipWP_Helper { /** * Get Saved Token. * * @since 4.0.0 * @return string */ public static function get_token() { $token_details = get_option( 'zip_ai_settings', array( 'auth_token' => '', 'zip_token' => '', 'email' => '' ) ); return isset( $token_details['zip_token'] ) ? self::decrypt( $token_details['zip_token'] ) : ''; } /** * Get Saved Auth Token. * * @since 4.1.0 * @return string */ public static function get_auth_token() { $token_details = get_option( 'zip_ai_settings', array( 'auth_token' => '', 'zip_token' => '', 'email' => '' ) ); return isset( $token_details['auth_token'] ) ? self::decrypt( $token_details['auth_token'] ) : ''; } /** * Get Saved ZipWP user email. * * @since 4.0.0 * @return string */ public static function get_zip_user_email() { $token_details = get_option( 'zip_ai_settings', array( 'auth_token' => '', 'zip_token' => '', 'email' => '' ) ); return isset( $token_details['email'] ) ? $token_details['email'] : ''; } /** * Get Saved settings. * * @since 4.0.0 * @return array<string, string> */ public static function get_setting() { return get_option( 'zip_ai_settings', array( 'auth_token' => '', 'zip_token' => '', 'email' => '' ) ); } /** * Encrypt data using base64. * * @param string $input The input string which needs to be encrypted. * @since 4.0.0 * @return string The encrypted string. */ public static function encrypt( $input ) { // If the input is empty or not a string, then abandon ship. if ( empty( $input ) || ! is_string( $input ) ) { return ''; } // Encrypt the input and return it. $base_64 = base64_encode( $input ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode $encode = rtrim( $base_64, '=' ); return $encode; } /** * Decrypt data using base64. * * @param string $input The input string which needs to be decrypted. * @since 4.0.0 * @return string The decrypted string. */ public static function decrypt( $input ) { // If the input is empty or not a string, then abandon ship. if ( empty( $input ) || ! is_string( $input ) ) { return ''; } // Decrypt the input and return it. $base_64 = $input . str_repeat( '=', strlen( $input ) % 4 ); $decode = base64_decode( $base_64 ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode return $decode; } /** * Get Business details. * * @since 4.0.0 * @param string $key options name. * @return mixed|array<string, mixed> Array for business details or single detail in a string. */ public static function get_business_details( $key = '' ) { $details = get_option( 'zipwp_user_business_details', array( 'business_name' => '', 'business_address' => '', 'business_phone' => '', 'business_email' => '', 'business_category' => '', 'business_description' => '', 'templates' => array(), 'language' => 'en', 'images' => array(), 'image_keyword' => array(), 'social_profiles' => array() ) ); $details = array( 'business_name' => ( ! empty( $details['business_name'] ) ) ? $details['business_name'] : '', 'business_address' => ( ! empty( $details['business_address'] ) ) ? $details['business_address'] : '', 'business_phone' => ( ! empty( $details['business_phone'] ) ) ? $details['business_phone'] : '', 'business_email' => ( ! empty( $details['business_email'] ) ) ? $details['business_email'] : '', 'business_category' => ( ! empty( $details['business_category'] ) ) ? $details['business_category'] : '', 'business_description' => ( ! empty( $details['business_description'] ) ) ? $details['business_description'] : '', 'templates' => ( ! empty( $details['templates'] ) ) ? $details['templates'] : array(), 'language' => ( ! empty( $details['language'] ) ) ? $details['language'] : 'en', 'images' => ( ! empty( $details['images'] ) ) ? $details['images'] : array(), 'social_profiles' => ( ! empty( $details['social_profiles'] ) ) ? $details['social_profiles'] : array(), 'image_keyword' => ( ! empty( $details['image_keyword'] ) ) ? $details['image_keyword'] : array(), ); if ( ! empty( $key ) ) { return isset( $details[ $key ] ) ? $details[ $key ] : array(); } return $details; } /** * Download image from URL. * * @param array<string> $image Image data. * @return int|\WP_Error Image ID or WP_Error. * @since {{since}} */ public static function download_image( $image ) { $image_url = $image['url']; $id = $image['id']; $downloaded_ids = get_option( 'ast_sites_downloaded_images', array() ); $downloaded_ids = ( is_array( $downloaded_ids ) ) ? $downloaded_ids : array(); if ( array_key_exists( $id, $downloaded_ids ) ) { return $downloaded_ids[ $id ]; } // Check if image is uploaded/downloaded already. If yes the update meta and mark it as downloaded. $site_domain = parse_url( get_home_url(), PHP_URL_HOST ); if ( strpos( $image_url, strval( $site_domain ) ) !== false ) { $downloaded_ids[ $id ] = $id; // Add our meta data for uploaded image. if( '1' !== get_post_meta( intval( $downloaded_ids[ $id ] ), '_astra_sites_imported_post', true ) ){ update_post_meta( intval( $downloaded_ids[ $id ] ), '_astra_sites_imported_post', true ); } update_option( 'ast_sites_downloaded_images', $downloaded_ids ); return intval( $downloaded_ids[ $id ] ); } // Use parse_url to get the path component of the URL. $path = wp_parse_url( $image_url, PHP_URL_PATH ); if ( empty( $path ) ) { return new \WP_Error( 'parse_url', 'Unable to parse URL' ); } // Using $id to create image name instead of $path. $image_name = 'zipwp-image-' . sanitize_title( $id ); // Use pathinfo to get the file name without the extension. $image_extension = pathinfo( $image_name, PATHINFO_EXTENSION ); // If the extension is empty, default to jpg. Set image_name with the extension. if ( empty( $image_extension ) ) { $image_extension = 'jpeg'; $image_name = $image_name . '.' . $image_extension; } $description = $image['description'] ?? ''; Astra_Sites_Importer_Log::add( 'Downloading Image as - ' . $image_name ); $new_attachment_id = Astra_Sites::get_instance()->create_image_from_url( $image_url, $image_name, $id, $description ); //Mark image downloaded. $downloaded_ids[ $id ] = $new_attachment_id; update_option( 'ast_sites_downloaded_images', $downloaded_ids ); return $new_attachment_id; } } classes/class-astra-sites-reporting.php 0000644 00000014247 15104367571 0014304 0 ustar 00 <?php /** * Reporting error * * @since 3.1.4 * @package Astra Sites */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Reporting error */ class Astra_Sites_Reporting { /** * Instance * * @since 4.0.0 * @access private * @var self Class object. */ private static $instance = null; /** * Initiator * * @since 4.0.0 * @return self */ public static function get_instance() { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } /** * Constructor * * @since 3.1.4 */ public function __construct() { add_action( 'st_before_start_import_process', array( $this, 'schedule_reporting_event' ) ); add_action( 'generate_analytics_lead', array( $this, 'send_analytics_lead' ) ); } /** * Schedule the reporting of Error. * * @since 3.1.4 * @return void */ public function schedule_reporting_event() { $has_sent_error_report = get_option( 'astra_sites_has_sent_error_report', 'no' ); if ( 'no' === $has_sent_error_report ) { // Schedule and event in next 3mins to send error report. wp_schedule_single_event( time() + 180, 'generate_analytics_lead' ); update_option( 'astra_sites_has_sent_error_report', 'yes' ); } } /** * Send Error. * * @since 3.1.4 * @return void */ public function send_analytics_lead() { $cached_errors = get_option( 'astra_sites_cached_import_error', false ); if ( false === $cached_errors ) { return; } $id = ( isset( $cached_errors['id'] ) ) ? $cached_errors['id'] : 0; if ( $id === 0 ) { return; } $data = json_decode( $cached_errors['err'] ); $report_data = array( 'id' => $id, 'import_attempts' => isset( $data->tryAgainCount ) ? absint( $data->tryAgainCount ) : 0, 'import_status' => 'false', 'exit_intend' => 'true', 'type' => isset( $cached_errors['type'] ) ? sanitize_text_field( $cached_errors['type'] ) : 'astra-sites', 'page_builder' => isset( $cached_errors['page_builder'] ) ? sanitize_text_field( $cached_errors['page_builder'] ) : '', 'template_type' => isset( $cached_errors['template_type'] ) ? sanitize_text_field( $cached_errors['template_type'] ) : '', 'failure_reason' => is_object( $data ) && isset( $data->primaryText ) ? sanitize_text_field( $data->primaryText ) : 'unknown', 'secondary_text' => is_object( $data ) && isset( $data->secondaryText ) ? sanitize_text_field( $data->secondaryText ) : '', 'error_text' => is_object( $data ) && isset( $data->errorText ) ? sanitize_text_field( $data->errorText ) : '', ); $this->report( $report_data ); update_option( 'astra_sites_has_sent_error_report', 'no' ); delete_option( 'astra_sites_cached_import_error' ); } /** * Report Error. * * @param array<string, mixed> $data Error data. * @since 3.1.4 * * @return array<string, mixed> */ public function report( $data ) { $id = isset( $data['id'] ) ? $data['id'] : 0; $import_attempts = isset( $data['import_attempts'] ) ? absint( $data['import_attempts'] ) : 0; $import_status = isset( $data['import_status'] ) ? sanitize_text_field( $data['import_status'] ) : 'true'; $type = isset( $data['type'] ) ? sanitize_text_field( $data['type'] ) : 'astra-sites'; $page_builder = isset( $data['page_builder'] ) ? sanitize_text_field( $data['page_builder'] ) : 'gutenberg'; $exit_intend = isset( $data['exit_intend'] ) ? sanitize_text_field( $data['exit_intend'] ) : 'false'; $user_agent_string = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ) : ''; $template_type = isset( $data['template_type'] ) ? sanitize_text_field( $data['template_type'] ) : ''; $failure_reason = isset( $data['failure_reason'] ) ? sanitize_text_field( $data['failure_reason'] ) : ''; $secondary_text = isset( $data['secondary_text'] ) ? sanitize_text_field( $data['secondary_text'] ) : ''; $error_text = isset( $data['error_text'] ) ? sanitize_text_field( $data['error_text'] ) : ''; // If secondary text is not empty, append it to failure reason. if ( ! empty( $secondary_text ) && is_string( $secondary_text ) ) { $failure_reason .= ' (' . $secondary_text . ')'; } $api_args = array( 'timeout' => 60, 'blocking' => true, 'body' => array( 'url' => esc_url( site_url() ), 'import_status' => $import_status, 'id' => $id, 'import_attempts' => $import_attempts, 'version' => ASTRA_SITES_VER, 'type' => $type, 'builder' => $page_builder, 'user_agent' => $user_agent_string, 'exit_intend' => $exit_intend, 'import_event_data' => array( 'plugin_type' => defined( 'ASTRA_PRO_SITES_VER' ) ? 'premium' : 'free', 'template_type' => $template_type, 'failure_reason' => $failure_reason, 'error_text' => $error_text, ), ), ); $request = wp_safe_remote_post( Astra_Sites::get_instance()->import_analytics_url, $api_args ); if ( is_wp_error( $request ) ) { return array( 'status' => false, 'data' => $request, ); } $code = (int) wp_remote_retrieve_response_code( $request ); $data = json_decode( wp_remote_retrieve_body( $request ), true ); if ( 200 === $code ) { return array( 'status' => true, 'data' => $data, ); } return array( 'status' => false, 'data' => $data, ); } } Astra_Sites_Reporting::get_instance(); classes/class-astra-sites-replace-images.php 0000644 00000103374 15104367571 0015151 0 ustar 00 <?php /** * Replace Images * * @since 3.1.4 * @package Astra Sites */ if ( ! defined( 'ABSPATH' ) ) { exit; } use AiBuilder\Inc\Traits\Helper; /** * Replace Images */ class Astra_Sites_Replace_Images { /** * Member Variable * * @var instance */ private static $instance; /** * Image index * * @since 4.1.0 * @var array<string,int> */ public static $image_index = 0; /** * Old Images ids * * @var array<int,int> * @since 4.1.0 */ public static $old_image_urls = array(); /** * Reusable block tracking. * * @var array<int,int> */ public static $reusable_blocks = array(); /** * Filtered images. * * @var array<string, array<string, string>> */ public static $filtered_images = array(); /** * Initiator * * @since 3.1.4 */ public static function get_instance() { if ( ! isset( self::$instance ) ) { self::$instance = new self(); } return self::$instance; } /** * Constructor * * @since 3.1.4 */ public function __construct() { add_action( 'wp_ajax_astra-sites-download-image', array( $this, 'download_selected_image' ) ); } /** * Download Images * * @since 4.1.0 * @return void */ public function download_selected_image( ){ check_ajax_referer( 'astra-sites', '_ajax_nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'data' => 'You do not have permission to do this action.', 'status' => false, ) ); } $index = isset( $_POST['index'] ) ? sanitize_text_field( wp_unslash( $_POST['index'] ) ) : ''; $images = Astra_Sites_ZipWP_Helper::get_business_details('images'); if ( empty( $images ) ) { wp_send_json_success( array( 'data' => 'No images selected to download!', 'status' => true, ) ); } $image = $images[ $index ]; if( empty( $image ) ){ wp_send_json_success( array( 'data' => 'No image to download!', 'status' => true, ) ); } $prepare_image = array( 'id' => $image['id'], 'url' => $image['url'], 'description' => isset( $image['description'] ) ? $image['description'] : '', ); Astra_Sites_Importer_Log::add( 'Downloading Image ' . $image['url'] ); $id = Astra_Sites_ZipWP_Helper::download_image( $prepare_image ); Astra_Sites_Importer_Log::add( 'Downloaded Image attachment id: ' . $id ); wp_send_json_success( array( 'data' => 'Image downloaded successfully!', 'status' => true, ) ); } /** * Replace images in pages. * @since 4.1.0 * * @retuen void */ public function replace_images() { $this->replace_in_pages(); $this->replace_in_post(); // Replace customizer content. if ( function_exists( 'astra_update_option' ) && function_exists( 'astra_get_option' ) ) { $this->replace_in_customizer(); } $this->cleanup(); } /** * Replace images in post. * @since 4.1.0 * * @retuen void */ public function replace_in_post(){ $posts = $this->get_pages( 'post' ); foreach ( $posts as $key => $post ) { if ( ! is_object( $post ) ) { continue; } $this->parse_featured_image( $post ); } } /** Parses images and other content in the Spectra Info Box block. * * @since {{since}} * @param \WP_Post $post Post. * @return void */ public function parse_featured_image( $post ) { $image = $this->get_image( self::$image_index ); if ( empty( $image ) || ! is_array( $image ) || is_bool( $image ) ) { return; } $image = Astra_Sites_ZipWP_Helper::download_image( $image ); if ( is_wp_error( $image ) ) { Astra_Sites_Importer_Log::add( 'Replacing Image problem : Warning: ' . wp_json_encode( $image ), 'warning' ); return; } $attachment = wp_prepare_attachment_for_js( absint( $image ) ); if ( ! is_array( $attachment ) ) { return; } Astra_Sites_Importer_Log::add( 'Replacing thumbnail Image to ' . $attachment['url'] . '" with index "' . self::$image_index . '"' ); set_post_thumbnail( $post, $attachment['id'] ); $this->increment_image_index(); } /** * Cleanup the old images. * * @return void * @since 4.1.0 */ public function cleanup() { $old_image_urls = self::$old_image_urls; Astra_Sites_Importer_Log::add( 'Cleaning up old images - ' . print_r( $old_image_urls, true ) ); if ( ! empty( $old_image_urls ) ) { $guid_list = implode("', '", $old_image_urls); global $wpdb; $query = "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND guid IN ('$guid_list')"; $old_image_ids = $wpdb->get_results($query); foreach ( $old_image_ids as $old_image_id ) { wp_delete_attachment( $old_image_id->ID, true ); } } delete_option( 'ast_sites_downloaded_images' ); } /** * Replace images in customizer. * * @since 4.1.0 */ public function replace_in_customizer() { $footer_image_obj = astra_get_option( 'footer-bg-obj-responsive' ); if ( isset( $footer_image_obj ) && ! empty( $footer_image_obj ) ) { $footer_image_obj = $this->get_updated_astra_option( $footer_image_obj ); astra_update_option( 'footer-bg-obj-responsive', $footer_image_obj ); } $header_image_obj = astra_get_option( 'header-bg-obj-responsive' ); if ( isset( $header_image_obj ) && ! empty( $header_image_obj ) ) { $header_image_obj = $this->get_updated_astra_option( $header_image_obj ); astra_update_option( 'header-bg-obj-responsive', $header_image_obj ); } $blog_archieve_image_obj = astra_get_option( 'ast-dynamic-archive-post-banner-custom-bg' ); if ( isset( $blog_archieve_image_obj ) && ! empty( $blog_archieve_image_obj ) ) { $blog_archieve_image_obj = $this->get_updated_astra_option( $blog_archieve_image_obj ); astra_update_option( 'ast-dynamic-archive-post-banner-custom-bg', $blog_archieve_image_obj ); } $social_options = $this->get_options(); /** * Social Element Options */ $this->update_social_options( $social_options ); } /** * Update the Social options * * @param array $options Social Options. * @since {{since}} * @return void */ public function update_social_options( $options ) { if ( ! empty( $options ) ) { $social_profiles = Astra_Sites_ZipWP_Helper::get_business_details( 'social_profiles' ); $business_phone = Astra_Sites_ZipWP_Helper::get_business_details( 'business_phone' ); $business_email = Astra_Sites_ZipWP_Helper::get_business_details( 'business_email' ); foreach ( $options as $key => $name ) { $value = astra_get_option( $name ); $items = isset( $value['items'] ) ? $value['items'] : array(); $social_icons = array_map( function( $item ) { return $item['type']; }, $social_profiles ); $social_icons = array_merge( $social_icons, array( 'phone', 'email' ) ); if ( is_array( $items ) && ! empty( $items ) ) { foreach ( $items as $index => &$item ) { $cached_first_item = isset( $items[0] ) ? $items[0] : []; if ( ! in_array( $item['id'], $social_icons, true ) ) { unset( $items[ $index ] ); continue; } if ( $item['enabled'] && false !== strpos( $item['id'], 'phone' ) && '' !== $business_phone ) { $item['url'] = $business_phone ; Astra_Sites_Importer_Log::add( 'Replacing Social Icon - "' . $item['id'] . '" Phone value with "' . $business_phone . '"' ); } if ( $item['enabled'] && false !== strpos( $item['id'], 'email' ) && '' !== $business_email ) { $item['url'] = $business_email; Astra_Sites_Importer_Log::add( 'Replacing Social Icon - "' . $item['id'] . '" email value with "' . $business_email . '"' ); } if ( ! empty( $social_profiles ) ) { $id = $item['id']; $src = array_reduce( $social_profiles, function ( $carry, $element ) use ( $id ) { if ( ! $carry && $element['type'] === $id ) { $carry = $element; } return $carry; } ); if ( ! empty( $src ) ) { $item['url'] = $src['url']; Astra_Sites_Importer_Log::add( 'Replacing Social Icon - "' . $item['id'] . '" value with "' . $src['url'] . '"' ); } } } $yelp_google = [ 'yelp', 'google' ]; foreach ( $yelp_google as $yelp_google_item ) { if ( in_array( $yelp_google_item, $social_icons, true ) && ! empty( $cached_first_item ) ) { $new_inner_item = $cached_first_item; $new_inner_item['id'] = $yelp_google_item; $new_inner_item['icon'] = $yelp_google_item; $new_inner_item['label'] = ucfirst( $yelp_google_item ); $link = '#'; foreach ( $social_profiles as $social_icon ) { if ( $yelp_google_item === $social_icon['type'] ) { $link = $social_icon['url']; break; } } $new_inner_item['url'] = $link; $items[] = $new_inner_item; } } $value['items'] = array_values( $items ); astra_update_option( $name, $value ); } } } } /** * Gather all options eligible for replacement algorithm. * All elements placed in Header and Footer builder. * * @since {{since}} * @return array $options Options. */ public function get_options() { $zones = array( 'above', 'below', 'primary', 'popup' ); $header = astra_get_option( 'header-desktop-items', array() ); $header_mobile = astra_get_option( 'header-mobile-items', array() ); $footer = astra_get_option( 'footer-desktop-items', array() ); $social_options = array(); foreach ( $zones as $locations ) { // Header - Desktop Scanning for replacement text. if ( ! empty( $header[ $locations ] ) ) { foreach ( $header[ $locations ] as $location ) { if ( empty( $location ) ) { continue; } foreach ( $location as $loc ) { if ( false !== strpos( $loc, 'social-icons' ) ) { $social_options[] = 'header-' . $loc; } } } } // Header - Mobile Scanning for replacement text. if ( ! empty( $header_mobile[ $locations ] ) ) { foreach ( $header_mobile[ $locations ] as $location ) { if ( empty( $location ) ) { continue; } foreach ( $location as $loc ) { if ( false !== strpos( $loc, 'social-icons' ) ) { $social_options[] = 'header-' . $loc; } } } } // Footer Scanning for replacement text. if ( ! empty( $footer[ $locations ] ) ) { foreach ( $footer[ $locations ] as $location ) { if ( empty( $location ) ) { continue; } foreach ( $location as $loc ) { if ( false !== strpos( $loc, 'social-icons' ) ) { $social_options[] = 'footer-' . $loc; } } } } } return $social_options; } /** * Updating the header and footer background image. * * @since 4.1.0 * @param array<string,array<string,string>> $obj Reference of Block array. * @return array<string,array<string,int|string>> $obj Updated Block array. */ public function get_updated_astra_option( $obj ) { $image_id = ( isset( $obj['desktop']['background-media'] ) ) ? $obj['desktop']['background-media'] : 0; if ( 0 === $image_id ) { return $obj; } $image = $this->get_image( self::$image_index ); if ( empty( $image ) || ! is_array( $image ) || is_bool( $image ) ) { return $obj; } $image = Astra_Sites_ZipWP_Helper::download_image( $image ); if ( is_wp_error( $image ) ) { Astra_Sites_Importer_Log::add( 'Replacing Image problem : ' . $obj['desktop']['background-image'] . ' Warning: ' . wp_json_encode( $image ) ); return $obj; } $attachment = wp_prepare_attachment_for_js( absint( $image ) ); Astra_Sites_Importer_Log::add( 'Replacing Image : ' . $obj['desktop']['background-image'] . ' with: ' . $attachment['url'] ); $obj['desktop']['background-image'] = $attachment['url']; $obj['desktop']['background-media'] = $attachment['id']; $this->increment_image_index(); return $obj; } /** * Replace the content with AI generated data in all Pages. * * @since 4.1.0 * @return void */ public function replace_in_pages() { $pages = $this->get_pages(); foreach ( $pages as $key => $post ) { if ( ! is_object( $post ) ) { continue; } // Log the page title. Astra_Sites_Importer_Log::add( 'Building "' . $post->post_title . '" page with AI content' ); // Replaced content. $new_content = $this->parse_replace_images( $post->post_content ); // Update content. wp_update_post( array( 'ID' => $post->ID, 'post_content' => $new_content, ) ); Astra_Sites_Importer_Log::add( 'Replaced the images for Page ' . $post->post_title . '[ID:' . $post->ID . ']' ); } } /** * Parse the content for potential AI based content. * * @since 4.1.0 * @param string $content Post Content. * @return string $content Modified content. */ public function parse_replace_images( $content ) { $blocks = parse_blocks( $content ); // Get replaced blocks images. $content = serialize_blocks( $this->get_updated_blocks( $blocks ) ); return $this->replace_content_glitch( $content ); } /** * Update the Blocks with new mapping data. * * @since 4.1.0 * @param array<mixed> $blocks Array of Blocks. * @return array<mixed> $blocks Modified array of Blocks. */ public function get_updated_blocks( &$blocks ) { foreach ( $blocks as $i => &$block ) { if ( is_array( $block ) ) { if ( '' === $block['blockName'] ) { continue; } if ( 'core/block' === $block['blockName'] && isset( $block['attrs']['ref'] ) ) { $reusable_block_id = $block['attrs']['ref']; $reusable_block = get_post( $reusable_block_id ); if ( empty( $reusable_block ) || ! is_object( $reusable_block ) ) { continue; } Astra_Sites_Importer_Log::add( 'Reusable ID: ' . $reusable_block->ID ); if ( in_array( $reusable_block_id, self::$reusable_blocks, true ) ) { continue; } self::$reusable_blocks[] = $reusable_block_id; Astra_Sites_Importer_Log::add( 'Replacing content for Reusable ID: ' . $reusable_block->ID ); // Update content. wp_update_post( array( 'ID' => $reusable_block->ID, 'post_content' => $this->parse_replace_images( $reusable_block->post_content ), ) ); } /** Replace images if present in the block */ $this->replace_images_in_blocks( $block ); if ( ! empty( $block['innerBlocks'] ) ) { /** Find the last node of the nested blocks */ $this->get_updated_blocks( $block['innerBlocks'] ); } } } return $blocks; } /** * Parse social icon list. * * @since {{since}} * @param array<mixed> $block Block. * @return array<mixed> $block Block. */ public function parse_social_icons( $block ) { $social_profile = Astra_Sites_ZipWP_Helper::get_business_details( 'social_profiles' ); if ( empty( $social_profile ) ) { return $block; } $social_icons = array_map( function( $item ) { return $item['type']; }, $social_profile ); $social_icons = array_merge( $social_icons, array( 'phone' ) ); foreach ( $social_icons as $key => $social_icon ) { if ( 'linkedin' === $social_icon ) { $social_icons[ $key ] = 'linkedin-in'; break; } } $inner_blocks = $block['innerBlocks']; if ( is_array( $inner_blocks ) ) { $cached_first_item = isset( $block['innerBlocks'][0] ) ? $block['innerBlocks'][0] : []; $list_social_icons = array_map( function( $item ) { return isset( $item['attrs']['icon'] ) ? $item['attrs']['icon'] : ''; }, $inner_blocks ); if ( ! in_array( 'facebook', $list_social_icons, true ) ) { return $block; } foreach ( $inner_blocks as $index => &$inner_block ) { if ( 'uagb/icon-list-child' !== $inner_block['blockName'] ) { continue; } $icon = $inner_block['attrs']['icon']; if ( empty( $icon ) ) { continue; } if ( ! in_array( $icon, $social_icons, true ) ) { unset( $block['innerBlocks'][ $index ] ); } } $yelp_google = [ 'yelp' => '<svg xmlns="https://www.w3.org/2000/svg" viewBox="0 0 384 512"><path d="M42.9 240.3l99.62 48.61c19.2 9.4 16.2 37.51-4.5 42.71L30.5 358.5a22.79 22.79 0 0 1 -28.21-19.6 197.2 197.2 0 0 1 9-85.32 22.8 22.8 0 0 1 31.61-13.21zm44 239.3a199.4 199.4 0 0 0 79.42 32.11A22.78 22.78 0 0 0 192.9 490l3.9-110.8c.7-21.3-25.5-31.91-39.81-16.1l-74.21 82.4a22.82 22.82 0 0 0 4.09 34.09zm145.3-109.9l58.81 94a22.93 22.93 0 0 0 34 5.5 198.4 198.4 0 0 0 52.71-67.61A23 23 0 0 0 364.2 370l-105.4-34.26c-20.31-6.5-37.81 15.8-26.51 33.91zm148.3-132.2a197.4 197.4 0 0 0 -50.41-69.31 22.85 22.85 0 0 0 -34 4.4l-62 91.92c-11.9 17.7 4.7 40.61 25.2 34.71L366 268.6a23 23 0 0 0 14.61-31.21zM62.11 30.18a22.86 22.86 0 0 0 -9.9 32l104.1 180.4c11.7 20.2 42.61 11.9 42.61-11.4V22.88a22.67 22.67 0 0 0 -24.5-22.8 320.4 320.4 0 0 0 -112.3 30.1z"></path></svg>', 'google' => '<svg xmlns="https://www.w3.org/2000/svg" viewBox="0 0 488 512"><path d="M488 261.8C488 403.3 391.1 504 248 504 110.8 504 0 393.2 0 256S110.8 8 248 8c66.8 0 123 24.5 166.3 64.9l-67.5 64.9C258.5 52.6 94.3 116.6 94.3 256c0 86.5 69.1 156.6 153.7 156.6 98.2 0 135-70.4 140.8-106.9H248v-85.3h236.1c2.3 12.7 3.9 24.9 3.9 41.4z"></path></svg>', ]; foreach ( $yelp_google as $yelp_google_key => $yelp_google_item ) { if ( in_array( $yelp_google_key, $social_icons, true ) && ! empty( $cached_first_item ) ) { $new_inner_block = $cached_first_item; $new_inner_block['attrs']['icon'] = $yelp_google_key; $link = '#'; foreach ( $social_profile as $social_icon ) { if ( $yelp_google_key === $social_icon['type'] ) { $link = $social_icon['url']; break; } } $new_inner_block['attrs']['link'] = $link; $svg_pattern = '/<svg.*?>.*?<\/svg>/s'; // The 's' modifier allows the dot (.) to match newline characters. preg_match( $svg_pattern, $new_inner_block['innerHTML'], $matches ); if( is_array( $matches ) && ! empty( $matches ) ){ $new_inner_block['innerHTML'] = str_replace( $matches[0], $yelp_google_item, $new_inner_block['innerHTML'] ); } $href_pattern = '/href=".*?"/s'; // The 's' modifier allows the dot (.) to match newline characters. preg_match( $href_pattern, $new_inner_block['innerHTML'], $href_matches ); if ( ! empty( $href_matches ) ) { $new_inner_block['innerHTML'] = str_replace( $href_matches[0], 'href="' . $link . '"', $new_inner_block['innerHTML'] ); } foreach ( $new_inner_block['innerContent'] as $key => $inner_content ) { if ( empty( $inner_content ) ) { continue; } preg_match( $svg_pattern, $inner_content, $matches ); if ( ! empty( $matches ) ) { $new_inner_block['innerContent'][ $key ] = str_replace( $matches[0], $yelp_google_item, $new_inner_block['innerContent'][ $key ] ); } preg_match( $href_pattern, $inner_content, $href_matches ); if ( ! empty( $href_matches ) ) { $new_inner_block['innerContent'][ $key ] = str_replace( $href_matches[0], 'href="' . $link . '"', $new_inner_block['innerContent'][ $key ] ); } } array_push( $block['innerBlocks'], $new_inner_block ); $last_index = count( $block['innerContent'] ) - 1; array_splice( $block['innerContent'], $last_index, 0, '' ); $new_last_index = count( $block['innerContent'] ) - 1; array_splice( $block['innerContent'], $new_last_index, 0, [ null ] ); } } $block['innerBlocks'] = array_values( $block['innerBlocks'] ); } return $block; } /** * Replace the image in the block if present from the AI generated images. * * @since 4.1.0 * @param array<mixed> $block Reference of Block array. * @return void */ public function replace_images_in_blocks( &$block ) { switch ( $block['blockName'] ) { case 'uagb/container': $block = $this->parse_spectra_container( $block ); break; case 'uagb/image': $block = $this->parse_spectra_image( $block ); break; case 'uagb/image-gallery': $block = $this->parse_spectra_gallery( $block ); break; case 'uagb/info-box': $block = $this->parse_spectra_infobox( $block ); break; case 'uagb/google-map': $block = $this->parse_spectra_google_map( $block ); break; case 'uagb/forms': $block = $this->parse_spectra_form( $block ); break; case 'uagb/icon-list': $block = $this->parse_social_icons( $block ); } } /** * Get pages. * * @return array<int|\WP_Post> Array for pages. * @param string $type Post type. * @since 4.1.0 */ public static function get_pages( $type = 'page' ) { $query_args = array( 'post_type' => array( $type ), // Query performance optimization. 'fields' => array( 'ids', 'post_content', 'post_title' ), 'posts_per_page' => '10', 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'no_found_rows' => true, 'meta_query' => array( array( 'key' => '_astra_sites_imported_post', // Replace 'your_meta_key' with your actual meta key 'value' => '1', // Replace 'desired_meta_value' with the value you are querying 'compare' => '=', // Change the comparison operator if needed ), ), ); $query = new WP_Query( $query_args ); $desired_first_page_id = intval( get_option( 'page_on_front', 0 ) ); $pages = $query->posts ? $query->posts : []; $desired_page_index = false; if ( is_array( $pages ) && ! empty( $pages ) && ! empty( $desired_first_page_id ) ) { foreach ( $pages as $key => $page ) { if ( isset( $page->ID ) && $page->ID === $desired_first_page_id ) { $desired_page_index = $key; break; } } if ( false !== $desired_page_index ) { $desired_page = $pages[ $desired_page_index ]; unset( $pages[ $desired_page_index ] ); array_unshift( $pages, $desired_page ); } } return $pages; } /** * Parses Spectra form block. * * @since 4.1.0 * @param array<mixed> $block Block. * @return void */ public function parse_spectra_form( $block ) { $business_email = Astra_Sites_ZipWP_Helper::get_business_details( 'business_email' ); if ( ! empty( $business_email ) ) { $block['attrs']['afterSubmitToEmail'] = $business_email; } return $block; } /** * Parses Google Map for the Spectra Google Map block. * * @since 4.1.0 * @param array<mixed> $block Block. * @return array<mixed> $block Block. */ public function parse_spectra_google_map( $block ) { $address = Astra_Sites_ZipWP_Helper::get_business_details( 'business_address' ); if ( empty( $address ) ) { return $block; } $block['attrs']['address'] = $address; Astra_Sites_Importer_Log::add( 'Replacing Google Map to "' . $address ); return $block; } /** * Parses images and other content in the Spectra Container block. * * @since 4.1.0 * @param array<mixed> $block Block. * @return array<mixed> $block Block. */ public function parse_spectra_container( $block ) { if ( ! isset( $block['attrs']['backgroundImageDesktop'] ) || empty( $block['attrs']['backgroundImageDesktop'] ) || $this->is_skipable( $block['attrs']['backgroundImageDesktop']['url'] ) ) { return $block; } $image = $this->get_image( self::$image_index ); if ( empty( $image ) || ! is_array( $image ) || is_bool( $image ) ) { return $block; } $image = Astra_Sites_ZipWP_Helper::download_image( $image ); if ( is_wp_error( $image ) ) { Astra_Sites_Importer_Log::add( 'Replacing Image problem : ' . $block['attrs']['backgroundImageDesktop']['url'] . ' Warning: ' . wp_json_encode( $image ) ); return $block; } $attachment = wp_prepare_attachment_for_js( absint( $image ) ); if ( is_wp_error( $attachment ) || ! is_array( $attachment ) ) { return $block; } self::$old_image_urls[] = $block['attrs']['backgroundImageDesktop']['url']; Astra_Sites_Importer_Log::add( 'Replacing Image from ' . $block['attrs']['backgroundImageDesktop']['url'] . ' to "' . $attachment['url'] . '" for ' . $block['blockName'] . '" with index "' . self::$image_index . '"' ); $block['attrs']['backgroundImageDesktop'] = $attachment; $this->increment_image_index(); return $block; } /** * Parses images and other content in the Spectra Info Box block. * * @since 4.1.0 * @param array<mixed> $block Block. * @return array<mixed> $block Block. */ public function parse_spectra_infobox( $block ) { if ( ! isset( $block['attrs']['iconImage'] ) || empty( $block['attrs']['iconImage'] ) || $this->is_skipable( $block['attrs']['iconImage']['url'] ) ) { return $block; } $image = $this->get_image( self::$image_index ); if ( empty( $image ) || ! is_array( $image ) || is_bool( $image ) ) { return $block; } $image = Astra_Sites_ZipWP_Helper::download_image( $image ); if ( is_wp_error( $image ) ) { Astra_Sites_Importer_Log::add( 'Replacing Image problem : ' . $block['attrs']['iconImage']['url'] . ' Warning: ' . wp_json_encode( $image ) ); return $block; } $attachment = wp_prepare_attachment_for_js( absint( $image ) ); if ( is_wp_error( $attachment ) || ! is_array( $attachment ) ) { return $block; } self::$old_image_urls[] = $block['attrs']['iconImage']['url']; if ( ! empty( $block['attrs']['iconImage']['url'] ) ) { Astra_Sites_Importer_Log::add( 'Replacing Image from ' . $block['attrs']['iconImage']['url'] . ' to "' . $attachment['url'] . '" for ' . $block['blockName'] . '" with index "' . self::$image_index . '"' ); $block['innerHTML'] = str_replace( $block['attrs']['iconImage']['url'], $attachment['url'], $block['innerHTML'] ); } foreach ( $block['innerContent'] as $key => &$inner_content ) { if ( is_string( $block['innerContent'][ $key ] ) && '' === trim( $block['innerContent'][ $key ] ) ) { continue; } $block['innerContent'][ $key ] = str_replace( $block['attrs']['iconImage']['url'], $attachment['url'], $block['innerContent'][ $key ] ); } $block['attrs']['iconImage'] = $attachment; $this->increment_image_index(); return $block; } /** * Parses images and other content in the Spectra Image block. * * @since 4.1.0 * @param array<mixed> $block Block. * @return array<mixed> $block Block. */ public function parse_spectra_image( $block ) { if ( ! isset( $block['attrs']['url'] ) || $this->is_skipable( $block['attrs']['url'] ) ) { return $block; } $image = $this->get_image( self::$image_index ); if ( empty( $image ) || ! is_array( $image ) ) { return $block; } $image = Astra_Sites_ZipWP_Helper::download_image( $image ); if ( is_wp_error( $image ) ) { Astra_Sites_Importer_Log::add( 'Replacing Image problem : ' . $block['attrs']['url'] . ' Warning: ' . wp_json_encode( $image ) ); return $block; } $attachment = wp_prepare_attachment_for_js( absint( $image ) ); if ( is_wp_error( $attachment ) || ! is_array( $attachment ) ) { return $block; } self::$old_image_urls[] = $block['attrs']['url']; Astra_Sites_Importer_Log::add( 'Replacing Image from ' . $block['attrs']['url'] . ' to "' . $attachment['url'] . '" for ' . $block['blockName'] . '" with index "' . self::$image_index . '"' ); $block['innerHTML'] = str_replace( $block['attrs']['url'], $attachment['url'], $block['innerHTML'] ); $tablet_size_slug = ! empty( $block['attrs']['sizeSlugTablet'] ) ? $block['attrs']['sizeSlugTablet'] : ''; $mobile_size_slug = ! empty( $block['attrs']['sizeSlugMobile'] ) ? $block['attrs']['sizeSlugMobile'] : ''; $tablet_dest_url = ''; $mobile_dest_url = ''; if ( isset( $block['attrs']['urlTablet'] ) && ! empty( $block['attrs']['urlTablet'] ) && ! empty( $tablet_size_slug ) ) { $tablet_dest_url = isset( $attachment['sizes'][ $tablet_size_slug ]['url'] ) ? $attachment['sizes'][ $tablet_size_slug ]['url'] : $attachment['url']; $block['innerHTML'] = str_replace( $block['attrs']['urlTablet'], $tablet_dest_url, $block['innerHTML'] ); $block['attrs']['urlTablet'] = $tablet_dest_url; } if ( isset( $block['attrs']['urlMobile'] ) && ! empty( $block['attrs']['urlMobile'] ) && ! empty( $mobile_size_slug ) ) { $mobile_dest_url = isset( $attachment['sizes'][ $mobile_size_slug ]['url'] ) ? $attachment['sizes'][ $mobile_size_slug ]['url'] : $attachment['url']; $block['innerHTML'] = str_replace( $block['attrs']['urlMobile'], $mobile_dest_url, $block['innerHTML'] ); $block['attrs']['urlMobile'] = $mobile_dest_url; } $block['innerHTML'] = str_replace( 'uag-image-' . $block['attrs']['id'], 'uag-image-' . $attachment['id'], $block['innerHTML'] ); foreach ( $block['innerContent'] as $key => &$inner_content ) { if ( is_string( $block['innerContent'][ $key ] ) && '' === trim( $block['innerContent'][ $key ] ) ) { continue; } $block['innerContent'][ $key ] = str_replace( $block['attrs']['url'], $attachment['url'], $block['innerContent'][ $key ] ); if ( isset( $block['attrs']['urlTablet'] ) && ! empty( $block['attrs']['urlTablet'] ) ) { $block['innerContent'][ $key ] = str_replace( $block['attrs']['urlTablet'], $tablet_dest_url, $block['innerContent'][ $key ] ); } if ( isset( $block['attrs']['urlMobile'] ) && ! empty( $block['attrs']['urlMobile'] ) ) { $block['innerContent'][ $key ] = str_replace( $block['attrs']['urlMobile'], $mobile_dest_url, $block['innerContent'][ $key ] ); } $block['innerContent'][ $key ] = str_replace( 'uag-image-' . $block['attrs']['id'], 'uag-image-' . $attachment['id'], $block['innerContent'][ $key ] ); } $block['attrs']['url'] = $attachment['url']; $block['attrs']['id'] = $attachment['id']; $this->increment_image_index(); return $block; } /** * Parses images and other content in the Spectra Info Box block. * * @since 4.1.0 * @param array<mixed> $block Block. * @return array<mixed> $block Block. */ public function parse_spectra_gallery( $block ) { $images = $block['attrs']['mediaGallery']; $gallery_ids = []; foreach ( $images as $key => &$image ) { if ( ! isset( $image ) || empty( $image ) || $this->is_skipable( $image['url'] ) ) { continue; } $new_image = $this->get_image( self::$image_index ); if ( empty( $new_image ) || ! is_array( $new_image ) || is_bool( $new_image ) ) { continue; } $new_image = Astra_Sites_ZipWP_Helper::download_image( $new_image ); if ( is_wp_error( $new_image ) ) { Astra_Sites_Importer_Log::add( 'Replacing Image problem : ' . $image['url'] . ' Warning: ' . wp_json_encode( $new_image ) ); continue; } $attachment = wp_prepare_attachment_for_js( absint( $new_image ) ); if ( is_wp_error( $attachment ) || ! is_array( $attachment ) ) { continue; } $gallery_ids[] = $attachment['id']; self::$old_image_urls[] = $image['url']; Astra_Sites_Importer_Log::add( 'Replacing Image from ' . $image['url'] . ' to "' . $attachment['url'] . '" for ' . $block['blockName'] . '" with index "' . self::$image_index . '"' ); $image['url'] = ! empty( $attachment['url'] ) ? $attachment['url'] : $image['url']; $image['sizes'] = ! empty( $attachment['sizes'] ) ? $attachment['sizes'] : $image['sizes']; $image['mime'] = ! empty( $attachment['mime'] ) ? $attachment['mime'] : $image['mime']; $image['type'] = ! empty( $attachment['type'] ) ? $attachment['type'] : $image['type']; $image['subtype'] = ! empty( $attachment['subtype'] ) ? $attachment['subtype'] : $image['subtype']; $image['id'] = ! empty( $attachment['id'] ) ? $attachment['id'] : $image['id']; $image['alt'] = ! empty( $attachment['alt'] ) ? $attachment['alt'] : $image['alt']; $image['link'] = ! empty( $attachment['link'] ) ? $attachment['link'] : $image['link']; $this->increment_image_index(); } $block['attrs']['mediaGallery'] = $images; $block['attrs']['mediaIDs'] = $gallery_ids; return $block; } /** * Check if we need to skip the URL. * * @param string $url URL to check. * @return boolean * @since 4.1.0 */ public static function is_skipable( $url ) { if ( strpos( $url, 'skip' ) !== false ) { return true; } return false; } /** * Get Image for the specified index * * @param int $index Index of the image. * @return array|boolean Array of images or false. * @since 4.1.0 */ public function get_image( $index = 0 ) { $this->set_images(); Astra_Sites_Importer_Log::add( 'Fetching image with index ' . $index ); return ( isset( self::$filtered_images[ $index ] ) ) ? self::$filtered_images[ $index ] : false; } /** * Set Image as per oriantation * * @return void */ public function set_images() { if( empty( self::$filtered_images ) ){ $images = Astra_Sites_ZipWP_Helper::get_business_details('images'); if( ! empty( $images ) ){ foreach ( $images as $image ) { self::$filtered_images[] = $image; } } else { $placeholder_images = Helper::get_image_placeholders(); self::$filtered_images[] = $placeholder_images[0]; self::$filtered_images[] = $placeholder_images[1]; } } } /** * Increment Image index * * * @return void */ public function increment_image_index() { $this->set_images(); $new_index = self::$image_index + 1; if ( ! isset( self::$filtered_images[ $new_index ] ) ) { $new_index = 0; } self::$image_index = $new_index; } /** * Fix to alter the Astra global color variables. * * @since {{since}} * @param string $content Post Content. * @return string $content Modified content. */ public function replace_content_glitch( $content ) { $content = str_replace( 'var(\u002d\u002dast', 'var(--ast', $content ); $content = str_replace( 'var(u002du002dast', 'var(--ast', $content ); $content = str_replace( ' u0026', '&', $content ); $content = str_replace( '\u0026', '&', $content ); return $content; } } Astra_Sites_Replace_Images::get_instance(); classes/class-astra-sites-zipwp-api.php 0000644 00000014071 15104367571 0014206 0 ustar 00 <?php class Astra_Sites_ZipWP_Api { /** * Member Variable * * @var mixed */ private static $instance = null; /** * Initiator * * @since 4.0.0 * * @return mixed */ public static function get_instance() { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } /** * Constructor * * @since 4.0.0 */ public function __construct() { add_action( 'rest_api_init', array( $this, 'register_route' ) ); } /** * Get api domain * * @since 4.0.0 * @return string */ public function get_api_domain() { return (defined('ZIPWP_API') ? ZIPWP_API : 'https://api.zipwp.com/api/'); } /** * Get api namespace * * @since 4.0.0 * @return string */ public function get_api_namespace() { return 'zipwp/v1'; } /** * Get API headers * * @since 4.0.0 * @return array<string, string> */ public function get_api_headers() { return array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . Astra_Sites_ZipWP_Helper::get_token(), ); } /** * Check whether a given request has permission to read notes. * * @param object $request WP_REST_Request Full details about the request. * @return object|boolean */ public function get_item_permissions_check( $request ) { if ( ! current_user_can( 'manage_options' ) ) { return new \WP_Error( 'gt_rest_cannot_access', __( 'Sorry, you are not allowed to do that.', 'astra-sites' ), array( 'status' => rest_authorization_required_code() ) ); } return true; } /** * Register route * * @since 4.0.0 * @return void */ public function register_route() { $namespace = $this->get_api_namespace(); register_rest_route( $namespace, '/get-credits/', array( array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_user_credits' ), 'permission_callback' => array( $this, 'get_item_permissions_check' ), ), ) ); register_rest_route( $namespace, '/zip-plan/', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'get_zip_plan_details' ), 'permission_callback' => array( $this, 'get_item_permissions_check' ), 'args' => array(), ) ); register_rest_route( $namespace, '/revoke-access/', array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'revoke_access' ), 'permission_callback' => array( $this, 'get_item_permissions_check' ), ), ) ); } /** * Get the zip plan details * @since 4.0.0 * * @return \WP_REST_Response */ public function get_zip_plan_details() { $zip_plan = Astra_Sites_ZipWP_Integration::get_instance()->get_zip_plans(); $response = new \WP_REST_Response( array( 'success' => $zip_plan['status'], 'data' => $zip_plan['data'], ) ); $response->set_status( 200 ); return $response; } /** * Get User Credits. * * @param \WP_REST_Request $request Full details about the request. * @return mixed */ public function get_user_credits( $request ) { $nonce = (string)$request->get_header( 'X-WP-Nonce' ); // Verify the nonce. if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) { wp_send_json_error( array( 'data' => __( 'Nonce verification failed.', 'astra-sites' ), 'status' => false, ) ); } $api_endpoint = $this->get_api_domain() . '/scs-usage/'; $request_args = array( 'headers' => $this->get_api_headers(), 'timeout' => 100, ); $response = wp_safe_remote_post( $api_endpoint, $request_args ); if ( is_wp_error( $response ) ) { // There was an error in the request. wp_send_json_error( array( 'data' => 'Failed ' . $response->get_error_message(), 'status' => false, ) ); } $response_code = wp_remote_retrieve_response_code( $response ); $response_body = wp_remote_retrieve_body( $response ); if ( 200 === $response_code ) { $response_data = json_decode( $response_body, true ); if ( $response_data ) { $credit_details = array(); $credit_details['used'] = ! empty( $response_data['total_used_credits'] ) ? $response_data['total_used_credits'] : 0; $credit_details['total'] = $response_data['total_credits']; $credit_details['percentage'] = intval( ( $credit_details['used'] / $credit_details['total'] ) * 100 ); $credit_details['free_user'] = $response_data['free_user']; wp_send_json_success( array( 'data' => $credit_details, 'status' => true, ) ); } wp_send_json_error( array( 'data' => $response_data, 'status' => false, ) ); } wp_send_json_error( array( 'data' => 'Failed ' . $response_body, 'status' => false, ) ); } /** * Revoke access. * * @param \WP_REST_Request $request Full details about the request. * @return WP_REST_Response */ public function revoke_access( $request ): WP_REST_Response { $nonce = $request->get_header( 'X-WP-Nonce' ); $nonce = isset( $nonce ) ? sanitize_text_field( $nonce ) : ''; // Verify the nonce. if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) { wp_send_json_error( array( 'data' => __( 'Nonce verification failed.', 'astra-sites' ), 'status' => false, ) ); } $business_details = get_option( 'ast-templates-business-details', false ); delete_option( 'ast-block-templates-show-onboarding' ); if ( ! $business_details ) { $business_details = array(); } $business_details['token'] = ''; $updated = update_option( 'ast-templates-business-details', $business_details ); delete_option( 'zip_ai_settings' ); $response = new WP_REST_Response( array( 'success' => $updated, ) ); $response->set_status( 200 ); return $response; } } Astra_Sites_ZipWP_Api::get_instance(); classes/class-astra-sites-onboarding-setup.php 0000644 00000006571 15104367571 0015554 0 ustar 00 <?php /** * Ai site setup * * @since 3.0.0-beta.1 * @package Astra Sites */ if ( ! defined( 'ABSPATH' ) ) { exit; } use STImporter\Importer\ST_Importer; if ( ! class_exists( 'Astra_Sites_Onboarding_Setup' ) ) : /** * AI Site Setup */ class Astra_Sites_Onboarding_Setup { /** * Instance * * @since 4.0.0 * @access private * @var object Class object. */ private static $instance; /** * Initiator * * @since 4.0.0 * @return mixed */ public static function get_instance() { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } /** * Constructor * * @since 3.0.0-beta.1 */ public function __construct() { add_action( 'st_before_sending_error_report', array( $this, 'delete_transient_for_import_process' ) ); add_action( 'st_before_sending_error_report', array( $this, 'temporary_cache_errors' ), 10, 1 ); add_action( 'wp_ajax_astra-sites-import_prepare_xml', array( $this, 'import_prepare_xml' ) ); add_action( 'wp_ajax_bsf_analytics_optin_status', array( $this, 'bsf_analytics_optin_status' ) ); } /** * Prepare XML Data. * * @since 1.1.0 * @return void */ public function import_prepare_xml() { // Verify Nonce. check_ajax_referer( 'astra-sites', '_ajax_nonce' ); if ( ! current_user_can( 'customize' ) ) { wp_send_json_error( __( 'You are not allowed to perform this action', 'astra-sites' ) ); } do_action( 'astra_sites_before_import_prepare_xml' ); if ( ! class_exists( 'XMLReader' ) ) { wp_send_json_error( __( 'The XMLReader library is not available. This library is required to import the content for the website.', 'astra-sites' ) ); } $wxr_url = astra_get_site_data( 'astra-site-wxr-path' ); $result = array( 'status' => false, ); if( class_exists( 'STImporter\Importer\ST_Importer' ) ) { $result = ST_Importer::prepare_xml_data( $wxr_url ); } if ( false === $result['status'] ) { wp_send_json_error( $result['error'] ); } else { wp_send_json_success( $result['data'] ); } } /** * BSF Analytics Opt-in. * * @return void * @since 4.4.19 */ public function bsf_analytics_optin_status() { // Verify Nonce. check_ajax_referer( 'astra-sites', '_ajax_nonce' ); if ( ! current_user_can( 'customize' ) ) { wp_send_json_error( esc_html__( 'You are not allowed to perform this action', 'astra-sites' ) ); } if ( empty( $_POST ) || ! isset( $_POST['bsfUsageTracking'] ) ) { wp_send_json_error( esc_html__( 'Missing required parameter.', 'astra-sites' ) ); } $opt_in = filter_input( INPUT_POST, 'bsfUsageTracking', FILTER_VALIDATE_BOOLEAN ) ? 'yes' : 'no'; update_site_option( 'bsf_analytics_optin', $opt_in ); wp_send_json_success( esc_html__( 'Usage tracking updated successfully.', 'astra-sites' ) ); } /** * Delete transient for import process. * * @param array<string|int, mixed> $posted_data * @since 3.1.4 * @return void */ public function temporary_cache_errors( $posted_data ) { update_option( 'astra_sites_cached_import_error', $posted_data, false ); } /** * Delete transient for import process. * * @since 3.1.4 * @return void */ public function delete_transient_for_import_process() { delete_transient( 'astra_sites_import_started' ); } } Astra_Sites_Onboarding_Setup::get_instance(); endif; classes/class-astra-sites-zipwp-integration.php 0000644 00000011354 15104367571 0015761 0 ustar 00 <?php class Astra_Sites_ZipWP_Integration { /** * Instance * * @since 4.0.0 * @access private * @var object Class object. */ private static $instance = null; /** * Initiator * * @since 4.0.0 * @return mixed */ public static function get_instance() { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } /** * Constructor * * @since 4.0.0 */ public function __construct() { $this->define_constants(); add_action( 'wp_enqueue_scripts', array( $this, 'register_preview_scripts' ) ); } /** * Check whether a given request has permission to read notes. * * @param object $request WP_REST_Request Full details about the request. * @return object|boolean */ public function get_items_permissions_check( $request ) { if ( ! current_user_can( 'manage_options' ) ) { return new \WP_Error( 'gt_rest_cannot_access', __( 'Sorry, you are not allowed to do that.', 'astra-sites' ), array( 'status' => rest_authorization_required_code() ) ); } return true; } /** * Register scripts. * * @return void * @since 4.0.0 */ public function register_preview_scripts() { if ( is_customize_preview() ) { return; } $handle = 'starter-templates-zip-preview'; $js_deps_file = INTELLIGENT_TEMPLATES_DIR . 'assets/dist/template-preview/main.asset.php'; $js_dep = [ 'dependencies' => array(), 'version' => ASTRA_SITES_VER, ]; if ( file_exists( $js_deps_file ) ) { $script_info = include_once $js_deps_file; if ( isset( $script_info['dependencies'] ) && isset( $script_info['version'] ) ) { $js_dep['dependencies'] = $script_info['dependencies']; $js_dep['version'] = $script_info['version']; } } wp_register_script( $handle, INTELLIGENT_TEMPLATES_URI . 'assets/dist/template-preview/main.js', $js_dep['dependencies'], $js_dep['version'], true ); $color_palette_prefix = '--ast-global-'; $ele_color_palette_prefix = '--ast-global-'; if ( class_exists( 'Astra_Global_Palette' ) ) { $astra_callable_class = new \Astra_Global_Palette(); if ( is_callable( array( $astra_callable_class, 'get_css_variable_prefix' ) ) ) { $color_palette_prefix = \Astra_Global_Palette::get_css_variable_prefix(); } if ( is_callable( array( $astra_callable_class, 'get_palette_slugs' ) ) ) { $ele_color_palette_prefix = \Astra_Global_Palette::get_palette_slugs(); } } wp_localize_script( $handle, 'starter_templates_zip_preview', array( 'AstColorPaletteVarPrefix' => $color_palette_prefix, 'AstEleColorPaletteVarPrefix' => $ele_color_palette_prefix, ) ); wp_enqueue_script( $handle ); wp_add_inline_style( 'starter-templates-zip-preview-custom', '#wpadminbar { display: none !important; }' ); } /** * Define Constants * * @since 4.0.0 * @return void */ public function define_constants() : void { if ( ! defined( 'ZIPWP_APP' ) ) { define( 'ZIPWP_APP', apply_filters( 'ast_block_templates_zip_app_url', 'https://app.zipwp.com/auth' ) ); } if ( ! defined( 'ZIPWP_API' ) ) { define( 'ZIPWP_API', apply_filters( 'ast_block_templates_zip_api_url', 'https://api.zipwp.com/api' ) ); } } /** * Get ZIP Plans. * * @return array<string, mixed> */ public function get_zip_plans() { $api_endpoint = Astra_Sites_ZipWP_Api::get_instance()->get_api_domain() . '/plan/current-plan'; $request_args = array( 'headers' => Astra_Sites_ZipWP_Api::get_instance()->get_api_headers(), 'timeout' => 100, 'sslverify' => false, ); $response = wp_safe_remote_get( $api_endpoint, $request_args ); if ( is_wp_error( $response ) ) { // There was an error in the request. return array( 'data' => 'Failed ' . $response->get_error_message(), 'status' => false, ); } else { $response_code = wp_remote_retrieve_response_code( $response ); $response_body = wp_remote_retrieve_body( $response ); if ( 200 === $response_code ) { $response_data = json_decode( $response_body, true ); if ( $response_data ) { return array( 'data' => $response_data, 'status' => true, ); } else { return array( 'data' => $response_data, 'status' => false, ); } } else { return array( 'data' => 'Failed', 'status' => false, ); } } } } Astra_Sites_ZipWP_Integration::get_instance(); assets/images/placeholder.png 0000644 00000145370 15104367571 0012326 0 ustar 00 �PNG IHDR � e�8� tEXtSoftware Adobe ImageReadyq�e<