Plugin Directory

Changeset 2738474


Ignore:
Timestamp:
06/07/2022 08:39:35 AM (4 years ago)
Author:
eventilla
Message:

Added support for batch processsing large amount of events. Also added a setting to require all "Allowed tags" to exist in an event to be included, default behaviour is check if one of them exists.

Location:
eventilla-events/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • eventilla-events/trunk/README.txt

    r2734284 r2738474  
    66Tested up to: 5.9.3
    77Requires PHP: 7.3
    8 Stable tag: 1.5.2
     8Stable tag: 1.6.1
    99License: GPLv2 or later
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    4646
    4747== Changelog ==
    48 = 1.5.2 =
    49 Updated Finnish translations.
     48= 1.6.1 =
     49Fixed event fetching for events that have no ending date. They are considered ongoing events for as long they don't have an end date.
     50= 1.6 =
     51Added support for batch processsing large amount of events. Also added a setting to require all "Allowed tags" to exist in an event to be included, default behaviour is check if one of them exists.
    5052= 1.5.1 =
    5153Fixed event update process that checked for allowed tags in events. Option name was incorrectly handled and tag check did not work properly.
  • eventilla-events/trunk/admin/class-eventilla-wp-admin.php

    r2711342 r2738474  
    224224        );
    225225        add_settings_field(
     226            $this->option_name . '_batchsize',
     227            __( 'Processing batch size', 'eventilla-wp' ),
     228            array( $this, $this->option_name . '_batchsize' ),
     229            $this->plugin_name,
     230            $this->option_name . '_general',
     231            array( 'label_for' => $this->option_name . '_batchsize' )
     232        );
     233        add_settings_field(
     234            $this->option_name . '_delay',
     235            __( 'Interval', 'eventilla-wp' ),
     236            array( $this, $this->option_name . '_delay' ),
     237            $this->plugin_name,
     238            $this->option_name . '_general',
     239            array( 'label_for' => $this->option_name . '_delay' )
     240        );
     241        add_settings_field(
    226242            $this->option_name . '_download_images',
    227243            __( 'Do not save event images', 'eventilla-wp' ),
     
    241257            __( 'Delete past events', 'eventilla-wp' ),
    242258            array( $this, $this->option_name . '_delete_past_events' ),
     259            $this->plugin_name,
     260            $this->option_name . '_general'
     261        );
     262        add_settings_field(
     263            $this->option_name . '_delete_all_events',
     264            __( 'Delete ALL events', 'eventilla-wp' ),
     265            array( $this, $this->option_name . '_delete_all_events' ),
    243266            $this->plugin_name,
    244267            $this->option_name . '_general'
     
    285308            $this->option_name . '_general',
    286309            array( 'label_for' => $this->option_name . '_allowed_tags' )
     310        );
     311        add_settings_field(
     312            $this->option_name . '_match_all',
     313            __( 'Require all tags', 'eventilla-wp' ),
     314            array( $this, $this->option_name . '_match_all' ),
     315            $this->plugin_name,
     316            $this->option_name . '_general'
    287317        );
    288318        add_settings_field(
     
    302332            array( 'label_for' => $this->option_name . '_current_lang' )
    303333        );
     334        register_setting( $this->plugin_name, $this->option_name . '_batchsize', array( $this, $this->option_name . '_sanitize_batchsize' ), false, 30 );
     335        register_setting( $this->plugin_name, $this->option_name . '_delay', array( $this, $this->option_name . '_sanitize_delay' ), false, 0);
    304336        register_setting( $this->plugin_name, $this->option_name . '_position', array( $this, $this->option_name . '_sanitize_position' ) );
    305337        register_setting( $this->plugin_name, $this->option_name . '_apikey', array( $this, $this->option_name . '_sanitize_apikey' ) );
     
    307339        register_setting( $this->plugin_name, $this->option_name . '_download_images');
    308340        register_setting( $this->plugin_name, $this->option_name . '_delete_past_events');
     341        register_setting( $this->plugin_name, $this->option_name . '_delete_all_events');
     342        register_setting( $this->plugin_name, $this->option_name . '_reset');
     343        register_setting( $this->plugin_name, $this->option_name . '_pause');
    309344        register_setting( $this->plugin_name, $this->option_name . '_dont_import_past_events');
     345        register_setting( $this->plugin_name, $this->option_name . '_match_all');
    310346        register_setting( $this->plugin_name, $this->option_name . $css_editor, array( $this, $this->option_name . '_sanitize_css_editor' ) );
    311347        register_setting( $this->plugin_name, $this->option_name . $template_editor . '_list', array( $this, $this->option_name . '_sanitize_template_editor' ) );
     
    368404     */
    369405    public function eventilla_opt_general_cb() {
    370         echo '<p>' . __( 'To use this plugin, you must have a valid API key and Account ID from Eventilla.com. Please change the settings accordingly.', 'eventilla-wp' ) . '</p>';
     406        $synced = get_option('eventilla_opt_last_sync', false);
     407        $is_restarting = (bool) get_option('eventilla_opt_reset', false);
     408        $is_paused = (bool) get_option('eventilla_opt_pause', false);
     409        $is_deleting = (bool) get_option('eventilla_opt_delete_all_events', false);
     410        $count = count(get_option('eventilla_opt_eventsdata', []));
     411        $queue = (int) get_option('eventilla_opt_queue', 0);
     412        $processed = $count - $queue;
     413        if(!$is_deleting) {
     414            echo '<p>' . __( 'To use this plugin, you must have a valid API key and Account ID from Eventilla.com. Please change the settings accordingly.', 'eventilla-wp' ) . '</p>';
     415            echo '<p>' . __( 'Events processed: ', 'eventilla-wp' ) . $processed . " / " . $count . "<br>";
     416            if($synced) {
     417                echo __( 'Last syncronization: ', 'eventilla-wp' ) . date('d.m.Y H:i:s', (int) $synced). "<br>";
     418            }
     419            else {
     420                echo __( 'Last syncronization: never', 'eventilla-wp' ). "<br>";
     421            }
     422            if($is_restarting) {
     423                echo __( 'Event processing is restarting in a moment.', 'eventilla-wp' ) .'<br>';
     424            }
     425            else {
     426                echo '<input type="checkbox" name="eventilla_opt_reset" value="1">' . __( 'Restart processing.', 'eventilla-wp' ) .'<br>';
     427            }
     428            if($is_paused) {
     429                echo __( 'Event processing is currently paused.', 'eventilla-wp' ) .'<br>';
     430                echo '<input type="checkbox" name="eventilla_opt_pause" value="">' . __( 'Continue processing.', 'eventilla-wp' ) .'<br>';
     431            }
     432            else {
     433                echo '<input type="checkbox" name="eventilla_opt_pause" value="1">' . __( 'Pause processing.', 'eventilla-wp' ) .'<br>';
     434            }
     435            echo '</p>';
     436        }
     437        else {
     438            echo "<p>" . __( 'Deleting all events...', 'eventilla-wp' ) . "</p>";
     439        }
     440       
    371441    }
    372442    /**
     
    621691        return implode(',', $allowedTags);
    622692    }
     693
     694    public function eventilla_opt_sanitize_batchsize( $batchsize ) {
     695        if(is_numeric($batchsize)) {
     696            return (int) $batchsize;
     697        }
     698        return 30;
     699    }
     700
     701    public function eventilla_opt_sanitize_delay( $delay ) {
     702        if(is_numeric($delay)) {
     703            return (int) $delay;
     704        }
     705        return 15;
     706    }
     707
    623708    /**
    624709     * The Logger option and logs viewer
     
    693778    }
    694779
    695 
     780    /**
     781     * Option to not to import past events
     782     *
     783     * @since 1.6
     784     */
     785    public function eventilla_opt_batchsize() {
     786        $batchsize = get_option( $this->option_name . '_batchsize', 50);
     787        echo '<input type="text" name="' . $this->option_name . '_batchsize' . '" id="' . $this->option_name . '_batchsize' . '" value="' . $batchsize . '"> ' . __( 'Events to process in a single cron job.', 'eventilla-wp' );
     788    }
     789    public function eventilla_opt_delay() {
     790        $delay = get_option( $this->option_name . '_delay', 15);
     791        echo '<input type="text" name="' . $this->option_name . '_delay' . '" id="' . $this->option_name . '_delay' . '" value="' . $delay . '"> ' . __( 'Minutes between syncronizations.', 'eventilla-wp' );
     792    }
     793    public function eventilla_opt_delete_all_events() {
     794        $is_checked = get_option($this->option_name . '_delete_all_events');
     795
     796        ?>
     797        <input type="checkbox" name="<?php echo $this->option_name . '_delete_all_events' ?>" value="1" <?php checked(1, $is_checked, true); ?> />
     798        <?php
     799        echo __( 'Start background process to delete all events from Wordpress.', 'eventilla-wp' );
     800    }
     801    public function eventilla_opt_match_all() {
     802        $is_checked = get_option($this->option_name . '_match_all');
     803
     804        ?>
     805        <input type="checkbox" name="<?php echo $this->option_name . '_match_all' ?>" value="1" <?php checked(1, $is_checked, true); ?> />
     806        <?php
     807            echo __( 'Every tag above must be tagged to event.', 'eventilla-wp' );
     808    }
     809   
    696810    /**
    697811     * Option to not to import past events
  • eventilla-events/trunk/eventilla-wp.php

    r2734284 r2738474  
    1616 * Plugin URI:        https://www.eventilla.com/
    1717 * Description:       Eventilla Events brings your event information from eventilla.com to WordPress as custom posts.
    18  * Version:           1.5.2
     18 * Version:           1.6.1
    1919 * Author:            Eventilla
    2020 * Author URI:        http://www.eventilla.com
  • eventilla-events/trunk/includes/class-eventilla-wp-api-request-v2.php

    r2711342 r2738474  
    187187     * @return Response of the POST request or false.
    188188     */
    189     public function post_curl( $action_type, $event_id = '', $post_body_array, $return_raw = false ) {
     189    public function post_curl( $action_type = '', $event_id = '', $post_body_array = false, $return_raw = false ) {
    190190        // Set the url to the API endpoint for request.
    191191        $endpoint_url = $this->set_enpoint_url( $this->api_url, $action_type, $event_id );
     
    260260     * @return Response of the POST request or false.
    261261     */
    262     public function post( $action_type, $event_id = '', $post_body_array, $return_raw = false ) {
     262    public function post( $action_type = '', $event_id = '', $post_body_array = false, $return_raw = false ) {
    263263        // Set the url to the API endpoint for request.
    264264        $endpoint_url = $this->set_enpoint_url( $this->api_url, $action_type, $event_id );
     
    368368            $endpoint_url .= "fields=id,name,organization,organization_id,starts,ends,short_description,timezone,title,url,tabs,datafields,description,forms,languages,location,logo,max_attendees,modified,registration_open,status,tags,template,tickets";
    369369        }
     370
     371        if(!get_option('eventilla_opt_match_all', false) && get_option('eventilla_opt_allowed_tags', false) && $event_id == '') {
     372            $endpoint_url .= "&tag=" . get_option('eventilla_opt_allowed_tags', '');
     373        }
    370374       
    371375        // Generate Authentication String as per required by Eventilla API.
     
    387391       //echo "Kutsu:\n" . $endpoint_url . "\n\nVastaus:\n";
    388392        $response = wp_remote_get( $endpoint_url, $args );
     393        //var_dump($response);
     394        //die();
    389395        $response_code = wp_remote_retrieve_response_code( $response );
    390396        $response_body = wp_remote_retrieve_body( $response );
    391         //var_dump($response_body);
    392         //die();
     397       //var_dump($response_body);
     398       // die();
    393399        //echo "\n\n";
    394400        $this->log_date('GET', $endpoint_url, $response_code, $response_body);
     
    745751        $eventilla_json = '';
    746752        $eventilla_common = new Eventilla_WP_Common();
    747 
     753        //echo "\n\nevents_to_posts\n";
     754        //var_dump($event_list_json);
    748755        // Get events from Eventilla if not empty.
    749756        if ( empty( $event_list_json ) ) {
     
    753760        }
    754761
    755         if (! is_string($event_list_json)) {
     762        if (! is_string($event_list_json) && ! is_array($event_list_json)) {
    756763            $message = 'Eventilla API v2 was empty in ' . __METHOD__ . ' on line:' .  __LINE__;
    757764            $eventilla_common->log_error('api_v2', $message);
     
    759766            return false;
    760767        }
    761         $eventilla_json_ob = json_decode($event_list_json);
    762         if (! empty($eventilla_json_ob->events)) {
    763             $eventilla_json_ob = $eventilla_json_ob->events;
     768
     769        if (!is_array($event_list_json)) {
     770            $eventilla_json_ob = json_decode($event_list_json);
     771            if (! empty($eventilla_json_ob->events)) {
     772                $eventilla_json_ob = $eventilla_json_ob->events;
     773            }
     774        }
     775        else {
     776            $eventilla_json_ob = $event_list_json;
    764777        }
    765778
     
    775788            }
    776789        }
    777 
     790        //var_dump($allowedTags);
    778791        /**
    779792         * Init vars for saving the data.
     
    864877            }
    865878            $event = $event->event;
    866 
    867 
    868             if(strtotime($event->ends) < strtotime('today') && get_option('eventilla_opt_dont_import_past_events', false)) {
     879            //var_dump($event);
     880
     881            if($event->ends && $event->ends != "" && strtotime($event->ends) != 0 && strtotime($event->ends) < strtotime('today') && get_option('eventilla_opt_dont_import_past_events', false)) {
    869882                continue;
    870883            }
    871             if(count($allowedTags) > 0) {
     884            $should_skip = false;
     885
     886            if(count($allowedTags) > 0 && get_option('eventilla_opt_match_all', false)) {
     887
    872888                if(!is_array($event->tags)) {
    873                     continue;
    874                 }
     889                    $event_tags = trim($event->tags);
     890                    $event_tags = explode(',', $event_tags);
     891                } else {
     892                    $event_tags = $event->tags;   
     893                }
     894                $should_skip = false;
     895                //var_dump($event_tags);
    875896                foreach($allowedTags as $allowedTag) {
    876                     if(!in_array($allowedTag,$event->tags)) {
    877                         continue;
     897                    if(!in_array($allowedTag, $event_tags)) {
     898                        $should_skip = true;
    878899                    }
    879900                }
    880901            }
     902
     903            if($should_skip) {
     904                echo "Skipataan";
     905                continue;
     906            }
     907
    881908            $is_modified = false;
    882909            $postid                         = 0; // This will be set to post_id when updating an event.
     
    12511278                ),
    12521279                'fields' => 'ids',
     1280                'posts_per_page' => -1,
     1281                'post_status' => 'any'
    12531282            );
    12541283            // perform the query
    12551284            $uid_query = new WP_Query( $args );
    1256 
     1285            //var_dump($uid_query);
    12571286            $uid_ids = $uid_query->posts;
    12581287            // If UID does not match any in the meta, we can continue
     
    12901319     */
    12911320    public function update_event() {
    1292         if ($this->api_credentials['account_id'] != false && $this->api_credentials['apikey'] != false) {
     1321        $delete_all_events = get_option('eventilla_opt_delete_all_events', false);
     1322        if($delete_all_events) {
     1323            update_option('eventilla_opt_abort', true, false);
     1324            update_option('eventilla_opt_queue', 0, false);
     1325            update_option('eventilla_opt_pause', true, false);
     1326            $this->delete_unsync_events(['fake-id']);
     1327            update_option('eventilla_opt_delete_all_events', false, false);
     1328            die("\nAll events deleted.\n");
     1329        }
     1330
     1331        $reset = get_option('eventilla_opt_reset', false);
     1332        if($reset) {
     1333            update_option('eventilla_opt_abort', true, false);
     1334            update_option('eventilla_opt_queue', 0, false);
     1335            update_option('eventilla_opt_reset', false, false);
     1336            update_option('eventilla_opt_isprocessing', false, false);
     1337            echo "\nQueue reseted.\n";
     1338        }     
     1339
     1340        $pause = get_option('eventilla_opt_pause', false);
     1341        if($pause) {
     1342            update_option('eventilla_opt_abort', true, false);
     1343            die("\nProcessing paused.\n");
     1344        }
     1345
     1346        $synced = get_option('eventilla_opt_last_sync', false);
     1347        if($synced) {
     1348            $interval = (int) get_option('eventilla_opt_delay', 0);
     1349            $next_time = $interval * 60 + (int) $synced;
     1350            if(time() < $next_time) {
     1351                die("\nSleeping until:" . date('Y-m-d H:i:s',$next_time)."\n");
     1352            }
     1353        }
     1354
     1355        $queue = (int) get_option('eventilla_opt_queue', 0);
     1356        if ($queue == 0 && $this->api_credentials['account_id'] != false && $this->api_credentials['apikey'] != false) {
     1357            echo "\nFetching eventlist from Eventilla.\n";
    12931358            $response = $this->get('events', '', false, false, true);
    1294             //var_dump($response);
    1295             $this->events_to_posts($response);
     1359           
     1360            $eventilla_json_ob = json_decode($response);
     1361            if (! empty($eventilla_json_ob->events)) {
     1362                $eventilla_json_ob = $eventilla_json_ob->events;
     1363            }
     1364            //var_dump($eventilla_json_ob);
     1365            //die();
     1366            update_option('eventilla_opt_queue', count($eventilla_json_ob), false);
     1367            update_option('eventilla_opt_eventsdata', (array) $eventilla_json_ob, false);
     1368            echo count($eventilla_json_ob) . " events added to process queue.\n";
     1369            $this->process_event_batch();
     1370        } else {
     1371            $this->process_event_batch();
     1372        }
     1373    }
     1374
     1375    public function process_event_batch() {
     1376        $is_processing = (bool) get_option('eventilla_opt_isprocessing', false);
     1377        if(!$is_processing) {
     1378            update_option('eventilla_opt_isprocessing', true, false);
     1379            $eventsdata = get_option('eventilla_opt_eventsdata', []);
     1380            $batchsize = (int) get_option('eventilla_opt_batchsize', 50);
     1381            $queue = (int) get_option('eventilla_opt_queue', 0);
     1382            //echo "\n\n" .  $queue . "\n\n";
     1383            $do_removal = false;
     1384            if($queue <= $batchsize) {
     1385                $batchsize = $queue;
     1386                $do_removal = true;
     1387            }
     1388            $processed = count($eventsdata) - $queue;
     1389            $batch = array_slice($eventsdata, $processed, $batchsize);
     1390            echo "\nProsessing batch of ".$batchsize." events. There are " .  $queue . " events left in queue.\n";
     1391            foreach($batch as $event) {
     1392                $abort = get_option('eventilla_opt_abort', false);
     1393                if($abort) {
     1394                    update_option('eventilla_opt_isprocessing', false, false);
     1395                    update_option('eventilla_opt_abort', false, false);
     1396                    die("\nProcess aborted because of option change at admin panel!");
     1397                }
     1398                echo "Processing event: " . $event->id . "\n";
     1399                $event_from_api = $this->get('events', $event->id);
     1400                //var_dump($event);
     1401                //var_dump($event_from_api);
     1402                $this->events_to_posts($event_from_api, false, true);
     1403                $queue--;
     1404                update_option('eventilla_opt_queue', $queue, false);
     1405                if($do_removal) {
     1406                    $eventilla_ids = [];
     1407                    foreach($eventsdata as $event) {
     1408                        array_push($eventilla_ids, $event->id);
     1409                    }
     1410                    //var_dump($eventilla_ids);
     1411                    $this->delete_unsync_events( $eventilla_ids );
     1412                    update_option('eventilla_opt_last_sync', time(), false);
     1413                }
     1414            }
     1415            update_option('eventilla_opt_isprocessing', false, false);
     1416        } else {
     1417            echo "\nSkipping cron because previous one is still running.\n\n";
    12961418        }
    12971419    }
  • eventilla-events/trunk/includes/class-eventilla-wp-cron.php

    r2734050 r2738474  
    1414    public static function set_cron() {
    1515        if ( ! wp_next_scheduled( 'eventilla_wp_update_from_eventilla' ) ) {
    16             wp_schedule_event(time(),  'every_five_min','eventilla_wp_update_from_eventilla');
     16            wp_schedule_event(time(),  'every_min','eventilla_wp_update_from_eventilla');
    1717        }
    1818
     
    3636                        'value'   => strtotime('today'),
    3737                        'compare' => '<',
     38                    ],
     39                    [
     40                        'key'     => 'eventilla_end_unix',
     41                        'value'   => 0,
     42                        'compare' => '!=',
    3843                    ]
    3944                ]
     
    4550        }
    4651
    47         if(get_option('eventilla_opt_allowed_tags', false)) {
     52        if(get_option('eventilla_opt_allowed_tags', false) && get_option('eventilla_opt_match_all', false)) {
    4853            $allowedTags = trim(get_option('eventilla_opt_allowed_tags'));
    4954
     
    6570
    6671            foreach($localEvents as $localEvent) {
     72                $event_tags = trim($localEvent->tags);
     73                $event_tags = explode(',', $event_tags);
    6774                foreach($allowedTags as $allowedTag) {
    68                     if(!in_array($allowedTag,$localEvent->tags)) {
     75                    if(!in_array($allowedTag, $event_tags)) {
    6976                        wp_delete_post($localEvent->ID, true);
    7077                    }
     
    8996                'display' => __( 'Every 5 mins', 'eventilla_wp' ),
    9097        );
     98        $schedules['every_min'] = array(
     99            'interval' => 60,
     100            'display' => __( 'Every min', 'eventilla_wp' ),
     101    );
    91102        return $schedules;
    92103    }
  • eventilla-events/trunk/includes/class-eventilla-wp-form-type.php

    r2542377 r2738474  
    1919     * @return string HTML of the form field
    2020     */
    21     public function get_text_input( $index = 0, $field ) {
     21    public function get_text_input( $index = 0, $field = '' ) {
    2222        return $this->get_input( $index, $field, 'text' );
    2323    }
     
    2929     * @return string HTML of the form field
    3030     */
    31     public function get_email_input( $index = 0, $field ) {
     31    public function get_email_input( $index = 0, $field = '' ) {
    3232        return $this->get_input( $index, $field, 'email' );
    3333    }
     
    3939     * @return string HTML of the form field
    4040     */
    41     public function get_number( $index = 0, $field ) {
     41    public function get_number( $index = 0, $field = '' ) {
    4242        $choices = '';
    4343
     
    8282     * @return string HTML of the form field
    8383     */
    84     private function get_input( $index = 0, $field, $input_type = 'text' ) {
     84    private function get_input( $index = 0, $field = '', $input_type = 'text' ) {
    8585        $required = $field->required == true ? '*' : '';
    8686        $required_attribute = $field->required == true ? ' required="required"' : '';
     
    107107     * @return string html
    108108     */
    109     public function get_title( $index = 0, $field ) {
     109    public function get_title( $index = 0, $field = '' ) {
    110110        $options = '';
    111111
     
    140140     * @return string html
    141141     */
    142     public function get_telephone( $index = 0, $field ) {
     142    public function get_telephone( $index = 0, $field = '') {
    143143        $filename = dirname( __FILE__ ) . '/../data/country_codes.json';
    144144        $phone_codes_filedata = file_get_contents( $filename );
     
    187187     * @return string html
    188188     */
    189     public function get_checkbox( $index = 0, $field ) {
     189    public function get_checkbox( $index = 0, $field = '' ) {
    190190        $options = '';
    191191
     
    230230     * @return string html
    231231     */
    232     public function get_select( $index = 0, $field ) {
     232    public function get_select( $index = 0, $field = '' ) {
    233233        $options = '';
    234234
     
    269269     * @return string html
    270270     */
    271     public function get_radio( $index = 0, $field ) {
     271    public function get_radio( $index = 0, $field = '') {
    272272        $options = '';
    273273
     
    312312     * @return string html
    313313     */
    314     public function get_textarea( $index = 0, $field ) {
     314    public function get_textarea( $index = 0, $field = '') {
    315315        $html = '<div id="field_div_0_' . $field->id . '" class="field">
    316316            <label>' . $field->label . ' </label>
Note: See TracChangeset for help on using the changeset viewer.