$39 GRAYBYTE WORDPRESS FILE MANAGER $99

SERVER : premium201.web-hosting.com #1 SMP Wed Mar 26 12:08:09 UTC 2025
SERVER IP : 104.21.15.130 | ADMIN IP 216.73.216.51
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : NONE

/home/bravetechrwanda/itiministry.org/plugins/give/src/FormMigration/Steps/

HOME
Current File : /home/bravetechrwanda/itiministry.org/plugins/give/src/FormMigration/Steps//FormFieldManager.php
<?php

namespace Give\FormMigration\Steps;

use Give\FormMigration\Contracts\FormMigrationStep;
use Give\Framework\Blocks\BlockModel;

class FormFieldManager extends FormMigrationStep
{

    /** @var array {0: BlockCollection, 1: string, 2: string|null} */
    private $inserter;

    /** @var array {fieldName: {field: array, block: BlockModel}} */
    private $fieldBlockRelationships = [];

    /**
     * Suppressed settings for all fields:
     * - "field_width" (Field Width)
     * - "css" (CSS Class Name)
     *
     * @since 3.0.0 added support for conditions based on Donation Amount.
     * @since 3.0.0
     */
    public function process()
    {
        $formFields = $this->formV2->getFormFields();

        if (!$formFields) {
            return;
        }

        $this->fieldBlockRelationships['give-amount'] = [
            'field' => [], // This is a core field and doesn't have any FFM settings.
            'block' => $this->fieldBlocks->findByName('givewp/donation-amount'),
        ];

        $this->inserter = $this->getInitialInserter();

        $map = [
            'checkbox' => [$this, 'addMultiSelectField'],
            'date' => [$this, 'addDateField'],
            'select' => [$this, 'addDropdownField'],
            'email' => [$this, 'addEmailField'],
            'file_upload' => [$this, 'addFileUploadField'],
            'hidden' => [$this, 'addHiddenField'],
            'html' => [$this, 'addHtmlField'],
            'multiselect' => [$this, 'addMultiSelectField'],
            'phone' => [$this, 'addPhoneField'],
            'radio' => [$this, 'addRadioField'],
            'text' => [$this, 'addTextField'],
            'textarea' => [$this, 'addTextareaField'],
            'url' => [$this, 'addUrlField'],
        ];

        foreach ($formFields as $field) {
            if ($field['input_type'] === 'section') {
                $this->addSection($field);
                continue;
            }

            if (!array_key_exists($field['input_type'], $map) || !$field['name']) {
                continue;
            }

            $method = $map[$field['input_type']];

            $block = $this->applyCommonAttributes($method($field), $field);
            $this->fieldBlockRelationships[$field['name']] = [
                'field' => $field,
                'block' => $block
            ];
            $this->insertBlock($block);
        }

        $this->mapConditionalLogicToBlocks();
    }

    /**
     * Suppressed settings for the Date field:
     * - "time" (Enable time input)
     * - "format_time" (Time Format)
     *
     * @since 3.0.0
     */
    private function addDateField($field): BlockModel
    {
        $dateFormatOrder = [
            'yyyy' => strpos(strtolower($field['format']), 'y'),
            'mm' => strpos(strtolower($field['format']), 'm'),
            'dd' => strpos(strtolower($field['format']), 'd'),
        ];
        asort($dateFormatOrder);
        $dateFormat = implode('/', array_keys($dateFormatOrder));

        return BlockModel::make([
            'name' => 'givewp-form-field-manager/date',
            'attributes' => [
                'dateFormat' => $dateFormat,
            ]
        ]);
    }

    /**
     * Suppressed settings for the Dropdown field:
     * - "first" (Select Text)
     *
     * @since 3.0.0
     */
    private function addDropdownField($field): BlockModel
    {
        $options = array_map(function ($option) use ($field) {
            return [
                'label' => $option,
                'value' => '',
                'checked' => array_key_exists('selected', $field) && $option === $field['selected'],
            ];
        }, array_filter($field['options']));

        return BlockModel::make([
            'name' => 'givewp-form-field-manager/dropdown',
            'attributes' => [
                'options' => $options,
            ]
        ]);
    }

    /**
     * Suppressed settings for the Email field:
     * - "maxlength" (Max Length)
     *
     * @since 3.0.0
     */
    private function addEmailField($field): BlockModel
    {
        return BlockModel::make([
            'name' => 'givewp-form-field-manager/email',
        ]);
    }

    /**
     * Suppressed settings for the File Upload field:
     * - "count" (Max. files)
     *
     * @since 3.0.0
     */
    private function addFileUploadField($field): BlockModel
    {
        $allowedFileTypes = array_map(function ($type) {
            switch ($type) {
                case 'images':
                    return 'image';
                default:
                    return $type;
            }
        }, $field['extension']);

        return BlockModel::make([
            'name' => 'givewp-form-field-manager/file-upload',
            'attributes' => [
                'maxFileSize' => $field['max_size'],
                'allowedFileTypes' => $allowedFileTypes,
            ]
        ]);
    }

    /**
     * @since 3.0.0
     */
    private function addHiddenField($field): BlockModel
    {
        return BlockModel::make([
            'name' => 'givewp-form-field-manager/hidden',
        ]);
    }

    /**
     * @since 3.0.0
     */

    private function addHtmlField($field): BlockModel
    {
        return BlockModel::make([
            'name' => 'givewp-form-field-manager/html',
            'attributes' => [
                'htmlCode' => $field['html'],
            ]
        ]);
    }

    /**
     * Suppressed settings for the Dropdown field:
     * - "first" (Select Text)
     *
     * @since 3.0.0
     */
    private function addMultiSelectField($field): BlockModel
    {
        $fieldType = $field['template'] === 'checkbox_field' ? 'checkbox' : 'dropdown';
        $options = array_map(function ($option) use ($field) {
            return [
                'label' => $option,
                'value' => '',
                'checked' => array_key_exists('selected', $field) && in_array($option, (array)$field['selected'], true),
            ];
        }, array_filter($field['options']));

        return BlockModel::make([
            'name' => 'givewp-form-field-manager/multi-select',
            'attributes' => [
                'fieldType' => $fieldType,
                'options' => $options,
            ]
        ]);
    }

    /**
     * @since 3.0.0
     */

    private function addPhoneField($field): BlockModel
    {
        $phoneFormat = $field['format'] === 'domestic' ? 'domestic' : 'unformatted';

        return BlockModel::make([
            'name' => 'givewp-form-field-manager/phone',
            'attributes' => [
                'phoneFormat' => $phoneFormat,
            ]
        ]);
    }

    /**
     * @since 3.0.0
     */

    private function addRadioField($field): BlockModel
    {
        $options = array_map(function ($option) use ($field) {
            return [
                'label' => $option,
                'value' => '',
                'checked' => array_key_exists('selected', $field) && $option === $field['selected'],
            ];
        }, array_filter($field['options']));

        return BlockModel::make([
            'name' => 'givewp-form-field-manager/radio',
            'attributes' => [
                'options' => $options,
            ]
        ]);
    }

    /**
     * Suppressed settings for the Text field:
     * - "maxlength" (Max Length)
     *
     * @since 3.0.0
     */
    private function addTextField($field): BlockModel
    {
        return BlockModel::make([
            'name' => 'givewp/text',
        ]);
    }

    /**
     * Suppressed settings for the Textarea field:
     * - "cols" (Columns)
     *
     * @since 3.0.0
     */
    private function addTextareaField($field): BlockModel
    {
        return BlockModel::make([
            'name' => 'givewp-form-field-manager/textarea',
            'attributes' => [
                'rows' => $field['rows'],
            ],
        ]);
    }

    /**
     * Suppressed settings for the Text field:
     * - "maxlength" (Max Length)
     *
     * @since 3.0.0
     */
    private function addUrlField($field): BlockModel
    {
        return BlockModel::make([
            'name' => 'givewp-form-field-manager/url',
        ]);
    }

    /**
     * @since 3.0.0
     */

    private function addSection($field): void
    {
        $block = BlockModel::make([
            'name' => 'givewp/section',
            'attributes' => [
                'title' => $field['label'],
                'description' => ''
            ]
        ]);

        list($blockCollection, $method) = $this->inserter;
        $this->inserter = [$block->innerBlocks, 'append'];
        $found = $this->fieldBlocks->findParentByBlockCollection($blockCollection);

        if (!$found) {
            $this->fieldBlocks->append($block);
            return;
        }

        list($parentBlock, $parentBlockIndex) = $found;

        if ($method === 'insertBefore') {
            $this->fieldBlocks->insertBefore(
                $parentBlock->name,
                $block,
                $parentBlockIndex
            );
        } else {
            $this->fieldBlocks->insertAfter(
                $parentBlock->name,
                $block,
                $parentBlockIndex
            );
        }
    }

    /**
     * @since 3.0.0
     */

    private function getInitialInserter(): array
    {
        $placement = $this->formV2->getFormFieldsPlacement();

        switch ($placement) {
            case 'give_before_donation_levels':
                $parentBlock = BlockModel::make([
                    'name' => 'givewp/section',
                ]);
                $this->fieldBlocks->prepend($parentBlock);
                return [$parentBlock->innerBlocks, 'append'];
            case 'give_payment_mode_top':
                return [$this->fieldBlocks, 'insertBefore', 'givewp/donation-summary'];
            case 'give_payment_mode_bottom':
                $parentBlock = $this->fieldBlocks->findParentByChildName('givewp/donation-summary');
                return [$parentBlock->innerBlocks, 'append'];
            case 'give_donation_form_before_personal_info':
                return [$this->fieldBlocks, 'insertBefore', 'givewp/donor-name'];
            case 'give_donation_form_after_personal_info':
                $parentBlock = $this->fieldBlocks->findParentByChildName('givewp/donor-name');
                return [$parentBlock->innerBlocks, 'append'];
            case 'give_donation_form_before_cc_form':
                return [$this->fieldBlocks, 'insertBefore', 'givewp/payment-gateways'];
            case 'give_donation_form_bottom':
            case 'give_donation_form_after_cc_form':
                $parentBlock = $this->fieldBlocks->findParentByChildName('givewp/payment-gateways');
                return [$parentBlock->innerBlocks, 'append'];
            case 'give_donation_form_top':
            case 'give_after_donation_levels':
            default:
                $parentBlock = $this->fieldBlocks->findParentByChildName('givewp/donation-amount');
                return [$parentBlock->innerBlocks, 'append'];
        }
    }

    /**
     * @since 3.0.0
     */

    private function applyCommonAttributes($block, $field): BlockModel
    {
        $protectedFieldNames = [
            'donation-amount',
            'donor-name',
            'email',
        ];

        if (in_array($field['name'], $protectedFieldNames, true)) {
            $field['name'] .= '_1';
        }

        $block->setAttribute('fieldName', $field['name']);
        $block->setAttribute('displayInAdmin', true);
        $block->setAttribute('displayInReceipt', true);
        $block->setAttribute('emailTag', "meta_donation_{$field['name']}");
        $block->setAttribute('metaUUID', $block->clientId);

        if (array_key_exists('required', $field)) {
            $block->setAttribute('isRequired', $field['required'] === 'yes');
        }

        if (array_key_exists('label', $field)) {
            $block->setAttribute('label', $field['label']);
        }

        if (array_key_exists('placeholder', $field)) {
            $block->setAttribute('placeholder', $field['placeholder']);
        }

        if (array_key_exists('help', $field)) {
            $block->setAttribute('description', $field['help']);
        }

        if (array_key_exists('default', $field)) {
            $block->setAttribute('defaultValue', $field['default']);
        }

        return $block;
    }

    /**
     * @since 3.0.0
     */

    private function insertBlock($block): void
    {
        list($blockCollection, $method, $target) = array_pad($this->inserter, 3, null);
        call_user_func_array([$blockCollection, $method], array_filter([$target, $block]));
    }

    /**
     * @since 3.0.0 Fixed missing conditionalLogic attribute on custom fields.
     * @since 3.0.0
     */
    private function mapConditionalLogicToBlocks(): void
    {
        foreach ($this->fieldBlockRelationships as $item) {
            ['field' => $field, 'block' => $block] = $item;

            // Initialize conditional logic support for custom fields.
            // The `conditionalLogic` attribute is used to signal support for conditional logic.
            $block->setAttribute('conditionalLogic', [
                'enabled' => give_is_setting_enabled($field['control_field_visibility']),
                'action' => 'show',
                'boolean' => 'and',
                'rules' => [],
            ]);

            if (!array_key_exists('control_field_visibility', $field)) {
                continue;
            }

            if(!isset($this->fieldBlockRelationships[$field['controller_field_name']])) {
                continue;
            }

            $referenceBlock = $this->fieldBlockRelationships[$field['controller_field_name']]['block'];

            $block->setAttribute('conditionalLogic', [
                'enabled' => true,
                'action' => 'show',
                'boolean' => 'and',
                'rules' => [
                    [
                        'field' => $referenceBlock->clientId,
                        'operator' => $field['controller_field_operator'],
                        'value' => $field['controller_field_value'],
                    ],
                ],
            ]);
        }
    }
}

