Plugin Directory

Changeset 2576755


Ignore:
Timestamp:
08/02/2021 07:58:00 PM (5 years ago)
Author:
winrid
Message:

3.9 - Sync of comments from WordPress to FastComments when there are many comments improved.

Location:
fastcomments
Files:
34 added
6 edited

Legend:

Unmodified
Added
Removed
  • fastcomments/trunk/README.txt

    r2575553 r2576755  
    44Requires at least: 4.6
    55Tested up to: 5.8
    6 Stable tag: 3.8
     6Stable tag: 3.9
    77Requires PHP: 5.2.4
    88License: GPLv2 or later
     
    8585== Changelog ==
    8686
     87= 3.9 =
     88* Sync of comments from WordPress to FastComments when there are many comments improved.
     89
    8790= 3.8 =
    8891* Improvements for when upgrading from 2.1.
  • fastcomments/trunk/admin/fastcomments-admin-setup-view.js

    r2575412 r2576755  
    1313    }
    1414    function tick(cb) {
     15        var called = false;
     16        function next() {
     17            if (!called) {
     18                called = true;
     19                cb();
     20            }
     21        }
    1522        jQuery.ajax({
    1623            url: window.FC_DATA.siteUrl + '/index.php?rest_route=/fastcomments/v1/api/tick',
    1724            method: 'GET',
    1825            dataType: 'json',
    19             success: cb,
    20             error: cb,
     26            success: next,
     27            error: next,
    2128            xhrFields: {
    2229               withCredentials: true
     
    3542    }, function error(response) {
    3643        console.error('Could not fetch FastComments configuration', response);
     44        checkNext();
    3745    });
    3846
    3947    // Check for setup being complete every couple seconds and then reload the page when it is to show the new admin page with all the fancy options.
    4048    function checkNext() {
     49        var called = false;
    4150        function tickAndCheckNext() {
    42             tick(function() {
    43                 setTimeout(checkNext, 2000);
    44             });
     51            if (!called) {
     52                called = true;
     53                tick(function() {
     54                    setTimeout(checkNext, 2000);
     55                });
     56            }
    4557        }
    4658        getFCConfig(function success(response) {
  • fastcomments/trunk/admin/fastcomments-admin-setup-view.php

    r2575412 r2576755  
    1010        </ol>
    1111        <h2>Do you have a FastComments Account?</h2>
    12         <a class="button-primary"
     12        <a class="button-primary button-has-account"
    1313           href="https://fastcomments.com/auth/my-account/integrations/v1/setup?token=<?php echo get_option("fastcomments_token") ?>&hasAccount=true"
    1414           target="_blank">Yes</a>
    15         <a class="button-primary"
     15        <a class="button-primary button-no-account"
    1616           href="https://fastcomments.com/auth/my-account/integrations/v1/setup?token=<?php echo get_option("fastcomments_token") ?>&hasAccount=false"
    1717           target="_blank">No</a>
  • fastcomments/trunk/core/FastCommentsIntegrationCore.php

    r2575553 r2576755  
    113113
    114114    public function tick() {
     115        $this->log('debug', "BEGIN Tick");
    115116        $nextStateMachineName = 'integrationStateInitial';
    116117        while ($nextStateMachineName) {
     
    118119            $nextStateMachineName = call_user_func(array($this, $nextStateMachineName));
    119120        }
     121        $this->log('debug', "END Tick");
    120122    }
    121123
     
    171173                if ($response->status === 'success' && $response->commands) {
    172174                    foreach ($response->commands as $command) {
     175                        $this->log('debug', "Processing command $command->command");
    173176                        switch ($command->command) {
    174177                            case 'FetchEvents':
     
    218221        $startedAt = time();
    219222        $hasMore = true;
    220         $countSyncedSoFar = 0;
     223        $countSyncedSoFar = $this->getSettingValue('fastcomments_comment_sent_count') ? $this->getSettingValue('fastcomments_comment_sent_count') : 0;
    221224        $commentCount = $this->getCommentCount();
    222225        if ($commentCount == 0) {
     
    241244                $this->log('info', "Got comments to send count=[$count] hasMore=[$hasMore]");
    242245                if ($getCommentsResponse['comments'] && count($getCommentsResponse['comments']) > 0) {
    243                     $countRemaining = max($commentCount - count($getCommentsResponse['comments']) + $countSyncedSoFar, 0);
     246                    $countRemaining = max($commentCount - (count($getCommentsResponse['comments']) + $countSyncedSoFar), 0);
    244247                    $requestBody = json_encode(
    245248                        array(
     
    256259                        $this->setSettingValue('fastcomments_stream_last_send_timestamp', $fromDateTime);
    257260                        $countSyncedSoFar += count($getCommentsResponse['comments']);
    258                         if (!$hasMore) {
     261                        $this->setSettingValue('fastcomments_comment_sent_count', $countSyncedSoFar);
     262                        if (!$hasMore || $countRemaining === 0) {
    259263                            $this->setSetupDone();
    260264                            break;
  • fastcomments/trunk/core/FastCommentsWordPressIntegration.php

    r2575553 r2576755  
    2626        update_option('fc_fastcomments_comment_ids_version', '1.0');
    2727
    28         $timestamp = wp_next_scheduled('fastcomments_cron');
     28        global $FASTCOMMENTS_VERSION;
     29        $this->setSettingValue('fastcomments_version', $FASTCOMMENTS_VERSION);
     30
     31        $timestamp = wp_next_scheduled('fastcomments_cron_hook');
    2932        if (!$timestamp) {
    30             wp_schedule_event(time() + 86400, 'daily', 'fastcomments_cron');
     33            wp_schedule_event(time() + 86400, 'daily', 'fastcomments_cron_hook');
    3134        }
    3235    }
     
    3740
    3841    public function update() {
    39         $is_old_version = !get_option('fc_fastcomments_comment_ids_version');
    40         if ($is_old_version) {
    41             // force setup, but allow comment widget to load
    42             delete_option('fastcomments_setup');
    43             delete_option('fastcomments_token_validated');
    44         }
    45 
    46         $this->ensure_plugin_dependencies();
     42        global $FASTCOMMENTS_VERSION;
     43
     44        if ((string) $FASTCOMMENTS_VERSION !== (string) $this->getSettingValue('fastcomments_version')) {
     45            $is_old_version = !get_option('fc_fastcomments_comment_ids_version');
     46            if ($is_old_version) {
     47                // force setup, but allow comment widget to load
     48                delete_option('fastcomments_setup');
     49                delete_option('fastcomments_token_validated');
     50            }
     51
     52            $timestamp = wp_next_scheduled('fastcomments_cron_hook');
     53            wp_unschedule_event($timestamp, 'fastcomments_cron_hook');
     54            $this->ensure_plugin_dependencies();
     55            $this->setSettingValue('fastcomments_version', $FASTCOMMENTS_VERSION);
     56        }
    4757    }
    4858
     
    6373        delete_option('fastcomments_sso_key');
    6474        delete_option('fastcomments_sso_enabled');
     75        delete_option('fastcomments_cron');
     76        delete_option('fastcomments_version');
     77        delete_option('fastcomments_stream_last_fetch_timestamp');
     78        delete_option('fastcomments_stream_last_send_timestamp');
     79        delete_option('fastcomments_comment_sent_count');
    6580
    6681        $timestamp = wp_next_scheduled('fastcomments_cron');
     
    99114
    100115    private function addCommentIDMapEntry($fcId, $wpId) {
     116        $this->log('debug', "addCommentIDMapEntry $fcId -> $wpId");
    101117        global $wpdb;
    102118        $id_map_table_name = $wpdb->prefix . "fastcomments_comment_ids";
    103         $insert_result = $wpdb->insert(
    104             $id_map_table_name,
    105             array(
    106                 'id' => $fcId,
    107                 'wp_id' => $wpId,
    108             )
    109         );
    110         if ($insert_result === false) {
    111             $this->log('error', "Was not able to map $fcId to $wpId");
     119        $existing_wp_id = $this->getWPCommentId($fcId);
     120        if ($existing_wp_id !== null) {
     121            $insert_result = $wpdb->insert(
     122                $id_map_table_name,
     123                array(
     124                    'id' => $fcId,
     125                    'wp_id' => $wpId,
     126                )
     127            );
     128            if ($insert_result === false) {
     129                $this->log('error', "Was not able to map $fcId to $wpId");
     130            }
     131        } else {
     132            $this->log('debug', "Skipped mapping $fcId to $wpId - mapping already exists.");
    112133        }
    113134    }
     
    116137        global $wpdb;
    117138        $id_map_table_name = $wpdb->prefix . "fastcomments_comment_ids";
    118         $id_row = $wpdb->get_row(
    119             $id_map_table_name,
    120             array(
    121                 'id' => $fcId
    122             )
    123         );
    124         if ($id_row) {
     139        $this->log('debug', "getWPCommentId $fcId");
     140        $id_row = $wpdb->get_row("SELECT wp_id FROM $id_map_table_name WHERE id = \"$fcId\"");
     141        if ($id_row !== false) {
    125142            return $id_row->wp_id;
    126143        }
     
    207224
    208225        $wp_id = $this->getWPCommentId($fc_comment->_id);
    209         $wp_parent_id = $this->getWPCommentId($fc_comment->parentId);
     226        $wp_parent_id = $fc_comment->parentId ? $this->getWPCommentId($fc_comment->parentId) : null;
    210227
    211228        $wp_comment['comment_ID'] = is_numeric($wp_id) ? $wp_id : null;
     
    267284                switch ($eventData->type) {
    268285                    case 'new-comment':
     286                        $fcId = $eventData->comment->_id;
     287                        $this->log('debug', "Incoming comment $fcId");
    269288                        $comment_id_or_false = wp_insert_comment($this->fc_to_wp_comment($eventData->comment));
    270289                        if ($comment_id_or_false) {
     
    276295                        break;
    277296                    case 'updated-comment':
     297                        $fcId = $eventData->comment->_id;
     298                        $this->log('debug', "Updating comment $fcId");
    278299                        $wp_comment = $this->fc_to_wp_comment($eventData->comment);
    279                         $wp_id = $this->getWPCommentId($eventData->comment->_id);
     300                        $wp_id = $this->getWPCommentId($fcId);
    280301                        $wp_comment['comment_ID'] = $wp_id;
    281302                        wp_update_comment($wp_comment);
    282303                        break;
    283304                    case 'deleted-comment':
     305                        $this->log('debug', "Deleting comment $fcId");
    284306                        $wp_id = $this->getWPCommentId($eventData->comment->_id);
    285307                        if ($wp_id != null) {
     
    288310                        break;
    289311                    case 'new-vote':
    290                         $wp_id = $this->getWPCommentId($eventData->vote->commentId);
     312                        $fcId = $eventData->vote->commentId;
     313                        $this->log('debug', "New vote for comment $fcId");
     314                        $wp_id = $this->getWPCommentId($fcId);
    291315                        $wp_comment = get_comment($wp_id, ARRAY_A);
    292316                        if (!$wp_comment['comment_karma']) {
     
    300324                        break;
    301325                    case 'deleted-vote':
    302                         $wp_id = $this->getWPCommentId($eventData->vote->commentId);
     326                        $fcId = $eventData->vote->commentId;
     327                        $this->log('debug', "New vote for comment $fcId");
     328                        $wp_id = $this->getWPCommentId($fcId);
    303329                        $wp_comment = get_comment($wp_id, ARRAY_A);
    304330                        if ($wp_comment['comment_karma']) {
     
    329355                'after' => date('c', $startFromDateTime ? $startFromDateTime / 1000 : 0),
    330356                'inclusive' => true
    331             )
     357            ),
     358            'orderby' => 'comment_date',
     359            'order' => 'ASC'
    332360        );
    333361        $wp_comments = get_comments($args);
  • fastcomments/trunk/fastcomments-wordpress-plugin.php

    r2575553 r2576755  
    44Plugin URI: https://fastcomments.com
    55Description: Live Comments, Fast. A comment system that will delight your users and developers.
    6 Version: 3.8
     6Version: 3.9
    77Author: winrid @ FastComments
    88License: GPL-2.0+
     
    1414}
    1515
    16 $FASTCOMMENTS_VERSION = 3.8;
     16$FASTCOMMENTS_VERSION = 3.9;
    1717
    1818
     
    2121$fastcomments_public = new FastCommentsPublic();
    2222$fastcomments_public->setup_api_listeners(); // TODO able to do this without new()?
     23
    2324
    2425// Returns the FastComments embed comments template
     
    4041    require_once plugin_dir_path(__FILE__) . 'core/FastCommentsWordPressIntegration.php';
    4142    $fastcomments = new FastCommentsWordPressIntegration();
     43    $fastcomments->log('debug', 'Begin cron tick.');
    4244    $fastcomments->tick();
     45    $fastcomments->log('debug', 'End cron tick.');
    4346}
     47add_action('fastcomments_cron_hook', 'fastcomments_cron');
    4448
    4549function fastcomments_activate() {
Note: See TracChangeset for help on using the changeset viewer.