flexible-layout-preview-image-for-acf
Last commit date
assets
1 month ago
CHANGELOG.md
9 months ago
LICENSE.md
1 year ago
README.md
9 months ago
flexible-layout-preview-image-for-acf.php
1 month ago
readme.txt
1 month ago
flexible-layout-preview-image-for-acf.php
204 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin Name: Flexible Layout Preview Image for ACF |
| 4 | * Description: Adds flexible layout preview images for ACF in the WordPress admin. |
| 5 | * Version: 1.4.3 |
| 6 | * Author: Galaxy Weblinks |
| 7 | * License: GPLv2 or later |
| 8 | * License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 9 | * Author URI: https://www.galaxyweblinks.com/ |
| 10 | * Text Domain: flexible-layout-preview-image-for-acf |
| 11 | * Domain Path: /languages |
| 12 | * |
| 13 | * @package flexible_layout_preview-image_for_acf |
| 14 | */ |
| 15 | |
| 16 | // Exit if accessed directly. |
| 17 | if ( ! defined( 'ABSPATH' ) ) { |
| 18 | exit; |
| 19 | } |
| 20 | |
| 21 | // Define plugin constants. |
| 22 | define( 'FLEXIBLE_LAYOUT_PREVIEW_IMAGE_ACF_VERSION', '1.3' ); |
| 23 | define( 'FLEXIBLE_LAYOUT_PREVIEW_IMAGE_ACF_URL', plugin_dir_url( __FILE__ ) ); |
| 24 | define( 'FLEXIBLE_LAYOUT_PREVIEW_IMAGE_ACF_PATH', plugin_dir_path( __FILE__ ) ); |
| 25 | |
| 26 | /** |
| 27 | * Initialize hooks and actions. |
| 28 | */ |
| 29 | function flpi_acf_init() { |
| 30 | // Register assets. |
| 31 | add_action( 'admin_footer', 'flpi_acf_register_assets', 1 ); |
| 32 | add_action( 'admin_footer', 'flpi_acf_enqueue_assets' ); |
| 33 | |
| 34 | // Layout images. |
| 35 | add_action( 'acf/input/admin_footer', 'flpi_acf_layouts_images_style', 20 ); |
| 36 | |
| 37 | // Retrieve Flexible Keys. |
| 38 | add_action( 'acf/input/admin_head', 'flpi_acf_retrieve_flexible_keys', 1 ); |
| 39 | } |
| 40 | add_action( 'init', 'flpi_acf_init' ); |
| 41 | |
| 42 | /** |
| 43 | * Display the flexible layouts images related CSS for backgrounds. |
| 44 | */ |
| 45 | function flpi_acf_layouts_images_style() { |
| 46 | $images = flpi_acf_get_layouts_images(); |
| 47 | if ( empty( $images ) ) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | $css = "\n<style>"; |
| 52 | $css .= "\n\t /** Flexible Content Preview for Advanced Custom Fields: dynamic images */"; |
| 53 | foreach ( $images as $layout_key => $image_url ) { |
| 54 | $css .= sprintf( "\n\t .acf-fc-popup ul li a[data-layout=%s] .acf-fc-popup-image { background-image: url(\"%s\"); }", esc_attr( $layout_key ), esc_url( $image_url ) ); |
| 55 | } |
| 56 | $css .= "\n</style>\n"; |
| 57 | |
| 58 | echo $css; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get all ACF flexible content field layout keys. |
| 63 | * |
| 64 | * @return array Array of layout keys. |
| 65 | */ |
| 66 | function flpi_acf_retrieve_flexible_keys() { |
| 67 | $keys = array(); |
| 68 | $groups = acf_get_field_groups(); |
| 69 | if ( empty( $groups ) ) { |
| 70 | return $keys; |
| 71 | } |
| 72 | |
| 73 | foreach ( $groups as $group ) { |
| 74 | $fields = (array) acf_get_fields( $group ); |
| 75 | if ( ! empty( $fields ) ) { |
| 76 | flpi_acf_retrieve_flexible_keys_from_fields( $fields, $keys ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return $keys; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Recursively get ACF flexible content field layout keys from fields. |
| 85 | * |
| 86 | * @param array $fields Array of fields. |
| 87 | * @param array $keys Array of keys passed by reference. |
| 88 | */ |
| 89 | function flpi_acf_retrieve_flexible_keys_from_fields( $fields, &$keys ) { |
| 90 | foreach ( $fields as $field ) { |
| 91 | if ( 'flexible_content' === $field['type'] ) { |
| 92 | foreach ( $field['layouts'] as $layout_field ) { |
| 93 | // Don't revisit keys we've recorded already. |
| 94 | if ( ! empty( $keys[ $layout_field['key'] ] ) ) { |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | $keys[ $layout_field['key'] ] = $layout_field['name']; |
| 99 | |
| 100 | // Flexible content has a potentially recursive structure. Each layout has its own sub-fields that could in turn be flexible content. |
| 101 | if ( ! empty( $layout_field['sub_fields'] ) ) { |
| 102 | flpi_acf_retrieve_flexible_keys_from_fields( $layout_field['sub_fields'], $keys ); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get images for all flexible content field keys. |
| 111 | * |
| 112 | * @return array Array of flexible content field keys with associated image URLs. |
| 113 | */ |
| 114 | function flpi_acf_get_layouts_images() { |
| 115 | $layouts_images = array(); |
| 116 | $flexibles = flpi_acf_retrieve_flexible_keys(); |
| 117 | if ( empty( $flexibles ) ) { |
| 118 | return array(); |
| 119 | } |
| 120 | |
| 121 | foreach ( $flexibles as $flexible ) { |
| 122 | $layouts_images[ $flexible ] = flpi_acf_locate_image( $flexible ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Filter to allow adding/removing/changing flexible layout keys. |
| 127 | * |
| 128 | * @param array $layouts_images Array of flexible content field layout's keys with associated image URLs. |
| 129 | */ |
| 130 | return apply_filters( 'flexible_layout_preview-image_for_acf_images', $layouts_images ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Locate layout in the theme or plugin if needed. |
| 135 | * |
| 136 | * @param string $layout The layout name. Adds .jpg at the end of the file. |
| 137 | * |
| 138 | * @return false|string The URL of the image or a default image URL if not found. |
| 139 | */ |
| 140 | function flpi_acf_locate_image( $layout ) { |
| 141 | if ( empty( $layout ) ) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Filter to allow adding/removing/changing the path to images. |
| 147 | * |
| 148 | * @param string $path Path to check. |
| 149 | */ |
| 150 | $path = apply_filters( 'flexible_layout_preview-image_for_acf_images_path', 'lib/admin/images/flexible-layout-preview-image-for-acf' ); |
| 151 | |
| 152 | // Replace underscores with hyphens in the layout name. |
| 153 | $layout = str_replace( '_', '-', $layout ); |
| 154 | |
| 155 | // Construct the path to the image file. |
| 156 | $image_path = get_stylesheet_directory() . '/' . $path . '/' . $layout . '.jpg'; |
| 157 | $image_uri = get_stylesheet_directory_uri() . '/' . $path . '/' . $layout . '.jpg'; |
| 158 | |
| 159 | // Check if the image file exists in the theme directory. |
| 160 | if ( is_file( $image_path ) ) { |
| 161 | return $image_uri; |
| 162 | } |
| 163 | |
| 164 | // Return the default image URL if the specific image is not found. |
| 165 | return FLEXIBLE_LAYOUT_PREVIEW_IMAGE_ACF_URL . 'assets/images/default.jpg'; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Register assets. |
| 170 | */ |
| 171 | function flpi_acf_register_assets() { |
| 172 | wp_register_script( 'flpi-acf-flexible-layout-preview-script', FLEXIBLE_LAYOUT_PREVIEW_IMAGE_ACF_URL . 'assets/js/flexible-layout-preview-image-for-acf.js', array( 'jquery' ), FLEXIBLE_LAYOUT_PREVIEW_IMAGE_ACF_VERSION, true ); |
| 173 | wp_register_style( 'flpi-acf-flexible-layout-preview-style', FLEXIBLE_LAYOUT_PREVIEW_IMAGE_ACF_URL . 'assets/css/flexible-layout-preview-image-for-acf.css', array(), FLEXIBLE_LAYOUT_PREVIEW_IMAGE_ACF_VERSION ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Enqueue assets. |
| 178 | */ |
| 179 | function flpi_acf_enqueue_assets() { |
| 180 | wp_enqueue_script( 'flpi-acf-flexible-layout-preview-script' ); |
| 181 | wp_enqueue_style( 'flpi-acf-flexible-layout-preview-style' ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * You can use these filters to add custom links to your plugin row in the plugin list. |
| 186 | * @param $links, $file |
| 187 | * @return $links [array] |
| 188 | * @since 1.1 |
| 189 | */ |
| 190 | if (! function_exists('flpi_add_custom_plugin_links')) { |
| 191 | function flpi_add_custom_plugin_links($links, $file) |
| 192 | { |
| 193 | if (!isset($plugin)){ |
| 194 | $plugin = plugin_basename(__FILE__); |
| 195 | } |
| 196 | |
| 197 | if ($plugin == $file) { |
| 198 | $links[] = '<a href="https://wp-plugins.galaxyweblinks.com/wp-plugins/flexible-layout-preview-image-for-acf/doc/" target="_blank">Documentation</a>'; |
| 199 | $links[] = '<a href="https://wp-plugins.galaxyweblinks.com/contact/" target="_blank">Contact Support</a>'; |
| 200 | } |
| 201 | return $links; |
| 202 | } |
| 203 | } |
| 204 | add_filter('plugin_row_meta', 'flpi_add_custom_plugin_links', 10, 2); |