fediembedi.php 10.1 KB
Newer Older
MediaFormat's avatar
MediaFormat committed
1 2 3 4
<?php
/**
 * Plugin Name: FediEmbedi
 * Plugin URI: https://git.feneas.org/mediaformat/fediembedi
MediaFormat's avatar
MediaFormat committed
5
 * Github Plugin URI: https://github.com/mediaformat/fediembedi
MediaFormat's avatar
MediaFormat committed
6
 * Description: A widget to show your Mastodon profile timeline
7
 * Version: 0.7.2
MediaFormat's avatar
MediaFormat committed
8 9
 * Author: mediaformat
 * Author URI: https://mediaformat.org
MediaFormat's avatar
MediaFormat committed
10 11
 * License: GPLv3
 * License URI: https://www.gnu.org/licenses/gpl-3.0.en.html
MediaFormat's avatar
MediaFormat committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
 * Text Domain: fediembedi
 * Domain Path: /languages
 */
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'));
        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'));
        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'])) {
            $code = $_GET['code'];
            $client_id = get_option('fediembedi-client-id');
            $client_secret = get_option('fediembedi-client-secret');

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

                update_option('fediembedi-token', 'nada');

                $instance = get_option('fediembedi-instance');
54
                $client = new \FediClient($instance);
MediaFormat's avatar
MediaFormat committed
55
                $token = $client->get_bearer_token($client_id, $client_secret, $code, get_admin_url());
56
                //$instance_info = $client->getInstance();
MediaFormat's avatar
MediaFormat committed
57 58

                if (isset($token->error)) {
MediaFormat's avatar
MediaFormat committed
59
                    //TODO: Proper error message
MediaFormat's avatar
MediaFormat committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
                    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);
                    update_option('fediembedi-token', '');
                } else {
                    update_option('fediembedi-client-id', '');
                    update_option('fediembedi-client-secret', '');
                    update_option('fediembedi-token', $token->access_token);
76
                    //update_option('fediembedi-instance-type', $instance_type);
MediaFormat's avatar
MediaFormat committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

                }
                $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;
        }

        $token = get_option('fediembedi-token');

    }

    /*
     *  Widget
     */
    public function fediembedi_widget() {
    	include(plugin_dir_path(__FILE__) . 'fediembedi-widget.php' );//
    	register_widget( 'WP_Widget_fediembedi' );
    }

    public function enqueue_styles($hook)
    {
        if( is_active_widget( false, false, 'fediembedi') ) {
104
          $instance_type = get_option('fediembedi-instance-type');
MediaFormat's avatar
MediaFormat committed
105 106
          switch ($instance_type) {
    		      case 'Mastodon':
107
    		        wp_enqueue_style( 'mastodon', plugin_dir_url( __FILE__ ) . 'assets/mastodon.css', array(), filemtime(plugin_dir_path( __FILE__ ) . 'assets/mastodon.css') );
MediaFormat's avatar
MediaFormat committed
108 109
    		        break;
    		      case 'Pixelfed':
110
                wp_enqueue_style( 'bootstrap', plugin_dir_url( __FILE__ ) . 'assets/bootstrap/css/bootstrap.min.css', array(), '4.4.1' );
MediaFormat's avatar
MediaFormat committed
111
                wp_enqueue_style( 'pixelfed', plugin_dir_url( __FILE__ ) . 'assets/pixelfed.css', array(), filemtime(plugin_dir_path( __FILE__ ) . 'assets/pixelfed.css') );
MediaFormat's avatar
MediaFormat committed
112 113 114
                //https://css-tricks.com/lozad-js-performant-lazy-loading-images/ lazyloading for background images
    		        break;
    		      default:
115
    		        wp_enqueue_style( 'fediembedi', plugin_dir_url( __FILE__ ) . 'assets/mastodon.css', array(), filemtime(plugin_dir_path( __FILE__ ) . 'assets/mastodon.css') );
MediaFormat's avatar
MediaFormat committed
116 117
    		        break;
    		    }
MediaFormat's avatar
MediaFormat committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
        }
    }

    /**
     * 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');

        if (isset($_GET['disconnect'])) {
            update_option('fediembedi-token', '');
        }

        $token = get_option('fediembedi-token');

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

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

            if ($is_valid_nonce) {
                $instance = esc_url($_POST['instance']);
164
                $instance_type = esc_attr($_POST['instance_type']);
MediaFormat's avatar
MediaFormat committed
165
                //TODO switch($instance_type) case() return $scopes
MediaFormat's avatar
MediaFormat committed
166

167
                $client = new \FediClient($instance);
MediaFormat's avatar
MediaFormat committed
168
                $redirect_url = get_admin_url();
169 170 171 172 173 174 175 176 177 178 179 180

                switch ($instance_type) {
                    case 'Mastodon':
                      $auth_url = $client->register_app($redirect_url);
                      break;
                    case 'Pixelfed':
                      $auth_url = $client->register_app($redirect_url);
                      break;
                    case 'PeerTube':
                      $auth_url = $client->register_client($redirect_url, 'user');
                      break;
                }
MediaFormat's avatar
MediaFormat committed
181
                //$auth_url = $client->register_app($redirect_url, $scopes);
MediaFormat's avatar
MediaFormat committed
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

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

                } else {
                  //var_dump($auth_url); //die;
                    if (empty($instance)) {
                      //var_dump($instance); //die;
                        update_option(
                            'fediembedi-notice',
                            serialize(
                                array(
                                    'message' => '<strong>FediEmbedi</strong> : ' . __('Please set your instance before you connect !', 'fediembedi'),
                                    'class' => 'error',
                                )
                            )
                        );
                    } else {
                        update_option('fediembedi-client-id', $client->get_client_id());
                        update_option('fediembedi-client-secret', $client->get_client_secret());
                        update_option('fediembedi-instance', $instance);
212
                        update_option('fediembedi-instance-type', $instance_type);
MediaFormat's avatar
MediaFormat committed
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 239 240

                        $account = $client->verify_credentials($token);

                        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();
            }
        }

        $instance = get_option('fediembedi-instance');
241
        $instance_type = get_option('fediembedi-instance-type');
MediaFormat's avatar
MediaFormat committed
242 243

        if (!empty($token)) {
244
            $client = new \FediClient($instance);
MediaFormat's avatar
MediaFormat committed
245 246 247
            $account = $client->verify_credentials($token);
        }

MediaFormat's avatar
MediaFormat committed
248
        include 'fediembedi-settings-form.tpl.php';
MediaFormat's avatar
MediaFormat committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    }

    /**
     * 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>';
    	return $links;
    }

}

$fediconfig = new FediConfig();