Last active
August 31, 2018 19:49
-
-
Save panoslyrakis/149642138c9680d6eda9d80da2415db4 to your computer and use it in GitHub Desktop.
Convert an array to a query string
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 | |
/** | |
* Plugin Name: Convert array to query string | |
* Plugin URI: https://gist.github.com/panoslyrakis | |
* Description: Convert an array to a query string. Usefull when you have a huge array with several keys and need it be converted into query string. | |
* Author: Panos Lyrakis | |
* Author URI: https://gist.github.com/panoslyrakis | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_Array_To_QueryString' ) ) { | |
class WPMUDEV_Array_To_QueryString { | |
private static $_instance = null; | |
private static $shortcode = null; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_Array_To_QueryString(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
self::$shortcode = 'wpmudev_array_to_query'; | |
add_shortcode( 'wpmudev_array_to_query', array( $this, 'shortcode' ) ); | |
add_action( 'wp_head', array( $this, 'css' ) ); | |
add_action( 'wp_footer', array( $this, 'js' ) ); | |
add_action( 'wp_ajax_wpmudev_convert_array_to_query_string', array( $this, 'convert_ajax' ), 10 ); | |
add_action( 'wp_ajax_nopriv_wpmudev_convert_array_to_query_string', array( $this, 'convert_ajax' ), 10 ); | |
} | |
public static function convert( $input ) { | |
$output = array(); | |
$_input = explode( PHP_EOL, trim( $input, '()[]{}' ) ); | |
foreach ( $_input as $line ) { | |
$_line = explode( '=>', $line ); | |
$key = self::clear_string( $_line[0] ); | |
$value = self::clear_string( $_line[1] ); | |
$output[] = "{$key}={$value}"; | |
} | |
return implode( '&', $output ); | |
} | |
public static function clear_string( $str ){ | |
$str = trim( $str ); | |
$str = trim( $str, '{}[]' ); | |
$str = trim( $str, '"' ); | |
$str = trim( $str, "'" ); | |
return $str; | |
} | |
public function convert_ajax() { | |
check_ajax_referer( 'wpmudev_convert_array_to_query_string', 'security' ); | |
$input = filter_input( INPUT_POST, 'input', FILTER_DEFAULT ); | |
$return = array( | |
'success' => true, | |
'content' => self::convert( $input ) | |
); | |
wp_send_json($return); | |
} | |
public function js(){ | |
global $post; | |
if ( ! $post instanceof WP_Post || ! has_shortcode( $post->post_content, self::$shortcode ) ) { | |
return; | |
} | |
?> | |
<script type="text/javascript"> | |
(function($){ | |
$(document).ready(function(){ | |
var converter_btn = $('#converted-btn'), | |
input = $('#array-input'), | |
output = $('#query-output'), | |
ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); ?>'; | |
converter_btn.on('click',function(){ | |
var data = { | |
action: 'wpmudev_convert_array_to_query_string', | |
security: '<?php echo wp_create_nonce( "wpmudev_convert_array_to_query_string" ); ?>', | |
input: input.val() | |
}; | |
$.post(ajaxurl, data, function(response) { | |
if( response.success ){ | |
output.html( response.content ); | |
} | |
else{ | |
output.html( response.content ); | |
} | |
}); | |
}); | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
} | |
public function shortcode( $atts, $content ) { | |
ob_start(); | |
?> | |
<table style="width:100%;" class="container-table"> | |
<tr> | |
<td> | |
<textarea id="array-input"></textarea> | |
</td> | |
<td> | |
<div id="query-output" class="text-pane"></div> | |
</td> | |
</tr> | |
</table> | |
<div> | |
<a class="button-primary" id="converted-btn">Convert</a> | |
</div> | |
<?php | |
return ob_get_clean(); | |
} | |
public function css() { | |
global $post; | |
if ( ! $post instanceof WP_Post || ! has_shortcode( $post->post_content, self::$shortcode ) ) { | |
return; | |
} | |
?> | |
<style type="text/css"> | |
.text-pane { | |
display: block; | |
border: 1px solid #dfdfdf; | |
color: #555; | |
overflow-wrap: break-word; | |
word-wrap: break-word; | |
width: 100%; | |
max-width: 100%; | |
} | |
.container-table { | |
width: 100%; | |
} | |
.container-table td { | |
width: 50%; | |
max-width: 400px; | |
} | |
#array-input, | |
#query-output { | |
min-width: 100%; | |
min-height: 300px; | |
} | |
/* | |
Max width before this PARTICULAR table gets nasty | |
This query will take effect for any screen smaller than 760px | |
and also iPads specifically. | |
*/ | |
@media | |
only screen and (max-width: 760px), | |
(min-device-width: 768px) and (max-device-width: 1024px) { | |
/* Force table to not be like tables anymore */ | |
table, thead, tbody, th, td, tr { | |
display: block; | |
} | |
/* Hide table headers (but not display: none;, for accessibility) */ | |
thead tr { | |
position: absolute; | |
top: -9999px; | |
left: -9999px; | |
} | |
tr { border: 1px solid #ccc; } | |
td { | |
/* Behave like a "row" */ | |
border: none; | |
border-bottom: 1px solid #eee; | |
position: relative; | |
padding-left: 50%; | |
} | |
td:before { | |
/* Now like a table header */ | |
position: absolute; | |
/* Top/left values mimic padding */ | |
top: 6px; | |
left: 6px; | |
width: 45%; | |
padding-right: 10px; | |
white-space: nowrap; | |
} | |
/* | |
Label the data | |
*/ | |
td:nth-of-type(1):before { content: "Input"; } | |
td:nth-of-type(2):before { content: "Output"; } | |
} | |
</style> | |
<?php | |
} | |
} | |
if ( ! function_exists( 'wpmudev_array_to_querystring' ) ) { | |
function wpmudev_array_to_querystring(){ | |
return WPMUDEV_Array_To_QueryString::get_instance(); | |
}; | |
add_action( 'plugins_loaded', 'wpmudev_array_to_querystring', 10 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment