-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInstagramCommentReplyAbility.php
More file actions
152 lines (126 loc) · 4.5 KB
/
InstagramCommentReplyAbility.php
File metadata and controls
152 lines (126 loc) · 4.5 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
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
<?php
/**
* Instagram Comment Reply Ability
*
* Abilities API primitive for replying to Instagram comments.
*
* @package DataMachineSocials
* @subpackage Abilities\Instagram
* @since 0.3.0
*/
namespace DataMachineSocials\Abilities\Instagram;
use DataMachine\Abilities\PermissionHelper;
use DataMachineSocials\Handlers\Instagram\InstagramAuth;
use DataMachineSocials\Abilities\Traits\HasCheckPermission;
use DataMachineSocials\Abilities\Instagram\InstagramDeleteAbility;
defined( 'ABSPATH' ) || exit;
class InstagramCommentReplyAbility {
use HasCheckPermission;
private static bool $registered = false;
const GRAPH_API_URL = 'https://graph.facebook.com/v23.0';
const MAX_REPLY_LENGTH = 1000;
public function __construct() {
if ( ! class_exists( 'WP_Ability' ) ) {
return;
}
if ( self::$registered ) {
return;
}
$this->registerAbilities();
self::$registered = true;
}
private function registerAbilities(): void {
$register_callback = function () {
wp_register_ability(
'datamachine/instagram-comment-reply',
array(
'label' => __( 'Reply to Instagram Comment', 'data-machine-socials' ),
'description' => __( 'Reply to a specific Instagram comment', 'data-machine-socials' ),
'category' => 'datamachine',
'input_schema' => array(
'type' => 'object',
'properties' => array(
'comment_id' => array(
'type' => 'string',
'description' => __( 'Instagram comment ID to reply to', 'data-machine-socials' ),
),
'message' => array(
'type' => 'string',
'maxLength' => self::MAX_REPLY_LENGTH,
'description' => __( 'Reply text', 'data-machine-socials' ),
),
),
'required' => array( 'comment_id', 'message' ),
),
'output_schema' => array(
'type' => 'object',
'properties' => array(
'success' => array( 'type' => 'boolean' ),
'data' => array( 'type' => 'object' ),
'error' => array( 'type' => 'string' ),
),
),
'execute_callback' => array( $this, 'execute' ),
'permission_callback' => array( $this, 'checkPermission' ),
'meta' => array( 'show_in_rest' => true ),
)
);
};
if ( doing_action( 'wp_abilities_api_init' ) ) {
$register_callback();
} elseif ( ! did_action( 'wp_abilities_api_init' ) ) {
add_action( 'wp_abilities_api_init', $register_callback );
}
}
public function execute( array $input ): array|\WP_Error {
$auth = $this->getAuthProvider();
if ( ! $auth ) {
return new \WP_Error( 'missing_auth', 'Instagram auth provider not available', array( 'status' => 401 ) );
}
$access_token = $auth->get_valid_access_token();
if ( empty( $access_token ) ) {
return new \WP_Error( 'missing_auth', 'Instagram access token unavailable (expired or refresh failed)', array( 'status' => 401 ) );
}
$comment_id = sanitize_text_field( $input['comment_id'] ?? '' );
$message = trim( sanitize_textarea_field( $input['message'] ?? '' ) );
if ( '' === $comment_id ) {
return new \WP_Error( 'missing_param', 'comment_id is required', array( 'status' => 400 ) );
}
if ( '' === $message ) {
return new \WP_Error( 'missing_param', 'message is required', array( 'status' => 400 ) );
}
if ( mb_strlen( $message ) > self::MAX_REPLY_LENGTH ) {
$message = mb_substr( $message, 0, self::MAX_REPLY_LENGTH );
}
return $this->replyToComment( $access_token, $comment_id, $message );
}
private function replyToComment( string $access_token, string $comment_id, string $message ): array|\WP_Error {
$url = self::GRAPH_API_URL . '/' . rawurlencode( $comment_id ) . '/replies';
$response = wp_remote_post(
$url,
array(
'timeout' => 30,
'body' => array(
'access_token' => $access_token,
'message' => $message,
),
)
);
if ( is_wp_error( $response ) ) {
return new \WP_Error( 'api_error', $response->get_error_message(), array( 'status' => 500 ) );
}
$status_code = wp_remote_retrieve_response_code( $response );
$body = json_decode( wp_remote_retrieve_body( $response ), true );
if ( 200 !== $status_code || isset( $body['error'] ) ) {
return new \WP_Error( 'api_error', $body['error']['message'] ?? 'Failed to reply to Instagram comment', array( 'status' => 500 ) );
}
return array(
'success' => true,
'data' => array(
'reply_id' => $body['id'] ?? '',
'comment_id' => $comment_id,
'message' => $message,
),
);
}
}