-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFacebookDeleteAbility.php
More file actions
143 lines (118 loc) · 3.86 KB
/
FacebookDeleteAbility.php
File metadata and controls
143 lines (118 loc) · 3.86 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
<?php
/**
* Facebook Delete Ability
*
* Abilities API primitive for deleting Facebook Page posts.
*
* @package DataMachineSocials
* @subpackage Abilities\Facebook
* @since 0.3.0
*/
namespace DataMachineSocials\Abilities\Facebook;
use DataMachine\Abilities\PermissionHelper;
use DataMachineSocials\Handlers\Facebook\FacebookAuth;
use DataMachineSocials\Abilities\Traits\HasCheckPermission;
defined( 'ABSPATH' ) || exit;
class FacebookDeleteAbility {
use HasCheckPermission;
private static bool $registered = false;
const GRAPH_API_URL = 'https://graph.facebook.com/v23.0';
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/facebook-delete',
array(
'label' => __( 'Delete Facebook Posts', 'data-machine-socials' ),
'description' => __( 'Delete posts from your Facebook Page', 'data-machine-socials' ),
'category' => 'datamachine',
'input_schema' => array(
'type' => 'object',
'properties' => array(
'post_id' => array(
'type' => 'string',
'description' => __( 'Facebook post ID to delete', 'data-machine-socials' ),
),
),
'required' => array( 'post_id' ),
),
'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', 'Facebook auth provider not available', array( 'status' => 401 ) );
}
$access_token = $auth->get_page_access_token();
if ( empty( $access_token ) ) {
return new \WP_Error( 'missing_auth', 'Facebook page access token unavailable', array( 'status' => 401 ) );
}
if ( empty( $input['post_id'] ) ) {
return new \WP_Error( 'missing_param', 'post_id is required', array( 'status' => 400 ) );
}
$url = self::GRAPH_API_URL . '/' . rawurlencode( $input['post_id'] );
$response = wp_remote_post(
$url,
array(
'timeout' => 30,
'body' => array(
'access_token' => $access_token,
'method' => 'DELETE',
),
)
);
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['success'] ) && $body['success'] ) ) {
return array(
'success' => true,
'data' => array(
'post_id' => $input['post_id'],
'deleted' => true,
),
);
}
return new \WP_Error( 'api_error', $body['error']['message'] ?? 'Failed to delete post', array( 'status' => 500 ) );
}
private function getAuthProvider(): ?FacebookAuth {
if ( ! class_exists( '\DataMachine\Abilities\AuthAbilities' ) ) {
return null;
}
$auth = new \DataMachine\Abilities\AuthAbilities();
$provider = $auth->getProvider( 'facebook' );
if ( ! $provider instanceof FacebookAuth ) {
return null;
}
return $provider;
}
}