-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhello.php
87 lines (75 loc) · 2.3 KB
/
hello.php
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/*
* @package Using prefixed Guzzle with PHP-Prefixer
*
* @author PHP-Prefixer <team@php-prefixer.com>
* @copyright Copyright (c)2020-2021 DIV, SL. All rights reserved.
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL
*
* @see https://php-prefixer.com
*/
/*
Plugin Name: Using prefixed Guzzle with PHP-Prefixer
Plugin URI: https://php-prefixer.com/docs/guides/how-to-prefix-wordpress-plugin/
Description: A plugin to integrate Guzzle in a WordPress plug-in with PHP-Prefixer. The plugin shows a number fact using Guzzle from numbersapi.com. Inspired by the Hello Dolly plugin.
Author: PHP-Prefixer
Version: 1.0.0
Author URI: https://php-prefixer.com/
*/
use GuzzleHttp\Client as GuzzleHttpClient;
function hello_prefixed_guzzle_get_number_fact()
{
// Create a client
$client = new GuzzleHttpClient();
$response = $client->get('http://numbersapi.com/'.mt_rand(0, 256));
return wptexturize($response->getBody());
}
// This just echoes the chosen line, we'll position it later.
function hello_prefixed_guzzle()
{
require_once __DIR__.'/vendor/autoload.php';
$randomNumberFact = hello_prefixed_guzzle_get_number_fact();
$lang = '';
if ('en_' !== substr(get_user_locale(), 0, 3)) {
$lang = ' lang="en"';
}
// The modified version of the Hello Dolly plugin shows a number fact using Guzzle to query numbersapi.com.
printf(
'<p id="prefixed_guzzle"><span class="screen-reader-text">%s </span><span dir="ltr"%s>%s</span></p>',
__('A random number fact from numbersapi.com:', 'hello-prefixed_guzzle'),
$lang,
$randomNumberFact
);
}
// Now we set that function up to execute when the admin_notices action is called.
add_action('admin_notices', 'hello_prefixed_guzzle');
// We need some CSS to position the paragraph.
function prefixed_guzzle_css()
{
echo "
<style type='text/css'>
#prefixed_guzzle {
float: right;
padding: 5px 10px;
margin: 0;
font-size: 12px;
line-height: 1.6666;
}
.rtl #prefixed_guzzle {
float: left;
}
.block-editor-page #prefixed_guzzle {
display: none;
}
@media screen and (max-width: 782px) {
#prefixed_guzzle,
.rtl #prefixed_guzzle {
float: none;
padding-left: 0;
padding-right: 0;
}
}
</style>
";
}
add_action('admin_head', 'prefixed_guzzle_css');