diff --git a/includes/admin/abstract/class-rop-model-abstract.php b/includes/admin/abstract/class-rop-model-abstract.php index ea9a47180..f3a8238da 100644 --- a/includes/admin/abstract/class-rop-model-abstract.php +++ b/includes/admin/abstract/class-rop-model-abstract.php @@ -92,7 +92,7 @@ protected function set( $key, $value = '', $refresh = false ) { $this->data[ $key ] = apply_filters( 'rop_set_key_' . $key, $value ); - return update_option( $this->namespace, $this->data ); + return update_option( $this->namespace, $this->data, false ); } diff --git a/includes/admin/class-rop-logger.php b/includes/admin/class-rop-logger.php index 209fc69de..63f40c4c4 100644 --- a/includes/admin/class-rop-logger.php +++ b/includes/admin/class-rop-logger.php @@ -29,6 +29,14 @@ class Rop_Logger { */ private $stream; + /** + * Holds the log namespace. + * + * @access private + * @var string $log_namespace Defaults 'rop_logs'. + */ + private $log_namespace = 'rop_logs'; + /** * Rop_Logger constructor. * Instantiate the Logger with specified formatter and stream. @@ -38,7 +46,7 @@ class Rop_Logger { */ public function __construct() { - $this->stream = new Rop_Log_Handler( 'rop_logs' ); + $this->stream = new Rop_Log_Handler( $this->log_namespace ); } @@ -230,4 +238,23 @@ public function is_status_error_necessary( $logs = array() ) { } + /** + * Utility method to cleanup authenticated services. + * + * @access public + * + * @param int $cleanup_log_count Cleanup the log count. + * @return array + */ + public function cleanup_logs( $cleanup_log_count = 1000 ) { + $logs = $this->stream->get_logs(); + + if ( count( $logs ) > $cleanup_log_count ) { + $logs = array_slice( $logs, 0, $cleanup_log_count ); + + update_option( $this->log_namespace, array_reverse( $logs ), false ); + } + + return $logs; + } } diff --git a/includes/admin/class-rop-rest-api.php b/includes/admin/class-rop-rest-api.php index c90649aa5..85dac17c2 100644 --- a/includes/admin/class-rop-rest-api.php +++ b/includes/admin/class-rop-rest-api.php @@ -1642,6 +1642,20 @@ private function add_account_bluesky( $data ) { return $this->response->to_array(); } + /** + * API method called to cleanup logs. + * + * @access private + * @return array + */ + private function cleanup_logs() { + $model = new Rop_Logger(); + $this->response->set_code( '200' ) + ->set_data( $model->cleanup_logs() ); + + return $this->response->to_array(); + } + /** * Share API method. * diff --git a/includes/admin/models/class-rop-services-model.php b/includes/admin/models/class-rop-services-model.php index 0bb8a3f7f..2c589557a 100644 --- a/includes/admin/models/class-rop-services-model.php +++ b/includes/admin/models/class-rop-services-model.php @@ -37,7 +37,6 @@ class Rop_Services_Model extends Rop_Model_Abstract { */ private $accounts_namespace = 'active_accounts'; - /** * Utility method to clear authenticated services. * @@ -519,5 +518,4 @@ public function find_account( $account_id ) { return false; } - } diff --git a/includes/class-rop-i18n.php b/includes/class-rop-i18n.php index 338b1f06d..be8ac60b2 100644 --- a/includes/class-rop-i18n.php +++ b/includes/class-rop-i18n.php @@ -359,6 +359,12 @@ public static function get_labels( $key = '' ) { 'clear_btn' => __( 'Clear logs', 'tweet-old-post' ), 'no_logs' => __( 'No recent logs!', 'tweet-old-post' ), 'export_btn' => __( 'Export logs', 'tweet-old-post' ), + 'cleanup' => array( + 'cta' => __( 'Cleanup logs', 'tweet-old-post' ), + 'title' => __( 'Cleanup logs', 'tweet-old-post' ), + 'description' => __( 'This will remove older logs and keep only the most recent 1,000 logs. This helps improve performance and reduce stored data.', 'tweet-old-post' ), + 'btn' => __( 'Cleanup now', 'tweet-old-post' ), + ), ), 'general' => array( 'plugin_name' => __( 'Revive Social', 'tweet-old-post' ), diff --git a/vue/src/models/rop_store.js b/vue/src/models/rop_store.js index a82262670..1d7b47486 100644 --- a/vue/src/models/rop_store.js +++ b/vue/src/models/rop_store.js @@ -270,6 +270,10 @@ export default new Vuex.Store({ case 'toggle_tracking': break + case 'cleanup_logs': + state.page.logs = stateData; + state.cron_status.logs_number = stateData.length || 0; + break; default: Vue.$log.error('No state request for ', requestName); } diff --git a/vue/src/vue-elements/accounts-tab-panel.vue b/vue/src/vue-elements/accounts-tab-panel.vue index ad2cb37be..858351947 100644 --- a/vue/src/vue-elements/accounts-tab-panel.vue +++ b/vue/src/vue-elements/accounts-tab-panel.vue @@ -142,7 +142,7 @@ labels: this.$store.state.labels.accounts, upsell_link: ropApiSettings.upsell_link, pro_installed: ropApiSettings.pro_installed, - postTimeout: '' + postTimeout: '', } }, computed: { @@ -189,7 +189,7 @@ }, hasActiveAccountsLimitation: function () { return !this.pro_installed && this.accountsCount >= 2 && this.checkLicense ; - } + }, }, mounted: function () { if (0 === this.is_preloading) { diff --git a/vue/src/vue-elements/logs-tab-panel.vue b/vue/src/vue-elements/logs-tab-panel.vue index 2f8cd1de9..665cc8a1f 100644 --- a/vue/src/vue-elements/logs-tab-panel.vue +++ b/vue/src/vue-elements/logs-tab-panel.vue @@ -29,6 +29,21 @@ /> {{ labels.clear_btn }} +
@@ -70,6 +85,34 @@
+ @@ -86,6 +129,7 @@ is_loading: false, labels: this.$store.state.labels.logs, upsell_link: ropApiSettings.upsell_link, + cleanupModal: false, } }, computed: { @@ -95,6 +139,11 @@ logs_no: function () { return this.$store.state.cron_status.logs_number; }, + cleanupModalClass: function() { + return { + 'active': true === this.cleanupModal + } + } }, watch: { logs_no: function () { @@ -153,8 +202,34 @@ document.body.appendChild(element); element.click(); document.body.removeChild(element); - } - + }, + openCleanupModal() { + this.cleanupModal = true; + }, + closeCleanupModal() { + this.cleanupModal = false; + }, + cleanupLogs: function() { + if (this.is_loading) { + this.$log.warn('Request in progress...Bail'); + return; + } + this.is_loading = true; + this.$store.dispatch('fetchAJAXPromise', { + req: 'cleanup_logs', + data: {} + }).then(response => { + this.is_loading = false; + this.cleanupModal = false; + if (this.$parent.start_status === true) { + // Stop sharing process if enabled. + this.$parent.togglePosting(); + } + }, error => { + this.is_loading = false; + Vue.$log.error('Got nothing from server. Prompt user to check internet connection and try again', error) + }) + }, }, } @@ -208,4 +283,27 @@ background-color: #FBE8E8; } } + #rop_core .rop-cleanup-modal .modal-container{ + max-width: 500px; + padding: 25px; + .modal-title, .modal-footer{ + text-align: center; + } + .btn-success{ + border:none; + background-color:#00a32a; + color: #fff; + padding: 0.5rem 1rem; + height: auto; + display: inline; + } + .btn-success:hover{ + background-color:#009528; + } + .modal-body{ + font-size: 0.7rem; + margin: 10px 30px; + padding: 0px; + } + }