prefix . 'astr_customizer_snapshots'; $table_safe = esc_sql( $snapshots_table ); $per_page = 10; $paged = astr_safe_get( 'paged', 'int', 1, 'manage_options', false ); $offset = ( $paged - 1 ) * $per_page; $search = astr_safe_get( 's', 'text', '', 'manage_options', false ); $filter = astr_safe_get( 'change_type', 'key', '', 'manage_options', false ); if ( in_array( $filter, [ '0', 'all' ], true ) ) { $filter = ''; } $allowed_orderby = [ 'change_type' => 'change_type', 'user_name' => 'user_name', 'theme' => 'theme', 'time' => 'time', ]; $orderby_key = astr_safe_get( 'orderby', 'key', 'time', 'manage_options', false ); $orderby = $allowed_orderby[ $orderby_key ] ?? 'time'; $order = ( strtoupper( astr_safe_get( 'order', 'text', 'DESC', 'manage_options', false ) ) === 'ASC' ) ? 'ASC' : 'DESC'; $where_parts = []; $args = []; if ( ! empty( $filter ) ) { $where_parts[] = "change_type = %s"; $args[] = $filter; } if ( $search !== '' ) { $like = '%' . $wpdb->esc_like( $search ) . '%'; $where_parts[] = "( CAST(id AS CHAR) LIKE %s OR theme LIKE %s OR user_name LIKE %s OR change_type LIKE %s OR action LIKE %s OR time LIKE %s )"; array_push($args, $like, $like, $like, $like, $like, $like); } $m = astr_safe_get( 'm', 'text', '', 'manage_options', false ); if ( $m !== '' && $m !== '0' ) { $year = absint( substr( $m, 0, 4 ) ); $month = absint( substr( $m, 4, 2 ) ); if ( $year > 0 && $month >= 1 && $month <= 12 ) { $where_parts[] = 'YEAR(`time`) = %d'; $where_parts[] = 'MONTH(`time`) = %d'; $args[] = $year; $args[] = $month; } } $theme_filter = astr_safe_get( 'theme', 'text', '', 'manage_options', false ); if ( $theme_filter !== '' && $theme_filter !== '0' ) { $where_parts[] = 'theme = %s'; $args[] = sanitize_text_field( $theme_filter ); } $where = $where_parts ? 'WHERE ' . implode( ' AND ', $where_parts ) : ''; // --- Counts --- // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching $count_all = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$table_safe}" ); $total_sql = "SELECT COUNT(*) FROM {$table_safe} {$where}"; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter $total = ! empty( $args ) ? (int) $wpdb->get_var( $wpdb->prepare( $total_sql, $args ) ) : (int) $wpdb->get_var( $total_sql ); $total_pages = max( 1, ceil( $total / $per_page ) ); // --- Get rows --- $max_limit = 100; $per_page = min($per_page, $max_limit); $offset = max(0, (int) $offset); $sql_base = "SELECT * FROM {$table_safe} $where ORDER BY {$orderby} {$order} LIMIT %d OFFSET %d"; $params = $args; $params[] = $per_page; $params[] = $offset; // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter $rows = $wpdb->get_results( $wpdb->prepare( $sql_base, ...$params ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching $themes = $wpdb->get_results( "SELECT DISTINCT theme FROM {$table_safe} WHERE theme <> '' ORDER BY theme ASC" ); echo '
'; echo '

CSS History

'; echo '
'; if ( astr_safe_get( 'restored', 'text', '', 'manage_options', false ) === 'success' ) { echo '

CSS restored successfully

'; } $query_args = [ 's' => $search, 'm' => $m, 'theme' => $theme_filter, ]; if ( $filter !== '' ) { $query_args['change_type'] = $filter; } $base_url = admin_url( 'admin.php?page=astr-css-history' ); $page = astr_safe_get( 'page', 'key', 'astr-css-history', 'manage_options', false ); // === FILTERS + SEARCH (WP style) === echo '
'; // LEFT: filters echo '
'; echo '
'; echo '
    '; $all_url = remove_query_arg( [ 'change_type', 's', 'm', 'theme', 'paged' ], admin_url( 'admin.php?page=astr-css-history' ) ); echo '
  • All (' . esc_html( $count_all ) . ')
  • '; echo '
