| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || die(); |
| 4 |
|
| 5 |
use Sellkit_Elementor_Optin_Tab_Content as Content_Controls; |
| 6 |
use Sellkit_Elementor_Optin_Tab_Style as Style_Controls; |
| 7 |
use Sellkit_Elementor_Optin_Module as Module; |
| 8 |
use Elementor\Plugin as Elementor; |
| 9 |
|
| 10 |
class Sellkit_Elementor_Optin_Widget extends Sellkit_Elementor_Base_Widget { |
| 11 |
|
| 12 |
public function get_name() { |
| 13 |
return 'sellkit-optin'; |
| 14 |
} |
| 15 |
|
| 16 |
public function get_title() { |
| 17 |
return esc_html__( 'Opt-In', 'sellkit' ); |
| 18 |
} |
| 19 |
|
| 20 |
public function get_icon() { |
| 21 |
return 'sellkit-element-icon sellkit-optin-icon'; |
| 22 |
} |
| 23 |
|
| 24 |
protected function register_controls() { |
| 25 |
new Content_Controls( $this ); |
| 26 |
new Style_Controls( $this ); |
| 27 |
} |
| 28 |
|
| 29 |
protected function render() { |
| 30 |
$settings = $this->get_settings_for_display(); |
| 31 |
$fields = $settings['fields']; |
| 32 |
$has_address = false; |
| 33 |
|
| 34 |
$messages = ''; |
| 35 |
|
| 36 |
if ( 'yes' === $settings['messages_custom'] ) { |
| 37 |
$messages = esc_attr( wp_json_encode( [ |
| 38 |
'error' => esc_attr( $settings['messages_error'] ), |
| 39 |
'required' => esc_attr( $settings['messages_required'] ), |
| 40 |
] ) ); |
| 41 |
} |
| 42 |
|
| 43 |
?> |
| 44 |
<form |
| 45 |
novalidate |
| 46 |
method="POST" |
| 47 |
name="optin-<?php echo esc_attr( $this->get_id() ); ?>" |
| 48 |
onsubmit="(e)=>e.preventDefault()" |
| 49 |
class="sellkit-optin sellkit-flex sellkit-flex-wrap sellkit-flex-bottom" |
| 50 |
data-messages=<?php echo '"' . esc_attr( $messages ) . '"'; ?>> |
| 51 |
<input type="hidden" name="post_id" value="<?php echo esc_attr( self::get_current_post_id() ); ?>" /> |
| 52 |
<input type="hidden" name="form_id" value="<?php echo esc_attr( $this->get_id() ); ?>" /> |
| 53 |
<?php |
| 54 |
|
| 55 |
foreach ( $fields as $field ) { |
| 56 |
if ( 'address' === $field['type'] ) { |
| 57 |
$has_address = true; |
| 58 |
} |
| 59 |
|
| 60 |
Module::render_field( $this, $field ); |
| 61 |
} |
| 62 |
|
| 63 |
?> |
| 64 |
<div <?php echo $this->button_wrapper_attributes( $settings ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor-generated attributes. ?>> |
| 65 |
<button type="submit" class="sellkit-submit-button"> |
| 66 |
<?php Module::render_icon( $settings['submit_button_icon'] ); ?> |
| 67 |
<div class="sellkit-submit-button-texts"> |
| 68 |
<span class="sellkit-submit-button-text"> |
| 69 |
<?php echo esc_html( $settings['submit_button_text'] ); ?> |
| 70 |
</span> |
| 71 |
<span class="sellkit-submit-button-subtext"> |
| 72 |
<?php echo esc_html( $settings['submit_button_subtext'] ); ?> |
| 73 |
</span> |
| 74 |
</div> |
| 75 |
</button> |
| 76 |
</div> |
| 77 |
</form> |
| 78 |
<?php |
| 79 |
|
| 80 |
if ( $has_address ) { |
| 81 |
$this->enqueue_google_places_api(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
private function button_wrapper_attributes( $settings ) { |
| 86 |
$this->add_render_attribute( |
| 87 |
'button-wrapper', |
| 88 |
'class', |
| 89 |
'sellkit-field-group sellkit-field-type-submit-button elementor-column elementor-col-' . $settings['submit_button_width'] |
| 90 |
); |
| 91 |
|
| 92 |
// We add responsive widths as css variables and apply them according to "data-elementor-device-mode" attribute of <body> element. |
| 93 |
foreach ( Module::get_active_breakpoints() as $device => $value ) { |
| 94 |
if ( 'desktop' !== $device && empty( $settings[ "submit_button_width_{$device}" ] ) ) { |
| 95 |
continue; |
| 96 |
} |
| 97 |
|
| 98 |
$setting_key = 'desktop' === $device ? 'submit_button_width' : "submit_button_width_{$device}"; |
| 99 |
|
| 100 |
$this->add_render_attribute( |
| 101 |
'button-wrapper', |
| 102 |
'style', |
| 103 |
"--sellkit-field-width-{$device}:" . $settings[ $setting_key ] . '%;' |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
return $this->get_render_attribute_string( 'button-wrapper' ); |
| 108 |
} |
| 109 |
|
| 110 |
private static function get_current_post_id() { |
| 111 |
if ( isset( Elementor::$instance->documents ) && ! empty( Elementor::$instance->documents->get_current() ) ) { |
| 112 |
return Elementor::$instance->documents->get_current()->get_main_id(); |
| 113 |
} |
| 114 |
|
| 115 |
return get_the_ID(); |
| 116 |
} |
| 117 |
|
| 118 |
private function enqueue_google_places_api() { |
| 119 |
$options = get_option( 'sellkit' ); |
| 120 |
|
| 121 |
/** |
| 122 |
* Let's make a small migration |
| 123 |
* |
| 124 |
* When user updates plugin, once we check if optin is filled, we fill google_api_key field with it. |
| 125 |
* And we set empty value for it, so next time it will be ignored. |
| 126 |
* |
| 127 |
* @todo Should be removed at next version |
| 128 |
* @since 1.5.7 |
| 129 |
*/ |
| 130 |
if ( |
| 131 |
empty( $settings['google_api_key'] ) && |
| 132 |
array_key_exists( 'google_api_key_optin', $options ) && |
| 133 |
! empty( $options['google_api_key_optin'] ) |
| 134 |
) { |
| 135 |
$options['google_api_key'] = $options['google_api_key_optin']; |
| 136 |
$options['google_api_key_optin'] = ''; |
| 137 |
|
| 138 |
update_option( 'sellkit', $options ); |
| 139 |
} |
| 140 |
|
| 141 |
if ( empty( $options ) || empty( $options['google_api_key'] ) ) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
$api_key = $options['google_api_key']; |
| 146 |
|
| 147 |
wp_enqueue_script( |
| 148 |
'sellkit_google_places_api', |
| 149 |
"https://maps.googleapis.com/maps/api/js?key={$api_key}&libraries=places", |
| 150 |
[], |
| 151 |
'1.0.0', |
| 152 |
false |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
} |
| 157 |
|