adminConfigHandler = $adminConfigHandler; $this->contactFormConfigHandler = $contactFormConfigHandler; $this->apiHandler = $apiHandler; } /*** * @return string */ public function getSlug() { return self::BLOCK_SLUG; } /** * @return \WPCF7_ContactForm|bool */ public function getContactForm() { $form = \WPCF7_ContactForm::get_current(); if (!$form) { return false; } return $form; } /** * @param array $panels * @return mixed */ public function registerCleverReachOptionPanel($panels) { if (!$this->getContactForm()) { return $panels; } $panels[self::PANEL_ID] = [ 'title' => __('CleverReach Integration', AID_CF7CR_TEXTDOMAIN), 'callback' => [$this, 'renderHtmlContent'] ]; return $panels; } /** * @return bool */ public function isCleverReachIntegrationEnabled() { $contactForm = $this->getContactForm(); $options = $this->loadOptions($contactForm); return !!$options['enabled']; } /** * @return Group[] */ public function getGroupSelection() { return $this->apiHandler->getGroups(); } /** * @param Group $group * @return bool */ public function isGroupSelected(Group $group) { $contactForm = $this->getContactForm(); $options = $this->loadOptions($contactForm); return $group->getId() == ($options['group_id'] ?? 0); } /** * @return Group|null */ public function getActiveGroup() { $groupSet = $this->getGroupSelection(); foreach ($groupSet as $group) { if ($this->isGroupSelected($group)) { return $group; } } return null; } /** * @return \AllInData\CF7CRIntegration\CleverReach\Model\Form[] */ public function getFormSelection() { $activeGroup = $this->getActiveGroup(); if (!$activeGroup) { return []; } return $this->apiHandler->getFormsByGroup($activeGroup); } /** * @param Form $form * @return bool */ public function isFormSelected(Form $form) { $contactForm = $this->getContactForm(); $options = $this->loadOptions($contactForm); return $form->getId() == ($options['form_id'] ?? 0); } /** * @return \AllInData\CF7CRIntegration\CleverReach\Model\Attribute[] */ public function getGroupAttributeSelection() { $attributeSet = $this->apiHandler->getAttributes(); $attribute = new Attribute(); $attribute->setId(1); $attribute->setGroupId(null); $attribute->setName(__('Email', AID_CF7CR_TEXTDOMAIN)); $attribute->setDescription(__('Email', AID_CF7CR_TEXTDOMAIN)); $attribute->setPreviewValue(null); $attribute->setDefaultValue(null); $attribute->setType('email'); $attribute->setTag(null); $attribute->setIsGlobal(true); $attributeSet[] = $attribute; return $attributeSet; } /** * @param int $fieldId * @param Attribute $attribute * @return bool */ public function isAttributeSelected($fieldId, Attribute $attribute) { $contactForm = $this->getContactForm(); $options = $this->loadOptions($contactForm); if (!isset($options['field_mappings'])) { return false; } return $attribute->getId() == ($options['field_mappings'][$fieldId] ?? 0); } /** * @return string[] */ public function getContactForm7FieldSelection() { $contactForm = $this->getContactForm(); $fieldSet = []; foreach ($contactForm->scan_form_tags() as $tag) { /** @var \WPCF7_FormTag $tag */ if (empty($tag['name']) || in_array($tag['basetype'], ['submit', 'button'])) { continue; } $fieldSet[$tag['name']] = $tag['name']; } return $fieldSet; } /** * Load template */ public function renderHtmlContent() { load_template(AID_CF7CR_TEMPLATE_DIR . 'contactform7/contactform7-cleverreach-settings.php'); } /** * @inheritdoc */ protected function doInit() { parent::doInit(); add_filter('wpcf7_editor_panels', [$this, 'registerCleverReachOptionPanel'], 10, 1); } /** * @param \WPCF7_ContactForm $contactForm * @return array */ private function loadOptions(\WPCF7_ContactForm $contactForm) { if (!empty($this->contactFormOptions)) { return $this->contactFormOptions; } $this->contactFormOptions = $this->contactFormConfigHandler->getOptions($contactForm->id()); return $this->contactFormOptions; } }