class-delete-old-posts-filters.php
3 years ago
class-delete-old-posts-redirects.php
3 years ago
class-delete-old-posts.php
3 years ago
class-enqueue-assets.php
3 years ago
class-delete-old-posts-filters.php
551 lines
| 1 | <?php |
| 2 | namespace DEL\OLD\Posts\Cls; |
| 3 | |
| 4 | /** |
| 5 | * Make the plugin class. |
| 6 | */ |
| 7 | class Delete_Old_Posts_Filters extends Delete_Old_Posts { |
| 8 | |
| 9 | private $colors; |
| 10 | private $bcolors; |
| 11 | |
| 12 | /** |
| 13 | * Hooks init (nothing else) and calls things that need to run right away. |
| 14 | */ |
| 15 | public function __construct(){ |
| 16 | add_action( 'admin_menu', [ $this, 'deloldp_custom_menu_page' ] ); |
| 17 | $this->colors = array( |
| 18 | 'decoration-sky-600 text-sky-600', |
| 19 | 'decoration-red-600 text-red-600', |
| 20 | 'decoration-green-600 text-green-600', |
| 21 | 'decoration-orange-600 text-orange-600', |
| 22 | 'decoration-indigo-600 text-indigo-600', |
| 23 | 'decoration-rose-600 text-rose-600', |
| 24 | 'decoration-purple-600 text-purple-600', |
| 25 | 'decoration-stone-600 text-stone-600', |
| 26 | 'decoration-yellow-600 text-yellow-600', |
| 27 | 'decoration-lime-600 text-lime-600', |
| 28 | ); |
| 29 | $this->bcolors = array( |
| 30 | 'border-sky-200', |
| 31 | 'border-red-200', |
| 32 | 'border-green-200', |
| 33 | 'border-orange-200', |
| 34 | 'border-indigo-200', |
| 35 | 'border-rose-200', |
| 36 | 'border-purple-200', |
| 37 | 'border-stone-200', |
| 38 | 'border-yellow-200', |
| 39 | 'border-lime-200', |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * add a custom menu in admin menu |
| 45 | */ |
| 46 | function deloldp_custom_menu_page() { |
| 47 | |
| 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 | 'Filters - Delete old posts automatically', |
| 52 | 'Filters', |
| 53 | 'manage_options', |
| 54 | esc_attr__('delete-old-posts-filters', 'delete-old-posts'), |
| 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"> |
| 69 | <div class="wrap mb-4"> |
| 70 | <h2><?php esc_html_e( 'Available filters to use when deleting the posts', 'delete-old-posts' ); ?></h2> |
| 71 | <?php |
| 72 | // display info |
| 73 | echo $this->deloldp_makeAlert( esc_html__("Choose in the options below what filters to apply when deleting the posts.", "delete-old-posts"), "info", "is-dismissible" ); |
| 74 | ?> |
| 75 | </div> |
| 76 | <div class="max-w-full bg-white border border-inherit p-8 mr-5"> |
| 77 | <form method="post" action=""> |
| 78 | <div class="flex flex-row flex-wrap gap-5"> |
| 79 | <?php |
| 80 | if( current_user_can( 'delete_posts' ) & current_user_can( 'delete_others_posts' ) ) { |
| 81 | esc_html_e(" |
| 82 | After you decide the number of days in the past for the posts to be automatically deleted, you can choose more options for the deleted posts here. |
| 83 | If you are looking to delete only one type of post, or only the posts in some categories, or having one or more taxonomies (for custom post types), |
| 84 | then select in the options below what would you like to apply. |
| 85 | Then, when deleting the posts published before the selected date, all selected options below will also have to apply for the post. |
| 86 | This way, you can filter the deleted posts and delete only the posts for which the selected options apply. |
| 87 | ", "delete-old-posts"); |
| 88 | $this->delop_get_form_input( 'cpt' ); |
| 89 | if( current_user_can( 'manage_categories' ) ){ |
| 90 | $this->delop_get_form_input( 'categories' ); |
| 91 | $this->delop_get_form_input( 'relation' ); |
| 92 | } |
| 93 | if( current_user_can( 'list_users' ) ) |
| 94 | $this->delop_get_form_input( 'users' ); |
| 95 | $this->delop_get_form_input( 'postids' ); |
| 96 | $this->delop_get_form_input( 'search_keywords' ); |
| 97 | $this->delop_get_form_input( 'attached_img' ); |
| 98 | } else { |
| 99 | esc_html_e("You don't have rights to delete the posts.", "delete-old-posts"); |
| 100 | } |
| 101 | ?> |
| 102 | <div class="w-full flex items-center"> |
| 103 | <div class="w-full text-right"> |
| 104 | <button |
| 105 | type="submit" |
| 106 | class="bg-blue-500 rounded-full font-bold text-white px-4 py-3 transition duration-300 ease-in-out hover:bg-blue-600" |
| 107 | > |
| 108 | <?php esc_html_e('Save and test the deleted posts.', 'delete-old-posts'); ?> |
| 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 | |
| 135 | // save Filters option into an Option |
| 136 | update_option('delop_filters', $filtersOptions); |
| 137 | |
| 138 | // show a list with deleted posts. Test the filter. |
| 139 | $this->tryFilter(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Return the Filter's form Post Vars. |
| 145 | */ |
| 146 | function getFiltersOpt( $whatToReturn = '', $nestedarray = '' ){ |
| 147 | // delete_option('delop_filters'); |
| 148 | if( $this->delop_checkIfPage( 'delete-old-posts-filters' ) ) { |
| 149 | // we are on the filter's page |
| 150 | // get filters options saved values |
| 151 | $delop_filters = get_option('delop_filters'); |
| 152 | |
| 153 | if( isset($delop_filters[ trim($whatToReturn) ]) & $nestedarray == '' ) return $delop_filters[ trim($whatToReturn) ]; |
| 154 | if( isset($delop_filters[ trim($whatToReturn) ][ trim($nestedarray) ]) & $nestedarray != '' ) return $delop_filters[ trim($whatToReturn) ][ trim($nestedarray) ]; |
| 155 | |
| 156 | // no data saved for the request |
| 157 | return false; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Check if we are on specific page |
| 163 | */ |
| 164 | function delop_checkIfPage( $requestedPage ){ |
| 165 | |
| 166 | global $wp; |
| 167 | |
| 168 | /** |
| 169 | * get the page url |
| 170 | */ |
| 171 | $current_url = esc_url( home_url( add_query_arg($_GET, $wp->request) ) ); |
| 172 | /** |
| 173 | * check if $_POST in the filter page |
| 174 | */ |
| 175 | if( preg_match( '/page='.$requestedPage.'/i', $current_url ) ) { |
| 176 | // we are on the page |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | // not the requested page |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Create form inputs |
| 186 | */ |
| 187 | function delop_get_form_input( $inputName ){ |
| 188 | |
| 189 | global $dop_fs; |
| 190 | |
| 191 | switch( $inputName ){ |
| 192 | case 'cpt': ?> |
| 193 | <div class="border border-inherit p-7 relative delop-filter"> |
| 194 | <span class="dashicons dashicons-admin-post text-4xl mb-4"></span> |
| 195 | <?php |
| 196 | $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>' ); |
| 197 | $this->generateHelpTooltip( $helpTxt, 'w-full text-right absolute right-1 top-1', 'text-left right-1' ); |
| 198 | /** |
| 199 | * Get the list with custom post types |
| 200 | */ |
| 201 | $args = array( |
| 202 | 'public' => true, |
| 203 | '_builtin' => false |
| 204 | ); |
| 205 | $getCustomPostTypes = get_post_types($args); |
| 206 | // get saved cpt if form was submited |
| 207 | $cpts = $this->getFiltersOpt( 'cpt' ); |
| 208 | ?> |
| 209 | <div class='m-2'> |
| 210 | <?php |
| 211 | echo "<div class='text-base'>"; esc_html_e( 'Choose the post type:', 'delete-old-posts' ); echo "</div>"; |
| 212 | echo " |
| 213 | <label class='block my-2'> |
| 214 | <input type='checkbox' name='cpt[]' value='post'"; if( empty($cpts) || (isset($cpts) && is_array($cpts) && array_search('post', $cpts) !== false) ) echo "checked"; echo "/> |
| 215 | <span class='underline ".$this->colors[0]." ml-1 decoration-2'> |
| 216 | Post |
| 217 | </span> |
| 218 | </label>"; |
| 219 | $cptColor = 1; |
| 220 | if( is_array($getCustomPostTypes) ) foreach( $getCustomPostTypes as $customPostType ){ |
| 221 | $checked = false; |
| 222 | if( isset($cpts) && is_array($cpts) && array_search($customPostType, $cpts) !== false ) $checked = true; |
| 223 | echo " |
| 224 | <label class='block my-2'> |
| 225 | <input type='checkbox' name='cpt[]' value='".$customPostType."'"; if( $checked ) echo "checked"; echo " /> |
| 226 | <span class='ml-1 "; echo ( $checked && isset($this->colors[$cptColor]) ) ? $this->colors[$cptColor] . ' underline decoration-2' : ''; echo "'> |
| 227 | " . ucfirst(str_replace("_", " ", $customPostType)) . " |
| 228 | </span> |
| 229 | </label>"; |
| 230 | $cptColor++; |
| 231 | } |
| 232 | echo " |
| 233 | </div>"; |
| 234 | ?> |
| 235 | </div> |
| 236 | <?php |
| 237 | break; |
| 238 | case 'categories': |
| 239 | // get all custom taxonomies |
| 240 | $args = array( |
| 241 | 'public' => true, |
| 242 | '_builtin' => false |
| 243 | ); |
| 244 | $allCustomTaxonomies = get_taxonomies($args); |
| 245 | // add default category to taxonomies |
| 246 | $allCustomTaxonomies = array('category' => 'category') + $allCustomTaxonomies; |
| 247 | /** |
| 248 | * Get all CPT |
| 249 | * Used to find the co;or index for the category box |
| 250 | */ |
| 251 | $args = array( |
| 252 | 'public' => true, |
| 253 | '_builtin' => false |
| 254 | ); |
| 255 | $getAllCustomPostTypes = get_post_types($args); |
| 256 | /** |
| 257 | * display a list of checkboxes for each taxonomy |
| 258 | * create the color for the taxonomy border to be the same as color used for the related custom post |
| 259 | */ |
| 260 | // get the list with checked post types |
| 261 | $getCustomPostTypes = $this->getFiltersOpt( 'cpt' ); |
| 262 | // get taxonomies for every checked post type |
| 263 | if( is_array($getCustomPostTypes) ) foreach( $getCustomPostTypes as $key => $cpt ){ |
| 264 | $cptTaxonomies[$key] = get_object_taxonomies( (string) $cpt ); |
| 265 | } |
| 266 | // list all categories and taxonomies |
| 267 | if( is_array($allCustomTaxonomies) ) foreach( $allCustomTaxonomies as $category ){ |
| 268 | /** |
| 269 | * check if category is registered for the current cpt |
| 270 | */ |
| 271 | $catInTaxList = false; |
| 272 | if( isset($cptTaxonomies) && is_array($cptTaxonomies) ) foreach( $cptTaxonomies as $cptIndex => $taxCPTSelected ){ |
| 273 | if( in_array($category, $taxCPTSelected) ) { |
| 274 | $catInTaxList = true; |
| 275 | break; // exit loop |
| 276 | } |
| 277 | } |
| 278 | /** |
| 279 | * show category if it is in the list of registered taxonomies for the selected post type |
| 280 | */ |
| 281 | if( $catInTaxList ){ |
| 282 | /** |
| 283 | * find border color index for the box |
| 284 | * need to be the same as the color of CPT in the list |
| 285 | */ |
| 286 | $getCPTKeyInAllCPTArray = $this->countArrayUntilTarget( $getAllCustomPostTypes, $getCustomPostTypes[$cptIndex] ); |
| 287 | // color index will be incremented with 1 (post is not in the CPT array so will need to be counted too) |
| 288 | $borderColorIndex = ( $category != 'category' ) ? $getCPTKeyInAllCPTArray + 1 : 0; |
| 289 | ?> |
| 290 | <div class="border p-7 relative max-w-xs <?php echo (isset($this->bcolors[$borderColorIndex])) ? $this->bcolors[$borderColorIndex] : ''; ?> delop-filter"> |
| 291 | <span class="dashicons dashicons-category text-4xl mb-4"></span> |
| 292 | <?php |
| 293 | $titleText = 'Choose taxonomies to filter the deleted %s.'; |
| 294 | $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.'; |
| 295 | $strongText = "any taxonomy"; |
| 296 | if( $category == 'category' ) { |
| 297 | $titleText = 'Choose categories to filter the deleted posts.'; |
| 298 | $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.'; |
| 299 | $strongText = "any category"; |
| 300 | } |
| 301 | $helpTxt = sprintf( esc_html__( $helpText, 'delete-old-posts'), '<strong>'.esc_html__($strongText, "delete-old-posts").'</strong>' ); |
| 302 | $this->generateHelpTooltip( $helpTxt, 'w-full text-right absolute right-1 top-1', 'text-left right-1' ); |
| 303 | ?> |
| 304 | <div class="mb-2 text-base"> |
| 305 | <?php |
| 306 | $custompostname = ucfirst(str_replace("_", " ", $getCustomPostTypes[$cptIndex])); |
| 307 | $getTaxName = get_taxonomy_labels(get_taxonomy($category)); |
| 308 | echo sprintf( esc_html__( $titleText, "delete-old-posts" ), "<strong>". esc_html__($custompostname, "delete-old-posts" ) ."</strong>" ); |
| 309 | echo "<br /><strong>". $getTaxName->name . "</strong> "; esc_html_e("list:", 'delete-old-posts'); |
| 310 | ?> |
| 311 | </div> |
| 312 | <div class="max-h-56 overflow-auto"> |
| 313 | <?php |
| 314 | $choosedCats = ( $category == 'category' ) ? $this->getFiltersOpt( 'post_category' ) : $this->getFiltersOpt( 'tax_input', $category ); |
| 315 | $choosedCats = ( is_array($choosedCats) ) ? $choosedCats : false; |
| 316 | /** |
| 317 | * Display checkboxes with categories |
| 318 | */ |
| 319 | $cat = get_taxonomy( $category ); |
| 320 | if( current_user_can( $cat->cap->assign_terms ) ){ |
| 321 | // get selected categories if form was previous save |
| 322 | $args = array( |
| 323 | 'taxonomy' => $category, |
| 324 | 'hierarchical' => true, |
| 325 | 'title_li' => '', |
| 326 | 'hide_empty' => false, |
| 327 | 'selected_cats' => $choosedCats, |
| 328 | 'popular_cats' => false, |
| 329 | 'checked_ontop' => true, |
| 330 | ); |
| 331 | wp_terms_checklist( $post_id = 0, $args ); |
| 332 | } |
| 333 | ?> |
| 334 | </div> |
| 335 | </div> |
| 336 | <?php |
| 337 | } |
| 338 | } |
| 339 | break; |
| 340 | case 'users': |
| 341 | // get saved user filter if form was submited |
| 342 | $usersFilter = $this->getFiltersOpt( 'userid' ); |
| 343 | // get all users |
| 344 | $users = get_users( array( 'fields' => array( 'ID' ) ) ); |
| 345 | // list users |
| 346 | ?> |
| 347 | <div class='border border-inherit p-7 relative max-w-xs delop-filter'> |
| 348 | <span class="dashicons dashicons-admin-users text-4xl mb-4"></span> |
| 349 | <?php |
| 350 | $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>' ); |
| 351 | $this->generateHelpTooltip( $helpTxt, 'w-full text-right absolute right-1 top-1', 'text-left right-1' ); |
| 352 | ?> |
| 353 | <div class='m-2'> |
| 354 | <?php |
| 355 | echo "<div class='text-base'>"; esc_html_e( 'Delete only the posts from specific users:', 'delete-old-posts' ); echo "</div>"; |
| 356 | foreach( $users as $user ) { |
| 357 | $userObj = get_user_by('ID', $user->ID); |
| 358 | 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>"; |
| 359 | } |
| 360 | ?> |
| 361 | </div> |
| 362 | </div> |
| 363 | <?php |
| 364 | break; |
| 365 | case 'relation': |
| 366 | // get saved relation filter if form was submited |
| 367 | $relationFilter = $this->getFiltersOpt( 'relation' ); |
| 368 | if( empty($relationFilter) ) $relationFilter = 'AND'; |
| 369 | ?> |
| 370 | <div class='border border-inherit p-7 relative max-w-xs delop-filter'> |
| 371 | <span class="dashicons dashicons-forms text-4xl mb-4"></span> |
| 372 | <?php |
| 373 | $helpTxt = sprintf( esc_html__( 'Choose the relation applied to terms. By default (no relation selected), %s will be used. "All selected terms" mean that the deleted post will need to have relation with all selected terms. "Any selected 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>' ); |
| 374 | $this->generateHelpTooltip( $helpTxt, 'w-full text-right absolute right-1 top-1', 'text-left right-1' ); |
| 375 | ?> |
| 376 | <div class='m-2'> |
| 377 | <?php |
| 378 | echo "<div class='text-base'>"; esc_html_e( 'Choose the relation applied to taxonomies terms:', 'delete-old-posts' ); echo "</div>"; |
| 379 | echo "<label class='block my-2'><input type='radio' name='relation' value='AND'"; if( isset($relationFilter) && $relationFilter == 'AND' ) echo "checked"; echo "/>".esc_html__("All selected terms", "delete-old-posts")."</label>"; |
| 380 | echo "<label class='block my-2'><input type='radio' name='relation' value='OR'"; if( isset($relationFilter) && $relationFilter == 'OR' ) echo "checked"; echo "/>".esc_html__("Any selected terms", "delete-old-posts")."</label>"; |
| 381 | ?> |
| 382 | </div> |
| 383 | </div> |
| 384 | <?php |
| 385 | break; |
| 386 | case 'postids': ?> |
| 387 | <div class="border border-inherit p-7 relative max-w-xs delop-filter"> |
| 388 | <span class="dashicons dashicons-portfolio text-4xl mb-4"></span> |
| 389 | <?php |
| 390 | // get saved post ids if form was submited |
| 391 | $postids = $this->getFiltersOpt( 'postids' ); |
| 392 | $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>' ); |
| 393 | $this->generateHelpTooltip( $helpTxt, 'w-full text-right absolute right-1 top-1', 'text-left right-1' ); |
| 394 | ?> |
| 395 | <div class="m-2"> |
| 396 | <?php |
| 397 | echo "<div class='text-base'>"; esc_html_e( 'Have some important posts? Enter the post IDs that you want to keep:', 'delete-old-posts' ); echo "</div>"; |
| 398 | ?> |
| 399 | <label class="block my-2 mt-8"> |
| 400 | <span><?php esc_html_e("Post IDs to exclude:", "delete-old-posts" )?></span> |
| 401 | <input class="w-full" type="text" name="postids" value="<?php echo sanitize_text_field( $postids ); ?>" /> |
| 402 | </label> |
| 403 | </div> |
| 404 | </div> |
| 405 | <?php |
| 406 | break; |
| 407 | case 'search_keywords': ?> |
| 408 | <div class="border border-inherit p-7 relative max-w-xs delop-filter"> |
| 409 | <span class="dashicons dashicons-search text-4xl mb-4"></span> |
| 410 | <?php |
| 411 | // get saved post ids if form was submited |
| 412 | $search_keyword = $this->getFiltersOpt( 'search_keyword' ); |
| 413 | $search_keyword_negativ = $this->getFiltersOpt( 'search_keyword_negativ' ); |
| 414 | $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>' ); |
| 415 | $this->generateHelpTooltip( $helpTxt, 'w-full text-right absolute right-1 top-1', 'text-left right-1' ); |
| 416 | ?> |
| 417 | <div class="m-2"> |
| 418 | <?php |
| 419 | echo "<div class='text-base'>"; esc_html_e( 'Delete only posts that contain the keyword(s):', 'delete-old-posts' ); echo "</div>"; |
| 420 | ?> |
| 421 | <label class="block my-2"> |
| 422 | <span><?php esc_html_e("Look for posts that include the keyword(s):", "delete-old-posts" )?></span> |
| 423 | <input class="w-full" type="text" name="search_keyword" value="<?php echo sanitize_text_field( $search_keyword ); ?>" /> |
| 424 | </label> |
| 425 | <label class="block my-2"> |
| 426 | <span><?php esc_html_e("Exclude from search results any posts that contain the keyword(s):", "delete-old-posts" )?></span> |
| 427 | <input class="w-full" type="text" name="search_keyword_negativ" value="<?php echo sanitize_text_field( $search_keyword_negativ ); ?>" /> |
| 428 | </label> |
| 429 | </div> |
| 430 | </div> |
| 431 | <?php |
| 432 | break; |
| 433 | case 'attached_img': ?> |
| 434 | <div class="border border-inherit p-7 relative max-w-xs delop-filter"> |
| 435 | <span class="dashicons dashicons-images-alt2 text-4xl mb-4"></span> |
| 436 | <?php |
| 437 | // get saved attached_img option if form was submited |
| 438 | $attached_img = $this->getFiltersOpt( 'attached_img' ); |
| 439 | $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 being used by some other post.', 'delete-old-posts' ), '<strong>'.esc_html__("featured image and all files", "delete-old-posts").'</strong>' ); |
| 440 | $this->generateHelpTooltip( $helpTxt, 'w-full text-right absolute right-1 top-1', 'text-left right-1' ); |
| 441 | ?> |
| 442 | <div class="m-2"> |
| 443 | <?php |
| 444 | echo "<div class='text-base'>"; 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?', 'delete-old-posts' ); echo "</div>"; |
| 445 | ?> |
| 446 | <label class="block my-2"> |
| 447 | <span class="font-bold"> |
| 448 | <?php esc_html_e("Delete all files attached to post:", "delete-old-posts" )?> |
| 449 | </span> |
| 450 | <input class="w-full" type="checkbox" name="attached_img" value="1" <?php if ( ! $dop_fs->can_use_premium_code() ) echo "disabled"; ?> <?php if( $attached_img == 1 ) echo "checked"; ?> /> |
| 451 | </label> |
| 452 | <?php |
| 453 | if ( ! $dop_fs->can_use_premium_code() ) { |
| 454 | echo ' |
| 455 | <section class="text-red-600">' . esc_html__('This option is available in the Professional version.', 'delete-old-posts'); |
| 456 | echo ' |
| 457 | <a href="' . $dop_fs->get_upgrade_url() . '">' . |
| 458 | '<u>' .esc_html__('Upgrade and activate it.', 'delete-old-posts') . '</u>' . |
| 459 | '</a>'; |
| 460 | echo ' |
| 461 | </section>'; |
| 462 | } |
| 463 | ?> |
| 464 | </div> |
| 465 | </div> |
| 466 | <?php |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * try the filter and see the results |
| 473 | */ |
| 474 | function tryFilter(){ |
| 475 | |
| 476 | global $dop_fs; |
| 477 | |
| 478 | // get the posts to delete |
| 479 | $nrOfPostsToGet = $this->getNumberOfPosts(); |
| 480 | $altPostsArray = $this->getAltestPostsObj($nrOfPostsToGet); |
| 481 | ?> |
| 482 | <div class="p-5 bg-amber-50"> |
| 483 | <div class="text-base mb-2"> |
| 484 | <?php |
| 485 | ( ! empty($altPostsArray) ) ? esc_html_e('The following ', 'delete-old-posts') : esc_html_e('No ', 'delete-old-posts'); |
| 486 | esc_html_e("posts will be automatically deleted when the next scheduled cron job runs.", "delete-old-posts"); |
| 487 | // check if strat deleteing the post option is on off, then show a message to set it on on |
| 488 | $toggledelete = false; |
| 489 | $getOptionObject = get_option('deloldp-post-days-option'); // get user saved options |
| 490 | if ( is_object($getOptionObject) && property_exists($getOptionObject,'params') ){ |
| 491 | if( property_exists($getOptionObject->params,'toggledelete' ) ){ |
| 492 | if ( $getOptionObject->params->toggledelete == 1 ) $toggledelete = true; |
| 493 | } |
| 494 | } |
| 495 | if( ! $toggledelete ) 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"); |
| 496 | ?> |
| 497 | </div> |
| 498 | <?php |
| 499 | // get saved attached_img option |
| 500 | $attached_img_opt = $this->getFiltersOpt( 'attached_img' ); |
| 501 | foreach( $altPostsArray as $altPostDataObj ){ |
| 502 | if( is_object($altPostDataObj) ) { |
| 503 | echo 'Post ' . 'ID <a href="'. get_permalink($altPostDataObj->ID) . '" target="_blank">' . $altPostDataObj->ID .'</a> - '. $altPostDataObj->post_title . ' (published: ' . date("d M Y H:i", strtotime($altPostDataObj->post_date)) . ')<br />'; |
| 504 | } |
| 505 | /** |
| 506 | * display a list with deleted attachments |
| 507 | */ |
| 508 | if ( $dop_fs->can_use_premium_code() ) { |
| 509 | if( $attached_img_opt == 1 ) { |
| 510 | echo " |
| 511 | <ul class='list-disc ml-4 mt-2'>"; |
| 512 | $get_all_deleted_attached_media = $this->delopt_delete_all_attached_media( $altPostDataObj->ID, false ); |
| 513 | if( is_array($get_all_deleted_attached_media) ) foreach( $get_all_deleted_attached_media as $deleted_attachment_guid ){ |
| 514 | echo "<li><span class='dashicons dashicons-format-image align-middle text-red-300 text-sm'></span>"; echo " <a href='".$deleted_attachment_guid."' target='_blank'>" . $deleted_attachment_guid . "</a>"; echo "</li>"; |
| 515 | } |
| 516 | echo " |
| 517 | </ul>"; |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | ?> |
| 522 | </div> |
| 523 | <?php |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * count how many elemnts before a target is reached in an array |
| 528 | * @param $haystack - array to search |
| 529 | * @param $target - value to search |
| 530 | */ |
| 531 | function countArrayUntilTarget( $haystack, $target ){ |
| 532 | |
| 533 | $total = 0; |
| 534 | |
| 535 | // check if target exists in array |
| 536 | if( ! array_search($target, $haystack) ) return 0; |
| 537 | |
| 538 | // if target exists count the array elements until target |
| 539 | foreach($haystack as $key => $value) { |
| 540 | if ($value == $target) { |
| 541 | break; |
| 542 | } |
| 543 | |
| 544 | $total++; |
| 545 | } |
| 546 | |
| 547 | return $total; |
| 548 | } |
| 549 | |
| 550 | } |
| 551 | ?> |