PluginProbe ʕ •ᴥ•ʔ
Auto Post Cleaner / 3.3.10
Auto Post Cleaner v3.3.10
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 3 years ago class-delete-old-posts-redirects.php 3 years ago class-delete-old-posts.php 3 years ago class-enqueue-assets.php 3 years ago
class-delete-old-posts.php
1383 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 ) && !empty($_POST) ) {
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 ( isset( $_POST['deloldp-post-days'] ) && $_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 // create tax_query array
827 $tax_query = array();
828 $tax_query['relation'] = $relation;
829 if ( is_array( $postCat ) ) {
830 foreach ( $postCat as $cat ) {
831 $tax_query[] = array(
832 'taxonomy' => 'category',
833 'field' => 'term_id',
834 'terms' => $cat,
835 );
836 }
837 }
838 // get the altest post
839 if ( is_array( $postTypes ) ) {
840 $post_list = get_posts( array(
841 'post_type' => $postTypes,
842 'post_status' => 'publish',
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 }
855 return $post_list;
856 }
857
858 /**
859 * get the custom posts list
860 * @param $nrOfPostsToGet - the number of posts to retrieve
861 * @return post object
862 */
863 function getAltestCustomPostObj( $nrOfPostsToGet = 1 )
864 {
865 $custom_post_list = array();
866 // set sql query vars
867 $customPostTypes = $this->formatSQLParam( 'customPostTypes' );
868 // create a request for each specified cpt
869 $custom_post_list = array();
870 if ( is_array( $customPostTypes ) ) {
871 foreach ( $customPostTypes as $cpt ) {
872 // get the tax_query
873 $postTax = $this->formatSQLParam( 'postTax', $cpt );
874 $postAuthor = $this->formatSQLParam( 'postAuthor' );
875 $postids = $this->formatSQLParam( 'postids' );
876 $skeywords = $this->formatSQLParam( 'search_keywords' );
877 // get the altest post
878 $custom_post_arr = get_posts( array(
879 'post_type' => $cpt,
880 'post_status' => 'publish',
881 'orderby' => 'date',
882 'order' => 'ASC',
883 'posts_per_page' => $nrOfPostsToGet,
884 'include' => array(),
885 'exclude' => $postids,
886 'author' => $postAuthor,
887 'meta_key' => '',
888 'meta_value' => '',
889 's' => $skeywords,
890 'tax_query' => $postTax,
891 ) );
892 if ( is_array( $custom_post_arr ) ) {
893 foreach ( $custom_post_arr as $postsObject ) {
894 $custom_post_list[] = $postsObject;
895 }
896 }
897 }
898 }
899 return $custom_post_list;
900 }
901
902 /**
903 * make user choosed days in a timestamp
904 */
905 function userDaysToTimestamp()
906 {
907 $getOptionObject = get_option( 'deloldp-post-days-option' );
908 // check if fixed date selected
909 $fixDate = false;
910 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) ) {
911 if ( isset( $getOptionObject->params->deloldpFixDate ) && $getOptionObject->params->deloldpFixDate != '' ) {
912 $fixDate = true;
913 }
914 }
915 switch ( $fixDate ) {
916 case true:
917 // get the number of days between two dates
918 $fixDateTimestamp = strtotime( $getOptionObject->params->deloldpFixDate );
919 $datediff = time() - $fixDateTimestamp;
920 $deleteSavedDays = absint( round( $datediff / (60 * 60 * 24) ) + 1 );
921 break;
922 case false:
923 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) ) {
924 if ( property_exists( $getOptionObject->params, 'deloldpDays' ) ) {
925 $deleteSavedDays = absint( $getOptionObject->params->deloldpDays + 1 );
926 }
927 }
928 break;
929 }
930
931 if ( isset( $deleteSavedDays ) && is_int( $deleteSavedDays ) ) {
932 $post2DelTimestamp = strtotime( "-" . $deleteSavedDays . ' days' );
933 /**
934 * get the timestamp of the date at 23:59:59
935 * all post published untill this date and time will be deleted
936 */
937 $post2DelTimestamp00 = mktime(
938 23,
939 59,
940 59,
941 date( 'm', $post2DelTimestamp ),
942 date( 'd', $post2DelTimestamp ),
943 date( 'Y', $post2DelTimestamp )
944 );
945 return $post2DelTimestamp00;
946 }
947
948 return false;
949 }
950
951 /**
952 * save options - save plugin data
953 */
954 function saveOption( $optionValue = array() )
955 {
956 $myOptions = new \stdClass();
957 $myOptions->name = 'deloldp-post-days-option';
958 $myOptions->params = (object) $this->sanitize_text_or_array_field( $optionValue );
959 /**
960 * save the plugin options
961 */
962 return update_option( 'deloldp-post-days-option', $myOptions );
963 }
964
965 /**
966 * Recursive sanitation for text or array
967 *
968 * @param $array_or_string (array|string)
969 * @since 0.1
970 * @return mixed
971 */
972 function sanitize_text_or_array_field( $array_or_string )
973 {
974
975 if ( is_string( $array_or_string ) ) {
976 $array_or_string = sanitize_text_field( $array_or_string );
977 } elseif ( is_array( $array_or_string ) ) {
978 foreach ( $array_or_string as $key => &$value ) {
979
980 if ( is_array( $value ) ) {
981 $value = $this->sanitize_text_or_array_field( $value );
982 } else {
983 $value = sanitize_text_field( $value );
984 }
985
986 }
987 }
988
989 return $array_or_string;
990 }
991
992 /**
993 * Make some actions when the plugin is removed
994 */
995 function deloldp_deleted()
996 {
997 return true;
998 }
999
1000 /**
1001 * Save deleted posts data in an option and use it to rediect traffic
1002 *
1003 * @param $deletedPostData (array)
1004 */
1005 function saveToRedirectOpt( $deletedPostData )
1006 {
1007 // check if option to redirect the deleted posts is checked
1008 $deloldpRedirect_opt = get_option( 'deloldp-post-days-option' );
1009 if ( is_object( $deloldpRedirect_opt ) && property_exists( $deloldpRedirect_opt->params, 'deloldpRedirect' ) ) {
1010 if ( $deloldpRedirect_opt->params->deloldpRedirect == 1 ) {
1011 // option checked - save the redirect
1012
1013 if ( is_object( $deletedPostData ) ) {
1014 // update deletedpostredirectsopt
1015 $deletedpostredirectsopt = get_option( 'deletedpostredirectsopt' );
1016 if ( !is_array( $deletedpostredirectsopt ) ) {
1017 $deletedpostredirectsopt = array();
1018 }
1019 $deletedpostredirectsopt[] = ( isset( $deletedPostData->post_name ) ? $deletedPostData->post_name : '' );
1020 // update the option
1021 array_unique( $deletedpostredirectsopt );
1022 update_option( 'deletedpostredirectsopt', $deletedpostredirectsopt );
1023 }
1024
1025 }
1026 }
1027 }
1028
1029 /**
1030 * Create help tooltips
1031 */
1032 function generateHelpTooltip( $helpTxt, $tooltipClass = '', $tooltipTxtClass = '' )
1033 {
1034 ?>
1035 <span
1036 class="dashicons dashicons-editor-help has-tooltip <?php
1037 echo $tooltipClass ;
1038 ?>"
1039 x-on:mouseover="tooltip = true"
1040 x-on:mouseleave="tooltip = false"
1041 >
1042 <span class='tooltip rounded shadow-lg bg-gray-100 p-3 text-sm font-sans -mt-8 max-w-md <?php
1043 echo $tooltipTxtClass ;
1044 ?>'>
1045 <?php
1046 echo $helpTxt ;
1047 ?>
1048 </span>
1049 </span>
1050 <?php
1051 }
1052
1053 /**
1054 * format the SQL parameters used to fetch the posts to be deleted
1055 * @param $what -> define what parameter to return
1056 * @return array | string
1057 */
1058 function formatSQLParam( $what = '', $cpt = '' )
1059 {
1060 // get filters options saved values
1061 $delop_filters = get_option( 'delop_filters' );
1062 switch ( $what ) {
1063 case 'postType':
1064 // default post type = post
1065 $postTypes = array( 'post' );
1066 // check if only custom post types selected
1067 if ( is_array( $delop_filters ) && isset( $delop_filters['cpt'] ) && is_array( $delop_filters['cpt'] ) && !in_array( 'post', $delop_filters['cpt'] ) ) {
1068 $postTypes = 'skip';
1069 }
1070 return $postTypes;
1071 case 'customPostTypes':
1072 $customPostTypes = 'skip';
1073 // if user selected the post types just return the cpt array
1074
1075 if ( is_array( $delop_filters ) && isset( $delop_filters['cpt'] ) ) {
1076 $customPostTypes = $delop_filters['cpt'];
1077 // delete post in cpt array
1078 if ( ($post_val_key = array_search( 'post', $customPostTypes )) !== false ) {
1079 unset( $customPostTypes[$post_val_key] );
1080 }
1081 // check if any post left in the cpt array
1082 if ( count( $customPostTypes ) == 0 ) {
1083 $customPostTypes = 'skip';
1084 }
1085 return $customPostTypes;
1086 }
1087
1088 case 'postCategory':
1089 // default val. for category = 0 -> no category
1090 $postCat = 0;
1091 if ( is_array( $delop_filters ) && isset( $delop_filters['post_category'] ) && is_array( $delop_filters['post_category'] ) ) {
1092 // $postCat = implode( ',', filter_var_array($delop_filters['post_category']) );
1093 $postCat = filter_var_array( $delop_filters['post_category'] );
1094 }
1095 return $postCat;
1096 case 'postAuthor':
1097 $postAuthor = 0;
1098 if ( is_array( $delop_filters ) && isset( $delop_filters['userid'] ) && is_array( $delop_filters['userid'] ) ) {
1099 $postAuthor = implode( ',', filter_var_array( $delop_filters['userid'] ) );
1100 }
1101 return $postAuthor;
1102 case 'postTax':
1103 $postTax = array();
1104 if ( isset( $cpt ) && is_string( $cpt ) && $cpt != '' ) {
1105 // check if cpt specified - needed to check if the taxonomies are registered for (belongs to) the cpt
1106
1107 if ( is_array( $delop_filters ) && isset( $delop_filters['tax_input'] ) && is_array( $delop_filters['tax_input'] ) ) {
1108 // format the taxonomy array like this
1109 // array(
1110 // 'relation' => 'AND',
1111 // array(
1112 // 'taxonomy' => 'services', // you can change it according to your taxonomy
1113 // 'field' => 'term_id', // this can be 'term_id', 'slug' & 'name'
1114 // 'terms' => $service->term_id,
1115 // )
1116 // )
1117 $relation = 'AND';
1118 if ( is_array( $delop_filters ) && isset( $delop_filters['relation'] ) ) {
1119 $relation = sanitize_text_field( $delop_filters['relation'] );
1120 }
1121 $postTax['relation'] = $relation;
1122 // use relation when are more terms (AND | OR)
1123 foreach ( $delop_filters['tax_input'] as $key => $tax_input ) {
1124 $get_obj_taxonomies = get_object_taxonomies( $cpt );
1125 if ( is_array( $tax_input ) && in_array( $key, $get_obj_taxonomies ) ) {
1126 if ( is_array( $tax_input ) ) {
1127 foreach ( $tax_input as $term ) {
1128 $postTax[] = array(
1129 'taxonomy' => $key,
1130 'field' => 'term_id',
1131 'terms' => $term,
1132 );
1133 }
1134 }
1135 }
1136 }
1137 }
1138
1139 }
1140 return $postTax;
1141 case 'relation':
1142 // get the selected relation for tax_query
1143 $relation = 'AND';
1144 if ( is_array( $delop_filters ) && isset( $delop_filters['relation'] ) ) {
1145 $relation = sanitize_text_field( $delop_filters['relation'] );
1146 }
1147 return $relation;
1148 case 'postids':
1149 $postids = array();
1150 if ( is_array( $delop_filters ) && isset( $delop_filters['postids'] ) ) {
1151 $postids = array_map( 'trim', explode( ',', sanitize_text_field( $delop_filters['postids'] ) ) );
1152 }
1153 return $postids;
1154 case 'search_keywords':
1155 $search_keywords = '';
1156 if ( is_array( $delop_filters ) ) {
1157
1158 if ( isset( $delop_filters['search_keyword'] ) && $delop_filters['search_keyword'] != '' ) {
1159 $search_keywords = sanitize_text_field( $delop_filters['search_keyword'] );
1160 // check if negative keywords exists
1161 if ( isset( $delop_filters['search_keyword_negativ'] ) && $delop_filters['search_keyword_negativ'] != '' ) {
1162 $search_keywords = $search_keywords . ' -' . sanitize_text_field( $delop_filters['search_keyword_negativ'] );
1163 }
1164 }
1165
1166 }
1167 return $search_keywords;
1168 }
1169 return '';
1170 }
1171
1172 /**
1173 * get the old posts to delete array
1174 */
1175 function getAltestPostsObj( $nrOfPostsToGet )
1176 {
1177 $altPostsFinalArray = array();
1178 // get the default posts list
1179 $altPostsArray = $this->getAltestPostObj( $nrOfPostsToGet );
1180 // get the custom posts list
1181 $altCustomPostsArray = $this->getAltestCustomPostObj( $nrOfPostsToGet );
1182 // combine the lists toghether
1183 if ( is_array( $altPostsArray ) ) {
1184 foreach ( $altPostsArray as $oldPost ) {
1185 $altPostsFinalArray[] = $oldPost;
1186 }
1187 }
1188 if ( is_array( $altCustomPostsArray ) ) {
1189 foreach ( $altCustomPostsArray as $oldCustomPost ) {
1190 $altPostsFinalArray[] = $oldCustomPost;
1191 }
1192 }
1193 // get user saved options
1194 $getOptionObject = get_option( 'deloldp-post-days-option' );
1195 // get the list of deleted posts without deleting them
1196 $postsDeleted = $this->deletePosts( $altPostsFinalArray, $getOptionObject, false );
1197 if ( !is_array( $postsDeleted ) ) {
1198 $postsDeleted = array();
1199 }
1200 // if NULL returned
1201 return $postsDeleted;
1202 }
1203
1204 /**
1205 * retrieve the number of post to delete
1206 */
1207 function getNumberOfPosts()
1208 {
1209 global $dop_fs ;
1210 $nrOfPostsToGet = 1;
1211
1212 if ( $dop_fs->can_use_premium_code() ) {
1213 $getOptionObject = get_option( 'deloldp-post-days-option' );
1214 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) ) {
1215 if ( property_exists( $getOptionObject->params, 'deloldpPostsNr' ) ) {
1216 $nrOfPostsToGet = $getOptionObject->params->deloldpPostsNr;
1217 }
1218 }
1219 }
1220
1221 return $nrOfPostsToGet;
1222 }
1223
1224 /**
1225 * log var_dump errors
1226 * you can only echo the results you have to capture the output buffer with ob_start(), assign it to a variable,
1227 * and then clean the buffer with ob_end_clean() allowing you to write the resulting variable to the error_log
1228 */
1229 function var_error_log( $object = null )
1230 {
1231 ob_start();
1232 // start buffer capture
1233 var_dump( $object );
1234 // dump the values
1235 $contents = ob_get_contents();
1236 // put the buffer into a variable
1237 ob_end_clean();
1238 // end capture
1239 error_log( $contents );
1240 // log contents of the result of var_dump( $object )
1241 }
1242
1243 /**
1244 * delete the posts
1245 * @param array $altPostsArray = array of posts selected from DB
1246 * @param object $getOptionObject = saved plugin options
1247 * @param true|false $action -> true = delete the posts | false = test (return the list of posts to delete)
1248 */
1249 function deletePosts( $altPostsArray, $getOptionObject, $action = true )
1250 {
1251 global $dop_fs ;
1252 // get the user delete days
1253 $postDeleteDaysTime = $this->userDaysToTimestamp();
1254 /** check if number of days was set */
1255 if ( !$postDeleteDaysTime ) {
1256 return;
1257 }
1258 // posts nr. of days to be deleted not defined - exit
1259 // error_log("Time: " . date("d M Y", $postDeleteDaysTime));
1260
1261 if ( isset( $altPostsArray ) && is_array( $altPostsArray ) ) {
1262 $i = 0;
1263 $deletedPostsArray = array();
1264 // get saved attached_img option
1265 $delop_filters = get_option( 'delop_filters' );
1266 $attached_img_opt = ( is_array( $delop_filters ) && isset( $delop_filters['attached_img'] ) ? $delop_filters['attached_img'] : '' );
1267 foreach ( $altPostsArray as $altPostDataObj ) {
1268
1269 if ( is_object( $altPostDataObj ) ) {
1270 $altPostTimestamp = strtotime( $altPostDataObj->post_date );
1271
1272 if ( $postDeleteDaysTime > $altPostTimestamp ) {
1273 // save the last entry for log
1274 if ( $i == count( $altPostsArray ) - 1 ) {
1275 if ( $action ) {
1276 update_option( 'post_delete_log_array', array(
1277 'deleted_post_id' => $altPostDataObj->ID,
1278 'deleted_post_timestamp' => $altPostTimestamp,
1279 'deleted_post_title' => $altPostDataObj->post_title,
1280 'deleted_post_name' => $altPostDataObj->post_name,
1281 'deleted_post_url' => $altPostDataObj->guid,
1282 ) );
1283 }
1284 }
1285 $i++;
1286 // ======== delete post =========
1287 $deletePostSkipTrash = false;
1288
1289 if ( $dop_fs->can_use_premium_code() ) {
1290 // check if skipTrash is set
1291 $skipTrashOption = 0;
1292 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) ) {
1293 if ( $getOptionObject->params->deloldpSkiptrash ) {
1294 $skipTrashOption = $getOptionObject->params->deloldpSkiptrash;
1295 }
1296 }
1297 if ( $skipTrashOption == 1 ) {
1298 $deletePostSkipTrash = true;
1299 }
1300 }
1301
1302 // delete the post
1303 if ( isset( $altPostDataObj->ID ) ) {
1304
1305 if ( $action ) {
1306
1307 if ( $deletePostSkipTrash ) {
1308 $deletePostResult = wp_delete_post( (int) $altPostDataObj->ID, $deletePostSkipTrash );
1309 } else {
1310 $deletePostResult = wp_trash_post( (int) $altPostDataObj->ID );
1311 }
1312
1313 } else {
1314 /**
1315 * test -> create the array of deleted posts to return without actually deleting them
1316 */
1317 $deletedPostsArray[] = $altPostDataObj;
1318 }
1319
1320 }
1321 /**
1322 * Save deleted post data in an option to redirect it later to a similar post if old post URL called
1323 */
1324 if ( $action ) {
1325 $this->saveToRedirectOpt( $altPostDataObj );
1326 }
1327 }
1328
1329 }
1330
1331 }
1332 if ( !$action ) {
1333 // return the array with posts that normally would have been deleted
1334 return $deletedPostsArray;
1335 }
1336 }
1337
1338 }
1339
1340 /**
1341 * Return the Filter's form Post Vars.
1342 */
1343 function getFiltersOpt( $whatToReturn = '', $nestedarray = '' )
1344 {
1345 // delete_option('delop_filters');
1346 // get filters options saved values
1347 $delop_filters = get_option( 'delop_filters' );
1348 if ( isset( $delop_filters[trim( $whatToReturn )] ) & $nestedarray == '' ) {
1349 return $delop_filters[trim( $whatToReturn )];
1350 }
1351 if ( isset( $delop_filters[trim( $whatToReturn )][trim( $nestedarray )] ) & $nestedarray != '' ) {
1352 return $delop_filters[trim( $whatToReturn )][trim( $nestedarray )];
1353 }
1354 // if the options are empty return the default post type
1355 if ( empty($delop_filters) && $whatToReturn == 'cpt' ) {
1356 return array( 'post' );
1357 }
1358 // no data saved for the request
1359 return false;
1360 }
1361
1362 /**
1363 * Check if we are on specific page
1364 */
1365 function delop_checkIfPage( $requestedPage )
1366 {
1367 global $wp ;
1368 /**
1369 * get the page url
1370 */
1371 $current_url = esc_url( home_url( add_query_arg( $_GET, $wp->request ) ) );
1372 /**
1373 * check if $_POST in the filter page
1374 */
1375 if ( preg_match( '/page=' . $requestedPage . '/i', $current_url ) ) {
1376 // we are on the page
1377 return true;
1378 }
1379 // not the requested page
1380 return false;
1381 }
1382
1383 }