cleverington / elegant

An OOP + ACF + Timber / TWIG based WordPress theme.
4 stars 0 forks source link

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

Open cleverington opened 2 years ago

cleverington commented 2 years ago

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... :shrug:

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

#

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.
     * /
  }
}