| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wdk\Extensions; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) exit; // Exit if accessed directly |
| 6 |
|
| 7 |
class WdkAutosuggestionFields |
| 8 |
{ |
| 9 |
/** |
| 10 |
* data array |
| 11 |
* |
| 12 |
* @var array |
| 13 |
*/ |
| 14 |
public $data = array(); |
| 15 |
public $custom_js = ''; |
| 16 |
|
| 17 |
private $is_addon_active = null; |
| 18 |
|
| 19 |
|
| 20 |
public function __construct($data = array(), $args = null) |
| 21 |
{ |
| 22 |
$this->is_addon_active (); |
| 23 |
/* admin actions */ |
| 24 |
add_action( 'wpdirectorykit/admin/listing/edit/after_form', array($this, 'autosuggestion')); |
| 25 |
add_action( 'wdk-membership/view/listing_edit/after_form', array($this, 'autosuggestion')); |
| 26 |
} |
| 27 |
|
| 28 |
private function is_addon_active () { |
| 29 |
if(!is_null($this->is_addon_active)) { |
| 30 |
return $this->is_addon_active; |
| 31 |
} |
| 32 |
|
| 33 |
if ( in_array( 'wdk-autosuggestion/wdk-autosuggestion.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { |
| 34 |
$this->is_addon_active = true; |
| 35 |
} else { |
| 36 |
$this->is_addon_active = false; |
| 37 |
} |
| 38 |
|
| 39 |
return $this->is_addon_active; |
| 40 |
} |
| 41 |
|
| 42 |
public function autosuggestion($data = NULL) |
| 43 |
{ |
| 44 |
wp_register_script( 'wdk-trial-autosuggestion-drop', WPDIRECTORYKIT_URL. 'public/js/wdk-trial-autosuggestion-drop.js', array(), 1.0, 'all' ); |
| 45 |
wp_register_style( 'wdk-trial-autosuggestion-drop', WPDIRECTORYKIT_URL. 'public/css/wdk-trial-autosuggestion-drop.css', array(), 1.0, 'all' ); |
| 46 |
|
| 47 |
if($this->is_addon_active ()) { |
| 48 |
wp_register_script( 'wdk-autosuggestion-drop', WDK_AUTOSUGGESTION_URL. 'public/js/wdk-autosuggestion-drop.js', array(), 1.0, 'all' ); |
| 49 |
wp_register_style( 'wdk-autosuggestion-drop', WDK_AUTOSUGGESTION_URL. 'public/css/wdk-autosuggestion-drop.css', array(), 1.0, 'all' ); |
| 50 |
} |
| 51 |
|
| 52 |
global $Winter_MVC_WDK; |
| 53 |
$Winter_MVC_WDK->model('field_m'); |
| 54 |
$autosuggestion_fields = $Winter_MVC_WDK->field_m->get_by(array('autosuggestion is NOT NULL' => NULL)); |
| 55 |
|
| 56 |
if($autosuggestion_fields) { |
| 57 |
foreach ($autosuggestion_fields as $field) { |
| 58 |
|
| 59 |
switch (wmvc_show_data('autosuggestion', $field)) { |
| 60 |
case 'google_api_cities': |
| 61 |
$this->google_api_cities('#field_'.$field->idfield); |
| 62 |
break; |
| 63 |
case 'google_api_countries': |
| 64 |
$this->google_api_countries('#field_'.$field->idfield); |
| 65 |
break; |
| 66 |
case 'rapid_api_cities': |
| 67 |
$this->rapid_api_cities('#field_'.$field->idfield); |
| 68 |
break; |
| 69 |
case 'rapid_api_countries': |
| 70 |
$this->rapid_api_countries('#field_'.$field->idfield); |
| 71 |
break; |
| 72 |
case 'countries': |
| 73 |
$this->countries('#field_'.$field->idfield); |
| 74 |
break; |
| 75 |
case 'db_selft_field': |
| 76 |
$this->db_selft_field('#field_'.$field->idfield, $field->idfield); |
| 77 |
break; |
| 78 |
case 'db_locations': |
| 79 |
$this->db_locations('#field_'.$field->idfield, $field->idfield); |
| 80 |
break; |
| 81 |
case 'db_categories': |
| 82 |
$this->db_categories('#field_'.$field->idfield, $field->idfield); |
| 83 |
break; |
| 84 |
default: |
| 85 |
# code... |
| 86 |
break; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
if(!empty($this->custom_js)) { |
| 91 |
|
| 92 |
?> |
| 93 |
<script> |
| 94 |
<?php echo wp_kses_post ($this->custom_js);?> |
| 95 |
</script> |
| 96 |
<?php |
| 97 |
|
| 98 |
|
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
private function google_api_cities ($seletor = NULL) { |
| 104 |
if(empty($seletor)) return false; |
| 105 |
if(!get_option('wdk_autosuggestion_google_api_key')) return false; |
| 106 |
if(!$this->is_addon_active ()) return false; |
| 107 |
|
| 108 |
wp_enqueue_script( 'google-map-api', 'https://maps.googleapis.com/maps/api/js?key='.get_option('wdk_autosuggestion_google_api_key').'&libraries=places', array(), 1.0, 'all' ); |
| 109 |
wp_enqueue_script( 'wdk-autosuggestion-drop'); |
| 110 |
wp_enqueue_style( 'wdk-autosuggestion-drop'); |
| 111 |
wp_enqueue_script( 'wdk-trial-autosuggestion-drop'); |
| 112 |
$this->custom_js .= " |
| 113 |
jQuery('".$seletor."').on('input', function(e){ |
| 114 |
wdk_autosuggestion_google_cities(jQuery(this)); |
| 115 |
}) |
| 116 |
"; |
| 117 |
} |
| 118 |
|
| 119 |
private function google_api_countries ($seletor = NULL) { |
| 120 |
if(empty($seletor)) return false; |
| 121 |
if(!get_option('wdk_autosuggestion_google_api_key')) return false; |
| 122 |
if(!$this->is_addon_active ()) return false; |
| 123 |
|
| 124 |
wp_enqueue_script( 'google-map-api', 'https://maps.googleapis.com/maps/api/js?key='.get_option('wdk_autosuggestion_google_api_key').'&libraries=places', array(), 1.0, 'all' ); |
| 125 |
wp_enqueue_script( 'wdk-autosuggestion-drop'); |
| 126 |
wp_enqueue_style( 'wdk-autosuggestion-drop'); |
| 127 |
wp_enqueue_script( 'wdk-trial-autosuggestion-drop'); |
| 128 |
$this->custom_js .= " |
| 129 |
jQuery('".$seletor."').on('input', function(e){ |
| 130 |
wdk_autosuggestion_google_countries(jQuery(this)); |
| 131 |
}) |
| 132 |
"; |
| 133 |
} |
| 134 |
|
| 135 |
private function rapid_api_cities ($seletor = NULL) { |
| 136 |
if(empty($seletor)) return false; |
| 137 |
if(!get_option('wdk_autosuggestion_rapidapi_api_key')) return false; |
| 138 |
if(!$this->is_addon_active ()) return false; |
| 139 |
|
| 140 |
wp_enqueue_script( 'wdk-autosuggestion-drop'); |
| 141 |
wp_enqueue_style( 'wdk-autosuggestion-drop'); |
| 142 |
wp_enqueue_script( 'wdk-trial-autosuggestion-drop'); |
| 143 |
$this->custom_js .= " |
| 144 |
jQuery('".$seletor."').on('input', function(e){ |
| 145 |
wdk_autosuggestion_rapidapi_cities(jQuery(this), '".get_option('wdk_autosuggestion_rapidapi_api_key')."'); |
| 146 |
}) |
| 147 |
"; |
| 148 |
} |
| 149 |
|
| 150 |
private function rapid_api_countries ($seletor = NULL) { |
| 151 |
if(empty($seletor)) return false; |
| 152 |
if(!get_option('wdk_autosuggestion_rapidapi_api_key')) return false; |
| 153 |
if(!$this->is_addon_active ()) return false; |
| 154 |
|
| 155 |
wp_enqueue_script( 'wdk-autosuggestion-drop'); |
| 156 |
wp_enqueue_style( 'wdk-autosuggestion-drop'); |
| 157 |
wp_enqueue_script( 'wdk-trial-autosuggestion-drop'); |
| 158 |
$this->custom_js .= " |
| 159 |
jQuery('".$seletor."').on('input', function(e){ |
| 160 |
wdk_autosuggestion_rapidapi_countries(jQuery(this), '".get_option('wdk_autosuggestion_rapidapi_api_key')."'); |
| 161 |
}) |
| 162 |
"; |
| 163 |
} |
| 164 |
|
| 165 |
private function countries ($seletor = NULL) { |
| 166 |
if(empty($seletor)) return false; |
| 167 |
|
| 168 |
wp_enqueue_style( 'wdk-autosuggestion-drop'); |
| 169 |
wp_enqueue_script( 'wdk-trial-autosuggestion-drop'); |
| 170 |
$this->custom_js .= " |
| 171 |
jQuery('".$seletor."').on('input', function(e){ |
| 172 |
wdk_autosuggestion_rest_countries(jQuery(this)); |
| 173 |
}) |
| 174 |
"; |
| 175 |
} |
| 176 |
|
| 177 |
private function db_selft_field ($seletor = NULL, $field_id = NULL) { |
| 178 |
if(empty($seletor) || empty($field_id)) return false; |
| 179 |
|
| 180 |
wp_enqueue_style( 'wdk-trial-autosuggestion-drop'); |
| 181 |
wp_enqueue_script( 'wdk-trial-autosuggestion-drop'); |
| 182 |
$this->custom_js .= " |
| 183 |
jQuery('".$seletor."').on('input', function(e){ |
| 184 |
wdk_autosuggestion_db_field(jQuery(this), '".esc_js($field_id)."'); |
| 185 |
}) |
| 186 |
"; |
| 187 |
} |
| 188 |
|
| 189 |
private function db_locations ($seletor = NULL, $field_id = NULL) { |
| 190 |
if(empty($seletor) || empty($field_id)) return false; |
| 191 |
|
| 192 |
wp_enqueue_style( 'wdk-trial-autosuggestion-drop'); |
| 193 |
wp_enqueue_script( 'wdk-trial-autosuggestion-drop'); |
| 194 |
$this->custom_js .= " |
| 195 |
jQuery('".$seletor."').on('input', function(e){ |
| 196 |
wdk_autosuggestion_db_locations(jQuery(this)); |
| 197 |
}) |
| 198 |
"; |
| 199 |
} |
| 200 |
|
| 201 |
private function db_categories ($seletor = NULL, $field_id = NULL) { |
| 202 |
if(empty($seletor) || empty($field_id)) return false; |
| 203 |
|
| 204 |
wp_enqueue_style( 'wdk-trial-autosuggestion-drop'); |
| 205 |
wp_enqueue_script( 'wdk-trial-autosuggestion-drop'); |
| 206 |
$this->custom_js .= " |
| 207 |
jQuery('".$seletor."').on('input', function(e){ |
| 208 |
wdk_autosuggestion_db_categories(jQuery(this)); |
| 209 |
}) |
| 210 |
"; |
| 211 |
} |
| 212 |
|
| 213 |
} |
| 214 |
|