plugin install

This commit is contained in:
Tony Volpe
2024-06-18 17:29:05 -04:00
parent e1aaedd1ae
commit 41f50eacc4
5880 changed files with 1057631 additions and 39681 deletions

View File

@@ -0,0 +1,256 @@
<?php
namespace ACFGravityformsField;
use acf_field;
use GFAPI;
class Field extends acf_field
{
/**
* Make sure we can easily access our notices
*
* @var Notices
*/
public $notices;
/**
* Get our forms
*
* @var array
*/
public $forms;
public function __construct()
{
$this->name = 'forms';
$this->label = __('Gravity Form', 'gravityforms');
$this->category = __('Relational', 'acf');
$this->defaults = [
'return_format' => 'form_object',
'multiple' => 0,
'allow_null' => 0
];
// Get our notices up and running
$this->notices = new Notices();
// Execute the parent constructor as well
parent::__construct();
}
/**
* Create extra settings for our gravityforms field. These are visible when editing a field.
*
* @param $field
*/
public function render_field_settings($field)
{
// Render a field settings that will tell us if an empty field is allowed or not
acf_render_field_setting($field, [
'label' => __('Return Value', 'acf'),
'instructions' => __('Specify the returned value on front end', 'acf'),
'type' => 'radio',
'name' => 'return_format',
'layout' => 'horizontal',
'choices' => [
'post_object' => __('Form Object', 'acf-gravityforms-add-on'),
'id' => __('Form ID', 'acf-gravityforms-add-on')
],
]);
// Render a field setting that will tell us if an empty field is allowed or not.
acf_render_field_setting($field, [
'label' => __('Allow Null?', 'acf'),
'type' => 'radio',
'name' => 'allow_null',
'choices' => [
1 => __('Yes', 'acf'),
0 => __('No', 'acf'),
],
'layout' => 'horizontal'
]);
// Render a field setting that will tell us if multiple forms are allowed.
acf_render_field_setting($field, [
'label' => __('Select multiple values?', 'acf'),
'type' => 'radio',
'name' => 'multiple',
'choices' => [
1 => __('Yes', 'acf'),
0 => __('No', 'acf'),
],
'layout' => 'horizontal'
]);
}
/**
* Render our Gravity Form field with all the forms as options
*
* @param $field
* @return bool
*/
public function render_field($field)
{
if (class_exists('GFAPI')) {
$this->forms = GFAPI::get_forms(true, false, 'title');
}
// Set our defaults
$field = array_merge($this->defaults, $field);
$choices = [];
// Check if we have some valid forms
if (!$this->hasValidForms()) {
return false;
}
foreach ($this->forms as $form) {
$choices[$form['id']] = $form['title'];
}
// Override field settings and start rendering
$field['choices'] = $choices;
$field['type'] = 'select';
// Create a css id for our field
$fieldId = str_replace(['[', ']'], ['-', ''], $field['name']);
// Check if we're allowing multiple selections.
$hiddenField = '';
$multiple = '';
$field_options = '';
if ($field['multiple']) {
$hiddenField = '<input type="hidden" name="{$field[\'name\']}">';
$multiple = '[]" multiple="multiple" data-multiple="1';
}
// Check if we're allowing an empty form. If so, create a default option
if ($field['allow_null']) {
$field_options .= '<option value="">' . __('- Select a form -', 'acf-gravityforms-add-on') . '</option>';
}
// Loop trough all our choices
foreach ($field['choices'] as $formId => $formTitle) {
$selected = '';
if ((is_array($field['value']) && in_array($formId, $field['value'], false))
|| (int)$field['value'] === (int)$formId
) {
$selected = ' selected';
}
$field_options .= '<option value="' . $formId . '"' . $selected . '>' . $formTitle . '</option>';
}
// Start building the html for our field
$field_html = $hiddenField;
$field_html .= '<select id="' . $fieldId . '" name="' . $field['name'] . $multiple . '">';
$field_html .= $field_options;
$field_html .= '</select>';
echo apply_filters('acf-gravityforms-add-on/field_html', $field_html, $field, $field_options, $multiple);
}
/**
* Return a form object when not empty
*
* @param $value
* @param $postId
* @param $field
* @return array|bool
*/
public function format_value($value, $postId, $field)
{
return $this->processValue($value, $field);
}
/**
*
* This filter is applied to the $value before it is updated in the db
*
* @param $value - the value which will be saved in the database
* @param $post_id - the $post_id of which the value will be saved
* @param $field - the field array holding all the field options
*
* @return $value - the modified value
*/
public function update_value($value)
{
// Strip empty array values
if (is_array($value)) {
$value = array_values(array_filter($value));
}
return $value;
}
/**
* Check what to return on basis of return format
*
* @param $value
* @param $field
* @return array|bool|int
*/
public function processValue($value, $field)
{
if (is_array($value)) {
$formObjects = [];
foreach ($value as $key => $formId) {
$form = $this->processValue($formId, $field);
//Add it if it's not an error object
if ($form) {
$formObjects[$key] = $form;
}
}
// Return the form object
if (!empty($formObjects)) {
return $formObjects;
}
// Else return false
return false;
}
// Make sure field is an array
$field = (array)$field;
if (!empty($field['return_format']) && $field['return_format'] === 'id') {
return (int)$value;
}
$form = GFAPI::get_form($value);
//Return the form object if it's not an error object. Otherwise return false.
if (!is_wp_error($form)) {
return $form;
}
return false;
}
/**
* Check if we actually have forms that we can use for our field
*
* @return bool
*/
public function hasValidForms()
{
// Stop if Gravityforms is not active
if (!class_exists('GFAPI')) {
$this->notices->isGravityformsActive(true, true);
return false;
}
// Check if there are forms and set our choices
if (!$this->forms) {
$this->notices->hasActiveGravityForms(true, true);
return false;
}
return true;
}
}

