diff --git a/repeater2-for-gravity-forms.php b/repeater2-for-gravity-forms.php index 6df069c..0e51982 100644 --- a/repeater2-for-gravity-forms.php +++ b/repeater2-for-gravity-forms.php @@ -6,8 +6,10 @@ * Version: 2.0.0 * Author: Ciprian Popescu * Author URI: http://www.4property.com - * GitHub Plugin URI: https://github.com/4property/repeater2-for-gravity-forms + * GitHub Plugin URI: 4property/repeater2-for-gravity-forms * GitHub Branch: master + * License: GNU General Public License v3 or later + * License URI: https://www.gnu.org/licenses/gpl-3.0.html * * Based on Kodie Grantham's work * @url https://github.com/kodie/gravityforms-repeater2 @@ -19,91 +21,97 @@ /**/ define('GF_REPEATER_VERSION', '2.0.0'); -define('GF_REPEATER_PATH', basename(__DIR__).'/'.basename(__FILE__)); - -if (class_exists("GFForms")) { - GFForms::include_addon_framework(); - - class GFRepeater extends GFAddOn { - protected $_version = GF_REPEATER_VERSION; - protected $_min_gravityforms_version = "1.9"; - protected $_slug = "repeater2addon"; - protected $_path = GF_REPEATER_PATH; - protected $_full_path = __FILE__; - protected $_title = "Gravity Forms Repeater Add-On"; - protected $_short_title = "Repeater Add-On"; - - public function scripts() { - $scripts = array( - array( - "handle" => "gf_repeater2_js_admin", - "src" => $this->get_base_url() . "/js/gf-repeater2-admin.js", - "version" => $this->_version, - "deps" => array('jquery'), - "in_footer" => false, - "callback" => array($this, 'localize_scripts'), - "strings" => array('page' => rgget('page')), - "enqueue" => array( - array( - "admin_page" => array('form_editor', 'entry_view', 'entry_detail') - ) - ) - ) - ); - return array_merge(parent::scripts(), $scripts); - } - - public function init_admin() { - parent::init_admin(); - GF_Field_Repeater2::init_admin(); - GF_Field_Repeater2_End::init_admin(); - } - - public function init_frontend() { - parent::init_frontend(); - GF_Field_Repeater2::init_frontend(); - } - - public function upgrade($previous_version) { - if (version_compare($previous_version, '1.0.5') == -1) { - $forms = GFAPI::get_forms(true); - - foreach ($forms as $form) { - $entries = GFAPI::get_entries($form['id']); - $fields = GFAPI::get_fields_by_type($form, 'repeater2'); - - foreach ($entries as $entry) { - foreach ($fields as $field) { - if (array_key_exists($field['id'], $entry)) { - $dataArray = GFFormsModel::unserialize($entry[$field['id']]); - $dataUpdated = false; - - if (!is_array($dataArray)) { continue; } - - foreach ($dataArray as $repeater2ChildId=>$repeater2Child) { - foreach ($repeater2Child as $repeatedFieldId=>$repeatedField) { - if (!is_array($repeatedField)) { - if ($repeatedField !== '[gfRepeater-section]') { - $dataUpdated = true; - $dataArray[$repeater2ChildId][$repeatedFieldId] = Array($repeatedField); - } - } elseif (reset($repeatedField) == '[gfRepeater-section]') { - $dataUpdated = true; - $dataArray[$repeater2ChildId][$repeatedFieldId] = reset($repeatedField); - } - } - } - - if ($dataUpdated) { GFAPI::update_entry_field($entry['id'], $field['id'], maybe_serialize($dataArray)); } - } - } - } - } - } - } - } - new GFRepeater(); - - require_once('class-gf-field-repeater2.php'); - require_once('class-gf-field-repeater2-end.php'); -} \ No newline at end of file +define('GF_REPEATER_PATH', basename(__DIR__) . '/' . basename(__FILE__)); + +if (class_exists('GFForms')) { + GFForms::include_addon_framework(); + + class GFRepeater extends GFAddOn { + protected $_version = GF_REPEATER_VERSION; + protected $_min_gravityforms_version = '2.5'; + protected $_slug = 'repeater2addon'; + protected $_path = GF_REPEATER_PATH; + protected $_full_path = __FILE__; + protected $_title = 'Gravity Forms Repeater Add-On'; + protected $_short_title = 'Repeater Add-On'; + + public function scripts() { + $scripts = [ + [ + 'handle' => 'gf_repeater2_js_admin', + 'src' => $this->get_base_url() . '/js/gf-repeater2-admin.js', + 'version' => $this->_version, + 'deps' => ['jquery'], + 'in_footer' => false, + 'callback' => [$this, 'localize_scripts'], + 'strings' => ['page' => rgget('page')], + 'enqueue' => [ + [ + 'admin_page' => ['form_editor', 'entry_view', 'entry_detail'] + ] + ] + ] + ]; + + return array_merge(parent::scripts(), $scripts); + } + + public function init_admin() { + parent::init_admin(); + + GF_Field_Repeater2::init_admin(); + GF_Field_Repeater2_End::init_admin(); + } + + public function init_frontend() { + parent::init_frontend(); + + GF_Field_Repeater2::init_frontend(); + } + + public function upgrade($previous_version) { + if (version_compare($previous_version, '1.0.5') == -1) { + $forms = GFAPI::get_forms(true); + + foreach ($forms as $form) { + $entries = GFAPI::get_entries($form['id']); + $fields = GFAPI::get_fields_by_type($form, 'repeater2'); + + foreach ($entries as $entry) { + foreach ($fields as $field) { + if (array_key_exists($field['id'], $entry)) { + $dataArray = GFFormsModel::unserialize($entry[$field['id']]); + $dataUpdated = false; + + if (!is_array($dataArray)) { continue; } + + foreach ($dataArray as $repeater2ChildId=>$repeater2Child) { + foreach ($repeater2Child as $repeatedFieldId=>$repeatedField) { + if (!is_array($repeatedField)) { + if ($repeatedField !== '[gfRepeater-section]') { + $dataUpdated = true; + $dataArray[$repeater2ChildId][$repeatedFieldId] = Array($repeatedField); + } + } else if (reset($repeatedField) == '[gfRepeater-section]') { + $dataUpdated = true; + $dataArray[$repeater2ChildId][$repeatedFieldId] = reset($repeatedField); + } + } + } + + if ($dataUpdated) { + GFAPI::update_entry_field($entry['id'], $field['id'], maybe_serialize($dataArray)); + } + } + } + } + } + } + } + } + + new GFRepeater(); + + require_once 'class-gf-field-repeater2.php'; + require_once 'class-gf-field-repeater2-end.php'; +}