]> git.mxchange.org Git - friendica-addons.git/blobdiff - twitter/twitter.php
Use short form array syntax everywhere
[friendica-addons.git] / twitter / twitter.php
index 5782c4b50d2379930c30caabeec9d6c2f7a434e8..807445b6de536fcb368760666d5f78017ea21c27 100644 (file)
@@ -61,6 +61,7 @@
  */
 
 use Friendica\App;
+use Friendica\Content\OEmbed;
 use Friendica\Core\Config;
 use Friendica\Core\PConfig;
 use Friendica\Core\Worker;
@@ -150,7 +151,7 @@ function twitter_follow(App $a, &$contact)
        $cb->setConsumerKey($ckey, $csecret);
        $cb->setToken($otoken, $osecret);
 
-       $parameters = array();
+       $parameters = [];
        $parameters["screen_name"] = $nickname;
 
        $user = $cb->friendships_create($parameters);
@@ -258,16 +259,12 @@ function twitter_settings(App $a, &$s)
        $csecret = Config::get('twitter', 'consumersecret' );
        $otoken  = PConfig::get(local_user(), 'twitter', 'oauthtoken'  );
        $osecret = PConfig::get(local_user(), 'twitter', 'oauthsecret' );
-       $enabled = PConfig::get(local_user(), 'twitter', 'post');
-       $checked = (($enabled) ? ' checked="checked" ' : '');
-       $defenabled = PConfig::get(local_user(), 'twitter', 'post_by_default');
-       $defchecked = (($defenabled) ? ' checked="checked" ' : '');
-       $mirrorenabled = PConfig::get(local_user(), 'twitter', 'mirror_posts');
-       $mirrorchecked = (($mirrorenabled) ? ' checked="checked" ' : '');
-       $importenabled = PConfig::get(local_user(), 'twitter', 'import');
-       $importchecked = (($importenabled) ? ' checked="checked" ' : '');
-       $create_userenabled = PConfig::get(local_user(), 'twitter', 'create_user');
-       $create_userchecked = (($create_userenabled) ? ' checked="checked" ' : '');
+
+       $enabled            = intval(PConfig::get(local_user(), 'twitter', 'post'));
+       $defenabled         = intval(PConfig::get(local_user(), 'twitter', 'post_by_default'));
+       $mirrorenabled      = intval(PConfig::get(local_user(), 'twitter', 'mirror_posts'));
+       $importenabled      = intval(PConfig::get(local_user(), 'twitter', 'import'));
+       $create_userenabled = intval(PConfig::get(local_user(), 'twitter', 'create_user'));
 
        $css = (($enabled) ? '' : '-disabled');
 
@@ -320,36 +317,40 @@ function twitter_settings(App $a, &$s)
                        require_once 'library/twitteroauth.php';
                        $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
                        $details = $connection->get('account/verify_credentials');
-                       $s .= '<div id="twitter-info" ><img id="twitter-avatar" src="' . $details->profile_image_url . '" /><p id="twitter-info-block">' . t('Currently connected to: ') . '<a href="https://twitter.com/' . $details->screen_name . '" target="_twitter">' . $details->screen_name . '</a><br /><em>' . $details->description . '</em></p></div>';
-                       $s .= '<p>' . t('If enabled all your <strong>public</strong> postings can be posted to the associated Twitter account. You can choose to do so by default (here) or for every posting separately in the posting options when writing the entry.') . '</p>';
-                       if ($a->user['hidewall']) {
-                               $s .= '<p>' . t('<strong>Note</strong>: Due your privacy settings (<em>Hide your profile details from unknown viewers?</em>) the link potentially included in public postings relayed to Twitter will lead the visitor to a blank page informing the visitor that the access to your profile has been restricted.') . '</p>';
-                       }
-                       $s .= '<div id="twitter-enable-wrapper">';
-                       $s .= '<label id="twitter-enable-label" for="twitter-checkbox">' . t('Allow posting to Twitter') . '</label>';
-                       $s .= '<input id="twitter-checkbox" type="checkbox" name="twitter-enable" value="1" ' . $checked . '/>';
-                       $s .= '<div class="clear"></div>';
-                       $s .= '<label id="twitter-default-label" for="twitter-default">' . t('Send public postings to Twitter by default') . '</label>';
-                       $s .= '<input id="twitter-default" type="checkbox" name="twitter-default" value="1" ' . $defchecked . '/>';
-                       $s .= '<div class="clear"></div>';
 
