abstracts
2 years ago
admin
2 years ago
elementor
4 years ago
export
3 years ago
fields
2 years ago
interfaces
8 years ago
libraries
2 years ago
log-handlers
4 years ago
shortcodes
2 years ago
stats
3 years ago
templates
5 years ago
class-everest-forms.php
2 years ago
class-evf-ajax.php
2 years ago
class-evf-autoloader.php
7 years ago
class-evf-background-updater.php
7 years ago
class-evf-cache-helper.php
6 years ago
class-evf-cron.php
3 years ago
class-evf-deprecated-action-hooks.php
6 years ago
class-evf-deprecated-filter-hooks.php
5 years ago
class-evf-emails.php
2 years ago
class-evf-fields.php
2 years ago
class-evf-form-block.php
4 years ago
class-evf-form-handler.php
3 years ago
class-evf-form-task.php
2 years ago
class-evf-forms-features.php
2 years ago
class-evf-frontend-scripts.php
2 years ago
class-evf-install.php
2 years ago
class-evf-integrations.php
7 years ago
class-evf-log-levels.php
8 years ago
class-evf-logger.php
5 years ago
class-evf-post-types.php
5 years ago
class-evf-privacy.php
6 years ago
class-evf-session-handler.php
7 years ago
class-evf-shortcodes.php
4 years ago
class-evf-smart-tags.php
2 years ago
class-evf-template-loader.php
2 years ago
class-evf-validation.php
6 years ago
evf-conditional-functions.php
6 years ago
evf-core-functions.php
2 years ago
evf-deprecated-functions.php
6 years ago
evf-entry-functions.php
3 years ago
evf-formatting-functions.php
4 years ago
evf-notice-functions.php
4 years ago
evf-template-functions.php
4 years ago
evf-template-hooks.php
7 years ago
evf-update-functions.php
5 years ago
class-evf-background-updater.php
136 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Background Updater |
| 4 | * |
| 5 | * @package EverestForms\Classes |
| 6 | * @since 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | if ( ! class_exists( 'EVF_Background_Process', false ) ) { |
| 12 | include_once dirname( __FILE__ ) . '/abstracts/class-evf-background-process.php'; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * EVF_Background_Updater Class. |
| 17 | */ |
| 18 | class EVF_Background_Updater extends EVF_Background_Process { |
| 19 | |
| 20 | /** |
| 21 | * Initiate new background process. |
| 22 | */ |
| 23 | public function __construct() { |
| 24 | // Uses unique prefix per blog so each blog has separate queue. |
| 25 | $this->prefix = 'wp_' . get_current_blog_id(); |
| 26 | $this->action = 'evf_updater'; |
| 27 | |
| 28 | parent::__construct(); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Dispatch updater. |
| 33 | * |
| 34 | * Updater will still run via cron job if this fails for any reason. |
| 35 | */ |
| 36 | public function dispatch() { |
| 37 | $dispatched = parent::dispatch(); |
| 38 | $logger = evf_get_logger(); |
| 39 | |
| 40 | if ( is_wp_error( $dispatched ) ) { |
| 41 | $logger->error( |
| 42 | sprintf( 'Unable to dispatch EverestFormsupdater: %s', $dispatched->get_error_message() ), |
| 43 | array( 'source' => 'evf_db_updates' ) |
| 44 | ); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Handle cron healthcheck |
| 50 | * |
| 51 | * Restart the background process if not already running |
| 52 | * and data exists in the queue. |
| 53 | */ |
| 54 | public function handle_cron_healthcheck() { |
| 55 | if ( $this->is_process_running() ) { |
| 56 | // Background process already running. |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | if ( $this->is_queue_empty() ) { |
| 61 | // No data to process. |
| 62 | $this->clear_scheduled_event(); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | $this->handle(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Schedule fallback event. |
| 71 | */ |
| 72 | protected function schedule_event() { |
| 73 | if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { |
| 74 | wp_schedule_event( time() + 10, $this->cron_interval_identifier, $this->cron_hook_identifier ); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Is the updater running? |
| 80 | * |
| 81 | * @return boolean |
| 82 | */ |
| 83 | public function is_updating() { |
| 84 | return false === $this->is_queue_empty(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Task |
| 89 | * |
| 90 | * Override this method to perform any actions required on each |
| 91 | * queue item. Return the modified item for further processing |
| 92 | * in the next pass through. Or, return false to remove the |
| 93 | * item from the queue. |
| 94 | * |
| 95 | * @param string $callback Update callback function. |
| 96 | * @return string|bool |
| 97 | */ |
| 98 | protected function task( $callback ) { |
| 99 | evf_maybe_define_constant( 'EVF_UPDATING', true ); |
| 100 | |
| 101 | $logger = evf_get_logger(); |
| 102 | |
| 103 | include_once dirname( __FILE__ ) . '/evf-update-functions.php'; |
| 104 | |
| 105 | $result = false; |
| 106 | |
| 107 | if ( is_callable( $callback ) ) { |
| 108 | $logger->info( sprintf( 'Running %s callback', $callback ), array( 'source' => 'evf_db_updates' ) ); |
| 109 | $result = (bool) call_user_func( $callback ); |
| 110 | |
| 111 | if ( $result ) { |
| 112 | $logger->info( sprintf( '%s callback needs to run again', $callback ), array( 'source' => 'evf_db_updates' ) ); |
| 113 | } else { |
| 114 | $logger->info( sprintf( 'Finished running %s callback', $callback ), array( 'source' => 'evf_db_updates' ) ); |
| 115 | } |
| 116 | } else { |
| 117 | $logger->notice( sprintf( 'Could not find %s callback', $callback ), array( 'source' => 'evf_db_updates' ) ); |
| 118 | } |
| 119 | |
| 120 | return $result ? $callback : false; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Complete |
| 125 | * |
| 126 | * Override if applicable, but ensure that the below actions are |
| 127 | * performed, or, call parent::complete(). |
| 128 | */ |
| 129 | protected function complete() { |
| 130 | $logger = evf_get_logger(); |
| 131 | $logger->info( 'Data update complete', array( 'source' => 'evf_db_updates' ) ); |
| 132 | EVF_Install::update_db_version(); |
| 133 | parent::complete(); |
| 134 | } |
| 135 | } |
| 136 |