-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom-dog.php
More file actions
54 lines (47 loc) · 1.88 KB
/
Copy pathrandom-dog.php
File metadata and controls
54 lines (47 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/*
Plugin Name: Random Dog
Plugin URI: https://github.com/seieric/random-dog
Description: This plugin adds widgets and shortcodes that show dogs randomly. Dogs are fetched from Dogs API(https://dog.ceo/dog-api/).
Version: 1.0
Author: seieric
Author URI: https://about.monolithon.net
License: GPLv2
*/
/* Copyright 2021 seieric (email : opensource@yaseiblog.org)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if( !defined( 'ABSPATH' ) ) exit;
define( 'RANDOM_DOG_EP', 'https://dog.ceo/api/breeds/image/random' );
function random_dog_js_tag() {
$ep = RANDOM_DOG_EP;
$tag = <<< TAG
<script>(function() {
const thisTag = document.currentScript;
fetch('{$ep}').then(r=>r.json()).then(d=>{
const img = document.createElement('img');
img.src = d.message;
img.alt = 'A picture of a dog.';
thisTag.parentNode.insertBefore(img, thisTag.nextSibling);
}).catch(e=>console.log(e))})();</script>
TAG;
return $tag;
}
function random_dog_json_get( string $url ) {
$res = wp_remote_get( $url );
$http_code = wp_remote_retrieve_response_code( $res );
if ($http_code !== 200) return null;
$body = wp_remote_retrieve_body( $res );
return json_decode( $body, true );
}
require_once dirname( __FILE__ ) . '/shortcode.php';
require_once dirname( __FILE__ ) . '/widget.php';