-                       $s .= '<label id="twitter-mirror-label" for="twitter-mirror">' . t('Mirror all posts from twitter that are no replies') . '</label>';
-                       $s .= '<input id="twitter-mirror" type="checkbox" name="twitter-mirror" value="1" ' . $mirrorchecked . '/>';
+                       $field_checkbox = get_markup_template('field_checkbox.tpl');
+
+                       $s .= '<div id="twitter-info" >
+                               <p>' . t('Currently connected to: ') . '<a href="https://twitter.com/' . $details->screen_name . '" target="_twitter">' . $details->screen_name . '</a>
+                                       <button type="submit" name="twitter-disconnect" value="1">' . t('Disconnect') . '</button>
+                               </p>
+                               <p id="twitter-info-block">
+                                       <a href="https://twitter.com/' . $details->screen_name . '" target="_twitter"><img id="twitter-avatar" src="' . $details->profile_image_url . '" /></a>
+                                       <em>' . $details->description . '</em>
+                               </p>
+                       </div>';
                        $s .= '<div class="clear"></div>';
-                       $s .= '</div>';
 
-                       $s .= '<label id="twitter-import-label" for="twitter-import">' . t('Import the remote timeline') . '</label>';
-                       $s .= '<input id="twitter-import" type="checkbox" name="twitter-import" value="1" ' . $importchecked . '/>';
-                       $s .= '<div class="clear"></div>';
+                       $s .= replace_macros($field_checkbox, [
+                               '$field' => ['twitter-enable', t('Allow posting to Twitter'), $enabled, t('If enabled all your <strong>public</strong> postings can be posted to the associated Twitter account. You can choose to do so by default (here) or for every posting separately in the posting options when writing the entry.')]
+                       ]);
+                       if ($a->user['hidewall']) {
+                               $s .= '<p>' . t('<strong>Note</strong>: Due to your privacy settings (<em>Hide your profile details from unknown viewers?</em>) the link potentially included in public postings relayed to Twitter will lead the visitor to a blank page informing the visitor that the access to your profile has been restricted.') . '</p>';
+                       }
+                       $s .= replace_macros($field_checkbox, [
+                               '$field' => ['twitter-default', t('Send public postings to Twitter by default'), $defenabled, '']
+                       ]);
+                       $s .= replace_macros($field_checkbox, [
+                               '$field' => ['twitter-mirror', t('Mirror all posts from twitter that are no replies'), $mirrorenabled, '']
+                       ]);
+                       $s .= replace_macros($field_checkbox, [
+                               '$field' => ['twitter-import', t('Import the remote timeline'), $importenabled, '']
+                       ]);
+                       $s .= replace_macros($field_checkbox, [
+                               '$field' => ['twitter-create_user', t('Automatically create contacts'), $create_userenabled, '']
+                       ]);
 
-                       $s .= '<label id="twitter-create_user-label" for="twitter-create_user">' . t('Automatically create contacts') . '</label>';
-                       $s .= '<input id="twitter-create_user" type="checkbox" name="twitter-create_user" value="1" ' . $create_userchecked . '/>';
                        $s .= '<div class="clear"></div>';
-
-                       $s .= '<div id="twitter-disconnect-wrapper">';
-                       $s .= '<label id="twitter-disconnect-label" for="twitter-disconnect">' . t('Clear OAuth configuration') . '</label>';
-                       $s .= '<input id="twitter-disconnect" type="checkbox" name="twitter-disconnect" value="1" />';
-                       $s .= '</div><div class="clear"></div>';
                        $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>';
                }
        }
@@ -398,7 +399,7 @@ function twitter_action(App $a, $uid, $pid, $action)
        $cb->setConsumerKey($ckey, $csecret);
        $cb->setToken($otoken, $osecret);
 
-       $post = array('id' => $pid);
+       $post = ['id' => $pid];
 
        logger("twitter_action '" . $action . "' ID: " . $pid . " data: " . print_r($post, true), LOGGER_DATA);
 
@@ -467,7 +468,7 @@ function twitter_post_hook(App $a, &$b)
 
                // Dont't post if the post doesn't belong to us.
                // This is a check for forum postings
-               $self = dba::select('contact', array('id'), array('uid' => $b['uid'], 'self' => true), array('limit' => 1));
+               $self = dba::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
                if ($b['contact-id'] != $self['id']) {
                        return;
                }
@@ -522,7 +523,7 @@ function twitter_post_hook(App $a, &$b)
 
                $max_char = 280;
                require_once "include/plaintext.php";
-               $msgarr = plaintext($a, $b, $max_char, true, 8);
+               $msgarr = plaintext($b, $max_char, true, 8);
                $msg = $msgarr["text"];
 
                if (($msg == "") && isset($msgarr["title"])) {
@@ -556,7 +557,7 @@ function twitter_post_hook(App $a, &$b)
                        $cb->setConsumerKey($ckey, $csecret);
                        $cb->setToken($otoken, $osecret);
 
-                       $post = array('status' => $msg, 'media[]' => $tempfile);
+                       $post = ['status' => $msg, 'media[]' => $tempfile];
 
                        if ($iscomment) {
                                $post["in_reply_to_status_id"] = substr($orig_post["uri"], 9);
@@ -591,7 +592,7 @@ function twitter_post_hook(App $a, &$b)
 // -----------------
                        $max_char = 280;
                        require_once "include/plaintext.php";
-                       $msgarr = plaintext($a, $b, $max_char, true, 8);
+                       $msgarr = plaintext($b, $max_char, true, 8);
                        $msg = $msgarr["text"];
 
                        if (($msg == "") && isset($msgarr["title"])) {
@@ -603,7 +604,7 @@ function twitter_post_hook(App $a, &$b)
                        }
 // -----------------
                        $url = 'statuses/update';
-                       $post = array('status' => $msg, 'weighted_character_count' => 'true');
+                       $post = ['status' => $msg, 'weighted_character_count' => 'true'];
 
                        if ($iscomment) {
                                $post["in_reply_to_status_id"] = substr($orig_post["uri"], 9);
@@ -624,7 +625,7 @@ function twitter_post_hook(App $a, &$b)
                                        $a->contact = $r[0]["id"];
                                }
 
-                               $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $post));
+                               $s = serialize(['url' => $url, 'item' => $b['id'], 'post' => $post]);
                                require_once 'include/queue_fn.php';
                                add_to_queue($a->contact, NETWORK_TWITTER, $s);
                                notice(t('Twitter post failed. Queued for retry.') . EOL);
@@ -652,12 +653,12 @@ function twitter_plugin_admin(App $a, &$o)
 {
        $t = get_markup_template("admin.tpl", "addon/twitter/");
 
-       $o = replace_macros($t, array(
+       $o = replace_macros($t, [
                '$submit' => t('Save Settings'),
                // name, label, value, help, [extra values]
-               '$consumerkey' => array('consumerkey', t('Consumer key'), Config::get('twitter', 'consumerkey'), ''),
-               '$consumersecret' => array('consumersecret', t('Consumer secret'), Config::get('twitter', 'consumersecret'), ''),
-       ));
+               '$consumerkey' => ['consumerkey', t('Consumer key'), Config::get('twitter', 'consumerkey'), ''],
+               '$consumersecret' => ['consumersecret', t('Consumer secret'), Config::get('twitter', 'consumersecret'), ''],
+       ]);
 }
 
 function twitter_cron(App $a, $b)
@@ -736,9 +737,9 @@ function twitter_expire(App $a, $b)
        }
 
        if (method_exists('dba', 'delete')) {
-               $r = dba::select('item', array('id'), array('deleted' => true, 'network' => NETWORK_TWITTER));
+               $r = dba::select('item', ['id'], ['deleted' => true, 'network' => NETWORK_TWITTER]);
                while ($row = dba::fetch($r)) {
-                       dba::delete('item', array('id' => $row['id']));
+                       dba::delete('item', ['id' => $row['id']]);
                }
                dba::close($r);
        } else {
@@ -788,7 +789,7 @@ function twitter_prepare_body(App $a, &$b)
                        }
                }
 
-               $msgarr = plaintext($a, $item, $max_char, true, 8);
+               $msgarr = plaintext($item, $max_char, true, 8);
                $msg = $msgarr["text"];
 
                if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
@@ -824,13 +825,13 @@ function twitter_do_mirrorpost(App $a, $uid, $post)
 
        if (is_object($post->retweeted_status)) {
                // We don't support nested shares, so we mustn't show quotes as shares on retweets
-               $item = twitter_createpost($a, $uid, $post->retweeted_status, array('id' => 0), false, false, true);
+               $item = twitter_createpost($a, $uid, $post->retweeted_status, ['id' => 0], false, false, true);
 
                $datarray['body'] = "\n" . share_header($item['author-name'], $item['author-link'], $item['author-avatar'], "", $item['created'], $item['plink']);
 
                $datarray['body'] .= $item['body'] . '[/share]';
        } else {
-               $item = twitter_createpost($a, $uid, $post, array('id' => 0), false, false, false);
+               $item = twitter_createpost($a, $uid, $post, ['id' => 0], false, false, false);
 
                $datarray['body'] = $item['body'];
        }
@@ -872,7 +873,7 @@ function twitter_fetchtimeline(App $a, $uid)
        require_once 'library/twitteroauth.php';
        $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
 
-       $parameters = array("exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended");
+       $parameters = ["exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended"];
 
        $first_time = ($lastid == "");
 
@@ -1003,11 +1004,11 @@ function twitter_fetch_contact($uid, $contact, $create_user)
 
        $avatar = twitter_fix_avatar($contact->profile_image_url_https);
 
-       GContact::update(array("url" => "https://twitter.com/" . $contact->screen_name,
+       GContact::update(["url" => "https://twitter.com/" . $contact->screen_name,
                "network" => NETWORK_TWITTER, "photo" => $avatar, "hide" => true,
                "name" => $contact->name, "nick" => $contact->screen_name,
                "location" => $contact->location, "about" => $contact->description,
-               "addr" => $contact->screen_name . "@twitter.com", "generation" => 2));
+               "addr" => $contact->screen_name . "@twitter.com", "generation" => 2]);
 
        $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
                intval($uid), dbesc("twitter::" . $contact->id_str));
@@ -1148,7 +1149,7 @@ function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "")
                return;
        }
 
