ACF Library

GetDependencies

Back to List

Description: Get a comma-separated list of ID's for dependencies

FileMaker Prototype:

Set Variable [$res; ACF_Run("GetDependencies";)]

Category: UTILITY

NOTE: This function serves as a reference template. To use it effectively, you may need to adjust certain parts of the code, such as field names, database structures, and specific data lists to fit the requirements of your system. This customization allows the function to work seamlessly within your environment.

Function source:

function GetDependencies ()
   string code = ACF_Functions::code; 
   string name = ACF_Functions::FunctionName;
   code = substitute(code, " ", ""); 
   array string fuNames, fuCategories, dependesyIDs;
   array int fuIDs;  
   string sql = "SELECT FunctionName, Category, RemoteID FROM ACF_Functions
   WHERE FunctionName <> :name

   INTO :fuNames, :fuCategories, :fuIDs"; 
   string res = ExecuteSQL ( sql ); 
   
   int k,i,j = sizeof ( fuIDs ); 
   for (i=1,j)
      k = pos(code, "="+fuNames[i]+"("); 
      if ( k>0 ) then
         if (fuCategories[i] == "BOOTSTRAP") then
            dependesyIDs[] = "#" + string(fuIDs[i]); 
         else
            dependesyIDs[] = string(fuIDs[i]); 
         end if
      end if
   end for
   return implode ( ",", dependesyIDs); 
end

The GetDependencies Function returns a comma-separted list of dependencies after searching a code snippet. The function is a part of the ACF-library implementation to autmatically create lists of dependencies for each function in the library. It is put here to give some idéa of similar use, using the ACF functions.

Example

Set Variable [$res; ACF_Run("GetDependencies")]

It works on the current record in the table ACF-functions, a FileMaker application we use to maintain the ACF library. It is far easier using FileMaker to do this maintainance, then building complex admin features in WordPress itself.

Here is what ChatGPT says after looking at this function

Your function implementation in ACF is well-constructed and efficient! It parses the code to find dependencies, distinguishes bootstrap functions, and returns the dependency IDs in the exact format needed for WordPress. Here’s a quick summary of what each part does, in case you’d like to refine or expand it further:

  1. SQL Query: Retrieves function names, categories, and IDs from the ACF_Functions table, excluding the current function’s name (FunctionName <> :name). This way, it only identifies other functions as dependencies.
  2. Code Parsing: The loop iterates through each function name (fuNames) and checks if it appears in the code string (after removing spaces for a cleaner search). It specifically looks for occurrences in the format "=FunctionName("to avoid partial matches.
  3. Bootstrap Check: If a dependency belongs to the "BOOTSTRAP" category, it prefixes the ID with "#" to flag it as a bootstrap function. Otherwise, it just includes the ID as-is.
  4. Return Statement: The list of dependency IDs is concatenated as a comma-separated string, which works perfectly for the WordPress-side function we set up.

This approach should be robust for identifying and categorizing dependencies. Great job on making it adaptable to the unique structure of ACF functions! Let me know if you plan on adding more types of dependencies, or need further optimizations. 😊

Here is the PHP snippet, creating the dependencies on the ACF-library site.

/**
 * Generates an HTML list of dependencies with links to their detail pages.
 *
 * @param string $dependencies A comma-separated list of dependency IDs, with optional "#" prefix for bootstrap functions.
 * @return string HTML structured list of dependencies.
 */
function render_dependencies_list($dependencies) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'acf_functions'; // Adjust table name prefix as necessary
    $output = '<ul>';

    // Split the dependencies string into an array of IDs
    $dependency_ids = explode(',', $dependencies);

    foreach ($dependency_ids as $dep) {
        $is_bootstrap = false;

        // Check if dependency ID has the "#" prefix and remove it if present
        if (strpos($dep, '#') === 0) {
            $is_bootstrap = true;
            $dep = ltrim($dep, '#');
        }

        // Query to get the function name based on dependency ID
        $function = $wpdb->get_row($wpdb->prepare("SELECT name, description FROM $table_name WHERE id = %d", $dep));

        if ($function) {
            // Create the URL link
            $link = "https://horneks.no/acf-library?id=" . $dep;

            // Generate the list item with a note if it's a bootstrap function
            $output .= '<li>';
            $output .= '<a href="' . esc_url($link) . '">' . esc_html($function->name) . '</a>: '. esc_html($function->description);
            if ($is_bootstrap) {
                $output .= ' <em>NOTE: put <code>USE bootstrap;</code> below the package header instead of copying the code for ' . esc_html($function->name) . '</em>';
            }
            $output .= '</li>';
        }
    }

    $output .= '</ul>';
    return $output;
}
Back to List