custom post type and ACF json syncing

This commit is contained in:
Steve Ross
2024-11-06 22:42:07 +00:00
parent d0733bfc01
commit feebb7c94a
8 changed files with 13592 additions and 0 deletions

60
app/ACF/Dynamic.php Normal file
View File

@@ -0,0 +1,60 @@
<?php
namespace App\ACF;
class Dynamic
{
public function __construct()
{
add_filter('acf/load_field/name=fontawesome_regular', [ $this, 'load_fontawesome_regular_icons' ]);
add_filter('acf/load_field/name=fontawesome_solid', [ $this, 'load_fontawesome_solid_icons' ]);
add_filter('acf/load_field/name=fontawesome_brands', [ $this, 'load_fontawesome_brand_icons' ]);
}
public function load_fontawesome_regular_icons( $field )
{
$field['choices'] = [];
$field['choices'] = $this->fontawesome_choices('regular');
return $field;
}
public function load_fontawesome_solid_icons( $field )
{
$field['choices'] = [];
$field['choices'] = $this->fontawesome_choices('solid');
return $field;
}
public function load_fontawesome_brand_icons( $field )
{
$field['choices'] = [];
$field['choices'] = $this->fontawesome_choices('brands');
return $field;
}
public function fontawesome_choices($set = 'solid')
{
$path = get_stylesheet_directory() . '/resources/json/font-awesome-' . $set . '.json';
$json = @file_get_contents($path);
if(!$json) return false;
$icons = json_decode($json, true);
$choices = [
'0' => '<i class="fa-solid"></i> <span>Please select an icon</span>',
];
foreach($icons as $slug => $props):
if(in_array($slug, range(0,9))) continue;
$choices[$slug] = '<i class="fa-'.$set.' fa-'.$slug.'" style="color: #2271b1;"></i> <span>' . (ucwords(str_replace('-', ' ', $slug))) . '</span>';
endforeach;
return $choices;
}
}

25
app/ACF/JSON.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
namespace App\ACF;
class JSON
{
public function __construct()
{
add_filter('acf/settings/save_json', [$this, 'save']);
add_filter('acf/settings/load_json', [$this, 'load']);
}
public function save( $path )
{
$path = get_stylesheet_directory() . '/acf-json';
return $path;
}
public function load( $paths )
{
unset($paths[0]);
$paths[] = get_stylesheet_directory() . '/acf-json';
return $paths;
}
}

22
app/Admin/Enqueue.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
namespace App\Admin;
class Enqueue
{
public function __construct()
{
add_action( 'admin_enqueue_scripts', [$this, 'fontawesome']);
}
public function fontawesome()
{
wp_enqueue_style(
'fontawesome',
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css',
false,
'6.5.2',
'all'
);
}
}

57
app/PostTypes/Social.php Normal file
View File

@@ -0,0 +1,57 @@
<?php
namespace App\PostTypes;
class Social
{
public function __construct()
{
add_action('init', [$this, 'register']);
}
public function register()
{
$td = 'sage';
$postType = 'social';
register_extended_post_type(
$postType,
[
'menu_position' => 28,
'supports' => [
'title',
'page-attributes',
],
'menu_icon' => 'dashicons-share',
'rewrite' => false,
'has_archive' => false,
'publicly_queryable' => false,
'exclude_from_search' => true,
'capability_type' => 'page',
'show_in_nav_menus' => false,
'admin_cols' => [
'social_link' => [
'title' => __('Social Link', $td),
'meta_key' => 'fontawesome_brands',
'function' => function(){
$icon = get_field('fontawesome_brands');
$url = get_field('url');
if($icon): ?>
<a
href="<?= $url ?: '#' ?>"
class="fa-brands fa-<?= $icon ?>"
rel="nofollow noindex"
style="font-size: 2em;"
></a>
<?php endif;
},
],
],
],
);
}
}