get( 'system_user_token', '' ); $app_id = (string) $settings->get_facebook_app_id(); $app_secret = $secure_options->get( 'facebook_app_secret', '' ); if ( empty( $system_user_token ) || empty( $app_id ) || empty( $app_secret ) ) { return false; } $appToken = $app_id . '|' . $app_secret; $debug_token = $this->debugToken( $system_user_token, $appToken ); if ( (string) ($debug_token['app_id'] ?? '') !== $app_id || empty( $debug_token['is_valid'] ) ) { return false; } if ( (string) ($debug_token['type'] ?? '') !== 'SYSTEM_USER' ) { return false; } return true; } public function debugToken( $input_token, $access_token ) { $url = $this->graphUrl( 'debug_token', array( 'input_token' => $input_token, 'access_token' => $access_token, )); $response = new Response(wp_remote_get( $url )); if (!$response->isSuccessful()) { Log::error( 'Debug token request failed', array( 'error' => $response->getError() ) ); return false; } return $response->getBody(); } protected function graphUrl( $path, $params = array() ) { $url = 'https://graph.facebook.com/v24.0/' . $path; $url = add_query_arg( $params, $url ); return $url; } /** * Helper to GET from the Facebook Graph API, always including the system user access token. * * @param string $path Graph endpoint path (e.g., 'me', 'some_object'). * @param array $params Additional parameters for the request. * @return array|false|Response Decoded response body array, or WP_Error. */ public function get( $path, $params = array(), $token = null, $return_response = false ) { $secure_options = new SecureOptions(); $system_user_token = $token ?? $secure_options->get( 'system_user_token', '' ); if ( empty( $system_user_token ) ) { return false; } $params['access_token'] = $system_user_token; $url = $this->graphUrl( $path, $params ); $get = wp_remote_get( $url, array( 'timeout' => 30, ) ); $response = new Response($get); if (!$response->isSuccessful()) { return $return_response ? $response : false; } return $return_response ? $response : $response->getBody(); } /** * Helper to POST to the Facebook Graph API, always including the system user access token. * * @param string $path Graph endpoint path (e.g., 'me/feed'). * @param array $params POST data to send. * @return array|false|Response Decoded response body array, or WP_Error. */ public function post( $path, $params = array(), $token = null, $return_response = false ) { $secure_options = new SecureOptions(); $system_user_token = $token ?? $secure_options->get( 'system_user_token', '' ); if ( empty( $system_user_token ) ) { return false; } $url = $this->graphUrl( $path ); $post = wp_remote_post( $url, array( 'body' => wp_json_encode( (array) $params ), 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8', 'Authorization' => 'Bearer ' . $system_user_token, ), 'timeout' => 30, ) ); $response = new Response($post); if (!$response->isSuccessful()) { return $return_response ? $response : false; } return $return_response ? $response : $response->getBody(); } /** * Helper to POST JSON to the Facebook Graph API, always using bearer token auth. * * Required by endpoints like WhatsApp Cloud API `/messages`. * * @param string $path Graph endpoint path (e.g., '{phone_number_id}/messages'). * @param array $payload JSON payload. * @return array|false|Response Decoded response body array, or WP_Error. */ public function postJson( $path, $payload = array(), $return_response = false ) { $secure_options = new SecureOptions(); $system_user_token = $secure_options->get( 'system_user_token', '' ); if ( empty( $system_user_token ) ) { return false; } $url = $this->graphUrl( $path ); $post = wp_remote_post( $url, array( 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8', 'Authorization' => 'Bearer ' . $system_user_token, ), 'body' => wp_json_encode( (array) $payload ), 'timeout' => 30, ) ); $response = new Response($post); if (!$response->isSuccessful()) { return $return_response ? $response : false; } return $return_response ? $response : $response->getBody(); } /** * Get info about the owner of the system user token ("/me" endpoint). * * @return array Decoded response body array with owner info, or WP_Error. */ public function me($token = null) { return $this->get( 'me', array(), $token ); } /** * Helper to DELETE from the Facebook Graph API. * * @param string $path Graph endpoint path (e.g., '{waba_id}/message_templates'). * @param array $params Query parameters (e.g., hsm_id). * @return array|false Decoded response body array, or false on failure. */ public function delete( $path, $params = array() ) { $secure_options = new SecureOptions(); $system_user_token = $secure_options->get( 'system_user_token', '' ); if ( empty( $system_user_token ) ) { return false; } $params['access_token'] = $system_user_token; $url = $this->graphUrl( $path, $params ); $response = wp_remote_request( $url, array( 'method' => 'DELETE', 'timeout' => 30, ) ); $resp = new Response( $response ); if ( ! $resp->isSuccessful() ) { return false; } return true; } }