Skip to content

Commit a490d09

Browse files
Merge pull request #1073 from Codeinwp/bugfix/pro/431
Added cleanup records
2 parents 8a92ad2 + c90f11a commit a490d09

8 files changed

Lines changed: 155 additions & 8 deletions

File tree

includes/admin/abstract/class-rop-model-abstract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected function set( $key, $value = '', $refresh = false ) {
9292

9393
$this->data[ $key ] = apply_filters( 'rop_set_key_' . $key, $value );
9494

95-
return update_option( $this->namespace, $this->data );
95+
return update_option( $this->namespace, $this->data, false );
9696
}
9797

9898

includes/admin/class-rop-logger.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ class Rop_Logger {
2929
*/
3030
private $stream;
3131

32+
/**
33+
* Holds the log namespace.
34+
*
35+
* @access private
36+
* @var string $log_namespace Defaults 'rop_logs'.
37+
*/
38+
private $log_namespace = 'rop_logs';
39+
3240
/**
3341
* Rop_Logger constructor.
3442
* Instantiate the Logger with specified formatter and stream.
@@ -38,7 +46,7 @@ class Rop_Logger {
3846
*/
3947
public function __construct() {
4048

41-
$this->stream = new Rop_Log_Handler( 'rop_logs' );
49+
$this->stream = new Rop_Log_Handler( $this->log_namespace );
4250

4351
}
4452

@@ -230,4 +238,23 @@ public function is_status_error_necessary( $logs = array() ) {
230238

231239
}
232240

241+
/**
242+
* Utility method to cleanup authenticated services.
243+
*
244+
* @access public
245+
*
246+
* @param int $cleanup_log_count Cleanup the log count.
247+
* @return array<string, mixed>
248+
*/
249+
public function cleanup_logs( $cleanup_log_count = 1000 ) {
250+
$logs = $this->stream->get_logs();
251+
252+
if ( count( $logs ) > $cleanup_log_count ) {
253+
$logs = array_slice( $logs, 0, $cleanup_log_count );
254+
255+
update_option( $this->log_namespace, array_reverse( $logs ), false );
256+
}
257+
258+
return $logs;
259+
}
233260
}

includes/admin/class-rop-rest-api.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,20 @@ private function add_account_bluesky( $data ) {
16421642
return $this->response->to_array();
16431643
}
16441644

1645+
/**
1646+
* API method called to cleanup logs.
1647+
*
1648+
* @access private
1649+
* @return array<string, mixed>
1650+
*/
1651+
private function cleanup_logs() {
1652+
$model = new Rop_Logger();
1653+
$this->response->set_code( '200' )
1654+
->set_data( $model->cleanup_logs() );
1655+
1656+
return $this->response->to_array();
1657+
}
1658+
16451659
/**
16461660
* Share API method.
16471661
*

includes/admin/models/class-rop-services-model.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class Rop_Services_Model extends Rop_Model_Abstract {
3737
*/
3838
private $accounts_namespace = 'active_accounts';
3939

