PluginProbe ʕ •ᴥ•ʔ
Auto Post Cleaner / trunk
Auto Post Cleaner vtrunk
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-filters.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-filters.php
678 lines
1 <?php
2
3 namespace DEL\OLD\Posts\Cls;
4
5 /**
6 * Make the plugin class.
7 */
8 class Delete_Old_Posts_Filters extends Delete_Old_Posts {
9 private $colors;
10
11 private $bcolors;
12
13 /**
14 * Hooks init (nothing else) and calls things that need to run right away.
15 */
16 public function __construct() {
17 add_action( 'admin_menu', [$this, 'deloldp_custom_menu_page'] );
18 $this->colors = array(
19 'decoration-sky-600 text-sky-600',
20 'decoration-red-600 text-red-600',
21 'decoration-green-600 text-green-600',
22 'decoration-orange-600 text-orange-600',
23 'decoration-indigo-600 text-indigo-600',
24 'decoration-rose-600 text-rose-600',
25 'decoration-purple-600 text-purple-600',
26 'decoration-stone-600 text-stone-600',
27 'decoration-yellow-600 text-yellow-600',
28 'decoration-lime-600 text-lime-600'
29 );
30 $this->bcolors = array(
31 'border-sky-200',
32 'border-red-200',
33 'border-green-200',
34 'border-orange-200',
35 'border-indigo-200',
36 'border-rose-200',
37 'border-purple-200',
38 'border-stone-200',
39 'border-yellow-200',
40 'border-lime-200'
41 );
42 }
43
44 /**
45 * add a custom menu in admin menu
46 */
47 function deloldp_custom_menu_page() {
48 // Add submenu page with same slug as parent to ensure no duplicates
49 $deloldp_filters_submenu = add_submenu_page(
50 'delete-old-posts',
51 esc_html__( 'Filters - Auto Post Cleaner' ),
52 esc_html__( 'Filters', 'delete-old-posts' ),
53 'manage_options',
54 'delete-old-posts-filters',
55 [$this, 'deloldpFilters']
56 );
57 }
58
59 /**
60 * Create filters page
61 */
62 function deloldpFilters() {
63 /**
64 * Handle the Filter's form data saving
65 */
66 $this->filtersFormSave();
67 ?>
68 <section class="mx-4 my-8 delop" x-data="deloldp_Start()" x-init="onstart()">
69 <div class="wrap mb-4">
70 <h2><?php
71 esc_html_e( 'Available filters to use when deleting the posts', 'delete-old-posts' );
72 ?></h2>
73 <?php
74 // display info
75 echo $this->deloldp_makeAlert( esc_html__( "Select which filters you would like to apply when deleting your posts from the options below.", "delete-old-posts" ), "info", "is-dismissible" );
76 ?>
77 </div>
78 <div class="max-w-full bg-white border border-inherit p-8 mr-5">
79 <form method="post" action="">
80 <div class="flex flex-row flex-wrap gap-5">
81 <?php
82 if ( current_user_can( 'delete_posts' ) & current_user_can( 'delete_others_posts' ) ) {
83 esc_html_e( "\n Once you've decided on the number of days in the past that posts should automatically be deleted, you can refine the criteria for deletion even further. If you want to delete posts of only one type, posts in specific categories, or posts with one or more taxonomies (for custom post types), simply select the appropriate options below. Any posts published before the date you have chosen will then be filtered to make sure only those meeting your additional criteria are deleted. This way, you can be sure that only the posts you want removed are deleted.\n ", "delete-old-posts" );
84 $this->delop_get_form_input( 'cpt' );
85 $this->delop_get_form_input( 'cpt_type' );
86 if ( current_user_can( 'manage_categories' ) ) {
87 $this->delop_get_form_input( 'categories' );
88 $this->delop_get_form_input( 'relation' );
89 }
90 if ( current_user_can( 'list_users' ) ) {
91 $this->delop_get_form_input( 'users' );
92 }
93 $this->delop_get_form_input( 'postids' );
94 $this->delop_get_form_input( 'search_keywords' );
95 $this->delop_get_form_input( 'attached_img' );
96 } else {
97 esc_html_e( "You don't have rights to delete the posts.", "delete-old-posts" );
98 }
99 ?>
100 <div class="w-full flex items-center">
101 <div class="w-full text-right">
102 <button
103 type="submit"
104 class="bg-blue-500 rounded-full font-bold text-white px-4 py-3 transition duration-300 ease-in-out hover:bg-blue-600"
105 >
106 <?php
107 esc_html_e( 'Save and test the deleted posts', 'delete-old-posts' );
108 ?>
109 <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">
110 <line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/>
111 </svg>
112 </button>
113 <?php
114 wp_nonce_field( 'delop_filters_save', 'delop_nonce_filters' );
115 ?>
116 </div>
117 </div>
118 </div>
119 </form>
120 </div>
121 </section>
122 <?php
123 }
124
125 /**
126 * Handle the Filter's form change
127 */
128 function filtersFormSave() {
129 // check if filters form was just saved
130 if ( isset( $_POST['delop_nonce_filters'] ) && wp_verify_nonce( $_POST['delop_nonce_filters'], 'delop_filters_save' ) ) {
131 // sanitize $_POST vars
132 $filtersOptions = ( isset( $_POST ) ? (array) $_POST : array() );
133 $filtersOptions = filter_var_array( $filtersOptions );
134 // check if any post type selected (at least the default post type have to be selected)
135 if ( !isset( $filtersOptions['cpt'] ) || isset( $filtersOptions['cpt'] ) && empty( $filtersOptions['cpt'] ) ) {
136 // set the default post
137 $filtersOptions['cpt'] = array('post');
138 echo $this->deloldp_makeAlert( esc_html__( "At least one post type has to be selected. The default post type have been automatically selected.", "delete-old-posts" ), "warning", "is-dismissible" );
139 }
140 // save Filters option into an Option
141 update_option( 'delop_filters', $filtersOptions );
142 // show a list with deleted posts. Test the filter.
143 $this->tryFilter();
144 }
145 }
146
147 /**
148 * Create form inputs
149 */
150 function delop_get_form_input( $inputName ) {
151 global $dop_fs;
152 switch ( $inputName ) {
153 case 'cpt':
154 ?>
155 <div class="border border-inherit p-7 relative delop-filter">
156 <span class="dashicons dashicons-admin-post text-4xl mb-4"></span>
157 <?php
158 $helpTxt = sprintf( esc_html__( 'You can choose to delete only the posts from a specific custom post type, or all posts types. If not otherwise specified, only the posts from the %s will be deleted.', 'delete-old-posts' ), '<strong>' . esc_html__( "default post type", "delete-old-posts" ) . '</strong>' );
159 $this->generateHelpTooltip( $helpTxt, 'w-full text-right absolute right-1 top-1', 'text-left right-1' );
160 /**
161 * Get the list with custom post types
162 */
163 // get saved options
164 $toggle_hidden_cpt = $this->getFiltersOpt( 'toggle_hidden_cpt' );
165 $args = array(
166 '_builtin' => false,
167 );
168 if ( !$toggle_hidden_cpt ) {
169 $args['public'] = true;
170 }
171 $getCustomPostTypes = get_post_types( $args );
172 // get saved cpt if form was submited
173 $cpts = $this->getFiltersOpt( 'cpt' );
174 ?>
175 <div class='m-2 max-h-56 overflow-y-scroll'>
176 <?php
177 echo "<div class='text-base'>";
178 esc_html_e( 'Choose the post type:', 'delete-old-posts' );
179 echo "</div>";
180 echo "\n <label class='block my-2'>\n <input type='checkbox' name='cpt[]' value='post'";
181 if ( empty( $cpts ) || isset( $cpts ) && is_array( $cpts ) && array_search( 'post', $cpts ) !== false ) {
182 echo "checked";
183 }
184 echo "/>\n <span class='underline " . $this->colors[0] . " ml-1 decoration-2'>\n Post\n </span>\n </label>";
185 $cptColor = 1;
186 if ( is_array( $getCustomPostTypes ) ) {
187 foreach ( $getCustomPostTypes as $customPostType ) {
188 $checked = false;
189 if ( isset( $cpts ) && is_array( $cpts ) && array_search( $customPostType, $cpts ) !== false ) {
190 $checked = true;
191 }
192 echo "\n <label class='block my-2'>\n <input type='checkbox' name='cpt[]' value='" . $customPostType . "'";
193 if ( $checked ) {
194 echo "checked";
195 }
196 echo " />\n <span class='ml-1 ";
197 echo ( $checked && isset( $this->colors[$cptColor] ) ? $this->colors[$cptColor] . ' underline decoration-2' : '' );
198 echo "'>\n " . ucfirst( str_replace( "_", " ", $customPostType ) ) . "\n </span>\n </label>";
199 $cptColor++;
200 }
201 }
202 echo "\n </div>";
203 ?>
204 <!-- Rounded switch -->
205 <?php
206 // get saved options
207 $toggle_hidden_cpt = $this->getFiltersOpt( 'toggle_hidden_cpt' );
208 ?>
209 <label class="relative inline-flex items-center mb-5 cursor-pointer">
210 <input
211 type="checkbox"
212 id="toggle_hidden_cpt"
213 name="toggle_hidden_cpt"
214 value="1"
215 <?php
216 if ( $toggle_hidden_cpt ) {
217 echo "checked";
218 }
219 ?>
220 class="sr-only peer"
221 >
222 <div class="w-9 h-5 bg-gray-200 peer-focus:outline-none peer-focus:ring-0 peer-focus:ring-blue-300 dark:peer-focus:ring-blue-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-4 after:w-4 after:transition-all dark:border-gray-600 peer-checked:bg-blue-600"></div>
223 </label>
224 <span class="ms-3 text-sm text-gray-900 dark:text-gray-300 ml-2 leading-6 max-w-[150px] inline-block"><?php
225 _e( 'Show hidden Custom Post Types.', 'delete-old-posts' );
226 ?></span>
227 </div>
228 <?php
229 break;
230 case 'cpt_type':
231 $all_posts_statuses = get_post_stati();
232 unset($all_posts_statuses['trash'], $all_posts_statuses['preview'], $all_posts_statuses['auto-draft']);
233 ?>
234 <div class="border border-inherit p-7 relative delop-filter">
235 <span class="dashicons dashicons-post-status text-4xl mb-4"></span>
236 <?php
237 $helpTxt = esc_html__( 'You can choose to delete only the posts by a specific status. The default status used is "publish".', 'delete-old-posts' );
238 $this->generateHelpTooltip( $helpTxt, 'w-full text-right absolute right-1 top-1', 'text-left right-1' );
239 // Generate the checkboxes
240 echo "<div class='text-base'>";
241 esc_html_e( 'Choose the post status:', 'delete-old-posts' );
242 echo "</div>";
243 if ( is_array( $all_posts_statuses ) ) {
244 foreach ( $all_posts_statuses as $key => $posts_status ) {
245 $this->delopt_generate_check_box( $key, ucfirst( $posts_status ) );
246 }
247 }
248 ?>
249 </div>
250 <?php
251 break;
252 case 'categories':
253 // get the list with checked post types
254 $getCustomPostTypes = $this->getFiltersOpt( 'cpt' );
255 /**
256 * Get all CPT
257 * Used to find the co;or index for the category box
258 */
259 $args = array(
260 'public' => true,
261 '_builtin' => false,
262 );
263 $getAllCustomPostTypes = get_post_types( $args );
264 /**
265 * display a list of checkboxes for each taxonomy
266 * create the color for the taxonomy border to be the same as color used for the related custom post
267 */
268 // get taxonomies for every checked post type
269 if ( is_array( $getCustomPostTypes ) ) {
270 foreach ( $getCustomPostTypes as $key => $cpt ) {
271 $cptTaxonomies[$key] = get_object_taxonomies( (string) $cpt );
272 }
273 }
274 // Convert multidimensional array into single array
275 $cptFlatTaxonomies = array_reduce( $cptTaxonomies, 'array_merge', array() );
276 $removeFromFlatTaxonomies = array('post_format');
277 // , 'post_tag'
278 foreach ( $removeFromFlatTaxonomies as $removeFromFlatTaxonomiesVal ) {
279 if ( array_search( $removeFromFlatTaxonomiesVal, $cptFlatTaxonomies ) !== false ) {
280 $post_format_key = array_search( $removeFromFlatTaxonomiesVal, $cptFlatTaxonomies );
281 unset($cptFlatTaxonomies[$post_format_key]);
282 }
283 }
284 // list all categories and taxonomies
285 if ( is_array( $cptFlatTaxonomies ) ) {
286 foreach ( $cptFlatTaxonomies as $category ) {
287 /**
288 * check if category is registered for the current cpt
289 * Get the CP array index
290 */
291 $cptIndex = $this->delop_find_parent( $cptTaxonomies, $category );
292 /**
293 * find border color index for the box
294 * need to be the same as the color of CPT in the list
295 */
296 $getCPTKeyInAllCPTArray = $this->countArrayUntilTarget( $getAllCustomPostTypes, $getCustomPostTypes[$cptIndex] );
297 // color index will be incremented with 1 (post is not in the CPT array so will need to be counted too)
298 $borderColorIndex = ( $category != 'category' ? $getCPTKeyInAllCPTArray + 1 : 0 );
299 ?>
300 <div class="border p-7 relative max-w-xs <?php
301 echo ( isset( $this->bcolors[$borderColorIndex] ) ? $this->bcolors[$borderColorIndex] : '' );
302 ?> delop-filter">
303 <span class="dashicons dashicons-category text-4xl mb-4"></span>
304 <?php
305 $titleText = 'Choose taxonomies to filter the deleted %s.';
306 $helpText = 'You can choose to delete only the custom posts with specific taxonomies. If no taxonomy is selected, the custom posts with %s will be deleted.';
307 $strongText = "any taxonomy";
308 if ( $category == 'category' ) {
309 $titleText = 'Choose categories to filter the deleted posts.';
310 $helpText = 'You can choose to delete only the posts from specific categories. If selected, only post from the selected category (categories) will be deleted. If no category is selected, the posts from %s will be deleted.';
311 $strongText = "any category";
312 }
313 $helpTxt = sprintf( $helpText, '<strong>' . esc_html__( $strongText, "delete-old-posts" ) . '</strong>' );
314 $this->generateHelpTooltip( $helpTxt, 'w-full text-right absolute right-1 top-1', 'text-left right-1' );
315 ?>
316 <div class="mb-2 text-base">
317 <?php
318 $custompostname = ucfirst( str_replace( "_", " ", $getCustomPostTypes[$cptIndex] ) );
319 $getTaxName = get_taxonomy_labels( get_taxonomy( $category ) );
320 echo sprintf( esc_html__( $titleText, "delete-old-posts" ), "<strong>" . esc_html__( $custompostname, "delete-old-posts" ) . "</strong>" );
321 echo "<br /><strong>" . $getTaxName->name . "</strong> ";
322 esc_html_e( "list:", 'delete-old-posts' );
323 ?>
324 </div>
325 <div class="max-h-56 overflow-auto">
326 <?php
327 $choosedCats = ( $category == 'category' ? $this->getFiltersOpt( 'post_category' ) : $this->getFiltersOpt( 'tax_input', $category ) );
328 $choosedCats = ( is_array( $choosedCats ) ? $choosedCats : false );
329 /**
330 * Display checkboxes with categories
331 */
332 $cat = get_taxonomy( $category );
333 if ( current_user_can( $cat->cap->assign_terms ) ) {
334 // get selected categories if form was previous saved
335 $args = array(
336 'taxonomy' => $category,
337 'hierarchical' => true,
338 'title_li' => '',
339 'hide_empty' => false,
340 'selected_cats' => $choosedCats,
341 'popular_cats' => false,
342 'checked_ontop' => true,
343 );
344 wp_terms_checklist( $post_id = 0, $args );
345 }
346 ?>
347 </div>
348 </div>
349 <?php
350 }
351 }
352 break;
353 case 'users':
354 // get saved user filter if form was submited
355 $usersFilter = $this->getFiltersOpt( 'userid' );
356 // get all users
357 $users = get_users( array(
358 'fields' => array('ID', 'display_name'),
359 ) );
360 // sort users alphabeticaly
361 usort( $users, function ( $a, $b ) {
362 return strcasecmp( $a->display_name, $b->display_name );
363 } );
364 // list users
365 ?>
366 <div class='border border-inherit p-7 relative max-w-xs delop-filter'>
367 <span class="dashicons dashicons-admin-users text-4xl mb-4"></span>
368 <?php
369 $helpTxt = sprintf( esc_html__( 'You can choose to delete only the posts from specific users. By default (no user selected), posts from %s will be deleted.', 'delete-old-posts' ), '<strong>' . esc_html__( "any user", "delete-old-posts" ) . '</strong>' );
370 $this->generateHelpTooltip( $helpTxt, 'w-full text-right absolute right-1 top-1', 'text-left right-1' );
371 ?>
372 <div class='m-2'>
373 <?php
374 echo "<div class='text-base'>";
375 esc_html_e( 'Delete only the posts from specific users:', 'delete-old-posts' );
376 echo "</div>";
377 echo "\n <div class='max-h-56'>\n <select id='userid' name='userid' data-placeholder='Choose users...' multiple data-multi-select>";
378 foreach ( $users as $user ) {
379 $userObj = get_user_by( 'ID', $user->ID );
380 // echo "<label class='block my-2'><input type='checkbox' name='userid[]' value='".$user->ID."'"; if( isset($usersFilter) && is_array($usersFilter) && array_search($user->ID, $usersFilter) !== false ) echo "checked"; echo "/> ".$userObj->display_name."</label>";
381 echo "<option value='" . $user->ID . "'";
382 if ( isset( $usersFilter ) && is_array( $usersFilter ) && array_search( $user->ID, $usersFilter ) !== false ) {
383 echo " selected";
384 }
385 echo ">" . $userObj->display_name . "</option>";
386 }
387 echo "\n </select>\n </div>";
388 ?>
389 </div>
390 </div>
391 <?php
392 break;
393 case 'relation':
394 // get saved relation filter if form was submited
395 $relationFilter = $this->getFiltersOpt( 'relation' );
396 if ( empty( $relationFilter ) ) {
397 $relationFilter = 'OR';
398 }
399 ?>
400 <div class='border border-inherit p-7 relative max-w-xs delop-filter'>
401 <span class="dashicons dashicons-forms text-4xl mb-4"></span>
402 <?php
403 $helpTxt = sprintf( esc_html__( 'Choose the relation applied to selected categories or terms. By default (no relation selected), %s will be used. "All selected categories or terms" mean that the deleted post will need to have relation with all selected categories or terms. "Any selected categories or terms" mean that if the post has relationship with only one of the selected terms, then the post will be deleted.', 'delete-old-posts' ), '<strong>' . esc_html__( "all selected terms", "delete-old-posts" ) . '</strong>' );
404 $this->generateHelpTooltip( $helpTxt, 'w-full text-right absolute right-1 top-1', 'text-left right-1' );
405 ?>
406 <div class='m-2'>
407 <?php
408 echo "<div class='text-base'>";
409 esc_html_e( 'Choose the relation applied to selected categories or taxonomies terms:', 'delete-old-posts' );
410 echo "</div>";
411 echo "<label class='block my-2'><input type='radio' name='relation' value='AND'";
412 if ( isset( $relationFilter ) && $relationFilter == 'AND' ) {
413 echo "checked";
414 }
415 echo "/>" . sprintf( esc_html__( "All selected categories or terms (post will need to be in %s selected categories or terms)", "delete-old-posts" ), '<strong>' . esc_html__( 'ALL', 'delete-old-posts' ) . '</strong>' ) . "</label>";
416 echo "<label class='block my-2'><input type='radio' name='relation' value='OR'";
417 if ( isset( $relationFilter ) && $relationFilter == 'OR' ) {
418 echo "checked";
419 }
420 echo "/>" . sprintf( esc_html__( "Any selected categories or terms (post will need to be in %s selected categories or terms)", "delete-old-posts" ), '<strong>' . esc_html__( 'ANY', 'delete-old-posts' ) . '</strong>' ) . "</label>";
421 ?>
422 </div>
423 </div>
424 <?php
425 break;
426 case 'postids':
427 ?>
428 <div class="border border-inherit p-7 relative max-w-xs delop-filter">
429 <span class="dashicons dashicons-portfolio text-4xl mb-4"></span>
430 <?php
431 // get saved post ids if form was submited
432 $postids = $this->getFiltersOpt( 'postids' );
433 $helpTxt = sprintf( esc_html__( 'If you have some posts that you %s, write the posts Ids in the text box separated with coma (ex. 1009, 2345, 4563). You can find the post id in the browser location when you edit the post (ex. ?post=2033).', 'delete-old-posts' ), '<strong>' . esc_html__( "don't want to be deleted", "delete-old-posts" ) . '</strong>' );
434 $this->generateHelpTooltip( $helpTxt, 'w-full text-right absolute right-1 top-1', 'text-left right-1' );
435 ?>
436 <div class="m-2">
437 <?php
438 echo "<div class='text-base'>";
439 esc_html_e( 'Have some important posts? Enter the post IDs that you want to keep:', 'delete-old-posts' );
440 echo "</div>";
441 ?>
442 <label class="block my-2 mt-8">
443 <span><?php
444 esc_html_e( "Post IDs to exclude:", "delete-old-posts" );
445 ?></span>
446 <input class="w-full" type="text" name="postids" value="<?php
447 echo sanitize_text_field( $postids );
448 ?>" />
449 </label>
450 </div>
451 </div>
452 <?php
453 break;
454 case 'search_keywords':
455 ?>
456 <div class="border border-inherit p-7 relative max-w-xs delop-filter">
457 <span class="dashicons dashicons-search text-4xl mb-4"></span>
458 <?php
459 // get saved post ids if form was submited
460 $search_keyword = $this->getFiltersOpt( 'search_keyword' );
461 $search_keyword_negativ = $this->getFiltersOpt( 'search_keyword_negativ' );
462 $helpTxt = sprintf( esc_html__( 'If you want delete only some posts containing some %s, write the keywords to search in the posts here (ex. pillow). If you want to exclude some posts from the search, write your keywords in the "Negative keywords" field below. Eg, "Look for posts that include" = "pillow" and "Exclude from search results" = "sofa", will return posts containing "pillow" but not "sofa".', 'delete-old-posts' ), '<strong>' . esc_html__( "keywords", "delete-old-posts" ) . '</strong>' );
463 $this->generateHelpTooltip( $helpTxt, 'w-full text-right absolute right-1 top-1', 'text-left right-1' );
464 ?>
465 <div class="m-2">
466 <?php
467 echo "<div class='text-base'>";
468 esc_html_e( 'Delete only posts that contain the keyword(s):', 'delete-old-posts' );
469 echo "</div>";
470 ?>
471 <label class="block my-2">
472 <span><?php
473 esc_html_e( "Look for posts that include the keyword(s):", "delete-old-posts" );
474 ?></span>
475 <input class="w-full" type="text" name="search_keyword" value="<?php
476 echo sanitize_text_field( $search_keyword );
477 ?>" />
478 </label>
479 <label class="block my-2">
480 <span><?php
481 esc_html_e( "Exclude from search results any posts that contain the keyword(s):", "delete-old-posts" );
482 ?></span>
483 <input class="w-full" type="text" name="search_keyword_negativ" value="<?php
484 echo sanitize_text_field( $search_keyword_negativ );
485 ?>" />
486 </label>
487 </div>
488 </div>
489 <?php
490 break;
491 case 'attached_img':
492 ?>
493 <div class="border border-inherit p-7 relative max-w-xs delop-filter">
494 <span class="dashicons dashicons-images-alt2 text-4xl mb-4"></span>
495 <?php
496 // get saved attached_img option if form was submited
497 $attached_img = $this->getFiltersOpt( 'attached_img' );
498 $force_delete_attached_img = $this->getFiltersOpt( 'force_delete_attached_img' );
499 $helpTxt = sprintf( esc_html__( 'If you also want to delete the %s attached to the post, select this option. When the post is deleted, the post thumbnail and all files attached to it will be deleted as well. Use this option carefully because there is no guarantee that the attachment isn\'t still published in some other post (ex. picture galleries).', 'delete-old-posts' ), '<strong>' . esc_html__( "featured image and all files", "delete-old-posts" ) . '</strong>' );
500 $this->generateHelpTooltip( $helpTxt, 'w-full text-right absolute right-1 top-1', 'text-left right-1' );
501 ?>
502 <div class="m-2">
503 <?php
504 echo "<div class='text-base'>";
505 esc_html_e( 'Do you want to delete the images attached to the post (featured image and all files attached to it) when the post is deleted? Note: Attachments will not be deleted if used in another post (use "force delete" for that).', 'delete-old-posts' );
506 echo "</div>";
507 ?>
508 <label class="block my-2">
509 <span class="font-bold">
510 <?php
511 esc_html_e( "Delete all files attached to post:", "delete-old-posts" );
512 ?>
513 </span>
514 <input class="w-full" type="checkbox" name="attached_img" value="1" <?php
515 if ( !$dop_fs->can_use_premium_code() ) {
516 echo "disabled";
517 }
518 ?> <?php
519 if ( $attached_img == 1 ) {
520 echo "checked";
521 }
522 ?> />
523 </label>
524 <label class="block my-2">
525 <span class="font-bold">
526 <?php
527 esc_html_e( "Force delete attached files:", "delete-old-posts" );
528 ?>
529 </span>
530 <input
531 class="w-full"
532 type="checkbox"
533 @click="confirmForceDelete()"
534 id="forcedeleteattachedimg"
535 name="force_delete_attached_img"
536 value="1"
537 <?php
538 if ( !$dop_fs->can_use_premium_code() ) {
539 echo "disabled";
540 }
541 ?>
542 <?php
543 if ( $force_delete_attached_img == 1 ) {
544 echo "checked";
545 }
546 ?>
547 />
548 </label>
549 <?php
550 if ( !$dop_fs->can_use_premium_code() ) {
551 echo '
552 <section class="text-red-600">' . esc_html__( 'This option is available in the Professional version.', 'delete-old-posts' );
553 echo '
554 <a href="' . $dop_fs->get_upgrade_url() . '">' . '<u>' . esc_html__( 'Upgrade and activate it.', 'delete-old-posts' ) . '</u>' . '</a>';
555 echo '
556 </section>';
557 }
558 ?>
559 </div>
560 </div>
561 <?php
562 break;
563 }
564 }
565
566 /**
567 * try the filter and see the results
568 */
569 function tryFilter() {
570 global $dop_fs;
571 // get the posts to delete
572 $nrOfPostsToGet = $this->getNumberOfPosts();
573 $altPostsArray = $this->getAltestPostsObj( $nrOfPostsToGet );
574 ?>
575 <div class="p-5 bg-amber-50">
576 <div class="text-base mb-2">
577 <?php
578 ( !empty( $altPostsArray ) ? esc_html_e( 'The following ', 'delete-old-posts' ) : esc_html_e( 'No ', 'delete-old-posts' ) );
579 esc_html_e( "posts will be automatically deleted when the next scheduled cron job runs.", "delete-old-posts" );
580 // check if strat deleteing the post option is on off, then show a message to set it on on
581 $toggledelete = false;
582 $getOptionObject = get_option( 'deloldp-post-days-option' );
583 // get user saved options
584 if ( is_object( $getOptionObject ) && property_exists( $getOptionObject, 'params' ) ) {
585 if ( property_exists( $getOptionObject->params, 'toggledelete' ) ) {
586 if ( $getOptionObject->params->toggledelete == 1 ) {
587 $toggledelete = true;
588 }
589 }
590 }
591 if ( !$toggledelete ) {
592 esc_html_e( " Don't forget to set the \"Start deleting posts\" option ON, to start deleting the post automatically in the background.", "delete-old-posts" );
593 }
594 ?>
595 </div>
596 <?php
597 // get saved attached_img option
598 $attached_img_opt = $this->getFiltersOpt( 'attached_img' );
599 foreach ( $altPostsArray as $altPostDataObj ) {
600 if ( is_object( $altPostDataObj ) ) {
601 echo 'Post ' . 'ID <a href="' . get_permalink( $altPostDataObj->ID ) . '" target="_blank">' . $altPostDataObj->ID . '</a> - ' . $altPostDataObj->post_title . ' (' . $altPostDataObj->post_status . ': ' . date( "d M Y H:i", strtotime( $altPostDataObj->post_date ) ) . ')<br />';
602 }
603 }
604 ?>
605 </div>
606 <?php
607 }
608
609 /**
610 * count how many elemnts before a target is reached in an array
611 * @param $haystack - array to search
612 * @param $target - value to search
613 */
614 function countArrayUntilTarget( $haystack, $target ) {
615 $total = 0;
616 // check if target exists in array
617 if ( !array_search( $target, $haystack ) ) {
618 return 0;
619 }
620 // if target exists count the array elements until target
621 foreach ( $haystack as $key => $value ) {
622 if ( $value == $target ) {
623 break;
624 }
625 $total++;
626 }
627 return $total;
628 }
629
630 /**
631 * Generate a checkbox in form
632 *
633 * @param [string] $value
634 * @param [string] $label
635 * @return void
636 */
637 private function delopt_generate_check_box( $value, $label ) {
638 $checked = '';
639 // get saved cpt if form was submited
640 $cpt_type = $this->getFiltersOpt( 'cpt_type' );
641 switch ( $value ) {
642 case 'publish':
643 if ( empty( $cpt_type ) || isset( $cpt_type ) && is_array( $cpt_type ) && array_search( 'publish', $cpt_type ) !== false ) {
644 $checked = "checked";
645 }
646 break;
647 default:
648 if ( isset( $cpt_type ) && is_array( $cpt_type ) && array_search( $value, $cpt_type ) !== false ) {
649 $checked = "checked";
650 }
651 break;
652 }
653 echo "\n <label class='block my-2'>\n <input type='checkbox' name='cpt_type[]' value='" . $value . "'";
654 echo $checked;
655 echo "/>\n <span class='ml-1 decoration-2'>\n " . esc_html( $label, 'delete-old-posts' ) . "\n </span>\n </label>";
656 }
657
658 /*
659 * Find parent key of array
660 *
661 * @param [type] $array
662 * @param [type] $needle
663 * @param [type] $parent
664 * @return void
665 */
666 private function delop_find_parent( $array, $needle, $parent = null ) {
667 foreach ( $array as $key => $value ) {
668 $ind = array_search( $needle, $value );
669 if ( $ind !== false ) {
670 return $key;
671 break;
672 }
673 }
674 return false;
675 }
676
677 }
678