Last active
May 31, 2019 23:35
-
-
Save gdarko/49e952d17d3f7767fc4df638c4a94261 to your computer and use it in GitHub Desktop.
Useful Vanilla PHP and WordPress helper functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This file contains general useful PHP functions that can be included everywhere. | |
/** | |
* Wrapper for writing the interactions to /wp-content/uploads/ file | |
* | |
* @param $message | |
* @param string $filename | |
*/ | |
function dg_log_write( $message, $filename = "log.txt" ) { | |
$log_file_path = dirname( __FILE__ ) . '/' . $filename; // Change path if needed | |
if ( is_object( $message ) || is_array( $message ) ) { | |
ob_start(); | |
var_dump( $message ); | |
$message = ob_get_clean(); | |
} | |
$fp = fopen( $log_file_path, 'a+' ); | |
fwrite( $fp, $message . "\n" ); | |
fclose( $fp ); | |
} | |
/** | |
* Replaces german umlauts with normal characters in a given string. | |
* | |
* @param $str | |
* @return string | |
*/ | |
function dg_umlauts_fix( $str ) { | |
$umlauts = array( | |
'Ä' => 'Ae', | |
'Ö' => 'Oe', | |
'Ü' => 'Ue', | |
'ü' => 'ue', | |
'ö' => 'oe', | |
'ä' => 'ae', | |
'ß' => 'ss' | |
); | |
$str = strtr( $str, $umlauts ); | |
return $str; | |
} | |
/** | |
* Returns the client ip address | |
* @return mixed | |
*/ | |
function dg_get_client_ip() { | |
if ( getenv( 'HTTP_CLIENT_IP' ) ) { | |
$ip_address = getenv( 'HTTP_CLIENT_IP' ); | |
} else if ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) { | |
$ip_address = getenv( 'HTTP_X_FORWARDED_FOR' ); | |
} else if ( getenv( 'HTTP_X_FORWARDED' ) ) { | |
$ip_address = getenv( 'HTTP_X_FORWARDED' ); | |
} else if ( getenv( 'HTTP_FORWARDED_FOR' ) ) { | |
$ip_address = getenv( 'HTTP_FORWARDED_FOR' ); | |
} else if ( getenv( 'HTTP_FORWARDED' ) ) { | |
$ip_address = getenv( 'HTTP_FORWARDED' ); | |
} else if ( getenv( 'REMOTE_ADDR' ) ) { | |
$ip_address = getenv( 'REMOTE_ADDR' ); | |
} else { | |
$ip_address = false; | |
} | |
return $ip_address; | |
} | |
/** | |
* Returns true if string contains another string | |
* | |
* @param $text | |
* @param $string | |
* | |
* @return bool | |
*/ | |
function dg_str_contains( $text, $string ) { | |
return ( strpos( $text, $string ) !== false ); | |
} | |
/** | |
* Pluck an array of values from an array. (Only for PHP 5.3+) | |
* | |
* @param $array - data | |
* @param $key - value you want to pluck from array | |
* | |
* @return array plucked array only with key data | |
*/ | |
function dg_array_pluck( $array, $key ) { | |
return array_map( function ( $v ) use ( $key ) { | |
return is_object( $v ) ? $v->$key : $v[ $key ]; | |
}, $array ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Returns the value by Input name (value in Allow this field to be populated dynamically) | |
* @param $fields | |
* @param $input_name | |
* @param $choice_id | |
* | |
* @return string | |
*/ | |
function dg_gf_get_value_by_input_name($fields, $input_name, $choice_id = 0) { | |
$value = NULL; | |
//error_log(print_r($_POST, true)); | |
foreach($fields as $field) { | |
if($field->inputName == $input_name) { | |
if($choice_id > 0) { | |
$handle = 'input_' . $field->id . '_' . $choice_id; | |
} else { | |
$handle = 'input_' . $field->id; | |
} | |
$value = isset($_POST[$handle]) ? $_POST[$handle] : NULL; | |
} | |
} | |
return $value; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This file contains useful WordPress functions that can be included in any WP theme or plugin and used. | |
/** | |
* Used to upload single image into WP, returns false or the attachment_id that is created in the db | |
* Example input element: <input type=file name=file_handler/> | |
* @param $file_handler | |
* @param $post_id | |
* | |
* @return bool|int|WP_Error | |
*/ | |
function dg_handle_media_upload( $file_handler, $post_id ) { | |
if ( ! file_exists( $_FILES[ $file_handler ]['tmp_name'] ) || ! is_uploaded_file( $_FILES[ $file_handler ]['tmp_name'] ) ) { | |
return false; | |
} | |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); | |
require_once( ABSPATH . 'wp-admin/includes/media.php' ); | |
$attachment_id = media_handle_upload( $file_handler, $post_id ); | |
if ( ! is_wp_error( $attachment_id ) ) { | |
return $attachment_id; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* Handles multiple files and stores into WP based on one input and returns the stored attachment ids. | |
* Example input element: <input type=file name=file_handler[]/> | |
* @param $file_handler | |
* @param $post_id | |
* | |
* @return array | |
*/ | |
function dg_handle_multiple_media_uploads( $file_handler, $post_id ) { | |
$attachment_ids = array(); | |
if ( $_FILES ) { | |
$files = isset( $_FILES[ $file_handler ] ) ? $_FILES[ $file_handler ] : array(); | |
if ( ! empty( $files ) ) { | |
foreach ( $files['name'] as $key => $value ) { | |
if ( $files['name'][ $key ] ) { | |
$file = array( | |
'name' => $files['name'][ $key ], | |
'type' => $files['type'][ $key ], | |
'tmp_name' => $files['tmp_name'][ $key ], | |
'error' => $files['error'][ $key ], | |
'size' => $files['size'][ $key ] | |
); | |
$attachment_ids[] = dg_handle_media_upload( $file_handler, $post_id); | |
} | |
} | |
} | |
} | |
return $attachment_ids; | |
} | |
/** | |
* Returns latitude and longitude from Google's geocoder with transient cache | |
* for WordPress | |
* | |
* Note: Set constant ___GOOGLE__YOUR__API__KEY___ with your google api key. | |
* | |
* @param string $address | |
* @param bool $force_new_data | |
* | |
* @return array | |
*/ | |
function dg_get_lat_long( $address, $force_new_data = false ) { | |
if ( $force_new_data ) { | |
delete_transient( $address ); | |
} | |
$transient_key = sanitize_key($address); | |
// Cache address to save Google geocoding calls. | |
if ( false === ( $coordinates = get_transient( $transient_key ) ) ) { | |
$address = str_replace( " ", "+", $address ); | |
$json = file_get_contents( "https://maps.googleapis.com/maps/api/geocode/json?address=" . $address . "&key=" . ___YOUR__API__KEY___ ); | |
$json = json_decode( $json ); | |
$coordinates['lat'] = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'}; | |
$coordinates['lng'] = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'}; | |
if( $coordinates['lat'] !== NULL && $coordinates['lng'] !== NULL ) { | |
set_transient( $transient_key, $coordinates, DAY_IN_SECONDS * 15 ); | |
} | |
} | |
return $coordinates; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment