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