Current_dir [ WRITEABLE ] Document_root [ NOT WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
25 May 2026 4.20 PM
bravetechrwanda / bravetechrwanda
0755
FormFields
--
25 May 2026 4.20 PM
bravetechrwanda / bravetechrwanda
0755
FormTemplate
--
25 May 2026 4.20 PM
bravetechrwanda / bravetechrwanda
0755
ActiveCampaign.php
1.088 KB
1 May 2024 11.40 PM
bravetechrwanda / bravetechrwanda
0644
ConstantContact.php
0.876 KB
10 Apr 2024 9.54 PM
bravetechrwanda / bravetechrwanda
0644
ConvertKit.php
1.075 KB
8 May 2024 6.09 PM
bravetechrwanda / bravetechrwanda
0644
CurrencySwitcher.php
1.107 KB
26 Jun 2024 8.32 PM
bravetechrwanda / bravetechrwanda
0644
DonationGoal.php
0.712 KB
16 Oct 2023 9.55 PM
bravetechrwanda / bravetechrwanda
0644
DonationOptions.php
2.396 KB
15 May 2024 7.28 PM
bravetechrwanda / bravetechrwanda
0644
DoubleTheDonation.php
0.892 KB
17 Apr 2024 11.51 PM
bravetechrwanda / bravetechrwanda
0644
EmailSettings.php
1.52 KB
16 Oct 2023 9.55 PM
bravetechrwanda / bravetechrwanda
0644
FeeRecovery.php
2.275 KB
16 Oct 2023 9.55 PM
bravetechrwanda / bravetechrwanda
0644
FormExcerpt.php
0.322 KB
10 Apr 2024 9.54 PM
bravetechrwanda / bravetechrwanda
0644
FormFeaturedImage.php
0.567 KB
1 Mar 2024 6.13 AM
bravetechrwanda / bravetechrwanda
0644
FormFieldManager.php
13.989 KB
16 Oct 2023 9.55 PM
bravetechrwanda / bravetechrwanda
0644
FormFields.php
1.367 KB
16 Oct 2023 9.55 PM
bravetechrwanda / bravetechrwanda
0644
FormGrid.php
0.62 KB
16 Oct 2023 9.55 PM
bravetechrwanda / bravetechrwanda
0644
FormMeta.php
1.064 KB
16 Oct 2023 9.55 PM
bravetechrwanda / bravetechrwanda
0644
FormTaxonomies.php
0.729 KB
28 Aug 2024 10.13 PM
bravetechrwanda / bravetechrwanda
0644
FormTitle.php
0.298 KB
16 Oct 2023 9.55 PM
bravetechrwanda / bravetechrwanda
0644
FundsAndDesignations.php
0.813 KB
11 Jan 2024 12.22 AM
bravetechrwanda / bravetechrwanda
0644
GiftAid.php
1.639 KB
9 Feb 2024 4.53 AM
bravetechrwanda / bravetechrwanda
0644
Mailchimp.php
1.288 KB
10 Apr 2024 9.54 PM
bravetechrwanda / bravetechrwanda
0644
MigrateMeta.php
0.329 KB
16 Oct 2023 9.55 PM
bravetechrwanda / bravetechrwanda
0644
OfflineDonations.php
0.739 KB
16 Oct 2023 9.55 PM
bravetechrwanda / bravetechrwanda
0644
PaymentGateways.php
0.775 KB
16 Oct 2023 9.55 PM
bravetechrwanda / bravetechrwanda
0644
PdfSettings.php
2.402 KB
16 Oct 2023 9.55 PM
bravetechrwanda / bravetechrwanda
0644
PerFormGateways.php
1.196 KB
9 Feb 2024 4.53 AM
bravetechrwanda / bravetechrwanda
0644
RazorpayPerFormSettings.php
1.64 KB
18 Jul 2024 12.34 AM
bravetechrwanda / bravetechrwanda
0644
RecurringDonationOptions.php
5.927 KB
16 Oct 2023 9.55 PM
bravetechrwanda / bravetechrwanda
0644
TermsAndConditions.php
0.591 KB
16 Oct 2023 9.55 PM
bravetechrwanda / bravetechrwanda
0644

GRAYBYTE WORDPRESS FILE MANAGER @ 2026 CONTACT ME
Static GIF Static GIF