Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a Timber.php Class and... #5

Open
cleverington opened this issue Jun 4, 2022 · 0 comments
Open

Create a Timber.php Class and... #5

cleverington opened this issue Jun 4, 2022 · 0 comments

Comments

@cleverington
Copy link
Owner

cleverington commented Jun 4, 2022

Alternative:

The Theme class in Theme.php could be refactored to do this, since its mostly Timber focused anyway. That said.... the requirements are also heavily ACF required, so... 🤷

Changes and/or things to put in Timber.php class

  • Move acf_block_gutenberg_callback() into it, so its only needed once (though, obviously, unique blocks can call their own).
  • Move elegant_get_timber_template_directories() into it, so its not a 'stand-alone' function on Theme.php and embarrassing me.
  • Move elegant_add_to_timber_paths() into it, so its not a 'stand-alone' function on Theme.php and embarrassing me.

New

Create a hook_load_point() and hook_save_point() function to leverage both the acf/settings/load_json filter and the acf/settings/save_json filter for the custom ACF blocks loaded within the Block directory.

Something like:

<?php

namespace Elegant\ACFLoader

if ( !acf_function()) {
  // Toss an error of 'ACF Pro Required'
  exit;
}

class ACFLoader {
  public function run() : void {
      
    // Update the ACF-JSON Load Point
    add_filter('acf/settings/load_json', array( $this, 'acf_json_load_point' ));

    // Update the ACF-JSON Save Point
    add_filter('acf/settings/save_json', array( $this, 'acf_json_save_point' ));
  }

  /**
   * ACF Compile Function
   *
   * Loads the default ACF file for this Module.
   *
   * Note: Any changes, when saved, will be saved in ACF's default save-point.
   *   This loader function only loads the default settings.
   *
   * @since 0.1.2
   * @param array $acf_paths - Current set of ACF directories.
   * @return array $acf_paths - New set of ACF directories to load.
   * @see https://www.advancedcustomfields.com/resources/local-json/
   * @see https://www.awesomeacf.com/snippets/load-acf-json-files-from-multiple-locations/
   */
  public static function acf_json_load_point( $acf_paths ) {
    // Do things to set the Load Point
    //  example pseudocode
     
    // Remove original path (optional).
    unset($acf_paths[0]);
    // Load Block ACFs.
    $block_paths = glob(get_template_directory() . '/inc/Block/' . "any" . '/' . "any" . '.php');
    
    foreach($block_paths as $block_path) {
      $block_name = basename($block_path);
      $file_name = get_template_directory() . '/inc/Block/' . "???" . '/' . $block_name . '.php'
    
      // This would require creating an /acf/ folder inside of each /Block/ folder.
      if (file_exists($file_name)) {
        $acf_paths[] = get_stylesheet_directory() . '/blocks/' . $block_name . '/acf/';
      }
    }

    // Append the path.
    $acf_paths[] = get_stylesheet_directory() . '/inc/acf/json';

    // Return the updated path.
    return $acf_paths;
  }

  /**
   * Helper Function to overwrite default acf-json folder.
   *
   * See previous function for notes.
   *
   * @since 1.0.1
   * @param string $path used by ACF to determine where acf.json files are saved.
   * @return string $path
   * @see https://www.advancedcustomfields.com/resources/local-json/#saving-explained
   */
  public static function acf_json_save_point( $path ) {
    // Do things to set the Save Point

    /**
     * So each custom block created by ACF has a { location: [param:block]}
     * 
     * The trick would be that IF 'block' is selected, then it needs to go in /Block.
     * Similarly, IF '<theme-setting>' is selected, it should be in (something like) /Module.
     * 
     * The second trick is: It is NOT an array, like the load_point.
     * 
     * Which means it would have to be reset-to-default EVERY time, in order not to lose anything.
     * /
  }
}
@cleverington cleverington changed the title Create a Timber.php Class and move acf_block_gutenberg_callback() into it, for cleaner code Create a Timber.php Class and... Jun 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant