fediembedi.php 18.3 KB
Newer Older
MediaFormat's avatar
MediaFormat committed
1 2 3 4
<?php
/**
 * Plugin Name: FediEmbedi
 * Plugin URI: https://git.feneas.org/mediaformat/fediembedi
5
 * Description: Widgets and shortcodes to show your Fediverse profile timeline
MediaFormat's avatar
MediaFormat committed
6
 * Version: 0.10.1
MediaFormat's avatar
MediaFormat committed
7 8
 * Author: mediaformat
 * Author URI: https://mediaformat.org
MediaFormat's avatar
MediaFormat committed
9 10
 * License: GPLv3
 * License URI: https://www.gnu.org/licenses/gpl-3.0.en.html
MediaFormat's avatar
MediaFormat committed
11 12
 * Text Domain: fediembedi
 * Domain Path: /languages
MediaFormat's avatar
MediaFormat committed
13
 * Github Plugin URI: https://github.com/mediaformat/fediembedi
MediaFormat's avatar
MediaFormat committed
14 15 16 17 18 19 20 21 22 23
 */
namespace FediEmbedi;
require_once 'fediembedi-client.php';

class FediConfig
{
    public function __construct()
    {
        add_action('plugins_loaded', array($this, 'init'));
        add_action('widgets_init', array($this, 'fediembedi_widget'));
24 25 26
        add_shortcode('mastodon', array($this, 'mastodon_shortcode'));
        add_shortcode('pixelfed', array($this, 'pixelfed_shortcode'));
        add_shortcode('peertube', array($this, 'peertube_shortcode'));
27
        add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
MediaFormat's avatar
MediaFormat committed
28 29 30
        add_action('wp_enqueue_scripts', array($this, 'enqueue_styles'));
        add_action('admin_menu', array($this, 'configuration_page'));
        add_action('admin_notices', array($this, 'admin_notices'));
MediaFormat's avatar
MediaFormat committed
31
        add_filter('fedi_emoji', array($this, 'convert_emoji'), 10, 2);
MediaFormat's avatar
MediaFormat committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
        add_filter('plugin_action_links_'.plugin_basename(__FILE__), array($this, 'fediembedi_add_plugin_page_settings_link'));

    }

    /**
     * Init
     *
     * Plugin initialization
     *
     * @return void
     */
    public function init()
    {
        $plugin_dir = basename(dirname(__FILE__));
        //load_plugin_textdomain('fediembedi', false, $plugin_dir . '/languages');

        if (isset($_GET['code'])) {
49 50 51
        //if (isset($_GET['code']) && isset($GET['instance_type'])) {

            $instance_type = $_REQUEST['instance_type'];
MediaFormat's avatar
MediaFormat committed
52
            $code = $_GET['code'];
53 54 55 56 57 58 59 60 61 62 63

            switch ($instance_type) {
              case 'mastodon':
                $client_id = get_option('fediembedi-mastodon-client-id');
                $client_secret = get_option('fediembedi-mastodon-client-secret');
                break;
              case 'pixelfed':
                $client_id = get_option('fediembedi-pixelfed-client-id');
                $client_secret = get_option('fediembedi-pixelfed-client-secret');
                break;
            }
MediaFormat's avatar
MediaFormat committed
64 65 66 67

            if (!empty($code) && !empty($client_id) && !empty($client_secret)) {
                //echo __('Authentification, please wait', 'fediembedi') . '...';

68 69 70 71 72 73 74 75 76 77 78 79 80 81
                switch ($instance_type) {
                  case 'mastodon':
                    update_option('fediembedi-mastodon-token', 'nada');
                    $instance = get_option('fediembedi-mastodon-instance');
                    $client = new \FediClient($instance);
                    $token = $client->get_bearer_token($client_id, $client_secret, $code, get_admin_url() . '?instance_type=' . $instance_type);
                    break;
                  case 'pixelfed':
                    update_option('fediembedi-pixelfed-token', 'nada');
                    $instance = get_option('fediembedi-pixelfed-instance');
                    $client = new \FediClient($instance);
                    $token = $client->get_bearer_token($client_id, $client_secret, $code, get_admin_url() . '?instance_type=' . $instance_type);
                    break;
                }
MediaFormat's avatar
MediaFormat committed
82 83

                if (isset($token->error)) {
MediaFormat's avatar
MediaFormat committed
84
                    //TODO: Proper error message
MediaFormat's avatar
MediaFormat committed
85 86 87 88 89 90 91 92 93 94 95
                    update_option(
                        'fediembedi-notice',
                        serialize(
                            array(
                                'message' => '<strong>FediEmbedi</strong> : ' . __("Can't log you in.", 'fediembedi') .
                                '<p><strong>' . __('Instance message', 'fediembedi') . '</strong> : ' . $token->error_description . '</p>',
                                'class' => 'error',
                            )
                        )
                    );
                    unset($token);
96 97 98 99 100 101 102 103
                    switch ($instance_type) {
                      case 'mastodon':
                        update_option('fediembedi-mastodon-token', '');
                        break;
                      case 'pixelfed':
                        update_option('fediembedi-pixelfed-token', '');
                        break;
                    }
MediaFormat's avatar
MediaFormat committed
104
                } else {
105 106 107 108 109 110 111 112 113 114 115 116
                    switch ($instance_type) {
                      case 'mastodon':
                        update_option('fediembedi-mastodon-client-id', '');
                        update_option('fediembedi-mastodon-client-secret', '');
                        update_option('fediembedi-mastodon-token', $token->access_token);
                        break;
                      case 'pixelfed':
                        update_option('fediembedi-pixelfed-client-id', '');
                        update_option('fediembedi-pixelfed-client-secret', '');
                        update_option('fediembedi-pixelfed-token', $token->access_token);
                        break;
                    }
MediaFormat's avatar
MediaFormat committed
117 118 119 120 121 122 123 124 125 126 127 128

                }
                $redirect_url = get_admin_url() . 'options-general.php?page=fediembedi';
            } else {
                //Probably hack or bad refresh, redirect to homepage
                $redirect_url = home_url();
            }

            wp_redirect($redirect_url);
            exit;
        }

129 130
        $mastodon_token = get_option('fediembedi-mastodon-token');
        $pixelfed_token = get_option('fediembedi-pixelfed-token');
MediaFormat's avatar
MediaFormat committed
131 132 133 134 135 136
    }

    /*
     *  Widget
     */
    public function fediembedi_widget() {
137
      //Mastodon
138
      include(plugin_dir_path(__FILE__) . 'fediembedi-mastodon-widget.php' );
139
      register_widget( 'FediEmbedi_Mastodon' );
140 141 142
      if(empty(get_option('fediembedi-mastodon-token'))){
        unregister_widget( 'FediEmbedi_Mastodon' );
      }
143 144

      //Pixelfed
145
      include(plugin_dir_path(__FILE__) . 'fediembedi-pixelfed-widget.php' );
146
      register_widget( 'FediEmbedi_Pixelfed' );
147 148 149
      if(empty(get_option('fediembedi-pixelfed-token'))){
        unregister_widget( 'FediEmbedi_Pixelfed' );
      }
150 151

      //PeerTube
152
      include(plugin_dir_path(__FILE__) . 'fediembedi-peertube-widget.php' );
153
    	register_widget( 'FediEmbedi_PeerTube' );
MediaFormat's avatar
MediaFormat committed
154 155
    }

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    public function mastodon_shortcode($atts){
      //fedi instance
  		$fedi_instance = get_option('fediembedi-mastodon-instance');
  		$access_token = get_option('fediembedi-mastodon-token');
  		$client = new \FediClient($fedi_instance, $access_token);
  		$cred = $client->verify_credentials($access_token);

      $atts = shortcode_atts( array(
        'only_media' => false,
        'pinned' => false,
        'exclude_replies' => false,
        'max_id' => null,
        'since_id' => null,
        'min_id' => null,
        'limit' => 5,
        'exclude_reblogs' => false,
        'show_header' => true,
      ), $atts, 'mastodon' );

      //getStatus from remote instance
      $status = $client->getStatus($atts['only_media'], $atts['pinned'], $atts['exclude_replies'], null, null, null, $atts['limit'], $atts['exclude_reblogs']);
177
      //if(WP_DEBUG_DISPLAY === true): echo '<details><summary>Mastodon</summary><pre>'; var_dump($client->getStatus($atts['only_media'], $atts['pinned'], $atts['exclude_replies'], null, null, null, $atts['limit'], $atts['exclude_reblogs'])); echo '</pre></details>'; endif;
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
      $show_header = $atts['show_header'];
      $account = $status[0]->account;
      include(plugin_dir_path(__FILE__) . 'templates/mastodon.tpl.php' );
    }

    public function pixelfed_shortcode($atts){
      //fedi instance
      $fedi_instance = get_option('fediembedi-pixelfed-instance');
      $access_token = get_option('fediembedi-pixelfed-token');
      $client = new \FediClient($fedi_instance, $access_token);
      $cred = $client->verify_credentials($access_token);

      $atts = shortcode_atts( array(
        'only_media' => false,
        'pinned' => false,
        'exclude_replies' => false,
        'max_id' => null,
        'since_id' => null,
        'min_id' => null,
        'limit' => 9,
        'exclude_reblogs' => false,
        'show_header' => true,
      ), $atts, 'pixelfed' );

      //getStatus from remote instance
      $status = $client->getStatus($atts['only_media'], $atts['pinned'], $atts['exclude_replies'], null, null, null, $atts['limit'], $atts['exclude_reblogs']);
      //if(WP_DEBUG_DISPLAY === true): echo '<details><summary>Mastodon</summary><pre>'; var_dump($client->getStatus($atts)); echo '</pre></details>'; endif;
      $show_header = $atts['show_header'];
      $account = $status[0]->account;
      include(plugin_dir_path(__FILE__) . 'templates/pixelfed.tpl.php' );
    }

    public function peertube_shortcode($atts){

      $atts = shortcode_atts( array(
        'instance' => null,
        'actor' => null,
        'is_channel' => null,
        'limit' => 9,
        'show_header' => true,
      ), $atts, 'peertube' );

      $client = new \FediClient($atts['instance']);

      //getVideos from remote instance
      $status = $client->getVideos($atts['actor'], $atts['is_channel']);
      if(!is_null($atts['is_channel'])){
        $account = $status->data[0]->channel;
      } else {
        $account = $status->data[0]->account;
      }

      //if(WP_DEBUG_DISPLAY === true): echo '<details><summary>PeerTube</summary><pre>'; var_dump($status); echo '</pre></details>'; endif;
      $show_header = $atts['show_header'];
      include(plugin_dir_path(__FILE__) . 'templates/peertube.tpl.php' );

    }

    /*
     * convert_emoji
     */
MediaFormat's avatar
MediaFormat committed
239 240 241 242 243 244 245 246 247 248 249
    public function convert_emoji($string, $emojis){
      if(is_null($emojis) || !is_array($emojis)){
        return $string;
      }
      foreach($emojis as $emoji){
           $match = '/:' . $emoji->shortcode .':/';
           $string = preg_replace($match, "<img draggable=\"false\" role=\"img\" class=\"emoji\" src=\"{$emoji->static_url}\">", $string);
      }
      return $string;
    }

MediaFormat's avatar
MediaFormat committed
250 251
    public function enqueue_styles($hook)
    {
252 253
        global $post;
        if( is_active_widget( false, false, 'mastodon') || is_active_widget( false, false, 'pixelfed') || ( is_a( $post, 'WP_Post' ) && ( has_shortcode( $post->post_content, 'mastodon') || has_shortcode( $post->post_content, 'pixelfed') ) ) ) {
254 255 256
            wp_enqueue_script( 'resize-sensor', plugin_dir_url( __FILE__ ) . 'assets/ResizeSensor.js', array(), 'css-element-queries-1.2.2' );
            wp_enqueue_script( 'element-queries', plugin_dir_url( __FILE__ ) . 'assets/ElementQueries.js', array('resize-sensor'), 'css-element-queries-1.2.2' );
        }
257
        if( is_active_widget( false, false, 'mastodon')  || ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'mastodon') ) ) {
258 259
            wp_enqueue_style( 'mastodon', plugin_dir_url( __FILE__ ) . 'assets/mastodon.css', array(), filemtime(plugin_dir_path( __FILE__ ) . 'assets/mastodon.css') );
        }
260
        if( is_active_widget( false, false, 'pixelfed')  || ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'pixelfed') )  ) {
261 262 263
            wp_enqueue_style( 'bootstrap', plugin_dir_url( __FILE__ ) . 'assets/bootstrap/css/bootstrap.min.css', array(), '4.4.1' );
            wp_enqueue_style( 'pixelfed', plugin_dir_url( __FILE__ ) . 'assets/pixelfed.css', array(), filemtime(plugin_dir_path( __FILE__ ) . 'assets/pixelfed.css') );
        }
264
        if( is_active_widget( false, false, 'peertube')  || ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'peertube') ) ) {
265
            wp_enqueue_style( 'peertube', plugin_dir_url( __FILE__ ) . 'assets/peertube.css', array(), filemtime(plugin_dir_path( __FILE__ ) . 'assets/mastodon.css') );
MediaFormat's avatar
MediaFormat committed
266 267 268
        }
    }

269 270 271 272 273 274 275 276 277 278 279 280
    public function enqueue_scripts($hook)
    {
      global $pagenow;
      $infos = get_plugin_data(__FILE__);
      if ($pagenow == "options-general.php") {
          //We might be on settings page <-- Do you know a bette solution to get if we are in our own settings page?
          $plugin_url = plugin_dir_url(__FILE__);
          //wp_enqueue_script('settings_page', $plugin_url . 'assets/settings_page.js', array('jquery'), $infos['Version'], true);

      }
    }

MediaFormat's avatar
MediaFormat committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    /**
     * Configuration_page
     *
     * Add the configuration page menu
     *
     * @return void
     */
    public function configuration_page()
    {
        add_options_page(
            'FediEmbedi',
            'FediEmbedi',
            'manage_options',
            'fediembedi',
            array($this, 'show_configuration_page')
        );
    }

    /**
     * Show_configuration_page
     *
     * Content of the configuration page
     *
     * @throws Exception The exception.
     * @return void
     */
    public function show_configuration_page()
    {

        wp_enqueue_style('fediembedi-configuration', plugin_dir_url(__FILE__) . 'style.css');

312 313 314 315 316 317 318 319 320
        if (isset($_GET['fediembedi-disconnect'])) {
          switch ($_GET['fediembedi-disconnect']) {
            case 'mastodon':
              update_option('fediembedi-mastodon-token', '');
              break;
            case 'pixelfed':
              update_option('fediembedi-pixelfed-token', '');
              break;
          }
MediaFormat's avatar
MediaFormat committed
321

322 323 324 325 326
        }
        $mastodon_instance = null;
        $mastodon_token = get_option('fediembedi-mastodon-token');
        $pixelfed_instance = null;
        $pixelfed_token = get_option('fediembedi-pixelfed-token');
MediaFormat's avatar
MediaFormat committed
327 328 329 330 331 332

        if (isset($_POST['save'])) {

            $is_valid_nonce = wp_verify_nonce($_POST['_wpnonce'], 'fediembedi-configuration');

            if ($is_valid_nonce) {
333

MediaFormat's avatar
MediaFormat committed
334
                $instance = esc_url($_POST['instance']);
335
                $instance_type = esc_attr($_POST['instance_type']);
MediaFormat's avatar
MediaFormat committed
336

337
                $client = new \FediClient($instance);
338 339
                $redirect_url = get_admin_url() . '?instance_type=' . $instance_type;
                $auth_url = $client->register_app($redirect_url);
MediaFormat's avatar
MediaFormat committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

                if ($auth_url == "ERROR") {
                    update_option(
                        'fediembedi-notice',
                        serialize(
                            array(
                                'message' => '<strong>FediEmbedi</strong> : ' . __('The given instance url belongs to an unrecognized service!', 'fediembedi'),
                                'class' => 'error',
                            )
                        )
                    );

                } else {
                    if (empty($instance)) {
                        update_option(
                            'fediembedi-notice',
                            serialize(
                                array(
                                    'message' => '<strong>FediEmbedi</strong> : ' . __('Please set your instance before you connect !', 'fediembedi'),
                                    'class' => 'error',
                                )
                            )
                        );
                    } else {

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
                        switch ($instance_type) {
                          case 'mastodon':
                            update_option('fediembedi-mastodon-client-id', $client->get_client_id());
                            update_option('fediembedi-mastodon-client-secret', $client->get_client_secret());
                            update_option('fediembedi-mastodon-instance', $instance);
                            $mastodon_account = $client->verify_credentials($mastodon_token);
                            $account = $mastodon_account;
                            break;
                          case 'pixelfed':
                            update_option('fediembedi-pixelfed-client-id', $client->get_client_id());
                            update_option('fediembedi-pixelfed-client-secret', $client->get_client_secret());
                            update_option('fediembedi-pixelfed-instance', $instance);
                            $pixelfed_account = $client->verify_credentials($pixelfed_token);
                            $account = $pixelfed_account;
                            break;
                        }

                        //$account = $client->verify_credentials($token);
MediaFormat's avatar
MediaFormat committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

                        if (is_null($account) || isset($account->error)) {
                            echo '<meta http-equiv="refresh" content="0; url=' . $auth_url . '" />';
                            echo __('Redirect to ', 'fediembedi') . $instance;
                            exit;
                        }

                        //Inform user that save was successfull
                        update_option(
                            'fediembedi-notice',
                            serialize(
                                array(
                                    'message' => '<strong>FediEmbedi</strong> : ' . __('Configuration successfully saved!', 'fediembedi'),
                                    'class' => 'success',
                                )
                            )
                        );

                    }
                }

                $this->admin_notices();
            }
        }

408 409 410 411 412 413 414 415 416
        if (!empty($mastodon_token)) {
            $mastodon_instance = get_option('fediembedi-mastodon-instance');
            $client = new \FediClient($mastodon_instance);
            $mastodon_account = $client->verify_credentials($mastodon_token);
        }
        if (!empty($pixelfed_token)) {
            $pixelfed_instance = get_option('fediembedi-pixelfed-instance');
            $client = new \FediClient($pixelfed_instance);
            $pixelfed_account = $client->verify_credentials($pixelfed_token);
MediaFormat's avatar
MediaFormat committed
417 418
        }

MediaFormat's avatar
MediaFormat committed
419
        include 'fediembedi-settings-form.tpl.php';
MediaFormat's avatar
MediaFormat committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
    }

    /**
     * Admin_notices
     * Show the notice (error or info)
     *
     * @return void
     */
    public function admin_notices()
    {

        $notice = unserialize(get_option('fediembedi-notice'));

        if (is_array($notice)) {
            echo '<div class="notice notice-' . sanitize_html_class($notice['class']) . ' is-dismissible"><p>' . $notice['message'] . '</p></div>';
            update_option('fediembedi-notice', null);
        }
    }

    /**
     * @param $links
     *
     * @return array
     */
    function fediembedi_add_plugin_page_settings_link( $links ) {
      $links[] = '<a href="' . admin_url( 'options-general.php?page=fediembedi' ) . '">' . __('Configuration', 'fediembedi') . '</a>';
MediaFormat's avatar
MediaFormat committed
446
      $links[] = '<a href="https://git.feneas.org/mediaformat/fediembedi/issues" target="_blank">' . __('Support', 'fediembedi') . '</a>';
MediaFormat's avatar
MediaFormat committed
447 448 449 450 451 452
    	return $links;
    }

}

$fediconfig = new FediConfig();