From ff26ba3d0c90711921f4bfd47fa27aebb2f67491 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 6 Mar 2019 14:37:01 +0100 Subject: [PATCH] Post featured news post links to Twitter. --- config/config.example.ini | 2 ++ public/manage/news.php | 20 ++++++++++++++++++-- src/twitter.php | 6 ++---- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/config/config.example.ini b/config/config.example.ini index a71b1ba6..eec552db 100644 --- a/config/config.example.ini +++ b/config/config.example.ini @@ -50,3 +50,5 @@ secret_key = change_this_or_i_will_beat_you_up [Twitter] api_key = api_secret = +token = +token_secret = diff --git a/public/manage/news.php b/public/manage/news.php index b34ef5c4..34e08599 100644 --- a/public/manage/news.php +++ b/public/manage/news.php @@ -86,12 +86,14 @@ switch ($_GET['v'] ?? null) { if (!empty($_POST['post']) && csrf_verify('news_post', $_POST['csrf'] ?? '')) { $originalPostId = (int)($_POST['post']['id'] ?? null); $currentUserId = user_session_current('user_id'); + $title = $_POST['post']['title'] ?? null; + $isFeatured = !empty($_POST['post']['featured']); $postId = news_post_create( - $_POST['post']['title'] ?? null, + $title, $_POST['post']['text'] ?? null, (int)($_POST['post']['category'] ?? null), user_session_current('user_id'), - !empty($_POST['post']['featured']), + $isFeatured, null, $originalPostId ); @@ -102,6 +104,20 @@ switch ($_GET['v'] ?? null) { $currentUserId, [$postId] ); + + if (!$originalPostId && $isFeatured) { + $twitterApiKey = config_get('Twitter', 'api_key'); + $twitterApiSecret = config_get('Twitter', 'api_secret'); + $twitterToken = config_get('Twitter', 'token'); + $twitterTokenSecret = config_get('Twitter', 'token_secret'); + + if (!empty($twitterApiKey) && !empty($twitterApiSecret) + && !empty($twitterToken) && !empty($twitterTokenSecret)) { + twitter_init($twitterApiKey, $twitterApiSecret, $twitterToken, $twitterTokenSecret); + $url = url('news-post', ['post' => $postId]); + twitter_tweet_post("News :: {$title}\nhttps://{$_SERVER['HTTP_HOST']}{$url}"); + } + } } if ($postId > 0) { diff --git a/src/twitter.php b/src/twitter.php index 2f8d4e86..ec0a936e 100644 --- a/src/twitter.php +++ b/src/twitter.php @@ -55,9 +55,7 @@ function twitter_auth_complete(string $pin): array function twitter_tweet_post(string $text): void { - $params = [ + Codebird::getInstance()->statuses_update([ 'status' => $text, - ]; - - Codebird::getInstance()->statuses_update($params); + ]); }