← All changes
|
addons/automator/includes/actions/facebook-actions.php
+111
-111
8.7.9
→
8.8.3
View file →
| @@ -1,111 +1,111 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * Facebook Graph API Actions | |
| 4 | - * | |
| 5 | - * @package WPbot_Automator | |
| 6 | - */ | |
| 7 | - | |
| 8 | -namespace WPbot_Automator\Actions; | |
| 9 | - | |
| 10 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | - exit; | |
| 12 | -} | |
| 13 | - | |
| 14 | -/** | |
| 15 | - * Class Facebook_Actions | |
| 16 | - */ | |
| 17 | -class Facebook_Actions extends Action { | |
| 18 | - | |
| 19 | - /** | |
| 20 | - * Constructor | |
| 21 | - */ | |
| 22 | - public function __construct() { | |
| 23 | - $this->id = 'facebook'; | |
| 24 | - $this->group = __( 'Facebook', 'wpbot-automator' ); | |
| 25 | - } | |
| 26 | - | |
| 27 | - /** | |
| 28 | - * Execute the action. | |
| 29 | - * | |
| 30 | - * @param array $action_data Configuration for this action. | |
| 31 | - * @param array $trigger_data Data passed from the trigger. | |
| 32 | - * @return array | |
| 33 | - */ | |
| 34 | - public function execute( $action_data, $trigger_data ) { | |
| 35 | - $action_id = isset( $action_data['actionId'] ) ? $action_data['actionId'] : ''; | |
| 36 | - | |
| 37 | - if ( empty( $action_id ) ) { | |
| 38 | - return false; | |
| 39 | - } | |
| 40 | - | |
| 41 | - switch ( $action_id ) { | |
| 42 | - case 'fb_create_post': | |
| 43 | - return $this->create_page_post( $action_data, $trigger_data ); | |
| 44 | - default: | |
| 45 | - return false; | |
| 46 | - } | |
| 47 | - } | |
| 48 | - | |
| 49 | - /** | |
| 50 | - * Publish a post to a Facebook Page via Graph API. | |
| 51 | - */ | |
| 52 | - private function create_page_post( $action_data, $trigger_data ) { | |
| 53 | - $cfg = isset( $action_data['config'] ) ? $action_data['config'] : array(); | |
| 54 | - | |
| 55 | - $cred_id = isset( $cfg['credential'] ) ? $cfg['credential'] : ''; | |
| 56 | - $credentials = get_option( 'wpbot_automator_credentials', array() ); | |
| 57 | - $cred_data = isset( $credentials[ $cred_id ]['data'] ) ? $credentials[ $cred_id ]['data'] : array(); | |
| 58 | - | |
| 59 | - $page_access_token = trim( $this->parse_tokens( isset( $cred_data['page_access_token'] ) ? $cred_data['page_access_token'] : '', $trigger_data ) ); | |
| 60 | - $page_id = trim( $this->parse_tokens( isset( $cfg['page_id'] ) ? $cfg['page_id'] : '', $trigger_data ) ); | |
| 61 | - $message = $this->parse_tokens( isset( $cfg['message'] ) ? $cfg['message'] : '', $trigger_data ); | |
| 62 | - $link = trim( $this->parse_tokens( isset( $cfg['link'] ) ? $cfg['link'] : '', $trigger_data ) ); | |
| 63 | - | |
| 64 | - if ( empty( $page_access_token ) || empty( $page_id ) || empty( $message ) ) { | |
| 65 | - return array( | |
| 66 | - 'success' => false, | |
| 67 | - 'message' => 'Facebook Create Post: page_access_token (from credential), page_id, and message are required.', | |
| 68 | - ); | |
| 69 | - } | |
| 70 | - | |
| 71 | - $url = "https://graph.facebook.com/v25.0/{$page_id}/feed"; | |
| 72 | - | |
| 73 | - $body = array( | |
| 74 | - 'message' => $message, | |
| 75 | - ); | |
| 76 | - | |
| 77 | - if ( ! empty( $link ) ) { | |
| 78 | - $body['link'] = $link; | |
| 79 | - } | |
| 80 | - | |
| 81 | - $response = wp_remote_post( | |
| 82 | - $url, | |
| 83 | - array( | |
| 84 | - 'headers' => array( | |
| 85 | - 'Content-Type' => 'application/json', | |
| 86 | - 'Authorization' => 'Bearer ' . $page_access_token, | |
| 87 | - ), | |
| 88 | - 'body' => wp_json_encode( $body ), | |
| 89 | - 'timeout' => 45, | |
| 90 | - ) | |
| 91 | - ); | |
| 92 | - | |
| 93 | - if ( is_wp_error( $response ) ) { | |
| 94 | - return array( 'success' => false, 'message' => $response->get_error_message() ); | |
| 95 | - } | |
| 96 | - | |
| 97 | - $code = wp_remote_retrieve_response_code( $response ); | |
| 98 | - $body_response = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 99 | - | |
| 100 | - if ( $code >= 200 && $code < 300 && isset( $body_response['id'] ) ) { | |
| 101 | - return array( | |
| 102 | - 'success' => true, | |
| 103 | - 'message' => "Facebook Post published successfully. Post ID: {$body_response['id']}", | |
| 104 | - 'post_id' => $body_response['id'], | |
| 105 | - ); | |
| 106 | - } | |
| 107 | - | |
| 108 | - $error = isset( $body_response['error']['message'] ) ? $body_response['error']['message'] : 'Unknown API error.'; | |
| 109 | - return array( 'success' => false, 'message' => "Facebook Create Post failed (HTTP {$code}): {$error}" ); | |
| 110 | - } | |
| 111 | -} | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Facebook Graph API Actions | |
| 4 | + * | |
| 5 | + * @package WPbot_Automator | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace WPbot_Automator\Actions; | |
| 9 | + | |
| 10 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | + exit; | |
| 12 | +} | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Class Facebook_Actions | |
| 16 | + */ | |
| 17 | +class Facebook_Actions extends Action { | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * Constructor | |
| 21 | + */ | |
| 22 | + public function __construct() { | |
| 23 | + $this->id = 'facebook'; | |
| 24 | + $this->group = __( 'Facebook', 'wpbot-automator' ); | |
| 25 | + } | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * Execute the action. | |
| 29 | + * | |
| 30 | + * @param array $action_data Configuration for this action. | |
| 31 | + * @param array $trigger_data Data passed from the trigger. | |
| 32 | + * @return array | |
| 33 | + */ | |
| 34 | + public function execute( $action_data, $trigger_data ) { | |
| 35 | + $action_id = isset( $action_data['actionId'] ) ? $action_data['actionId'] : ''; | |
| 36 | + | |
| 37 | + if ( empty( $action_id ) ) { | |
| 38 | + return false; | |
| 39 | + } | |
| 40 | + | |
| 41 | + switch ( $action_id ) { | |
| 42 | + case 'fb_create_post': | |
| 43 | + return $this->create_page_post( $action_data, $trigger_data ); | |
| 44 | + default: | |
| 45 | + return false; | |
| 46 | + } | |
| 47 | + } | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * Publish a post to a Facebook Page via Graph API. | |
| 51 | + */ | |
| 52 | + private function create_page_post( $action_data, $trigger_data ) { | |
| 53 | + $cfg = isset( $action_data['config'] ) ? $action_data['config'] : array(); | |
| 54 | + | |
| 55 | + $cred_id = isset( $cfg['credential'] ) ? $cfg['credential'] : ''; | |
| 56 | + $credentials = get_option( 'wpbot_automator_credentials', array() ); | |
| 57 | + $cred_data = isset( $credentials[ $cred_id ]['data'] ) ? $credentials[ $cred_id ]['data'] : array(); | |
| 58 | + | |
| 59 | + $page_access_token = trim( $this->parse_tokens( isset( $cred_data['page_access_token'] ) ? $cred_data['page_access_token'] : '', $trigger_data ) ); | |
| 60 | + $page_id = trim( $this->parse_tokens( isset( $cfg['page_id'] ) ? $cfg['page_id'] : '', $trigger_data ) ); | |
| 61 | + $message = $this->parse_tokens( isset( $cfg['message'] ) ? $cfg['message'] : '', $trigger_data ); | |
| 62 | + $link = trim( $this->parse_tokens( isset( $cfg['link'] ) ? $cfg['link'] : '', $trigger_data ) ); | |
| 63 | + | |
| 64 | + if ( empty( $page_access_token ) || empty( $page_id ) || empty( $message ) ) { | |
| 65 | + return array( | |
| 66 | + 'success' => false, | |
| 67 | + 'message' => 'Facebook Create Post: page_access_token (from credential), page_id, and message are required.', | |
| 68 | + ); | |
| 69 | + } | |
| 70 | + | |
| 71 | + $url = "https://graph.facebook.com/v25.0/{$page_id}/feed"; | |
| 72 | + | |
| 73 | + $body = array( | |
| 74 | + 'message' => $message, | |
| 75 | + ); | |
| 76 | + | |
| 77 | + if ( ! empty( $link ) ) { | |
| 78 | + $body['link'] = $link; | |
| 79 | + } | |
| 80 | + | |
| 81 | + $response = wp_remote_post( | |
| 82 | + $url, | |
| 83 | + array( | |
| 84 | + 'headers' => array( | |
| 85 | + 'Content-Type' => 'application/json', | |
| 86 | + 'Authorization' => 'Bearer ' . $page_access_token, | |
| 87 | + ), | |
| 88 | + 'body' => wp_json_encode( $body ), | |
| 89 | + 'timeout' => 45, | |
| 90 | + ) | |
| 91 | + ); | |
| 92 | + | |
| 93 | + if ( is_wp_error( $response ) ) { | |
| 94 | + return array( 'success' => false, 'message' => $response->get_error_message() ); | |
| 95 | + } | |
| 96 | + | |
| 97 | + $code = wp_remote_retrieve_response_code( $response ); | |
| 98 | + $body_response = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 99 | + | |
| 100 | + if ( $code >= 200 && $code < 300 && isset( $body_response['id'] ) ) { | |
| 101 | + return array( | |
| 102 | + 'success' => true, | |
| 103 | + 'message' => "Facebook Post published successfully. Post ID: {$body_response['id']}", | |
| 104 | + 'post_id' => $body_response['id'], | |
| 105 | + ); | |
| 106 | + } | |
| 107 | + | |
| 108 | + $error = isset( $body_response['error']['message'] ) ? $body_response['error']['message'] : 'Unknown API error.'; | |
| 109 | + return array( 'success' => false, 'message' => "Facebook Create Post failed (HTTP {$code}): {$error}" ); | |
| 110 | + } | |
| 111 | +} | |