'; echo '
'; // RIGHT: search echo '
'; echo '
'; echo ''; foreach ( [ 'orderby', 'order', 'change_type', 'm', 'theme' ] as $k ) { $value = astr_safe_get( $k, 'text', '', 'manage_options', false ); if ( $value !== '' ) { echo ''; } } echo ''; echo '
'; echo '
'; echo '
'; echo '
'; echo '
'; // === Top Table Nav === echo ''; // === Bottom Pagination === echo '
'; if ( $total >= $per_page ) { astr_pagination( $total, $total_pages, $paged ); } echo '
'; // === Modal for Preview === ?>
'; } function astr_get_global_styles_css_for_theme( $theme_slug ) { $posts = get_posts( [ 'post_type' => 'wp_global_styles', 'post_status' => 'publish', 'posts_per_page' => -1, ] ); $clean_slug = sanitize_title( $theme_slug ); foreach ( $posts as $post ) { if ( strpos( $post->post_name, $clean_slug ) === false && strpos( $post->post_name, $theme_slug ) === false ) { continue; } $data = json_decode( (string) $post->post_content, true ); if ( ! is_array( $data ) ) { continue; } $css = $data['styles']['css'] ?? ''; if ( is_string( $css ) && trim( $css ) !== '' ) { return $css; } } /* * If the theme has no saved Global Styles post, * use the CSS from its theme.json as the baseline. */ $theme = wp_get_theme( $theme_slug ); if ( ! $theme->exists() ) { return ''; } $theme_json_file = trailingslashit( $theme->get_stylesheet_directory() ) . 'theme.json'; if ( ! file_exists( $theme_json_file ) || ! is_readable( $theme_json_file ) ) { return ''; } $json = file_get_contents( $theme_json_file ); if ( ! is_string( $json ) || $json === '' ) { return ''; } $data = json_decode( $json, true ); if ( ! is_array( $data ) ) { return ''; } $css = $data['styles']['css'] ?? ''; return is_string( $css ) ? $css : ''; } /** * Log theme switching in CSS History. */ function astr_log_theme_switch( $new_name, $new_theme, $old_theme ) { if ( ! current_user_can( 'edit_theme_options' ) ) { return; } if ( ! $old_theme instanceof WP_Theme || ! $new_theme instanceof WP_Theme ) { return; } global $wpdb; $user = wp_get_current_user(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->insert( $wpdb->prefix . 'astr_customizer_snapshots', [ 'theme' => $new_name, 'user_id' => $user->ID, 'user_name' => $user->display_name, 'time' => current_time( 'mysql' ), 'change_type' => 'theme_switch', 'action' => 'switch', ], [ '%s', '%d', '%s', '%s', '%s', '%s', ] ); $snapshot_id = (int) $wpdb->insert_id; if ( ! $snapshot_id ) { return; } $meta_table = $wpdb->prefix . 'astr_customizer_snapshot_meta'; /* * Theme names and slugs. */ $meta = [ 'previous_theme' => $old_theme->get( 'Name' ), 'new_theme' => $new_name, 'previous_theme_slug' => $old_theme->get_stylesheet(), 'new_theme_slug' => $new_theme->get_stylesheet(), /* * CSS tracked by SiteTrace. */ 'previous_customizer_css' => (string) wp_get_custom_css( $old_theme->get_stylesheet() ), 'new_customizer_css' => (string) wp_get_custom_css( $new_theme->get_stylesheet() ), 'previous_global_styles_css' => astr_get_global_styles_css_for_theme( $old_theme->get_stylesheet() ), 'new_global_styles_css' => astr_get_global_styles_css_for_theme( $new_theme->get_stylesheet() ), ]; foreach ( $meta as $key => $value ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->insert( $meta_table, [ 'snapshot_id' => $snapshot_id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key 'meta_key' => $key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value 'meta_value' => $value, ], [ '%d', '%s', '%s', ] ); } } add_action( 'switch_theme', 'astr_log_theme_switch', 10, 3 ); function astr_capture_old_customizer_state() { if ( ! current_user_can( 'edit_theme_options' ) ) { return; } $theme = get_stylesheet(); set_transient( 'astr_customizer_old_' . $theme, [ 'wp_css' => (string) wp_get_custom_css( $theme ), ], 5 * MINUTE_IN_SECONDS ); } add_action( 'customize_save', 'astr_capture_old_customizer_state', 1 ); /** * Returns the custom CSS defined in the active theme's theme.json. * Until the user saves Additional CSS in the Site Editor for the first time, the CSS field shows this value, so it is the real baseline for a diff. */ function astr_get_theme_json_custom_css() { if ( ! class_exists( 'WP_Theme_JSON_Resolver' ) ) { return ''; } $theme_json = WP_Theme_JSON_Resolver::get_theme_data(); if ( ! is_object( $theme_json ) || ! method_exists( $theme_json, 'get_raw_data' ) ) { return ''; } $raw = $theme_json->get_raw_data(); $css = $raw['styles']['css'] ?? ''; return is_string( $css ) ? $css : ''; } function astr_capture_old_global_styles_state( $post_id, $post ) { if ( ! current_user_can( 'edit_theme_options' ) ) { return; } $data = json_decode( (string) $post->post_content, true ); if ( json_last_error() !== JSON_ERROR_NONE || ! is_array( $data ) ) { return; } $css = $data['styles']['css'] ?? null; if ( ! is_string( $css ) ) { // The post has no saved CSS yet, fall back to the theme.json CSS shown in the editor field. $css = astr_get_theme_json_custom_css(); } set_transient( 'astr_global_styles_old_css', $css, 5 * MINUTE_IN_SECONDS ); } function astr_save_setting( $snapshot_id, $theme, $key, $old_value, $new_value ) { global $wpdb; $settings_table = $wpdb->prefix . 'astr_customizer_settings'; $snapshot_settings_table = $wpdb->prefix . 'astr_customizer_snapshot_settings'; $theme = sanitize_text_field( $theme ); $key = sanitize_text_field( $key ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->query( $wpdb->prepare( 'INSERT INTO %i (theme, setting_key) VALUES (%s, %s) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id)', $settings_table, $theme, $key ) ); $setting_id = (int) $wpdb->insert_id; if ( ! $setting_id ) { return false; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->insert( $snapshot_settings_table, [ 'snapshot_id' => $snapshot_id, 'setting_id' => $setting_id, 'old_value' => is_string( $old_value ) ? $old_value : maybe_serialize( $old_value ), 'new_value' => is_string( $new_value ) ? $new_value : maybe_serialize( $new_value ), ] ); } add_action( 'pre_post_update', function ( $post_id ) { if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || wp_is_post_revision( $post_id ) ) { return; } if ( get_post_type( $post_id ) !== 'wp_global_styles' ) { return; } $post = get_post( $post_id ); if ( $post ) { astr_capture_old_global_styles_state( $post_id, $post ); } }, 1 ); function astr_record_css_save() { if ( ! current_user_can( 'edit_theme_options' ) ) { return; } global $wpdb; $theme_slug = get_stylesheet(); $theme_name = wp_get_theme()->get( 'Name' ); $user = wp_get_current_user(); $old = get_transient( 'astr_customizer_old_' . $theme_slug ); $new_css = (string) wp_get_custom_css( $theme_slug ); if ( ! is_array( $old ) || ! isset( $old['wp_css'] ) ) { $old_css = (string) wp_get_custom_css( $theme_slug ); } else { $old_css = (string) $old['wp_css']; } if ( astr_normalize_css( $old_css ) === astr_normalize_css( $new_css ) ) { delete_transient( 'astr_customizer_old_' . $theme_slug ); return; } if ( ! astr_css_has_real_changes( $old_css, $new_css ) ) { delete_transient( 'astr_customizer_old_' . $theme_slug ); return; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->insert( $wpdb->prefix . 'astr_customizer_snapshots', [ 'theme' => $theme_name, 'user_id' => $user->ID, 'user_name' => $user->display_name, 'time' => current_time( 'mysql' ), 'change_type' => 'customizer_css', 'action' => 'update', ] ); $snapshot_id = (int) $wpdb->insert_id; astr_save_setting( $snapshot_id, $theme_name, 'customizer_css', $old_css, $new_css ); delete_transient( 'astr_customizer_old_' . $theme_slug ); } add_action( 'customize_save_after', 'astr_record_css_save', 10 ); add_action( 'save_post_wp_global_styles', function ( $post_id, $post, $update ) { static $is_processing = false; if ( $is_processing ) { return; } $is_processing = true; try { if ( ! $post instanceof WP_Post || ! current_user_can( 'edit_theme_options' ) ) { return; } if ( get_transient( 'astr_skip_global_styles_log' ) ) { return; } $content_json = isset( $post->post_content ) ? (string) $post->post_content : ''; $data = json_decode( $content_json, true ); $new_css = $data['styles']['css'] ?? null; if ( ! is_string( $new_css ) ) { // No CSS key in the saved post (e.g. the post was just created or a non-CSS change was saved), the effective CSS is still the theme.json CSS. $new_css = astr_get_theme_json_custom_css(); } $old_css = get_transient( 'astr_global_styles_old_css' ); if ( ! is_string( $old_css ) ) { $revisions = wp_get_post_revisions( $post_id, [ 'posts_per_page' => 1 ] ); if ( ! empty( $revisions ) ) { $prev_post = reset( $revisions ); $prev_data = json_decode( (string) $prev_post->post_content, true ); $old_css = $prev_data['styles']['css'] ?? null; } } if ( ! is_string( $old_css ) ) { // No stored CSS anywhere, the previous state is the theme.json CSS. $old_css = astr_get_theme_json_custom_css(); } if ( $old_css === $new_css || ! astr_css_has_real_changes( $old_css, $new_css ) ) { delete_transient( 'astr_global_styles_old_css' ); return; } global $wpdb; $theme = wp_get_theme()->get( 'Name' ); $user = wp_get_current_user(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->insert( $wpdb->prefix . 'astr_customizer_snapshots', [ 'theme' => $theme, 'user_id' => $user->ID, 'user_name' => $user->display_name, 'time' => current_time( 'mysql' ), 'change_type' => 'global_styles_css', 'action' => 'update', ] ); $snapshot_id = (int) $wpdb->insert_id; $settings_table = $wpdb->prefix . 'astr_customizer_settings'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->query( $wpdb->prepare( 'INSERT INTO %i (theme, setting_key) VALUES (%s, %s) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id)', $settings_table, $theme, 'global_styles_css' ) ); $setting_id = (int) $wpdb->insert_id; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->insert( $wpdb->prefix . 'astr_customizer_snapshot_settings', [ 'snapshot_id' => $snapshot_id, 'setting_id' => $setting_id, 'old_value' => $old_css, 'new_value' => $new_css, ] ); delete_transient( 'astr_global_styles_old_css' ); } finally { $is_processing = false; } }, 10, 3 ); function astr_restore_snapshot_handler() { if ( ! current_user_can( 'edit_theme_options' ) ) { wp_die( 'You do not have permission to restore customizer CSS.' ); } if ( ! isset( $_POST['snapshot_id'], $_POST['_wpnonce'] ) ) { wp_die( 'Invalid request.' ); } $nonce = sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ); if ( ! wp_verify_nonce( $nonce, 'astr_restore_snapshot' ) ) { wp_die( 'Nonce verification failed.' ); } global $wpdb; $snapshot_id = isset( $_POST['snapshot_id'] ) ? absint( wp_unslash( $_POST['snapshot_id'] ) ) : 0; $snapshots_table = $wpdb->prefix . 'astr_customizer_snapshots'; $snapshot_settings_table = $wpdb->prefix . 'astr_customizer_snapshot_settings'; $settings_table = $wpdb->prefix . 'astr_customizer_settings'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $snapshot = $wpdb->get_row( $wpdb->prepare( 'SELECT id FROM %i WHERE id = %d', $snapshots_table, $snapshot_id ) ); if ( ! $snapshot ) { wp_die( 'Snapshot not found.' ); } $query = $wpdb->prepare( 'SELECT ss.old_value, s.setting_key FROM %i ss JOIN %i s ON s.id = ss.setting_id WHERE ss.snapshot_id = %d', $snapshot_settings_table, $settings_table, $snapshot_id ); // Query has already been prepared above. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared $row = $wpdb->get_row( $query, ARRAY_A ); if ( empty( $row ) ) { wp_die( 'Snapshot has no CSS data.' ); } $css = maybe_unserialize( $row['old_value'] ?? '' ); switch ( $row['setting_key'] ) { case 'customizer_css': $theme = get_stylesheet(); wp_update_custom_css_post( $css, [ 'stylesheet' => $theme, ] ); break; case 'global_styles_css': $theme_slug = get_stylesheet(); $posts = get_posts( [ 'post_type' => 'wp_global_styles', 'post_status' => 'publish', 'posts_per_page' => -1, ] ); $global_styles_post = null; foreach ( $posts as $post ) { if ( strpos( $post->post_name, $theme_slug ) !== false ) { $global_styles_post = $post; break; } } if ( ! $global_styles_post ) { $user_id = get_current_user_id(); $post_id = wp_insert_post([ 'post_title' => 'Custom Styles', 'post_name' => 'wp-global-styles-' . $theme_slug, 'post_status' => 'publish', 'post_type' => 'wp_global_styles', 'post_author' => $user_id, ]); if ( is_wp_error( $post_id ) || ! $post_id ) { wp_die( 'Could not create global styles post for current theme.' ); } $global_styles_post = get_post( $post_id ); } $content = json_decode( (string) $global_styles_post->post_content, true ); if ( ! is_array( $content ) ) { $content = []; } if ( ! isset( $content['styles'] ) || ! is_array( $content['styles'] ) ) { $content['styles'] = []; } $content['styles']['css'] = $css; set_transient('astr_skip_global_styles_log', 1, 60); wp_update_post([ 'ID' => $global_styles_post->ID, 'post_content' => wp_slash( wp_json_encode($content) ), ]); break; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->update( $snapshots_table, [ 'action' => 'restored' ], [ 'id' => $snapshot_id, 'action' => 'update' ], [ '%s' ], [ '%d', '%s' ] ); wp_safe_redirect( add_query_arg( [ 'page' => 'astr-css-history', 'restored' => 'success', ], admin_url( 'admin.php' ) ) ); exit; } add_action( 'admin_post_astr_restore_snapshot', 'astr_restore_snapshot_handler' ); add_action( 'wp_ajax_astr_preview_snapshot', 'astr_preview_snapshot_ajax' ); function astr_preview_snapshot_ajax() { if ( ! current_user_can( 'edit_theme_options' ) ) { wp_send_json_error( 'No permission' ); } $snapshot_id = isset( $_POST['snapshot_id'] ) ? absint( wp_unslash( $_POST['snapshot_id'] ) ) : 0; $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'astr_preview_snapshot_' . $snapshot_id ) ) { wp_send_json_error( 'Invalid nonce' ); } global $wpdb; $snapshots_table = $wpdb->prefix . 'astr_customizer_snapshots'; $snapshot_settings_table = $wpdb->prefix . 'astr_customizer_snapshot_settings'; $settings_table = $wpdb->prefix . 'astr_customizer_settings'; $meta_table = $wpdb->prefix . 'astr_customizer_snapshot_meta'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $snapshot = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d LIMIT 1', $snapshots_table, $snapshot_id ) ); if ( ! $snapshot ) { wp_send_json_error( 'Snapshot not found' ); } /* * Theme switch preview */ if ( $snapshot->change_type === 'theme_switch' ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $meta_rows = $wpdb->get_results( $wpdb->prepare( 'SELECT meta_key, meta_value FROM %i WHERE snapshot_id = %d', $meta_table, $snapshot_id ), OBJECT_K ); $get_meta = static function ( $key ) use ( $meta_rows ) { return isset( $meta_rows[ $key ] ) ? (string) $meta_rows[ $key ]->meta_value : ''; }; $previous_name = $get_meta( 'previous_theme' ); $new_name = $get_meta( 'new_theme' ); if ( $new_name === '' ) { $new_name = $snapshot->theme; } /* * CSS actually tracked by SiteTrace. */ $previous_customizer_css = $get_meta( 'previous_customizer_css' ); $new_customizer_css = $get_meta( 'new_customizer_css' ); $previous_global_css = $get_meta( 'previous_global_styles_css' ); $new_global_css = $get_meta( 'new_global_styles_css' ); /* * Old history records may not contain CSS data. */ if ( $previous_customizer_css === '' && $new_customizer_css === '' && $previous_global_css === '' && $new_global_css === '' ) { $output = '
'; $output .= 'Theme switched
'; $output .= esc_html( mysql2date( 'd-m-Y H:i:s', $snapshot->time ) ) . '
'; $output .= '
'; $output .= '
'; $output .= 'No custom CSS or Global Styles CSS was found for either theme.'; $output .= '
'; wp_send_json_success( [ 'html' => $output, 'theme_switch' => true, 'restore_allowed' => false, ] ); } $output = '
'; $output .= 'Theme switched
'; $output .= esc_html( mysql2date( 'd-m-Y H:i:s', $snapshot->time ) ) . '
'; $output .= '
'; /* * Customizer CSS */ if ( $previous_customizer_css !== '' || $new_customizer_css !== '' ) { $output .= '
'; $output .= '
Customizer CSS
'; $output .= '
'; $output .= '
'; $output .= '
' . esc_html( $previous_name ?: 'Previous theme' ) . '
'; $output .= '
'; $output .= '
' .
                astr_highlight_css_diff(
                    $previous_customizer_css,
                    $new_customizer_css,
                    'old'
                ) .
                '
'; $output .= '
'; $output .= '
'; $output .= '
'; $output .= '
' . esc_html( $new_name ?: 'New theme' ) . '
'; $output .= '
'; $output .= '
' .
                astr_highlight_css_diff(
                    $previous_customizer_css,
                    $new_customizer_css,
                    'new'
                ) .
                '
'; $output .= '
'; $output .= '
'; $output .= '
'; $output .= '
'; } /* * Global Styles CSS */ if ( $previous_global_css !== '' || $new_global_css !== '' ) { $output .= '
'; $output .= '
Global Styles (Additional CSS)
'; $output .= '
'; $output .= '
'; $output .= '
' . esc_html( $previous_name ?: 'Previous theme' ) . '
'; $output .= '
'; $output .= '
' .
                astr_highlight_css_diff(
                    $previous_global_css,
                    $new_global_css,
                    'old'
                ) .
                '
'; $output .= '
'; $output .= '
'; $output .= '
'; $output .= '
' . esc_html( $new_name ?: 'New theme' ) . '
'; $output .= '
'; $output .= '
' .
                astr_highlight_css_diff(
                    $previous_global_css,
                    $new_global_css,
                    'new'
                ) .
                '
'; $output .= '
'; $output .= '
'; $output .= '
'; $output .= '
'; } wp_send_json_success( [ 'html' => $output, 'theme_switch' => true, 'restore_allowed' => false, ] ); } /* * Regular CSS snapshot preview. */ // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $row = $wpdb->get_row( $wpdb->prepare( 'SELECT ss.old_value, ss.new_value FROM %i ss JOIN %i s ON s.id = ss.setting_id WHERE ss.snapshot_id = %d AND s.setting_key = %s', $snapshot_settings_table, $settings_table, (int) $snapshot_id, $snapshot->change_type ), ARRAY_A ); $old_css = (string) ( $row['old_value'] ?? '' ); $new_css = (string) ( $row['new_value'] ?? '' ); $is_restored = ( $snapshot->action ?? '' ) === 'restored'; if ( $is_restored ) { $tmp = $old_css; $old_css = $new_css; $new_css = $tmp; } $output = '
'; $output .= '' . esc_html( $snapshot->user_name ) . '
'; $output .= 'updated — ' . esc_html( mysql2date( 'd-m-Y H:i:s', $snapshot->time ) ) . '
'; $output .= esc_html( $snapshot->theme ); $output .= '
'; $output .= '
'; $output .= '
'; $output .= '
Old CSS
'; $output .= '
'; $output .= '
' .
        astr_highlight_css_diff(
            $old_css,
            $new_css,
            'old'
        ) .
        '
'; $output .= '
'; $output .= '
'; $output .= '
'; $output .= '
New CSS
'; $output .= '
'; $output .= '
' .
        astr_highlight_css_diff(
            $old_css,
            $new_css,
            'new'
        ) .
        '
'; $output .= '
'; $output .= '
'; $output .= '
'; wp_send_json_success( [ 'html' => $output, 'theme_switch' => false, 'restore_allowed' => true, ] ); } function astr_normalize_css($css) { if (!is_string($css) || trim($css) === '') { return ''; } $css = str_replace(['{', '}', ';'], ["{\n", "\n}\n", ";\n"], $css); $lines = preg_split('/\R/', $css); $result = []; foreach ($lines as $line) { $line = trim($line); $line = preg_replace('/\s+/', ' ', $line); $line = preg_replace('/\s*:\s*/', ': ', $line); if ($line === '') { continue; } $result[] = $line; } return implode("\n", $result); } // Global back-light function function astr_highlight_css_diff($old_css, $new_css, $type = 'new'){ $old_css = astr_normalize_css((string)$old_css); $new_css = astr_normalize_css((string)$new_css); $old_raw = $old_css !== '' ? preg_split('/\R/', $old_css) : []; $new_raw = $new_css !== '' ? preg_split('/\R/', $new_css) : []; $old = []; $new = []; foreach ($old_raw as $line) { $line = trim($line); if ($line === '') continue; $old[] = $line; } foreach ($new_raw as $line) { $line = trim($line); if ($line === '') continue; $new[] = $line; } $old_count = count($old); $new_count = count($new); $lcs = array_fill(0, $old_count + 1, array_fill(0, $new_count + 1, 0)); for ($i = $old_count - 1; $i >= 0; $i--) { for ($j = $new_count - 1; $j >= 0; $j--) { if ($old[$i] === $new[$j]) { $lcs[$i][$j] = $lcs[$i + 1][$j + 1] + 1; } else { $lcs[$i][$j] = max( $lcs[$i + 1][$j], $lcs[$i][$j + 1] ); } } } $output = []; $i = 0; $j = 0; while ($i < $old_count && $j < $new_count) { if ($old[$i] === $new[$j]) { $output[] = esc_html( $type === 'old' ? $old[$i] : $new[$j] ); $i++; $j++; continue; } if ($lcs[$i + 1][$j] > $lcs[$i][$j + 1]) { if ($type === 'old' && trim($old[$i]) !== '') { $output[] = '' . esc_html($old[$i]) . ''; } $i++; continue; } if ($lcs[$i][$j + 1] > $lcs[$i + 1][$j]) { if ($type === 'new' && trim($new[$j]) !== '') { $output[] = '' . esc_html($new[$j]) . ''; } $j++; continue; } if ($type === 'old' && trim($old[$i]) !== '') { $output[] = '' . esc_html($old[$i]) . ''; } if ($type === 'new' && trim($new[$j]) !== '') { $output[] = '' . esc_html($new[$j]) . ''; } $i++; $j++; continue; } while ($i < $old_count) { if ($type === 'old' && trim($old[$i]) !== '') { $output[] = '' . esc_html($old[$i]) . ''; } $i++; } while ($j < $new_count) { if ($type === 'new' && trim($new[$j]) !== '') { $output[] = '' . esc_html($new[$j]) . ''; } $j++; } return implode("\n", $output); } function astr_css_has_real_changes( $old_css, $new_css ) { $old_diff = astr_highlight_css_diff( $old_css, $new_css, 'old' ); $new_diff = astr_highlight_css_diff( $old_css, $new_css, 'new' ); return ( preg_match('/.+?<\/span>/s', $old_diff) || preg_match('/.+?<\/span>/s', $new_diff) ); }