-       $parameters = array();
+       $parameters = [];
 
        if ($screen_name != "") {
                $parameters["screen_name"] = $screen_name;
@@ -1172,7 +1173,6 @@ function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "")
 
 function twitter_expand_entities(App $a, $body, $item, $no_tags = false, $picture)
 {
-       require_once "include/oembed.php";
        require_once "include/network.php";
 
        $tags = "";
@@ -1191,7 +1191,7 @@ function twitter_expand_entities(App $a, $body, $item, $no_tags = false, $pictur
                        if ($url->url && $url->expanded_url && $url->display_url) {
                                $expanded_url = original_url($url->expanded_url);
 
-                               $oembed_data = oembed_fetch_url($expanded_url);
+                               $oembed_data = OEmbed::fetchURL($expanded_url);
 
                                // Quickfix: Workaround for URL with "[" and "]" in it
                                if (strpos($expanded_url, "[") || strpos($expanded_url, "]")) {
@@ -1261,10 +1261,10 @@ function twitter_expand_entities(App $a, $body, $item, $no_tags = false, $pictur
                }
 
                if ($no_tags) {
-                       return array("body" => $body, "tags" => "", "plain" => $plain);
+                       return ["body" => $body, "tags" => "", "plain" => $plain];
                }
 
-               $tags_arr = array();
+               $tags_arr = [];
 
                foreach ($item->entities->hashtags AS $hashtag) {
                        $url = "#[url=" . $a->get_baseurl() . "/search?tag=" . rawurlencode($hashtag->text) . "]" . $hashtag->text . "[/url]";
@@ -1319,7 +1319,7 @@ function twitter_expand_entities(App $a, $body, $item, $no_tags = false, $pictur
 
                $tags = implode($tags_arr, ",");
        }
-       return array("body" => $body, "tags" => $tags, "plain" => $plain);
+       return ["body" => $body, "tags" => $tags, "plain" => $plain];
 }
 
 /**
@@ -1351,7 +1351,7 @@ function twitter_media_entities($post, &$postarray)
        }
 
        // This is a pure media post, first search for all media urls
-       $media = array();
+       $media = [];
        foreach ($post->extended_entities->media AS $medium) {
                switch ($medium->type) {
                        case 'photo':
@@ -1388,7 +1388,7 @@ function twitter_media_entities($post, &$postarray)
 
 function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_existing_contact, $noquote)
 {
-       $postarray = array();
+       $postarray = [];
        $postarray['network'] = NETWORK_TWITTER;
        $postarray['gravity'] = 0;
        $postarray['uid'] = $uid;
@@ -1404,7 +1404,7 @@ function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_exis
 
        if (count($r)) {
                logger("Item with extid " . $postarray['uri'] . " found.", LOGGER_DEBUG);
-               return array();
+               return [];
        }
 
        $contactid = 0;
@@ -1453,7 +1453,7 @@ function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_exis
                                $postarray['owner-avatar'] = $r[0]["photo"];
                        } else {
                                logger("No self contact for user " . $uid, LOGGER_DEBUG);
-                               return array();
+                               return [];
                        }
                }
                // Don't create accounts of people who just comment something
@@ -1475,7 +1475,7 @@ function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_exis
                $contactid = $self['id'];
        } elseif ($contactid <= 0) {
                logger("Contact ID is zero or less than zero.", LOGGER_DEBUG);
-               return array();
+               return [];
        }
 
        $postarray['contact-id'] = $contactid;
@@ -1599,7 +1599,7 @@ function twitter_checknotification(App $a, $uid, $own_id, $top_item, $postarray)
 
                        $conv_parent = $conv['parent'];
 
-                       notification(array(
+                       notification([
                                'type' => NOTIFY_COMMENT,
                                'notify_flags' => $user[0]['notify-flags'],
                                'language' => $user[0]['language'],
@@ -1614,7 +1614,7 @@ function twitter_checknotification(App $a, $uid, $own_id, $top_item, $postarray)
                                'verb' => ACTIVITY_POST,
                                'otype' => 'item',
                                'parent' => $conv_parent,
-                       ));
+                       ]);
 
                        // only send one notification
                        break;
@@ -1626,10 +1626,10 @@ function twitter_fetchparentposts(App $a, $uid, $post, $connection, $self, $own_
 {
        logger("twitter_fetchparentposts: Fetching for user " . $uid . " and post " . $post->id_str, LOGGER_DEBUG);
 
-       $posts = array();
+       $posts = [];
 
        while ($post->in_reply_to_status_id_str != "") {
-               $parameters = array("trim_user" => false, "tweet_mode" => "extended", "id" => $post->in_reply_to_status_id_str);
+               $parameters = ["trim_user" => false, "tweet_mode" => "extended", "id" => $post->in_reply_to_status_id_str];
 
                $post = $connection->get('statuses/show', $parameters);
 
@@ -1725,7 +1725,7 @@ function twitter_fetchhometimeline(App $a, $uid)
                return;
        }
 
-       $parameters = array("exclude_replies" => false, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended");
+       $parameters = ["exclude_replies" => false, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended"];
        //$parameters["count"] = 200;
        // Fetching timeline
        $lastid = PConfig::get($uid, 'twitter', 'lasthometimelineid');
@@ -1857,7 +1857,7 @@ function twitter_fetchhometimeline(App $a, $uid)
 
                        if (($item != 0) && !function_exists("check_item_notification")) {
                                require_once 'include/enotify.php';
-                               notification(array(
+                               notification([
                                        'type'         => NOTIFY_TAGSELF,
                                        'notify_flags' => $u[0]['notify-flags'],
                                        'language'     => $u[0]['language'],
@@ -1872,7 +1872,7 @@ function twitter_fetchhometimeline(App $a, $uid)
                                        'verb'         => ACTIVITY_TAG,
                                        'otype'        => 'item',
                                        'parent'       => $parent_id
-                               ));
+                               ]);
                        }
                }
        }