add-on class base name. */ function agrfuxd_wpai_field_class_map() { return array( 'enhanced_gallery' => 'Gallery', 'enhanced_repeater' => 'Repeater', ); } /** * Namespaces the ACF Add-On has shipped its field classes under * * @return array */ function agrfuxd_wpai_field_namespaces() { return array( '\\wpai_acf_add_on\\acf\\fields\\', '\\wpai_acf_add_on_pro\\fields\\acf\\', '\\pmai_acf_add_on\\fields\\', ); } /** * Find the add-on's importer class for a base name such as "Repeater" * * Returns the un-suffixed class name. FieldFactory appends the "V5"/"V4" * variant itself, so we must not do it here. * * @param string $base Class base name ("Gallery", "Repeater"). * @return string|false Fully qualified class name, or false when the add-on is * not loaded or has been restructured. */ function agrfuxd_wpai_locate_field_class($base) { foreach (agrfuxd_wpai_field_namespaces() as $namespace) { $class = $namespace . 'Field' . $base; if (class_exists($class) || class_exists($class . 'V5') || class_exists($class . 'V4')) { return $class; } } return false; } /** * Tell WP All Import which class to use for our field types * * Available since ACF Add-On 3.3. * * @param string $class Class name the add-on resolved. * @param array $field_data ACF field array. * @param mixed $post Import post data. * @param string $field_name Field name. * @param mixed $field_parent Parent field object. * @return string */ function agrfuxd_wpai_acf_field_class($class, $field_data, $post = null, $field_name = '', $field_parent = false) { if (empty($field_data['type'])) { return $class; } $map = agrfuxd_wpai_field_class_map(); if (!isset($map[$field_data['type']])) { return $class; } $located = agrfuxd_wpai_locate_field_class($map[$field_data['type']]); return $located ? $located : $class; } add_filter('wp_all_import_acf_field_class', 'agrfuxd_wpai_acf_field_class', 10, 5); /** * Support for ACF Add-On versions older than 3.3 * * Those releases have no wp_all_import_acf_field_class filter - FieldFactory * builds the class name from the field type and instantiates it directly. An * alias under the name it looks for makes our types resolve to the add-on's own * classes without touching the add-on. */ function agrfuxd_wpai_register_field_class_aliases() { foreach (agrfuxd_wpai_field_class_map() as $type => $base) { // enhanced_repeater -> FieldEnhancedRepeater, the name FieldFactory // derives from the ACF field type. $alias_base = 'Field' . str_replace(' ', '', ucwords(str_replace('_', ' ', $type))); foreach (agrfuxd_wpai_field_namespaces() as $namespace) { $namespace = ltrim($namespace, '\\'); foreach (array('V5', 'V4', '') as $variant) { $target = $namespace . 'Field' . $base . $variant; $alias = $namespace . $alias_base . $variant; if (class_exists($target) && !class_exists($alias, false)) { class_alias($target, $alias); } } } } } add_action('init', 'agrfuxd_wpai_register_field_class_aliases', 5); /** * Point the add-on at its own view for our field types * * The add-on resolves the mapping UI from the instantiated object's $type, so * returning its Gallery/Repeater class already yields the right view. These * filters only matter if a future release keys the view off the ACF field type * instead. * * @param string $file_path Resolved view path. * @param object $field Add-on field object. * @return string */ function agrfuxd_wpai_acf_field_view_path($file_path, $field) { if (is_file($file_path)) { return $file_path; } if (!is_object($field) || !isset($field->type)) { return $file_path; } $map = agrfuxd_wpai_field_class_map(); if (!isset($map[$field->type])) { return $file_path; } $native = strtolower($map[$field->type]); // ".../views/enhanced_repeater/enhanced_repeater-v5.php" -> ".../views/repeater/repeater-v5.php" $candidate = str_replace($field->type, $native, $file_path); return is_file($candidate) ? $candidate : $file_path; } add_filter('wp_all_import_acf_field_view_path_enhanced_gallery', 'agrfuxd_wpai_acf_field_view_path', 10, 2); add_filter('wp_all_import_acf_field_view_path_enhanced_repeater', 'agrfuxd_wpai_acf_field_view_path', 10, 2);