Plugin Directory

Changeset 2613148


Ignore:
Timestamp:
10/12/2021 04:28:16 PM (4 years ago)
Author:
winrid
Message:

3.9.10 - When using SSO, Admins and Moderators now have the appropriate tags shown on with comments. Additionally, configuration has been added for the FastComments log level.

Location:
fastcomments
Files:
40 added
10 edited

Legend:

Unmodified
Added
Removed
  • fastcomments/trunk/README.txt

    r2588650 r2613148  
    44Requires at least: 4.6
    55Tested up to: 5.8
    6 Stable tag: 3.9.9
     6Stable tag: 3.9.10
    77Requires PHP: 5.2.4
    88License: GPLv2 or later
     
    8484
    8585== Changelog ==
     86
     87= 3.9.10 =
     88* When using SSO, Admins and Moderators now have the appropriate tags shown on with comments. Additionally, configuration has been added for the FastComments log level.
    8689
    8790= 3.9.9 =
  • fastcomments/trunk/admin/fastcomments-admin-sso-view.js

    r2577451 r2613148  
    11(function () {
     2    function addNoticeDismissalEventListeners() {
     3        const noticeDismissButtons = document.querySelectorAll('.notice-dismiss');
     4
     5        noticeDismissButtons.forEach(function (dismissNoticeButton) {
     6            dismissNoticeButton.addEventListener('click', function () {
     7                dismissNoticeButton.parentNode.classList.add('hidden');
     8            });
     9        });
     10    }
     11
    212    (function disableSSOFlow() {
    313        const disableButton = document.getElementById('fc-sso-disable');
     
    616        const noticeSSODisabledSuccess = document.getElementById('sso-disabled-success');
    717        const noticeSSODisabledFailure = document.getElementById('sso-disabled-failure');
     18
     19        addNoticeDismissalEventListeners();
    820
    921        if (disableButton) {
     
    8092        const noticeSSOEnabledFailure = document.getElementById('sso-enabled-failure');
    8193
     94        addNoticeDismissalEventListeners();
     95
    8296        if (enableButton) {
    8397            jQuery('#dialog-enable-sso').dialog({
  • fastcomments/trunk/admin/fastcomments-admin-sso-view.php

    r2577451 r2613148  
    1717        <?php if (get_option('users_can_register')) { ?>
    1818            <div class="notice notice-success is-dismissible hidden" id="sso-disabled-success">
    19                 <p><strong>SSO Disabled! <a href="<?php echo get_admin_url(null, "?page=fastcomments&sub_page=sso", null) ?>">Refresh</a>.</strong></p>
     19                <p><strong>SSO Disabled! <a href="<?php echo get_admin_url(null, "admin.php?page=fastcomments&sub_page=sso", null) ?>">Refresh</a>.</strong></p>
    2020                <button type="button" class="notice-dismiss">
    2121                    <span class="screen-reader-text">Dismiss this notice.</span>
     
    5151    <?php } else { ?>
    5252        <div class="notice notice-success is-dismissible hidden" id="sso-enabled-success">
    53             <p><strong>SSO Enabled! <a href="<?php echo get_admin_url(null, "?page=fastcomments&sub_page=sso", null) ?>">Refresh</a>.</strong></p>
     53            <p><strong>SSO Enabled! <a href="<?php echo get_admin_url(null, "admin.php?page=fastcomments&sub_page=sso", null) ?>">Refresh</a>.</strong></p>
    5454            <button type="button" class="notice-dismiss">
    5555                <span class="screen-reader-text">Dismiss this notice.</span>
  • fastcomments/trunk/admin/fastcomments-admin-view.php

    r2576755 r2613148  
    3333            <div>SSO Settings</div>
    3434        </a>
     35        <a href="<?php echo admin_url('admin.php?page=fastcomments&sub_page=advanced-settings'); ?>">
     36            <img src="<?php echo plugin_dir_url( dirname( __FILE__ ) ); ?>/admin/images/settings.png" alt="Advanced Settings" title="Advanced Settings"/>
     37            <div>Advanced Settings</div>
     38        </a>
    3539    </div>
    3640</div>
  • fastcomments/trunk/admin/fastcomments-admin.php

    r2577451 r2613148  
    138138                require_once plugin_dir_path(__FILE__) . 'fastcomments-admin-sso-view.php';
    139139                break;
     140            case 'advanced-settings':
     141                require_once plugin_dir_path(__FILE__) . 'fastcomments-admin-advanced-settings-view.php';
     142                break;
    140143            default:
    141144                require_once plugin_dir_path(__FILE__) . 'fastcomments-admin-view.php';
  • fastcomments/trunk/core/FastCommentsIntegrationCore.php

    r2588650 r2613148  
    168168            $lastFetchDate = $this->getSettingValue('fastcomments_stream_last_fetch_timestamp');
    169169            $lastFetchDateToSend = $lastFetchDate ? $lastFetchDate : 0;
     170            $this->log('debug', "Polling next commands for fromDateTime=[$lastFetchDateToSend].");
    170171            $rawIntegrationStreamResponse = $this->makeHTTPRequest('GET', "$this->baseUrl/commands?token=$token&fromDateTime=$lastFetchDateToSend", null);
    171172            $this->log('debug', 'Stream response status: ' . $rawIntegrationStreamResponse->responseStatusCode);
     
    193194
    194195    public function commandFetchEvents($token) {
     196        $this->log('debug', "BEGIN commandFetchEvents");
    195197        $fromDateTime = $this->getSettingValue('fastcomments_stream_last_fetch_timestamp');
    196198        $hasMore = true;
    197199        $startedAt = time();
    198         while ($hasMore && time() - $startedAt < 30 * 1000) {
    199             $this->log('debug', 'Send events command loop...');
     200        while ($hasMore && time() - $startedAt < 30) {
    200201            $fromDateTimeToSend = $fromDateTime ? $fromDateTime : 0;
     202            $this->log('debug', "Send events command loop... Fetching events fromDateTime=[$fromDateTimeToSend]");
    201203            $rawIntegrationEventsResponse = $this->makeHTTPRequest('GET', "$this->baseUrl/events?token=$token&fromDateTime=$fromDateTimeToSend", null);
    202204            $response = json_decode($rawIntegrationEventsResponse->responseBody);
    203205            if ($response->status === 'success') {
    204206                $count = count($response->events);
    205                 $this->log('info', "Got events count=[$count]");
     207                $this->log('info', "Got events count=[$count] hasMore=[$response->hasMore]");
    206208                if ($response->events && count($response->events) > 0) {
    207209                    $this->handleEvents($response->events);
    208                     $fromDateTime = strtotime($response->events[count($response->events) - 1]->createdAt);
     210                    $fromDateTime = strtotime($response->events[count($response->events) - 1]->createdAt) * 1000;
    209211                }
    210                 $hasMore = $response->hasMore;
     212                $hasMore = !!$response->hasMore;
    211213                $this->setSettingValue('fastcomments_stream_last_fetch_timestamp', $fromDateTime);
    212214            } else {
     
    215217            }
    216218        }
     219        $this->log('debug', "END commandFetchEvents");
    217220    }
    218221
     
    294297                                $response = json_decode($httpResponse->responseBody);
    295298                                if ($response->status === 'success') {
     299                                    foreach ($response->commentIds as $wpId => $fcId) {
     300                                        update_comment_meta((int) $wpId, 'fastcomments_id', $fcId);
     301                                    }
    296302                                    $countRemaining = $countRemainingIfSuccessful;
    297303                                    $fromDateTime = $lastCommentFromDateTime;
  • fastcomments/trunk/core/FastCommentsWordPressIntegration.php

    r2588650 r2613148  
    1111    private function ensure_plugin_dependencies() {
    1212        global $wpdb;
     13        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    1314
    1415        $charset_collate = $wpdb->get_charset_collate();
     
    2223        ) $charset_collate;";
    2324
    24         require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    2525        dbDelta($create_id_map_table_sql);
    26         update_option('fc_fastcomments_comment_ids_version', '1.0');
     26
     27        $event_ids_table_name = $wpdb->prefix . "fastcomments_event_ids"; // TODO AUTO REMOVE OLD EVENT MARKERS
     28
     29        $create_event_ids_table_sql = "CREATE TABLE $event_ids_table_name (
     30          id varchar(100) NOT NULL,
     31          PRIMARY KEY  (id)
     32        ) $charset_collate;";
     33
     34        dbDelta($create_event_ids_table_sql);
     35
     36        update_option('fc_fastcomments_comment_ids_version', '2.0');
    2737
    2838        global $FASTCOMMENTS_VERSION;
     
    3242        if (!$timestamp) {
    3343            wp_schedule_event(time() + 86400, 'daily', 'fastcomments_cron_hook');
     44        }
     45
     46        if (!get_option('fastcomments_log_level')) {
     47            update_option('fastcomments_log_level', 'warn');
    3448        }
    3549    }
     
    5872
    5973    public function deactivate() {
    60         global $wpdb;
    61 
    62         $id_map_table_name = $wpdb->prefix . "fastcomments_comment_ids";
    63 
    64         $wpdb->query("DROP TABLE IF EXISTS $id_map_table_name");
    65 
    6674        delete_option('fc_fastcomments_comment_ids_version');
    6775        delete_option('fastcomments_token');
     
    7684        delete_option('fastcomments_stream_last_send_id');
    7785        delete_option('fastcomments_comment_sent_count');
     86        delete_option('fastcomments_log_level');
    7887
    7988        $timestamp = wp_next_scheduled('fastcomments_cron');
     
    8190    }
    8291
     92    // note - if the user uninstalls and re-installs - duplicate data my occur as we cleanup the tables to prevent duplicates.
     93    public function uninstall() {
     94        global $wpdb;
     95
     96        $id_map_table_name = $wpdb->prefix . "fastcomments_comment_ids";
     97
     98        $was_drop_successful = $wpdb->query("DROP TABLE IF EXISTS $id_map_table_name");
     99
     100        if (!$was_drop_successful) {
     101            $this->log('warn', "Dropping of $id_map_table_name was not successful!");
     102        }
     103
     104        $event_ids_table_name = $wpdb->prefix . "fastcomments_event_ids";
     105
     106        $was_drop_successful = $wpdb->query("DROP TABLE IF EXISTS $event_ids_table_name");
     107
     108        if (!$was_drop_successful) {
     109            $this->log('warn', "Dropping of $event_ids_table_name was not successful!");
     110        }
     111
     112        // TODO cleanup comment meta items where key = fastcomments_id etc (there is an index on key)
     113    }
     114
     115    private static $logLevels = array(
     116        'debug' => 0, // log everything
     117        'info' => 1, // log info, warnings, and errors
     118        'warn' => 2, // only log warnings, errors
     119        'error' => 3, // only log errors
     120        'disabled' => 10 // don't log
     121    );
     122
    83123    public function log($level, $message) {
    84         switch ($level) {
    85             case 'debug':
    86                 error_log("DEBUG:::" . $message);
    87                 break;
    88             case 'info':
    89                 error_log("INFO:::" . $message);
    90                 break;
    91             case 'error':
    92                 error_log("ERROR:::" . $message);
    93                 break;
     124        $lowestLogLevel = $this->getSettingValue('fastcomments_log_level');
     125        // handle no log level defined for backwards compat.
     126        if (!$lowestLogLevel || FastCommentsWordPressIntegration::$logLevels[$lowestLogLevel] <= FastCommentsWordPressIntegration::$logLevels[$level]) {
     127            switch ($level) {
     128                case 'debug':
     129                    error_log("DEBUG:::" . $message);
     130                    break;
     131                case 'info':
     132                    error_log("INFO:::" . $message);
     133                    break;
     134                case 'warn':
     135                    error_log("WARN:::" . $message);
     136                    break;
     137                case 'error':
     138                    error_log("ERROR:::" . $message);
     139                    break;
     140            }
    94141        }
    95142    }
     
    109156    public function setSettingValue($settingName, $settingValue) {
    110157        update_option($settingName, $settingValue);
     158    }
     159
     160    private function setEventHandled($eventId) {
     161        global $wpdb;
     162        $event_ids_table_name = $wpdb->prefix . "fastcomments_event_ids";
     163        $insert_result = $wpdb->insert(
     164            $event_ids_table_name,
     165            array(
     166                'id' => $eventId,
     167            )
     168        );
     169        if ($insert_result === false) {
     170            $this->log('error', "Was not able to mark event $eventId as handled.");
     171        }
     172    }
     173
     174    private function isEventHandled($eventId) {
     175        global $wpdb;
     176        $event_ids_table_name = $wpdb->prefix . "fastcomments_event_ids";
     177        $id_row = $wpdb->get_row("SELECT id FROM $event_ids_table_name WHERE id = \"$eventId\"");
     178        if ($id_row) {
     179            return true;
     180        }
     181        return false;
    111182    }
    112183
     
    127198                $this->log('error', "Was not able to map $fcId to $wpId");
    128199            }
     200        } else if ($existing_wp_id === $wpId) {
     201            $this->log('debug', "Skipped mapping $fcId to $wpId - mapping already exists.");
    129202        } else {
    130             $this->log('debug', "Skipped mapping $fcId to $wpId - mapping already exists.");
    131         }
     203            $this->log('warn', "Skipped mapping $fcId to $wpId - ALREADY MAPPED TO $existing_wp_id!");
     204        }
     205    }
     206
     207    private function removeCommentIDMapEntry($fcId, $wpId) {
     208        global $wpdb;
     209        $id_map_table_name = $wpdb->prefix . "fastcomments_comment_ids";
     210        $id_row = $wpdb->get_row("DELETE FROM $id_map_table_name WHERE id = \"$fcId\" AND wp_id = $wpId LIMIT 1");
     211        if ($id_row) {
     212            return $id_row->wp_id;
     213        }
     214        return null;
    132215    }
    133216
     
    135218        global $wpdb;
    136219        $id_map_table_name = $wpdb->prefix . "fastcomments_comment_ids";
    137         $this->log('debug', "getWPCommentId $fcId");
    138220        $id_row = $wpdb->get_row("SELECT wp_id FROM $id_map_table_name WHERE id = \"$fcId\"");
    139221        if ($id_row) {
     
    292374        foreach ($events as $event) {
    293375            try {
     376                $this->log('debug', "BEGIN handleEvents EVENT $event->_id");
     377
     378                if ($this->isEventHandled($event->_id)) {
     379                    $this->log('debug', "END handleEvents EVENT $event->_id SUCCESS - ALREADY HANDLED.");
     380                    continue;
     381                }
     382
    294383                /** @type {FastCommentsEventStreamItemData} * */
    295384                $eventData = json_decode($event->data);
     
    302391                        $wp_id = $this->getWPCommentId($fcId);
    303392                        $existingComment = isset($wp_id) ? get_comment($wp_id) : null;
    304                         if (!$existingComment) {
    305                             $this->log('debug', "Incoming comment $fcId");
    306                             $new_wp_comment = $this->fc_to_wp_comment($eventData->comment);
    307                             if ($new_wp_comment) {
    308                                 $comment_id_or_false = wp_insert_comment($new_wp_comment);
    309                                 if ($comment_id_or_false) {
    310                                     $this->addCommentIDMapEntry($fcId, $comment_id_or_false);
    311                                     add_comment_meta($comment_id_or_false, 'fastcomments_id', $eventData->comment->_id, true);
    312                                 } else {
    313                                     $debug_data = $event->data;
    314                                     $this->log('error', "Failed to save comment: $debug_data");
    315                                 }
     393                        $this->log('debug', "Incoming comment $fcId");
     394                        if ($existingComment) {
     395                            $this->log('debug', "Incoming comment $fcId will overwrite existing $wp_id");
     396                            wp_delete_comment($wp_id, true);
     397                        }
     398                        if ($wp_id) {
     399                            $this->removeCommentIDMapEntry($fcId, $wp_id);
     400                            $this->log('debug', "Removing stale $fcId -> $wp_id mapping.");
     401                        }
     402
     403                        $new_wp_comment = $this->fc_to_wp_comment($eventData->comment);
     404                        if ($new_wp_comment) {
     405                            $comment_id_or_false = wp_insert_comment($new_wp_comment);
     406                            if ($comment_id_or_false) {
     407                                $this->addCommentIDMapEntry($fcId, $comment_id_or_false);
     408                                update_comment_meta($comment_id_or_false, 'fastcomments_id', $eventData->comment->_id);
     409                                $this->log('debug', "Saved $fcId (wp id $comment_id_or_false)");
    316410                            } else {
    317                                 $this->log('debug', "Skipping sync of $fcId - is not from the WP integration.");
     411                                $debug_data = $event->data;
     412                                $this->log('error', "Failed to save comment: $debug_data");
    318413                            }
    319414                        } else {
    320                             $this->log('debug', "Incoming comment $fcId ignored, already maps to comment $wp_id");
     415                            $this->log('debug', "Skipping sync of $fcId - is not from the WP integration.");
    321416                        }
    322417                        break;
     
    327422                        if ($wp_comment) {
    328423                            wp_update_comment($wp_comment);
    329                             add_comment_meta($wp_comment['comment_ID'], 'fastcomments_id', $eventData->comment->_id, true);
     424                            update_comment_meta($wp_comment['comment_ID'], 'fastcomments_id', $eventData->comment->_id, true);
    330425                        } else {
    331426                            $this->log('debug', "Skipping sync of $fcId - is not from the WP integration.");
     
    333428                        break;
    334429                    case 'deleted-comment':
     430                        $fcId = $eventData->comment->_id;
    335431                        $this->log('debug', "Deleting comment $fcId");
    336                         $wp_id = $this->getWPCommentId($eventData->comment->_id);
     432                        $wp_id = $this->getWPCommentId($fcId);
    337433                        if (is_numeric($wp_id)) {
    338                             wp_trash_comment($wp_id);
     434                            $was_successful = wp_trash_comment($wp_id);
     435                            if (!$was_successful) {
     436                                $this->log('debug', "Attempted to delete $fcId ($wp_id) - but it was not successful!");
     437                            } else {
     438                                $this->removeCommentIDMapEntry($fcId, $wp_id);
     439                            }
     440                        } else {
     441                            $this->log('debug', "Attempted to delete $fcId - but it was not found.");
    339442                        }
    340443                        break;
     
    367470                        break;
    368471                }
     472                $this->setEventHandled($event->_id);
     473                $this->log('debug', "END handleEvents EVENT $event->_id SUCCESS");
    369474            } catch (Exception $e) {
     475                $this->log('debug', "END handleEvents EVENT $event->_id FAILURE");
    370476                $this->log('error', $e->getMessage());
    371477            }
     
    376482    private function getCommentQueryWhere($afterId) {
    377483        // This query ensures a stable sort for pagination and allows us to paginate while not seeing the same comment twice.
    378         return "comment_ID > $afterId";
     484        return "comment_ID > $afterId AND comment_approved != \"trash\"";
    379485    }
    380486
  • fastcomments/trunk/fastcomments-wordpress-plugin.php

    r2588650 r2613148  
    44Plugin URI: https://fastcomments.com
    55Description: Live Comments, Fast. A comment system that will delight your users and developers.
    6 Version: 3.9.9
     6Version: 3.9.10
    77Author: winrid @ FastComments
    88License: GPL-2.0+
     
    1414}
    1515
    16 $FASTCOMMENTS_VERSION = 3.99;
     16$FASTCOMMENTS_VERSION = 3.910;
    1717
    1818require_once plugin_dir_path(__FILE__) . 'admin/fastcomments-admin.php';
  • fastcomments/trunk/public/fastcomments-public.php

    r2588650 r2613148  
    5454        require_once plugin_dir_path(__FILE__) . '../core/FastCommentsWordPressIntegration.php';
    5555        $fastcomments = new FastCommentsWordPressIntegration();
    56         if ($should_set_enabled) {
     56        if ($should_set_enabled === true || $should_set_enabled === 'true') {
    5757            $fastcomments->enableSSO();
    5858        } else {
     
    8888        $result['timestamp'] = $timestamp;
    8989
     90        $is_admin = current_user_can('administrator');
     91        $is_moderator = current_user_can('moderate_comments');
     92
    9093        $sso_user = array();
    9194        if ($wp_user) {
     
    9598            $sso_user['avatar'] = get_avatar_url($wp_user->ID, 95);
    9699            $sso_user['optedInNotifications'] = true;
    97             // TODO pass isAdmin/isModerator flags
     100            $sso_user['isAdmin'] = $is_admin;
     101            $sso_user['isModerator'] = $is_moderator;
    98102        }
    99103
  • fastcomments/trunk/uninstall.php

    r2576755 r2613148  
    1515delete_option( 'fastcomments_sso_enabled' );
    1616delete_option( 'fastcomments_setup' );
     17delete_option( 'fastcomments_log_level' );
    1718
    1819$fastcomments = new FastCommentsWordPressIntegration();
Note: See TracChangeset for help on using the changeset viewer.