resources
10 months ago
DonationHistoryWidget.php
10 months ago
DonationReceiptWidget.php
10 months ago
GiveDonorWallWidget.php
10 months ago
GiveFormGridWidget.php
9 months ago
GiveFormWidget.php
10 months ago
GiveGoalWidget.php
10 months ago
GiveLoginWidget.php
10 months ago
GiveMultiFormGoalWidget.php
10 months ago
GiveProfileEditorWidget.php
10 months ago
GiveRegisterWidget.php
10 months ago
GiveSubscriptionsWidget.php
10 months ago
GiveTotalsWidget.php
10 months ago
GiveFormWidget.php
427 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\ThirdPartySupport\Elementor\Widgets\V1; |
| 4 | |
| 5 | use Give\DonationForms\Models\DonationForm; |
| 6 | use Give\Framework\Database\DB; |
| 7 | use Give\Helpers\Form\Utils; |
| 8 | use Elementor\Widget_Base; |
| 9 | use Give\ThirdPartySupport\Elementor\Actions\RegisterWidgetEditorScripts; |
| 10 | |
| 11 | /** |
| 12 | * Elementor Give Form Widget. |
| 13 | * |
| 14 | * Elementor widget that inserts the GiveWP [give_form] shortcode to output a form total with options. |
| 15 | * |
| 16 | * @since 4.7.0 migrated from givewp-elementor-widgets |
| 17 | */ |
| 18 | class GiveFormWidget extends Widget_Base |
| 19 | { |
| 20 | /** |
| 21 | * Get widget name. |
| 22 | * |
| 23 | * Retrieve Give Form widget name. |
| 24 | * |
| 25 | * @since 4.7.0 migrated from givewp-elementor-widgets |
| 26 | * @access public |
| 27 | * |
| 28 | * @return string Widget name. |
| 29 | */ |
| 30 | public function get_name() |
| 31 | { |
| 32 | return 'Give Form'; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get widget title. |
| 37 | * |
| 38 | * Retrieve Give Form widget title. |
| 39 | * |
| 40 | * @since 4.7.0 migrated from givewp-elementor-widgets |
| 41 | * @access public |
| 42 | * |
| 43 | * @return string Widget title. |
| 44 | */ |
| 45 | public function get_title() |
| 46 | { |
| 47 | return __('Give Donation Form (Legacy)', 'give'); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get widget icon. |
| 52 | * |
| 53 | * Retrieve Give Form widget icon. |
| 54 | * |
| 55 | * @since 4.7.0 migrated from givewp-elementor-widgets |
| 56 | * @access public |
| 57 | * |
| 58 | * @return string Widget icon. |
| 59 | */ |
| 60 | public function get_icon() |
| 61 | { |
| 62 | return 'give-icon'; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get widget categories. |
| 67 | * |
| 68 | * Retrieve the list of categories the Give Form widget belongs to. |
| 69 | * |
| 70 | * @since 4.7.0 migrated from givewp-elementor-widgets |
| 71 | * @access public |
| 72 | * |
| 73 | * @return array Widget categories. |
| 74 | */ |
| 75 | public function get_categories() |
| 76 | { |
| 77 | return ['givewp-category-legacy']; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Widget inner wrapper. |
| 82 | * |
| 83 | * Use optimized DOM structure, without the inner wrapper. |
| 84 | * |
| 85 | * @since 4.7.0 migrated from givewp-elementor-widgets |
| 86 | * @access public |
| 87 | */ |
| 88 | public function has_widget_inner_wrapper(): bool |
| 89 | { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Register Give Form widget controls. |
| 95 | * |
| 96 | * Adds different input fields to allow the user to change and customize the widget settings. |
| 97 | * |
| 98 | * @since 4.7.0 migrated from givewp-elementor-widgets |
| 99 | * @access protected |
| 100 | */ |
| 101 | protected function register_controls() |
| 102 | { |
| 103 | $forms = $this->getDonationFormsOptions(); |
| 104 | $legacyForms = $this->getLegacyForms($forms); |
| 105 | $classicForms = $this->getClassicForms($forms); |
| 106 | $v3Forms = $this->getV3Forms($forms); |
| 107 | |
| 108 | $this->start_controls_section( |
| 109 | 'give_form_settings', |
| 110 | [ |
| 111 | 'label' => __('GiveWP Form Widget', 'give'), |
| 112 | 'tab' => \Elementor\Controls_Manager::TAB_CONTENT, |
| 113 | ] |
| 114 | ); |
| 115 | |
| 116 | $this->add_control( |
| 117 | 'form_id', |
| 118 | [ |
| 119 | 'label' => __('Form ID', 'give'), |
| 120 | 'type' => \Elementor\Controls_Manager::SELECT, |
| 121 | 'description' => __('Choose the GiveWP Form you want to embed.', 'give'), |
| 122 | 'default' => '', |
| 123 | 'options' => $forms, |
| 124 | ] |
| 125 | ); |
| 126 | |
| 127 | $this->add_control( |
| 128 | 'show_title', |
| 129 | [ |
| 130 | 'label' => __('Show Form Title', 'give'), |
| 131 | 'type' => \Elementor\Controls_Manager::SWITCHER, |
| 132 | 'description' => __('Show/hide the GiveWP form title.', 'give'), |
| 133 | 'label_on' => __('Show', 'give'), |
| 134 | 'label_off' => __('Hide', 'give'), |
| 135 | 'default' => 'yes', |
| 136 | 'conditions' => [ |
| 137 | 'terms' => [ |
| 138 | [ |
| 139 | 'name' => 'form_id', |
| 140 | 'operator' => '!in', |
| 141 | 'value' => $classicForms, |
| 142 | ], |
| 143 | ], |
| 144 | ], |
| 145 | ] |
| 146 | ); |
| 147 | |
| 148 | $this->add_control( |
| 149 | 'show_goal', |
| 150 | [ |
| 151 | 'label' => __('Show Goal', 'give'), |
| 152 | 'type' => \Elementor\Controls_Manager::SWITCHER, |
| 153 | 'description' => __('Show/hide the progress bar and goal for this form.', 'give'), |
| 154 | 'label_on' => __('Show', 'give'), |
| 155 | 'label_off' => __('Hide', 'give'), |
| 156 | 'return_value' => 'yes', |
| 157 | 'default' => 'yes', |
| 158 | ] |
| 159 | ); |
| 160 | |
| 161 | $this->add_control( |
| 162 | 'show_content', |
| 163 | [ |
| 164 | 'label' => __('Show Form Content', 'give'), |
| 165 | 'type' => \Elementor\Controls_Manager::SWITCHER, |
| 166 | 'description' => __('Show/hide the content of this form.', 'give'), |
| 167 | 'label_on' => __('Show', 'give'), |
| 168 | 'label_off' => __('Hide', 'give'), |
| 169 | 'return_value' => 'yes', |
| 170 | 'default' => 'no', |
| 171 | 'conditions' => [ |
| 172 | 'terms' => [ |
| 173 | [ |
| 174 | 'name' => 'form_id', |
| 175 | 'operator' => 'in', |
| 176 | 'value' => $legacyForms, |
| 177 | ], |
| 178 | ], |
| 179 | ], |
| 180 | ] |
| 181 | ); |
| 182 | |
| 183 | $this->add_control( |
| 184 | 'display_style', |
| 185 | [ |
| 186 | 'label' => __('Form Display Style', 'give'), |
| 187 | 'type' => \Elementor\Controls_Manager::SELECT, |
| 188 | 'description' => __('Choose which display to use for this GiveWP form.', 'give'), |
| 189 | 'options' => [ |
| 190 | 'onpage' => __('Full Form', 'give'), |
| 191 | 'button' => __('Button Only', 'give'), |
| 192 | 'modal' => __('Modal Reveal', 'give'), |
| 193 | 'reveal' => __('Reveal', 'give'), |
| 194 | ], |
| 195 | 'default' => 'onpage', |
| 196 | ] |
| 197 | ); |
| 198 | |
| 199 | $this->add_control( |
| 200 | 'continue_button_title', |
| 201 | [ |
| 202 | 'label' => __('Reveal Button Text', 'give'), |
| 203 | 'type' => \Elementor\Controls_Manager::TEXT, |
| 204 | 'description' => __('Text on the button that reveals the form.', 'give'), |
| 205 | 'default' => __('Continue to Donate', 'give'), |
| 206 | 'condition' => [ |
| 207 | 'display_style!' => 'onpage', |
| 208 | ], |
| 209 | ] |
| 210 | ); |
| 211 | |
| 212 | $this->add_control( |
| 213 | 'v3_notice', |
| 214 | [ |
| 215 | 'label' => __('Important Note', 'give'), |
| 216 | 'type' => \Elementor\Controls_Manager::RAW_HTML, |
| 217 | 'raw' => esc_html__( |
| 218 | 'Form Display Style changes will not be visible for Donation forms created using the Visual Form Builder. Save the page and view it on the front end.', |
| 219 | 'give' |
| 220 | ), |
| 221 | 'content_classes' => 'give-elementor-notice', |
| 222 | 'conditions' => [ |
| 223 | 'terms' => [ |
| 224 | [ |
| 225 | 'name' => 'form_id', |
| 226 | 'operator' => 'in', |
| 227 | 'value' => $v3Forms, |
| 228 | ], |
| 229 | ], |
| 230 | ], |
| 231 | ] |
| 232 | ); |
| 233 | |
| 234 | $this->add_control( |
| 235 | 'give_form_info', |
| 236 | [ |
| 237 | 'label' => '', |
| 238 | 'type' => \Elementor\Controls_Manager::RAW_HTML, |
| 239 | 'content_classes' => 'give-info', |
| 240 | 'raw' => ' |
| 241 | <div class="give"> |
| 242 | <p class="info-head"> |
| 243 | ' . __('GIVEWP FORM WIDGET', 'give') . '</p> |
| 244 | <p class="info-message">' . __( |
| 245 | 'This is the GiveWP Form widget. Choose which form you want to embed on this page with it\'s form "ID".', |
| 246 | 'give' |
| 247 | ) . '</p> |
| 248 | <p class="give-docs-links"> |
| 249 | <a href="https://givewp.com/documentation/core/shortcodes/give_form/?utm_source=plugin_settings&utm_medium=referral&utm_campaign=Free_Addons&utm_content=givelementor" rel="noopener noreferrer" target="_blank"><i class="fa fa-book" aria-hidden="true"></i>' . __( |
| 250 | 'Visit the GiveWP Docs for more info on the GiveWP Form.', |
| 251 | 'give' |
| 252 | ) . '</a> |
| 253 | </p> |
| 254 | </div>', |
| 255 | ] |
| 256 | ); |
| 257 | |
| 258 | $this->end_controls_section(); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Render the [give_form] output on the frontend. |
| 263 | * |
| 264 | * Written in PHP and used to generate the final HTML. |
| 265 | * |
| 266 | * @since 4.7.0 migrated from givewp-elementor-widgets |
| 267 | * @access protected |
| 268 | */ |
| 269 | protected function render() |
| 270 | { |
| 271 | $settings = $this->get_data('settings'); |
| 272 | |
| 273 | $formId = (int)$this->get_settings('form_id'); |
| 274 | $showTitle = isset($settings['show_title']) ? $settings['show_title'] : 'true'; |
| 275 | $showGoal = isset($settings['show_goal']) ? $settings['show_goal'] : 'true'; |
| 276 | $showContent = isset($settings['show_content']) ? $settings['show_content'] : 'true'; |
| 277 | $displayStyle = isset($settings['display_style']) ? $settings['display_style'] : 'onpage'; |
| 278 | $continueButtonTitle = isset($settings['continue_button_title']) ? $settings['continue_button_title'] : __('Continue to Donate', 'give'); |
| 279 | |
| 280 | if (isset($_POST['action']) && $_POST['action'] === 'elementor_ajax') { |
| 281 | // is this v3 form? |
| 282 | if (Utils::isV3Form($formId)) { |
| 283 | if ($donationForm = DonationForm::find($formId)) { |
| 284 | $donationForm->settings->showHeading = boolval($showTitle); |
| 285 | $donationForm->settings->enableDonationGoal = boolval($showGoal); |
| 286 | $donationForm->save(); |
| 287 | } |
| 288 | } else { |
| 289 | // For some strange reason, passing show_goal attr to give_form shortcode doesn't work, so in order for this to work we have to enable/disable goal by updating meta |
| 290 | give_update_meta($formId, '_give_goal_option', $showGoal ? 'enabled' : 'disabled'); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | $shortcode = sprintf( |
| 295 | '[give_form id="%s" show_title="%s" show_goal="%s" show_content="%s" display_style="%s" continue_button_title="%s"]', |
| 296 | $formId, |
| 297 | $showTitle, |
| 298 | $showGoal, |
| 299 | $showContent, |
| 300 | $displayStyle, |
| 301 | $continueButtonTitle |
| 302 | ); |
| 303 | |
| 304 | echo '<div class="givewp-elementor-widget give-form-shortcode-wrap">'; |
| 305 | |
| 306 | echo do_shortcode($shortcode); |
| 307 | |
| 308 | echo '</div>'; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * @return array |
| 313 | */ |
| 314 | private function getDonationFormsOptions() |
| 315 | { |
| 316 | $options = []; |
| 317 | |
| 318 | $forms = DB::table('posts') |
| 319 | ->select('ID', 'post_title') |
| 320 | ->where('post_type', 'give_forms') |
| 321 | ->where('post_status', 'publish') |
| 322 | ->getAll(); |
| 323 | |
| 324 | foreach ($forms as $form) { |
| 325 | $options[$form->ID] = $form->post_title; |
| 326 | } |
| 327 | |
| 328 | return $options; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Get forms using legacy template from list of forms returned by GiveFormWidget::getDonationFormsOptions |
| 333 | * |
| 334 | * @since 4.7.0 |
| 335 | * |
| 336 | * @param array $forms |
| 337 | * |
| 338 | * @return array |
| 339 | */ |
| 340 | private function getLegacyForms($forms) |
| 341 | { |
| 342 | $data = []; |
| 343 | |
| 344 | foreach (array_keys($forms) as $formId) { |
| 345 | if ('legacy' === $this->getFormTemplate($formId)) { |
| 346 | $data[] = (string)$formId; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | return $data; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Get forms using classic template from list of forms returned by GiveFormWidget::getDonationFormsOptions |
| 355 | * |
| 356 | * @since 4.7.0 |
| 357 | * |
| 358 | * @param array $forms |
| 359 | * |
| 360 | * @return array |
| 361 | */ |
| 362 | private function getClassicForms($forms) |
| 363 | { |
| 364 | $data = []; |
| 365 | |
| 366 | foreach (array_keys($forms) as $formId) { |
| 367 | if ('classic' === $this->getFormTemplate($formId)) { |
| 368 | $data[] = (string)$formId; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | return $data; |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Get v3 forms from list of forms returned by GiveFormWidget::getDonationFormsOptions |
| 377 | * |
| 378 | * @since 4.7.0 |
| 379 | * |
| 380 | * @param array $forms |
| 381 | * |
| 382 | * @return array |
| 383 | */ |
| 384 | private function getV3Forms($forms) |
| 385 | { |
| 386 | $data = []; |
| 387 | |
| 388 | foreach (array_keys($forms) as $formId) { |
| 389 | if (Utils::isV3Form((int)$formId)) { |
| 390 | $data[] = (string)$formId; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | return $data; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Get form template |
| 399 | * |
| 400 | * @since 4.7.0 migrated from givewp-elementor-widgets |
| 401 | * |
| 402 | * @param $formId |
| 403 | * |
| 404 | * @return string |
| 405 | */ |
| 406 | private function getFormTemplate($formId) |
| 407 | { |
| 408 | return Give()->form_meta->get_meta($formId, '_give_form_template', true); |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * @since 4.7.1 |
| 413 | */ |
| 414 | public function get_script_depends(): array |
| 415 | { |
| 416 | return [RegisterWidgetEditorScripts::LEGACY_GIVE_FORM_WIDGET_SCRIPT_NAME]; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * @since 4.7.1 |
| 421 | */ |
| 422 | public function get_style_depends(): array |
| 423 | { |
| 424 | return [RegisterWidgetEditorScripts::LEGACY_GIVE_FORM_WIDGET_SCRIPT_NAME]; |
| 425 | } |
| 426 | } |
| 427 |