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