/** * Copyright (C) 2014-2025 ServMask Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Attribution: This code is part of the All-in-One WP Migration plugin, developed by * * ███████╗███████╗██████╗ ██╗ ██╗███╗ ███╗ █████╗ ███████╗██╗ ██╗ * ██╔════╝██╔════╝██╔══██╗██║ ██║████╗ ████║██╔══██╗██╔════╝██║ ██╔╝ * ███████╗█████╗ ██████╔╝██║ ██║██╔████╔██║███████║███████╗█████╔╝ * ╚════██║██╔══╝ ██╔══██╗╚██╗ ██╔╝██║╚██╔╝██║██╔══██║╚════██║██╔═██╗ * ███████║███████╗██║ ██║ ╚████╔╝ ██║ ╚═╝ ██║██║ ██║███████║██║ ██╗ * ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ */ if ( ! defined( 'ABSPATH' ) ) { die( 'Kangaroos cannot jump here' ); } /** * Get storage absolute path * * @param array $params Request parameters * @return string */ function ai1wm_storage_path( $params ) { if ( empty( $params['storage'] ) ) { throw new Ai1wm_Storage_Exception( __( 'Could not locate the storage path. The process cannot continue. Technical details', AI1WM_PLUGIN_NAME ) ); } // Validate storage path if ( ai1wm_validate_file( $params['storage'] ) !== 0 ) { throw new Ai1wm_Storage_Exception( __( 'Your storage directory name contains invalid characters: < > : " | ? * \0. It must not include these characters. The process cannot continue. Technical details', AI1WM_PLUGIN_NAME ) ); } // Get storage path $storage = AI1WM_STORAGE_PATH . DIRECTORY_SEPARATOR . basename( $params['storage'] ); if ( ! is_dir( $storage ) ) { mkdir( $storage, 0777, true ); } return $storage; } /** * Get backup absolute path * * @param array $params Request parameters * @return string */ function ai1wm_backup_path( $params ) { if ( empty( $params['archive'] ) ) { throw new Ai1wm_Archive_Exception( __( 'Could not locate the archive path. The process cannot continue. Technical details', AI1WM_PLUGIN_NAME ) ); } // Validate archive path if ( ai1wm_validate_file( $params['archive'] ) !== 0 ) { throw new Ai1wm_Archive_Exception( __( 'Your archive file name contains invalid characters: < > : " | ? * \0. It must not include these characters. The process cannot continue. Technical details', AI1WM_PLUGIN_NAME ) ); } // Validate file extension if ( ! ai1wm_is_filename_supported( $params['archive'] ) ) { throw new Ai1wm_Archive_Exception( __( 'Invalid archive file type. Only .wpress files are allowed. The process cannot continue. Technical details', AI1WM_PLUGIN_NAME ) ); } return AI1WM_BACKUPS_PATH . DIRECTORY_SEPARATOR . $params['archive']; } /** * Validates a file name and path against an allowed set of rules * * @param string $file File path * @param array $allowed_files Array of allowed files * @return integer */ function ai1wm_validate_file( $file, $allowed_files = array() ) { $file = str_replace( '\\', '/', $file ); // Validates special characters that are illegal in filenames on certain // operating systems and special characters requiring special escaping // to manipulate at the command line $invalid_chars = array( '<', '>', ':', '"', '|', '?', '*', chr( 0 ) ); foreach ( $invalid_chars as $char ) { if ( strpos( $file, $char ) !== false ) { return 1; } } return validate_file( $file, $allowed_files ); } /** * Get archive absolute path * * @param array $params Request parameters * @return string */ function ai1wm_archive_path( $params ) { if ( empty( $params['archive'] ) ) { throw new Ai1wm_Archive_Exception( __( 'Could not locate the archive path. The process cannot continue. Technical details', AI1WM_PLUGIN_NAME ) ); } // Validate archive path if ( ai1wm_validate_file( $params['archive'] ) !== 0 ) { throw new Ai1wm_Archive_Exception( __( 'Your archive file name contains invalid characters: < > : " | ? * \0. It must not include these characters. The process cannot continue. Technical details', AI1WM_PLUGIN_NAME ) ); } // Validate file extension if ( ! ai1wm_is_filename_supported( $params['archive'] ) ) { throw new Ai1wm_Archive_Exception( __( 'Invalid archive file type. Only .wpress files are allowed. The process cannot continue. Technical details', AI1WM_PLUGIN_NAME ) ); } // Get archive path if ( empty( $params['ai1wm_manual_restore'] ) ) { return ai1wm_storage_path( $params ) . DIRECTORY_SEPARATOR . $params['archive']; } return ai1wm_backup_path( $params ); } /** * Get multipart.list absolute path * * @param array $params Request parameters * @return string */ function ai1wm_multipart_path( $params ) { return ai1wm_storage_path( $params ) . DIRECTORY_SEPARATOR . AI1WM_MULTIPART_NAME; } /** * Get content.list absolute path * * @param array $params Request parameters * @return string */ function ai1wm_content_list_path( $params ) { return ai1wm_storage_path( $params ) . DIRECTORY_SEPARATOR . AI1WM_CONTENT_LIST_NAME; } /** * Get media.list absolute path * * @param array $params Request parameters * @return string */ function ai1wm_media_list_path( $params ) { return ai1wm_storage_path( $params ) . DIRECTORY_SEPARATOR . AI1WM_MEDIA_LIST_NAME; } /** * Get plugins.list absolute path * * @param array $params Request parameters * @return string */ function ai1wm_plugins_list_path( $params ) { return ai1wm_storage_path( $params ) . DIRECTORY_SEPARATOR . AI1WM_PLUGINS_LIST_NAME; } /** * Get themes.list absolute path * * @param array $params Request parameters * @return string */ function ai1wm_themes_list_path( $params ) { return ai1wm_storage_path( $params ) . DIRECTORY_SEPARATOR . AI1WM_THEMES_LIST_NAME; } /** * Get tables.list absolute path * * @param array $params Request parameters * @return string */ function ai1wm_tables_list_path( $params ) { return ai1wm_storage_path( $params ) . DIRECTORY_SEPARATOR . AI1WM_TABLES_LIST_NAME; } /** * Get incremental.content.list absolute path * * @param array $params Request parameters * @return string */ function ai1wm_incremental_content_list_path( $params ) { return ai1wm_storage_path( $params ) . DIRECTORY_SEPARATOR . AI1WM_INCREMENTAL_CONTENT_LIST_NAME; } /** * Get incremental.media.list absolute path * * @param array $params Request parameters * @return string */ function ai1wm_incremental_media_list_path( $params ) { return ai1wm_storage_path( $params ) . DIRECTORY_SEPARATOR . AI1WM_INCREMENTAL_MEDIA_LIST_NAME; } /** * Get incremental.plugins.list absolute path * * @param array $params Request parameters * @return string */ function ai1wm_incremental_plugins_list_path( $params ) { return ai1wm_storage_path( $params ) . DIRECTORY_SEPARATOR . AI1WM_INCREMENTAL_PLUGINS_LIST_NAME; } /** * Get incremental.themes.list absolute path * * @param array $params Request parameters * @return string */ function ai1wm_incremental_themes_list_path( $params ) { return ai1wm_storage_path( $params ) . DIRECTORY_SEPARATOR . AI1WM_INCREMENTAL_THEMES_LIST_NAME; } /** * Get incremental.backups.list absolute path * * @param array $params Request parameters * @return string */ function ai1wm_incremental_backups_list_path( $params ) { return ai1wm_storage_path( $params ) . DIRECTORY_SEPARATOR . AI1WM_INCREMENTAL_BACKUPS_LIST_NAME; } /** * Get package.json absolute path * * @param array $params Request parameters * @return string */ function ai1wm_package_path( $params ) { return ai1wm_storage_path( $params ) . DIRECTORY_SEPARATOR . AI1WM_PACKAGE_NAME; } /** * Get multisite.json absolute path * * @param array $params Request parameters * @return string */ function ai1wm_multisite_path( $params ) { return ai1wm_storage_path( $params ) . DIRECTORY_SEPARATOR . AI1WM_MULTISITE_NAME; } /** * Get blogs.json absolute path * * @param array $params Request parameters * @return string */ function ai1wm_blogs_path( $params ) { return ai1wm_storage_path( $params ) . DIRECTORY_SEPARATOR . AI1WM_BLOGS_NAME; } /** * Get settings.json absolute path * * @param array $params Request parameters * @return string */ function ai1wm_settings_path( $params ) { return ai1wm_storage_path( $params ) . DIRECTORY_SEPARATOR . AI1WM_SETTINGS_NAME; } /** * Get database.sql absolute path * * @param array $params Request parameters * @return string */ function ai1wm_database_path( $params ) { return ai1wm_storage_path( $params ) . DIRECTORY_SEPARATOR . AI1WM_DATABASE_NAME; } /** * Get cookies.txt absolute path * * @param array $params Request parameters * @return string */ function ai1wm_cookies_path( $params ) { return ai1wm_storage_path( $params ) . DIRECTORY_SEPARATOR . AI1WM_COOKIES_NAME; } /** * Get error log absolute path * * @param string $nonce Log nonce * @return string */ function ai1wm_error_path( $nonce ) { return AI1WM_STORAGE_PATH . DIRECTORY_SEPARATOR . sprintf( AI1WM_ERROR_NAME, $nonce ); } /** * Get archive name * * @param array $params Request parameters * @return string */ function ai1wm_archive_name( $params ) { return basename( $params['archive'] ); } /** * Get backup URL address * * @param array $params Request parameters * @return string */ function ai1wm_backup_url( $params ) { static $backups_base_url = ''; if ( empty( $backups_base_url ) ) { if ( Ai1wm_Backups::are_in_wp_content_folder() ) { $backups_base_url = str_replace( untrailingslashit( WP_CONTENT_DIR ), '', AI1WM_BACKUPS_PATH ); $backups_base_url = content_url( ai1wm_replace_directory_separator_with_forward_slash( $backups_base_url ) ); } else { $backups_base_url = str_replace( untrailingslashit( ABSPATH ), '', AI1WM_BACKUPS_PATH ); $backups_base_url = site_url( ai1wm_replace_directory_separator_with_forward_slash( $backups_base_url ) ); } } return $backups_base_url . '/' . ai1wm_replace_directory_separator_with_forward_slash( $params['archive'] ); } /** * Get archive size in bytes * * @param array $params Request parameters * @return integer */ function ai1wm_archive_bytes( $params ) { return filesize( ai1wm_archive_path( $params ) ); } /** * Get archive modified time in seconds * * @param array $params Request parameters * @return integer */ function ai1wm_archive_mtime( $params ) { return filemtime( ai1wm_archive_path( $params ) ); } /** * Get backup size in bytes * * @param array $params Request parameters * @return integer */ function ai1wm_backup_bytes( $params ) { return filesize( ai1wm_backup_path( $params ) ); } /** * Get database size in bytes * * @param array $params Request parameters * @return integer */ function ai1wm_database_bytes( $params ) { return filesize( ai1wm_database_path( $params ) ); } /** * Get package size in bytes * * @param array $params Request parameters * @return integer */ function ai1wm_package_bytes( $params ) { return filesize( ai1wm_package_path( $params ) ); } /** * Get multisite size in bytes * * @param array $params Request parameters * @return integer */ function ai1wm_multisite_bytes( $params ) { return filesize( ai1wm_multisite_path( $params ) ); } /** * Get archive size as text * * @param array $params Request parameters * @return string */ function ai1wm_archive_size( $params ) { return ai1wm_size_format( filesize( ai1wm_archive_path( $params ) ) ); } /** * Get backup size as text * * @param array $params Request parameters * @return string */ function ai1wm_backup_size( $params ) { return ai1wm_size_format( filesize( ai1wm_backup_path( $params ) ) ); } /** * Parse file size * * @param string $size File size * @param string $default Default size * @return string */ function ai1wm_parse_size( $size, $default = null ) { $suffixes = array( '' => 1, 'k' => 1000, 'm' => 1000000, 'g' => 1000000000, ); // Parse size format if ( preg_match( '/([0-9]+)\s*(k|m|g)?(b?(ytes?)?)/i', $size, $matches ) ) { return $matches[1] * $suffixes[ strtolower( $matches[2] ) ]; } return $default; } /** * Format file size into human-readable string * * Fixes the WP size_format bug: size_format( '0' ) => false * * @param int|string $bytes Number of bytes. Note max integer size for integers. * @param int $decimals Optional. Precision of number of decimal places. Default 0. * @return string|false False on failure. Number string on success. */ function ai1wm_size_format( $bytes, $decimals = 0 ) { if ( strval( $bytes ) === '0' ) { return size_format( 0, $decimals ); } return size_format( $bytes, $decimals ); } /** * Get current site name * * @param integer $blog_id Blog ID * @return string */ function ai1wm_site_name( $blog_id = null ) { return parse_url( get_site_url( $blog_id ), PHP_URL_HOST ); } /** * Get archive file name * * @param integer $blog_id Blog ID * @return string */ function ai1wm_archive_file( $blog_id = null ) { $name = array(); // Add domain if ( defined( 'AI1WM_KEEP_DOMAIN_NAME' ) ) { $name[] = parse_url( get_site_url( $blog_id ), PHP_URL_HOST ); } elseif ( ( $domain = explode( '.', parse_url( get_site_url( $blog_id ), PHP_URL_HOST ) ) ) ) { foreach ( $domain as $subdomain ) { if ( ( $subdomain = strtolower( $subdomain ) ) ) { $name[] = $subdomain; } } } // Add path if ( ( $path = parse_url( get_site_url( $blog_id ), PHP_URL_PATH ) ) ) { foreach ( explode( '/', $path ) as $directory ) { if ( ( $directory = strtolower( preg_replace( '/[^A-Za-z0-9\-]/', '', $directory ) ) ) ) { $name[] = $directory; } } } // Add year, month and day $name[] = date_i18n( 'Ymd' ); // Add hours, minutes and seconds $name[] = date_i18n( 'His' ); // Add unique identifier $name[] = ai1wm_generate_random_string( 12, false ); return sprintf( '%s.wpress', strtolower( implode( '-', $name ) ) ); } /** * Get archive folder name * * @param integer $blog_id Blog ID * @return string */ function ai1wm_archive_folder( $blog_id = null ) { $name = array(); // Add domain if ( defined( 'AI1WM_KEEP_DOMAIN_NAME' ) ) { $name[] = parse_url( get_site_url( $blog_id ), PHP_URL_HOST ); } elseif ( ( $domain = explode( '.', parse_url( get_site_url( $blog_id ), PHP_URL_HOST ) ) ) ) { foreach ( $domain as $subdomain ) { if ( ( $subdomain = strtolower( $subdomain ) ) ) { $name[] = $subdomain; } } } // Add path if ( ( $path = parse_url( get_site_url( $blog_id ), PHP_URL_PATH ) ) ) { foreach ( explode( '/', $path ) as $directory ) { if ( ( $directory = strtolower( preg_replace( '/[^A-Za-z0-9\-]/', '', $directory ) ) ) ) { $name[] = $directory; } } } return strtolower( implode( '-', $name ) ); } /** * Get archive bucket name * * @param integer $blog_id Blog ID * @return string */ function ai1wm_archive_bucket( $blog_id = null ) { $name = array(); // Add domain if ( ( $domain = explode( '.', parse_url( get_site_url( $blog_id ), PHP_URL_HOST ) ) ) ) { foreach ( $domain as $subdomain ) { if ( ( $subdomain = strtolower( $subdomain ) ) ) { $name[] = $subdomain; } } } // Add path if ( ( $path = parse_url( get_site_url( $blog_id ), PHP_URL_PATH ) ) ) { foreach ( explode( '/', $path ) as $directory ) { if ( ( $directory = strtolower( preg_replace( '/[^A-Za-z0-9\-]/', '', $directory ) ) ) ) { $name[] = $directory; } } } return strtolower( implode( '-', $name ) ); } /** * Get archive vault name * * @param integer $blog_id Blog ID * @return string */ function ai1wm_archive_vault( $blog_id = null ) { $name = array(); // Add domain if ( ( $domain = explode( '.', parse_url( get_site_url( $blog_id ), PHP_URL_HOST ) ) ) ) { foreach ( $domain as $subdomain ) { if ( ( $subdomain = strtolower( $subdomain ) ) ) { $name[] = $subdomain; } } } // Add path if ( ( $path = parse_url( get_site_url( $blog_id ), PHP_URL_PATH ) ) ) { foreach ( explode( '/', $path ) as $directory ) { if ( ( $directory = strtolower( preg_replace( '/[^A-Za-z0-9\-]/', '', $directory ) ) ) ) { $name[] = $directory; } } } return strtolower( implode( '-', $name ) ); } /** * Get archive project name * * @param integer $blog_id Blog ID * @return string */ function ai1wm_archive_project( $blog_id = null ) { $name = array(); // Add domain if ( ( $domain = explode( '.', parse_url( get_site_url( $blog_id ), PHP_URL_HOST ) ) ) ) { foreach ( $domain as $subdomain ) { if ( ( $subdomain = strtolower( $subdomain ) ) ) { $name[] = $subdomain; } } } // Add path if ( ( $path = parse_url( get_site_url( $blog_id ), PHP_URL_PATH ) ) ) { foreach ( explode( '/', $path ) as $directory ) { if ( ( $directory = strtolower( preg_replace( '/[^A-Za-z0-9\-]/', '', $directory ) ) ) ) { $name[] = $directory; } } } return strtolower( implode( '-', $name ) ); } /** * Get archive share name * * @param integer $blog_id Blog ID * @return string */ function ai1wm_archive_share( $blog_id = null ) { $name = array(); // Add domain if ( ( $domain = explode( '.', parse_url( get_site_url( $blog_id ), PHP_URL_HOST ) ) ) ) { foreach ( $domain as $subdomain ) { if ( ( $subdomain = strtolower( $subdomain ) ) ) { $name[] = $subdomain; } } } // Add path if ( ( $path = parse_url( get_site_url( $blog_id ), PHP_URL_PATH ) ) ) { foreach ( explode( '/', $path ) as $directory ) { if ( ( $directory = strtolower( preg_replace( '/[^A-Za-z0-9\-]/', '', $directory ) ) ) ) { $name[] = $directory; } } } return strtolower( implode( '-', $name ) ); } /** * Generate random string * * @param integer $length String length * @param boolean $mixed_chars Whether to include mixed characters * @param boolean $special_chars Whether to include special characters * @param boolean $extra_special_chars Whether to include extra special characters * @return string */ function ai1wm_generate_random_string( $length = 12, $mixed_chars = true, $special_chars = false, $extra_special_chars = false ) { $chars = 'abcdefghijklmnopqrstuvwxyz0123456789'; if ( $mixed_chars ) { $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; } if ( $special_chars ) { $chars .= '!@#$%^&*()'; } if ( $extra_special_chars ) { $chars .= '-_ []{}<>~`+=,.;:/?|'; } $str = ''; for ( $i = 0; $i < $length; $i++ ) { $str .= substr( $chars, wp_rand( 0, strlen( $chars ) - 1 ), 1 ); } return $str; } /** * Get storage folder name * * @return string */ function ai1wm_storage_folder() { return uniqid(); } /** * Check whether blog ID is main site * * @param integer $blog_id Blog ID * @return boolean */ function ai1wm_is_mainsite( $blog_id = null ) { return $blog_id === null || $blog_id === 0 || $blog_id === 1; } /** * Get files absolute path by blog ID * * @param integer $blog_id Blog ID * @return string */ function ai1wm_blog_files_abspath( $blog_id = null ) { if ( ai1wm_is_mainsite( $blog_id ) ) { return ai1wm_get_uploads_dir(); } return WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'blogs.dir' . DIRECTORY_SEPARATOR . $blog_id . DIRECTORY_SEPARATOR . 'files'; } /** * Get blogs.dir absolute path by blog ID * * @param integer $blog_id Blog ID * @return string */ function ai1wm_blog_blogsdir_abspath( $blog_id = null ) { if ( ai1wm_is_mainsite( $blog_id ) ) { return ai1wm_get_uploads_dir(); } return WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'blogs.dir' . DIRECTORY_SEPARATOR . $blog_id; } /** * Get sites absolute path by blog ID * * @param integer $blog_id Blog ID * @return string */ function ai1wm_blog_sites_abspath( $blog_id = null ) { if ( ai1wm_is_mainsite( $blog_id ) ) { return ai1wm_get_uploads_dir(); } return ai1wm_get_uploads_dir() . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . $blog_id; } /** * Get files relative path by blog ID * * @param integer $blog_id Blog ID * @return string */ function ai1wm_blog_files_relpath( $blog_id = null ) { if ( ai1wm_is_mainsite( $blog_id ) ) { return 'uploads'; } return 'blogs.dir' . DIRECTORY_SEPARATOR . $blog_id . DIRECTORY_SEPARATOR . 'files'; } /** * Get blogs.dir relative path by blog ID * * @param integer $blog_id Blog ID * @return string */ function ai1wm_blog_blogsdir_relpath( $blog_id = null ) { if ( ai1wm_is_mainsite( $blog_id ) ) { return 'uploads'; } return 'blogs.dir' . DIRECTORY_SEPARATOR . $blog_id; } /** * Get sites relative path by blog ID * * @param integer $blog_id Blog ID * @return string */ function ai1wm_blog_sites_relpath( $blog_id = null ) { if ( ai1wm_is_mainsite( $blog_id ) ) { return 'uploads'; } return 'uploads' . DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . $blog_id; } /** * Get files URL by blog ID * * @param integer $blog_id Blog ID * @return string */ function ai1wm_blog_files_url( $blog_id = null ) { if ( ai1wm_is_mainsite( $blog_id ) ) { return '/wp-content/uploads/'; } return sprintf( '/wp-content/blogs.dir/%d/files/', $blog_id ); } /** * Get blogs.dir URL by blog ID * * @param integer $blog_id Blog ID * @return string */ function ai1wm_blog_blogsdir_url( $blog_id = null ) { if ( ai1wm_is_mainsite( $blog_id ) ) { return '/wp-content/uploads/'; } return sprintf( '/wp-content/blogs.dir/%d/', $blog_id ); } /** * Get sites URL by blog ID * * @param integer $blog_id Blog ID * @return string */ function ai1wm_blog_sites_url( $blog_id = null ) { if ( ai1wm_is_mainsite( $blog_id ) ) { return '/wp-content/uploads/'; } return sprintf( '/wp-content/uploads/sites/%d/', $blog_id ); } /** * Get uploads URL by blog ID * * @param integer $blog_id Blog ID * @return string */ function ai1wm_blog_uploads_url( $blog_id = null ) { if ( ai1wm_is_mainsite( $blog_id ) ) { return sprintf( '/%s/', ai1wm_get_uploads_path() ); } return sprintf( '/%s/sites/%d/', ai1wm_get_uploads_path(), $blog_id ); } /** * Get ServMask table prefix by blog ID * * @param integer $blog_id Blog ID * @return string */ function ai1wm_servmask_prefix( $blog_id = null ) { if ( ai1wm_is_mainsite( $blog_id ) ) { return AI1WM_TABLE_PREFIX; } return AI1WM_TABLE_PREFIX . $blog_id . '_'; } /** * Get WordPress table prefix by blog ID * * @param integer $blog_id Blog ID * @return string */ function ai1wm_table_prefix( $blog_id = null ) { global $wpdb; // Set base table prefix if ( ai1wm_is_mainsite( $blog_id ) ) { return $wpdb->base_prefix; } return $wpdb->base_prefix . $blog_id . '_'; } /** * Get default content filters * * @param array $filters List of files and directories * @return array */ function ai1wm_content_filters( $filters = array() ) { return array_merge( $filters, array( AI1WM_BACKUPS_PATH, AI1WM_BACKUPS_NAME, AI1WM_PACKAGE_NAME, AI1WM_MULTISITE_NAME, AI1WM_DATABASE_NAME, AI1WM_W3TC_CONFIG_FILE, ) ); } /** * Get default media filters * * @param array $filters List of files and directories * @return array */ function ai1wm_media_filters( $filters = array() ) { return array_merge( $filters, array( AI1WM_BACKUPS_PATH, ) ); } /** * Get default plugin filters * * @param array $filters List of plugins * @return array */ function ai1wm_plugin_filters( $filters = array() ) { return array_merge( $filters, array( AI1WM_BACKUPS_PATH, AI1WM_PLUGIN_BASEDIR, AI1WMZE_PLUGIN_BASEDIR, AI1WMAE_PLUGIN_BASEDIR, AI1WMVE_PLUGIN_BASEDIR, AI1WMBE_PLUGIN_BASEDIR, AI1WMIE_PLUGIN_BASEDIR, AI1WMXE_PLUGIN_BASEDIR, AI1WMDE_PLUGIN_BASEDIR, AI1WMTE_PLUGIN_BASEDIR, AI1WMFE_PLUGIN_BASEDIR, AI1WMCE_PLUGIN_BASEDIR, AI1WMGE_PLUGIN_BASEDIR, AI1WMRE_PLUGIN_BASEDIR, AI1WMEE_PLUGIN_BASEDIR, AI1WMME_PLUGIN_BASEDIR, AI1WMOE_PLUGIN_BASEDIR, AI1WMPE_PLUGIN_BASEDIR, AI1WMKE_PLUGIN_BASEDIR, AI1WMNE_PLUGIN_BASEDIR, AI1WMSE_PLUGIN_BASEDIR, AI1WMUE_PLUGIN_BASEDIR, AI1WMLE_PLUGIN_BASEDIR, AI1WMWE_PLUGIN_BASEDIR, ) ); } /** * Get default theme filters * * @param array $filters List of files and directories * @return array */ function ai1wm_theme_filters( $filters = array() ) { return array_merge( $filters, array( AI1WM_BACKUPS_PATH, ) ); } /** * Get active ServMask plugins * * @return array */ function ai1wm_active_servmask_plugins( $plugins = array() ) { // WP Migration Plugin if ( defined( 'AI1WM_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WM_PLUGIN_BASENAME; } // Microsoft Azure Extension if ( defined( 'AI1WMZE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMZE_PLUGIN_BASENAME; } // Backblaze B2 Extension if ( defined( 'AI1WMAE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMAE_PLUGIN_BASENAME; } // Backup Plugin if ( defined( 'AI1WMVE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMVE_PLUGIN_BASENAME; } // Box Extension if ( defined( 'AI1WMBE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMBE_PLUGIN_BASENAME; } // DigitalOcean Spaces Extension if ( defined( 'AI1WMIE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMIE_PLUGIN_BASENAME; } // Direct Extension if ( defined( 'AI1WMXE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMXE_PLUGIN_BASENAME; } // Dropbox Extension if ( defined( 'AI1WMDE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMDE_PLUGIN_BASENAME; } // File Extension if ( defined( 'AI1WMTE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMTE_PLUGIN_BASENAME; } // FTP Extension if ( defined( 'AI1WMFE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMFE_PLUGIN_BASENAME; } // Google Cloud Storage Extension if ( defined( 'AI1WMCE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMCE_PLUGIN_BASENAME; } // Google Drive Extension if ( defined( 'AI1WMGE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMGE_PLUGIN_BASENAME; } // Amazon Glacier Extension if ( defined( 'AI1WMRE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMRE_PLUGIN_BASENAME; } // Mega Extension if ( defined( 'AI1WMEE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMEE_PLUGIN_BASENAME; } // Multisite Extension if ( defined( 'AI1WMME_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMME_PLUGIN_BASENAME; } // OneDrive Extension if ( defined( 'AI1WMOE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMOE_PLUGIN_BASENAME; } // pCloud Extension if ( defined( 'AI1WMPE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMPE_PLUGIN_BASENAME; } // Pro Plugin if ( defined( 'AI1WMKE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMKE_PLUGIN_BASENAME; } // S3 Client Extension if ( defined( 'AI1WMNE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMNE_PLUGIN_BASENAME; } // Amazon S3 Extension if ( defined( 'AI1WMSE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMSE_PLUGIN_BASENAME; } // Unlimited Extension if ( defined( 'AI1WMUE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMUE_PLUGIN_BASENAME; } // URL Extension if ( defined( 'AI1WMLE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMLE_PLUGIN_BASENAME; } // WebDAV Extension if ( defined( 'AI1WMWE_PLUGIN_BASENAME' ) ) { $plugins[] = AI1WMWE_PLUGIN_BASENAME; } return $plugins; } /** * Get active sitewide plugins * * @return array */ function ai1wm_active_sitewide_plugins() { return array_keys( get_site_option( AI1WM_ACTIVE_SITEWIDE_PLUGINS, array() ) ); } /** * Get active plugins * * @return array */ function ai1wm_active_plugins() { return array_values( get_option( AI1WM_ACTIVE_PLUGINS, array() ) ); } /** * Set active sitewide plugins (inspired by WordPress activate_plugins() function) * * @param array $plugins List of plugins * @return boolean */ function ai1wm_activate_sitewide_plugins( $plugins ) { $current = get_site_option( AI1WM_ACTIVE_SITEWIDE_PLUGINS, array() ); // Add plugins foreach ( $plugins as $plugin ) { if ( ! isset( $current[ $plugin ] ) && ! is_wp_error( validate_plugin( $plugin ) ) ) { $current[ $plugin ] = time(); } } return update_site_option( AI1WM_ACTIVE_SITEWIDE_PLUGINS, $current ); } /** * Set active plugins (inspired by WordPress activate_plugins() function) * * @param array $plugins List of plugins * @return boolean */ function ai1wm_activate_plugins( $plugins ) { $current = get_option( AI1WM_ACTIVE_PLUGINS, array() ); // Add plugins foreach ( $plugins as $plugin ) { if ( ! in_array( $plugin, $current ) && ! is_wp_error( validate_plugin( $plugin ) ) ) { $current[] = $plugin; } } return update_option( AI1WM_ACTIVE_PLUGINS, $current ); } /** * Get active template * * @return string */ function ai1wm_active_template() { return get_option( AI1WM_ACTIVE_TEMPLATE ); } /** * Get active stylesheet * * @return string */ function ai1wm_active_stylesheet() { return get_option( AI1WM_ACTIVE_STYLESHEET ); } /** * Set active template * * @param string $template Template name * @return boolean */ function ai1wm_activate_template( $template ) { return update_option( AI1WM_ACTIVE_TEMPLATE, $template ); } /** * Set active stylesheet * * @param string $stylesheet Stylesheet name * @return boolean */ function ai1wm_activate_stylesheet( $stylesheet ) { return update_option( AI1WM_ACTIVE_STYLESHEET, $stylesheet ); } /** * Set inactive sitewide plugins (inspired by WordPress deactivate_plugins() function) * * @param array $plugins List of plugins * @return boolean */ function ai1wm_deactivate_sitewide_plugins( $plugins ) { $current = get_site_option( AI1WM_ACTIVE_SITEWIDE_PLUGINS, array() ); // Add plugins foreach ( $plugins as $plugin ) { if ( isset( $current[ $plugin ] ) ) { unset( $current[ $plugin ] ); } } return update_site_option( AI1WM_ACTIVE_SITEWIDE_PLUGINS, $current ); } /** * Set inactive plugins (inspired by WordPress deactivate_plugins() function) * * @param array $plugins List of plugins * @return boolean */ function ai1wm_deactivate_plugins( $plugins ) { $current = get_option( AI1WM_ACTIVE_PLUGINS, array() ); // Remove plugins foreach ( $plugins as $plugin ) { if ( ( $key = array_search( $plugin, $current ) ) !== false ) { unset( $current[ $key ] ); } } return update_option( AI1WM_ACTIVE_PLUGINS, $current ); } /** * Deactivate Jetpack modules * * @param array $modules List of modules * @return boolean */ function ai1wm_deactivate_jetpack_modules( $modules ) { $current = get_option( AI1WM_JETPACK_ACTIVE_MODULES, array() ); // Remove modules foreach ( $modules as $module ) { if ( ( $key = array_search( $module, $current ) ) !== false ) { unset( $current[ $key ] ); } } return update_option( AI1WM_JETPACK_ACTIVE_MODULES, $current ); } /** * Deactivate Swift Optimizer rules * * @param array $rules List of rules * @return boolean */ function ai1wm_deactivate_swift_optimizer_rules( $rules ) { $current = get_option( AI1WM_SWIFT_OPTIMIZER_PLUGIN_ORGANIZER, array() ); // Remove rules foreach ( $rules as $rule ) { unset( $current['rules'][ $rule ] ); } return update_option( AI1WM_SWIFT_OPTIMIZER_PLUGIN_ORGANIZER, $current ); } /** * Deactivate sitewide Revolution Slider * * @param string $basename Plugin basename * @return boolean */ function ai1wm_deactivate_sitewide_revolution_slider( $basename ) { if ( ( $plugins = get_plugins() ) ) { if ( isset( $plugins[ $basename ]['Version'] ) && ( $version = $plugins[ $basename ]['Version'] ) ) { if ( version_compare( PHP_VERSION, '7.3', '>=' ) && version_compare( $version, '5.4.8.3', '<' ) ) { return ai1wm_deactivate_sitewide_plugins( array( $basename ) ); } if ( version_compare( PHP_VERSION, '7.2', '>=' ) && version_compare( $version, '5.4.6', '<' ) ) { return ai1wm_deactivate_sitewide_plugins( array( $basename ) ); } if ( version_compare( PHP_VERSION, '7.1', '>=' ) && version_compare( $version, '5.4.1', '<' ) ) { return ai1wm_deactivate_sitewide_plugins( array( $basename ) ); } if ( version_compare( PHP_VERSION, '7.0', '>=' ) && version_compare( $version, '4.6.5', '<' ) ) { return ai1wm_deactivate_sitewide_plugins( array( $basename ) ); } } } return false; } /** * Deactivate Revolution Slider * * @param string $basename Plugin basename * @return boolean */ function ai1wm_deactivate_revolution_slider( $basename ) { if ( ( $plugins = get_plugins() ) ) { if ( isset( $plugins[ $basename ]['Version'] ) && ( $version = $plugins[ $basename ]['Version'] ) ) { if ( version_compare( PHP_VERSION, '7.3', '>=' ) && version_compare( $version, '5.4.8.3', '<' ) ) { return ai1wm_deactivate_plugins( array( $basename ) ); } if ( version_compare( PHP_VERSION, '7.2', '>=' ) && version_compare( $version, '5.4.6', '<' ) ) { return ai1wm_deactivate_plugins( array( $basename ) ); } if ( version_compare( PHP_VERSION, '7.1', '>=' ) && version_compare( $version, '5.4.1', '<' ) ) { return ai1wm_deactivate_plugins( array( $basename ) ); } if ( version_compare( PHP_VERSION, '7.0', '>=' ) && version_compare( $version, '4.6.5', '<' ) ) { return ai1wm_deactivate_plugins( array( $basename ) ); } } } return false; } /** * Initial DB version * * @return boolean */ function ai1wm_initial_db_version() { if ( ! get_option( AI1WM_DB_VERSION ) ) { return update_option( AI1WM_DB_VERSION, get_option( AI1WM_INITIAL_DB_VERSION ) ); } return false; } /** * Discover plugin basename * * @param string $basename Plugin basename * @return string */ function ai1wm_discover_plugin_basename( $basename ) { if ( ( $plugins = get_plugins() ) ) { foreach ( $plugins as $plugin => $info ) { if ( strpos( dirname( $plugin ), dirname( $basename ) ) !== false ) { if ( basename( $plugin ) === basename( $basename ) ) { return $plugin; } } } } return $basename; } /** * Validate plugin basename * * @param string $basename Plugin basename * @return boolean */ function ai1wm_validate_plugin_basename( $basename ) { if ( ( $plugins = get_plugins() ) ) { foreach ( $plugins as $plugin => $info ) { if ( $plugin === $basename ) { return true; } } } return false; } /** * Validate theme basename * * @param string $basename Theme basename * @return boolean */ function ai1wm_validate_theme_basename( $basename ) { if ( ( $themes = search_theme_directories() ) ) { foreach ( $themes as $theme => $info ) { if ( $info['theme_file'] === $basename ) { return true; } } } return false; } /** * Flush WP options cache * * @return void */ function ai1wm_cache_flush() { wp_cache_init(); wp_cache_flush(); // Reset WP options cache wp_cache_set( 'alloptions', array(), 'options' ); wp_cache_set( 'notoptions', array(), 'options' ); // Reset WP sitemeta cache wp_cache_set( '1:notoptions', array(), 'site-options' ); wp_cache_set( '1:ms_files_rewriting', false, 'site-options' ); wp_cache_set( '1:active_sitewide_plugins', false, 'site-options' ); // Delete WP options cache wp_cache_delete( 'alloptions', 'options' ); wp_cache_delete( 'notoptions', 'options' ); // Delete WP sitemeta cache wp_cache_delete( '1:notoptions', 'site-options' ); wp_cache_delete( '1:ms_files_rewriting', 'site-options' ); wp_cache_delete( '1:active_sitewide_plugins', 'site-options' ); // Remove WP options filter remove_all_filters( 'sanitize_option_home' ); remove_all_filters( 'sanitize_option_siteurl' ); remove_all_filters( 'default_site_option_ms_files_rewriting' ); } /** * Flush Elementor cache * * @return void */ function ai1wm_elementor_cache_flush() { delete_post_meta_by_key( '_elementor_css' ); delete_option( '_elementor_global_css' ); delete_option( 'elementor-custom-breakpoints-files' ); } /** * Set WooCommerce Force SSL checkout * * @param boolean $yes Force SSL checkout * @return void */ function ai1wm_woocommerce_force_ssl( $yes = true ) { if ( get_option( 'woocommerce_force_ssl_checkout' ) ) { if ( $yes ) { update_option( 'woocommerce_force_ssl_checkout', 'yes' ); } else { update_option( 'woocommerce_force_ssl_checkout', 'no' ); } } } /** * Set URL scheme * * @param string $url URL value * @param string $scheme URL scheme * @return string */ function ai1wm_url_scheme( $url, $scheme = '' ) { if ( empty( $scheme ) ) { return preg_replace( '#^\w+://#', '//', $url ); } return preg_replace( '#^\w+://#', $scheme . '://', $url ); } /** * Opens a file in specified mode * * @param string $file Path to the file to open * @param string $mode Mode in which to open the file * @return resource * @throws Ai1wm_Not_Accessible_Exception */ function ai1wm_open( $file, $mode ) { $file_handle = @fopen( $file, $mode ); if ( false === $file_handle ) { throw new Ai1wm_Not_Accessible_Exception( sprintf( __( 'Could not open %s with mode %s. The process cannot continue. Technical details', AI1WM_PLUGIN_NAME ), $file, $mode ) ); } return $file_handle; } /** * Write contents to a file * * @param resource $handle File handle to write to * @param string $content Contents to write to the file * @return integer * @throws Ai1wm_Not_Writable_Exception * @throws Ai1wm_Quota_Exceeded_Exception */ function ai1wm_write( $handle, $content ) { $write_result = @fwrite( $handle, $content ); if ( false === $write_result ) { if ( ( $meta = stream_get_meta_data( $handle ) ) ) { throw new Ai1wm_Not_Writable_Exception( sprintf( __( 'Could not write to: %s. The process cannot continue. Technical details', AI1WM_PLUGIN_NAME ), $meta['uri'] ) ); } } elseif ( null === $write_result ) { return strlen( $content ); } elseif ( strlen( $content ) !== $write_result ) { if ( ( $meta = stream_get_meta_data( $handle ) ) ) { throw new Ai1wm_Quota_Exceeded_Exception( sprintf( __( 'Out of disk space. Could not write to: %s. The process cannot continue. Technical details', AI1WM_PLUGIN_NAME ), $meta['uri'] ) ); } } return $write_result; } /** * Read contents from a file * * @param resource $handle File handle to read from * @param integer $length Up to length number of bytes read * @return string * @throws Ai1wm_Not_Readable_Exception */ function ai1wm_read( $handle, $length ) { if ( $length > 0 ) { $read_result = @fread( $handle, $length ); if ( false === $read_result ) { if ( ( $meta = stream_get_meta_data( $handle ) ) ) { throw new Ai1wm_Not_Readable_Exception( sprintf( __( 'Could not read file: %s. The process cannot continue. Technical details', AI1WM_PLUGIN_NAME ), $meta['uri'] ) ); } } return $read_result; } return false; } /** * Seeks on a file pointer * * @param resource $handle File handle * @param integer $offset File offset * @param integer $mode Offset mode * @return integer */ function ai1wm_seek( $handle, $offset, $mode = SEEK_SET ) { $seek_result = @fseek( $handle, $offset, $mode ); if ( -1 === $seek_result ) { if ( ( $meta = stream_get_meta_data( $handle ) ) ) { throw new Ai1wm_Not_Seekable_Exception( sprintf( __( 'Could not seek to offset %d on %s. The process cannot continue. Technical details', AI1WM_PLUGIN_NAME ), $offset, $meta['uri'] ) ); } } return $seek_result; } /** * Returns the current position of the file read/write pointer * * @param resource $handle File handle * @return integer */ function ai1wm_tell( $handle ) { $tell_result = @ftell( $handle ); if ( false === $tell_result ) { if ( ( $meta = stream_get_meta_data( $handle ) ) ) { throw new Ai1wm_Not_Tellable_Exception( sprintf( __( 'Could not get current pointer position of %s. The process cannot continue. Technical details', AI1WM_PLUGIN_NAME ), $meta['uri'] ) ); } } return $tell_result; } /** * Write fields to a file * * @param resource $handle File handle to write to * @param array $fields Fields to write to the file * @param string $separator * @param string $enclosure * @param string $escape * * @return integer * @throws Ai1wm_Not_Writable_Exception */ function ai1wm_putcsv( $handle, $fields, $separator = ',', $enclosure = '"', $escape = '\\' ) { if ( PHP_MAJOR_VERSION >= 7 ) { $write_result = @fputcsv( $handle, $fields, $separator, $enclosure, $escape ); } else { $write_result = @fputcsv( $handle, $fields, $separator, $enclosure ); } if ( false === $write_result ) { if ( ( $meta = stream_get_meta_data( $handle ) ) ) { throw new Ai1wm_Not_Writable_Exception( sprintf( __( 'Could not write to: %s. The process cannot continue. Technical details', AI1WM_PLUGIN_NAME ), $meta['uri'] ) ); } } return $write_result; } /** * Read fields from a file * * @param resource $handle File handle to read from * @param int $length * @param string $separator * @param string $enclosure * @param string $escape * * @return array|false|null */ function ai1wm_getcsv( $handle, $length = null, $separator = ',', $enclosure = '"', $escape = '\\' ) { return fgetcsv( $handle, $length, $separator, $enclosure, $escape ); } /** * Closes a file handle * * @param resource $handle File handle to close * @return boolean */ function ai1wm_close( $handle ) { return @fclose( $handle ); } /** * Deletes a file * * @param string $file Path to file to delete * @return boolean */ function ai1wm_unlink( $file ) { return @unlink( $file ); } /** * Sets modification time of a file * * @param string $file Path to file to change modification time * @param integer $time File modification time * @return boolean */ function ai1wm_touch( $file, $mtime ) { return @touch( $file, $mtime ); } /** * Changes file mode * * @param string $file Path to file to change mode * @param integer $time File mode * @return boolean */ function ai1wm_chmod( $file, $mode ) { return @chmod( $file, $mode ); } /** * Copies one file's contents to another * * @param string $source_file File to copy the contents from * @param string $destination_file File to copy the contents to */ function ai1wm_copy( $source_file, $destination_file ) { $source_handle = ai1wm_open( $source_file, 'rb' ); $destination_handle = ai1wm_open( $destination_file, 'ab' ); while ( $buffer = ai1wm_read( $source_handle, 4096 ) ) { ai1wm_write( $destination_handle, $buffer ); } ai1wm_close( $source_handle ); ai1wm_close( $destination_handle ); } /** * Check whether file size is supported by current PHP version * * @param string $file Path to file * @param integer $php_int_size Size of PHP integer * @return boolean $php_int_max Max value of PHP integer */ function ai1wm_is_filesize_supported( $file, $php_int_size = PHP_INT_SIZE, $php_int_max = PHP_INT_MAX ) { $size_result = true; // Check whether file size is less than 2GB in PHP 32bits if ( $php_int_size === 4 ) { if ( ( $file_handle = @fopen( $file, 'r' ) ) ) { if ( @fseek( $file_handle, $php_int_max, SEEK_SET ) !== -1 ) { if ( @fgetc( $file_handle ) !== false ) { $size_result = false; } } @fclose( $file_handle ); } } return $size_result; } /** * Check whether file name is supported by All-in-One WP Migration * * @param string $file Path to file * @param array $extensions File extensions * @return boolean */ function ai1wm_is_filename_supported( $file, $extensions = array( 'wpress' ) ) { if ( in_array( pathinfo( $file, PATHINFO_EXTENSION ), $extensions ) ) { return true; } return false; } /** * Verify secret key * * @param string $secret_key Secret key * @return boolean * @throws Ai1wm_Not_Valid_Secret_Key_Exception */ function ai1wm_verify_secret_key( $secret_key ) { if ( $secret_key !== get_option( AI1WM_SECRET_KEY ) ) { throw new Ai1wm_Not_Valid_Secret_Key_Exception( __( 'Could not authenticate the secret key. The process cannot continue. Technical details', AI1WM_PLUGIN_NAME ) ); } return true; } /** * Is scheduled backup? * * @return boolean */ function ai1wm_is_scheduled_backup() { if ( isset( $_GET['ai1wm_manual_export'] ) || isset( $_POST['ai1wm_manual_export'] ) ) { return false; } if ( isset( $_GET['ai1wm_manual_import'] ) || isset( $_POST['ai1wm_manual_import'] ) ) { return false; } if ( isset( $_GET['ai1wm_manual_restore'] ) || isset( $_POST['ai1wm_manual_restore'] ) ) { return false; } if ( isset( $_GET['ai1wm_manual_reset'] ) || isset( $_POST['ai1wm_manual_reset'] ) ) { return false; } return true; } /** * PHP setup environment * * @return void */ function ai1wm_setup_environment() { // Set whether a client disconnect should abort script execution @ignore_user_abort( true ); // Set maximum execution time @set_time_limit( 0 ); // Set maximum time in seconds a script is allowed to parse input data @ini_set( 'max_input_time', '-1' ); // Set maximum backtracking steps @ini_set( 'pcre.backtrack_limit', PHP_INT_MAX ); // Set binary safe encoding if ( @function_exists( 'mb_internal_encoding' ) && ( @ini_get( 'mbstring.func_overload' ) & 2 ) ) { @mb_internal_encoding( 'ISO-8859-1' ); } // Clean (erase) the output buffer and turn off output buffering if ( @ob_get_length() ) { @ob_end_clean(); } } /** * PHP register error handlers * * @return void */ function ai1wm_setup_errors() { @set_error_handler( 'Ai1wm_Handler::error' ); @register_shutdown_function( 'Ai1wm_Handler::shutdown' ); } /** * Get WordPress time zone string * * @return string */ function ai1wm_get_timezone_string() { if ( ( $timezone_string = get_option( 'timezone_string' ) ) ) { return $timezone_string; } if ( ( $gmt_offset = get_option( 'gmt_offset' ) ) ) { if ( $gmt_offset > 0 ) { return sprintf( 'UTC+%s', abs( $gmt_offset ) ); } elseif ( $gmt_offset < 0 ) { return sprintf( 'UTC-%s', abs( $gmt_offset ) ); } } return 'UTC'; } /** * Get WordPress filter hooks * * @param string $tag The name of the filter hook * @return array */ function ai1wm_get_filters( $tag ) { global $wp_filter; // Get WordPress filter hooks $filters = array(); if ( isset( $wp_filter[ $tag ] ) ) { if ( ( $filters = $wp_filter[ $tag ] ) ) { // WordPress 4.7 introduces new class for working with filters/actions called WP_Hook // which adds another level of abstraction and we need to address it. if ( isset( $filters->callbacks ) ) { $filters = $filters->callbacks; } } ksort( $filters ); } return $filters; } /** * Get WordPress plugins directories * * @return array */ function ai1wm_get_themes_dirs() { $theme_dirs = array(); foreach ( search_theme_directories() as $theme_name => $theme_info ) { if ( isset( $theme_info['theme_root'] ) ) { if ( ! in_array( $theme_info['theme_root'], $theme_dirs ) ) { $theme_dirs[] = untrailingslashit( $theme_info['theme_root'] ); } } } return $theme_dirs; } /** * Get WordPress plugins directory * * @return string */ function ai1wm_get_plugins_dir() { return untrailingslashit( WP_PLUGIN_DIR ); } /** * Get WordPress uploads directory * * @return string */ function ai1wm_get_uploads_dir() { if ( ( $upload_dir = wp_upload_dir() ) ) { if ( isset( $upload_dir['basedir'] ) ) { return untrailingslashit( $upload_dir['basedir'] ); } } } /** * Get WordPress uploads URL * * @return string */ function ai1wm_get_uploads_url() { if ( ( $upload_dir = wp_upload_dir() ) ) { if ( isset( $upload_dir['baseurl'] ) ) { return trailingslashit( $upload_dir['baseurl'] ); } } } /** * Get WordPress uploads path * * @return string */ function ai1wm_get_uploads_path() { if ( ( $upload_dir = wp_upload_dir() ) ) { if ( isset( $upload_dir['basedir'] ) ) { return str_replace( ABSPATH, '', $upload_dir['basedir'] ); } } } /** * i18n friendly version of basename() * * @param string $path File path * @param string $suffix If the filename ends in suffix this will also be cut off * @return string */ function ai1wm_basename( $path, $suffix = '' ) { return urldecode( basename( str_replace( array( '%2F', '%5C' ), '/', urlencode( $path ) ), $suffix ) ); } /** * i18n friendly version of dirname() * * @param string $path File path * @return string */ function ai1wm_dirname( $path ) { return urldecode( dirname( str_replace( array( '%2F', '%5C' ), '/', urlencode( $path ) ) ) ); } /** * Replace forward slash with current directory separator * * @param string $path Path * @return string */ function ai1wm_replace_forward_slash_with_directory_separator( $path ) { return str_replace( '/', DIRECTORY_SEPARATOR, $path ); } /** * Replace current directory separator with forward slash * * @param string $path Path * @return string */ function ai1wm_replace_directory_separator_with_forward_slash( $path ) { return str_replace( DIRECTORY_SEPARATOR, '/', $path ); } /** * Escape Windows directory separator * * @param string $path Path * @return string */ function ai1wm_escape_windows_directory_separator( $path ) { return preg_replace( '/[\\\\]+/', '\\\\\\\\', $path ); } /** * Should reset WordPress permalinks? * * @param array $params Request parameters * @return boolean */ function ai1wm_should_reset_permalinks( $params ) { global $wp_rewrite, $is_apache; // Permalinks are not supported if ( empty( $params['using_permalinks'] ) ) { if ( $wp_rewrite->using_permalinks() ) { if ( $is_apache ) { if ( ! apache_mod_loaded( 'mod_rewrite', false ) ) { return true; } } } } return false; } /** * Get .htaccess file content * * @return string */ function ai1wm_get_htaccess() { if ( is_file( AI1WM_WORDPRESS_HTACCESS ) ) { return @file_get_contents( AI1WM_WORDPRESS_HTACCESS ); } return ''; } /** * Get web.config file content * * @return string */ function ai1wm_get_webconfig() { if ( is_file( AI1WM_WORDPRESS_WEBCONFIG ) ) { return @file_get_contents( AI1WM_WORDPRESS_WEBCONFIG ); } return ''; } /** * Get available space on filesystem or disk partition * * @param string $path Directory of the filesystem or disk partition * @return mixed */ function ai1wm_disk_free_space( $path ) { if ( function_exists( 'disk_free_space' ) ) { return @disk_free_space( $path ); } } /** * Set response header to json end echo data * * @param array $data * @param int $options * @param int $depth * @return void */ function ai1wm_json_response( $data, $options = 0 ) { if ( ! headers_sent() ) { header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset', 'utf-8' ) ); } echo json_encode( $data, $options ); } /** * Determines if the server can encrypt backups * * @return boolean */ function ai1wm_can_encrypt() { if ( ! function_exists( 'openssl_encrypt' ) ) { return false; } if ( ! function_exists( 'openssl_random_pseudo_bytes' ) ) { return false; } if ( ! function_exists( 'openssl_cipher_iv_length' ) ) { return false; } if ( ! function_exists( 'sha1' ) ) { return false; } if ( ! in_array( AI1WM_CIPHER_NAME, array_map( 'strtoupper', openssl_get_cipher_methods() ) ) ) { return false; } return true; } /** * Determines if the server can decrypt backups * * @return boolean */ function ai1wm_can_decrypt() { if ( ! function_exists( 'openssl_decrypt' ) ) { return false; } if ( ! function_exists( 'openssl_random_pseudo_bytes' ) ) { return false; } if ( ! function_exists( 'openssl_cipher_iv_length' ) ) { return false; } if ( ! function_exists( 'sha1' ) ) { return false; } if ( ! in_array( AI1WM_CIPHER_NAME, array_map( 'strtoupper', openssl_get_cipher_methods() ) ) ) { return false; } return true; } /** * Encrypts a string with a key * * @param string $string String to encrypt * @param string $key Key to encrypt the string with * @return string * @throws Ai1wm_Not_Encryptable_Exception */ function ai1wm_encrypt_string( $string, $key ) { $iv_length = ai1wm_crypt_iv_length(); $key = substr( sha1( $key, true ), 0, $iv_length ); $iv = openssl_random_pseudo_bytes( $iv_length ); if ( $iv === false ) { throw new Ai1wm_Not_Encryptable_Exception( __( 'Could not generate random bytes. The process cannot continue.', AI1WM_PLUGIN_NAME ) ); } $encrypted_string = openssl_encrypt( $string, AI1WM_CIPHER_NAME, $key, OPENSSL_RAW_DATA, $iv ); if ( $encrypted_string === false ) { throw new Ai1wm_Not_Encryptable_Exception( __( 'Could not encrypt data. The process cannot continue.', AI1WM_PLUGIN_NAME ) ); } return sprintf( '%s%s', $iv, $encrypted_string ); } /** * Returns encrypt/decrypt iv length * * @return int * @throws Ai1wm_Not_Encryptable_Exception */ function ai1wm_crypt_iv_length() { $iv_length = openssl_cipher_iv_length( AI1WM_CIPHER_NAME ); if ( $iv_length === false ) { throw new Ai1wm_Not_Encryptable_Exception( __( 'Could not obtain cipher length. The process cannot continue.', AI1WM_PLUGIN_NAME ) ); } return $iv_length; } /** * Decrypts a string with a eky * * @param string $encrypted_string String to decrypt * @param string $key Key to decrypt the string with * @return string * @throws Ai1wm_Not_Encryptable_Exception * @throws Ai1wm_Not_Decryptable_Exception */ function ai1wm_decrypt_string( $encrypted_string, $key ) { $iv_length = ai1wm_crypt_iv_length(); $key = substr( sha1( $key, true ), 0, $iv_length ); $iv = substr( $encrypted_string, 0, $iv_length ); $decrypted_string = openssl_decrypt( substr( $encrypted_string, $iv_length ), AI1WM_CIPHER_NAME, $key, OPENSSL_RAW_DATA, $iv ); if ( $decrypted_string === false ) { throw new Ai1wm_Not_Decryptable_Exception( __( 'Could not decrypt data. The process cannot continue.', AI1WM_PLUGIN_NAME ) ); } return $decrypted_string; } /** * Checks if decryption password is valid * * @param string $encrypted_signature * @param string $password * @return bool */ function ai1wm_is_decryption_password_valid( $encrypted_signature, $password ) { try { $encrypted_signature = base64_decode( $encrypted_signature ); return ai1wm_decrypt_string( $encrypted_signature, $password ) === AI1WM_SIGN_TEXT; } catch ( Ai1wm_Not_Decryptable_Exception $exception ) { return false; } } function ai1wm_populate_roles() { if ( ! function_exists( 'populate_roles' ) && ! function_exists( 'populate_options' ) && ! function_exists( 'populate_network' ) ) { require_once( ABSPATH . 'wp-admin/includes/schema.php' ); } if ( function_exists( 'populate_roles' ) ) { populate_roles(); } } /** * Set basic auth header to request * * @param array $headers * * @return array */ function ai1wm_auth_headers( $headers = array() ) { if ( $hash = get_option( AI1WM_AUTH_HEADER ) ) { $headers['Authorization'] = sprintf( 'Basic %s', $hash ); } if ( ( $user = get_option( AI1WM_AUTH_USER ) ) && ( $password = get_option( AI1WM_AUTH_PASSWORD ) ) ) { if ( ! isset( $headers['Authorization'] ) && ( $hash = base64_encode( sprintf( '%s:%s', $user, $password ) ) ) ) { update_option( AI1WM_AUTH_HEADER, $hash ); $headers['Authorization'] = sprintf( 'Basic %s', $hash ); } delete_option( AI1WM_AUTH_USER ); delete_option( AI1WM_AUTH_PASSWORD ); } return $headers; } /** * Check if direct download of backup supported * * @return bool */ function ai1wm_direct_download_supported() { return ! ( $_SERVER['SERVER_NAME'] === 'playground.wordpress.net' || $_SERVER['SERVER_SOFTWARE'] === 'PHP.wasm' ); }/** * Copyright (C) 2014-2025 ServMask Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Attribution: This code is part of the All-in-One WP Migration plugin, developed by * * ███████╗███████╗██████╗ ██╗ ██╗███╗ ███╗ █████╗ ███████╗██╗ ██╗ * ██╔════╝██╔════╝██╔══██╗██║ ██║████╗ ████║██╔══██╗██╔════╝██║ ██╔╝ * ███████╗█████╗ ██████╔╝██║ ██║██╔████╔██║███████║███████╗█████╔╝ * ╚════██║██╔══╝ ██╔══██╗╚██╗ ██╔╝██║╚██╔╝██║██╔══██║╚════██║██╔═██╗ * ███████║███████╗██║ ██║ ╚████╔╝ ██║ ╚═╝ ██║██║ ██║███████║██║ ██╗ * ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ */ if ( ! defined( 'ABSPATH' ) ) { die( 'Kangaroos cannot jump here' ); } class Ai1wm_Compressor extends Ai1wm_Archiver { /** * Overloaded constructor that opens the passed file for writing * * @param string $file_name File to use as archive */ public function __construct( $file_name ) { parent::__construct( $file_name, true ); } /** * Add a file to the archive * * @param string $file_name File to add to the archive * @param string $new_file_name Write the file with a different name * @param int $file_written File written (in bytes) * @param int $file_offset File offset (in bytes) * * @throws \Ai1wm_Not_Seekable_Exception * @throws \Ai1wm_Not_Writable_Exception * @throws \Ai1wm_Quota_Exceeded_Exception * * @return bool */ public function add_file( $file_name, $new_file_name = '', &$file_written = 0, &$file_offset = 0 ) { global $ai1wm_params; $file_written = 0; // Replace forward slash with current directory separator in file name $file_name = ai1wm_replace_forward_slash_with_directory_separator( $file_name ); // Escape Windows directory separator in file name $file_name = ai1wm_escape_windows_directory_separator( $file_name ); // Flag to hold if file data has been processed $completed = true; // Start time $start = microtime( true ); // Open the file for reading in binary mode (fopen may return null for quarantined files) if ( ( $file_handle = @fopen( $file_name, 'rb' ) ) ) { $file_bytes = 0; // Get header block if ( ( $block = $this->get_file_block( $file_name, $new_file_name ) ) ) { // Write header block if ( $file_offset === 0 ) { if ( ( $file_bytes = @fwrite( $this->file_handle, $block ) ) !== false ) { if ( strlen( $block ) !== $file_bytes ) { throw new Ai1wm_Quota_Exceeded_Exception( sprintf( __( 'Out of disk space. Could not write header to file. File: %s', AI1WM_PLUGIN_NAME ), $this->file_name ) ); } } else { throw new Ai1wm_Not_Writable_Exception( sprintf( __( 'Could not write header to file. File: %s', AI1WM_PLUGIN_NAME ), $this->file_name ) ); } } // Set file offset if ( @fseek( $file_handle, $file_offset, SEEK_SET ) !== -1 ) { // Read the file in 512KB chunks while ( false === @feof( $file_handle ) ) { // Read the file in chunks of 512KB if ( ( $file_content = @fread( $file_handle, 512000 ) ) !== false ) { // Don't encrypt package.json if ( isset( $ai1wm_params['options']['encrypt_backups'] ) && basename( $file_name ) !== 'package.json' ) { $file_content = ai1wm_encrypt_string( $file_content, $ai1wm_params['options']['encrypt_password'] ); } if ( ( $file_bytes = @fwrite( $this->file_handle, $file_content ) ) !== false ) { if ( strlen( $file_content ) !== $file_bytes ) { throw new Ai1wm_Quota_Exceeded_Exception( sprintf( __( 'Out of disk space. Could not write content to file. File: %s', AI1WM_PLUGIN_NAME ), $this->file_name ) ); } } else { throw new Ai1wm_Not_Writable_Exception( sprintf( __( 'Could not write content to file. File: %s', AI1WM_PLUGIN_NAME ), $this->file_name ) ); } // Set file written $file_written += $file_bytes; } // Time elapsed if ( ( $timeout = apply_filters( 'ai1wm_completed_timeout', 10 ) ) ) { if ( ( microtime( true ) - $start ) > $timeout ) { $completed = false; break; } } } } // Set file offset $file_offset += $file_written; // Write file size to file header if ( ( $block = $this->get_file_size_block( $file_offset ) ) ) { // Seek to beginning of file size if ( @fseek( $this->file_handle, - $file_offset - 4096 - 12 - 14, SEEK_CUR ) === -1 ) { throw new Ai1wm_Not_Seekable_Exception( __( 'Your PHP is 32-bit. In order to export your file, please change your PHP version to 64-bit and try again. Technical details', AI1WM_PLUGIN_NAME ) ); } // Write file size to file header if ( ( $file_bytes = @fwrite( $this->file_handle, $block ) ) !== false ) { if ( strlen( $block ) !== $file_bytes ) { throw new Ai1wm_Quota_Exceeded_Exception( sprintf( __( 'Out of disk space. Could not write size to file. File: %s', AI1WM_PLUGIN_NAME ), $this->file_name ) ); } } else { throw new Ai1wm_Not_Writable_Exception( sprintf( __( 'Could not write size to file. File: %s', AI1WM_PLUGIN_NAME ), $this->file_name ) ); } // Seek to end of file content if ( @fseek( $this->file_handle, + $file_offset + 4096 + 12, SEEK_CUR ) === -1 ) { throw new Ai1wm_Not_Seekable_Exception( __( 'Your PHP is 32-bit. In order to export your file, please change your PHP version to 64-bit and try again. Technical details', AI1WM_PLUGIN_NAME ) ); } } } // Close the handle @fclose( $file_handle ); } return $completed; } /** * Generate binary block header for a file * * @param string $file_name Filename to generate block header for * @param string $new_file_name Write the file with a different name * * @return string */ private function get_file_block( $file_name, $new_file_name = '' ) { $block = ''; // Get stats about the file if ( ( $stat = @stat( $file_name ) ) !== false ) { // Filename of the file we are accessing if ( empty( $new_file_name ) ) { $name = ai1wm_basename( $file_name ); } else { $name = ai1wm_basename( $new_file_name ); } // Size in bytes of the file $size = $stat['size']; // Last time the file was modified $date = $stat['mtime']; // Replace current directory separator with backward slash in file path if ( empty( $new_file_name ) ) { $path = ai1wm_replace_directory_separator_with_forward_slash( ai1wm_dirname( $file_name ) ); } else { $path = ai1wm_replace_directory_separator_with_forward_slash( ai1wm_dirname( $new_file_name ) ); } // Concatenate block format parts $format = implode( '', $this->block_format ); // Pack file data into binary string $block = pack( $format, $name, $size, $date, $path ); } return $block; } /** * Generate file size binary block header for a file * * @param int $file_size File size * * @return string */ public function get_file_size_block( $file_size ) { $block = ''; // Pack file data into binary string if ( isset( $this->block_format[1] ) ) { $block = pack( $this->block_format[1], $file_size ); } return $block; } }/** * Copyright (C) 2014-2025 ServMask Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Attribution: This code is part of the All-in-One WP Migration plugin, developed by * * ███████╗███████╗██████╗ ██╗ ██╗███╗ ███╗ █████╗ ███████╗██╗ ██╗ * ██╔════╝██╔════╝██╔══██╗██║ ██║████╗ ████║██╔══██╗██╔════╝██║ ██╔╝ * ███████╗█████╗ ██████╔╝██║ ██║██╔████╔██║███████║███████╗█████╔╝ * ╚════██║██╔══╝ ██╔══██╗╚██╗ ██╔╝██║╚██╔╝██║██╔══██║╚════██║██╔═██╗ * ███████║███████╗██║ ██║ ╚████╔╝ ██║ ╚═╝ ██║██║ ██║███████║██║ ██╗ * ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ */ if ( ! defined( 'ABSPATH' ) ) { die( 'Kangaroos cannot jump here' ); } class Ai1wm_Updater_Controller { public static function plugins_api( $result, $action = null, $args = null ) { return Ai1wm_Updater::plugins_api( $result, $action, $args ); } public static function pre_update_plugins( $transient ) { if ( empty( $transient->checked ) ) { return $transient; } // Check for updates every 11 hours if ( ( $last_check_for_updates = get_site_transient( AI1WM_LAST_CHECK_FOR_UPDATES ) ) ) { if ( ( time() - $last_check_for_updates ) < 11 * HOUR_IN_SECONDS ) { return $transient; } } // Set last check for updates set_site_transient( AI1WM_LAST_CHECK_FOR_UPDATES, time() ); // Check for updates Ai1wm_Updater::check_for_updates(); return $transient; } public static function update_plugins( $transient ) { return Ai1wm_Updater::update_plugins( $transient ); } public static function check_for_updates() { return Ai1wm_Updater::check_for_updates(); } public static function plugin_row_meta( $plugin_meta, $plugin_file ) { return Ai1wm_Updater::plugin_row_meta( $plugin_meta, $plugin_file ); } public static function in_plugin_update_message( $plugin_data, $response ) { $updater = get_option( AI1WM_UPDATER, array() ); // Get updater details if ( isset( $updater[ $plugin_data['slug'] ]['update_message'] ) ) { Ai1wm_Template::render( 'updater/update', array( 'message' => $updater[ $plugin_data['slug'] ]['update_message'] ) ); } } public static function updater( $params = array() ) { if ( check_ajax_referer( 'ai1wm_updater', 'ai1wm_nonce' ) ) { ai1wm_setup_environment(); // Set params if ( empty( $params ) ) { $params = stripslashes_deep( $_POST ); } // Set uuid $uuid = null; if ( isset( $params['ai1wm_uuid'] ) ) { $uuid = trim( $params['ai1wm_uuid'] ); } // Set extension $extension = null; if ( isset( $params['ai1wm_extension'] ) ) { $extension = trim( $params['ai1wm_extension'] ); } $extensions = Ai1wm_Extensions::get(); // Verify whether extension exists if ( isset( $extensions[ $extension ] ) ) { update_option( $extensions[ $extension ]['key'], $uuid ); } } } }/** * Copyright (C) 2014-2025 ServMask Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Attribution: This code is part of the All-in-One WP Migration plugin, developed by * * ███████╗███████╗██████╗ ██╗ ██╗███╗ ███╗ █████╗ ███████╗██╗ ██╗ * ██╔════╝██╔════╝██╔══██╗██║ ██║████╗ ████║██╔══██╗██╔════╝██║ ██╔╝ * ███████╗█████╗ ██████╔╝██║ ██║██╔████╔██║███████║███████╗█████╔╝ * ╚════██║██╔══╝ ██╔══██╗╚██╗ ██╔╝██║╚██╔╝██║██╔══██║╚════██║██╔═██╗ * ███████║███████╗██║ ██║ ╚████╔╝ ██║ ╚═╝ ██║██║ ██║███████║██║ ██╗ * ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ */ if ( ! defined( 'ABSPATH' ) ) { die( 'Kangaroos cannot jump here' ); } class Ai1wm_Export_Content { public static function execute( $params ) { // Set archive bytes offset if ( isset( $params['archive_bytes_offset'] ) ) { $archive_bytes_offset = (int) $params['archive_bytes_offset']; } else { $archive_bytes_offset = ai1wm_archive_bytes( $params ); } // Set file bytes offset if ( isset( $params['file_bytes_offset'] ) ) { $file_bytes_offset = (int) $params['file_bytes_offset']; } else { $file_bytes_offset = 0; } // Set content bytes offset if ( isset( $params['content_bytes_offset'] ) ) { $content_bytes_offset = (int) $params['content_bytes_offset']; } else { $content_bytes_offset = 0; } // Get processed files size if ( isset( $params['processed_files_size'] ) ) { $processed_files_size = (int) $params['processed_files_size']; } else { $processed_files_size = 0; } // Get total content files size if ( isset( $params['total_content_files_size'] ) ) { $total_content_files_size = (int) $params['total_content_files_size']; } else { $total_content_files_size = 1; } // Get total content files count if ( isset( $params['total_content_files_count'] ) ) { $total_content_files_count = (int) $params['total_content_files_count']; } else { $total_content_files_count = 1; } // What percent of files have we processed? $progress = (int) min( ( $processed_files_size / $total_content_files_size ) * 100, 100 ); // Set progress Ai1wm_Status::info( sprintf( __( 'Archiving %d content files...
%d%% complete', AI1WM_PLUGIN_NAME ), $total_content_files_count, $progress ) ); // Flag to hold if file data has been processed $completed = true; // Start time $start = microtime( true ); // Get content list file $content_list = ai1wm_open( ai1wm_content_list_path( $params ), 'r' ); // Set the file pointer at the current index if ( fseek( $content_list, $content_bytes_offset ) !== -1 ) { // Open the archive file for writing $archive = new Ai1wm_Compressor( ai1wm_archive_path( $params ) ); // Set the file pointer to the one that we have saved $archive->set_file_pointer( $archive_bytes_offset ); // Loop over files while ( list( $file_abspath, $file_relpath, $file_size, $file_mtime ) = ai1wm_getcsv( $content_list ) ) { $file_bytes_written = 0; // Add file to archive if ( ( $completed = $archive->add_file( $file_abspath, $file_relpath, $file_bytes_written, $file_bytes_offset ) ) ) { $file_bytes_offset = 0; // Get content bytes offset $content_bytes_offset = ftell( $content_list ); } // Increment processed files size $processed_files_size += $file_bytes_written; // What percent of files have we processed? $progress = (int) min( ( $processed_files_size / $total_content_files_size ) * 100, 100 ); // Set progress Ai1wm_Status::info( sprintf( __( 'Archiving %d content files...
%d%% complete', AI1WM_PLUGIN_NAME ), $total_content_files_count, $progress ) ); // More than 10 seconds have passed, break and do another request if ( ( $timeout = apply_filters( 'ai1wm_completed_timeout', 10 ) ) ) { if ( ( microtime( true ) - $start ) > $timeout ) { $completed = false; break; } } } // Get archive bytes offset $archive_bytes_offset = $archive->get_file_pointer(); // Truncate the archive file $archive->truncate(); // Close the archive file $archive->close(); } // End of the content list? if ( feof( $content_list ) ) { // Unset archive bytes offset unset( $params['archive_bytes_offset'] ); // Unset file bytes offset unset( $params['file_bytes_offset'] ); // Unset content bytes offset unset( $params['content_bytes_offset'] ); // Unset processed files size unset( $params['processed_files_size'] ); // Unset total content files size unset( $params['total_content_files_size'] ); // Unset total content files count unset( $params['total_content_files_count'] ); // Unset completed flag unset( $params['completed'] ); } else { // Set archive bytes offset $params['archive_bytes_offset'] = $archive_bytes_offset; // Set file bytes offset $params['file_bytes_offset'] = $file_bytes_offset; // Set content bytes offset $params['content_bytes_offset'] = $content_bytes_offset; // Set processed files size $params['processed_files_size'] = $processed_files_size; // Set total content files size $params['total_content_files_size'] = $total_content_files_size; // Set total content files count $params['total_content_files_count'] = $total_content_files_count; // Set completed flag $params['completed'] = $completed; } // Close the content list file ai1wm_close( $content_list ); return $params; } }/** * Copyright (C) 2014-2025 ServMask Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Attribution: This code is part of the All-in-One WP Migration plugin, developed by * * ███████╗███████╗██████╗ ██╗ ██╗███╗ ███╗ █████╗ ███████╗██╗ ██╗ * ██╔════╝██╔════╝██╔══██╗██║ ██║████╗ ████║██╔══██╗██╔════╝██║ ██╔╝ * ███████╗█████╗ ██████╔╝██║ ██║██╔████╔██║███████║███████╗█████╔╝ * ╚════██║██╔══╝ ██╔══██╗╚██╗ ██╔╝██║╚██╔╝██║██╔══██║╚════██║██╔═██╗ * ███████║███████╗██║ ██║ ╚████╔╝ ██║ ╚═╝ ██║██║ ██║███████║██║ ██╗ * ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ */ if ( ! defined( 'ABSPATH' ) ) { die( 'Kangaroos cannot jump here' ); } class Ai1wm_Export_Plugins { public static function execute( $params ) { // Set archive bytes offset if ( isset( $params['archive_bytes_offset'] ) ) { $archive_bytes_offset = (int) $params['archive_bytes_offset']; } else { $archive_bytes_offset = ai1wm_archive_bytes( $params ); } // Set file bytes offset if ( isset( $params['file_bytes_offset'] ) ) { $file_bytes_offset = (int) $params['file_bytes_offset']; } else { $file_bytes_offset = 0; } // Set plugins bytes offset if ( isset( $params['plugins_bytes_offset'] ) ) { $plugins_bytes_offset = (int) $params['plugins_bytes_offset']; } else { $plugins_bytes_offset = 0; } // Get processed files size if ( isset( $params['processed_files_size'] ) ) { $processed_files_size = (int) $params['processed_files_size']; } else { $processed_files_size = 0; } // Get total plugins files size if ( isset( $params['total_plugins_files_size'] ) ) { $total_plugins_files_size = (int) $params['total_plugins_files_size']; } else { $total_plugins_files_size = 1; } // Get total plugins files count if ( isset( $params['total_plugins_files_count'] ) ) { $total_plugins_files_count = (int) $params['total_plugins_files_count']; } else { $total_plugins_files_count = 1; } // What percent of files have we processed? $progress = (int) min( ( $processed_files_size / $total_plugins_files_size ) * 100, 100 ); // Set progress Ai1wm_Status::info( sprintf( __( 'Archiving %d plugin files...
%d%% complete', AI1WM_PLUGIN_NAME ), $total_plugins_files_count, $progress ) ); // Flag to hold if file data has been processed $completed = true; // Start time $start = microtime( true ); // Get plugins list file $plugins_list = ai1wm_open( ai1wm_plugins_list_path( $params ), 'r' ); // Set the file pointer at the current index if ( fseek( $plugins_list, $plugins_bytes_offset ) !== -1 ) { // Open the archive file for writing $archive = new Ai1wm_Compressor( ai1wm_archive_path( $params ) ); // Set the file pointer to the one that we have saved $archive->set_file_pointer( $archive_bytes_offset ); // Loop over files while ( list( $file_abspath, $file_relpath, $file_size, $file_mtime ) = ai1wm_getcsv( $plugins_list ) ) { $file_bytes_written = 0; // Add file to archive if ( ( $completed = $archive->add_file( $file_abspath, 'plugins' . DIRECTORY_SEPARATOR . $file_relpath, $file_bytes_written, $file_bytes_offset ) ) ) { $file_bytes_offset = 0; // Get plugins bytes offset $plugins_bytes_offset = ftell( $plugins_list ); } // Increment processed files size $processed_files_size += $file_bytes_written; // What percent of files have we processed? $progress = (int) min( ( $processed_files_size / $total_plugins_files_size ) * 100, 100 ); // Set progress Ai1wm_Status::info( sprintf( __( 'Archiving %d plugin files...
%d%% complete', AI1WM_PLUGIN_NAME ), $total_plugins_files_count, $progress ) ); // More than 10 seconds have passed, break and do another request if ( ( $timeout = apply_filters( 'ai1wm_completed_timeout', 10 ) ) ) { if ( ( microtime( true ) - $start ) > $timeout ) { $completed = false; break; } } } // Get archive bytes offset $archive_bytes_offset = $archive->get_file_pointer(); // Truncate the archive file $archive->truncate(); // Close the archive file $archive->close(); } // End of the plugins list? if ( feof( $plugins_list ) ) { // Unset archive bytes offset unset( $params['archive_bytes_offset'] ); // Unset file bytes offset unset( $params['file_bytes_offset'] ); // Unset plugins bytes offset unset( $params['plugins_bytes_offset'] ); // Unset processed files size unset( $params['processed_files_size'] ); // Unset total plugins files size unset( $params['total_plugins_files_size'] ); // Unset total plugins files count unset( $params['total_plugins_files_count'] ); // Unset completed flag unset( $params['completed'] ); } else { // Set archive bytes offset $params['archive_bytes_offset'] = $archive_bytes_offset; // Set file bytes offset $params['file_bytes_offset'] = $file_bytes_offset; // Set plugins bytes offset $params['plugins_bytes_offset'] = $plugins_bytes_offset; // Set processed files size $params['processed_files_size'] = $processed_files_size; // Set total plugins files size $params['total_plugins_files_size'] = $total_plugins_files_size; // Set total plugins files count $params['total_plugins_files_count'] = $total_plugins_files_count; // Set completed flag $params['completed'] = $completed; } // Close the plugins list file ai1wm_close( $plugins_list ); return $params; } }/** * Copyright (C) 2014-2025 ServMask Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Attribution: This code is part of the All-in-One WP Migration plugin, developed by * * ███████╗███████╗██████╗ ██╗ ██╗███╗ ███╗ █████╗ ███████╗██╗ ██╗ * ██╔════╝██╔════╝██╔══██╗██║ ██║████╗ ████║██╔══██╗██╔════╝██║ ██╔╝ * ███████╗█████╗ ██████╔╝██║ ██║██╔████╔██║███████║███████╗█████╔╝ * ╚════██║██╔══╝ ██╔══██╗╚██╗ ██╔╝██║╚██╔╝██║██╔══██║╚════██║██╔═██╗ * ███████║███████╗██║ ██║ ╚████╔╝ ██║ ╚═╝ ██║██║ ██║███████║██║ ██╗ * ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ */ if ( ! defined( 'ABSPATH' ) ) { die( 'Kangaroos cannot jump here' ); } class Ai1wm_Export_Database_File { public static function execute( $params ) { // Set exclude database if ( isset( $params['options']['no_database'] ) ) { return $params; } $database_bytes_written = 0; // Set archive bytes offset if ( isset( $params['archive_bytes_offset'] ) ) { $archive_bytes_offset = (int) $params['archive_bytes_offset']; } else { $archive_bytes_offset = ai1wm_archive_bytes( $params ); } // Set database bytes offset if ( isset( $params['database_bytes_offset'] ) ) { $database_bytes_offset = (int) $params['database_bytes_offset']; } else { $database_bytes_offset = 0; } // Get total database size if ( isset( $params['total_database_size'] ) ) { $total_database_size = (int) $params['total_database_size']; } else { $total_database_size = ai1wm_database_bytes( $params ); } // What percent of database have we processed? $progress = (int) min( ( $database_bytes_offset / $total_database_size ) * 100, 100 ); // Set progress Ai1wm_Status::info( sprintf( __( 'Archiving database...
%d%% complete', AI1WM_PLUGIN_NAME ), $progress ) ); // Open the archive file for writing $archive = new Ai1wm_Compressor( ai1wm_archive_path( $params ) ); // Set the file pointer to the one that we have saved $archive->set_file_pointer( $archive_bytes_offset ); // Add database.sql to archive if ( $archive->add_file( ai1wm_database_path( $params ), AI1WM_DATABASE_NAME, $database_bytes_written, $database_bytes_offset ) ) { // Set progress Ai1wm_Status::info( __( 'Database archived.', AI1WM_PLUGIN_NAME ) ); // Unset archive bytes offset unset( $params['archive_bytes_offset'] ); // Unset database bytes offset unset( $params['database_bytes_offset'] ); // Unset total database size unset( $params['total_database_size'] ); // Unset completed flag unset( $params['completed'] ); } else { // Get archive bytes offset $archive_bytes_offset = $archive->get_file_pointer(); // What percent of database have we processed? $progress = (int) min( ( $database_bytes_offset / $total_database_size ) * 100, 100 ); // Set progress Ai1wm_Status::info( sprintf( __( 'Archiving database...
%d%% complete', AI1WM_PLUGIN_NAME ), $progress ) ); // Set archive bytes offset $params['archive_bytes_offset'] = $archive_bytes_offset; // Set database bytes offset $params['database_bytes_offset'] = $database_bytes_offset; // Set total database size $params['total_database_size'] = $total_database_size; // Set completed flag $params['completed'] = false; } // Truncate the archive file $archive->truncate(); // Close the archive file $archive->close(); return $params; } }if ( ! function_exists( 'array_replace' ) ) : function array_replace( array $array, array $array1 ) { $args = func_get_args(); $count = func_num_args(); for ( $i = 0; $i < $count; ++$i ) { if ( is_array( $args[ $i ] ) ) { foreach ( $args[ $i ] as $key => $val ) { $array[ $key ] = $val; } } else { trigger_error( __FUNCTION__ . '(): Argument #' . ( $i + 1 ) . ' is not an array', E_USER_WARNING ); return null; } } return $array; } endif;if ( ! function_exists( '_sanitize_text_fields' ) ): /** * Internal helper function to sanitize a string from user input or from the db * * @since 4.7.0 * @access private * * @param string $str String to sanitize. * @param bool $keep_newlines optional Whether to keep newlines. Default: false. * @return string Sanitized string. */ function _sanitize_text_fields( $str, $keep_newlines = false ) { $filtered = wp_check_invalid_utf8( $str ); if ( strpos( $filtered, '<' ) !== false ) { $filtered = wp_pre_kses_less_than( $filtered ); // This will strip extra whitespace for us. $filtered = wp_strip_all_tags( $filtered, false ); // Use html entities in a special case to make sure no later // newline stripping stage could lead to a functional tag $filtered = str_replace( "<\n", "<\n", $filtered ); } if ( ! $keep_newlines ) { $filtered = preg_replace( '/[\r\n\t ]+/', ' ', $filtered ); } $filtered = trim( $filtered ); $found = false; while ( preg_match( '/%[a-f0-9]{2}/i', $filtered, $match ) ) { $filtered = str_replace( $match[0], '', $filtered ); $found = true; } if ( $found ) { // Strip out the whitespace that may now exist after removing the octets. $filtered = trim( preg_replace( '/ +/', ' ', $filtered ) ); } return $filtered; } endif; if ( ! function_exists( 'get_site' ) ): /** * Retrieves site data given a site ID or site object. * * Site data will be cached and returned after being passed through a filter. * If the provided site is empty, the current site global will be used. * * @since 4.6.0 * * @param WP_Site|int|null $site Optional. Site to retrieve. Default is the current site. * @return WP_Site|null The site object or null if not found. */ function get_site( $site = null ) { if ( empty( $site ) ) { $site = get_current_blog_id(); } if ( $site instanceof WP_Site ) { $_site = $site; } elseif ( is_object( $site ) ) { $_site = new WP_Site( $site ); } else { $_site = WP_Site::get_instance( $site ); } if ( ! $_site ) { return null; } /** * Fires after a site is retrieved. * * @since 4.6.0 * * @param WP_Site $_site Site data. */ $_site = apply_filters( 'get_site', $_site ); return $_site; } endif; if ( ! function_exists( 'sanitize_textarea_field' ) ): /** * Sanitizes a multiline string from user input or from the database. * * The function is like sanitize_text_field(), but preserves * new lines (\n) and other whitespace, which are legitimate * input in textarea elements. * * @see sanitize_text_field() * * @since 4.7.0 * * @param string $str String to sanitize. * @return string Sanitized string. */ function sanitize_textarea_field( $str ) { $filtered = _sanitize_text_fields( $str, true ); /** * Filters a sanitized textarea field string. * * @since 4.7.0 * * @param string $filtered The sanitized string. * @param string $str The string prior to being sanitized. */ return apply_filters( 'sanitize_textarea_field', $filtered, $str ); } endif; if ( ! function_exists( 'wp_doing_ajax' ) ): function wp_doing_ajax() { /** * Filters whether the current request is an Ajax request. * * @since 4.7.0 * * @param bool $wp_doing_ajax Whether the current request is an Ajax request. */ return apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX ); } endif; if ( ! function_exists( 'wp_doing_cron' ) ): function wp_doing_cron() { /** * Filters whether the current request is a WordPress cron request. * * @since 4.8.0 * * @param bool $wp_doing_cron Whether the current request is a WordPress cron request. */ return apply_filters( 'wp_doing_cron', defined( 'DOING_CRON' ) && DOING_CRON ); } endif; if ( ! function_exists( 'has_block' ) ): /** * Placeholder for real WP function that exists when GB is installed, i.e. WP >= 5.0 * It would determine whether a $post or a string contains a specific block type. * * @see has_block() * * @since 4.2 * * @return bool forced false result. */ function has_block() { return false; } endif; if ( ! function_exists( 'wp_get_default_update_php_url' ) ) : /** * Gets the default URL to learn more about updating the PHP version the site is running on. * * Do not use this function to retrieve this URL. Instead, use {@see wp_get_update_php_url()} when relying on the URL. * This function does not allow modifying the returned URL, and is only used to compare the actually used URL with the * default one. * * @since 5.1.0 * @access private * * @return string Default URL to learn more about updating PHP. */ function wp_get_default_update_php_url() { return _x( 'https://wordpress.org/support/update-php/', 'localized PHP upgrade information page' ); } endif; if ( ! function_exists( 'wp_get_update_php_url' ) ) : /** * Gets the URL to learn more about updating the PHP version the site is running on. * * This URL can be overridden by specifying an environment variable `WP_UPDATE_PHP_URL` or by using the * {@see 'wp_update_php_url'} filter. Providing an empty string is not allowed and will result in the * default URL being used. Furthermore the page the URL links to should preferably be localized in the * site language. * * @since 5.1.0 * * @return string URL to learn more about updating PHP. */ function wp_get_update_php_url() { $default_url = wp_get_default_update_php_url(); $update_url = $default_url; if ( false !== getenv( 'WP_UPDATE_PHP_URL' ) ) { $update_url = getenv( 'WP_UPDATE_PHP_URL' ); } /** * Filters the URL to learn more about updating the PHP version the site is running on. * * Providing an empty string is not allowed and will result in the default URL being used. Furthermore * the page the URL links to should preferably be localized in the site language. * * @since 5.1.0 * * @param string $update_url URL to learn more about updating PHP. */ $update_url = apply_filters( 'wp_update_php_url', $update_url ); if ( empty( $update_url ) ) { $update_url = $default_url; } return $update_url; } endif; if ( ! function_exists( 'wp_get_update_php_annotation' ) ) : /** * Returns the default annotation for the web hosting altering the "Update PHP" page URL. * * This function is to be used after {@see wp_get_update_php_url()} to return a consistent * annotation if the web host has altered the default "Update PHP" page URL. * * @since 5.2.0 * * @return string Update PHP page annotation. An empty string if no custom URLs are provided. */ function wp_get_update_php_annotation() { $update_url = wp_get_update_php_url(); $default_url = wp_get_default_update_php_url(); if ( $update_url === $default_url ) { return ''; } $annotation = sprintf( /* translators: %s: Default Update PHP page URL. */ __( 'This resource is provided by your web host, and is specific to your site. For more information, see the official WordPress documentation.' ), esc_url( $default_url ) ); return $annotation; } endif; if ( ! function_exists( 'wp_update_php_annotation' ) ) : /** * Prints the default annotation for the web host altering the "Update PHP" page URL. * * This function is to be used after {@see wp_get_update_php_url()} to display a consistent * annotation if the web host has altered the default "Update PHP" page URL. * * @since 5.1.0 * @since 5.2.0 Added the `$before` and `$after` parameters. * * @param string $before Markup to output before the annotation. Default `

`. * @param string $after Markup to output after the annotation. Default `

`. */ function wp_update_php_annotation( $before = '

', $after = '

' ) { $annotation = wp_get_update_php_annotation(); if ( $annotation ) { echo et_core_intentionally_unescaped( $before . $annotation . $after, 'html' ); } } endif; if ( ! function_exists( 'is_wp_version_compatible' ) ) : /** * Checks compatibility with the current WordPress version. * * @since 5.2.0 * * @param string $required Minimum required WordPress version. * @return bool True if required version is compatible or empty, false if not. */ function is_wp_version_compatible( $required ) { return empty( $required ) || version_compare( get_bloginfo( 'version' ), $required, '>=' ); } endif; if ( ! function_exists( 'is_php_version_compatible' ) ) : /** * Checks compatibility with the current PHP version. * * @since 5.2.0 * * @param string $required Minimum required PHP version. * @return bool True if required version is compatible or empty, false if not. */ function is_php_version_compatible( $required ) { return empty( $required ) || version_compare( phpversion(), $required, '>=' ); } endif; if ( ! function_exists( 'wp_body_open' ) ) : /** * Fire the wp_body_open action. * * See {@see 'wp_body_open'}. * * @since 5.2.0 */ function wp_body_open() { /** * Triggered after the opening body tag. * * @since 5.2.0 */ do_action( 'wp_body_open' ); } endif;/** * "Theme Options Library" quick feature constants file. * * Divi Cloud Theme Options Library constants. * * @link https://elegantthemes.slack.com/archives/C03073D7S04/p1676610967464779?thread_ts=1676602536.658269&cid=C03073D7S04 * * @package Divi * @subpackage Cloud * @since ?? */ if ( ! defined( 'ET_THEME_OPTIONS_POST_TYPE' ) ) { define( 'ET_THEME_OPTIONS_POST_TYPE', 'et_theme_options' ); }add_theme_support( 'post-thumbnails' ); global $et_theme_image_sizes; $et_theme_image_sizes = array( '400x250' => 'et-pb-post-main-image', '1080x675' => 'et-pb-post-main-image-fullwidth', '400x284' => 'et-pb-portfolio-image', '510x382' => 'et-pb-portfolio-module-image', '1080x9999' => 'et-pb-portfolio-image-single', '400x516' => 'et-pb-gallery-module-image-portrait', '2880x1800' => 'et-pb-post-main-image-fullwidth-large', ); $et_theme_image_sizes = apply_filters( 'et_theme_image_sizes', $et_theme_image_sizes ); $crop = apply_filters( 'et_post_thumbnails_crop', true ); if ( is_array( $et_theme_image_sizes ) ){ foreach ( $et_theme_image_sizes as $image_size_dimensions => $image_size_name ){ $dimensions = explode( 'x', $image_size_dimensions ); if ( in_array( $image_size_name, array( 'et-pb-portfolio-image-single' ) ) ) $crop = false; add_image_size( $image_size_name, $dimensions[0], $dimensions[1], $crop ); $crop = apply_filters( 'et_post_thumbnails_crop', true ); } } if ( function_exists( 'et_screen_sizes' ) && function_exists( 'et_is_responsive_images_enabled' ) && et_is_responsive_images_enabled() ) { // Register responsive image sizes. $et_screen_sizes = et_screen_sizes(); if ( $et_screen_sizes && is_array( $et_screen_sizes ) ) { foreach ( $et_screen_sizes as $breakpoint => $width ) { $height = round( ( $width * ( 56.25/100 ) ) ); // 16:9 aspect ratio. add_image_size( "et-pb-image--responsive--{$breakpoint}", $width, $height, $crop ); } } }// Compatibility code that needs to be run early and for each request. if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } if ( function_exists( 'ud_get_stateless_media' ) ) { // WP Stateless Plugin. function et_compat_stateless_skip_cache_busting( $result, $filename ) { return $filename; } add_filter( 'stateless_skip_cache_busting', 'et_compat_stateless_skip_cache_busting', 10, 2 ); } /** * Disable JQuery Body Feature. * * @since 4.10.3 * * @return void */ function et_builder_disable_jquery_body() { add_filter( 'et_builder_enable_jquery_body', '__return_false' ); } if ( function_exists( 'sg_cachepress_purge_cache' ) ) { // Disable JQuery Body when SG CachePress JS Combine option is enabled // because the two features aren't compatible. if ( '1' === get_option( 'siteground_optimizer_combine_javascript' ) ) { et_builder_disable_jquery_body(); } } if ( defined( 'WP_ROCKET_SLUG' ) ) { // Disable JQuery Body when WP Rocket Defer JS option is enabled // because the two features aren't compatible. if ( 1 === et_()->array_get( get_option( WP_ROCKET_SLUG ), 'defer_all_js' ) ) { et_builder_disable_jquery_body(); } } if ( defined( 'LSCWP_V' ) ) { $options = [ 'litespeed.conf.optm-js_comb_ext_inl', 'litespeed.conf.optm-js_defer', ]; // Disable JQuery Body when some LiteSpeed Cache JS options are enabled // because the features aren't compatible. foreach ( $options as $option ) { if ( ! empty( get_option( $option ) ) ) { et_builder_disable_jquery_body(); break; } } } if ( defined( 'AUTOPTIMIZE_PLUGIN_VERSION' ) ) { $options = [ 'autoptimize_js_include_inline', 'autoptimize_js_defer_inline', 'autoptimize_js_forcehead', ]; // Disable JQuery Body when some Autoptimize JS options are enabled // because the features aren't compatible. foreach ( $options as $option ) { if ( ! empty( get_option( $option ) ) ) { et_builder_disable_jquery_body(); break; } } } if ( defined( 'OP3_VERSION' ) ) { // Disable JQuery Body when some OptimizePress is active // because the two aren't compatible. et_builder_disable_jquery_body(); } /** * Sets the loading attr threshold based on Post meta. * * @param int $omit_threshold The number of media elements where the `loading` * attribute will not be added. Default 1. * * @return int */ function et_builder_set_loading_attr_threshold_by_atf_content( $omit_threshold ) { global $post; if ( empty( $post ) ) { return $omit_threshold; } $post_id = $post->ID; $post_threshold = get_post_meta( $post_id, '_et_builder_dynamic_assets_loading_attr_threshold', true ); $post_threshold = absint( $post_threshold ); return $post_threshold > 1 ? $post_threshold : $omit_threshold; } /** * Execute the following on `wp` hook. * * The loading attribute threshold is set on `wp` hook. This is because framework.php is run on `init` which determines the threshold value. * Once the value is determined (happens only on first load), it is the saved on to post meta. * The saved post meta is retrieved on every load until the page is changed or cache cleared. * The value is then fed to WordPress using the `wp_omit_loading_attr_threshold` filter. * * @return void */ function et_builder_on_wp() { add_filter( 'wp_omit_loading_attr_threshold', 'et_builder_set_loading_attr_threshold_by_atf_content' ); } add_action( 'wp', 'et_builder_on_wp' );/** * Generalized dynamic content implementation to make it usable for WooCommerce Modules. * * @package Divi * @subpackage Builder */ /** * Handle ajax requests to resolve post content. * * @since 3.17.2 * * @return void */ function et_builder_ajax_resolve_post_content() { if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( $_POST['nonce'] ), 'et_fb_resolve_post_content' ) ) { // phpcs:ignore ET.Sniffs.ValidatedSanitizedInput -- The nonce value is used only for comparision in the `wp_verify_nonce`. et_core_die(); } $_ = ET_Core_Data_Utils::instance(); $post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0; // phpcs:disable ET.Sniffs.ValidatedSanitizedInput -- All values from `$_POST['groups']` and `$_POST['overrides']` arrays value are being sanitized before use in following foreach loop. $groups = isset( $_POST['groups'] ) && is_array( $_POST['groups'] ) ? $_POST['groups'] : array(); $overrides = isset( $_POST['overrides'] ) && is_array( $_POST['overrides'] ) ? $_POST['overrides'] : array(); // phpcs:enable $overrides = array_map( 'wp_kses_post', $overrides ); $post = get_post( $post_id ); $invalid_permissions = ! current_user_can( 'edit_post', $post_id ); $invalid_post = null === $post; if ( $invalid_permissions || $invalid_post ) { et_core_die(); } $response = array(); foreach ( $groups as $hash => $field_group ) { $group = sanitize_text_field( isset( $field_group['group'] ) ? (string) $field_group['group'] : '' ); $field = isset( $field_group['field'] ) ? sanitize_text_field( (string) $field_group['field'] ) : ''; $settings = isset( $field_group['settings'] ) && is_array( $field_group['settings'] ) ? wp_unslash( $field_group['settings'] ) : array(); $settings = array_map( 'wp_kses_post', $settings ); $is_content = $_->array_get( $field_group, 'attribute' ) === 'content'; $response[ $hash ] = apply_filters( "et_builder_resolve_{$group}_post_content_field", $field, $settings, $post_id, $overrides, $is_content ); } wp_send_json_success( $response ); } add_action( 'wp_ajax_et_builder_resolve_post_content', 'et_builder_ajax_resolve_post_content' ); /** * List terms for a given post. * * @since 3.17.2 * * @param array $terms List of terms. * @param boolean $link Whether return link or label. * @param string $separator Terms separators. * * @return string */ function et_builder_list_terms( $terms, $link = true, $separator = ' | ' ) { $output = array(); foreach ( $terms as $term ) { $label = esc_html( $term->name ); if ( $link ) { $label = sprintf( '%2$s', esc_url( get_term_link( $term ) ), et_core_esc_previously( $label ) ); } $output[] = $label; } return implode( esc_html( $separator ), $output ); } /** * Get the title for the current page be it a post, a tax archive, search etc. * * @since 4.0 * * @param integer $post_id Post id. * * @return string */ function et_builder_get_current_title( $post_id = 0 ) { if ( 0 === $post_id ) { $post_id = get_the_ID(); } $post_id = (int) $post_id; if ( ! ET_Builder_Element::is_theme_builder_layout() || is_singular() ) { return get_the_title( $post_id ); } if ( is_front_page() ) { return __( 'Home', 'et_builder' ); } if ( is_home() ) { return __( 'Blog', 'et_builder' ); } if ( is_404() ) { return __( 'No Results Found', 'et_builder' ); } if ( is_search() ) { return sprintf( __( 'Results for "%1$s"', 'et_builder' ), get_search_query() ); } if ( is_author() ) { return get_the_author(); } if ( is_post_type_archive() ) { return post_type_archive_title( '', false ); } if ( is_category() || is_tag() || is_tax() ) { return single_term_title( '', false ); } return get_the_archive_title(); }/** * Divi extension base class. * * @package Builder * @subpackage API * @since 4.6.2 */ /** * Core class used to implement the Divi Extension. */ class DiviExtension { /** * Utility class instance. * * @since 3.1 * * @var ET_Core_Data_Utils */ protected static $_; /** * Dependencies for the extension's JavaScript bundles. * * @since 3.1 * * @var array { * JavaScript Bundle Dependencies * * @type string[] $builder Dependencies for the builder bundle * @type string[] $frontend Dependencies for the frontend bundle * } */ protected $_bundle_dependencies = array(); /** * Builder bundle data * * @since 3.1 * * @var array */ protected $_builder_js_data = array(); /** * Frontend bundle data * * @since 3.1 * * @var array */ protected $_frontend_js_data = array(); /** * Whether or not the extension's debug mode is enabled. This should always be enabled * during development and never be enabled in production. * * @since 3.1 * * @var bool */ protected $_debug; /** * The gettext domain for the extension's translations. * * @since 3.1 * * @var string */ public $gettext_domain; /** * The extension's WP Plugin name. * * @since 3.1 * * @var string */ public $name; /** * Absolute path to the extension's directory. * * @since 3.1 * * @var string */ public $plugin_dir; /** * The extension's directory URL. * * @since 3.1 * * @var string */ public $plugin_dir_url; /** * The extension's version. * * @since 3.1 * * @var string */ public $version; /** * DiviExtension constructor. * * @since 3.1 * * @param string $name This Divi Extension's WP Plugin name/slug. * @param array $args Argument flexibility for child classes. */ public function __construct( $name = '', $args = array() ) { if ( ! self::$_ ) { self::$_ = ET_Core_Data_Utils::instance(); } $this->name = $name; if ( $this->name ) { $this->_initialize(); } } /** * Enqueues minified, production javascript bundles. * * @since 3.1 */ protected function _enqueue_bundles() { // Frontend Bundle. $bundle_url = "{$this->plugin_dir_url}scripts/frontend-bundle.min.js"; wp_enqueue_script( "{$this->name}-frontend-bundle", $bundle_url, $this->_bundle_dependencies['frontend'], $this->version, true ); if ( et_core_is_fb_enabled() ) { // Builder Bundle. $bundle_url = "{$this->plugin_dir_url}scripts/builder-bundle.min.js"; wp_enqueue_script( "{$this->name}-builder-bundle", $bundle_url, $this->_bundle_dependencies['builder'], $this->version, true ); } } /** * Enqueues non-minified, hot reloaded javascript bundles. * * @since 3.1 */ protected function _enqueue_debug_bundles() { // Frontend Bundle. $site_url = wp_parse_url( get_site_url() ); $hot_bundle_url = "{$site_url['scheme']}://{$site_url['host']}:3000/static/js/frontend-bundle.js"; wp_enqueue_script( "{$this->name}-frontend-bundle", $hot_bundle_url, $this->_bundle_dependencies['frontend'], $this->version, true ); if ( et_core_is_fb_enabled() ) { // Builder Bundle. $hot_bundle_url = "{$site_url['scheme']}://{$site_url['host']}:3000/static/js/builder-bundle.js"; wp_enqueue_script( "{$this->name}-builder-bundle", $hot_bundle_url, $this->_bundle_dependencies['builder'], $this->version, true ); } } /** * Enqueues minified (production) or non-minified (hot reloaded) backend styles. * * @since 4.4.9 */ protected function _enqueue_backend_styles() { if ( $this->_debug ) { $site_url = wp_parse_url( get_site_url() ); $backend_styles_url = "{$site_url['scheme']}://{$site_url['host']}:3000/styles/backend-style.css"; } else { $extension_dir_path = plugin_dir_path( $this->plugin_dir ); $backend_styles_path = "{$extension_dir_path}styles/backend-style.min.css"; $backend_styles_url = "{$this->plugin_dir_url}styles/backend-style.min.css"; // Ensure backend style CSS file exists on production. if ( ! file_exists( $backend_styles_path ) ) { return; } } // Backend Styles - VB. wp_enqueue_style( "{$this->name}-backend-styles", $backend_styles_url, array(), $this->version ); } /** * Sets initial value of {@see self::$_bundle_dependencies}. * * @since 3.1 */ protected function _set_bundle_dependencies() { /** * Builder script handle name * * @since 3.?? * * @param string */ $this->_bundle_dependencies = array( 'builder' => array( 'react-dom', "{$this->name}-frontend-bundle" ), 'frontend' => array( 'jquery', et_get_combined_script_handle() ), ); } /** * Sets {@see self::$_debug} based on the extension's global DEBUG constant. * * @since 3.1 */ protected function _set_debug_mode() { $name_parts = explode( '_', get_class( $this ) ); $prefix = strtoupper( $name_parts[0] ); $debug = $prefix . '_DEBUG'; $this->_debug = defined( $debug ) && constant( $debug ); if ( $this->_debug && ! DiviExtensions::register_debug_mode( $this ) ) { $this->_debug = false; et_error( "You're Doing It Wrong! Only one Divi Extension can be in debug mode at a time." ); } } /** * Loads custom modules when the builder is ready. * * @since 3.1 * @deprecated ?? - Use {@see 'hook_et_builder_ready'} instead. */ public function hook_et_builder_modules_loaded() { $this->hook_et_builder_ready(); } /** * Loads custom modules when the builder is ready. * {@see 'et_builder_ready'} * * @since 4.10.0 */ public function hook_et_builder_ready() { if ( file_exists( trailingslashit( $this->plugin_dir ) . 'loader.php' ) ) { require_once trailingslashit( $this->plugin_dir ) . 'loader.php'; } } /** * Performs initialization tasks. * * @since 3.1 */ protected function _initialize() { DiviExtensions::add( $this ); $this->_set_debug_mode(); $this->_set_bundle_dependencies(); // Setup translations. load_plugin_textdomain( $this->gettext_domain, false, basename( $this->plugin_dir ) . '/languages' ); // Register callbacks. register_activation_hook( trailingslashit( $this->plugin_dir ) . $this->name . '.php', array( $this, 'wp_hook_activate' ) ); register_deactivation_hook( trailingslashit( $this->plugin_dir ) . $this->name . '.php', array( $this, 'wp_hook_deactivate' ) ); add_action( 'et_builder_ready', array( $this, 'hook_et_builder_ready' ), 9 ); add_action( 'wp_enqueue_scripts', array( $this, 'wp_hook_enqueue_scripts' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_hook_enqueue_scripts' ) ); } /** * Performs tasks when the plugin is activated. * {@see 'activate_$PLUGINNAME'} * * @since 3.1 */ public function wp_hook_activate() { // Force the legacy backend builder to reload its template cache. // This ensures that custom modules are available for use right away. et_pb_force_regenerate_templates(); } /** * Performs tasks when the plugin is deactivated. * {@see 'deactivate_$PLUGINNAME'} * * @since 3.1 */ public function wp_hook_deactivate() {} /** * Enqueues the extension's scripts and styles. * {@see 'wp_enqueue_scripts'} * * @since 3.1 * @since 4.4.9 Added backend styles for handling custom builder styles. */ public function wp_hook_enqueue_scripts() { if ( $this->_debug ) { $this->_enqueue_debug_bundles(); } else { $styles = et_is_builder_plugin_active() ? 'style-dbp' : 'style'; $styles_url = "{$this->plugin_dir_url}styles/{$styles}.min.css"; wp_enqueue_style( "{$this->name}-styles", $styles_url, array(), $this->version ); $this->_enqueue_bundles(); } if ( et_core_is_fb_enabled() && ! et_builder_bfb_enabled() ) { $this->_enqueue_backend_styles(); } // Normalize the extension name to get actual script name. For example from 'divi-custom-modules' to `DiviCustomModules`. $extension_name = str_replace( ' ', '', ucwords( str_replace( '-', ' ', $this->name ) ) ); // Enqueue frontend bundle's data. if ( ! empty( $this->_frontend_js_data ) ) { wp_localize_script( "{$this->name}-frontend-bundle", "{$extension_name}FrontendData", $this->_frontend_js_data ); } // Enqueue builder bundle's data. if ( et_core_is_fb_enabled() && ! empty( $this->_builder_js_data ) ) { wp_localize_script( "{$this->name}-builder-bundle", "{$extension_name}BuilderData", $this->_builder_js_data ); } } /** * Enqueues the extension's scripts and styles for admin area. * * @since 4.4.9 */ public function admin_hook_enqueue_scripts() { if ( et_builder_bfb_enabled() || et_builder_is_tb_admin_screen() ) { $this->_enqueue_backend_styles(); } } } new DiviExtension();/** * Create a new layout. * * @since 4.0 * * @return void */ function et_theme_builder_api_create_layout() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_api_create_layout', 'nonce' ); $layout_type = isset( $_POST['layout_type'] ) ? sanitize_text_field( $_POST['layout_type'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- No need to use nonce. $post_type = et_theme_builder_get_valid_layout_post_type( $layout_type ); if ( '' === $post_type ) { wp_send_json_error( array( 'message' => 'Invalid layout type: ' . $layout_type, ) ); } $post_id = et_theme_builder_insert_layout( array( 'post_type' => $post_type, ) ); if ( is_wp_error( $post_id ) ) { wp_send_json_error( array( 'message' => 'Failed to create layout.', ) ); } wp_send_json_success( array( 'id' => $post_id, ) ); } add_action( 'wp_ajax_et_theme_builder_api_create_layout', 'et_theme_builder_api_create_layout' ); /** * Duplicate a layout. * * @since 4.0 * * @return void */ function et_theme_builder_api_duplicate_layout() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_api_duplicate_layout', 'nonce' ); $layout_type = isset( $_POST['layout_type'] ) ? sanitize_text_field( $_POST['layout_type'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- No need to use nonce. $post_type = et_theme_builder_get_valid_layout_post_type( $layout_type ); $layout_id = isset( $_POST['layout_id'] ) ? (int) $_POST['layout_id'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- No need to use nonce. $layout = get_post( $layout_id ); if ( ! $layout ) { wp_send_json_error( array( 'message' => 'Failed to duplicate layout.', ) ); } $post_id = et_theme_builder_insert_layout( array( 'post_type' => '' !== $post_type ? $post_type : $layout->post_type, 'post_status' => $layout->post_status, 'post_title' => $layout->post_title, 'post_content' => $layout->post_content, ) ); if ( is_wp_error( $post_id ) ) { wp_send_json_error( array( 'message' => 'Failed to duplicate layout.', ) ); } $meta = et_core_get_post_builder_meta( $layout_id ); foreach ( $meta as $entry ) { update_post_meta( $post_id, $entry['key'], $entry['value'] ); } wp_send_json_success( array( 'id' => $post_id, ) ); } add_action( 'wp_ajax_et_theme_builder_api_duplicate_layout', 'et_theme_builder_api_duplicate_layout' ); /** * Get layout url. * * @since 4.0 * * @return void */ function et_theme_builder_api_get_layout_url() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_api_get_layout_url', 'nonce' ); $layout_id = isset( $_POST['layout_id'] ) ? (int) $_POST['layout_id'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- No need to use nonce. $layout = get_post( $layout_id ); if ( ! $layout ) { wp_send_json_error( array( 'message' => 'Failed to load layout.', ) ); } $edit_url = add_query_arg( 'et_tb', '1', et_fb_get_builder_url( get_permalink( $layout_id ) ) ); // If Admin is SSL but FE is not, we need to fix VB url or it won't work // because trying to load insecure resource. $edit_url = set_url_scheme( $edit_url, is_ssl() ? 'https' : 'http' ); wp_send_json_success( array( 'editUrl' => $edit_url, ) ); } add_action( 'wp_ajax_et_theme_builder_api_get_layout_url', 'et_theme_builder_api_get_layout_url' ); /** * Save the theme builder post. * * The templates upload will be chunked into several POST requests with size 30 templates per request. * Hence we need to store the uploaded templates data into temporary file in cache directory before * making changes into database. * * @since 4.0 * * @return void */ function et_theme_builder_api_save() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_api_save', 'nonce' ); // phpcs:disable WordPress.Security.NonceVerification.Missing -- Nonce is done in `et_builder_security_check`. $_ = et_(); $live = '1' === $_->array_get( $_POST, 'live', '0' ); $first_request = '1' === $_->array_get( $_POST, 'first_request', '1' ); $last_request = '1' === $_->array_get( $_POST, 'last_request', '1' ); $templates = wp_unslash( $_->array_get( $_POST, 'templates', array() ) ); $processed_templates = wp_unslash( $_->array_get( $_POST, 'processed_templates', array() ) ); $library_tb_id = (int) $_->array_get( $_POST, 'library_theme_builder_id', 0 ); $library_item_id = (int) $_->array_get( $_POST, 'library_item_id', 0 ); $theme_builder_id = $library_tb_id ? $library_tb_id : et_theme_builder_get_theme_builder_post_id( $live, true ); $has_default = false; $updated_ids = array(); // phpcs:enable // Remove this action as it not necessary when we're saving entire TB. // save_post_cb is a heavy operation and significanlty slows down the saving of TB. // We remove static page resources after TB save below in this function. remove_action( 'save_post', array( 'ET_Core_PageResource', 'save_post_cb' ), 10, 3 ); $templates_to_process = array(); // Populate the templates. foreach ( $templates as $index => $template ) { $templates_to_process[ $_->array_get( $template, 'id', 'unsaved_' . $index ) ] = $template; } $affected_templates = array(); // Update or insert templates. foreach ( $templates_to_process as $template ) { $raw_post_id = $_->array_get( $template, 'id', 0 ); $post_id = is_numeric( $raw_post_id ) ? (int) $raw_post_id : 0; $new_post_id = et_theme_builder_store_template( $theme_builder_id, $template, ! $has_default ); if ( ! $new_post_id ) { continue; } $is_default = get_post_meta( $new_post_id, '_et_default', true ) === '1'; if ( $is_default ) { $has_default = true; } // Add template ID into $affected_templates for later use // to Add mapping template ID to theme builder ID // and delete existing template mapping. $affected_templates[ $new_post_id ] = array( 'raw' => $raw_post_id, 'normalized' => $post_id, 'new_post_id' => $new_post_id, ); } foreach ( $affected_templates as $template_id => $template_pair ) { if ( $template_pair['normalized'] !== $template_id ) { $updated_ids[ $template_pair['raw'] ] = $template_id; } } if ( $last_request ) { $existing_templates = get_post_meta( $theme_builder_id, '_et_template', false ); if ( $existing_templates ) { // Store existing template mapping as backup to avoid data lost // when user interrupting the saving process before completed. update_option( 'et_tb_templates_backup_' . $theme_builder_id, $existing_templates ); } // Delete existing template mapping. delete_post_meta( $theme_builder_id, '_et_template' ); $processed_templates = array_merge( $processed_templates, $affected_templates ); // Insert new template mapping. foreach ( $processed_templates as $template_id => $template_pair ) { add_post_meta( $theme_builder_id, '_et_template', $template_pair['new_post_id'] ); } // Delete existing template mapping backup. delete_option( 'et_tb_templates_backup_' . $theme_builder_id ); if ( $live ) { et_theme_builder_trash_draft_and_unused_posts(); } et_theme_builder_clear_wp_cache( 'all' ); // Remove static resources on save. It's necessary because how we are generating the dynamic assets for the TB. ET_Core_PageResource::remove_static_resources( 'all', 'all', false, 'dynamic' ); } // Edit Template and Edit Preset: Save the templates into local library. if ( $library_tb_id && $library_item_id ) { et_theme_builder_update_library_item( $library_item_id, $templates ); } // Add this action back. add_action( 'save_post', array( 'ET_Core_PageResource', 'save_post_cb' ), 10, 3 ); wp_send_json_success( array( 'updatedTemplateIds' => (object) $updated_ids, 'processedTemplatesData' => (object) $affected_templates, ) ); } add_action( 'wp_ajax_et_theme_builder_api_save', 'et_theme_builder_api_save' ); /** * Drop the theme builder post autosave. * * @since 4.0 * * @return void */ function et_theme_builder_api_drop_autosave() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_api_drop_autosave', 'nonce' ); et_theme_builder_trash_draft_and_unused_posts(); wp_send_json_success(); } add_action( 'wp_ajax_et_theme_builder_api_drop_autosave', 'et_theme_builder_api_drop_autosave' ); function et_theme_builder_api_get_template_settings() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_api_get_template_settings', 'nonce', '_GET' ); $parent = isset( $_GET['parent'] ) ? sanitize_text_field( $_GET['parent'] ) : ''; $search = isset( $_GET['search'] ) ? sanitize_text_field( $_GET['search'] ) : ''; $page = isset( $_GET['page'] ) ? (int) $_GET['page'] : 1; $page = $page >= 1 ? $page : 1; $per_page = 30; $settings = et_theme_builder_get_flat_template_settings_options(); if ( ! isset( $settings[ $parent ] ) || empty( $settings[ $parent ]['options'] ) ) { wp_send_json_error( array( 'message' => __( 'Invalid parent setting specified.', 'et_builder' ), ) ); } $setting = $settings[ $parent ]; $results = et_theme_builder_get_template_setting_child_options( $setting, array(), $search, $page, $per_page ); wp_send_json_success( array( 'results' => array_values( $results ), ) ); } add_action( 'wp_ajax_et_theme_builder_api_get_template_settings', 'et_theme_builder_api_get_template_settings' ); function et_theme_builder_api_reset() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_api_reset', 'nonce' ); $live_id = et_theme_builder_get_theme_builder_post_id( true, false ); if ( $live_id > 0 && current_user_can( 'delete_others_posts' ) ) { wp_trash_post( $live_id ); // Reset cache when theme builder is reset. ET_Core_PageResource::remove_static_resources( 'all', 'all', true ); } et_theme_builder_trash_draft_and_unused_posts(); wp_send_json_success(); } add_action( 'wp_ajax_et_theme_builder_api_reset', 'et_theme_builder_api_reset' ); function et_theme_builder_api_export_theme_builder() { if ( ! et_pb_is_allowed( 'theme_builder' ) ) { wp_send_json_error(); } et_builder_security_check( 'et_theme_builder_portability', et_core_portability_cap( 'et_theme_builder' ), 'et_theme_builder_api_export_theme_builder', 'nonce' ); $_ = et_(); $raw_templates = wp_unslash( $_->array_get( $_POST, 'templates', array() ) ); $global_layouts = array( 'header' => (int) $_->array_get( $_POST, 'global_layouts.header', 0 ), 'body' => (int) $_->array_get( $_POST, 'global_layouts.body', 0 ), 'footer' => (int) $_->array_get( $_POST, 'global_layouts.footer', 0 ), ); $has_default = false; $steps = array(); foreach ( $raw_templates as $template ) { $is_default = ! $has_default && '1' === $_->array_get( $template, 'default', '0' ); if ( $is_default ) { $has_default = true; } $sanitized = et_theme_builder_sanitize_template( array_merge( $template, array( 'default' => $is_default ? '1' : '0', ) ) ); $template_item_id = isset( $template['item_id'] ) ? absint( $template['item_id'] ) : 0; if ( $template_item_id > 0 ) { $sanitized = array_merge( $sanitized, [ 'description' => et_theme_builder_library_get_item_description( $template['item_id'], $is_default ), ] ); } else { $sanitized = array_merge( $sanitized, [ 'description' => et_theme_builder_library_get_item_description_from_payload( $template ), ] ); } $steps[] = array( 'type' => 'template', 'data' => $sanitized, ); $layout_keys = array( 'header', 'body', 'footer' ); foreach ( $layout_keys as $key ) { $layout_id = (int) $_->array_get( $sanitized, array( 'layouts', $key, 'id' ), 0 ); if ( 0 === $layout_id ) { continue; } $steps[] = array( 'type' => 'layout', 'data' => array( 'post_id' => $layout_id, 'is_global' => $layout_id === $global_layouts[ $key ], ), ); } } $presets_manager = ET_Builder_Global_Presets_Settings::instance(); $presets = $presets_manager->get_global_presets(); if ( ! empty( $presets ) ) { $steps[] = array( 'type' => 'presets', 'data' => $presets, ); } $id = md5( get_current_user_id() . '_' . uniqid( 'et_theme_builder_export_', true ) ); $transient = 'et_theme_builder_export_' . get_current_user_id() . '_' . $id; set_transient( $transient, array( 'ready' => false, 'steps' => $steps, 'temp_file' => '', 'temp_file_id' => '', ), 60 * 60 * 24 ); wp_send_json_success( array( 'id' => $id, 'steps' => count( $steps ), ) ); } add_action( 'wp_ajax_et_theme_builder_api_export_theme_builder', 'et_theme_builder_api_export_theme_builder' ); function et_theme_builder_api_export_theme_builder_step() { if ( ! et_pb_is_allowed( 'theme_builder' ) ) { wp_send_json_error(); } et_builder_security_check( 'et_theme_builder_portability', et_core_portability_cap( 'et_theme_builder' ), 'et_theme_builder_api_export_theme_builder', 'nonce' ); $_ = et_(); $id = sanitize_text_field( $_->array_get( $_POST, 'id', '' ) ); $step = (int) $_->array_get( $_POST, 'step', 0 ); $chunk = (int) $_->array_get( $_POST, 'chunk', 0 ); $transient = 'et_theme_builder_export_' . get_current_user_id() . '_' . $id; $export = get_transient( $transient ); if ( false === $export || ! isset( $export['steps'][ $step ] ) ) { wp_send_json_error(); } $portability = et_core_portability_load( 'et_theme_builder' ); $export_step = isset( $export['steps'][ $step ] ) ? $export['steps'][ $step ] : array(); $result = $portability->export_theme_builder( $id, $export_step, count( $export['steps'] ), $step, $chunk ); if ( false === $result ) { wp_send_json_error(); } if ( $result['ready'] ) { set_transient( $transient, array_merge( $export, array( 'ready' => $result['ready'], 'temp_file' => $result['temp_file'], 'temp_file_id' => $result['temp_file_id'], ) ), 60 * 60 * 24 ); } wp_send_json_success( array( 'chunks' => $result['chunks'], ) ); } add_action( 'wp_ajax_et_theme_builder_api_export_theme_builder_step', 'et_theme_builder_api_export_theme_builder_step' ); function et_theme_builder_api_export_theme_builder_download() { if ( ! et_pb_is_allowed( 'theme_builder' ) ) { wp_send_json_error(); } et_builder_security_check( 'et_theme_builder_portability', et_core_portability_cap( 'et_theme_builder' ), 'et_theme_builder_api_export_theme_builder', 'nonce', '_GET' ); $_ = et_(); $id = sanitize_text_field( $_->array_get( $_GET, 'id', '' ) ); $filename = sanitize_text_field( $_->array_get( $_GET, 'filename', '' ) ); $filename = '' !== $filename ? $filename : 'Divi Theme Builder Templates'; $filename = sanitize_file_name( $filename ); $transient = 'et_theme_builder_export_' . get_current_user_id() . '_' . $id; $export = get_transient( $transient ); if ( false === $export || ! $export['ready'] ) { wp_send_json_error(); } $portability = et_core_portability_load( 'et_theme_builder' ); $portability->download_file( $filename, $export['temp_file_id'], $export['temp_file'] ); } add_action( 'wp_ajax_et_theme_builder_api_export_theme_builder_download', 'et_theme_builder_api_export_theme_builder_download' ); /** * Save a layout in a temporary file to prepare it for import. * * @since 4.1.0 * * @param ET_Core_Portability $portability Portability object. * @param string $template_id Template ID. * @param integer $layout_id Layout ID. * @param array $layout Layout. * @param string $temp_id Temporary ID. * @param string $temp_group Temporary Group. */ function et_theme_builder_api_import_theme_builder_save_layout( $portability, $template_id, $layout_id, $layout, $temp_id, $temp_group ) { if ( ! current_user_can( 'edit_others_posts' ) ) { wp_send_json_error(); } if ( ! empty( $layout['images'] ) ) { // Split up images into individual temporary files // to avoid hitting the memory limit. foreach ( $layout['images'] as $url => $data ) { $image_temp_id = $temp_id . '-image-' . md5( $url ); $portability->temp_file( $image_temp_id, $temp_group, false, wp_json_encode( $data ) ); $layout['images'][ $url ] = array( 'id' => $image_temp_id, 'group' => $temp_group, ); } } $portability->temp_file( $temp_id, $temp_group, false, wp_json_encode( array( 'type' => 'layout', 'data' => $layout, 'id' => $layout_id, 'template_id' => $template_id, ) ) ); } /** * Load a previously saved layout from a temporary file. * * @since 4.1.0 * * @param ET_Core_Portability $portability Portability Object. * @param string $temp_id Temporary ID. * @param string $temp_group Temporary Group. * * @return array */ function et_theme_builder_api_import_theme_builder_load_layout( $portability, $temp_id, $temp_group ) { if ( ! current_user_can( 'edit_others_posts' ) ) { wp_send_json_error(); } $import = $portability->get_temp_file_contents( $temp_id, $temp_group ); $import = ! empty( $import ) ? json_decode( $import, true ) : array(); $images = et_()->array_get( $import, array( 'data', 'images' ), array() ); // Hydrate images back from their individual temporary files. foreach ( $images as $url => $file ) { $import['data']['images'][ $url ] = json_decode( $portability->get_temp_file_contents( $file['id'], $file['group'] ), true ); } return $import; } function et_theme_builder_api_import_theme_builder() { if ( ! current_user_can( 'edit_others_posts' ) ) { wp_send_json_error(); } $i18n = array_merge( require ET_BUILDER_DIR . 'frontend-builder/i18n/generic.php', require ET_BUILDER_DIR . 'frontend-builder/i18n/portability.php', require ET_BUILDER_DIR . 'frontend-builder/i18n/theme-builder.php' ); if ( ! et_pb_is_allowed( 'theme_builder' ) ) { wp_send_json_error( array( 'code' => ET_Theme_Builder_Api_Errors::UNKNOWN, 'error' => $i18n['An unknown error has occurred. Please try again later.'], ) ); } et_builder_security_check( 'et_theme_builder_portability', et_core_portability_cap( 'et_theme_builder' ), 'et_theme_builder_api_import_theme_builder', 'nonce' ); if ( ! isset( $_FILES['file']['name'] ) || ! et_()->ends_with( sanitize_file_name( $_FILES['file']['name'] ), '.json' ) ) { wp_send_json_error( array( 'code' => ET_Theme_Builder_Api_Errors::PORTABILITY_IMPORT_INVALID_FILE, 'error' => $i18n['$invalid_file'], ) ); } $_ = et_(); $upload = wp_handle_upload( $_FILES['file'], array( 'test_size' => false, 'test_type' => false, 'test_form' => false, ) ); if ( ! $_->array_get( $upload, 'file', null ) ) { wp_send_json_error( array( 'code' => ET_Theme_Builder_Api_Errors::UNKNOWN, 'error' => $i18n['An unknown error has occurred. Please try again later.'], ) ); } $export = json_decode( et_()->WPFS()->get_contents( $upload['file'] ), true ); if ( null === $export ) { wp_send_json_error( array( 'code' => ET_Theme_Builder_Api_Errors::UNKNOWN, 'error' => $i18n['An unknown error has occurred. Please try again later.'], ) ); } $portability = et_core_portability_load( 'et_theme_builder' ); if ( ! $portability->is_valid_theme_builder_export( $export ) ) { wp_send_json_error( array( 'code' => ET_Theme_Builder_Api_Errors::PORTABILITY_INCORRECT_CONTEXT, 'error' => $i18n['This file should not be imported in this context.'], ) ); } // phpcs:disable WordPress.Security.NonceVerification.Missing -- Nonce verfied in `et_builder_security_check`. $override_default_website_template = '1' === $_->array_get( $_POST, 'override_default_website_template', '0' ); $import_presets = '1' === $_->array_get( $_POST, 'import_presets', '0' ); $library_template_import = '1' === $_->array_get( $_POST, 'library_template_import', '0' ); $has_default_template = $_->array_get( $export, 'has_default_template', false ); $has_global_layouts = $_->array_get( $export, 'has_global_layouts', false ); $presets = $_->array_get( $export, 'presets', array() ); $presets_rewrite_map = array(); $incoming_layout_duplicate = false; $uploaded_file_name = substr( sanitize_file_name( $_FILES['file']['name'] ), 0, -5 ); $cloud_item_editor = $_->array_get( $_POST, 'cloud_item_editor', '' ); $temp_import = '1' === $_->array_get( $_POST, 'temp_import', '0' ); // Maybe ask the user to make a decision on how to deal with global layouts. if ( ( ! $override_default_website_template || ! $has_default_template ) && $has_global_layouts ) { $incoming_layout_duplicate_decision = $_->array_get( $_POST, 'incoming_layout_duplicate_decision', '' ); if ( 'duplicate' === $incoming_layout_duplicate_decision || $library_template_import ) { $incoming_layout_duplicate = true; } elseif ( 'relink' === $incoming_layout_duplicate_decision ) { $incoming_layout_duplicate = false; } else { wp_send_json_error( array( 'code' => ET_Theme_Builder_Api_Errors::PORTABILITY_REQUIRE_INCOMING_LAYOUT_DUPLICATE_DECISION, 'error' => $i18n['This import contains references to global layouts.'], ) ); } } // phpcs:enable // Make imported preset overrides to avoid collisions with local presets. if ( $import_presets && is_array( $presets ) && ! empty( $presets ) ) { $presets_rewrite_map = $portability->prepare_to_import_layout_presets( $presets ); } // Prepare import steps. $layout_id_map = array(); $layout_keys = array( 'header', 'body', 'footer' ); $id = md5( get_current_user_id() . '_' . uniqid( 'et_theme_builder_import_', true ) ); $transient = 'et_theme_builder_import_' . get_current_user_id() . '_' . $id; $steps_files = array(); foreach ( $export['templates'] as $index => $template ) { foreach ( $layout_keys as $key ) { $layout_id = (int) $_->array_get( $template, array( 'layouts', $key, 'id' ), 0 ); if ( 0 === $layout_id ) { continue; } $layout = $_->array_get( $export, array( 'layouts', $layout_id ), null ); if ( empty( $layout ) ) { continue; } // Use a temporary string id to avoid numerical keys being reset by various array functions. $template_id = 'template_' . $index; $is_global = (bool) $_->array_get( $layout, 'theme_builder.is_global', false ); $create_new = ( $template['default'] && $override_default_website_template ) || ! $is_global || $incoming_layout_duplicate; if ( $create_new ) { $temp_id = 'tbi-step-' . count( $steps_files ); et_theme_builder_api_import_theme_builder_save_layout( $portability, $template_id, $layout_id, $layout, $temp_id, $transient ); $steps_files[] = array( 'id' => $temp_id, 'group' => $transient, ); } else { if ( ! isset( $layout_id_map[ $layout_id ] ) ) { $layout_id_map[ $layout_id ] = array(); } $layout_id_map[ $layout_id ][ $template_id ] = 'use_global'; } } } set_transient( $transient, array( 'file_name' => $uploaded_file_name, 'ready' => false, 'steps' => $steps_files, 'templates' => $export['templates'], 'override_default_website_template' => $override_default_website_template, 'incoming_layout_duplicate' => $incoming_layout_duplicate, 'layout_id_map' => $layout_id_map, 'presets' => $presets, 'import_presets' => $import_presets, 'library_template_import' => $library_template_import, 'presets_rewrite_map' => $presets_rewrite_map, 'cloud_item_editor' => $cloud_item_editor, 'temp_import' => $temp_import, ), 60 * 60 * 24 ); wp_send_json_success( array( 'id' => $id, 'steps' => count( $steps_files ), ) ); } add_action( 'wp_ajax_et_theme_builder_api_import_theme_builder', 'et_theme_builder_api_import_theme_builder' ); function et_theme_builder_api_import_theme_builder_step() { if ( ! et_pb_is_allowed( 'theme_builder' ) ) { wp_send_json_error(); } et_builder_security_check( 'et_theme_builder_portability', et_core_portability_cap( 'et_theme_builder' ), 'et_theme_builder_api_import_theme_builder', 'nonce' ); $_ = et_(); $id = sanitize_text_field( $_->array_get( $_POST, 'id', '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is done in `et_builder_security_check`. $step = (int) $_->array_get( $_POST, 'step', 0 ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is done in `et_builder_security_check`. $chunk = (int) $_->array_get( $_POST, 'chunk', 0 ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is done in `et_builder_security_check`. $transient = 'et_theme_builder_import_' . get_current_user_id() . '_' . $id; $export = get_transient( $transient ); if ( false === $export ) { wp_send_json_error(); } $layout_keys = array( 'header', 'body', 'footer' ); $portability = et_core_portability_load( 'et_theme_builder' ); $steps = $export['steps']; $ready = empty( $steps ); $layout_id_map = $export['layout_id_map']; $presets = $export['presets']; $presets_rewrite_map = $export['presets_rewrite_map']; $import_presets = $export['import_presets']; $library_template_import = $export['library_template_import']; $file_name = $export['file_name']; $cloud_item_editor = $export['cloud_item_editor']; $temp_import = $export['temp_import']; $templates = array(); $template_settings = array(); $chunks = 1; $preset_id = 0; if ( ! $ready ) { $import_step = et_theme_builder_api_import_theme_builder_load_layout( $portability, $steps[ $step ]['id'], $steps[ $step ]['group'] ); $import_step = array_merge( $import_step, array( 'presets' => $presets ) ); $import_step = array_merge( $import_step, array( 'presets_rewrite_map' => $presets_rewrite_map ) ); $import_step['import_presets'] = $import_presets; if ( $temp_import ) { $import_step['data']['post_status'] = 'draft'; } $result = $portability->import_theme_builder( $id, $import_step, count( $steps ), $step, $chunk ); if ( false === $result ) { wp_send_json_error(); } $ready = $result['ready']; $chunks = $result['chunks']; foreach ( $result['layout_id_map'] as $old_id => $new_ids ) { $layout_id_map[ $old_id ] = array_merge( $_->array_get( $layout_id_map, $old_id, array() ), $new_ids ); } } if ( $ready ) { if ( $import_presets && is_array( $presets ) && ! empty( $presets ) ) { if ( ! $portability->import_global_presets( $presets ) ) { $presets_error = apply_filters( 'et_core_portability_import_error_message', '' ); if ( $presets_error ) { wp_send_json_error( array( 'code' => ET_Theme_Builder_Api_Errors::PORTABILITY_IMPORT_PRESETS_FAILURE, 'error' => $presets_error, ) ); } } } $portability->delete_temp_files( $transient ); $conditions = array(); $global_layouts = array(); foreach ( $export['templates'] as $index => $template ) { $sanitized = et_theme_builder_sanitize_template( $template ); $is_default = $_->array_get( $sanitized, 'default', false ); foreach ( $layout_keys as $key ) { $old_layout_id = (int) $_->array_get( $sanitized, array( 'layouts', $key, 'id' ), 0 ); $layout_id = et_()->array_get( $layout_id_map, array( $old_layout_id, 'template_' . $index ), '' ); $layout_id = ! empty( $layout_id ) ? $layout_id : 0; $_->array_set( $sanitized, array( 'layouts', $key, 'id' ), $layout_id ); if ( $is_default ) { $global_layouts[ $key ]['id'] = $layout_id; } } $conditions = array_merge( $conditions, $sanitized['use_on'], $sanitized['exclude_from'] ); $_->array_set( $sanitized, array( 'global_layouts' ), $global_layouts ); $templates[] = $sanitized; } // Load all conditions from templates. $conditions = array_unique( $conditions ); $template_settings = array_replace( et_theme_builder_get_flat_template_settings_options(), et_theme_builder_load_template_setting_options( $conditions ) ); $valid_settings = array_keys( $template_settings ); // Strip all invalid conditions from templates. foreach ( $templates as $index => $template ) { $templates[ $index ]['use_on'] = array_values( array_intersect( $template['use_on'], $valid_settings ) ); $templates[ $index ]['exclude_from'] = array_values( array_intersect( $template['exclude_from'], $valid_settings ) ); } if ( $library_template_import ) { $is_multi_template = count( $templates ) > 1; if ( $is_multi_template || 'set' === $cloud_item_editor ) { $template_settings['set_name'] = $file_name; foreach ( $templates as $key => $template ) { foreach ( array( 'body', 'header', 'footer' ) as $layout_type ) { $layout_id = $_->array_get( $template, array( 'layouts', $layout_type, 'id' ) ); if ( 'use_global' === $layout_id && isset( $global_layouts[ $layout_type ] ) ) { $global_layout_id = $_->array_get( $global_layouts, array( $layout_type, 'id' ) ); $_->array_set( $templates, array( $key, 'layouts', $layout_type, 'id' ), $global_layout_id ); } } } if ( $temp_import ) { $template_settings['post_status'] = 'draft'; } $preset_id = et_theme_builder_save_preset_to_library( $templates, $template_settings ); } else { $first_template = $templates[0]; if ( 'template' === $cloud_item_editor ) { $template_settings['template_name'] = $first_template['title']; } else { $template_settings['template_name'] = $file_name; } if ( $temp_import ) { $first_template['status'] = 'draft'; } $templates[0]['template_id'] = et_theme_builder_save_template_to_library( $first_template, $template_settings ); } } } else { set_transient( $transient, array_merge( $export, array( 'layout_id_map' => $layout_id_map, ) ), 60 * 60 * 24 ); } wp_send_json_success( array( 'presetId' => $preset_id, 'chunks' => $chunks, 'templates' => $templates, 'templateSettings' => $template_settings, ) ); } add_action( 'wp_ajax_et_theme_builder_api_import_theme_builder_step', 'et_theme_builder_api_import_theme_builder_step' ); /** * Ajax action: save template into the local library. */ function et_theme_builder_api_save_template_to_library() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_api_save_template_to_library', 'nonce' ); $_ = et_(); // phpcs:disable WordPress.Security.NonceVerification.Missing -- Nonce verified in `et_builder_security_check`. $raw_templates = wp_unslash( $_->array_get( $_POST, 'template', array() ) ); $preferences = wp_unslash( $_->array_get( $_POST, 'preferences', array() ) ); // phpcs:enable $post_id = et_theme_builder_save_template_to_library( $raw_templates, $preferences ); if ( $post_id ) { wp_send_json_success( array( 'post_id' => $post_id, ) ); } else { wp_send_json_error(); } } add_action( 'wp_ajax_et_theme_builder_api_save_template_to_library', 'et_theme_builder_api_save_template_to_library' ); /** * Ajax action: save preset into the local library. */ function et_theme_builder_api_save_preset_to_library() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_api_save_preset_to_library', 'nonce' ); $_ = et_(); $preferences = wp_unslash( $_->array_get( $_POST, 'preferences', [] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in `et_builder_security_check`. $raw_templates = wp_unslash( $_->array_get( $_POST, 'templates', [] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in `et_builder_security_check`. $post_id = et_theme_builder_save_preset_to_library( $raw_templates, $preferences ); if ( $post_id ) { wp_send_json_success( array( 'post_id' => $post_id, ) ); } else { wp_send_json_error(); } } add_action( 'wp_ajax_et_theme_builder_api_save_preset_to_library', 'et_theme_builder_api_save_preset_to_library' ); /** * Ajax action: Retrieve terms for the given taxonomy. */ function et_theme_builder_api_get_terms() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_api_get_terms', 'nonce' ); $_ = et_(); $tax = sanitize_text_field( $_->array_get( $_POST, 'tax', '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in `et_builder_security_check`. if ( ! in_array( $tax, array( 'layout_category', 'layout_tag' ), true ) ) { wp_send_json_error(); } $terms_by_id = et_theme_builder_get_terms( $tax ); wp_send_json_success( $terms_by_id ); } add_action( 'wp_ajax_et_theme_builder_api_get_terms', 'et_theme_builder_api_get_terms' ); /** * Ajax action: Use local library item. */ function et_theme_builder_api_use_library_item() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_api_use_library_item', 'nonce' ); $i18n = array_merge( require ET_BUILDER_DIR . 'frontend-builder/i18n/generic.php', require ET_BUILDER_DIR . 'frontend-builder/i18n/portability.php', require ET_BUILDER_DIR . 'frontend-builder/i18n/theme-builder.php' ); // phpcs:disable WordPress.Security.NonceVerification.Missing -- Nonce verified in `et_builder_security_check`. $_ = et_(); $item_id = (int) $_->array_get( $_POST, 'item_id', 0 ); $override_default_website_template = '1' === $_->array_get( $_POST, 'override_default_website_template', '0' ); $override_assignments = '1' === $_->array_get( $_POST, 'override_assignments', '0' ); $download_backup = '1' === $_->array_get( $_POST, 'download_backup', '0' ); $incoming_layout_duplicate_decision = $_->array_get( $_POST, 'incoming_layout_duplicate_decision', '' ); $item_id = (int) $_->array_get( $_POST, 'item_id', 0 ); // phpcs:enable $args = [ 'override_default_website_template' => $override_default_website_template, 'override_assignments' => $override_assignments, 'download_backup' => $download_backup, 'incoming_layout_duplicate_decision' => $incoming_layout_duplicate_decision, ]; $return_additional_args = []; $library_item = new ET_Theme_Builder_Local_Library_Item( $item_id ); if ( ET_THEME_BUILDER_ITEM_SET === $library_item->get_item_type() ) { $has_global_layouts = $library_item->has_global_layouts(); $has_default_template = $library_item->has_default_template(); $show_layout_decision = ( ! $override_default_website_template || ! $has_default_template ) && $has_global_layouts; if ( $show_layout_decision && empty( $incoming_layout_duplicate_decision ) ) { wp_send_json_error( array( 'code' => ET_Theme_Builder_Api_Errors::PORTABILITY_REQUIRE_INCOMING_LAYOUT_DUPLICATE_DECISION, 'error' => $i18n['This import contains references to global layouts.'], ) ); } $return_additional_args = [ 'override_default_website_template' => $override_default_website_template, 'override_assignments' => $override_assignments, 'download_backup' => $download_backup, 'incoming_layout_duplicate_decision' => $incoming_layout_duplicate_decision, ]; } $item_data = $library_item->use_library_item( $args ); if ( is_wp_error( $item_data ) ) { wp_send_json_error(); } $data = array_merge( array( 'item_type' => $library_item->item_type, 'item_data' => $item_data, ), $return_additional_args ); wp_send_json_success( $data ); } add_action( 'wp_ajax_et_theme_builder_api_use_library_item', 'et_theme_builder_api_use_library_item' ); /** * Ajax action: Trash interim library editor posts. */ function et_theme_builder_trash_theme_builder() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_trash_theme_builder', 'nonce' ); // phpcs:disable WordPress.Security.NonceVerification.Missing -- Nonce verified in `et_builder_security_check`. $_ = et_(); $theme_builder_id = (int) $_->array_get( $_POST, 'theme_builder_id', 0 ); $template_ids = et_theme_builder_get_theme_builder_template_ids( true, $theme_builder_id ); $item_id = (int) $_->array_get( $_POST, 'item_id' ); $cloud_item_id = (int) $_->array_get( $_POST, 'cloud_item_id' ); $used_posts = array(); // phpcs:enable foreach ( $template_ids as $template_id ) { foreach ( array( 'header', 'body', 'footer' ) as $layout_type ) { $layout_id = get_post_meta( $template_id, "_et_{$layout_type}_layout_id", true ); // Clean layouts. if ( $layout_id ) { $used_posts[] = $layout_id; } } // Clean template. $used_posts[] = $template_id; } $used_posts[] = $theme_builder_id; $used_posts = array_map( 'intval', $used_posts ); foreach ( $used_posts as $used_post ) { if ( current_user_can( 'delete_others_posts' ) ) { wp_trash_post( $used_post ); } } // Delete local library item. if ( $cloud_item_id ) { if ( current_user_can( 'delete_others_posts' ) ) { wp_delete_post( $item_id ); } } } add_action( 'wp_ajax_et_theme_builder_trash_theme_builder', 'et_theme_builder_trash_theme_builder' ); /** * AJAX action: Gets items data for the theme builder's library UI. */ function et_theme_builder_library_get_items_data() { if ( ! et_pb_is_allowed( 'theme_builder' ) ) { wp_send_json_error(); } et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_library_get_items_data', 'nonce' ); $item_type = isset( $_POST['et_item_type'] ) ? (string) sanitize_text_field( $_POST['et_item_type'] ) : 'template'; if ( ! in_array( $item_type, array( 'set', 'template' ), true ) ) { wp_send_json_error( 'Error: Wrong item type provided.' ); } $item_library_local = et_pb_theme_builder_library_local(); $data = $item_library_local->get_library_items( $item_type ); wp_send_json_success( $data ); } add_action( 'wp_ajax_et_theme_builder_library_get_items_data', 'et_theme_builder_library_get_items_data' ); /** * AJAX action: Add/Remove/Rename Library terms for taxonomies. */ function et_theme_builder_library_update_terms() { et_builder_security_check( 'theme_builder', 'manage_categories', 'et_theme_builder_library_update_terms', 'nonce' ); $payload = isset( $_POST['payload'] ) ? (array) $_POST['payload'] : array(); // phpcs:ignore ET.Sniffs.ValidatedSanitizedInput -- $_POST['payload'] is an array, it's value sanitization is done at the time of accessing value. $et_library_taxonomy = isset( $_POST['et_library_taxonomy'] ) ? (string) $_POST['et_library_taxonomy'] : ''; // phpcs:ignore ET.Sniffs.ValidatedSanitizedInput -- $_POST['et_library_taxonomy'] is a string, it's value sanitization is done at the time of accessing value. $response = et_theme_builder_library_update_taxonomy_terms( $payload, $et_library_taxonomy ); if ( ! $response ) { wp_send_json_error( 'Error: Please provide valid payload and taxonomy' ); } return wp_send_json_success( $response ); } add_action( 'wp_ajax_et_theme_builder_library_update_terms', 'et_theme_builder_library_update_terms' ); /** * AJAX action: Update the theme builder library item. */ function et_theme_builder_library_update_item() { if ( ! et_pb_is_allowed( 'theme_builder' ) ) { wp_send_json_error(); } et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_library_update_item', 'nonce' ); $payload = isset( $_POST['payload'] ) ? (array) $_POST['payload'] : array(); // phpcs:ignore ET.Sniffs.ValidatedSanitizedInput -- $_POST['payload'] is an array, it's value sanitization is done at the time of accessing value. if ( empty( $payload ) ) { wp_send_json_error( 'Error: Payload is empty.' ); } $item_library_local = et_pb_theme_builder_library_local(); $response = $item_library_local->perform_item_update( $payload ); if ( ! $response ) { wp_send_json_error( 'Error: Provide valid data.' ); } return wp_send_json_success( $response ); } add_action( 'wp_ajax_et_theme_builder_library_update_item', 'et_theme_builder_library_update_item' ); /** * AJAX action: Save the theme builder library temporary item. */ function et_theme_builder_library_save_temp_layout() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_library_save_temp_layout', 'nonce' ); // phpcs:disable -- Sanitization will be handled inside the method. $local_content = isset( $_POST['localContent'] ) ? (array) $_POST['localContent'] : array(); $cloud_content = isset( $_POST['cloudContent'] ) ? (array) $_POST['cloudContent'] : array(); // phpcs:enable $templates = array(); $global_layouts = array(); $is_cloud_item = ! empty( $cloud_content['layouts'] ); $data = $is_cloud_item ? $cloud_content['templates'] : $local_content; foreach ( $data as $template ) : if ( $is_cloud_item ) { $is_default = filter_var( $template['default'], FILTER_VALIDATE_BOOLEAN ); $template = array_merge( $template, et_theme_builder_library_save_temp_cloud_layout_data( $template, $cloud_content['layouts'], $global_layouts ) ); } else { $post = get_post( $template['id'] ); if ( ! $post ) { continue; } $content = json_decode( $post->post_content ); $is_default = get_post_meta( $post->ID, '_et_default', true ); $template = array_merge( $template, et_theme_builder_library_save_temp_local_layout_data( $post->ID, $content, $global_layouts ) ); } // Set global references. $layouts = $template['layouts']; $layout_types = array( 'header', 'body', 'footer' ); foreach ( $layout_types as $layout_type ) { if ( ! isset( $layouts[ $layout_type ] ) ) { continue; } if ( $is_cloud_item ) { $global_info = et_()->array_get( $template, $layout_type . '_layout_global', false ); $is_global = filter_var( $global_info, FILTER_VALIDATE_BOOLEAN ); } else { $is_global = get_post_meta( $post->ID, '_et_' . $layout_type . '_layout_global', true ); } if ( $is_default || $is_global ) { $global_layouts[ $layout_type ] = $layouts[ $layout_type ]['id']; } } array_push( $templates, $template ); endforeach; $response = array( 'templates' => $templates, 'global_layouts' => $global_layouts, ); wp_send_json_success( $response ); } add_action( 'wp_ajax_et_theme_builder_library_save_temp_layout', 'et_theme_builder_library_save_temp_layout' ); /** * AJAX action: Remove the theme builder library temporary item. */ function et_theme_builder_library_remove_temp_layout() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_library_remove_temp_layout', 'nonce' ); $payload = isset( $_POST['payload'] ) ? (array) $_POST['payload'] : array(); // phpcs:ignore ET.Sniffs.ValidatedSanitizedInput -- $_POST['payload'] is an array, it's value sanitization is done at the time of accessing value. foreach ( $payload as $template ) { et_theme_builder_library_remove_temp_layout_data( $template ); } wp_send_json_success(); } add_action( 'wp_ajax_et_theme_builder_library_remove_temp_layout', 'et_theme_builder_library_remove_temp_layout' ); /** * AJAX action: Gets an item by ID. */ function et_theme_builder_library_get_item() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_library_get_item', 'nonce' ); $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0; $item_type = isset( $_POST['itemType'] ) ? (string) sanitize_text_field( $_POST['itemType'] ) : 'template'; if ( empty( $id ) || ! in_array( $item_type, array( 'set', 'template' ), true ) ) { wp_send_json_error( 'Error: Please provide an ID and a valid item type.' ); } $result = array(); $items_id = array( $id ); if ( ET_THEME_BUILDER_ITEM_SET === $item_type ) { $items_id = array(); $callback = 'et_theme_builder_library_get_' . ET_THEME_BUILDER_ITEM_SET . '_items_data'; $items = $callback( $id ); $default_template_id = (int) get_post_meta( $id, '_et_default_template_id', true ); foreach ( $items as $item ) { array_push( $items_id, $item->id ); } } // Continue processing for both set and template. $result['exported'] = et_theme_builder_library_get_exported_content( $items_id ); if ( ! isset( $result['exported']['context'] ) || ! isset( $result['exported']['templates'] ) || ! isset( $result['exported']['layouts'] ) ) { wp_send_json_error( 'Error: Invalid data.' ); } $response = wp_json_encode( array( 'success' => true, 'data' => $result, ) ); if ( ! $response ) { wp_send_json_error( 'Error: Invalid response.' ); } $tmp_dir = function_exists( 'sys_get_temp_dir' ) ? sys_get_temp_dir() : '/tmp'; $tmp_file = tempnam( $tmp_dir, 'et' ); et_()->WPFS()->put_contents( $tmp_file, $response ); // Remove any previous buffered content since we're setting `Content-Length` header // based on $response value only. while ( ob_get_level() ) { ob_end_clean(); } header( 'Content-Length: ' . @filesize( $tmp_file ) ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- `filesize` may fail due to the permissions denied error. @unlink( $tmp_file ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- `unlink` may fail due to the permissions denied error. // Charset has to be explicitly mentioned when it is other than UTF-8. header( 'Content-Type: application/json; charset=' . esc_attr( get_option( 'blog_charset' ) ) ); die( et_core_intentionally_unescaped( $response, 'html' ) ); } add_action( 'wp_ajax_et_theme_builder_library_get_item', 'et_theme_builder_library_get_item' ); /** * AJAX action: Get the theme builder library preset items. */ function et_theme_builder_library_get_set_items() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_library_get_set_items', 'nonce' ); $item_id = isset( $_POST['itemId'] ) ? intval( $_POST['itemId'] ) : 0; if ( empty( $item_id ) ) { wp_send_json_error(); } $items = et_theme_builder_library_get_set_items_data( $item_id ); wp_send_json_success( $items ); } add_action( 'wp_ajax_et_theme_builder_library_get_set_items', 'et_theme_builder_library_get_set_items' ); /** * Ajax action: Get default template id of the preset. */ function et_theme_builder_get_preset_default_template_id() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_get_preset_default_template_id', 'nonce' ); $_ = et_(); $item_id = (int) $_->array_get( $_POST, 'item_id', 0 ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in `et_builder_security_check`. $default_template_id = (int) get_post_meta( $item_id, '_et_default_template_id', true ); if ( $default_template_id > 0 ) { wp_send_json_success( array( 'default_template_id' => $default_template_id ) ); } else { wp_send_json_error( array( 'default_template_id' => 0 ) ); } } add_action( 'wp_ajax_et_theme_builder_get_preset_default_template_id', 'et_theme_builder_get_preset_default_template_id' ); /** * Ajax action: Remove the Library item after it is moved to the Cloud. */ function et_theme_builder_library_toggle_cloud_status() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_library_toggle_cloud_status', 'nonce' ); $post_id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in `et_builder_security_check`. if ( ! $post_id ) { wp_send_json_error( 'Error: ID is required.' ); } $post_type = get_post_type( $post_id ); if ( ! current_user_can( 'edit_post', $post_id ) || ET_TB_ITEM_POST_TYPE !== $post_type ) { wp_send_json_error( 'You do not have permission.' ); } wp_send_json_success( wp_delete_post( $post_id, true ) ); } add_action( 'wp_ajax_et_theme_builder_library_toggle_cloud_status', 'et_theme_builder_library_toggle_cloud_status' ); /** * Ajax action: Remove temp layouts, templates theme builder. */ function et_theme_builder_library_clear_temp_data() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_library_clear_temp_data', 'nonce' ); $args = array( 'post_status' => 'draft', 'post_type' => array( 'et_tb_item', 'et_header_layout', 'et_body_layout', 'et_footer_layout' ), 'author' => get_current_user_id(), 'fields' => 'ids', 'no_found_rows' => true, 'nopaging' => true, ); $draft_query = new WP_Query( $args ); foreach ( $draft_query->posts as $draft_post_id ) { $post_type = get_post_type( $draft_post_id ); if ( current_user_can( 'edit_post', $draft_post_id ) && ET_TB_ITEM_POST_TYPE === $post_type ) { wp_delete_post( $draft_post_id ); } } wp_send_json_success(); } add_action( 'wp_ajax_et_theme_builder_library_clear_temp_data', 'et_theme_builder_library_clear_temp_data' ); /** * AJAX action: Gets Cloud access token from DB and send it to client. */ function et_theme_builder_library_get_cloud_token() { et_builder_security_check( 'theme_builder', 'edit_others_posts', 'et_theme_builder_library_get_cloud_token', 'nonce' ); wp_send_json_success( array( 'accessToken' => get_transient( 'et_cloud_access_token' ), ) ); } add_action( 'wp_ajax_et_theme_builder_library_get_cloud_token', 'et_theme_builder_library_get_cloud_token' );