/**
* 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
%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
%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
%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' );