| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace BulkWP\BulkDelete\Core\Posts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use BulkWP\BulkDelete\Core\Base\BaseModule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 | 1 |  | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Module for deleting posts. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @since 6.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | abstract class PostsModule extends BaseModule { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	 * Build query params for WP_Query by using delete options. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 * Return an empty query array to short-circuit deletion. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 * @param array $options Delete options. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * @return array Query. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	abstract protected function build_query( $options ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	protected $item_type = 'posts'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * Handle common filters. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 * @param array $request Request array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 * @return array User options. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	protected function parse_common_filters( $request ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		$options = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		$options['restrict']     = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_restrict', false ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		$options['limit_to']     = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_limit_to', 0 ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		$options['force_delete'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_force_delete', false ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		$options['date_op'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_op' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		$options['days']    = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_days' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		return $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	public function filter_js_array( $js_array ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		$js_array['msg']['deletePostsWarning'] = __( 'Are you sure you want to delete all the posts based on the selected option?', 'bulk-delete' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		$js_array['msg']['selectPostOption']   = __( 'Please select posts from at least one option', 'bulk-delete' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$js_array['validators']['delete_posts_by_category'] = 'validateSelect2'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$js_array['error_msg']['delete_posts_by_category']  = 'selectCategory'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		$js_array['msg']['selectCategory']                  = __( 'Please select at least one category', 'bulk-delete' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		$js_array['validators']['delete_posts_by_tag']     = 'validateSelect2'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$js_array['error_msg']['delete_posts_by_category'] = 'selectTag'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		$js_array['msg']['selectTag']                      = __( 'Please select at least one tag', 'bulk-delete' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		$js_array['validators']['delete_posts_by_url'] = 'validateUrl'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		$js_array['error_msg']['delete_posts_by_url']  = 'enterUrl'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$js_array['msg']['enterUrl']                   = __( 'Please enter at least one post url', 'bulk-delete' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		$js_array['dt_iterators'][] = '_cats'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		$js_array['dt_iterators'][] = '_tags'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		$js_array['dt_iterators'][] = '_taxs'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		$js_array['dt_iterators'][] = '_post_status'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		return $js_array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 * Helper function to build the query params. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * @param array $options Delete Options. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * @param array $query   Params for WP Query. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 * @return array Delete options array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 65 |  | 	protected function build_query_options( $options, $query ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 65 |  | 		return bd_build_query_options( $options, $query ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	 * Helper function for bd_query which runs query. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 * @param array $query Params for WP Query. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * @return array Deleted Post IDs array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 65 |  | 	protected function query( $query ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 65 |  | 		return bd_query( $query ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 56 |  | 	protected function do_delete( $options ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 56 |  | 		$query = $this->build_query( $options ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 56 |  | 		if ( empty( $query ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 			// Short circuit deletion, if nothing needs to be deleted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 56 |  | 		return $this->delete_posts_from_query( $query, $options ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	 * Build the query using query params and then Delete posts. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	 * @param array $query   Params for WP Query. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	 * @param array $options Delete Options. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 * @return int Number of posts deleted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 65 |  | 	protected function delete_posts_from_query( $query, $options ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 65 |  | 		$query    = $this->build_query_options( $options, $query ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 65 |  | 		$post_ids = $this->query( $query ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 65 |  | 		return $this->delete_posts_by_id( $post_ids, $options['force_delete'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	 * Render the "private post" setting fields. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  | 	protected function render_private_post_settings() { | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  | 		if( $this->are_private_posts_present() ){ | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  | 			bd_render_private_post_settings( $this->field_slug ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	 * Delete sticky posts. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	 * @param bool $force_delete Whether to bypass trash and force deletion. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	 * @return int Number of posts deleted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	protected function delete_sticky_posts( $force_delete ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		$sticky_post_ids = get_option( 'sticky_posts' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		if ( ! is_array( $sticky_post_ids ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 			return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		return $this->delete_posts_by_id( $sticky_post_ids, $force_delete ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	 * Delete posts by ids. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	 * @param int[] $post_ids     List of post ids to delete. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	 * @param bool  $force_delete True to force delete posts, False otherwise. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	 * @return int Number of posts deleted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 74 |  | 	protected function delete_posts_by_id( $post_ids, $force_delete ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 74 |  | 		foreach ( $post_ids as $post_id ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 			// `$force_delete` parameter to `wp_delete_post` won't work for custom post types. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 			// See https://core.trac.wordpress.org/ticket/43672 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 72 |  | 			if ( $force_delete ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 22 |  | 				wp_delete_post( $post_id, true ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 72 |  | 				wp_trash_post( $post_id ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 74 |  | 		return count( $post_ids ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 167 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |  |