40-
4140
/**
4241
* Utility method to clear authenticated services.
4342
*
@@ -519,5 +518,4 @@ public function find_account( $account_id ) {
519518

520519
return false;
521520
}
522-
523521
}

includes/class-rop-i18n.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,12 @@ public static function get_labels( $key = '' ) {
359359
'clear_btn' => __( 'Clear logs', 'tweet-old-post' ),
360360
'no_logs' => __( 'No recent logs!', 'tweet-old-post' ),
361361
'export_btn' => __( 'Export logs', 'tweet-old-post' ),
362+
'cleanup' => array(
363+
'cta' => __( 'Cleanup logs', 'tweet-old-post' ),
364+
'title' => __( 'Cleanup logs', 'tweet-old-post' ),
365+
'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' ),
366+
'btn' => __( 'Cleanup now', 'tweet-old-post' ),
367+
),
362368
),
363369
'general' => array(
364370
'plugin_name' => __( 'Revive Social', 'tweet-old-post' ),

vue/src/models/rop_store.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ export default new Vuex.Store({
270270
case 'toggle_tracking':
271271

272272
break
273+
case 'cleanup_logs':
274+
state.page.logs = stateData;
275+
state.cron_status.logs_number = stateData.length || 0;
276+
break;
273277
default:
274278
Vue.$log.error('No state request for ', requestName);
275279
}

vue/src/vue-elements/accounts-tab-panel.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
labels: this.$store.state.labels.accounts,
143143
upsell_link: ropApiSettings.upsell_link,
144144
pro_installed: ropApiSettings.pro_installed,
145-
postTimeout: ''
145+
postTimeout: '',
146146
}
147147
},
148148
computed: {
@@ -189,7 +189,7 @@
189189
},
190190
hasActiveAccountsLimitation: function () {
191191
return !this.pro_installed && this.accountsCount >= 2 && this.checkLicense ;
192-
}
192+
},
193193
},
194194
mounted: function () {
195195
if (0 === this.is_preloading) {

vue/src/vue-elements/logs-tab-panel.vue

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@
2929
/>
3030
{{ labels.clear_btn }}
3131
</button>
32+
<button
33+
class="btn btn-secondary"
34+
:class="logs_no <= 1000 ? 'd-none' : ''"
35+
@click="openCleanupModal()"
36+
>
37+
<i
38+
v-if="!is_loading"
39+
class="fa fa-trash"
40+
/>
41+
<i
42+
v-else
43+
class="fa fa-spinner fa-spin"
44+
/>
45+
{{ labels.cleanup.cta }}
46+
</button>
3247
</div>
3348
</div>
3449
<div class="columns">
@@ -70,6 +85,34 @@
7085
</div>
7186
</template>
7287
</div>
88+
<div
89+
class="modal rop-cleanup-modal"
90+
:class="cleanupModalClass"
91+
>
92+
<div class="modal-overlay" />
93+
<div class="modal-container">
94+
<div class="modal-header">
95+
<button
96+
class="btn btn-clear float-right"
97+
@click="closeCleanupModal()"
98+
/>
99+
<div class="modal-title h5">
100+
{{ labels.cleanup.title }}
101+
</div>
102+
</div>
103+
<div class="modal-body">
104+
{{ labels.cleanup.description }}
105+
</div>
106+
<div class="modal-footer">
107+
<button
108+
class="btn btn-success"
109+
@click="cleanupLogs()"
110+
>
111+
{{ labels.cleanup.btn }}
112+
</button>
113+
</div>
114+
</div>
115+
</div>
73116
</div>
74117
</div>
75118
</template>
@@ -86,6 +129,7 @@
86129
is_loading: false,
87130
labels: this.$store.state.labels.logs,
88131
upsell_link: ropApiSettings.upsell_link,
132+
cleanupModal: false,
89133
}
90134
},
91135
computed: {
@@ -95,6 +139,11 @@
95139
logs_no: function () {
96140
return this.$store.state.cron_status.logs_number;
97141
},
142+
cleanupModalClass: function() {
143+
return {
144+
'active': true === this.cleanupModal
145+
}
146+
}
98147
},
99148
watch: {
100149
logs_no: function () {
@@ -153,8 +202,34 @@
153202
document.body.appendChild(element);
154203
element.click();
155204
document.body.removeChild(element);
156-
}
157-
205+
},
206+
openCleanupModal() {
207+
this.cleanupModal = true;
208+
},
209+
closeCleanupModal() {
210+
this.cleanupModal = false;
211+
},
212+
cleanupLogs: function() {
213+
if (this.is_loading) {
214+
this.$log.warn('Request in progress...Bail');
215+
return;
216+
}
217+
this.is_loading = true;
218+
this.$store.dispatch('fetchAJAXPromise', {
219+
req: 'cleanup_logs',
220+
data: {}
221+
}).then(response => {
222+
this.is_loading = false;
223+
this.cleanupModal = false;
224+
if (this.$parent.start_status === true) {
225+
// Stop sharing process if enabled.
226+
this.$parent.togglePosting();
227+
}
228+
}, error => {
229+
this.is_loading = false;
230+
Vue.$log.error('Got nothing from server. Prompt user to check internet connection and try again', error)
231+
})
232+
},
158233
},
159234
}
160235
</script>
@@ -208,4 +283,27 @@
208283
background-color: #FBE8E8;
209284
}
210285
}
286+
#rop_core .rop-cleanup-modal .modal-container{
287+
max-width: 500px;
288+
padding: 25px;
289+
.modal-title, .modal-footer{
290+
text-align: center;
291+
}
292+
.btn-success{
293+
border:none;
294+
background-color:#00a32a;
295+
color: #fff;
296+
padding: 0.5rem 1rem;
297+
height: auto;
298+
display: inline;
299+
}
300+
.btn-success:hover{
301+
background-color:#009528;
302+
}
303+
.modal-body{
304+
font-size: 0.7rem;
305+
margin: 10px 30px;
306+
padding: 0px;
307+
}
308+
}
211309
</style>

0 commit comments

Comments
 (0)