Last active
June 8, 2022 03:31
-
-
Save timsayshey/a04b3d3c1098d11460739162cfc2a5f2 to your computer and use it in GitHub Desktop.
Wordpress Shortcodes in Golang
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
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"regexp" | |
"strconv" | |
"strings" | |
) | |
func main() { | |
output, _ := doShortcode(`tests fasdf [getremote url="http://outerplex.com" limit="3"] a a sdfsadf asf as [getremote url="http://sureplex.com" limit="4"] asd fadsf`) | |
fmt.Println(output) | |
} | |
func doShortcode(input string) (string, error) { | |
type attributes struct { | |
url string | |
limit int | |
} | |
matchShortcodeParametersRegex, _ := regexp.Compile(`([a-zA-Z]+)="([^"]+)+"`) | |
matchShortcodesRegex, _ := regexp.Compile(`\[getremote*.*?\]`) | |
shortcodes := matchShortcodesRegex.FindAllString(input, -1) | |
// input := `tests fasdf %v a a sdfsadf asf as %v asd fadsf` | |
var results []interface{} | |
for _, shortcode := range shortcodes { | |
// shortcode = [getremote url="http://outerplex.com" limit="3"] | |
parseAttrs := matchShortcodeParametersRegex.FindAllString(shortcode, -1) | |
attrs := &attributes{} | |
for _, attr := range parseAttrs { | |
keyValue := strings.Split(attr, "=") | |
key := keyValue[0] | |
value := keyValue[1] | |
value = strings.Replace(value, `"`, ``, -1) | |
if key == "url" { | |
attrs.url = value | |
} | |
if key == "limit" { | |
i, _ := strconv.Atoi(value) | |
attrs.limit = i | |
} | |
} | |
// If this fails, let's cancel the email and try again later | |
// the endpoint is not available and we don't want to send without this content | |
// TODO: Verify response headers to prevent malicious remote file downloads | |
response, err := http.Get(attrs.url) | |
if err != nil { | |
return input, err | |
} | |
defer response.Body.Close() | |
contents, err := ioutil.ReadAll(response.Body) | |
if err != nil { | |
return input, err | |
} | |
results = append(results, string(contents)) | |
} | |
input = matchShortcodesRegex.ReplaceAllString(input, "%v") | |
return fmt.Sprintf(input, results...), nil | |
} | |
/* | |
The MIT License (MIT) | |
Copyright (c) 2018 Tim Badolato and Contributors | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment