Skip to content

Instantly share code, notes, and snippets.

@davidwebca
Last active June 24, 2022 00:09
Show Gist options
  • Save davidwebca/135f03e6fbf950e354995d4ac9dd22a8 to your computer and use it in GitHub Desktop.
Save davidwebca/135f03e6fbf950e354995d4ac9dd22a8 to your computer and use it in GitHub Desktop.

Revisions

  1. davidwebca revised this gist Jun 24, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions lazyblock-tailwind-cache.php
    Original file line number Diff line number Diff line change
    @@ -32,6 +32,7 @@
    mkdir($cache_folder);
    }

    // Sometimes, editor side doesn't exist so we simply check if it's not empty before
    if(!empty(trim($context_code))) {
    file_put_contents(get_template_directory() . "/lzb-storage/{$block_slug}-{$context}.tmp", $context_code);
    }
  2. davidwebca revised this gist Jun 24, 2022. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions tailwind.config.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    /**
    * Add the folder and tmp files to the content
    * declaration in your tailwind.config.js file
    */
    module.exports = {
    content: [
    './lzb-storage/*.tmp' // Add this
    ],
    safelist: [
    ],
    theme: {
    extend: {}
    },
    plugins: [],
    };
  3. davidwebca revised this gist Jun 24, 2022. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion lazyblock-tailwind-cache.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,13 @@
    <?php

    /**
    * Allows HTML + Handlebars or database-saved PHP code to be
    * cached into files so that Tailwind CSS can detect and watch
    * for classes used and purge unecessary ones.
    *
    * @see https://github.com/nk-crew/lazy-blocks/
    * @see https://github.com/nk-crew/lazy-blocks/issues/246
    */
    add_action('save_post', function($post_ID, $post, $update) {
    if($post->post_type == 'lazyblocks') {
    // Account for both contexts
    @@ -21,7 +29,7 @@

    // Create the cache folder if it doesn't exist
    if(!is_dir($cache_folder)) {
    mkdir($cache_folder);
    mkdir($cache_folder);
    }

    if(!empty(trim($context_code))) {
  4. davidwebca created this gist Jun 24, 2022.
    33 changes: 33 additions & 0 deletions lazyblock-tailwind-cache.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    <?php

    add_action('save_post', function($post_ID, $post, $update) {
    if($post->post_type == 'lazyblocks') {
    // Account for both contexts
    $contexts = ['frontend', 'editor'];

    // Get output method
    $output_method = get_post_meta($post_ID, 'lazyblocks_code_output_method', true);

    // Get block slug to create legible tmp file name
    $block_slug = get_post_meta($post_ID, 'lazyblocks_slug', true);

    // Cache folder name
    $cache_folder = get_template_directory() . '/lzb-storage';

    // Only save tmp file if php or handlebars
    if($output_method == 'php' || $output_method == 'html') {
    foreach($contexts as $context) {
    $context_code = get_post_meta($post_ID, "lazyblocks_code_{$context}_html", true);

    // Create the cache folder if it doesn't exist
    if(!is_dir($cache_folder)) {
    mkdir($cache_folder);
    }

    if(!empty(trim($context_code))) {
    file_put_contents(get_template_directory() . "/lzb-storage/{$block_slug}-{$context}.tmp", $context_code);
    }
    }
    }
    }
    }, 10, 3);