View File

@@ -0,0 +1,197 @@
<?php
namespace ACFGravityformsField;
use acf_field;
use GFFormsModel;
class FieldForV4 extends acf_field
{
/**
* will hold info such as dir / path
*
* @var $settings
*/
public $settings;
/**
* will hold default field options
*
* @var array
*/
public $defaults;
/**
* Make sure we can easily access our notices
*
* @var Notices
*/
public $notices;
/**
* Get our forms
*
* @var array
*/
public $forms;
public function __construct()
{
$this->name = 'forms';
$this->label = __('Gravity Form', 'gravityforms');
$this->category = __('Relational', 'acf'); // Basic, Content, Choice, etc
$this->defaults = [
'return_format' => 'form_object',
'multiple' => 0,
'allow_null' => 0
];
// Get our notices up and running
$this->notices = new Notices();
// Execute the parent constructor as well
parent::__construct();
}
/**
* Create extra settings for our gravityforms field. These are visible when editing a field.
*
* @param $field
*/
public function create_options($field)
{
// defaults?
$field = array_merge($this->defaults, $field);
// key is needed in the field names to correctly save the data
$key = $field['name'];
// Create Field Options HTML
?>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php echo __('Return Value', 'acf'); ?></label>
</td>
<td>
<?php
do_action('acf/create_field', [
'type' => 'radio',
'name' => 'fields[' . $key . '][return_format]',
'value' => $field['return_format'],
'choices' => [
'post_object' => __('Form Object', 'acf-gravityforms-add-on'),
'id' => __('Form ID', 'acf-gravityforms-add-on')
],
'layout' => 'horizontal',
]); ?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php echo __('Allow Null?', 'acf'); ?></label>
</td>
<td>
<?php
do_action('acf/create_field', [
'type' => 'radio',
'name' => 'fields[' . $key . '][allow_null]',
'value' => $field['allow_null'],
'choices' => [
1 => __('Yes', 'acf'),
0 => __('No', 'acf'),
],
'layout' => 'horizontal',
]); ?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php echo __('Select multiple values?', 'acf'); ?></label>
</td>
<td>
<?php
do_action('acf/create_field', [
'type' => 'radio',
'name' => 'fields[' . $key . '][multiple]',
'value' => $field['multiple'],
'choices' => [
1 => __('Yes', 'acf'),
0 => __('No', 'acf'),
],
'layout' => 'horizontal',
]); ?>
</td>
</tr>
<?php
}
/**
* Render our Gravity Form field with all the forms as options
*
* @param $field
* @return bool
*/
public function create_field($field)
{
if (class_exists('GFFormsModel')) {
$this->forms = GFFormsModel::get_forms(true, false, 'title');
}
// Set our defaults
$field = array_merge($this->defaults, $field);
$choices = [];
// Check if we have some valid forms
$fieldObject = new Field();
if (!$fieldObject->hasValidForms()) {
return false;
}
foreach ($this->forms as $form) {
$choices[$form->id] = $form->title;
}
// Override field settings and start rendering
$field['choices'] = $choices;
$field['type'] = 'select';
do_action('acf/create_field', $field);
}
/**
* This filter is applied to the $value before it is updated in the db
*
* @param $value - the value which will be saved in the database
* @param $post_id - the $post_id of which the value will be saved
* @param $field - the field array holding all the field options
*
* @return $value - the modified value
*/
public function update_value($value, $post_id, $field)
{
// Strip empty array values
if (is_array($value)) {
$value = array_values(array_filter($value));
}
return $value;
}
/**
* Return a form object when not empty
*
* @param $value
* @param $postId
* @param $field
* @return array|bool
*/
public function format_value_for_api($value, $postId, $field)
{
$fieldObject = new Field();
return $fieldObject->processValue($value, $field);
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace ACFGravityformsField;
class Init
{
public function __construct()
{
$this->addHooks();
}
/**
* Make sure all hooks are being executed.
*/
private function addHooks()
{
add_action('acf/include_field_types', [$this, 'addField']);
add_action('acf/register_fields', [$this, 'addFieldforV4']);
add_action('admin_init', [$this, 'loadTextDomain']);
add_action('admin_init', [$this, 'addNotices']);
}
/**
* Initialize the notices
*/
public function addNotices()
{
$notices = new Notices();
$notices->addHooks();
}
/**
* Add a new Field object for our newest version in Advanced Custom Fields
*/
public function addField()
{
$field = new Field();
// Added 31.3.2022 to avoid errors when using “show in REST”
// https://wordpress.org/support/topic/fatal-error-when-show-in-rest-is-active/
if (function_exists('acf_register_field_type')) {
acf_register_field_type($field);
}
}
/**
* Add a new Field object for other versions (V4 in this case) of Advanced Custom Fields
*
*/
public function addFieldforV4()
{
new FieldForV4();
}
/**
* Load the gettext plugin textdomain located in our language directory.
*/
public function loadTextDomain()
{
load_plugin_textdomain('acf-gravityforms-add-on', false, ACF_GF_FIELD_LANGUAGES);
}
}

View File

@@ -0,0 +1,85 @@
<?php
namespace ACFGravityformsField;
use GFFormsModel;
class Notices
{
/**
* Get our forms
*
* @var array
*/
public $forms;
/**
* Make sure all hooks are being executed.
*/
public function addHooks()
{
add_action('admin_notices', [$this, 'isGravityFormsActive']);
add_action('admin_notices', [$this, 'isAdvancedCustomFieldsActive']);
}
/**
* Check if gravityforms is active. If not, issue a notice
*/
public function isGravityFormsActive($inline = '', $alt = '')
{
if (!class_exists('GFAPI')) {
$notice = sprintf(__(
'Warning: You need to <a href="%s">Activate Gravityforms</a> in order to use the Advanced Custom Fields: Gravityforms Add-on.',
'acf-gravityforms-add-on'
), admin_url('plugins.php'));
$this->createNotice($notice, $inline, $alt);
}
}
/**
* Check if there are any active gravityforms forms. If not, issue a notice
*/
public function hasActiveGravityForms($inline = '', $alt = '')
{
if (class_exists('GFFormsModel')) {
$this->forms = GFFormsModel::get_forms(true, false, 'title');
}
if (!$this->forms) {
$notice = sprintf(__(
' Warning: There are no active forms. You need to <a href="%s">Create a New Form</a> in order to use the Advanced Custom Fields: Gravityforms Add-on.',
'acf-gravityforms-add-on'
), admin_url('admin.php?page=gf_new_form'));
$this->createNotice($notice, $inline, $alt);
}
}
/**
* Check if advanced custom fields is active. If not, issue a notice
*/
public function isAdvancedCustomFieldsActive($inline = '', $alt = '')
{
if (!function_exists('get_field')) {
$notice = sprintf(__(
'Warning: You need to <a href="%s">Activate Advanced Custom Fields</a> in order to use the Advanced Custom Fields: Gravityforms Add-on.',
'acf-gravityforms-add-on'
), admin_url('plugins.php'));
$this->createNotice($notice, $inline, $alt);
}
}
/**
* A wrapper for all the notices.
*/
public function createNotice($notice, $inline = '', $alt = '')
{
$inline = $inline ? ' inline' : '';
$alt = $alt ? ' notice-alt' : '';
echo '<div class="notice notice-warning ' . $inline . $alt . '"><p>' . $notice . '</p></div>';
}
}