'Afghanistan', 'AX' => 'Aland Islands', 'AL' => 'Albania', 'DZ' => 'Algeria', 'AS' => 'American Samoa', 'AD' => 'Andorra', 'AO' => 'Angola', 'AI' => 'Anguilla', 'AQ' => 'Antarctica', 'AG' => 'Antigua And Barbuda', 'AR' => 'Argentina', 'AM' => 'Armenia', 'AW' => 'Aruba', 'AU' => 'Australia', 'AT' => 'Austria', 'AZ' => 'Azerbaijan', 'BS' => 'Bahamas', 'BH' => 'Bahrain', 'BD' => 'Bangladesh', 'BB' => 'Barbados', 'BY' => 'Belarus', 'BE' => 'Belgium', 'BZ' => 'Belize', 'BJ' => 'Benin', 'BM' => 'Bermuda', 'BT' => 'Bhutan', 'BO' => 'Bolivia', 'BA' => 'Bosnia And Herzegovina', 'BW' => 'Botswana', 'BV' => 'Bouvet Island', 'BR' => 'Brazil', 'IO' => 'British Indian Ocean Territory', 'BN' => 'Brunei Darussalam', 'BG' => 'Bulgaria', 'BF' => 'Burkina Faso', 'BI' => 'Burundi', 'KH' => 'Cambodia', 'CM' => 'Cameroon', 'CA' => 'Canada', 'CV' => 'Cape Verde', 'KY' => 'Cayman Islands', 'CF' => 'Central African Republic', 'TD' => 'Chad', 'CL' => 'Chile', 'CN' => 'China', 'CX' => 'Christmas Island', 'CC' => 'Cocos (Keeling) Islands', 'CO' => 'Colombia', 'KM' => 'Comoros', 'CG' => 'Congo', 'CD' => 'Congo, Democratic Republic', 'CK' => 'Cook Islands', 'CR' => 'Costa Rica', 'CI' => 'Cote D\'Ivoire', 'HR' => 'Croatia', 'CU' => 'Cuba', 'CY' => 'Cyprus', 'CZ' => 'Czech Republic', 'DK' => 'Denmark', 'DJ' => 'Djibouti', 'DM' => 'Dominica', 'DO' => 'Dominican Republic', 'EC' => 'Ecuador', 'EG' => 'Egypt', 'SV' => 'El Salvador', 'GQ' => 'Equatorial Guinea', 'ER' => 'Eritrea', 'EE' => 'Estonia', 'ET' => 'Ethiopia', 'FK' => 'Falkland Islands (Malvinas)', 'FO' => 'Faroe Islands', 'FJ' => 'Fiji', 'FI' => 'Finland', 'FR' => 'France', 'GF' => 'French Guiana', 'PF' => 'French Polynesia', 'TF' => 'French Southern Territories', 'GA' => 'Gabon', 'GM' => 'Gambia', 'GE' => 'Georgia', 'DE' => 'Germany', 'GH' => 'Ghana', 'GI' => 'Gibraltar', 'GR' => 'Greece', 'GL' => 'Greenland', 'GD' => 'Grenada', 'GP' => 'Guadeloupe', 'GU' => 'Guam', 'GT' => 'Guatemala', 'GG' => 'Guernsey', 'GN' => 'Guinea', 'GW' => 'Guinea-Bissau', 'GY' => 'Guyana', 'HT' => 'Haiti', 'HM' => 'Heard Island & Mcdonald Islands', 'VA' => 'Holy See (Vatican City State)', 'HN' => 'Honduras', 'HK' => 'Hong Kong', 'HU' => 'Hungary', 'IS' => 'Iceland', 'IN' => 'India', 'ID' => 'Indonesia', 'IR' => 'Iran, Islamic Republic Of', 'IQ' => 'Iraq', 'IE' => 'Ireland', 'IM' => 'Isle Of Man', 'IL' => 'Israel', 'IT' => 'Italy', 'JM' => 'Jamaica', 'JP' => 'Japan', 'JE' => 'Jersey', 'JO' => 'Jordan', 'KZ' => 'Kazakhstan', 'KE' => 'Kenya', 'KI' => 'Kiribati', 'KR' => 'Korea', 'KW' => 'Kuwait', 'KG' => 'Kyrgyzstan', 'LA' => 'Lao People\'s Democratic Republic', 'LV' => 'Latvia', 'LB' => 'Lebanon', 'LS' => 'Lesotho', 'LR' => 'Liberia', 'LY' => 'Libyan Arab Jamahiriya', 'LI' => 'Liechtenstein', 'LT' => 'Lithuania', 'LU' => 'Luxembourg', 'MO' => 'Macao', 'MK' => 'Macedonia', 'MG' => 'Madagascar', 'MW' => 'Malawi', 'MY' => 'Malaysia', 'MV' => 'Maldives', 'ML' => 'Mali', 'MT' => 'Malta', 'MH' => 'Marshall Islands', 'MQ' => 'Martinique', 'MR' => 'Mauritania', 'MU' => 'Mauritius', 'YT' => 'Mayotte', 'MX' => 'Mexico', 'FM' => 'Micronesia, Federated States Of', 'MD' => 'Moldova', 'MC' => 'Monaco', 'MN' => 'Mongolia', 'ME' => 'Montenegro', 'MS' => 'Montserrat', 'MA' => 'Morocco', 'MZ' => 'Mozambique', 'MM' => 'Myanmar', 'NA' => 'Namibia', 'NR' => 'Nauru', 'NP' => 'Nepal', 'NL' => 'Netherlands', 'AN' => 'Netherlands Antilles', 'NC' => 'New Caledonia', 'NZ' => 'New Zealand', 'NI' => 'Nicaragua', 'NE' => 'Niger', 'NG' => 'Nigeria', 'NU' => 'Niue', 'NF' => 'Norfolk Island', 'MP' => 'Northern Mariana Islands', 'NO' => 'Norway', 'OM' => 'Oman', 'PK' => 'Pakistan', 'PW' => 'Palau', 'PS' => 'Palestinian Territory, Occupied', 'PA' => 'Panama', 'PG' => 'Papua New Guinea', 'PY' => 'Paraguay', 'PE' => 'Peru', 'PH' => 'Philippines', 'PN' => 'Pitcairn', 'PL' => 'Poland', 'PT' => 'Portugal', 'PR' => 'Puerto Rico', 'QA' => 'Qatar', 'RE' => 'Reunion', 'RO' => 'Romania', 'RU' => 'Russian Federation', 'RW' => 'Rwanda', 'BL' => 'Saint Barthelemy', 'SH' => 'Saint Helena', 'KN' => 'Saint Kitts And Nevis', 'LC' => 'Saint Lucia', 'MF' => 'Saint Martin', 'PM' => 'Saint Pierre And Miquelon', 'VC' => 'Saint Vincent And Grenadines', 'WS' => 'Samoa', 'SM' => 'San Marino', 'ST' => 'Sao Tome And Principe', 'SA' => 'Saudi Arabia', 'SN' => 'Senegal', 'RS' => 'Serbia', 'SC' => 'Seychelles', 'SL' => 'Sierra Leone', 'SG' => 'Singapore', 'SK' => 'Slovakia', 'SI' => 'Slovenia', 'SB' => 'Solomon Islands', 'SO' => 'Somalia', 'ZA' => 'South Africa', 'GS' => 'South Georgia And Sandwich Isl.', 'ES' => 'Spain', 'LK' => 'Sri Lanka', 'SD' => 'Sudan', 'SR' => 'Suriname', 'SJ' => 'Svalbard And Jan Mayen', 'SZ' => 'Swaziland', 'SE' => 'Sweden', 'CH' => 'Switzerland', 'SY' => 'Syrian Arab Republic', 'TW' => 'Taiwan', 'TJ' => 'Tajikistan', 'TZ' => 'Tanzania', 'TH' => 'Thailand', 'TL' => 'Timor-Leste', 'TG' => 'Togo', 'TK' => 'Tokelau', 'TO' => 'Tonga', 'TT' => 'Trinidad And Tobago', 'TN' => 'Tunisia', 'TR' => 'Turkey', 'TM' => 'Turkmenistan', 'TC' => 'Turks And Caicos Islands', 'TV' => 'Tuvalu', 'UG' => 'Uganda', 'UA' => 'Ukraine', 'AE' => 'United Arab Emirates', 'GB' => 'United Kingdom', 'US' => 'United States', 'UM' => 'United States Outlying Islands', 'UY' => 'Uruguay', 'UZ' => 'Uzbekistan', 'VU' => 'Vanuatu', 'VE' => 'Venezuela', 'VN' => 'Viet Nam', 'VG' => 'Virgin Islands, British', 'VI' => 'Virgin Islands, U.S.', 'WF' => 'Wallis And Futuna', 'EH' => 'Western Sahara', 'YE' => 'Yemen', 'ZM' => 'Zambia', 'ZW' => 'Zimbabwe', ); function __construct() { $this->init(); } function init() { add_shortcode('getinvolved', array('GetInvolved', 'shortCode')); add_action('wp_ajax_nopriv_get_involved', array('GetInvolved', 'submit')); add_action('wp_ajax_get_involved', array('GetInvolved', 'submit')); } function submit() { // var_dump($_POST); /* 'action' => string 'get_involved' (length=12) 'contact_type' => string '' (length=0) 'form' => string '1' (length=1) 'coutrylist' => string 'CY' (length=2) 'name' => string '' (length=0) 'email' => string '' (length=0)*/ $res = static::validate(); if ($res->success) { $g = new GetInvolvedEntry(); $g->email = $res->email['value']; $g->name = $res->name['value']; $g->country = $res->country['value']; $g->industry = $res->contactType['value']; $g->save(); } echo json_encode($res); wp_die(); } function validate() { $out = new \stdClass(); $out->success = true; $p = $_POST; if ($p['contact_type'] == '') { $out->contactType = [ 'value' => 'unspecified', 'error' => '' ]; } else { $out->contactType = [ 'value' => $p['contact_type'], 'error' => false ]; } $out->country = [ 'value' => $p['country'], 'error' => false ]; $out->email = [ 'value' => $p['email'], 'error' => false ]; $out->name = [ 'value' => $p['name'], 'error' => false ]; if (!static::isValidEmail($p['email'])) { $out->email['error'] = 'Please enter a valid email address.'; $out->success = false; } if ($out->success) { $msg = "'Get Involved' Contact" . PHP_EOL; $msg .= "Name: " . $p['name'] . PHP_EOL; $msg .= "Email: " . $p['email'] . PHP_EOL; $msg .= "Country: " . $p['country'] . PHP_EOL; $msg .= "Industry: " . $p['contact_type'] . PHP_EOL; mail('alexandros@energylab.ac.cy','Enerfund: Get Involved',$msg); mail('ktoridesk@gmail.com', 'Enerfund: Get Involved', $msg); } return $out; } static function shortCode($a, $title = null) { $countryCode = static::getCountryCode(); $contactType = ''; if (isset($_GET['gi'])) { if (in_array($_GET['gi'], ['bank', 'municipality', 'retrofitter'])) { $contactType = $_GET['gi']; } else { $contactType = 'unspecified'; } } include __DIR__ . '/templates/form.php'; } static function getCountryCode() { try { $reader = new Reader(__DIR__ . '/GeoLite2-Country.mmdb'); $ip = $_SERVER['REMOTE_ADDR']; $ip = '91.184.212.44'; $record = $reader->country($ip); return $record->country->isoCode; } catch (\Exception $e) { return ''; } } static function isValidEmail($email) { $isValid = true; $atIndex = strrpos($email, "@"); if (is_bool($atIndex) && !$atIndex) { $isValid = false; } else { $domain = substr($email, $atIndex + 1); $local = substr($email, 0, $atIndex); $localLen = strlen($local); $domainLen = strlen($domain); if ($localLen < 1 || $localLen > 64) { // local part length exceeded $isValid = false; } else if ($domainLen < 1 || $domainLen > 255) { // domain part length exceeded $isValid = false; } else if ($local [0] == '.' || $local [$localLen - 1] == '.') { // local part starts or ends with '.' $isValid = false; } else if (preg_match('/\\.\\./', $local)) { // local part has two consecutive dots $isValid = false; } else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) { // character not valid in domain part $isValid = false; } else if (preg_match('/\\.\\./', $domain)) { // domain part has two consecutive dots $isValid = false; } else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\", "", $local))) { // character not valid in local part unless // local part is quoted if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\", "", $local))) { $isValid = false; } } if ($isValid && !(checkdnsrr($domain, "MX") || checkdnsrr($domain, "A"))) { // domain not found in DNS $isValid = false; } } return $isValid; } } $hm = new GetInvolved(); init(); } function init() { add_shortcode('ournetwork', array('OurNetwork', 'shortCode')); add_action('init', ['OurNetwork', 'create_post_type']); add_action('wp_ajax_enerfundGetCountry', ['OurNetwork', 'getCountryAJAX']); add_action('wp_ajax_nopriv_enerfundGetCountry', ['OurNetwork', 'getCountryAJAX']); add_action('wp_enqueue_scripts', ['OurNetwork', 'enqueue']); add_action('admin_enqueue_scripts', ['OurNetwork', 'admin_enqueue']); } static function getCountryAJAX() { $code = $_POST['country']; if (isset(static::$countries[$code])) { $args = array( 'post_type' => 'enerfund_contact', 'meta_query' => array( array( 'key' => 'country', 'value' => $code, 'compare' => 'LIKE' ) )); $q = new WP_Query($args); $contacts = $q->get_posts(); static::prepareContacts($contacts); $contacts = static::sortContacts($contacts); $baseURL = plugins_url('ournetwork') . '/'; $countryName = static::$countries[$code]; ob_start(); if (count($contacts) == 0) { include __DIR__ . '/templates/empty_country.php'; $header = '

 

'; } else { include __DIR__ . '/templates/country_list.php'; $header = '

Members in ' . $countryName . '

'; } $list = ob_get_clean(); $out = new stdClass(); $out->list = $list; $out->header = $header; $out->found = true; echo json_encode($out); } else { echo json_encode((object)['found' => false]); } wp_die(); } static function sortContacts($contacts) { usort($contacts, function ($a, $b) { if (($a->partner) && ($b->partner)) { return 0; } else { if ($a->partner) { return -1; } else { return 1; } } }); return $contacts; } static function prepareContacts(&$contacts) { foreach ($contacts as &$contact) { $contact->hasURL = false; $cvs = get_post_custom_values('url', $contact->ID); if (($cvs !== null) && (is_array($cvs))) { if (count($cvs) > 0) { $contact->hasURL = true; $contact->url = $cvs[0]; } } $cvs = get_post_custom_values('partner', $contact->ID); if (($cvs !== null) && (is_array($cvs))) { if (count($cvs) > 0) { $contact->partner = ($cvs[0] == 1); } else { $contact->partner = false; } } else { $contact->partner = false; } $summary = strip_tags($contact->post_content); $length = mb_strlen($summary); if ($length > 100) { $contact->hasMore = true; $contact->summary = substr($summary, 0, 100) . '…'; } else { $contact->hasMore = true; } } } static function shortCode($a, $title = null) { $baseURL = plugins_url('ournetwork') . '/'; global $wpdb; $activeCountries = $wpdb->get_results('SELECT meta_value as code,COUNT(meta_value) as c FROM wp_postmeta WHERE meta_key = \'country\' GROUP BY meta_value;'); include __DIR__ . '/templates/layout.php'; } static function admin_enqueue() { wp_register_style('on_admin', plugins_url('ournetwork') . '/css/admin-style.css', false, '1.0.0'); wp_enqueue_style('on_admin'); } static function enqueue() { wp_enqueue_script('on-script', plugins_url('ournetwork') . '/js/ournetwork.js', array('jquery')); wp_localize_script('on-script', 'onajax', array('ajaxurl' => admin_url('admin-ajax.php'))); } static function create_post_type() { register_post_type('enerfund_contact', array( 'labels' => array( 'name' => __('Contacts'), 'singular_name' => __('Contact'), 'add_new_item' => 'Add a New Contact', 'edit_item' => 'Edit Contact', 'new_item' => 'New Contact', 'view_item' => 'View Contact', 'featured_image' => 'Logo', 'set_featured_image' => 'Set Logo', 'use_featured_image' => 'Use Logo', 'remove_featured_image' => 'Remove Logo', ), 'public' => true, 'has_archive' => true, 'supports' => array('title', 'editor', 'thumbnail', 'custom-fields') ) ); } static $countries = [ 'be' => 'Belgium', 'bg' => 'Bulgaria', 'cz' => 'Czech Republic', 'dk' => 'Denmark', 'de' => 'Germany', 'ee' => 'Estonia', 'ie' => 'Ireland', 'el' => 'Greece', 'es' => 'Spain', 'fr' => 'France', 'hr' => 'Croatia', 'it' => 'Italy', 'cy' => 'Cyprus', 'lv' => 'Latvia', 'lt' => 'Lithuania', 'lu' => 'Luxembourg', 'hu' => 'Hungary', 'mt' => 'Malta', 'nl' => 'Netherlands', 'at' => 'Austria', 'pl' => 'Poland', 'pt' => 'Portugal', 'ro' => 'Romania', 'si' => 'Slovenia', 'sk' => 'Slovakia', 'fi' => 'Finland', 'se' => 'Sweden', 'gb' => 'United Kingdom', 'ad' => 'Andorra', 'al' => 'Albania', 'by' => 'Belarus', 'ba' => 'Bosnia and Herzegovina', 'is' => 'Iceland', 'li' => 'Lichtenstein', 'md' => 'Moldova', 'me' => 'Montenegro', 'no' => 'Norway', 'rs' => 'Serbia', 'ch' => 'Switzerland', 'mk' => 'FYR Macedonia', 'tr' => 'Turkey' ]; } $hm = new OurNetwork();