Skip to content

Instantly share code, notes, and snippets.

@greg-randall
Created February 10, 2022 16:21
Show Gist options
  • Select an option

  • Save greg-randall/a45134048d2aecec4e0e448356d28d27 to your computer and use it in GitHub Desktop.

Select an option

Save greg-randall/a45134048d2aecec4e0e448356d28d27 to your computer and use it in GitHub Desktop.
Generate json file for the WordPress plugin Redirection.
<pre>
<?php
//built to generate a json file for https://wordpress.org/plugins/redirection/
//might want to run this through a json validator before uploading. there isnt any checking to make sure things worked correctly
//paste two columns from excel here.
//make sure to have trailing slashes
$data = "/thing/asdf/ /other-thing/qwerty/
/ideas/ /better-ideas/";
//echo the first bit of the json that stays the same
$output = '{ "plugin": { "version": "5.2.3", "date": "Thu, 10 Feb 2022 15:21:25 +0000" }, "groups": [ { "id": 1, "name": "Redirections", "redirects": 1, "module_id": 1, "moduleName": "WordPress", "enabled": true }, { "id": 2, "name": "Modified Posts", "redirects": 0, "module_id": 1, "moduleName": "WordPress", "enabled": true } ], "redirects": [';
//split the pasted data in from excel
$data = explode( "\n", $data );
//counter for adding in id value & making sure that we add the right number of commas
$i = 1;
foreach ( $data as $items ) {
//get the two paths/urls for redirection, and trim off whitespace
$item = explode( "\t", trim($items) );
$item[0]=trim($item[0]);
$item[1]=trim($item[1]);
//print out the bit of json containing the redirect and some options set
$output .= ' { "id": ' . $i . ', "url": "' . $item[ 0 ] . '", "match_url": "' . substr( $item[ 0 ], 0, -1 ) . '", "match_data": { "source": { "flag_query": "ignore", "flag_case": false, "flag_trailing": true, "flag_regex": false }, "options": { "log_exclude": true } }, "action_code": 301, "action_type": "url", "action_data": { "url": "' . $item[ 1 ] . '" }, "match_type": "url", "title": "", "hits": 0, "regex": false, "group_id": 1, "position": 0, "last_access": "February 10, 2022", "enabled": true }';
//as long as we're not at the last item output a comma
if ( count( $data ) - $i > 0 ) {
$output .= ',';
}
$i++;
}
//close off the json
$output .= ']}';
echo prettyPrint($output);
//https://stackoverflow.com/a/9776726
function prettyPrint( $json ) {
$result = '';
$level = 0;
$in_quotes = false;
$in_escape = false;
$ends_line_level = NULL;
$json_length = strlen( $json );
for ( $i = 0; $i < $json_length; $i++ ) {
$char = $json[ $i ];
$new_line_level = NULL;
$post = "";
if ( $ends_line_level !== NULL ) {
$new_line_level = $ends_line_level;
$ends_line_level = NULL;
}
if ( $in_escape ) {
$in_escape = false;
} else if ( $char === '"' ) {
$in_quotes = !$in_quotes;
} else if ( !$in_quotes ) {
switch ( $char ) {
case '}':
case ']':
$level--;
$ends_line_level = NULL;
$new_line_level = $level;
break;
case '{':
case '[':
$level++;
case ',':
$ends_line_level = $level;
break;
case ':':
$post = " ";
break;
case " ":
case "\t":
case "\n":
case "\r":
$char = "";
$ends_line_level = $new_line_level;
$new_line_level = NULL;
break;
}
} else if ( $char === '\\' ) {
$in_escape = true;
}
if ( $new_line_level !== NULL ) {
$result .= "\n" . str_repeat( "\t", $new_line_level );
}
$result .= $char . $post;
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment