-
Notifications
You must be signed in to change notification settings - Fork 1
/
jfa-social-media-post.php
156 lines (147 loc) · 5.61 KB
/
jfa-social-media-post.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* Plugin Name: Jfa Social Media Post
* Plugin URI: https://jordifernandes.com/jfa-social-media-post/
* Description: This WordPress plugin allows you to retrieve a specific Instagram post and consume it via the REST API.
* Version: 1.0.0
* Author: Jordi Fernandes Alves
* Author URI: https://jordifernandes.com/
**/
/**
* Admin menu
*/
add_action('admin_menu', function () {
add_menu_page(
'Jfa Social Media Post',
'Social Post',
'publish_posts', //'manage_options', // https://wordpress.org/support/article/roles-and-capabilities/
'social_media_post',
'admin_social_media_post_page',
'dashicons-instagram'
);
});
/**
* Admin page
*/
function admin_social_media_post_page()
{
$settings = social_media_post_settings_get();
?>
<div class="wrap">
<h1 class="wp-heading-inline">
Jfa Social Media Post
</h1>
<p>This plugin allows you to retrieve a specific Instagram post.</p>
<div style="padding:10px;">
<div style="display:grid;grid-template-columns:repeat(3, 1fr);grid-gap:10px;grid-auto-rows:minmax(100px, auto);">
<div style="grid-column:1/3;grid-row:1;">
<form id="instagram-post-form" method="post" action="/wp-json/api/v2/social_media_post/settings/">
<h2>Config</h2>
<p>
Enter the API URL of the <a href="https://instant-tokens.com" target="_blank">instant-tokens.com</a> instagram account<br>
For example:<br>
<code>https://ig.instant-tokens.com/users/XXXXXX/instagram/XXXXXX/token?userSecret=XXXXX</code>
</p>
<label for="url_token">Instant Token API URL:</label><br>
<input type="url" name="url_token" value="<?php echo esc_attr($settings->url_token) ?>" style="width:418px">
<h2>Post displayed</h2>
<p>
Enter the URL of the Instagram post to be displayed (post must belong to the user's account)<br>
For example:<br>
<code>https://www.instagram.com/p/XXXXXXXXXX/</code>
</p>
<label for="permalink">URL:</label><br>
<input type="url" name="permalink" value="<?php echo esc_attr($settings->permalink) ?>" style="width:300px">
<button type="submit" class="button button-primary">
Save changes
</button>
</form>
<h2>Endpoint</h2>
<p>Access the post's JSON at the following endpoint.</p>
<p><code>/wp-json/api/v2/social_media_post/post/</code> <a href="/wp-json/api/v2/social_media_post/post/" target="_blank">view</a></p>
<p>Return:<br>
<code>{"permalink":"", "caption":"", "media_url":"", "url_token":"", "username":"", "timestamp":""}</code>
<h2>Info</h2>
Homepage: <a href="https://jordifernandes.com/jfa-social-media-post/" target="_blank">https://jordifernandes.com/jfa-social-media-post/</a><br>
Donate: <a href="https://jordifernandes.com/donate/" target="_blank">https://jordifernandes.com/donate/</a><br>
</div>
<div style="grid-column:3;grid-row:1;">
<?php if ($settings && $settings->media_url) { ?>
<h3>Preview: </h3>
<img src="<?php echo esc_url($settings->media_url) ?>" width="200px"><br><br>
<b>Permalink: </b><br>
<a href="<?php echo esc_url($settings->permalink) ?>" target="_blank">
<?php echo esc_html($settings->permalink) ?>
</a><br><br>
<b>Username: </b>
<a href="<?php echo esc_url("https://www.instagram.com/{$settings->username}/") ?>" target="_blank">
@<?php echo esc_html($settings->username) ?>
</a><br><br>
<b>Date: </b>
<?php echo esc_html($settings->timestamp) ?><br><br>
<b>Caption: </b><br>
<?php echo esc_html($settings->caption) ?>
<?php } ?>
</div>
</div>
</div>
<?php
}
/**
* Add endpoint to save config
*/
add_action('rest_api_init', function () {
register_rest_route('api/v2', '/social_media_post/settings/', [
'methods' => 'POST',
'callback' => 'api_social_media_post_settings'
]);
});
function api_social_media_post_settings($data)
{
if ($data['permalink'] && $data['url_token']) {
$access_token = file_get_contents($data['url_token']);
preg_match("/\'(.*)\'/", $access_token, $matches);
$access_token = $matches[1];
$fields = 'id,caption,media_url,permalink,username,timestamp';
$limit = '1000000';
$endpoint = "https://graph.instagram.com/me/media?fields=$fields&access_token=$access_token&limit=$limit";
$res = json_decode(file_get_contents($endpoint));
foreach ($res->data as $post) {
if ($post->permalink == $data['permalink']) {
break;
}
}
$settings = [
'permalink' => $post->permalink,
'caption' => $post->caption,
'media_url' => $post->media_url,
'url_token' => $data['url_token'],
'username' => $post->username,
'timestamp' => $post->timestamp,
];
social_media_post_settings_set($settings);
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
}
}
/**
* Add endpoint to retrieve post json
*/
add_action('rest_api_init', function () {
register_rest_route('api/v2', '/social_media_post/post/', [
'methods' => 'GET',
'callback' => 'api_social_media_post_post'
]);
});
function api_social_media_post_post()
{
return social_media_post_settings_get();
}
function social_media_post_settings_get()
{
return json_decode(file_get_contents(__DIR__ . '/data.json'));
}
function social_media_post_settings_set($obj)
{
file_put_contents(__DIR__ . '/data.json', json_encode($obj));
}