Plugin Directory

Changeset 3197083


Ignore:
Timestamp:
11/26/2024 06:52:59 AM (13 months ago)
Author:
navzme
Message:
  • [Added] support for GraphQL
  • [Fixed] issue with get_fields()
Location:
acf-galerie-4/trunk
Files:
4 added
3 edited

Legend:

Unmodified
Added
Removed
  • acf-galerie-4/trunk/acf-galerie-4.php

    r3179670 r3197083  
    99 * License: GPL v2 or later
    1010 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
    11  * Version: 1.2.0
     11 * Version: 1.3.0
    1212 * Domain Path: /lang
    1313 * Requires PHP: 7.4
     
    1919}
    2020
    21 define( 'ACFG4_VERSION', '1.2.0' );
     21define( 'ACFG4_VERSION', '1.3.0' );
    2222define( 'ACFG4_PLUGIN', __FILE__ );
    2323define( 'ACFG4_PLUGIN_BASENAME', plugin_basename( ACFG4_PLUGIN ) );
     
    2525define( 'ACFG4_PLUGIN_DIR', untrailingslashit( dirname( ACFG4_PLUGIN ) ) );
    2626
    27 //Load the text domain
     27/**
     28 * Loads the text domain for translation in the plugin.
     29 *
     30 * Hooks into the `init` action to:
     31 * - Load the plugin's text domain for translation, allowing the plugin to support
     32 *   multiple languages.
     33 * - Specifies the path to the translation files, located in the `lang` directory.
     34 * - The `acf-galerie-4` text domain is used for translation strings within the plugin.
     35 *
     36 * This ensures that any text strings in the plugin can be translated according
     37 * to the user's WordPress language settings.
     38 *
     39 * @return void
     40 */
    2841add_action('init', 'acfg4_textdomain');
    2942function acfg4_textdomain() {
     
    3144}
    3245
    33 //Registers the ACF field type.
     46/**
     47 * Registers the custom ACF field type during WordPress initialization.
     48 *
     49 * Hooks into the `init` action to:
     50 * - Check if the `acf_register_field_type` function is available.
     51 *   This ensures compatibility with ACF Pro 5.0 or higher.
     52 * - Require the file that defines the `acfg4_register_field_type` class.
     53 * - Register the custom field type (`acfg4_register_field_type`) with ACF.
     54 *
     55 * This setup ensures that the `galerie-4` custom field type is properly
     56 * initialized and available for use in ACF field groups.
     57 *
     58 * @return void
     59 */
    3460add_action( 'init', 'acfg4_init_register_type' );
    3561function acfg4_init_register_type() {
     
    3864    acf_register_field_type( 'acfg4_register_field_type' );
    3965}
     66
     67/**
     68 * Registers the custom ACF field type with WPGraphQL.
     69 *
     70 * Hooks into the `wpgraphql/acf/registry_init` action to initialize
     71 * support for the custom `galerie-4` ACF field type in WPGraphQL.
     72 *
     73 * The function:
     74 * - Requires the WPGraphQL integration class file.
     75 * - Instantiates the `acfg4_wpgraphql` class to register and configure
     76 *   the field type within the WPGraphQL schema.
     77 */
     78add_action( 'wpgraphql/acf/registry_init', function() {
     79    require_once __DIR__ . '/providers/class.wpgraphql.php';
     80    new acfg4_wpgraphql();
     81});
  • acf-galerie-4/trunk/class-acfg4-register-field-type.php

    r3179670 r3197083  
    2424
    2525    /**
    26      * Constructor.
     26     * Initializes the custom field type with its properties and defaults.
     27     *
     28     * This constructor sets up the essential information and configurations
     29     * for the custom ACF field type `galerie-4`. It defines:
     30     * - Name and label for identification and display in the UI.
     31     * - Category under which the field type is listed in the picker.
     32     * - Description for the field type, displayed in field settings.
     33     * - Links to documentation and tutorial resources for user guidance.
     34     * - Default settings specific to this field type.
     35     * - Environment settings, including the plugin version.
     36     *
     37     * Inherits functionality from the parent field type class.
    2738     */
    2839    public function __construct() {
     
    4455         * The category the field appears within in the field type picker.
    4556         */
    46         $this->category = 'content'; // basic | content | choice | relational | jquery | layout | CUSTOM GROUP NAME
     57        $this->category = 'content';
    4758
    4859        /**
     
    7586            'version' => ACFG4_VERSION,
    7687        );
    77 
     88       
    7889        parent::__construct();
    7990    }
     
    103114        if ( !empty( $field ) and !empty( $field['value'] ) ) {
    104115            $attachment_ids = array_map( 'intval', $field['value'] );
    105             $attachments = $this->get_attachments( $attachment_ids );
     116            $attachments = $this->transform( $attachment_ids );
    106117        }
    107118    ?>
     
    154165    }
    155166
     167    /**
     168     * Validates and processes the submitted field value before saving it.
     169     *
     170     * This function performs the following tasks:
     171     * - Verifies the nonce for security to ensure the request is valid.
     172     * - Checks the presence of the required POST data for the field.
     173     * - Sanitizes and processes the input value into an array of integers.
     174     *
     175     * If the nonce validation fails or required data is missing, the function
     176     * terminates execution to prevent further processing.
     177     *
     178     * @param mixed $value    The raw value submitted for the field.
     179     * @param int   $post_id  The ID of the post where the field is being updated.
     180     * @param array $field    The field configuration array, containing metadata about the field.
     181     *
     182     * @return array An array of sanitized integer values representing the field's data.
     183     */
    156184    function update_value( $value, $post_id, $field ) {
    157185        $field_type = $field['type'];
     
    176204    }
    177205
     206    /**
     207     * Formats the field value into a structured attachment data array.
     208     *
     209     * Converts raw attachment IDs into integer values, validates them, and
     210     * transforms the data into a detailed structure with attachment metadata.
     211     *
     212     * - If the input `$value` is empty, an empty array is returned.
     213     * - Valid attachment IDs are passed to the `transform` method for processing.
     214     *
     215     * @param mixed $value    The raw field value, typically an array of attachment IDs.
     216     * @param int   $post_id  The ID of the post where the field is being used.
     217     * @param array $field    The field configuration array.
     218     *
     219     * @return array Structured attachment data generated by the `transform` method.
     220     */
    178221    function format_value( $value, $post_id, $field ) {
    179         if ( empty($value) ) die();
    180 
    181222        $attachment_ids = array_map( 'intval', $value );
    182223
    183         return $this->get_attachments( $attachment_ids );
    184     }
    185 
    186     function get_attachments( $attachment_ids ){
    187         $attachments = get_posts(
    188             array(
    189                 'post_type' => 'attachment',
    190                 'post__in' => $attachment_ids,
    191                 'post_status' => 'inherit',
    192                 'orderby' => 'post__in',
    193                 'order' => 'ASC',
    194                 'numberposts' => -1
    195             )
    196         );
    197 
     224        if( empty( $attachment_ids ) ){
     225            return array();
     226        }
     227
     228        return $this->transform( $attachment_ids );
     229    }
     230
     231    /**
     232     * Transforms attachment IDs into structured attachment and metadata information.
     233     *
     234     * Fetches attachment data for the provided IDs and formats the output
     235     * to include detailed metadata such as file information, dimensions,
     236     * MIME types, and URLs for different image sizes.
     237     *
     238     * - Non-image attachments include basic file metadata such as MIME type and file size.
     239     * - Image attachments include metadata for the full image as well as additional sizes.
     240     *
     241     * Sensitive fields like `post_password` and `guid` are removed from the attachment object.
     242     *
     243     * @param array $attachment_ids List of attachment IDs to retrieve and transform.
     244     *
     245     * @return array A structured array containing attachment data and metadata.
     246     *               Each element includes:
     247     *               - 'attachment': The sanitized attachment object.
     248     *               - 'metadata': An array of metadata details (e.g., file size, URLs).
     249     */
     250    public function transform( $attachment_ids ){
    198251        $attachment_data = array();
     252        $attachments = $this->get_attachments( $attachment_ids );
    199253
    200254        if ( !empty( $attachments ) ) {
     
    247301    }
    248302   
     303    /**
     304     * Generates a sanitized CSS class name for an ACF field.
     305     *
     306     * Prepends a prefix to the provided class name and ensures it is properly escaped.
     307     *
     308     * @param string $class The class name to be prefixed and sanitized.
     309     *
     310     * @return string The sanitized and prefixed CSS class name.
     311     */
    249312    function add_class($class){
    250313        return esc_attr("acf-galerie-4-{$class}");
    251314    }
    252315
     316    /**
     317     * Prepares an array of HTML attributes for an ACF field.
     318     *
     319     * Generates attributes including an ID, a nonce for security,
     320     * and a CSS class for the ACF field.
     321     *
     322     * @param array $field The field configuration array containing field details.
     323     *
     324     * @return array Associative array of sanitized HTML attributes for the field.
     325     */
    253326    function add_attrs($field){
    254327        return array(
     
    295368        }
    296369    }
     370   
     371    /**
     372     * Retrieves attachment posts based on the provided attachment IDs.
     373     *
     374     * Fetches attachment posts matching the given IDs in their original order,
     375     * with a post type of 'attachment' and a status of 'inherit'.
     376     *
     377     * @param array $attachment_ids List of attachment post IDs to retrieve.
     378     *
     379     * @return array List of attachment posts that match the criteria.
     380     */
     381    public function get_attachments( $attachment_ids ){
     382        return get_posts(
     383            array(
     384                'post_type' => 'attachment',
     385                'post__in' => $attachment_ids,
     386                'post_status' => 'inherit',
     387                'orderby' => 'post__in',
     388                'order' => 'ASC',
     389                'numberposts' => -1
     390            )
     391        );
     392    }
     393   
    297394}
  • acf-galerie-4/trunk/readme.txt

    r3179670 r3197083  
    44Tags: acf, gallery, images, videos, media
    55Requires at least: 5.8
    6 Tested up to: 6.6
    7 Stable tag: 1.2.0
     6Tested up to: 6.7
     7Stable tag: 1.3.0
    88Requires PHP: 7.0
    99License: GPLv2 or later
     
    3636
    3737== Changelog ==
     38= 1.3.0 =
     39* [Added] support for GraphQL
     40* [Fixed] issue with get_fields()
     41
    3842= 1.2.0 =
    3943* [Added] support for taxonomy
Note: See TracChangeset for help on using the changeset viewer.