Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save seanlanglands/5c2721a7239379224ce667b56942998e to your computer and use it in GitHub Desktop.

Select an option

Save seanlanglands/5c2721a7239379224ce667b56942998e to your computer and use it in GitHub Desktop.
<?php
/**
* Allow Elementor CSS file updates
*
* Save post CSS file after document save.
*/
add_action( 'elementor/document/after_save', 'wpvip_elementor_after_save_update_css', 10, 2 );
/**
* Save post CSS file after Elementor document save.
*
* Uses a re-entrancy guard to avoid recursive `after_save` calls while CSS is
* being regenerated.
*
* @param object $document Elementor document instance.
* @param array $data Saved document data.
*
* @return void
*/
function wpvip_elementor_after_save_update_css( $document, $data ) {
static $post_css_update_in_progress = false;
if ( $post_css_update_in_progress ) {
return;
}
// Ensure VIP FS helper functions are available.
if (
! function_exists( 'wpvip_fs_local_file_list' ) ||
! function_exists( 'wpvip_fs_local_file_remove' ) ||
! function_exists( 'wpvip_fs_local_file_add' )
) {
return;
}
// Ensure Elementor classes are available.
if (
! class_exists( 'Elementor\Plugin' ) ||
! class_exists( 'Elementor\Core\Files\CSS\Post' )
) {
return;
}
$post_css_update_in_progress = true;
$css_file_pattern = 'vip://wp-content/uploads/elementor/css/*';
try {
// Temporarily allow Elementor CSS writes in VIP FS.
$fs_local_files = wpvip_fs_local_file_list();
if ( array_key_exists( $css_file_pattern, $fs_local_files ) ) {
wpvip_fs_local_file_remove( $css_file_pattern );
}
$post_id = $document->get_main_id();
$frontend_document = \Elementor\Plugin::$instance->documents->get_doc_for_frontend( $post_id );
if ( $frontend_document ) {
$css_file = \Elementor\Core\Files\CSS\Post::create( $post_id );
$css_file->update();
}
} finally {
// Always restore the VIP FS pattern.
$fs_local_files = wpvip_fs_local_file_list();
if ( ! array_key_exists( $css_file_pattern, $fs_local_files ) ) {
wpvip_fs_local_file_add( $css_file_pattern );
}
$post_css_update_in_progress = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment