PluginProbe
Social Engine / 0.8.8
Social Engine v0.8.8
0.9.2 0.9.1 0.9.0 0.8.9 trunk 0.0.1 0.0.2 0.0.4 0.0.5 0.0.7 0.0.8 0.0.9 0.1.1 0.1.2 0.1.3 0.1.4 0.1.5 0.1.6 0.1.8 0.1.9 0.2.0 0.2.1 0.2.2 0.2.3 0.2.4 All 88 releases
social-engine / classes / api.php

api.php in Social Engine 0.8.8, at classes/api.php

242 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Meow_SCLEGN_API {
4
5 public $core;
6 public $namespace = 'social-engine/api/v1';
7 private $bearer_token = null;
8
9 public function __construct( $core ) {
10 $this->core = $core;
11 add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
12 }
13
14 function rest_api_init() {
15 $public_api = $this->core->get_option( 'public_api', false );
16 if (!$public_api) { return; }
17
18 $this->bearer_token = $this->core->get_option( 'api_token', null );
19 if ( !empty( $this->bearer_token ) ) {
20 add_filter( 'sclegn_allow_public_api', [ $this, 'auth_via_bearer_token' ], 10, 3 );
21 }
22
23 register_rest_route( $this->namespace, '/post', array(
24 'methods' => 'POST',
25 'callback' => array( $this, 'rest_api_post' ),
26 'permission_callback' => function ( $request ) {
27 return $this->core->can_access_public_api( 'post', $request );
28 }
29 ) );
30
31 register_rest_route( $this->namespace, '/accounts', array(
32 'methods' => 'GET',
33 'callback' => array( $this, 'rest_api_accounts' ),
34 'permission_callback' => function ( $request ) {
35 return $this->core->can_access_public_api( 'accounts', $request );
36 }
37 ) );
38
39 register_rest_route( $this->namespace, '/account', array(
40 'methods' => 'GET',
41 'callback' => array( $this, 'rest_api_account' ),
42 'permission_callback' => function ( $request ) {
43 return $this->core->can_access_public_api( 'account', $request );
44 }
45 ) );
46 }
47
48 #region Helpers
49 private function get_default_account() {
50 $accounts = $this->core->get_accounts();
51 $default_accountID = $this->core->get_option( 'default_service', null );
52
53 $accounts = array_filter( $accounts, function( $account ) use ( $default_accountID ) {
54 return $account['accountId'] === $default_accountID;
55 } );
56
57 $account = array_shift( $accounts );
58
59 $response = array(
60 'name' => $account['name'],
61 'type' => $account['type'],
62 'serviceId' => $account['serviceId'],
63 'accountId' => $account['accountId'],
64 );
65
66 return $response;
67 }
68
69 private function verify_account( $account ) {
70 // Check if the account array has the required keys
71 $required_keys = [ 'serviceId', 'accountId', 'type' ];
72 foreach ( $required_keys as $key ) {
73 if ( !array_key_exists( $key, $account) || empty ($account[ $key ] ) ) {
74 $this->core->log( 'Missing required key in account: ' . $key );
75 return false; // Missing required key
76 }
77 }
78
79 return true; // All required keys are present
80 }
81 #endregion
82
83 #region REST API
84
85 public function rest_api_post( $request ) {
86 $params = $request->get_params();
87
88 $post_date = new DateTime( 'now' );
89 $post_content = $params['content'] ?? '';
90 $thumbnail_ids = $params['images'] ?? [];
91
92 $post_status = $params['status'] ?? 'publish';
93
94 $service_id = $params['service_id'] ?? null;
95 $account_id = $params['account_id'] ?? null;
96 $service_type = $params['service_type'] ?? null;
97
98 $use_default_account = $params['use_default_account'] ?? true;
99 if ($use_default_account) {
100 $default_account = $this->get_default_account();
101 $service_id = $default_account['serviceId'];
102 $account_id = $default_account['accountId'];
103 $service_type = $default_account['type'];
104 }
105
106 $post_id = $this->core->create_social_post( $post_date, $post_content, $post_status, $thumbnail_ids, $service_id, $account_id, $service_type );
107
108 $response = array(
109 'post_id' => $post_id
110 );
111
112 if ( $post_status === 'publish' ) {
113 $this->core->post( $post_id );
114 }
115
116 return rest_ensure_response( $response );
117 }
118
119 public function rest_api_account( $request ) {
120 return rest_ensure_response( $this->get_default_account() );
121 }
122
123 public function rest_api_accounts( $request ) {
124 $params = $request->get_params();
125
126 $name = $params['name'] ?? null;
127 $type = $params['type'] ?? null;
128
129 // Reuse the shared list_accounts method for consistency
130 $response = $this->list_accounts( $type, $name );
131
132 return rest_ensure_response( $response );
133 }
134
135 #endregion
136
137 #region Public API
138
139 public function post( $account = [], $content = [], $publish = true )
140 {
141 if( empty( $account ) ) {
142 $account = $this->get_default_account();
143 }
144
145 if( empty( $content ) || !is_array( $content ) ) {
146 return new WP_Error( 'invalid_content', __( 'Content must be provided.', SCLEGN_DOMAIN ), array( 'status' => 400 ) );
147 }
148
149 if( !is_array( $account ) ) {
150 return new WP_Error( 'invalid_account', __( 'Account must be an array.', SCLEGN_DOMAIN ), array( 'status' => 400 ) );
151 }
152
153 if ( !$this->verify_account( $account ) ) {
154 return new WP_Error( 'invalid_account', __( 'Invalid account provided.', SCLEGN_DOMAIN ), array( 'status' => 400 ) );
155 }
156
157 $date = new DateTime( 'now' );
158 $status = $publish ? 'publish' : 'draft';
159 $text = array_key_exists( 'text', $content ) ? $content['text'] : '';
160 $medias = array_key_exists( 'media', $content ) ? $content['media'] : [];
161
162 $service_id = $account['serviceId'];
163 $account_id = $account['accountId'];
164 $service_type = $account['type'];
165
166 $post_id = $this->core->create_social_post(
167 $date,
168 $text,
169 $status,
170 $medias,
171
172 $service_id,
173 $account_id,
174 $service_type
175 );
176
177 if ( $status === 'publish' ) {
178 $this->core->post( $post_id );
179 }
180
181 return $post_id;
182 }
183
184 public function list_accounts( $type = null, $name = null ) {
185 $accounts = $this->core->get_accounts();
186 $response = array();
187
188 foreach ( $accounts as $account ) {
189 // Use partial matching for name filter (stripos for case-insensitive search)
190 $name_matches = ($name === null || stripos( $account['name'], $name ) !== false);
191 $type_matches = ($type === null || $account['type'] === $type);
192
193 if ( $name_matches && $type_matches ) {
194 $response[] = array(
195 'name' => $account['name'],
196 'type' => $account['type'],
197 'serviceId' => $account['serviceId'],
198 'accountId' => $account['accountId'],
199 );
200 }
201 }
202
203 return $response;
204 }
205
206 public function get_account() {
207 $default_account = $this->get_default_account();
208 return $default_account;
209 }
210
211 #region Auth
212
213 public function auth_via_bearer_token( $allow, $feature, $extra ) {
214 if ( !empty( $extra ) && !empty( $extra->get_header( 'Authorization' ) ) ) {
215 $token = $extra->get_header( 'Authorization' );
216 $token = str_replace( 'Bearer ', '', $token );
217 if ( $token === $this->bearer_token ) {
218 $admin = $this->get_admin_user();
219 wp_set_current_user( $admin->ID, $admin->user_login );
220 return true;
221 }
222 }
223 return $allow;
224 }
225
226 function can_access_public_api( $feature, $extra ) {
227 $logged_in = is_user_logged_in();
228 return apply_filters( 'sclegn_allow_public_api', $logged_in, $feature, $extra );
229 }
230
231 function get_admin_user() {
232 $admin = get_users( [ 'role' => 'administrator' ] );
233 if ( !empty( $admin ) ) {
234 return $admin[0];
235 }
236 return null;
237 }
238
239 #endregion
240
241
242 }