PluginProbe ʕ •ᴥ•ʔ
Auto Post Cleaner / 3.7.6
Auto Post Cleaner v3.7.6
3.12.0 3.13.1 3.2.4 3.2.5 3.3.0 3.3.10 3.3.11 3.3.8 3.4.2 3.5.3 3.6.0 3.7.0 3.7.1 3.7.2 3.7.3 3.7.5 3.7.6 3.8.0 3.9.0 3.9.4 3.9.6 3.9.7 trunk 3.0.0 3.1.0 3.10.1 3.10.2 3.11.4
delete-old-posts-programmatically / inc / class-delete-old-posts.php
delete-old-posts-programmatically / inc Last commit date
class-delete-old-posts-filters.php 2 years ago class-delete-old-posts-redirects.php 2 years ago class-delete-old-posts.php 2 years ago class-enqueue-assets.php 2 years ago
class-delete-old-posts.php
1394 lines
1 <?php
2
3 namespace DEL\OLD\Posts\Cls;
4
5 /**
6 * Make the plugin class.
7 */
8 class Delete_Old_Posts
9 {
10 /**
11 * Hooks init (nothing else) and calls things that need to run right away.
12 */
13 public function __construct()
14 {
15 add_action( 'admin_menu', [ $this, 'deloldp_custom_menu_page' ] );
16 add_filter( 'plugin_action_links_delete-old-posts/delete-old-posts.php', [ $this, 'deloldp_settings_link' ] );
17 // add scheduled task
18 add_filter( 'cron_schedules', [ $this, 'deloldp_add_cron_interval' ] );
19 add_action( 'deloldp_cron_delete_old_posts', [ $this, 'deloldp_cron_exec' ] );
20 if ( !wp_next_scheduled( 'deloldp_cron_delete_old_posts' ) ) {
21 wp_schedule_event( time(), 'fifteen_seconds', 'deloldp_cron_delete_old_posts' );
22 }
23 // delete scheduled tasks when deactivated
24 register_deactivation_hook( plugin_dir_path( dirname( __FILE__, 1 ) ), [ $this, 'deloldp_deactivate' ] );
25 // Not like register_uninstall_hook(), you do NOT have to use a static function.
26 dop_fs()->add_action( 'after_uninstall', [ $this, 'deloldp_deleted' ] );
27 }
28
29 /**
30 * add a custom menu in admin menu
31 */
32 function deloldp_custom_menu_page()
33 {
34 $deloldp_menu = add_menu_page(
35 __( 'Delete old posts', 'delete-old-posts' ),
36 esc_html__( 'Delete old posts', 'delete-old-posts' ),
37 'manage_options',
38 'delete-old-posts',
39 array( $this, 'deloldp_PluginPage' ),
40 'dashicons-trash',
41 5
42 );
43 }
44
45 /**
46 * add settings link to plugin in the list of plugins
47 */
48 function deloldp_settings_link( $links )
49 {
50 // Build and escape the URL.
51 $url = esc_url( add_query_arg( 'page', 'delete-old-posts', get_admin_url() . 'admin.php' ) );
52 // Create the link.
53 $settings_link = "<a href='{$url}'>" . __( 'Settings' ) . '</a>';
54 // Adds the link to the end of the array.
55 array_push( $links, $settings_link );
56 return $links;
57 }
58
59 /**
60 * create an alert
61 */
62 function deloldp_makeAlert( $alertText = '', $alertStyle = 'success', $alertClose = 'is-dismissible' )
63 {
64 $alert = '
65 <div class="notice notice-' . $alertStyle . ' ' . $alertClose . '">
66 <p>' . $alertText . '</p>
67 </div>';
68 return $alert;
69 }
70
71 /*
72 * trim words
73 */
74 private function deloldp_TrimWords( $s, $limit = 3 )
75 {
76 return preg_replace( '/((\\w+\\W*){' . ($limit - 1) . '}(\\w+))(.*)/', '${1}', $s );
77 }
78
79 /**
80 * create a dropdown
81 */
82 public function deloldp_Dropdown()
83 {
84 ?>
85 <div x-data="{ open: false }" class="p-6 bg-gray-200 m-3.5">
86 <button @click="open = true">Open Dropdown</button>
87
88 <ul
89 x-show="open"
90 @click.away="open = false"
91 x-cloak
92 >
93 Dropdown Body
94 </ul>
95 </div>
96 <?php
97 }
98
99 /**
100 * function to display on Plugin Page
101 */
102 public function deloldp_PluginPage()
103 {
104 /**
105 * check if user have the rights to delete posts
106 */
107
108 if ( !current_user_can( 'delete_posts' ) & !current_user_can( 'delete_others_posts' ) ) {
109 esc_html_e( "You don't have rights to delete the posts.", "delete-old-posts" );
110 // exit
111 return;
112 }
113
114 global $dop_fs ;
115 $displayInfoTop = false;
116 $pluginOptionArray = array();
117 $postDeleteDays = 9999;
118 $pluginName = basename( plugin_dir_path( dirname( __FILE__, 1 ) ) );
119 $pluginName = ucwords( str_replace( "-", " ", $pluginName ) );
120 /**
121 * process the post data
122 */
123 if ( isset( $_POST['deloldp-post-days'] ) ) {
124
125 if ( !isset( $_POST['secured-delete-old-posts'] ) || !wp_verify_nonce( $_POST['secured-delete-old-posts'], 'start-delete-old-posts' ) ) {
126 esc_html__( 'Sorry, the security key did not verify.', 'delete-old-posts' );
127 exit;
128 } else {
129 // process form data
130
131 if ( $_POST['deloldp-post-days'] > 0 ) {
132 // save the desired post date in option
133 $pluginOptionArray['deloldpDays'] = (int) sanitize_text_field( $_POST['deloldp-post-days'] );
134
135 if ( $dop_fs->can_use_premium_code() ) {
136 // save skiptrash to option
137
138 if ( !isset( $_POST['deloldp-post-skiptrash'] ) ) {
139 $pluginOptionArray['deloldpSkiptrash'] = 0;
140 } else {
141 if ( $_POST['deloldp-post-skiptrash'] == 1 ) {
142 $pluginOptionArray['deloldpSkiptrash'] = 1;
143 }
144 }
145
146 // save number of posts to delete
147
148 if ( !isset( $_POST['deloldp-posts-number'] ) ) {
149 $pluginOptionArray['deloldpPostsNr'] = 1;
150 } else {
151 if ( $_POST['deloldp-posts-number'] > 1 ) {
152 $pluginOptionArray['deloldpPostsNr'] = (int) sanitize_text_field( $_POST['deloldp-posts-number'] );
153 }
154 }
155
156 }
157
158 // save redirection to option
159
160 if ( !isset( $_POST['deloldp-post-redirect'] ) ) {
161 $pluginOptionArray['deloldpRedirect'] = 0;
162 } else {
163 if ( $_POST['deloldp-post-redirect'] == 1 ) {
164 $pluginOptionArray['deloldpRedirect'] = 1;
165 }
166 }
167
168 // save start/ stop deleting posts option
169
170 if ( !isset( $_POST['toggledelete'] ) ) {
171 $pluginOptionArray['toggledelete'] = 0;
172 } else {
173 if ( $_POST['toggledelete'] == 1 ) {
174 $pluginOptionArray['toggledelete'] = 1;
175 }
176 }
177
178 // save fixed date to option
179 if ( isset( $_POST['deloldp-fix-date'] ) & isset( $_POST['date'] ) ) {
180 $pluginOptionArray['deloldpFixDate'] = sanitize_text_field( $_POST['date'] );
181 }
182 // save data into plugin option
183 $this->saveOption( $pluginOptionArray );
184 } else {
185 // display info
186 echo $this->deloldp_makeAlert( esc_html__( "Choose a date or specify the number of days for the posts to be deleted!", "delete-old-posts" ), "warning", "is-dismissible" ) ;
187 }
188
189 }
190
191 }
192 // get user saved options
193 $getOptionObject = get_option( 'deloldp-post-days-option' );
194 // display an Info on the top of the page
195 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) && property_exists( $getOptionObject->params, 'deloldpDays' ) ) {
196 $postDeleteDays = $getOptionObject->params->deloldpDays;
197 }
198 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) && property_exists( $getOptionObject->params, 'toggledelete' ) ) {
199 $postToggledelete = $getOptionObject->params->toggledelete;
200 }
201 $postDeleteDaysTime = $this->userDaysToTimestamp();
202 // display info on top only when the post delete days have been set
203 if ( isset( $postDeleteDaysTime ) && $postDeleteDaysTime > 0 ) {
204 $displayInfoTop = true;
205 }
206 $infoText = __( "Hint! Test out the list of deleted posts in the \"Filters\" menu, and when you're ready to begin deleting posts, activate the \"Start deleting posts\" option and click \"Save\". Note that no posts will be deleted until you take this action.", "delete-old-posts" );
207
208 if ( $displayInfoTop ) {
209
210 if ( $postDeleteDays > 0 & $postToggledelete == 1 ) {
211 $timezoneGmtOffset = get_option( 'gmt_offset' );
212 $timestampNextCronRun = wp_next_scheduled( 'deloldp_cron_delete_old_posts' );
213 $calculateMaxNumberOfDeletedPosts = ( isset( $getOptionObject->params->deloldpPostsNr ) ? 60 / 15 * 60 * 24 * $getOptionObject->params->deloldpPostsNr : 60 / 15 * 60 * 24 );
214 $infoText = sprintf(
215 __( "Posts published on %s %s, %s and before that will be deleted regularly in the background. Up to %d Posts/Day.", "delete-old-posts" ),
216 date( "F", $postDeleteDaysTime ),
217 date( "d", $postDeleteDaysTime ),
218 date( "Y", $postDeleteDaysTime ),
219 $calculateMaxNumberOfDeletedPosts
220 );
221 $infoText .= '<br />';
222 $infoText .= sprintf( __( "Next cron job run scheduled on %s.", "delete-old-posts" ), gmdate( "l jS \\of F Y h:i:s A", $timestampNextCronRun + $timezoneGmtOffset * 3600 ) );
223 // show warning if post are permanently deleted
224 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) ) {
225 if ( $dop_fs->can_use_premium_code() ) {
226 if ( is_object( $getOptionObject->params ) && property_exists( $getOptionObject->params, 'deloldpSkiptrash' ) & $getOptionObject->params->deloldpSkiptrash == 1 ) {
227 $infoText .= sprintf( " <span class='text-red-500'>%s.</span>", __( 'The posts will be permanently deleted', 'delete-old-posts' ) );
228 }
229 }
230 }
231 }
232
233 // show info on top
234 echo $this->deloldp_makeAlert( $infoText, "info", "is-dismissible" ) ;
235 }
236
237 // check if fixed date selected
238 $fixDate = false;
239 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) ) {
240 if ( isset( $getOptionObject->params->deloldpFixDate ) && $getOptionObject->params->deloldpFixDate != '' ) {
241 $fixDate = true;
242 }
243 }
244 ?>
245
246 <section x-data="deloldp_Start()" x-init="onstart()">
247 <div id="pluginInfo" x-cloak class="bg-green-50 border-t-4 border-green-500 rounded-b text-teal-900 px-4 py-3 shadow-md m-5 relative" role="info">
248 <div class="flex">
249 <div class="py-1"><svg class="fill-current h-6 w-6 text-teal-500 mr-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><path d="M2.93 17.07A10 10 0 1 1 17.07 2.93 10 10 0 0 1 2.93 17.07zm12.73-1.41A8 8 0 1 0 4.34 4.34a8 8 0 0 0 11.32 11.32zM9 11V9h2v6H9v-4zm0-6h2v2H9V5z"/></svg></div>
250 <div>
251 <p class="font-bold"><?php
252 esc_html_e( 'Choose a date or specify the number of days to have your posts automatically deleted.', 'delete-old-posts' );
253 ?></p>
254 <p class="text-sm">
255 <?php
256 esc_html_e( 'With the available filters, you have complete control to determine which posts are deleted. Posts are automatically deleted when the WordPress Cron runs or whenever someone visits your website.', 'delete-old-posts' );
257 ?>
258 </p>
259 </div>
260 </div>
261 <button type="button" class="notice-dismiss" @click="dismiss">
262 <span class="screen-reader-text"><?php
263 esc_html_e( 'Dismiss this notice.', 'delete-old-posts' );
264 ?></span>
265 </button>
266 </div>
267
268 <form method="post">
269 <div class="flex-grow container mx-auto sm:px-4 pt-6 pb-6">
270 <div class="bg-white border-t border-b sm:border-l sm:border-r sm:rounded shadow mb-6">
271 <div class="border-b px-6">
272 <div class="flex justify-between -mb-px">
273 <div class=" text-blue-dark py-4 text-lg">
274 <?php
275 esc_html_e( 'Delete old posts automatically', 'delete-old-posts' );
276 ?>
277 </div>
278 <div class="flex text-sm">
279 <div class="py-4 text-grey-dark border-b border-transparent hover:border-grey-dark mr-3">
280 <span class="dashicons dashicons-trash"></span>
281 <span class="dashicons dashicons-image-rotate"></span>
282 </div>
283 </div>
284 </div>
285 </div>
286 <div class="lg:flex">
287 <div class="lg:w-1/2 text-center py-8">
288 <div class="md:border-r md:border-b md:pb-6 lg:border-b-0">
289 <div class="m-2 text-base">
290 <?php
291 esc_html_e( 'Delete posts older than:', 'delete-old-posts' );
292 ?>
293 <input
294 type="number"
295 size="5"
296 name="deloldp-post-days"
297 id="deloldpDays"
298 x-on:input.debounce.1750="checkDays($event.target.value)"
299 class="w-20"
300 <?php
301
302 if ( !$fixDate ) {
303 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) ) {
304 if ( $getOptionObject->params->deloldpDays > 0 ) {
305 echo " value='" . $getOptionObject->params->deloldpDays . "'" ;
306 }
307 }
308 } else {
309 // get the number of days between two dates
310 $fixDateTimestamp = strtotime( $getOptionObject->params->deloldpFixDate );
311 $datediff = time() - $fixDateTimestamp;
312 echo " value='" . round( $datediff / (60 * 60 * 24) ) . "'" ;
313 }
314
315 ?>
316 />
317 <?php
318 esc_html_e( 'days.', 'delete-old-posts' );
319 ?>
320 <span
321 class="dashicons dashicons-editor-help has-tooltip"
322 x-on:mouseover="tooltip = true"
323 x-on:mouseleave="tooltip = false"
324 >
325 <span class='tooltip rounded shadow-lg bg-gray-100 p-3 text-sm font-sans -mt-8 text-left max-w-md'>
326 <?php
327 printf( __( 'Your posts published before the %s, will be automatically deleted in the background any time the WP Cron runs or someone visits your website.', 'delete-old-posts' ), '<strong>specified days</strong>' );
328 ?>
329 </span>
330 </span>
331 </div>
332 <div class="m-2 text-gray-500">
333 <?php
334 esc_html_e( "Choose the number of ", "delete-old-posts" );
335 ?>
336 <select
337 id="deloldp-post-months"
338 name="deloldp-post-months"
339 x-on:change="calculateDaysInMonths($event.target.value)"
340 >
341 <option value="" selected="selected"><?php
342 esc_html_e( "months", "delete-old-posts" );
343 ?></option>
344 <option value="1">1</option>
345 <option value="2">2</option>
346 <option value="3">3</option>
347 <option value="4">4</option>
348 <option value="5">5</option>
349 <option value="6">6</option>
350 <option value="7">7</option>
351 <option value="8">8</option>
352 <option value="9">9</option>
353 <option value="10">10</option>
354 <option value="11">11</option>
355 <option value="12">12</option>
356 </select>
357 <?php
358 esc_html_e( ' to calculate the number of days automatically,', 'delete-old-posts' );
359 ?>
360 <!-- <span class="dashicons dashicons-arrow-up-alt cursor-pointer" x-on:click="copyDays()"></span> -->
361 <span id="daysInXMonths" class="hidden"></span>
362 <?php
363 // esc_html_e('Days', 'delete-old-posts');
364 ?>
365 <!-- <span class="text-xs cursor-pointer" x-on:click="copyDays()"><?php
366 esc_html_e( 'Click to copy', 'delete-old-posts' );
367 ?></span> -->
368 </div>
369 <div class="m-2">
370 <div x-data="app()" x-init="[initDate(), getNoOfDays()]" x-cloak>
371 <div class="container mx-auto">
372 <div class="mx-auto w-64">
373 <label for="datepicker" class="mb-1 text-gray-500 block">
374 <?php
375 esc_html_e( "or select a date to calculate days in the past automatically:", "delete-old-posts" );
376 ?>
377 </label>
378 <div class="relative">
379 <input type="hidden" name="date" x-ref="date" :value="datepickerValue" />
380 <input type="text" x-on:click="showDatepicker = !showDatepicker" x-model="datepickerValue" x-on:keydown.escape="showDatepicker = false" class="w-full pl-4 pr-10 py-3 !bg-white leading-none rounded-lg shadow-sm focus:outline-none text-gray-600 font-medium focus:ring focus:ring-blue-600 focus:ring-opacity-50" placeholder="<?php
381 esc_html__( "Select date", "delete-old-posts" );
382 ?>" readonly />
383
384 <div class="absolute top-0 right-0 px-3 py-1">
385 <svg class="h-6 w-6 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
386 <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" />
387 </svg>
388 </div>
389
390 <!-- <div x-text="no_of_days.length"></div>
391 <div x-text="32 - new Date(year, month, 32).getDate()"></div>
392 <div x-text="new Date(year, month).getDay()"></div> -->
393
394 <div class="bg-white mt-12 rounded-lg shadow p-4 absolute top-0 left-0" style="width: 17rem" x-show.transition="showDatepicker" @click.away="showDatepicker = false">
395 <div class="flex justify-between items-center mb-2">
396 <div>
397 <span x-text="MONTH_NAMES[month]" class="text-lg font-bold text-gray-800"></span>
398 <span x-text="year" class="ml-1 text-lg text-gray-600 font-normal"></span>
399 </div>
400 <div>
401 <button type="button" class="focus:outline-none focus:shadow-outline transition ease-in-out duration-100 inline-flex cursor-pointer hover:bg-gray-100 p-1 rounded-full" @click="if (month == 0) {
402 year--;
403 month = 12;
404 } month--; getNoOfDays()">
405 <svg class="h-6 w-6 text-gray-400 inline-flex" fill="none" viewBox="0 0 24 24" stroke="currentColor">
406 <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
407 </svg>
408 </button>
409 <button type="button" class="focus:outline-none focus:shadow-outline transition ease-in-out duration-100 inline-flex cursor-pointer hover:bg-gray-100 p-1 rounded-full" @click="if (month == 11) {
410 month = 0;
411 year++;
412 } else {
413 month++;
414 } getNoOfDays()">
415 <svg class="h-6 w-6 text-gray-400 inline-flex" fill="none" viewBox="0 0 24 24" stroke="currentColor">
416 <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
417 </svg>
418 </button>
419 </div>
420 </div>
421
422 <div class="flex flex-wrap mb-3 -mx-1">
423 <template x-for="(day, index) in DAYS" :key="index">
424 <div style="width: 14.26%" class="px-0.5">
425 <div x-text="day" class="text-gray-800 font-medium text-center text-xs"></div>
426 </div>
427 </template>
428 </div>
429
430 <div class="flex flex-wrap -mx-1">
431 <template x-for="blankday in blankdays">
432 <div style="width: 14.28%" class="text-center border p-1 border-transparent text-sm"></div>
433 </template>
434 <template x-for="(date, dateIndex) in no_of_days" :key="dateIndex">
435 <div style="width: 14.28%" class="px-1 mb-1">
436 <div @click="getDateValue(date)" x-text="date" class="cursor-pointer text-center text-sm leading-none rounded-full leading-loose transition ease-in-out duration-100" :class="{
437 'bg-indigo-200': isToday(date) == true,
438 'text-gray-600 hover:bg-indigo-200': isToday(date) == false && isSelectedDate(date) == false,
439 'bg-indigo-500 text-white hover:bg-opacity-75': isSelectedDate(date) == true
440 }"></div>
441 </div>
442 </template>
443 </div>
444 </div>
445 </div>
446 </div>
447 </div>
448 </div>
449 </div>
450 <div class="mt-8">
451 <input
452 type="checkbox"
453 value="<?php
454 if ( $fixDate ) {
455 echo $getOptionObject->params->deloldpFixDate ;
456 }
457 ?>"
458 name="deloldp-fix-date"
459 id="fixdate"
460 <?php
461 if ( $fixDate ) {
462 echo ' checked' ;
463 }
464 ?>
465 />
466 <label for="fixdate">
467 <?php
468 esc_html_e( "Use a fixed date instead of a number of days in the past.", 'delete-old-posts' );
469 ?>
470 <span
471 class="dashicons dashicons-editor-help has-tooltip"
472 x-on:mouseover="tooltip = true"
473 x-on:mouseleave="tooltip = false"
474 >
475 <span class='tooltip rounded shadow-lg bg-gray-100 p-3 text-sm font-sans -mt-8 text-left max-w-md'>
476 <?php
477 esc_html_e( 'Select this option if you want to use a fixed date instead of a number of days in the past. If you select this option, only the posts older than the selected date will be deleted. If this option is not selected, the post older than the number of days in the past will be deleted, which will always be relative to today.', 'delete-old-posts' );
478 ?>
479 </span>
480 </span>
481 </label>
482 </div>
483 </div>
484 </div>
485 <div class="lg:w-1/2 text-center py-8">
486 <div class="m-2">
487 <input
488 type="checkbox"
489 value="1"
490 name="deloldp-post-redirect"
491 id="redirect"
492 <?php
493 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) ) {
494 if ( $getOptionObject->params->deloldpRedirect == 1 ) {
495 echo ' checked' ;
496 }
497 }
498 ?>
499 />
500 <label for="redirect" class="">
501 <?php
502 esc_html_e( "Redirect the URL of the deleted post to a similar post when requested.", 'delete-old-posts' );
503 ?>
504 <span
505 class="dashicons dashicons-editor-help has-tooltip"
506 x-on:mouseover="tooltip = true"
507 x-on:mouseleave="tooltip = false"
508 >
509 <span class='tooltip rounded shadow-lg bg-gray-100 p-3 text-sm font-sans -mt-8 max-w-md right-3 text-left'>
510 <?php
511 esc_html_e( 'Even after the posts are removed, they can still be linked to or found on Google. Choose this option if you want to redirect your visitors to the deleted post to another similar post on your website. The plugin will automatically find the best matching option, but you can customize it in the "Redirects" menu. The HTTP response status code 301 Moved Permanently is used for the redirect.', 'delete-old-posts' );
512 ?>
513 </span>
514 </span>
515 </label>
516 </div>
517
518 <div class="border-r">
519 <input
520 type="checkbox"
521 value="1"
522 @click="confirmSkipTrash()"
523 name="deloldp-post-skiptrash"
524 id="skiptrash"
525 <?php
526
527 if ( $dop_fs->can_use_premium_code() ) {
528 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) ) {
529 if ( $getOptionObject->params->deloldpSkiptrash == 1 ) {
530 echo ' checked' ;
531 }
532 }
533 } else {
534 echo ' disabled' ;
535 }
536
537 ?>
538 />
539 <label for="skiptrash" class="text-red-600">
540 <?php
541 esc_html_e( "Delete the Posts permanently, don't add them into Trash", 'delete-old-posts' );
542 ?> <span class="dashicons dashicons-database-remove"></span>.
543 </label>
544
545 <div class="m-2">
546 <?php
547 printf( __( 'Number of post to delete when the %s runs.', 'delete-old-posts' ), '<a href="https://developer.wordpress.org/plugins/cron/" target="_blank">WP-Cron</a>' );
548 $numberOfPostsToDelete = 1;
549 if ( $dop_fs->can_use_premium_code() ) {
550 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) ) {
551 if ( property_exists( $getOptionObject->params, 'deloldpPostsNr' ) ) {
552 $numberOfPostsToDelete = $getOptionObject->params->deloldpPostsNr;
553 }
554 }
555 }
556 ?>
557 <select
558 id="deloldp-post-number"
559 name="deloldp-posts-number"
560 <?php
561 if ( !$dop_fs->can_use_premium_code() ) {
562 echo "disabled" ;
563 }
564 ?>
565 >
566 <?php
567 $postNrs = array(
568 1,
569 2,
570 3,
571 4,
572 5,
573 10,
574 20,
575 30,
576 50,
577 100
578 );
579 foreach ( $postNrs as $pnr ) {
580 ?>
581 <option value="<?php
582 echo $pnr ;
583 ?>" <?php
584 echo ( isset( $numberOfPostsToDelete ) && $numberOfPostsToDelete == $pnr ? 'selected="selected"' : '' ) ;
585 ?>><?php
586 echo $pnr ;
587 ?></option>
588 <?php
589 }
590 ?>
591 </select>
592 <span
593 class="dashicons dashicons-editor-help has-tooltip"
594 x-on:mouseover="tooltip = true"
595 x-on:mouseleave="tooltip = false"
596 >
597 <span class='tooltip rounded shadow-lg bg-gray-100 p-3 text-sm font-sans -mt-8 max-w-md right-3 text-left'>
598 <?php
599 esc_html_e( 'Choose the number of post to be deleted at the same time. Please note, that a higher number of post deleted at once means a higher load on your server. You can choose a smaller number on a hosting with low resources (ex. shared hosting) and a higher number if your website is running for example on a dedicated server. By default, one post is deleted.', 'delete-old-posts' );
600 ?>
601 </span>
602 </span>
603 </div>
604
605 <?php
606
607 if ( !$dop_fs->can_use_premium_code() ) {
608 echo '
609 <section>' . esc_html__( 'These options are available in the Professional version.', 'delete-old-posts' ) ;
610 echo '
611 <a href="' . $dop_fs->get_upgrade_url() . '">' . '<u>' . esc_html__( 'Upgrade Now!', 'delete-old-posts' ) . '</u>' . '</a>' ;
612 echo '
613 </section>' ;
614 }
615
616 ?>
617
618 <section class="mx-8 mt-8 text-slate-500">
619 <?php
620 esc_html_e( "Filter the deleted post by selecting more options in the filters menu. Available filters are custom post types, categories, taxonomies, users, favorite post, and text search (posts containing a text), so only some from the posts will be deleted even if they are older than selected date (selected days).", "delete-old-posts" );
621 ?>
622 </section>
623
624 </div>
625 </div>
626 </div>
627 <div class="mt-3 mb-3 w-full flex items-center">
628 <div class="text-right w-1/2">
629 <div>
630 <!-- Rounded switch -->
631 <?php
632 $toggledelete = false;
633 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) && property_exists( $getOptionObject->params, 'toggledelete' ) ) {
634 if ( $getOptionObject->params->toggledelete == 1 ) {
635 $toggledelete = true;
636 }
637 }
638
639 if ( !$toggledelete ) {
640 esc_html_e( "Start", "delete-old-posts" );
641 } else {
642 esc_html_e( "Stop", "delete-old-posts" );
643 }
644
645 echo "&nbsp;" ;
646 esc_html_e( "deleting the posts:", "delete-old-posts" );
647 ?>
648 <label class="switch ml-2">
649 <input
650 type="checkbox"
651 id="toggledelete"
652 name="toggledelete"
653 value="1"
654 <?php
655 if ( $toggledelete ) {
656 echo "checked" ;
657 }
658 ?>
659 >
660 <span class="slider round"></span>
661 </label>
662 <!-- Help tooltip -->
663 <span
664 class="dashicons dashicons-editor-help has-tooltip"
665 x-on:mouseover="tooltip = true"
666 x-on:mouseleave="tooltip = false"
667 >
668 <span class='tooltip rounded shadow-lg bg-gray-100 p-3 text-sm text-left font-sans max-w-md'>
669 <?php
670 esc_html_e( 'Start/ Stop deleting posts. Select this option and click save to start/ stop deleting the posts automatically in background. Hint! You can first test the results when you save the filters.', 'delete-old-posts' );
671 ?>
672 </span>
673 </span>
674 </div>
675 </div>
676 <div class="text-left w-1/2 items-right">
677 <button
678 type="submit"
679 class="bg-blue-500 rounded-full font-bold text-white px-4 py-3 ml-5 inline-block transition duration-300 ease-in-out hover:bg-blue-600"
680 >
681 <?php
682 esc_html_e( 'Save', 'delete-old-posts' );
683 ?>
684 <svg xmlns="http://www.w3.org/2000/svg" class="inline ml-2 w-6 stroke-current text-white stroke-2" viewBox="0 0 24 24" fill="none" stroke-linecap="round" stroke-linejoin="round">
685 <line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/>
686 </svg>
687 </button>
688 <?php
689 wp_nonce_field( 'start-delete-old-posts', 'secured-delete-old-posts' );
690 ?>
691 </div>
692 </div>
693 </div>
694 </div>
695 </form>
696
697 <?php
698 /**
699 * get post deletion log
700 */
701 $post_delete_log = array();
702 if ( get_option( 'post_delete_log_array' ) !== false ) {
703 $post_delete_log = get_option( 'post_delete_log_array' );
704 }
705 ?>
706
707 <div class="flex-grow container mx-auto sm:px-4 pt-1 pb-1" x-data="{}">
708 <div class="bg-white border-t border-b sm:border-l sm:border-r sm:rounded shadow mb-6">
709 <div class="border-b px-6">
710 <div class="flex justify-between -mb-px">
711 <div class=" text-blue-dark py-4 text-lg">
712 <?php
713 esc_html_e( 'The latest deleted post:', 'delete-old-posts' );
714 ?>
715 </div>
716 <div class="flex text-sm">
717 <div class="py-4 text-grey-dark border-b border-transparent hover:border-grey-dark mr-3">
718 <span class="dashicons dashicons-nametag"></span>
719 </div>
720 </div>
721 </div>
722 </div>
723 <div class="text-gray-400 grid auto-cols-auto grid-flow-col gap-3">
724 <div class="tblrow">
725 <?php
726 esc_html_e( 'Post published date:', 'delete-old-posts' );
727 if ( isset( $post_delete_log['deleted_post_timestamp'] ) ) {
728 echo "&nbsp;" . date( "d M Y h:i:s A", $post_delete_log['deleted_post_timestamp'] ) ;
729 }
730 ?>
731 <br />
732 <?php
733 esc_html_e( 'Id:', 'delete-old-posts' );
734 echo "&nbsp;" ;
735 if ( isset( $post_delete_log['deleted_post_id'] ) ) {
736 echo $post_delete_log['deleted_post_id'] ;
737 }
738 ?>
739 </div>
740 <div class="tblrow">Title: <?php
741 if ( isset( $post_delete_log['deleted_post_title'] ) ) {
742 echo $post_delete_log['deleted_post_title'] ;
743 }
744 ?></div>
745 </div>
746 </div>
747 </div>
748 </section>
749
750 <?php
751 }
752
753 /**
754 * add custom cron interval
755 */
756 function deloldp_add_cron_interval( $schedules )
757 {
758 $schedules['fifteen_seconds'] = array(
759 'interval' => 15,
760 'display' => esc_html__( 'Every Fifteen Seconds' ),
761 );
762 return $schedules;
763 }
764
765 /**
766 * function to delete the cron job
767 */
768 function deloldp_deactivate()
769 {
770 // delete plugin data
771 delete_option( 'deloldp-post-days-option' );
772 // error_log('plugin deactivated');
773 $timestamp = wp_next_scheduled( 'deloldp_cron_delete_old_posts' );
774 wp_unschedule_event( $timestamp, 'deloldp_cron_delete_old_posts' );
775 }
776
777 /**
778 * function exec. when cron run
779 */
780 function deloldp_cron_exec()
781 {
782 // error_log('cron running...');
783 /**
784 * check if user selected the option to start deleting the posts
785 */
786 $startDeletingPosts = false;
787 // get user saved options
788 $getOptionObject = get_option( 'deloldp-post-days-option' );
789 // check if start delete posts option is on
790 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) && property_exists( $getOptionObject->params, 'toggledelete' ) ) {
791 if ( $getOptionObject->params->toggledelete == 1 ) {
792 $startDeletingPosts = true;
793 }
794 }
795 // get the posts to delete
796
797 if ( $startDeletingPosts ) {
798 $nrOfPostsToGet = $this->getNumberOfPosts();
799 $altPostsArray = $this->getAltestPostsObj( $nrOfPostsToGet );
800 }
801
802 /**
803 * delete the altest post
804 * @return NULL | array
805 */
806 if ( isset( $altPostsArray ) && is_array( $altPostsArray ) ) {
807 $this->deletePosts( $altPostsArray, $getOptionObject );
808 }
809 }
810
811 /**
812 * get the posts list
813 * @param $nrOfPostsToGet - the number of posts to retrieve
814 * @return post object
815 */
816 function getAltestPostObj( $nrOfPostsToGet = 1 )
817 {
818 $post_list = array();
819 // set sql query vars
820 $postTypes = $this->formatSQLParam( 'postType' );
821 $postCat = $this->formatSQLParam( 'postCategory' );
822 $postAuthor = $this->formatSQLParam( 'postAuthor' );
823 $relation = $this->formatSQLParam( 'relation' );
824 $postids = $this->formatSQLParam( 'postids' );
825 $skeywords = $this->formatSQLParam( 'search_keywords' );
826 $postStatus = $this->formatSQLParam( 'post_status' );
827 // create tax_query array
828 $tax_query = array();
829 $tax_query['relation'] = $relation;
830 if ( is_array( $postCat ) ) {
831 foreach ( $postCat as $cat ) {
832 $tax_query[] = array(
833 'taxonomy' => 'category',
834 'field' => 'term_id',
835 'terms' => $cat,
836 );
837 }
838 }
839 // get the altest post
840 $posts_arg = array(
841 'post_type' => $postTypes,
842 'post_status' => $postStatus,
843 'orderby' => 'date',
844 'order' => 'ASC',
845 'posts_per_page' => $nrOfPostsToGet,
846 'include' => array(),
847 'exclude' => $postids,
848 'author' => $postAuthor,
849 'meta_key' => '',
850 'meta_value' => '',
851 's' => $skeywords,
852 'tax_query' => $tax_query,
853 );
854 if ( is_array( $postTypes ) ) {
855 $post_list = get_posts( $posts_arg );
856 }
857 return $post_list;
858 }
859
860 /**
861 * get the custom posts list
862 * @param $nrOfPostsToGet - the number of posts to retrieve
863 * @return post object
864 */
865 function getAltestCustomPostObj( $nrOfPostsToGet = 1 )
866 {
867 $custom_post_list = array();
868 // set sql query vars
869 $customPostTypes = $this->formatSQLParam( 'customPostTypes' );
870 // create a request for each specified cpt
871 $custom_post_list = array();
872 if ( is_array( $customPostTypes ) ) {
873 foreach ( $customPostTypes as $cpt ) {
874 // get the tax_query
875 $postTax = $this->formatSQLParam( 'postTax', $cpt );
876 $postAuthor = $this->formatSQLParam( 'postAuthor' );
877 $postids = $this->formatSQLParam( 'postids' );
878 $skeywords = $this->formatSQLParam( 'search_keywords' );
879 $postStatus = $this->formatSQLParam( 'post_status' );
880 // get the altest post
881 $custom_post_arr = get_posts( array(
882 'post_type' => $cpt,
883 'post_status' => $postStatus,
884 'orderby' => 'date',
885 'order' => 'ASC',
886 'posts_per_page' => $nrOfPostsToGet,
887 'include' => array(),
888 'exclude' => $postids,
889 'author' => $postAuthor,
890 'meta_key' => '',
891 'meta_value' => '',
892 's' => $skeywords,
893 'tax_query' => $postTax,
894 ) );
895 if ( is_array( $custom_post_arr ) ) {
896 foreach ( $custom_post_arr as $postsObject ) {
897 $custom_post_list[] = $postsObject;
898 }
899 }
900 }
901 }
902 return $custom_post_list;
903 }
904
905 /**
906 * make user choosed days in a timestamp
907 */
908 function userDaysToTimestamp()
909 {
910 $getOptionObject = get_option( 'deloldp-post-days-option' );
911 // check if fixed date selected
912 $fixDate = false;
913 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) ) {
914 if ( isset( $getOptionObject->params->deloldpFixDate ) && $getOptionObject->params->deloldpFixDate != '' ) {
915 $fixDate = true;
916 }
917 }
918 switch ( $fixDate ) {
919 case true:
920 // get the number of days between two dates
921 $fixDateTimestamp = strtotime( $getOptionObject->params->deloldpFixDate );
922 $datediff = time() - $fixDateTimestamp;
923 $deleteSavedDays = absint( round( $datediff / (60 * 60 * 24) ) );
924 break;
925 case false:
926 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) ) {
927 if ( property_exists( $getOptionObject->params, 'deloldpDays' ) ) {
928 $deleteSavedDays = absint( $getOptionObject->params->deloldpDays );
929 }
930 }
931 break;
932 }
933
934 if ( isset( $deleteSavedDays ) && is_int( $deleteSavedDays ) ) {
935 $post2DelTimestamp = strtotime( "-" . $deleteSavedDays . ' days' );
936 /**
937 * get the timestamp of the date at 23:59:59
938 * all post published untill this date and time will be deleted
939 */
940 $post2DelTimestamp00 = mktime(
941 23,
942 59,
943 59,
944 date( 'm', $post2DelTimestamp ),
945 date( 'd', $post2DelTimestamp ),
946 date( 'Y', $post2DelTimestamp )
947 );
948 return $post2DelTimestamp00;
949 }
950
951 return false;
952 }
953
954 /**
955 * save options - save plugin data
956 */
957 function saveOption( $optionValue = array() )
958 {
959 $myOptions = new \stdClass();
960 $myOptions->name = 'deloldp-post-days-option';
961 $myOptions->params = (object) $this->sanitize_text_or_array_field( $optionValue );
962 /**
963 * save the plugin options
964 */
965 return update_option( 'deloldp-post-days-option', $myOptions );
966 }
967
968 /**
969 * Recursive sanitation for text or array
970 *
971 * @param $array_or_string (array|string)
972 * @since 0.1
973 * @return mixed
974 */
975 function sanitize_text_or_array_field( $array_or_string )
976 {
977
978 if ( is_string( $array_or_string ) ) {
979 $array_or_string = sanitize_text_field( $array_or_string );
980 } elseif ( is_array( $array_or_string ) ) {
981 foreach ( $array_or_string as $key => &$value ) {
982
983 if ( is_array( $value ) ) {
984 $value = $this->sanitize_text_or_array_field( $value );
985 } else {
986 $value = sanitize_text_field( $value );
987 }
988
989 }
990 }
991
992 return $array_or_string;
993 }
994
995 /**
996 * Make some actions when the plugin is removed
997 */
998 function deloldp_deleted()
999 {
1000 return true;
1001 }
1002
1003 /**
1004 * Save deleted posts data in an option and use it to rediect traffic
1005 *
1006 * @param $deletedPostData (array)
1007 */
1008 function saveToRedirectOpt( $deletedPostData )
1009 {
1010 // check if option to redirect the deleted posts is checked
1011 $deloldpRedirect_opt = get_option( 'deloldp-post-days-option' );
1012 if ( is_object( $deloldpRedirect_opt ) && property_exists( $deloldpRedirect_opt->params, 'deloldpRedirect' ) ) {
1013 if ( $deloldpRedirect_opt->params->deloldpRedirect == 1 ) {
1014 // option checked - save the redirect
1015
1016 if ( is_object( $deletedPostData ) ) {
1017 // update deletedpostredirectsopt
1018 $deletedpostredirectsopt = get_option( 'deletedpostredirectsopt' );
1019 if ( !is_array( $deletedpostredirectsopt ) ) {
1020 $deletedpostredirectsopt = array();
1021 }
1022 $deletedpostredirectsopt[] = ( isset( $deletedPostData->post_name ) ? $deletedPostData->post_name : '' );
1023 // update the option
1024 array_unique( $deletedpostredirectsopt );
1025 update_option( 'deletedpostredirectsopt', $deletedpostredirectsopt );
1026 }
1027
1028 }
1029 }
1030 }
1031
1032 /**
1033 * Create help tooltips
1034 */
1035 function generateHelpTooltip( $helpTxt, $tooltipClass = '', $tooltipTxtClass = '' )
1036 {
1037 ?>
1038 <span
1039 class="dashicons dashicons-editor-help has-tooltip <?php
1040 echo $tooltipClass ;
1041 ?>"
1042 x-on:mouseover="tooltip = true"
1043 x-on:mouseleave="tooltip = false"
1044 >
1045 <span class='tooltip rounded shadow-lg bg-gray-100 p-3 text-sm font-sans -mt-8 max-w-md <?php
1046 echo $tooltipTxtClass ;
1047 ?>'>
1048 <?php
1049 echo $helpTxt ;
1050 ?>
1051 </span>
1052 </span>
1053 <?php
1054 }
1055
1056 /**
1057 * format the SQL parameters used to fetch the posts to be deleted
1058 * @param $what -> define what parameter to return
1059 * @return array | string
1060 */
1061 function formatSQLParam( $what = '', $cpt = '' )
1062 {
1063 // get filters options saved values
1064 $delop_filters = get_option( 'delop_filters' );
1065 switch ( $what ) {
1066 case 'postType':
1067 // default post type = post
1068 $postTypes = array( 'post' );
1069 // check if only custom post types selected
1070 if ( is_array( $delop_filters ) && isset( $delop_filters['cpt'] ) && is_array( $delop_filters['cpt'] ) && !in_array( 'post', $delop_filters['cpt'] ) ) {
1071 $postTypes = 'skip';
1072 }
1073 return $postTypes;
1074 case 'customPostTypes':
1075 $customPostTypes = 'skip';
1076 // if user selected the post types just return the cpt array
1077
1078 if ( is_array( $delop_filters ) && isset( $delop_filters['cpt'] ) ) {
1079 $customPostTypes = $delop_filters['cpt'];
1080 // delete post in cpt array
1081 if ( ($post_val_key = array_search( 'post', $customPostTypes )) !== false ) {
1082 unset( $customPostTypes[$post_val_key] );
1083 }
1084 // check if any post left in the cpt array
1085 if ( count( $customPostTypes ) == 0 ) {
1086 $customPostTypes = 'skip';
1087 }
1088 return $customPostTypes;
1089 }
1090
1091 case 'post_status':
1092 $post_status = array();
1093 if ( is_array( $delop_filters ) && isset( $delop_filters['cpt_type'] ) && is_array( $delop_filters['cpt_type'] ) ) {
1094 $post_status = $delop_filters['cpt_type'];
1095 }
1096 return $post_status;
1097 case 'postCategory':
1098 // default val. for category = 0 -> no category
1099 $postCat = 0;
1100 if ( is_array( $delop_filters ) && isset( $delop_filters['post_category'] ) && is_array( $delop_filters['post_category'] ) ) {
1101 // $postCat = implode( ',', filter_var_array($delop_filters['post_category']) );
1102 $postCat = filter_var_array( $delop_filters['post_category'] );
1103 }
1104 return $postCat;
1105 case 'postAuthor':
1106 $postAuthor = 0;
1107 if ( is_array( $delop_filters ) && isset( $delop_filters['userid'] ) && is_array( $delop_filters['userid'] ) ) {
1108 $postAuthor = implode( ',', filter_var_array( $delop_filters['userid'] ) );
1109 }
1110 return $postAuthor;
1111 case 'postTax':
1112 $postTax = array();
1113 if ( isset( $cpt ) && is_string( $cpt ) && $cpt != '' ) {
1114 // check if cpt specified - needed to check if the taxonomies are registered for (belongs to) the cpt
1115
1116 if ( is_array( $delop_filters ) && isset( $delop_filters['tax_input'] ) && is_array( $delop_filters['tax_input'] ) ) {
1117 // format the taxonomy array like this
1118 // array(
1119 // 'relation' => 'AND',
1120 // array(
1121 // 'taxonomy' => 'services', // you can change it according to your taxonomy
1122 // 'field' => 'term_id', // this can be 'term_id', 'slug' & 'name'
1123 // 'terms' => $service->term_id,
1124 // )
1125 // )
1126 $relation = 'AND';
1127 if ( is_array( $delop_filters ) && isset( $delop_filters['relation'] ) ) {
1128 $relation = sanitize_text_field( $delop_filters['relation'] );
1129 }
1130 $postTax['relation'] = $relation;
1131 // use relation when are more terms (AND | OR)
1132 foreach ( $delop_filters['tax_input'] as $key => $tax_input ) {
1133 $get_obj_taxonomies = get_object_taxonomies( $cpt );
1134 if ( is_array( $tax_input ) && in_array( $key, $get_obj_taxonomies ) ) {
1135 if ( is_array( $tax_input ) ) {
1136 foreach ( $tax_input as $term ) {
1137 $postTax[] = array(
1138 'taxonomy' => $key,
1139 'field' => 'term_id',
1140 'terms' => $term,
1141 );
1142 }
1143 }
1144 }
1145 }
1146 }
1147
1148 }
1149 return $postTax;
1150 case 'relation':
1151 // get the selected relation for tax_query
1152 $relation = 'AND';
1153 if ( is_array( $delop_filters ) && isset( $delop_filters['relation'] ) ) {
1154 $relation = sanitize_text_field( $delop_filters['relation'] );
1155 }
1156 return $relation;
1157 case 'postids':
1158 $postids = array();
1159 if ( is_array( $delop_filters ) && isset( $delop_filters['postids'] ) ) {
1160 $postids = array_map( 'trim', explode( ',', sanitize_text_field( $delop_filters['postids'] ) ) );
1161 }
1162 return $postids;
1163 case 'search_keywords':
1164 $search_keywords = '';
1165 if ( is_array( $delop_filters ) ) {
1166
1167 if ( isset( $delop_filters['search_keyword'] ) && $delop_filters['search_keyword'] != '' ) {
1168 $search_keywords = sanitize_text_field( $delop_filters['search_keyword'] );
1169 // check if negative keywords exists
1170 if ( isset( $delop_filters['search_keyword_negativ'] ) && $delop_filters['search_keyword_negativ'] != '' ) {
1171 $search_keywords = $search_keywords . ' -' . sanitize_text_field( $delop_filters['search_keyword_negativ'] );
1172 }
1173 }
1174
1175 }
1176 return $search_keywords;
1177 }
1178 return '';
1179 }
1180
1181 /**
1182 * get the old posts to delete array
1183 */
1184 function getAltestPostsObj( $nrOfPostsToGet )
1185 {
1186 $altPostsFinalArray = array();
1187 // get the default posts list
1188 $altPostsArray = $this->getAltestPostObj( $nrOfPostsToGet );
1189 // get the custom posts list
1190 $altCustomPostsArray = $this->getAltestCustomPostObj( $nrOfPostsToGet );
1191 // combine the lists toghether
1192 if ( is_array( $altPostsArray ) ) {
1193 foreach ( $altPostsArray as $oldPost ) {
1194 $altPostsFinalArray[] = $oldPost;
1195 }
1196 }
1197 if ( is_array( $altCustomPostsArray ) ) {
1198 foreach ( $altCustomPostsArray as $oldCustomPost ) {
1199 $altPostsFinalArray[] = $oldCustomPost;
1200 }
1201 }
1202 // get user saved options
1203 $getOptionObject = get_option( 'deloldp-post-days-option' );
1204 // get the list of deleted posts without deleting them
1205 $postsDeleted = $this->deletePosts( $altPostsFinalArray, $getOptionObject, false );
1206 if ( !is_array( $postsDeleted ) ) {
1207 $postsDeleted = array();
1208 }
1209 // if NULL returned
1210 return $postsDeleted;
1211 }
1212
1213 /**
1214 * retrieve the number of post to delete
1215 */
1216 function getNumberOfPosts()
1217 {
1218 global $dop_fs ;
1219 $nrOfPostsToGet = 1;
1220
1221 if ( $dop_fs->can_use_premium_code() ) {
1222 $getOptionObject = get_option( 'deloldp-post-days-option' );
1223 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) ) {
1224 if ( property_exists( $getOptionObject->params, 'deloldpPostsNr' ) ) {
1225 $nrOfPostsToGet = $getOptionObject->params->deloldpPostsNr;
1226 }
1227 }
1228 }
1229
1230 return $nrOfPostsToGet;
1231 }
1232
1233 /**
1234 * log var_dump errors
1235 * you can only echo the results you have to capture the output buffer with ob_start(), assign it to a variable,
1236 * and then clean the buffer with ob_end_clean() allowing you to write the resulting variable to the error_log
1237 */
1238 function var_error_log( $object = null )
1239 {
1240 ob_start();
1241 // start buffer capture
1242 var_dump( $object );
1243 // dump the values
1244 $contents = ob_get_contents();
1245 // put the buffer into a variable
1246 ob_end_clean();
1247 // end capture
1248 error_log( $contents );
1249 // log contents of the result of var_dump( $object )
1250 }
1251
1252 /**
1253 * delete the posts
1254 * @param array $altPostsArray = array of posts selected from DB
1255 * @param object $getOptionObject = saved plugin options
1256 * @param true|false $action -> true = delete the posts | false = test (return the list of posts to delete)
1257 */
1258 function deletePosts( $altPostsArray, $getOptionObject, $action = true )
1259 {
1260 global $dop_fs ;
1261 // get the user delete days
1262 $postDeleteDaysTime = $this->userDaysToTimestamp();
1263 /** check if number of days was set */
1264 if ( !$postDeleteDaysTime ) {
1265 return;
1266 }
1267 // posts nr. of days to be deleted not defined - exit
1268 // error_log("Time: " . date("d M Y", $postDeleteDaysTime));
1269
1270 if ( isset( $altPostsArray ) && is_array( $altPostsArray ) ) {
1271 $i = 0;
1272 $deletedPostsArray = array();
1273 // get saved attached_img option
1274 $delop_filters = get_option( 'delop_filters' );
1275 $attached_img_opt = ( is_array( $delop_filters ) && isset( $delop_filters['attached_img'] ) ? $delop_filters['attached_img'] : '' );
1276 foreach ( $altPostsArray as $altPostDataObj ) {
1277
1278 if ( is_object( $altPostDataObj ) ) {
1279 $altPostTimestamp = strtotime( $altPostDataObj->post_date );
1280
1281 if ( $postDeleteDaysTime > $altPostTimestamp ) {
1282 // save the last entry for log
1283 if ( $i == count( $altPostsArray ) - 1 ) {
1284 if ( $action ) {
1285 update_option( 'post_delete_log_array', array(
1286 'deleted_post_id' => $altPostDataObj->ID,
1287 'deleted_post_timestamp' => $altPostTimestamp,
1288 'deleted_post_title' => $altPostDataObj->post_title,
1289 'deleted_post_name' => $altPostDataObj->post_name,
1290 'deleted_post_url' => $altPostDataObj->guid,
1291 ) );
1292 }
1293 }
1294 $i++;
1295 // ======== delete post =========
1296 $deletePostSkipTrash = false;
1297
1298 if ( $dop_fs->can_use_premium_code() ) {
1299 // check if skipTrash is set
1300 $skipTrashOption = 0;
1301 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) ) {
1302 if ( $getOptionObject->params->deloldpSkiptrash ) {
1303 $skipTrashOption = $getOptionObject->params->deloldpSkiptrash;
1304 }
1305 }
1306 if ( $skipTrashOption == 1 ) {
1307 $deletePostSkipTrash = true;
1308 }
1309 }
1310
1311 // delete the post
1312 if ( isset( $altPostDataObj->ID ) ) {
1313
1314 if ( $action ) {
1315 // delete the post
1316
1317 if ( $deletePostSkipTrash ) {
1318 $deletePostResult = wp_delete_post( (int) $altPostDataObj->ID, $deletePostSkipTrash );
1319 } else {
1320 $deletePostResult = wp_trash_post( (int) $altPostDataObj->ID );
1321 }
1322
1323 //cpt are not trashed with wp_delete_post
1324 } else {
1325 /**
1326 * test -> create the array of deleted posts to return without actually deleting them
1327 */
1328 $deletedPostsArray[] = $altPostDataObj;
1329 }
1330
1331 }
1332 /**
1333 * Save deleted post data in an option to redirect it later to a similar post if old post URL called
1334 */
1335 if ( $action ) {
1336 $this->saveToRedirectOpt( $altPostDataObj );
1337 }
1338 }
1339
1340 }
1341
1342 }
1343 if ( !$action ) {
1344 // return the array with posts that normally would have been deleted
1345 return $deletedPostsArray;
1346 }
1347 }
1348
1349 }
1350
1351 /**
1352 * Return the Filter's form Post Vars.
1353 */
1354 function getFiltersOpt( $whatToReturn = '', $nestedarray = '' )
1355 {
1356 // delete_option('delop_filters');
1357 // get filters options saved values
1358 $delop_filters = get_option( 'delop_filters' );
1359 if ( isset( $delop_filters[trim( $whatToReturn )] ) & $nestedarray == '' ) {
1360 return $delop_filters[trim( $whatToReturn )];
1361 }
1362 if ( isset( $delop_filters[trim( $whatToReturn )][trim( $nestedarray )] ) & $nestedarray != '' ) {
1363 return $delop_filters[trim( $whatToReturn )][trim( $nestedarray )];
1364 }
1365 // if the options are empty return the default post type
1366 if ( empty($delop_filters) && $whatToReturn == 'cpt' ) {
1367 return array( 'post' );
1368 }
1369 // no data saved for the request
1370 return false;
1371 }
1372
1373 /**
1374 * Check if we are on specific page
1375 */
1376 function delop_checkIfPage( $requestedPage )
1377 {
1378 global $wp ;
1379 /**
1380 * get the page url
1381 */
1382 $current_url = esc_url( home_url( add_query_arg( $_GET, $wp->request ) ) );
1383 /**
1384 * check if $_POST in the filter page
1385 */
1386 if ( preg_match( '/page=' . $requestedPage . '/i', $current_url ) ) {
1387 // we are on the page
1388 return true;
1389 }
1390 // not the requested page
1391 return false;
1392 }
1393
1394 }